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;
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>
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>
370 *
this =
ArrayView(starting_element, n_elements);
375 template <
typename ElementType,
typename MemorySpaceType>
383 template <
typename ElementType,
typename MemorySpaceType>
385 const ArrayView<
typename std::remove_cv<value_type>::type, MemorySpaceType>
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>
437 template <
typename ElementType,
typename MemorySpaceType>
441 MemorySpaceType> &other_view)
const 449 template <
typename ElementType,
typename MemorySpaceType>
454 return !(*
this == other_view);
459 template <
typename ElementType,
typename MemorySpaceType>
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>
500 template <
typename ElementType,
typename MemorySpaceType>
509 template <
typename ElementType,
typename MemorySpaceType>
518 template <
typename ElementType,
typename MemorySpaceType>
527 template <
typename ElementType,
typename MemorySpaceType>
535 "Accessing elements is only allowed if the data is stored in CPU memory!"));
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."));
616 Assert(internal::ArrayViewHelper::is_contiguous(begin, 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,
621 MemorySpaceType>(std::addressof(*begin), end -
begin);
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>
bool is_in_correct_memory_space(const void *const ptr)
auto make_const_array_view(const Container &container) -> decltype(make_array_view(container))
Contents is actually a matrix.
const_iterator cbegin() const
#define AssertIndexRange(index, range)
size_type n_elements() const
bool operator==(const ArrayView< const value_type, MemorySpaceType > &other_view) const
const std::conditional< std::is_const< VectorType >::value, const VectorType::value_type, VectorType::value_type >::type * const_iterator
static ::ExceptionBase & ExcMessage(std::string arg1)
value_type & operator[](const std::size_t i) const
void reinit(value_type *starting_element, const std::size_t n_elements)
std::conditional< std::is_const< VectorType >::value, const VectorType::value_type, VectorType::value_type >::type value_type
#define Assert(cond, exc)
#define AssertCuda(error_code)
#define DEAL_II_NAMESPACE_CLOSE
value_type * data() const noexcept
bool operator!=(const ArrayView< const value_type, MemorySpaceType > &other_view) const
MatrixTableIterators::Iterator< TransposeTable< T >, Constness, MatrixTableIterators::Storage::column_major > Iterator
size_type size(const unsigned int i) const
typename AlignedVector< T >::size_type size_type
#define DEAL_II_NAMESPACE_OPEN
value_type * starting_element
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
const_iterator cend() const