/**
* A class that represents a window of memory locations of type @p ElementType
- * and presents it as if it was an array that can be accessed via an
- * <code>operator[]</code>. In essence, this class is nothing more than just a
- * pointer to the first location and an integer that represents the length of
- * the array in elements. The memory remains owned by whoever allocated it, as
- * this class does not take over ownership.
+ * and presents it as if it was an array. In essence, this class is nothing more
+ * than just a pointer to the first location and an integer that represents the
+ * length of the array in elements. The memory remains owned by whoever
+ * allocated it, as this class does not take over ownership.
*
* The advantage of using this class is that you don't have to pass around
* pairs of pointers and that <code>operator[]</code> checks for the validity
- * of the index with which you subscript this array view.
+ * of the index with which you subscript this array view. Note that accessing
+ * elements is only allowed if the underlying data is stored in CPU memory.
*
* This class can handle views to both non-constant and constant memory
* locations. If you want to represent a view of a constant array, then the
* <em>view object</em>. It may however return a reference to a non-@p const
* memory location depending on whether the template type of the class is @p
* const or not.
+ *
+ * This function is only allowed to be called if the underlying data is indeed
+ * stored in CPU memory.
*/
value_type &operator[](const std::size_t i) const;
inline typename ArrayView<ElementType, MemorySpaceType>::value_type &
ArrayView<ElementType, MemorySpaceType>::operator[](const std::size_t i) const
{
+ Assert(
+ (std::is_same<MemorySpaceType, MemorySpace::Host>::value),
+ ExcMessage(
+ "Accessing elements is only allowed if the data is stored in CPU memory!"));
Assert(i < n_elements, ExcIndexRange(i, 0, n_elements));
return *(starting_element + i);
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// 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 that accessing CUDA memory in an ArrayView object
+// is not allowed.
+
+#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::unique_ptr<unsigned int[], void (*)(unsigned int *)> dummy_cuda(
+ Utilities::CUDA::allocate_device_data<unsigned int>(2),
+ Utilities::CUDA::delete_device_data<unsigned int>);
+
+ try
+ {
+ ArrayView<unsigned int, MemorySpace::CUDA> view(dummy_cuda.get(), 2);
+ const auto dummy = view[0];
+ }
+ catch (const ExceptionBase &exc)
+ {
+ deallog << exc.what() << std::endl;
+ }
+
+ return 0;
+}
--- /dev/null
+
+DEAL::
+--------------------------------------------------------
+An error occurred in file <array_view.h> in function
+ dealii::ArrayView<ElementType, MemorySpaceType>::value_type& dealii::ArrayView<ElementType, MemorySpaceType>::operator[](std::size_t) const [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:
+ (std::is_same<MemorySpaceType, MemorySpace::Host>::value)
+Additional information:
+ Accessing elements is only allowed if the data is stored in CPU memory!
+--------------------------------------------------------
+