16#ifndef dealii_array_view_h
17#define dealii_array_view_h
32template <
int N,
typename T>
35template <
typename number>
80template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
136 MemorySpaceType> &view);
159 const std::vector<
typename std::remove_cv<value_type>::type> &vector);
175 ArrayView(std::vector<
typename std::remove_cv<value_type>::type> &vector);
191 template <std::
size_t N>
202 template <std::
size_t N>
204 const std::array<
typename std::remove_cv<value_type>::type,
N> &vector);
214 template <std::
size_t N>
215 ArrayView(std::array<
typename std::remove_cv<value_type>::type,
N> &vector);
255 MemorySpaceType> &other_view)
const;
273 MemorySpaceType> &other_view)
const;
349 namespace ArrayViewHelper
351 template <
typename MemorySpaceType>
355#ifndef DEAL_II_COMPILER_CUDA_AWARE
357 static_assert(std::is_same<MemorySpaceType, MemorySpace::Host>::value,
358 "If the compiler doesn't understand CUDA code, "
359 "the only possible memory space is 'MemorySpace::Host'!");
362 cudaPointerAttributes attributes;
363 const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr);
364 if (cuda_error != cudaErrorInvalidValue)
367 if (std::is_same<MemorySpaceType, MemorySpace::Host>::value)
368 return (attributes.type == cudaMemoryTypeHost) ||
369 (attributes.type == cudaMemoryTypeUnregistered);
371 return attributes.type == cudaMemoryTypeDevice;
377 return std::is_same<MemorySpaceType, MemorySpace::Host>::value;
386template <
typename ElementType,
typename MemorySpaceType>
394template <
typename ElementType,
typename MemorySpaceType>
397 const std::size_t n_elements)
398 : starting_element(starting_element)
399 , n_elements(n_elements)
403 internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>(
405 ExcMessage(
"The memory space indicated by the template parameter "
406 "and the one derived from the pointer value do not match!"));
411template <
typename ElementType,
typename MemorySpaceType>
414 const std::size_t n_elements)
416 *
this =
ArrayView(starting_element, n_elements);
421template <
typename ElementType,
typename MemorySpaceType>
423 : starting_element(&element)
429template <
typename ElementType,
typename MemorySpaceType>
431 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
433 : starting_element(view.starting_element)
434 , n_elements(view.n_elements)
439template <
typename ElementType,
typename MemorySpaceType>
441 const std::vector<
typename std::remove_cv<value_type>::type> &vector)
454 static_assert(std::is_const<value_type>::value ==
true,
455 "This constructor may only be called if the ArrayView "
456 "object has a const value_type. In other words, you can "
457 "only create an ArrayView to const values from a const "
463template <
typename ElementType,
typename MemorySpaceType>
465 std::vector<
typename std::remove_cv<value_type>::type> &vector)
472template <
typename ElementType,
typename MemorySpaceType>
473template <std::
size_t N>
475 const std::array<
typename std::remove_cv<value_type>::type,
N> &vector)
488 static_assert(std::is_const<value_type>::value ==
true,
489 "This constructor may only be called if the ArrayView "
490 "object has a const value_type. In other words, you can "
491 "only create an ArrayView to const values from a const "
497template <
typename ElementType,
typename MemorySpaceType>
498template <std::
size_t N>
506template <
typename ElementType,
typename MemorySpaceType>
507template <std::
size_t N>
509 std::array<
typename std::remove_cv<value_type>::type,
N> &vector)
516template <
typename ElementType,
typename MemorySpaceType>
521 return (other_view.
data() == starting_element) &&
522 (other_view.
size() == n_elements);
527template <
typename ElementType,
typename MemorySpaceType>
531 MemorySpaceType> &other_view)
const
533 return (other_view.data() == starting_element) &&
534 (other_view.size() == n_elements);
539template <
typename ElementType,
typename MemorySpaceType>
544 return !(*
this == other_view);
549template <
typename ElementType,
typename MemorySpaceType>
556 return starting_element;
561template <
typename ElementType,
typename MemorySpaceType>
565 MemorySpaceType> &other_view)
const
567 return !(*
this == other_view);
572template <
typename ElementType,
typename MemorySpaceType>
581template <
typename ElementType,
typename MemorySpaceType>
585 return starting_element;
590template <
typename ElementType,
typename MemorySpaceType>
594 return starting_element + n_elements;
599template <
typename ElementType,
typename MemorySpaceType>
603 return starting_element;
608template <
typename ElementType,
typename MemorySpaceType>
612 return starting_element + n_elements;
617template <
typename ElementType,
typename MemorySpaceType>
623 (std::is_same<MemorySpaceType, MemorySpace::Host>::value),
625 "Accessing elements is only allowed if the data is stored in CPU memory!"));
627 return *(starting_element + i);
635 namespace ArrayViewHelper
642 template <
class Iterator>
644 is_contiguous(
const Iterator &
first,
const Iterator &last)
646 const auto n = std::distance(
first, last);
647 for (
typename std::decay<
decltype(n)>::type i = 0; i < n; ++i)
648 if (std::addressof(*(std::next(
first, i))) !=
649 std::next(std::addressof(*
first), i))
667 is_contiguous(
T *,
T *)
693template <
typename Iterator,
typename MemorySpaceType = MemorySpace::Host>
695 typename std::iterator_traits<Iterator>::reference>::type,
700 std::is_same<typename std::iterator_traits<Iterator>::iterator_category,
701 typename std::random_access_iterator_tag>::value,
702 "The provided iterator should be a random access iterator.");
705 "The beginning of the array view should be before the end."));
707 ExcMessage(
"The provided range isn't contiguous in memory!"));
709 return ArrayView<
typename std::remove_reference<
710 typename std::iterator_traits<Iterator>::reference>::type,
725template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
731 "The beginning of the array view should be before the end."));
747template <
typename Number,
typename MemorySpaceType>
766template <
typename Number,
typename MemorySpaceType>
791template <
int rank,
int dim,
typename Number>
816template <
int rank,
int dim,
typename Number>
841template <
int rank,
int dim,
typename Number>
867template <
int rank,
int dim,
typename Number>
889template <
typename ElementType,
int N>
911template <
typename ElementType>
934template <
typename ElementType>
957template <
typename ElementType>
980template <
typename ElementType>
1008template <
typename ElementType>
1011 const std::size_t starting_index,
1012 const std::size_t size_of_view)
1014 Assert(starting_index + size_of_view <= vector.size(),
1015 ExcMessage(
"The starting index and size of the view you want to "
1016 "create would lead to a view that extends beyond the end "
1017 "of the given vector."));
1042template <
typename ElementType>
1045 const std::size_t starting_index,
1046 const std::size_t size_of_view)
1048 Assert(starting_index + size_of_view <= vector.size(),
1049 ExcMessage(
"The starting index and size of the view you want to "
1050 "create would lead to a view that extends beyond the end "
1051 "of the given vector."));
1073template <
typename ElementType>
1100template <
typename ElementType>
1124template <
typename ElementType>
1149template <
typename ElementType>
1174template <
typename ElementType>
1199template <
typename ElementType>
1229template <
typename ElementType>
1234 const std::size_t size_of_view)
1238 Assert(starting_column + size_of_view <= table.size()[1],
1239 ExcMessage(
"The starting index and size of the view you want to "
1240 "create would lead to a view that extends beyond the end "
1241 "of a column of the given table."));
1266template <
typename ElementType>
1271 const std::size_t size_of_view)
1275 Assert(starting_column + size_of_view <= table.size()[1],
1276 ExcMessage(
"The starting index and size of the view you want to "
1277 "create would lead to a view that extends beyond the end "
1278 "of a column of the given table."));
1295template <
typename Container>
auto make_const_array_view(const Container &container) -> decltype(make_array_view(container))
ArrayView< const ElementType > make_array_view(const Vector< ElementType > &vector)
ArrayView(value_type &element)
ArrayView(const ArrayView< typename std::remove_cv< value_type >::type, MemorySpaceType > &view)
ArrayView< Number, MemorySpaceType > make_array_view(ArrayView< Number, MemorySpaceType > &array_view)
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
ArrayView< Number > make_array_view(SymmetricTensor< rank, dim, Number > &tensor)
value_type & operator[](const std::size_t i) const
ArrayView< const ElementType > make_array_view(const LAPACKFullMatrix< ElementType > &matrix)
ArrayView< const Number > make_array_view(const Tensor< rank, dim, Number > &tensor)
ArrayView< const Number > make_array_view(const SymmetricTensor< rank, dim, Number > &tensor)
ArrayView< ElementType > make_array_view(LAPACKFullMatrix< ElementType > &matrix)
ArrayView< ElementType > make_array_view(ElementType(&array)[N])
ArrayView< ElementType, MemorySpaceType > make_array_view(ElementType *const begin, ElementType *const end)
bool operator!=(const ArrayView< typename std::remove_cv< value_type >::type, MemorySpaceType > &other_view) const
const_iterator cend() const
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
ArrayView(const std::array< typename std::remove_cv< value_type >::type, N > &vector)
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector)
ArrayView(std::vector< typename std::remove_cv< value_type >::type > &vector)
bool operator==(const ArrayView< const value_type, MemorySpaceType > &other_view) const
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
bool operator!=(const ArrayView< const value_type, MemorySpaceType > &other_view) const
const_iterator cbegin() const
ArrayView(const std::vector< typename std::remove_cv< value_type >::type > &vector)
ArrayView(value_type(&array)[N])
ArrayView< Number > make_array_view(Tensor< rank, dim, Number > &tensor)
value_type * data() const noexcept
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
void reinit(value_type *starting_element, const std::size_t n_elements)
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table)
value_type * starting_element
bool operator==(const ArrayView< typename std::remove_cv< value_type >::type, MemorySpaceType > &other_view) const
ArrayView(value_type *starting_element, const std::size_t n_elements)
ArrayView< const Number, MemorySpaceType > make_array_view(const ArrayView< Number, MemorySpaceType > &array_view)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
const ElementType * const_iterator
ArrayView< ElementType > make_array_view(Vector< ElementType > &vector)
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector)
ArrayView(std::array< typename std::remove_cv< value_type >::type, N > &vector)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
#define AssertCuda(error_code)
static ::ExceptionBase & ExcMessage(std::string arg1)
@ matrix
Contents is actually a matrix.
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
bool is_in_correct_memory_space(const void *const ptr)