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;
326 operator[](const
std::
size_t i) const;
340 friend class
ArrayView<const ElementType, MemorySpaceType>;
350 namespace ArrayViewHelper
352 template <
typename MemorySpaceType>
356#ifndef DEAL_II_COMPILER_CUDA_AWARE
358 static_assert(std::is_same<MemorySpaceType, MemorySpace::Host>::value,
359 "If the compiler doesn't understand CUDA code, "
360 "the only possible memory space is 'MemorySpace::Host'!");
363 cudaPointerAttributes attributes;
364 const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr);
365 if (cuda_error != cudaErrorInvalidValue)
368 if (std::is_same<MemorySpaceType, MemorySpace::Host>::value)
369 return (attributes.type == cudaMemoryTypeHost) ||
370 (attributes.type == cudaMemoryTypeUnregistered);
372 return attributes.type == cudaMemoryTypeDevice;
378 return std::is_same<MemorySpaceType, MemorySpace::Host>::value;
387template <
typename ElementType,
typename MemorySpaceType>
395template <
typename ElementType,
typename MemorySpaceType>
398 const std::size_t n_elements)
399 : starting_element(starting_element)
400 , n_elements(n_elements)
404 internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>(
406 ExcMessage(
"The memory space indicated by the template parameter "
407 "and the one derived from the pointer value do not match!"));
412template <
typename ElementType,
typename MemorySpaceType>
415 const std::size_t n_elements)
417 this->starting_element = starting_element;
418 this->n_elements = n_elements;
423template <
typename ElementType,
typename MemorySpaceType>
425 : starting_element(&element)
431template <
typename ElementType,
typename MemorySpaceType>
433 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
435 : starting_element(view.starting_element)
436 , n_elements(view.n_elements)
441template <
typename ElementType,
typename MemorySpaceType>
443 const std::vector<
typename std::remove_cv<value_type>::type> &vector)
456 static_assert(std::is_const<value_type>::value ==
true,
457 "This constructor may only be called if the ArrayView "
458 "object has a const value_type. In other words, you can "
459 "only create an ArrayView to const values from a const "
465template <
typename ElementType,
typename MemorySpaceType>
467 std::vector<
typename std::remove_cv<value_type>::type> &vector)
474template <
typename ElementType,
typename MemorySpaceType>
475template <std::
size_t N>
477 const std::array<
typename std::remove_cv<value_type>::type, N> &vector)
490 static_assert(std::is_const<value_type>::value ==
true,
491 "This constructor may only be called if the ArrayView "
492 "object has a const value_type. In other words, you can "
493 "only create an ArrayView to const values from a const "
499template <
typename ElementType,
typename MemorySpaceType>
500template <std::
size_t N>
502 ElementType (&array)[N])
508template <
typename ElementType,
typename MemorySpaceType>
509template <std::
size_t N>
511 std::array<
typename std::remove_cv<value_type>::type, N> &vector)
518template <
typename ElementType,
typename MemorySpaceType>
523 return (other_view.
data() == starting_element) &&
524 (other_view.
size() == n_elements);
529template <
typename ElementType,
typename MemorySpaceType>
532 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
535 return (other_view.data() == starting_element) &&
536 (other_view.size() == n_elements);
541template <
typename ElementType,
typename MemorySpaceType>
546 return !(*
this == other_view);
551template <
typename ElementType,
typename MemorySpaceType>
558 return starting_element;
563template <
typename ElementType,
typename MemorySpaceType>
566 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
569 return !(*
this == other_view);
574template <
typename ElementType,
typename MemorySpaceType>
583template <
typename ElementType,
typename MemorySpaceType>
587 return starting_element;
592template <
typename ElementType,
typename MemorySpaceType>
596 return starting_element + n_elements;
601template <
typename ElementType,
typename MemorySpaceType>
605 return starting_element;
610template <
typename ElementType,
typename MemorySpaceType>
614 return starting_element + n_elements;
619template <
typename ElementType,
typename MemorySpaceType>
625 (std::is_same<MemorySpaceType, MemorySpace::Host>::value),
627 "Accessing elements is only allowed if the data is stored in CPU memory!"));
629 return *(starting_element + i);
637 namespace ArrayViewHelper
644 template <
class Iterator>
646 is_contiguous(
const Iterator &
first,
const Iterator &last)
648 const auto n = std::distance(
first, last);
649 for (
typename std::decay<
decltype(n)>::type i = 0; i < n; ++i)
650 if (std::addressof(*(std::next(
first, i))) !=
651 std::next(std::addressof(*
first), i))
669 is_contiguous(T *, T *)
695template <
typename Iterator,
typename MemorySpaceType = MemorySpace::Host>
697 typename std::iterator_traits<Iterator>::reference>::type,
702 std::is_same<typename std::iterator_traits<Iterator>::iterator_category,
703 typename std::random_access_iterator_tag>::value,
704 "The provided iterator should be a random access iterator.");
707 "The beginning of the array view should be before the end."));
708 Assert(internal::ArrayViewHelper::is_contiguous(begin, end),
709 ExcMessage(
"The provided range isn't contiguous in memory!"));
711 return ArrayView<
typename std::remove_reference<
712 typename std::iterator_traits<Iterator>::reference>::type,
713 MemorySpaceType>(std::addressof(*begin), end - begin);
727template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
733 "The beginning of the array view should be before the end."));
749template <
typename Number,
typename MemorySpaceType>
768template <
typename Number,
typename MemorySpaceType>
798template <
int rank,
int dim,
typename Number>
828template <
int rank,
int dim,
typename Number>
858template <
int rank,
int dim,
typename Number>
889template <
int rank,
int dim,
typename Number>
911template <
typename ElementType,
int N>
933template <
typename ElementType>
956template <
typename ElementType>
979template <
typename ElementType>
1002template <
typename ElementType>
1030template <
typename ElementType>
1033 const std::size_t starting_index,
1034 const std::size_t size_of_view)
1036 Assert(starting_index + size_of_view <= vector.size(),
1037 ExcMessage(
"The starting index and size of the view you want to "
1038 "create would lead to a view that extends beyond the end "
1039 "of the given vector."));
1064template <
typename ElementType>
1067 const std::size_t starting_index,
1068 const std::size_t size_of_view)
1070 Assert(starting_index + size_of_view <= vector.size(),
1071 ExcMessage(
"The starting index and size of the view you want to "
1072 "create would lead to a view that extends beyond the end "
1073 "of the given vector."));
1095template <
typename ElementType>
1122template <
typename ElementType>
1147template <
typename ElementType>
1172template <
typename ElementType>
1197template <
typename ElementType>
1222template <
typename ElementType>
1252template <
typename ElementType>
1257 const std::size_t size_of_view)
1261 Assert(starting_column + size_of_view <= table.size()[1],
1262 ExcMessage(
"The starting index and size of the view you want to "
1263 "create would lead to a view that extends beyond the end "
1264 "of a column of the given table."));
1289template <
typename ElementType>
1294 const std::size_t size_of_view)
1298 Assert(starting_column + size_of_view <= table.size()[1],
1299 ExcMessage(
"The starting index and size of the view you want to "
1300 "create would lead to a view that extends beyond the end "
1301 "of a column of the given table."));
1318template <
typename Container>
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
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)
bool is_in_correct_memory_space(const void *const ptr)