-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
cudaPackages.setupCudaHook: init #233581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
cudaPackages.setupCudaHook: init #233581
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f46beb
cudaPackages.setupCudaHook: init
SomeoneSerge bfb24ac
cudaPackages_10.cudatoolkit: fix infinite recursion in setupCudaHook
SomeoneSerge 4df8614
magma: symlinkJoin -> CUDAToolkit_ROOT
SomeoneSerge 251d316
cudaPackages.saxpy: init at unstable-2023-07-11
SomeoneSerge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
pkgs/development/compilers/cudatoolkit/hooks/mark-for-cudatoolkit-root-hook.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # shellcheck shell=bash | ||
|
|
||
| markForCUDAToolkit_ROOT() { | ||
| mkdir -p "${prefix}/nix-support" | ||
| touch "${prefix}/nix-support/include-in-cudatoolkit-root" | ||
| } | ||
|
|
||
| fixupOutputHooks+=(markForCUDAToolkit_ROOT) |
5 changes: 5 additions & 0 deletions
5
pkgs/development/compilers/cudatoolkit/hooks/nvcc-setup-hook.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # shellcheck shell=bash | ||
|
|
||
| # CMake's enable_language(CUDA) runs a compiler test and it doesn't account for | ||
| # CUDAToolkit_ROOT. We have to help it locate libcudart | ||
| export NVCC_APPEND_FLAGS+=" -L@cudartRoot@/lib -I@cudartRoot@/include" |
68 changes: 68 additions & 0 deletions
68
pkgs/development/compilers/cudatoolkit/hooks/setup-cuda-hook.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # shellcheck shell=bash | ||
|
|
||
| echo Sourcing setup-cuda-hook >&2 | ||
|
|
||
| extendCUDAToolkit_ROOT() { | ||
| if [[ -f "$1/nix-support/include-in-cudatoolkit-root" ]] ; then | ||
| addToSearchPathWithCustomDelimiter ";" CUDAToolkit_ROOT "$1" | ||
|
|
||
| if [[ -d "$1/include" ]] ; then | ||
| addToSearchPathWithCustomDelimiter ";" CUDAToolkit_INCLUDE_DIR "$1/include" | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| addEnvHooks "$targetOffset" extendCUDAToolkit_ROOT | ||
|
|
||
| setupCUDAToolkitCompilers() { | ||
| echo Executing setupCUDAToolkitCompilers >&2 | ||
|
|
||
| if [[ -n "${dontSetupCUDAToolkitCompilers-}" ]] ; then | ||
| return | ||
| fi | ||
|
|
||
| # Point NVCC at a compatible compiler | ||
|
|
||
| # For CMake-based projects: | ||
| # https://cmake.org/cmake/help/latest/module/FindCUDA.html#input-variables | ||
| # https://cmake.org/cmake/help/latest/envvar/CUDAHOSTCXX.html | ||
| # https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_HOST_COMPILER.html | ||
|
|
||
| export cmakeFlags+=" -DCUDA_HOST_COMPILER=@ccFullPath@" | ||
| export cmakeFlags+=" -DCMAKE_CUDA_HOST_COMPILER=@ccFullPath@" | ||
|
|
||
| # For non-CMake projects: | ||
| # We prepend --compiler-bindir to nvcc flags. | ||
| # Downstream packages can override these, because NVCC | ||
| # uses the last --compiler-bindir it gets on the command line. | ||
| # FIXME: this results in "incompatible redefinition" warnings. | ||
| # https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#compiler-bindir-directory-ccbin | ||
| if [ -z "${CUDAHOSTCXX-}" ]; then | ||
| export CUDAHOSTCXX="@ccFullPath@"; | ||
| fi | ||
|
|
||
| export NVCC_PREPEND_FLAGS+=" --compiler-bindir=@ccRoot@/bin" | ||
|
|
||
| # NOTE: We set -Xfatbin=-compress-all, which reduces the size of the compiled | ||
| # binaries. If binaries grow over 2GB, they will fail to link. This is a problem for us, as | ||
| # the default set of CUDA capabilities we build can regularly cause this to occur (for | ||
| # example, with Magma). | ||
| # | ||
| # @SomeoneSerge: original comment was made by @ConnorBaker in .../cudatoolkit/common.nix | ||
| if [[ -z "${dontCompressFatbin-}" ]]; then | ||
| export NVCC_PREPEND_FLAGS+=" -Xfatbin=-compress-all" | ||
| fi | ||
|
|
||
| # CMake's enable_language(CUDA) runs a compiler test and it doesn't account for | ||
| # CUDAToolkit_ROOT. We have to help it locate libcudart | ||
| if [[ -z "${nvccDontPrependCudartFlags-}" ]] ; then | ||
| export NVCC_APPEND_FLAGS+=" -L@cudartRoot@/lib -I@cudartRoot@/include" | ||
| fi | ||
| } | ||
|
|
||
| setupCMakeCUDAToolkit_ROOT() { | ||
| export cmakeFlags+=" -DCUDAToolkit_INCLUDE_DIR=$CUDAToolkit_INCLUDE_DIR -DCUDAToolkit_ROOT=$CUDAToolkit_ROOT" | ||
| } | ||
|
|
||
| postHooks+=(setupCUDAToolkitCompilers) | ||
| preConfigureHooks+=(setupCMakeCUDAToolkit_ROOT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
pkgs/development/compilers/cudatoolkit/saxpy/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| cmake_minimum_required(VERSION 3.25) | ||
| project(saxpy LANGUAGES CXX CUDA) | ||
|
|
||
| find_package(CUDAToolkit REQUIRED COMPONENTS cudart cublas) | ||
|
|
||
| add_executable(saxpy saxpy.cu) | ||
| target_link_libraries(saxpy PUBLIC CUDA::cublas CUDA::cudart m) | ||
| target_compile_features(saxpy PRIVATE cxx_std_14) | ||
| target_compile_options(saxpy PRIVATE $<$<COMPILE_LANGUAGE:CUDA>: | ||
| --expt-relaxed-constexpr>) | ||
|
|
||
| install(TARGETS saxpy) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| { autoAddOpenGLRunpathHook | ||
| , backendStdenv | ||
| , cmake | ||
| , cuda_cccl | ||
| , cuda_cudart | ||
| , cudaFlags | ||
| , cuda_nvcc | ||
| , lib | ||
| , libcublas | ||
| , setupCudaHook | ||
| , stdenv | ||
| }: | ||
|
|
||
| backendStdenv.mkDerivation { | ||
| pname = "saxpy"; | ||
| version = "unstable-2023-07-11"; | ||
|
|
||
| src = ./.; | ||
|
|
||
| buildInputs = [ | ||
| libcublas | ||
| cuda_cudart | ||
| cuda_cccl | ||
| ]; | ||
| nativeBuildInputs = [ | ||
| cmake | ||
|
|
||
| # NOTE: this needs to be pkgs.buildPackages.cudaPackages_XX_Y.cuda_nvcc for | ||
| # cross-compilation to work. This should work automatically once we move to | ||
| # spliced scopes. Delete this comment once that happens | ||
| cuda_nvcc | ||
|
|
||
| # Alternatively, we could remove the propagated hook from cuda_nvcc and add | ||
| # directly: | ||
| # setupCudaHook | ||
| autoAddOpenGLRunpathHook | ||
| ]; | ||
|
|
||
| cmakeFlags = [ | ||
| "-DCMAKE_VERBOSE_MAKEFILE=ON" | ||
| "-DCMAKE_CUDA_ARCHITECTURES=${with cudaFlags; builtins.concatStringsSep ";" (map dropDot cudaCapabilities)}" | ||
| ]; | ||
|
|
||
| meta = { | ||
| description = "A simple (Single-precision AX Plus Y) FindCUDAToolkit.cmake example for testing cross-compilation"; | ||
| license = lib.licenses.mit; | ||
| maintainers = lib.teams.cuda.members; | ||
| platforms = lib.platforms.unix; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include <cublas_v2.h> | ||
| #include <cuda_runtime.h> | ||
| #include <vector> | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| static inline void check(cudaError_t err, const char *context) { | ||
| if (err != cudaSuccess) { | ||
| fprintf(stderr, "CUDA error at %s: %s\n", context, cudaGetErrorString(err)); | ||
| std::exit(EXIT_FAILURE); | ||
| } | ||
| } | ||
|
|
||
| #define CHECK(x) check(x, #x) | ||
|
|
||
| __global__ void saxpy(int n, float a, float *x, float *y) { | ||
| int i = blockIdx.x * blockDim.x + threadIdx.x; | ||
| if (i < n) | ||
| y[i] = a * x[i] + y[i]; | ||
| } | ||
|
|
||
| int main(void) { | ||
| setbuf(stderr, NULL); | ||
| fprintf(stderr, "Start\n"); | ||
|
|
||
| int rtVersion, driverVersion; | ||
| CHECK(cudaRuntimeGetVersion(&rtVersion)); | ||
| CHECK(cudaDriverGetVersion(&driverVersion)); | ||
|
|
||
| fprintf(stderr, "Runtime version: %d\n", rtVersion); | ||
| fprintf(stderr, "Driver version: %d\n", driverVersion); | ||
|
|
||
| constexpr int N = 1 << 10; | ||
|
|
||
| std::vector<float> xHost(N), yHost(N); | ||
| for (int i = 0; i < N; i++) { | ||
| xHost[i] = 1.0f; | ||
| yHost[i] = 2.0f; | ||
| } | ||
|
|
||
| fprintf(stderr, "Host memory initialized, copying to the device\n"); | ||
| fflush(stderr); | ||
|
|
||
| float *xDevice, *yDevice; | ||
| CHECK(cudaMalloc(&xDevice, N * sizeof(float))); | ||
| CHECK(cudaMalloc(&yDevice, N * sizeof(float))); | ||
|
|
||
| CHECK(cudaMemcpy(xDevice, xHost.data(), N * sizeof(float), | ||
| cudaMemcpyHostToDevice)); | ||
| CHECK(cudaMemcpy(yDevice, yHost.data(), N * sizeof(float), | ||
| cudaMemcpyHostToDevice)); | ||
| fprintf(stderr, "Scheduled a cudaMemcpy, calling the kernel\n"); | ||
|
|
||
| saxpy<<<(N + 255) / 256, 256>>>(N, 2.0f, xDevice, yDevice); | ||
| fprintf(stderr, "Scheduled a kernel call\n"); | ||
| CHECK(cudaGetLastError()); | ||
|
|
||
| CHECK(cudaMemcpy(yHost.data(), yDevice, N * sizeof(float), | ||
| cudaMemcpyDeviceToHost)); | ||
|
|
||
| float maxError = 0.0f; | ||
| for (int i = 0; i < N; i++) | ||
| maxError = max(maxError, abs(yHost[i] - 4.0f)); | ||
| fprintf(stderr, "Max error: %f\n", maxError); | ||
|
|
||
| CHECK(cudaFree(xDevice)); | ||
| CHECK(cudaFree(yDevice)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.