From d4c2507dfe31f0d5c2b3455cff0f226805c1b5b2 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 14:08:12 +0000 Subject: [PATCH 1/6] magma: remove unused callPackage argument --- pkgs/development/libraries/science/math/magma/generic.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index 7ef60e0418ed..ad58a021830b 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -25,7 +25,6 @@ , rocmSupport ? !cudaSupport , static ? stdenv.hostPlatform.isStatic , stdenv -, symlinkJoin }: From be7e7209885cfb7a6016afea29de44730e85776c Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 14:10:07 +0000 Subject: [PATCH 2/6] magma: switch to cudaOlder and cudaAtLeast --- .../libraries/science/math/magma/generic.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index ad58a021830b..5c195db04bad 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -30,7 +30,7 @@ let inherit (lib) lists strings trivial; - inherit (cudaPackages) backendStdenv cudaFlags cudaVersion; + inherit (cudaPackages) backendStdenv cudaAtLeast cudaFlags cudaOlder; inherit (magmaRelease) version hash supportedGpuTargets; # NOTE: The lists.subtractLists function is perhaps a bit unintuitive. It subtracts the elements @@ -114,11 +114,11 @@ stdenv.mkDerivation { libcublas.lib # cublas libcusparse.dev # cusparse.h libcusparse.lib # cusparse - ] ++ lists.optionals (strings.versionOlder cudaVersion "11.8") [ + ] ++ lists.optionals (cudaOlder "11.8") [ cuda_nvprof.dev # - ] ++ lists.optionals (strings.versionAtLeast cudaVersion "11.8") [ + ] ++ lists.optionals (cudaAtLeast "11.8") [ cuda_profiler_api.dev # - ] ++ lists.optionals (strings.versionAtLeast cudaVersion "12.0") [ + ] ++ lists.optionals (cudaAtLeast "12.0") [ cuda_cccl.dev # ]) ++ lists.optionals rocmSupport [ rocmPackages.clr @@ -167,6 +167,6 @@ stdenv.mkDerivation { broken = !(cudaSupport || rocmSupport) # At least one back-end enabled || (cudaSupport && rocmSupport) # Mutually exclusive - || (cudaSupport && strings.versionOlder cudaVersion "9"); + || (cudaSupport && cudaOlder "9.0"); }; } From 9d4ac33716fc1f3f3626dcd3b0f0bfe771fdbc16 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 14:14:12 +0000 Subject: [PATCH 3/6] magma: switch to strings.cmake* functions --- .../libraries/science/math/magma/generic.nix | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index 5c195db04bad..c337f0e78529 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -128,21 +128,18 @@ stdenv.mkDerivation { ]; cmakeFlags = [ - "-DGPU_TARGET=${gpuTargetString}" - (lib.cmakeBool "MAGMA_ENABLE_CUDA" cudaSupport) - (lib.cmakeBool "MAGMA_ENABLE_HIP" rocmSupport) - ] ++ lists.optionals static [ - "-DBUILD_SHARED_LIBS=OFF" + (strings.cmakeFeature "GPU_TARGET" gpuTargetString) + (strings.cmakeBool "MAGMA_ENABLE_CUDA" cudaSupport) + (strings.cmakeBool "MAGMA_ENABLE_HIP" rocmSupport) + (strings.cmakeBool "BUILD_SHARED_LIBS" (!static)) ] ++ lists.optionals cudaSupport [ - "-DCMAKE_CUDA_ARCHITECTURES=${cudaArchitecturesString}" - "-DMIN_ARCH=${minArch}" # Disarms magma's asserts - "-DCMAKE_C_COMPILER=${backendStdenv.cc}/bin/cc" - "-DCMAKE_CXX_COMPILER=${backendStdenv.cc}/bin/c++" + (strings.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaArchitecturesString) + (strings.cmakeFeature "MIN_ARCH" minArch) # Disarms magma's asserts + (strings.cmakeFeature "CMAKE_C_COMPILER" "${backendStdenv.cc}/bin/cc") + (strings.cmakeFeature "CMAKE_CXX_COMPILER" "${backendStdenv.cc}/bin/c++") ] ++ lists.optionals rocmSupport [ - "-DCMAKE_C_COMPILER=${rocmPackages.clr}/bin/hipcc" - "-DCMAKE_CXX_COMPILER=${rocmPackages.clr}/bin/hipcc" - ] ++ lists.optionals (cudaPackages.cudaAtLeast "12.0.0") [ - (lib.cmakeBool "USE_FORTRAN" false) + (strings.cmakeFeature "CMAKE_C_COMPILER" "${rocmPackages.clr}/bin/hipcc") + (strings.cmakeFeature "CMAKE_CXX_COMPILER" "${rocmPackages.clr}/bin/hipcc") ]; buildFlags = [ From 0974463f47d3eacbe14aa7109155a4192ecb378a Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 14:14:36 +0000 Subject: [PATCH 4/6] magma: specify Fortran name mangling scheme --- pkgs/development/libraries/science/math/magma/generic.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index c337f0e78529..e257ade74481 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -132,6 +132,13 @@ stdenv.mkDerivation { (strings.cmakeBool "MAGMA_ENABLE_CUDA" cudaSupport) (strings.cmakeBool "MAGMA_ENABLE_HIP" rocmSupport) (strings.cmakeBool "BUILD_SHARED_LIBS" (!static)) + # Set the Fortran name mangling scheme explicitly. We must set FORTRAN_CONVENTION manually because it will + # otherwise not be set in NVCC_FLAGS or DEVCCFLAGS (which we cannot modify). + # See https://github.com/NixOS/nixpkgs/issues/281656#issuecomment-1902931289 + (strings.cmakeBool "USE_FORTRAN" true) + (strings.cmakeFeature "CMAKE_C_FLAGS" "-DADD_") + (strings.cmakeFeature "CMAKE_CXX_FLAGS" "-DADD_") + (strings.cmakeFeature "FORTRAN_CONVENTION" "-DADD_") ] ++ lists.optionals cudaSupport [ (strings.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaArchitecturesString) (strings.cmakeFeature "MIN_ARCH" minArch) # Disarms magma's asserts From 2898a9c4d747d3e6b27d37424e46e57e45470f40 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 23:28:00 +0000 Subject: [PATCH 5/6] magma: CUDA setup hook handles setting CC/CXX --- pkgs/development/libraries/science/math/magma/generic.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index e257ade74481..5cb30d94b270 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -30,7 +30,7 @@ let inherit (lib) lists strings trivial; - inherit (cudaPackages) backendStdenv cudaAtLeast cudaFlags cudaOlder; + inherit (cudaPackages) cudaAtLeast cudaFlags cudaOlder; inherit (magmaRelease) version hash supportedGpuTargets; # NOTE: The lists.subtractLists function is perhaps a bit unintuitive. It subtracts the elements @@ -142,8 +142,6 @@ stdenv.mkDerivation { ] ++ lists.optionals cudaSupport [ (strings.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaArchitecturesString) (strings.cmakeFeature "MIN_ARCH" minArch) # Disarms magma's asserts - (strings.cmakeFeature "CMAKE_C_COMPILER" "${backendStdenv.cc}/bin/cc") - (strings.cmakeFeature "CMAKE_CXX_COMPILER" "${backendStdenv.cc}/bin/c++") ] ++ lists.optionals rocmSupport [ (strings.cmakeFeature "CMAKE_C_COMPILER" "${rocmPackages.clr}/bin/hipcc") (strings.cmakeFeature "CMAKE_CXX_COMPILER" "${rocmPackages.clr}/bin/hipcc") From 4327d0aead1cbb282c9bc242e13ca9c33704a107 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 25 Jan 2024 23:31:39 +0000 Subject: [PATCH 6/6] magma: add test output for GPU testing --- .../libraries/science/math/magma/generic.nix | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/pkgs/development/libraries/science/math/magma/generic.nix b/pkgs/development/libraries/science/math/magma/generic.nix index 5cb30d94b270..1c63fa8a908c 100644 --- a/pkgs/development/libraries/science/math/magma/generic.nix +++ b/pkgs/development/libraries/science/math/magma/generic.nix @@ -5,7 +5,8 @@ # supportedGpuTargets: List String # } -{ blas +{ autoPatchelfHook +, blas , cmake , cudaPackages , cudaSupport ? config.cudaSupport @@ -19,6 +20,7 @@ , libpthreadstubs , magmaRelease , ninja +, python3 , config # At least one back-end has to be enabled, # and we can't default to CUDA since it's unfree @@ -94,7 +96,21 @@ stdenv.mkDerivation { inherit hash; }; + # Magma doesn't have anything which could be run under doCheck, but it does build test suite executables. + # These are moved to $test/bin/ and $test/lib/ in postInstall. + outputs = ["out" "test"]; + + # Fixup for the python test runners + postPatch = '' + patchShebangs ./testing/run_{tests,summarize}.py + substituteInPlace ./testing/run_tests.py \ + --replace-fail \ + "print >>sys.stderr, cmdp, \"doesn't exist (original name: \" + cmd + \", precision: \" + precision + \")\"" \ + "print(f\"{cmdp} doesn't exist (original name: {cmd}, precision: {precision})\", file=sys.stderr)" + ''; + nativeBuildInputs = [ + autoPatchelfHook cmake ninja gfortran @@ -106,6 +122,7 @@ stdenv.mkDerivation { libpthreadstubs lapack blas + python3 ] ++ lists.optionals cudaSupport (with cudaPackages; [ cuda_cudart.dev # cuda_runtime.h cuda_cudart.lib # cudart @@ -147,13 +164,37 @@ stdenv.mkDerivation { (strings.cmakeFeature "CMAKE_CXX_COMPILER" "${rocmPackages.clr}/bin/hipcc") ]; - buildFlags = [ - "magma" - "magma_sparse" - ]; - + # Magma doesn't have a test suite we can easily run, just loose executables, all of which require a GPU. doCheck = false; + # Copy the files to the test output and fix the RPATHs. + postInstall = + # NOTE: The python scripts aren't copied by CMake into the build directory, so we must copy them from the source. + # TODO(@connorbaker): This should be handled by having CMakeLists.txt install them, but such a patch is + # out of the scope of the PR which introduces the `test` output: https://github.com/NixOS/nixpkgs/pull/283777. + # See https://github.com/NixOS/nixpkgs/pull/283777#discussion_r1482125034 for more information. + '' + install -Dm755 ../testing/run_{tests,summarize}.py -t "$test/bin/" + '' + # Copy core test executables and libraries over to the test output. + # NOTE: Magma doesn't provide tests for sparse solvers for ROCm, but it does for CUDA -- we put them both in the same + # install command to avoid the case where a glob would fail to find any files and cause the install command to fail + # because it has no files to install. + + '' + install -Dm755 ./testing/testing_* ./sparse/testing/testing_* -t "$test/bin/" + install -Dm755 ./lib/libtester.so ./lib/liblapacktest.so -t "$test/lib/" + '' + # All of the test executables and libraries will have a reference to the build directory in their RPATH, which we + # must remove. We do this by shrinking the RPATH to only include the Nix store. The autoPatchelfHook will take care + # of supplying the correct RPATH for needed libraries (like `libtester.so`). + + '' + find "$test" -type f -exec \ + patchelf \ + --shrink-rpath \ + --allowed-rpath-prefixes "$NIX_STORE" \ + {} \; + ''; + passthru = { inherit cudaPackages cudaSupport rocmSupport gpuTargets; };