]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Unify is_in_correct_memory_space 7412/head
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Mon, 5 Nov 2018 11:23:22 +0000 (12:23 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Tue, 6 Nov 2018 23:03:21 +0000 (00:03 +0100)
include/deal.II/base/array_view.h
tests/cuda/array_view_wrong_memory.debug.output

index 19f0d9bd6addcf1e809cc4a59575e04fd8986ac6..ff410441b2d215521ccc582d41fac6c6b430a8e4 100644 (file)
@@ -273,50 +273,33 @@ namespace internal
   {
     template <typename MemorySpaceType>
     inline bool
-    correct_memory_space(const void *const)
-    {
-      return std::is_same<MemorySpaceType, MemorySpace::Host>::value;
-    }
-
-#ifdef DEAL_II_COMPILER_CUDA_AWARE
-    template <>
-    inline bool
-    correct_memory_space<MemorySpace::Host>(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<MemorySpace::CUDA>(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<MemorySpaceType, MemorySpace::Host>::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<MemorySpaceType, MemorySpace::Host>::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<MemorySpaceType, MemorySpace::Host>::value;
         }
-    }
 #endif
+    }
   } // namespace ArrayViewHelper
 } // namespace internal
 
@@ -329,7 +312,7 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
   : starting_element(starting_element)
   , n_elements(n_elements)
 {
-  Assert(internal::ArrayViewHelper::correct_memory_space<MemorySpaceType>(
+  Assert(internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>(
            starting_element),
          ExcMessage(
            "The memory space indicated by the template parameter "
@@ -368,7 +351,7 @@ inline ArrayView<ElementType, MemorySpaceType>::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<MemorySpaceType>(
+  Assert(internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>(
            vector.data()),
          ExcMessage(
            "The memory space indicated by the template parameter "
index f2413eaf9c2a31127df463c8acbf3fb0754ba2be..a714815cf8259f24cac050ef62ac5f97cb1cd9d1 100644 (file)
@@ -6,7 +6,7 @@ DEAL::
 An error occurred in file <array_view.h> in function
     dealii::ArrayView<ElementType, MemorySpaceType>::ArrayView(dealii::ArrayView<ElementType, MemorySpaceType>::value_type*, std::size_t) [with ElementType = unsigned int; MemorySpaceType = dealii::MemorySpace::CUDA; dealii::ArrayView<ElementType, MemorySpaceType>::value_type = unsigned int; std::size_t = long unsigned int]
 The violated condition was: 
-    internal::ArrayViewHelper::correct_memory_space<MemorySpaceType>( starting_element)
+    internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>( 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 <array_view.h> in function
     dealii::ArrayView<ElementType, MemorySpaceType>::ArrayView(dealii::ArrayView<ElementType, MemorySpaceType>::value_type*, std::size_t) [with ElementType = unsigned int; MemorySpaceType = dealii::MemorySpace::Host; dealii::ArrayView<ElementType, MemorySpaceType>::value_type = unsigned int; std::size_t = long unsigned int]
 The violated condition was: 
-    internal::ArrayViewHelper::correct_memory_space<MemorySpaceType>( starting_element)
+    internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>( starting_element)
 Additional information: 
     The memory space indicated by the template parameter and the one derived from the pointer value do not match!
 --------------------------------------------------------

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.