Let's say we are developing a C++ library with several functions implementing several operations on some data, eg. SumArray
, SquareElements
, AddVectors
. This is compiled into a C++ library and can be used from another program fine.
Then we add a function MatrixMultiply
. Because this is a perfect target for GPU-acceleration, we also add a function MatrixMultiplyCuda
, which internally calls some CUDA kernel.
So now the whole library requires CUDA, even if the user of the library never uses the MatrixMultiplyCuda
function.
So, the question: Is there a way to make the updated library functional even on a system without CUDA? Is there any library which deals with similar problem?
Obviously, the MatrixMultiplyCuda
function would not work without CUDA, which is fine.
My current solution is to have a macro MYLIB_USE_CUDA
guarding all the CUDA-specific code and functions, so that they are used only when the macro MYLIB_USE_CUDA
is defined, and the code is excluded if the macro is not defined. I compile the library using CMake, and if the flag -DMYLIB_USE_CUDA
is passed to CMake, the macro is defined during the compilation process and the CUDA libraries are linked.
I, however, don't really like this solution, because if the library is used in other code, the macro MYLIB_USE_CUDA
still has to be defined (because of header files) if the CUDA-specific functions are to be used, complicating the use of the library.
This does not only have to be CUDA, the issue is the same with any other library, but when the library is small it does not matter. People don't want to install several-gigabyte of CUDA because of my library, if they are not even going to use the CUDA functionality.