CUDA: Difference between revisions

Bachp (talk | contribs)
Update fhs example to fix warning: evaluation warning: 'buildFHSUserEnv' has been renamed to 'buildFHSEnv' and will be removed in 25.11
Bachp (talk | contribs)
mNo edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 6: Line 6:


== <code>cudatoolkit</code>, <code>cudnn</code>, and related packages ==
== <code>cudatoolkit</code>, <code>cudnn</code>, and related packages ==
 
{{outdated|scope=section|date=July 2024|reason=Note that these examples have been updated more recently (as of 2024-07-30). May not be the best solution. A better resource is likely the packaging CUDA sample code [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cutensor here].}}
The CUDA toolkit is available in a [https://search.nixos.org/packages?channel=unstable&from=0&size=50&buckets=%7B%22package_attr_set%22%3A%5B%22cudaPackages%22%5D%2C%22package_license_set%22%3A%5B%5D%2C%22package_maintainers_set%22%3A%5B%5D%2C%22package_platforms%22%3A%5B%5D%7D&sort=relevance&type=packages&query=cudatoolkit number of different versions]. Please use the latest major version. You can see where they're defined in nixpkgs [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/cuda-modules/cudatoolkit/releases.nix here].
The CUDA toolkit is available in a [https://search.nixos.org/packages?channel=unstable&from=0&size=50&buckets=%7B%22package_attr_set%22%3A%5B%22cudaPackages%22%5D%2C%22package_license_set%22%3A%5B%5D%2C%22package_maintainers_set%22%3A%5B%5D%2C%22package_platforms%22%3A%5B%5D%7D&sort=relevance&type=packages&query=cudatoolkit number of different versions]. Please use the latest major version. You can see where they're defined in nixpkgs [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/cuda-modules/cudatoolkit/releases.nix here].


Line 12: Line 12:
* cuDNN is packaged [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cudnn here].
* cuDNN is packaged [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cudnn here].
* cuTENSOR is packaged [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cutensor here].
* cuTENSOR is packaged [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cutensor here].
{{warning|1=Note that these examples have been updated more recently (as of 2024-07-30). May not be the best solution. A better resource is likely the packaging CUDA sample code [https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/cuda-modules/cutensor here].}}


There are some possible ways to setup a development environment using CUDA on NixOS. This can be accomplished in the following ways:
There are some possible ways to setup a development environment using CUDA on NixOS. This can be accomplished in the following ways:
Line 19: Line 17:
* By making a FHS user env
* By making a FHS user env


{{file|cuda-fhs.nix|nix|<nowiki>
<syntaxhighlight lang="nix" line="1" start="1"># flake.nix, run with `nix develop`
# Run with `nix-shell cuda-fhs.nix`
# Run with `nix-shell cuda-fhs.nix`
{ pkgs ? import </nowiki><nixpkgs><nowiki> {} }:
{ pkgs ? import </nowiki><nixpkgs><nowiki> {} }:
let
  # Change according to the driver used: stable, beta
  nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable;
in
(pkgs.buildFHSEnv {
(pkgs.buildFHSEnv {
   name = "cuda-env";
   name = "cuda-env";
Line 37: Line 39:
     unzip
     unzip
     cudatoolkit
     cudatoolkit
     linuxPackages.nvidia_x11
     nvidiaPackage
     libGLU libGL
     libGLU libGL
     xorg.libXi xorg.libXmu freeglut
     xorg.libXi xorg.libXmu freeglut
Line 49: Line 51:
   profile = ''
   profile = ''
     export CUDA_PATH=${pkgs.cudatoolkit}
     export CUDA_PATH=${pkgs.cudatoolkit}
     # export LD_LIBRARY_PATH=${pkgs.linuxPackages.nvidia_x11}/lib
     # export LD_LIBRARY_PATH=${nvidiaPackage}/lib
     export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
     export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib"
     export EXTRA_CCFLAGS="-I/usr/include"
     export EXTRA_CCFLAGS="-I/usr/include"
   '';
   '';
}).env
}).env
</nowiki>}}
</syntaxhighlight>




* By making a nix-shell
* By making a nix-shell
{{file|cuda-shell.nix|nix|<nowiki>
<syntaxhighlight lang="nix" line="1" start="1">
# Run with `nix-shell cuda-shell.nix`
# flake.nix, run with `nix develop`# Run with `nix-shell cuda-shell.nix`
{ pkgs ? import </nowiki><nixpkgs><nowiki> {} }:
{ pkgs ? import </nowiki><nixpkgs><nowiki> {} }:
let
  nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable;
in
pkgs.mkShell {
pkgs.mkShell {
   name = "cuda-env-shell";
   name = "cuda-env-shell";
Line 66: Line 71:
     git gitRepo gnupg autoconf curl
     git gitRepo gnupg autoconf curl
     procps gnumake util-linux m4 gperf unzip
     procps gnumake util-linux m4 gperf unzip
     cudatoolkit linuxPackages.nvidia_x11
     cudatoolkit nvidiaPackage
     libGLU libGL
     libGLU libGL
     xorg.libXi xorg.libXmu freeglut
     xorg.libXi xorg.libXmu freeglut
Line 74: Line 79:
   shellHook = ''
   shellHook = ''
       export CUDA_PATH=${pkgs.cudatoolkit}
       export CUDA_PATH=${pkgs.cudatoolkit}
       # export LD_LIBRARY_PATH=${pkgs.linuxPackages.nvidia_x11}/lib:${pkgs.ncurses5}/lib
       # export LD_LIBRARY_PATH=${nvidiaPackage}/lib:${pkgs.ncurses}/lib
       export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
       export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib"
       export EXTRA_CCFLAGS="-I/usr/include"
       export EXTRA_CCFLAGS="-I/usr/include"
   '';           
   '';           
}
}
</nowiki>}}
</syntaxhighlight>


* By making a flake.nix<syntaxhighlight lang="nix" line="1" start="1">
* By making a flake.nix
# flake.nix, run with `nix develop`
<syntaxhighlight lang="nix" line="1" start="1"># flake.nix, run with `nix develop`
{
{
   description = "CUDA development environment";
   description = "CUDA development environment";
Line 96: Line 101:
       config.cudaVersion = "12";
       config.cudaVersion = "12";
     };
     };
    # Change according to the driver used: stable, beta
    nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable;
   in {
   in {
     # alejandra is a nix formatter with a beautiful output
     # alejandra is a nix formatter with a beautiful output
Line 105: Line 112:
         cudaPackages.cuda_cudart
         cudaPackages.cuda_cudart
         cudatoolkit
         cudatoolkit
         linuxPackages.nvidia_x11
         nvidiaPackage
         cudaPackages.cudnn
         cudaPackages.cudnn
         libGLU
         libGLU
Line 117: Line 124:
         xorg.libXrandr
         xorg.libXrandr
         zlib
         zlib
         ncurses5
         ncurses
         stdenv.cc
         stdenv.cc
         binutils
         binutils
Line 124: Line 131:


       shellHook = ''
       shellHook = ''
         export LD_LIBRARY_PATH="${pkgs.linuxPackages.nvidia_x11}/lib:$LD_LIBRARY_PATH"
         export LD_LIBRARY_PATH="${nvidiaPackage}/lib:$LD_LIBRARY_PATH"
         export CUDA_PATH=${pkgs.cudatoolkit}
         export CUDA_PATH=${pkgs.cudatoolkit}
         export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
         export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib"
         export EXTRA_CCFLAGS="-I/usr/include"
         export EXTRA_CCFLAGS="-I/usr/include"
         export CMAKE_PREFIX_PATH="${pkgs.fmt.dev}:$CMAKE_PREFIX_PATH"
         export CMAKE_PREFIX_PATH="${pkgs.fmt.dev}:$CMAKE_PREFIX_PATH"
Line 133: Line 140:
     };
     };
   };
   };
}
}</syntaxhighlight>
</syntaxhighlight>


== Setting up CUDA Binary Cache ==
== Setting up CUDA Binary Cache ==