]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Test correct memory space
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 3 Nov 2018 11:41:02 +0000 (12:41 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Tue, 6 Nov 2018 21:19:59 +0000 (22:19 +0100)
include/deal.II/base/array_view.h
tests/cuda/array_view_wrong_memory.cu [new file with mode: 0644]
tests/cuda/array_view_wrong_memory.debug.output [new file with mode: 0644]

index 92aa30e8e90588910507779768fdf1782eea5c0b..5c769608c46c631fc5e8823eea69c6542ef0fd1f 100644 (file)
@@ -264,6 +264,60 @@ private:
 //---------------------------------------------------------------------------
 
 
+namespace internal
+{
+  namespace ArrayViewHelper
+  {
+    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)
+    {
+      cudaPointerAttributes attributes;
+      const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr);
+      if (cuda_error != cudaErrorInvalidValue)
+        {
+          AssertCuda(cuda_error);
+          return attributes.memoryType == cudaMemoryTypeDevice;
+        }
+      else
+        {
+          // ignore and reset the error since host pointers produce an error
+          cudaGetLastError();
+          return false;
+        }
+    }
+#endif
+  } // namespace ArrayViewHelper
+} // namespace internal
+
+
 
 template <typename ElementType, typename MemorySpaceType>
 inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
@@ -271,7 +325,13 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
   const std::size_t n_elements)
   : starting_element(starting_element)
   , n_elements(n_elements)
-{}
+{
+  Assert(internal::ArrayViewHelper::correct_memory_space<MemorySpaceType>(
+           starting_element),
+         ExcMessage(
+           "The memory space indicated by the template parameter "
+           "and the one derived from the pointer value do not match!"));
+}
 
 
 
@@ -305,6 +365,11 @@ 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>(
+           vector.data()),
+         ExcMessage(
+           "The memory space indicated by the template parameter "
+           "and the one derived from the pointer value do not match!"));
 }
 
 
@@ -381,6 +446,8 @@ ArrayView<ElementType, MemorySpaceType>::size() const
   return n_elements;
 }
 
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline typename ArrayView<ElementType, MemorySpaceType>::iterator
 ArrayView<ElementType, MemorySpaceType>::begin() const
diff --git a/tests/cuda/array_view_wrong_memory.cu b/tests/cuda/array_view_wrong_memory.cu
new file mode 100644 (file)
index 0000000..66f8972
--- /dev/null
@@ -0,0 +1,62 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2018 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+// check that we detect creating ArrayView objects wit the wrong memory space.
+
+#include <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+int
+main(int argc, char **argv)
+{
+  deal_II_exceptions::disable_abort_on_exception();
+
+  initlog();
+
+  std::vector<unsigned int>                                 dummy_host(2);
+  std::unique_ptr<unsigned int[], void (*)(unsigned int *)> dummy_cuda(
+    Utilities::CUDA::allocate_device_data<unsigned int>(2),
+    Utilities::CUDA::delete_device_data<unsigned int>);
+
+  deallog << "Testing host ArrayView with host memory" << std::endl;
+  ArrayView<unsigned int, MemorySpace::Host> view_1(dummy_host);
+
+  deallog << "Testing device ArrayView with host memory" << std::endl;
+  try
+    {
+      ArrayView<unsigned int, MemorySpace::CUDA> view_2(dummy_host);
+    }
+  catch (const ExceptionBase &exc)
+    {
+      deallog << exc.what() << std::endl;
+    }
+
+  deallog << "Testing host ArrayView with device memory" << std::endl;
+  try
+    {
+      ArrayView<unsigned int, MemorySpace::Host> view_3(dummy_cuda.get(), 2);
+    }
+  catch (const ExceptionBase &exc)
+    {
+      deallog << exc.what() << std::endl;
+    }
+
+  deallog << "Testing device ArrayView with device memory" << std::endl;
+  ArrayView<unsigned int, MemorySpace::CUDA> view_4(dummy_cuda.get(), 2);
+
+  return 0;
+}
diff --git a/tests/cuda/array_view_wrong_memory.debug.output b/tests/cuda/array_view_wrong_memory.debug.output
new file mode 100644 (file)
index 0000000..f2413ea
--- /dev/null
@@ -0,0 +1,25 @@
+
+DEAL::Testing host ArrayView with host memory
+DEAL::Testing device ArrayView with host memory
+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)
+Additional information: 
+    The memory space indicated by the template parameter and the one derived from the pointer value do not match!
+--------------------------------------------------------
+
+DEAL::Testing host ArrayView with device memory
+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)
+Additional information: 
+    The memory space indicated by the template parameter and the one derived from the pointer value do not match!
+--------------------------------------------------------
+
+DEAL::Testing device ArrayView with device memory

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.