]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add overload of copy_to_host() and copy_to_dev() 10962/head
authorBruno Turcksin <bruno.turcksin@gmail.com>
Thu, 24 Sep 2020 01:18:40 +0000 (01:18 +0000)
committerBruno Turcksin <bruno.turcksin@gmail.com>
Tue, 29 Sep 2020 02:31:23 +0000 (02:31 +0000)
include/deal.II/base/cuda.h

index 0e6eafbb7b31286bd1cc04a2f2b0f8599972c045..1b900045e29fe053f8fb29a7262146666be0879f 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <deal.II/base/config.h>
 
+#include <deal.II/base/array_view.h>
 #include <deal.II/base/exceptions.h>
 
 #ifdef DEAL_II_COMPILER_CUDA_AWARE
@@ -124,19 +125,49 @@ namespace Utilities
     }
 
     /**
-     * Copy the elements in @p pointer_dev to the host in @p vector_host.
+     * Copy the device ArrayView @p in to the host ArrayView @p out.
      */
     template <typename T>
     inline void
-    copy_to_host(const T *pointer_dev, std::vector<T> &vector_host)
+    copy_to_host(const ArrayView<const T, MemorySpace::CUDA> &in,
+                 ArrayView<T, MemorySpace::Host> &            out)
     {
-      cudaError_t cuda_error_code = cudaMemcpy(vector_host.data(),
-                                               pointer_dev,
-                                               vector_host.size() * sizeof(T),
+      AssertDimension(in.size(), out.size());
+      cudaError_t cuda_error_code = cudaMemcpy(out.data(),
+                                               in.data(),
+                                               in.size() * sizeof(T),
                                                cudaMemcpyDeviceToHost);
       AssertCuda(cuda_error_code);
     }
 
+    /**
+     * Copy the host ArrayView @p in to the device ArrayView @p out.
+     */
+    template <typename T>
+    inline void
+    copy_to_dev(const ArrayView<const T, MemorySpace::Host> &in,
+                ArrayView<T, MemorySpace::CUDA> &            out)
+    {
+      AssertDimension(in.size(), out.size());
+      cudaError_t cuda_error_code = cudaMemcpy(out.data(),
+                                               in.data(),
+                                               in.size() * sizeof(T),
+                                               cudaMemcpyHostToDevice);
+      AssertCuda(cuda_error_code);
+    }
+
+    /**
+     * 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)
+    {
+      ArrayView<const T, MemorySpace::CUDA> in(pointer_dev, vector_host.size());
+      auto                                  out = make_array_view(vector_host);
+      copy_to_host(in, out);
+    }
+
     /**
      * 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.
@@ -145,11 +176,9 @@ namespace Utilities
     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);
+      auto                            in = make_array_view(vector_host);
+      ArrayView<T, MemorySpace::CUDA> out(pointer_dev, vector_host.size());
+      copy_to_dev(in, out);
     }
   } // namespace CUDA
 } // namespace Utilities

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.