Skip to content

Commit

Permalink
Make CUDA optional dependency (lots of cleanup needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Oct 19, 2023
1 parent 7a920be commit 5c5bdd8
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 19 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/r_KLU_KLU.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>
#include <iostream>
#include <cmath>

#include <resolve/matrix/Coo.hpp>
#include <resolve/matrix/Csr.hpp>
Expand Down
1 change: 1 addition & 0 deletions examples/r_KLU_KLU_standalone.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>
#include <iostream>
#include <cmath>

#include <resolve/matrix/Coo.hpp>
#include <resolve/matrix/Csr.hpp>
Expand Down
15 changes: 13 additions & 2 deletions resolve/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

1 change: 1 addition & 0 deletions resolve/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

set(ReSolve_CPU_SRC
MemoryUtils.cpp
cpuVectorKernels.cpp
)

set(ReSolve_CPU_HEADER_INSTALL
Expand Down
15 changes: 15 additions & 0 deletions resolve/cpu/cpuVectorKernels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <resolve/Common.hpp>
#include <resolve/vector/VectorKernels.hpp>


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
24 changes: 19 additions & 5 deletions resolve/matrix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
]]


# C++ code
set(Matrix_SRC
io.cpp
Sparse.cpp
Expand All @@ -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
Expand All @@ -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()


Expand Down
7 changes: 7 additions & 0 deletions resolve/matrix/MatrixHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include <resolve/utilities/misc/IndexValuePair.hpp>
#include "MatrixHandler.hpp"
#include "MatrixHandlerCpu.hpp"

#ifdef RESOLVE_USE_CUDA
#include "MatrixHandlerCuda.hpp"
#endif

namespace ReSolve {
// Create a shortcut name for Logger static class
Expand All @@ -28,7 +31,9 @@ namespace ReSolve {
{
this->new_matrix_ = true;
cpuImpl_ = new MatrixHandlerCpu();
#ifdef RESOLVE_USE_CUDA
cudaImpl_ = new MatrixHandlerCuda();
#endif
}

/**
Expand Down Expand Up @@ -62,13 +67,15 @@ namespace ReSolve {
*
* @post A CUDA implementation instance is created with supplied workspace.
*/
#ifdef RESOLVE_USE_CUDA
MatrixHandler::MatrixHandler(LinAlgWorkspaceCUDA* new_workspace)
{
cpuImpl_ = new MatrixHandlerCpu();
cudaImpl_ = new MatrixHandlerCuda(new_workspace);
isCpuEnabled_ = true;
isCudaEnabled_ = true;
}
#endif

void MatrixHandler::setValuesChanged(bool isValuesChanged, std::string memspace)
{
Expand Down
24 changes: 19 additions & 5 deletions resolve/vector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
Expand Down
9 changes: 7 additions & 2 deletions resolve/vector/VectorHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include <iostream>
#include <cmath>

#include <resolve/utilities/logger/Logger.hpp>
#include <resolve/cuda/cudaKernels.h>
#include <resolve/vector/Vector.hpp>
#include <resolve/workspace/LinAlgWorkspace.hpp>
#include <resolve/vector/VectorHandlerImpl.hpp>
#include <resolve/vector/VectorHandlerCpu.hpp>
#include <resolve/vector/VectorHandlerCuda.hpp>
#include "VectorHandler.hpp"

#ifdef RESOLVE_USE_CUDA
#include <resolve/vector/VectorHandlerCuda.hpp>
#endif

namespace ReSolve {
using out = io::Logger;

Expand All @@ -32,6 +35,7 @@ namespace ReSolve {
isCpuEnabled_ = true;
}

#ifdef RESOLVE_USE_CUDA
/**
* @brief constructor
*
Expand All @@ -45,6 +49,7 @@ namespace ReSolve {
isCudaEnabled_ = true;
isCpuEnabled_ = true;
}
#endif

/**
* @brief destructor
Expand Down
17 changes: 12 additions & 5 deletions resolve/workspace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
Expand Down

0 comments on commit 5c5bdd8

Please sign in to comment.