16 #ifndef dealii_array_view_h
17 #define dealii_array_view_h
29 #include <type_traits>
76 template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
132 MemorySpaceType> &view);
154 const std::vector<
typename std::remove_cv<value_type>::type> &vector);
170 ArrayView(std::vector<
typename std::remove_cv<value_type>::type> &vector);
210 MemorySpaceType> &other_view)
const;
228 MemorySpaceType> &other_view)
const;
242 data() const noexcept;
280 value_type &operator[](const std::
size_t i) const;
304 namespace ArrayViewHelper
306 template <
typename MemorySpaceType>
310 #ifndef DEAL_II_COMPILER_CUDA_AWARE
313 "If the compiler doesn't understand CUDA code, "
314 "the only possible memory space is 'MemorySpace::Host'!");
317 cudaPointerAttributes attributes;
318 const cudaError_t cuda_error = cudaPointerGetAttributes(&attributes, ptr);
319 if (cuda_error != cudaErrorInvalidValue)
323 return attributes.memoryType == cudaMemoryTypeHost;
325 return attributes.memoryType == cudaMemoryTypeDevice;
340 template <
typename ElementType,
typename MemorySpaceType>
348 template <
typename ElementType,
typename MemorySpaceType>
351 const std::size_t n_elements)
352 : starting_element(starting_element)
353 , n_elements(n_elements)
357 internal::ArrayViewHelper::is_in_correct_memory_space<MemorySpaceType>(
359 ExcMessage(
"The memory space indicated by the template parameter "
360 "and the one derived from the pointer value do not match!"));
365 template <
typename ElementType,
typename MemorySpaceType>
368 const std::size_t n_elements)
370 *
this =
ArrayView(starting_element, n_elements);
375 template <
typename ElementType,
typename MemorySpaceType>
377 : starting_element(&element)
383 template <
typename ElementType,
typename MemorySpaceType>
385 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
387 : starting_element(view.starting_element)
388 , n_elements(view.n_elements)
393 template <
typename ElementType,
typename MemorySpaceType>
395 const std::vector<
typename std::remove_cv<value_type>::type> &vector)
409 "This constructor may only be called if the ArrayView "
410 "object has a const value_type. In other words, you can "
411 "only create an ArrayView to const values from a const "
417 template <
typename ElementType,
typename MemorySpaceType>
419 std::vector<
typename std::remove_cv<value_type>::type> &vector)
426 template <
typename ElementType,
typename MemorySpaceType>
431 return (other_view.
data() == starting_element) &&
432 (other_view.
size() == n_elements);
437 template <
typename ElementType,
typename MemorySpaceType>
441 MemorySpaceType> &other_view)
const
443 return (other_view.data() == starting_element) &&
444 (other_view.size() == n_elements);
449 template <
typename ElementType,
typename MemorySpaceType>
454 return !(*
this == other_view);
459 template <
typename ElementType,
typename MemorySpaceType>
466 return starting_element;
471 template <
typename ElementType,
typename MemorySpaceType>
475 MemorySpaceType> &other_view)
const
477 return !(*
this == other_view);
482 template <
typename ElementType,
typename MemorySpaceType>
491 template <
typename ElementType,
typename MemorySpaceType>
495 return starting_element;
500 template <
typename ElementType,
typename MemorySpaceType>
504 return starting_element + n_elements;
509 template <
typename ElementType,
typename MemorySpaceType>
513 return starting_element;
518 template <
typename ElementType,
typename MemorySpaceType>
522 return starting_element + n_elements;
527 template <
typename ElementType,
typename MemorySpaceType>
535 "Accessing elements is only allowed if the data is stored in CPU memory!"));
537 return *(starting_element + i);
545 namespace ArrayViewHelper
552 template <
class Iterator>
556 const auto n = std::distance(
first, last);
557 for (
typename std::decay<decltype(n)>::type i = 0; i < n; ++i)
558 if (std::addressof(*(std::next(
first, i))) !=
559 std::next(std::addressof(*
first), i))
577 is_contiguous(
T *,
T *)
603 template <
typename Iterator,
typename MemorySpaceType = MemorySpace::Host>
604 ArrayView<
typename std::remove_reference<
605 typename std::iterator_traits<Iterator>::reference>::type,
610 std::is_same<
typename std::iterator_traits<Iterator>::iterator_category,
611 typename std::random_access_iterator_tag>::
value,
612 "The provided iterator should be a random access iterator.");
615 "The beginning of the array view should be before the end."));
617 ExcMessage(
"The provided range isn't contiguous in memory!"));
619 return ArrayView<
typename std::remove_reference<
620 typename std::iterator_traits<Iterator>::reference>::type,
635 template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
641 "The beginning of the array view should be before the end."));
657 template <
typename Number,
typename MemorySpaceType>
676 template <
typename Number,
typename MemorySpaceType>
701 template <
int rank,
int dim,
typename Number>
726 template <
int rank,
int dim,
typename Number>
751 template <
int rank,
int dim,
typename Number>
777 template <
int rank,
int dim,
typename Number>
799 template <
typename ElementType,
int N>
821 template <
typename ElementType>
844 template <
typename ElementType>
867 template <
typename ElementType>
890 template <
typename ElementType>
918 template <
typename ElementType>
921 const std::size_t starting_index,
922 const std::size_t size_of_view)
924 Assert(starting_index + size_of_view <= vector.size(),
925 ExcMessage(
"The starting index and size of the view you want to "
926 "create would lead to a view that extends beyond the end "
927 "of the given vector."));
952 template <
typename ElementType>
955 const std::size_t starting_index,
956 const std::size_t size_of_view)
958 Assert(starting_index + size_of_view <= vector.size(),
959 ExcMessage(
"The starting index and size of the view you want to "
960 "create would lead to a view that extends beyond the end "
961 "of the given vector."));
983 template <
typename ElementType>
1010 template <
typename ElementType>
1034 template <
typename ElementType>
1059 template <
typename ElementType>
1084 template <
typename ElementType>
1109 template <
typename ElementType>
1139 template <
typename ElementType>
1144 const std::size_t size_of_view)
1148 Assert(starting_column + size_of_view <= table.
size()[1],
1149 ExcMessage(
"The starting index and size of the view you want to "
1150 "create would lead to a view that extends beyond the end "
1151 "of a column of the given table."));
1176 template <
typename ElementType>
1181 const std::size_t size_of_view)
1185 Assert(starting_column + size_of_view <= table.
size()[1],
1186 ExcMessage(
"The starting index and size of the view you want to "
1187 "create would lead to a view that extends beyond the end "
1188 "of a column of the given table."));
1205 template <
typename Container>