]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add handles for cuSOLVER and cuSPARSE in Utilities::CUDA::Handle
authorBruno Turcksin <bruno.turcksin@gmail.com>
Mon, 23 Apr 2018 22:32:37 +0000 (18:32 -0400)
committerBruno Turcksin <bruno.turcksin@gmail.com>
Thu, 26 Apr 2018 21:23:41 +0000 (17:23 -0400)
include/deal.II/base/cuda.h
source/base/cuda.cu

index c2594dbdba5236e3468c0c9d899ce93d549d5065..3e9119781ce2fe8c56ce04193d3b2029bbf9f1a3 100644 (file)
 
 #ifdef DEAL_II_WITH_CUDA
 
+#include <deal.II/base/exceptions.h>
+
+#include <cusolverDn.h>
+#include <cusolverSp.h>
 #include <cusparse.h>
+#include <vector>
 
 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 <typename T>
+    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 <typename T>
+    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 <typename T>
+    inline void copy_to_host(const T *pointer_dev,
+                             std::vector<T> &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 <typename T>
+    inline void copy_to_dev(const std::vector<T> &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);
+    }
   }
 }
 
index d89c68e141c50ce33e5edabe25229abb0e86ee57..3032bef6f607c9d5ce94e5adf7eb9d807d9a6219 100644 (file)
@@ -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<LinearAlgebra::CUDAWrappers::Vector<double>>
           ::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);
     }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.