From 5c5bdd85279e0e89efb02e2daaef7b489920a7b0 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 19 Oct 2023 19:46:13 -0400 Subject: [PATCH] Make CUDA optional dependency (lots of cleanup needed. --- examples/CMakeLists.txt | 1 + examples/r_KLU_KLU.cpp | 1 + examples/r_KLU_KLU_standalone.cpp | 1 + resolve/CMakeLists.txt | 15 +++++++++++++-- resolve/cpu/CMakeLists.txt | 1 + resolve/cpu/cpuVectorKernels.cpp | 15 +++++++++++++++ resolve/matrix/CMakeLists.txt | 24 +++++++++++++++++++----- resolve/matrix/MatrixHandler.cpp | 7 +++++++ resolve/vector/CMakeLists.txt | 24 +++++++++++++++++++----- resolve/vector/VectorHandler.cpp | 9 +++++++-- resolve/workspace/CMakeLists.txt | 17 ++++++++++++----- 11 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 resolve/cpu/cpuVectorKernels.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6250400b..f2d323f4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -14,6 +14,7 @@ target_link_libraries(klu_klu.exe PRIVATE ReSolve) add_executable(klu_klu_standalone.exe r_KLU_KLU_standalone.cpp) target_link_libraries(klu_klu_standalone.exe PRIVATE ReSolve) +# Create CUDA examples if(RESOLVE_USE_CUDA) # Build example with KLU factorization and GLU refactorization diff --git a/examples/r_KLU_KLU.cpp b/examples/r_KLU_KLU.cpp index 550144ae..19c9fc33 100644 --- a/examples/r_KLU_KLU.cpp +++ b/examples/r_KLU_KLU.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/examples/r_KLU_KLU_standalone.cpp b/examples/r_KLU_KLU_standalone.cpp index 628eeb06..9d658b6e 100644 --- a/examples/r_KLU_KLU_standalone.cpp +++ b/examples/r_KLU_KLU_standalone.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index 2efac125..8dbcc467 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -8,16 +8,21 @@ add_subdirectory(utilities) +# C++ files set(ReSolve_SRC LinSolver.cpp LinSolverDirectKLU.cpp +) + +# C++ code that links to CUDA SDK libraries +set(ReSolve_CUDASDK_SRC LinSolverIterativeFGMRES.cpp GramSchmidt.cpp LinSolverDirectCuSolverGLU.cpp LinSolverDirectCuSolverRf.cpp ) - +# Header files to be installed set(ReSolve_HEADER_INSTALL Common.hpp cusolver_defs.hpp @@ -71,17 +76,21 @@ set(ReSolve_Targets_List resolve_workspace ) +# If CUDA support is enabled add CUDA SDK specific code and dependencies if(RESOLVE_USE_CUDA) + set(ReSolve_SRC ${ReSolve_SRC} ${ReSolve_CUDASDK_SRC}) set(ReSolve_Targets_List ${ReSolve_Targets_List} resolve_backend_cuda) endif() +# If no GPU support is enabled, link to dummy device backend if(NOT RESOLVE_USE_GPU) set(ReSolve_Targets_List ${ReSolve_Targets_List} resolve_backend_cpu) endif() - +# Set installable targets install(TARGETS ${ReSolve_Targets_List} EXPORT ReSolveTargets) +# Create ReSolve library add_library(ReSolve SHARED ${ReSolve_SRC}) target_include_directories(ReSolve INTERFACE @@ -92,10 +101,12 @@ target_include_directories(ReSolve INTERFACE # TODO: Make this PRIVATE dependency (requires refactoring ReSolve code) target_link_libraries(ReSolve PUBLIC ${ReSolve_Targets_List}) +# Install targets install(TARGETS ReSolve EXPORT ReSolveTargets ARCHIVE DESTINATION lib LIBRARY DESTINATION lib) + # install include headers install(FILES ${ReSolve_HEADER_INSTALL} DESTINATION include/resolve) diff --git a/resolve/cpu/CMakeLists.txt b/resolve/cpu/CMakeLists.txt index cb84dc55..4e58e94a 100644 --- a/resolve/cpu/CMakeLists.txt +++ b/resolve/cpu/CMakeLists.txt @@ -8,6 +8,7 @@ set(ReSolve_CPU_SRC MemoryUtils.cpp + cpuVectorKernels.cpp ) set(ReSolve_CPU_HEADER_INSTALL diff --git a/resolve/cpu/cpuVectorKernels.cpp b/resolve/cpu/cpuVectorKernels.cpp new file mode 100644 index 00000000..26a20413 --- /dev/null +++ b/resolve/cpu/cpuVectorKernels.cpp @@ -0,0 +1,15 @@ +#include +#include + + +namespace ReSolve { namespace vector { + + +void set_array_const(index_type n, real_type val, real_type* arr) +{ + for(index_type i = 0; i < n; ++i) { + arr[i] = val; + } +} + +}} // namespace ReSolve::vector \ No newline at end of file diff --git a/resolve/matrix/CMakeLists.txt b/resolve/matrix/CMakeLists.txt index 5d05df1b..78b1689c 100644 --- a/resolve/matrix/CMakeLists.txt +++ b/resolve/matrix/CMakeLists.txt @@ -6,7 +6,7 @@ ]] - +# C++ code set(Matrix_SRC io.cpp Sparse.cpp @@ -15,10 +15,14 @@ set(Matrix_SRC Coo.cpp MatrixHandler.cpp MatrixHandlerCpu.cpp - MatrixHandlerCuda.cpp ) +# C++ code that depends on CUDA SDK libraries +set(Matrix_CUDASDK_SRC + MatrixHandlerCuda.cpp +) +# Header files to be installed set(Matrix_HEADER_INSTALL io.hpp Sparse.hpp @@ -28,13 +32,23 @@ set(Matrix_HEADER_INSTALL MatrixHandler.hpp ) +# Add CUDA matrix handler if CUDA support is enabled +if(RESOLVE_USE_CUDA) + set(Matrix_SRC ${Matrix_SRC} ${Matrix_CUDASDK_SRC}) +endif() + + # Build shared library ReSolve::matrix add_library(resolve_matrix SHARED ${Matrix_SRC}) +# Link to CUDA ReSolve backend if CUDA is support enabled if (RESOLVE_USE_CUDA) - target_link_libraries(resolve_matrix PUBLIC resolve_backend_cuda) -else() - target_link_libraries(resolve_matrix PUBLIC resolve_backend_cpu) + target_link_libraries(resolve_matrix PUBLIC resolve_backend_cuda) +endif() + +# Link to dummy device backend if GPU support is not enabled +if (NOT RESOLVE_USE_GPU) + target_link_libraries(resolve_matrix PUBLIC resolve_backend_cpu) endif() diff --git a/resolve/matrix/MatrixHandler.cpp b/resolve/matrix/MatrixHandler.cpp index c4849c18..b0ae516e 100644 --- a/resolve/matrix/MatrixHandler.cpp +++ b/resolve/matrix/MatrixHandler.cpp @@ -9,7 +9,10 @@ #include #include "MatrixHandler.hpp" #include "MatrixHandlerCpu.hpp" + +#ifdef RESOLVE_USE_CUDA #include "MatrixHandlerCuda.hpp" +#endif namespace ReSolve { // Create a shortcut name for Logger static class @@ -28,7 +31,9 @@ namespace ReSolve { { this->new_matrix_ = true; cpuImpl_ = new MatrixHandlerCpu(); +#ifdef RESOLVE_USE_CUDA cudaImpl_ = new MatrixHandlerCuda(); +#endif } /** @@ -62,6 +67,7 @@ namespace ReSolve { * * @post A CUDA implementation instance is created with supplied workspace. */ +#ifdef RESOLVE_USE_CUDA MatrixHandler::MatrixHandler(LinAlgWorkspaceCUDA* new_workspace) { cpuImpl_ = new MatrixHandlerCpu(); @@ -69,6 +75,7 @@ namespace ReSolve { isCpuEnabled_ = true; isCudaEnabled_ = true; } +#endif void MatrixHandler::setValuesChanged(bool isValuesChanged, std::string memspace) { diff --git a/resolve/vector/CMakeLists.txt b/resolve/vector/CMakeLists.txt index f418bf55..ca056acb 100644 --- a/resolve/vector/CMakeLists.txt +++ b/resolve/vector/CMakeLists.txt @@ -6,29 +6,43 @@ ]] - +# C++ code set(Vector_SRC Vector.cpp VectorHandler.cpp VectorHandlerCpu.cpp - VectorHandlerCuda.cpp ) +# C++ code that depends on CUDA SDK libraries +set(Vector_CUDASDK_SRC + VectorHandlerCuda.cpp +) +# Header files to be installed set(Vector_HEADER_INSTALL Vector.hpp VectorHandler.hpp VectorKernels.hpp ) +# Add CUDA vector handler if CUDA support is enabled +if(RESOLVE_USE_CUDA) + set(Vector_SRC ${Vector_SRC} ${Vector_CUDASDK_SRC}) +endif() + add_library(resolve_vector SHARED ${Vector_SRC}) +# Link to ReSolve CUDA backend if CUDA is enabled if (RESOLVE_USE_CUDA) - target_link_libraries(resolve_vector PUBLIC resolve_backend_cuda) -else() - target_link_libraries(resolve_vector PUBLIC resolve_backend_cpu) + target_link_libraries(resolve_vector PUBLIC resolve_backend_cuda) endif() +# If no GPU is enabled link to dummy device backend +if(NOT RESOLVE_USE_GPU) + target_link_libraries(resolve_vector PUBLIC resolve_backend_cpu) +endif(NOT RESOLVE_USE_GPU) + + target_include_directories(resolve_vector INTERFACE $ $ diff --git a/resolve/vector/VectorHandler.cpp b/resolve/vector/VectorHandler.cpp index 0feb96a8..8c89cb2f 100644 --- a/resolve/vector/VectorHandler.cpp +++ b/resolve/vector/VectorHandler.cpp @@ -1,14 +1,17 @@ #include +#include #include -#include #include #include #include #include -#include #include "VectorHandler.hpp" +#ifdef RESOLVE_USE_CUDA +#include +#endif + namespace ReSolve { using out = io::Logger; @@ -32,6 +35,7 @@ namespace ReSolve { isCpuEnabled_ = true; } +#ifdef RESOLVE_USE_CUDA /** * @brief constructor * @@ -45,6 +49,7 @@ namespace ReSolve { isCudaEnabled_ = true; isCpuEnabled_ = true; } +#endif /** * @brief destructor diff --git a/resolve/workspace/CMakeLists.txt b/resolve/workspace/CMakeLists.txt index c6225487..673fac4b 100644 --- a/resolve/workspace/CMakeLists.txt +++ b/resolve/workspace/CMakeLists.txt @@ -6,11 +6,13 @@ ]] +# C++ code set(ReSolve_Workspace_SRC LinAlgWorkspaceCpu.cpp ) -set(ReSolve_Workspace_CUDA_SRC # consider adding a separate dir for cuda-sdk dependent code +# C++ code that depends on CUDA SDK libraries +set(ReSolve_Workspace_CUDASDK_SRC LinAlgWorkspaceCUDA.cpp ) @@ -20,12 +22,17 @@ set(ReSolve_Workspace_HEADER_INSTALL LinAlgWorkspaceCUDA.hpp ) +# If cuda is enabled, add CUDA SDK workspace files +if(RESOLVE_USE_CUDA) + set(ReSolve_Workspace_SRC ${ReSolve_Workspace_SRC} ${ReSolve_Workspace_CUDASDK_SRC}) +endif() + +add_library(resolve_workspace SHARED ${ReSolve_Workspace_SRC}) + +# If CUDA is enabled, link to ReSolve CUDA backend if(RESOLVE_USE_CUDA) - add_library(resolve_workspace SHARED ${ReSolve_Workspace_SRC} ${ReSolve_Workspace_CUDA_SRC}) target_link_libraries(resolve_workspace PUBLIC resolve_backend_cuda) -else(RESOLVE_USE_CUDA) - add_library(resolve_workspace SHARED ${ReSolve_Workspace_SRC}) -endif(RESOLVE_USE_CUDA) +endif(RESOLVE_USE_CUDA) target_include_directories(resolve_workspace INTERFACE $