From: Bruno Turcksin Date: Thu, 24 Sep 2020 01:18:40 +0000 (+0000) Subject: Add overload of copy_to_host() and copy_to_dev() X-Git-Tag: v9.3.0-rc1~1057^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F10962%2Fhead;p=dealii.git Add overload of copy_to_host() and copy_to_dev() --- diff --git a/include/deal.II/base/cuda.h b/include/deal.II/base/cuda.h index 0e6eafbb7b..1b900045e2 100644 --- a/include/deal.II/base/cuda.h +++ b/include/deal.II/base/cuda.h @@ -18,6 +18,7 @@ #include +#include #include #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 inline void - copy_to_host(const T *pointer_dev, std::vector &vector_host) + copy_to_host(const ArrayView &in, + ArrayView & 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 + inline void + copy_to_dev(const ArrayView &in, + ArrayView & 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 + inline void + copy_to_host(const T *pointer_dev, std::vector &vector_host) + { + ArrayView 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 &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 out(pointer_dev, vector_host.size()); + copy_to_dev(in, out); } } // namespace CUDA } // namespace Utilities