From 7afbcdc355d31159a28f025e3e6bdebce29e1f05 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Mon, 5 Nov 2018 12:23:22 +0100 Subject: [PATCH] Unify is_in_correct_memory_space --- include/deal.II/base/array_view.h | 49 ++++++------------- .../cuda/array_view_wrong_memory.debug.output | 4 +- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 19f0d9bd6a..ff410441b2 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -273,50 +273,33 @@ namespace internal { template inline bool - correct_memory_space(const void *const) - { - return std::is_same::value; - } - -#ifdef DEAL_II_COMPILER_CUDA_AWARE - template <> - inline bool - correct_memory_space(const void *ptr) - { - cudaPointerAttributes attributes; - const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr); - if (cuda_error != cudaErrorInvalidValue) - { - AssertCuda(cuda_error); - return attributes.memoryType == cudaMemoryTypeHost; - } - else - { - // ignore and reset the error since host pointers produce an error - cudaGetLastError(); - return true; - } - } - - template <> - inline bool - correct_memory_space(const void *ptr) + is_in_correct_memory_space(const void *const ptr) { +#ifndef DEAL_II_COMPILER_CUDA_AWARE + (void)ptr; + static_assert(std::is_same::value, + "If the compiler doesn't understand CUDA code, " + "the only possible memory space is 'MemorySpace::Host'!"); + return true; +#else cudaPointerAttributes attributes; const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr); if (cuda_error != cudaErrorInvalidValue) { AssertCuda(cuda_error); - return attributes.memoryType == cudaMemoryTypeDevice; + if (std::is_same::value) + return attributes.memoryType == cudaMemoryTypeHost; + else + return attributes.memoryType == cudaMemoryTypeDevice; } else { // ignore and reset the error since host pointers produce an error cudaGetLastError(); - return false; + return std::is_same::value; } - } #endif + } } // namespace ArrayViewHelper } // namespace internal @@ -329,7 +312,7 @@ inline ArrayView::ArrayView( : starting_element(starting_element) , n_elements(n_elements) { - Assert(internal::ArrayViewHelper::correct_memory_space( + Assert(internal::ArrayViewHelper::is_in_correct_memory_space( starting_element), ExcMessage( "The memory space indicated by the template parameter " @@ -368,7 +351,7 @@ inline ArrayView::ArrayView( "object has a const value_type. In other words, you can " "only create an ArrayView to const values from a const " "std::vector."); - Assert(internal::ArrayViewHelper::correct_memory_space( + Assert(internal::ArrayViewHelper::is_in_correct_memory_space( vector.data()), ExcMessage( "The memory space indicated by the template parameter " diff --git a/tests/cuda/array_view_wrong_memory.debug.output b/tests/cuda/array_view_wrong_memory.debug.output index f2413eaf9c..a714815cf8 100644 --- a/tests/cuda/array_view_wrong_memory.debug.output +++ b/tests/cuda/array_view_wrong_memory.debug.output @@ -6,7 +6,7 @@ DEAL:: An error occurred in file in function dealii::ArrayView::ArrayView(dealii::ArrayView::value_type*, std::size_t) [with ElementType = unsigned int; MemorySpaceType = dealii::MemorySpace::CUDA; dealii::ArrayView::value_type = unsigned int; std::size_t = long unsigned int] The violated condition was: - internal::ArrayViewHelper::correct_memory_space( starting_element) + internal::ArrayViewHelper::is_in_correct_memory_space( starting_element) Additional information: The memory space indicated by the template parameter and the one derived from the pointer value do not match! -------------------------------------------------------- @@ -17,7 +17,7 @@ DEAL:: An error occurred in file in function dealii::ArrayView::ArrayView(dealii::ArrayView::value_type*, std::size_t) [with ElementType = unsigned int; MemorySpaceType = dealii::MemorySpace::Host; dealii::ArrayView::value_type = unsigned int; std::size_t = long unsigned int] The violated condition was: - internal::ArrayViewHelper::correct_memory_space( starting_element) + internal::ArrayViewHelper::is_in_correct_memory_space( starting_element) Additional information: The memory space indicated by the template parameter and the one derived from the pointer value do not match! -------------------------------------------------------- -- 2.39.5