From e517034debffe4725711db5dd06b8a86d9ae1192 Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Mon, 23 Apr 2018 18:32:37 -0400 Subject: [PATCH] Add handles for cuSOLVER and cuSPARSE in Utilities::CUDA::Handle --- include/deal.II/base/cuda.h | 57 +++++++++++++++++++++++++++++++++++++ source/base/cuda.cu | 12 ++++++++ 2 files changed, 69 insertions(+) diff --git a/include/deal.II/base/cuda.h b/include/deal.II/base/cuda.h index c2594dbdba..3e9119781c 100644 --- a/include/deal.II/base/cuda.h +++ b/include/deal.II/base/cuda.h @@ -20,7 +20,12 @@ #ifdef DEAL_II_WITH_CUDA +#include + +#include +#include #include +#include DEAL_II_NAMESPACE_OPEN namespace Utilities @@ -52,11 +57,63 @@ namespace Utilities */ ~Handle(); + cusolverDnHandle_t cusolver_dn_handle; + + cusolverSpHandle_t cusolver_sp_handle; + /** * Handle to the cuSPARSE library. */ cusparseHandle_t cusparse_handle; }; + + /** + * Allocate @p n_elements on the device. + */ + template + inline void malloc(T *&pointer, const unsigned int n_elements) + { + cudaError_t cuda_error_code = cudaMalloc(&pointer, n_elements * sizeof(T)); + AssertCuda(cuda_error_code); + } + + /** + * Free memory on the device. + */ + template + inline void free(T *&pointer) + { + cudaError_t cuda_error_code = cudaFree(pointer); + AssertCuda(cuda_error_code); + pointer = nullptr; + } + + /** + * Copy the elements in @p pointer_dev to the host in @p vector_host. + */ + template + inline void copy_to_host(const T *pointer_dev, + std::vector &vector_host) + { + cudaError_t cuda_error_code = + cudaMemcpy(vector_host.data(), pointer_dev, + vector_host.size() * sizeof(T), cudaMemcpyDeviceToHost); + AssertCuda(cuda_error_code); + } + + /** + * Copy the elements in @p vector_host to the device in @p pointer_dev. The + * memory needs to be allocate on the device before this function is called. + */ + template + inline void copy_to_dev(const std::vector &vector_host, + T *pointer_dev) + { + cudaError_t cuda_error_code = + cudaMemcpy(pointer_dev, vector_host.data(), + vector_host.size() * sizeof(T), cudaMemcpyHostToDevice); + AssertCuda(cuda_error_code); + } } } diff --git a/source/base/cuda.cu b/source/base/cuda.cu index d89c68e141..3032bef6f6 100644 --- a/source/base/cuda.cu +++ b/source/base/cuda.cu @@ -27,6 +27,12 @@ namespace Utilities { Handle::Handle() { + cusolverStatus_t cusolver_error_code = cusolverDnCreate(&cusolver_dn_handle); + AssertCusolver(cusolver_error_code); + + cusolver_error_code = cusolverSpCreate(&cusolver_sp_handle); + AssertCusolver(cusolver_error_code); + cusparseStatus_t cusparse_error_code = cusparseCreate(&cusparse_handle); AssertCusparse(cusparse_error_code); } @@ -40,6 +46,12 @@ namespace Utilities dealii::GrowingVectorMemory> ::release_unused_memory(); + cusolverStatus_t cusolver_error_code = cusolverDnDestroy(cusolver_dn_handle); + AssertCusolver(cusolver_error_code); + + cusolver_error_code = cusolverSpDestroy(cusolver_sp_handle); + AssertCusolver(cusolver_error_code); + cusparseStatus_t cusparse_error_code = cusparseDestroy(cusparse_handle); AssertCusparse(cusparse_error_code); } -- 2.39.5