How to add CUDA as optional dependency?

There is a simple solution to this, which has been adopted by many packages already:

  1. add CUDA.jl as a normal dependency
  2. do using CUDA in your package
  3. check at runtime if CUDA is functional or not (with CUDA.functional()) and use the GPU in function of this.

The installation of CUDA.jl as well as using CUDA and compiling CUDA.jl code will work without error on a system without GPU. Only at runtime, trying to use the GPU for real, it would fail. But, this you will avoid by not doing so if CUDA.functional() returns false.

Example:

  • here is how it is done in ImplicitGlobalGrid
3 Likes