nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 autoAddDriverRunpath,
3 backendStdenv,
4 _cuda,
5 cmake,
6 cuda_cccl,
7 cuda_culibos,
8 cuda_cudart,
9 cuda_nvcc,
10 cuda_nvrtc,
11 cuda_nvtx,
12 cuda_profiler_api,
13 cudaAtLeast,
14 cudaNamePrefix,
15 cudaOlder,
16 fetchFromGitHub,
17 flags,
18 lib,
19 libcublas,
20 libcudla,
21 libcufft,
22 libcurand,
23 libcusolver,
24 libcusparse,
25 libnpp,
26 libnvjitlink,
27 libnvjpeg,
28}:
29backendStdenv.mkDerivation (finalAttrs: {
30 __structuredAttrs = true;
31 strictDeps = true;
32
33 name = "${cudaNamePrefix}-${finalAttrs.pname}-${finalAttrs.version}";
34 pname = "cuda-samples";
35 version = if cudaAtLeast "13" then "13.0" else "12.8";
36
37 # We should be able to use samples from the latest version of CUDA
38 # on most of the CUDA package sets we have.
39 # Plus, 12.8 and later are rewritten to use CMake which makes it much, much easier to build.
40 src = fetchFromGitHub {
41 owner = "NVIDIA";
42 repo = "cuda-samples";
43 tag = "v${finalAttrs.version}";
44 hash = lib.getAttr finalAttrs.version {
45 "13.0" = "sha256-bOcAE/OzOI6MWTh+bFZfq1en6Yawu+HI8W+xK+XaCqg=";
46 "12.8" = "sha256-Ba0Fi0v/sQ+1iJ4mslgyIAE+oK5KO0lMoTQCC91vpiA=";
47 };
48 };
49
50 prePatch =
51 let
52 samplesAtLeast = lib.versionAtLeast finalAttrs.version;
53 samplesOlder = lib.versionOlder finalAttrs.version;
54 in
55 # https://github.com/NVIDIA/cuda-samples/issues/333
56 ''
57 nixLog "removing sample 0_Introduction/UnifiedMemoryStreams which requires OpenMP support for CUDA"
58 substituteInPlace \
59 "$NIX_BUILD_TOP/$sourceRoot/Samples/0_Introduction/CMakeLists.txt" \
60 --replace-fail \
61 'add_subdirectory(UnifiedMemoryStreams)' \
62 '# add_subdirectory(UnifiedMemoryStreams)'
63 ''
64 # This sample tries to use a relative path, which doesn't work for our splayed installation.
65 + lib.optionalString (samplesOlder "13") ''
66 nixLog "patching sample 0_Introduction/matrixMul_nvrtc"
67 substituteInPlace \
68 "$NIX_BUILD_TOP/$sourceRoot/Samples/0_Introduction/matrixMul_nvrtc/CMakeLists.txt" \
69 --replace-fail \
70 "\''${CUDAToolkit_BIN_DIR}/../include/cooperative_groups" \
71 "${lib.getOutput "include" cuda_cudart}/include/cooperative_groups" \
72 --replace-fail \
73 "\''${CUDAToolkit_BIN_DIR}/../include/nv" \
74 "${lib.getOutput "include" cuda_cccl}/include/nv" \
75 --replace-fail \
76 "\''${CUDAToolkit_BIN_DIR}/../include/cuda" \
77 "${lib.getOutput "include" cuda_cccl}/include/cuda"
78 ''
79 + lib.optionalString (samplesAtLeast "13") ''
80 nixLog "patching sample 0_Introduction/matrixMul_nvrtc"
81 substituteInPlace \
82 "$NIX_BUILD_TOP/$sourceRoot/Samples/0_Introduction/matrixMul_nvrtc/CMakeLists.txt" \
83 --replace-fail \
84 "\''${CUDA_INCLUDE_DIR}/cooperative_groups" \
85 "${lib.getOutput "include" cuda_cudart}/include/cooperative_groups" \
86 --replace-fail \
87 "\''${CUDA_INCLUDE_DIR}/cccl/nv" \
88 "${lib.getOutput "include" cuda_cccl}/include/nv" \
89 --replace-fail \
90 "\''${CUDA_INCLUDE_DIR}/cccl/cuda" \
91 "${lib.getOutput "include" cuda_cccl}/include/cuda"
92 ''
93 # These three samples give undefined references, like
94 # nvlink error : Undefined reference to '__cudaCDP2Free' in 'CMakeFiles/cdpBezierTessellation.dir/BezierLineCDP.cu.o'
95 # nvlink error : Undefined reference to '__cudaCDP2Malloc' in 'CMakeFiles/cdpBezierTessellation.dir/BezierLineCDP.cu.o'
96 # nvlink error : Undefined reference to '__cudaCDP2GetParameterBufferV2' in 'CMakeFiles/cdpBezierTessellation.dir/BezierLineCDP.cu.o'
97 # nvlink error : Undefined reference to '__cudaCDP2LaunchDeviceV2' in 'CMakeFiles/cdpBezierTessellation.dir/BezierLineCDP.cu.o'
98 + ''
99 for sample in cdp{AdvancedQuicksort,BezierTessellation,Quadtree,SimplePrint,SimpleQuicksort}; do
100 nixLog "removing sample 3_CUDA_Features/$sample which fails to link"
101 substituteInPlace \
102 "$NIX_BUILD_TOP/$sourceRoot/Samples/3_CUDA_Features/CMakeLists.txt" \
103 --replace-fail \
104 "add_subdirectory($sample)" \
105 "# add_subdirectory($sample)"
106 done
107 unset -v sample
108 ''
109 + lib.optionalString (cudaOlder "12.4") ''
110 nixLog "removing sample 3_CUDA_Features/graphConditionalNodes which requires at least CUDA 12.4"
111 substituteInPlace \
112 "$NIX_BUILD_TOP/$sourceRoot/Samples/3_CUDA_Features/CMakeLists.txt" \
113 --replace-fail \
114 "add_subdirectory(graphConditionalNodes)" \
115 "# add_subdirectory(graphConditionalNodes)"
116 ''
117 # For some reason this sample requires a static library, which we don't propagate by default due to size.
118 + ''
119 nixLog "patching sample 4_CUDA_Libraries/simpleCUFFT_callback to use dynamic library"
120 substituteInPlace \
121 "$NIX_BUILD_TOP/$sourceRoot/Samples/4_CUDA_Libraries/simpleCUFFT_callback/CMakeLists.txt" \
122 --replace-fail \
123 'CUDA::cufft_static' \
124 'CUDA::cufft'
125 ''
126 # Patch to use the correct path to libnvJitLink.so, or disable the sample if older than 12.4.
127 + lib.optionalString (cudaOlder "12.4") ''
128 nixLog "removing sample 4_CUDA_Libraries/jitLto which requires at least CUDA 12.4"
129 substituteInPlace \
130 "$NIX_BUILD_TOP/$sourceRoot/Samples/4_CUDA_Libraries/CMakeLists.txt" \
131 --replace-fail \
132 "add_subdirectory(jitLto)" \
133 "# add_subdirectory(jitLto)"
134 ''
135 + lib.optionalString (samplesOlder "13") ''
136 nixLog "patching sample 4_CUDA_Libraries/jitLto to use correct path to libnvJitLink.so"
137 substituteInPlace \
138 "$NIX_BUILD_TOP/$sourceRoot/Samples/4_CUDA_Libraries/jitLto/CMakeLists.txt" \
139 --replace-fail \
140 "\''${CUDAToolkit_LIBRARY_DIR}/libnvJitLink.so" \
141 "${lib.getLib libnvjitlink}/lib/libnvJitLink.so"
142 ''
143 # /build/NVIDIA-cuda-samples-v12.8/Samples/4_CUDA_Libraries/watershedSegmentationNPP/watershedSegmentationNPP.cpp:272:80: error: cannot convert 'size_t*' {aka 'long unsigned int*'} to 'int*'
144 # 272 | nppStatus = nppiSegmentWatershedGetBufferSize_8u_C1R(oSizeROI[nImage], &aSegmentationScratchBufferSize[nImage]);
145 # | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146 # | |
147 # | size_t* {aka long unsigned int*}
148 + lib.optionalString (cudaOlder "12.8") ''
149 nixLog "removing sample 4_CUDA_Libraries/watershedSegmentationNPP which requires at least CUDA 12.8"
150 substituteInPlace \
151 "$NIX_BUILD_TOP/$sourceRoot/Samples/4_CUDA_Libraries/CMakeLists.txt" \
152 --replace-fail \
153 "add_subdirectory(watershedSegmentationNPP)" \
154 "# add_subdirectory(watershedSegmentationNPP)"
155 ''
156 # NVVM samples require a specific build of LLVM, which is a hassle.
157 + ''
158 nixLog "removing samples 7_libNVVM which require a specific build of LLVM"
159 substituteInPlace \
160 "$NIX_BUILD_TOP/$sourceRoot/Samples/CMakeLists.txt" \
161 --replace-fail \
162 'add_subdirectory(7_libNVVM)' \
163 '# add_subdirectory(7_libNVVM)'
164 ''
165 # Don't use hard-coded CUDA architectures
166 + ''
167 nixLog "patching CMakeLists.txt to use provided CUDA architectures"
168 local path=""
169 while IFS= read -r -d $'\0' path; do
170 nixLog "removing CMAKE_CUDA_ARCHITECTURES declaration from $path"
171 substituteInPlace \
172 "$path" \
173 --replace-fail \
174 'set(CMAKE_CUDA_ARCHITECTURES' \
175 '# set(CMAKE_CUDA_ARCHITECTURES'
176 done < <(grep --files-with-matches --null "set(CMAKE_CUDA_ARCHITECTURES" --recursive "$NIX_BUILD_TOP/$sourceRoot")
177 unset -v path
178 '';
179
180 nativeBuildInputs = [
181 autoAddDriverRunpath
182 cmake
183 cuda_nvcc
184 ];
185
186 buildInputs = [
187 cuda_cccl
188 cuda_cudart
189 cuda_nvrtc
190 cuda_nvtx
191 cuda_profiler_api
192 libcublas
193 libcufft
194 libcurand
195 libcusolver
196 libcusparse
197 libnpp
198 libnvjitlink
199 libnvjpeg
200 ]
201 ++ lib.optionals libcudla.meta.available [ libcudla ]
202 ++ lib.optionals (cudaAtLeast "13") [ cuda_culibos ];
203
204 cmakeFlags = [
205 (lib.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" flags.cmakeCudaArchitecturesString)
206 (lib.cmakeBool "BUILD_TEGRA" backendStdenv.hasJetsonCudaCapability)
207 ];
208
209 # TODO(@connorbaker):
210 # For some reason, using the combined find command doesn't delete directories:
211 # find "$PWD/Samples" \
212 # \( -type d -name CMakeFiles \) \
213 # -o \( -type f -name cmake_install.cmake \) \
214 # -o \( -type f -name Makefile \) \
215 # -exec rm -rf {} +
216 installPhase = ''
217 runHook preInstall
218
219 pushd "$NIX_BUILD_TOP/$sourceRoot/''${cmakeBuildDir:?}" >/dev/null
220
221 nixLog "deleting CMake related files"
222
223 find "$PWD/Samples" -type d -name CMakeFiles -exec rm -rf {} +
224 find "$PWD/Samples" -type f -name cmake_install.cmake -exec rm -rf {} +
225 find "$PWD/Samples" -type f -name Makefile -exec rm -rf {} +
226
227 nixLog "copying $PWD/Samples to $out/"
228 mkdir -p "$out"
229 cp -rv "$PWD/Samples"/* "$out/"
230
231 popd >/dev/null
232
233 runHook postInstall
234 '';
235
236 meta = {
237 description = "Samples for CUDA Developers which demonstrates features in CUDA Toolkit";
238 homepage = "https://github.com/NVIDIA/cuda-samples";
239 license = lib.licenses.nvidiaCuda;
240 platforms = [
241 "aarch64-linux"
242 "x86_64-linux"
243 ];
244 maintainers = [ lib.maintainers.connorbaker ];
245 teams = [ lib.teams.cuda ];
246 };
247})