Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Utilities::MemorySpace namespace and MemoryBlock class #12821

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/news/changes/minor/20211012Turcksin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
New: The new Utilities::MemorySpace namespace contains functions to help
memory space independent code. The new MemoryBlock class allocates a
block of memory on the host or the device. The underlying data can be accessed
using ArrayView.
<br>
(Bruno Turcksin, 2021/10/12)
27 changes: 15 additions & 12 deletions include/deal.II/base/array_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class ArrayView
*/
using value_type = ElementType;

/**
* An alias that denotes the memory space of this conlainer-like class.
*/
using memory_space = MemorySpaceType;

/**
* An alias for iterators pointing into the array.
*/
Expand Down Expand Up @@ -132,6 +137,7 @@ class ArrayView
* non-@p const view to a @p const view, akin to converting a non-@p const
* pointer to a @p const pointer.
*/
DEAL_II_CUDA_HOST_DEV
ArrayView(const ArrayView<typename std::remove_cv<value_type>::type,
MemorySpaceType> &view);

Expand Down Expand Up @@ -318,12 +324,9 @@ class ArrayView
* <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;
DEAL_II_CUDA_HOST_DEV value_type &
operator[](const std::size_t i) const;

private:
/**
Expand Down Expand Up @@ -429,7 +432,8 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(ElementType &element)


template <typename ElementType, typename MemorySpaceType>
inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
inline DEAL_II_CUDA_HOST_DEV
ArrayView<ElementType, MemorySpaceType>::ArrayView(
const ArrayView<typename std::remove_cv<value_type>::type, MemorySpaceType>
&view)
: starting_element(view.starting_element)
Expand Down Expand Up @@ -617,14 +621,13 @@ ArrayView<ElementType, MemorySpaceType>::cend() const


template <typename ElementType, typename MemorySpaceType>
inline typename ArrayView<ElementType, MemorySpaceType>::value_type &
ArrayView<ElementType, MemorySpaceType>::operator[](const std::size_t i) const
inline DEAL_II_CUDA_HOST_DEV
typename ArrayView<ElementType, MemorySpaceType>::value_type &
ArrayView<ElementType, MemorySpaceType>::operator[](const std::size_t i) const
{
#ifndef DEAL_II_COMPILER_CUDA_AWARE
AssertIndexRange(i, n_elements);
Assert(
(std::is_same<MemorySpaceType, MemorySpace::Host>::value),
ExcMessage(
"Accessing elements is only allowed if the data is stored in CPU memory!"));
#endif

return *(starting_element + i);
}
Expand Down
2 changes: 2 additions & 0 deletions include/deal.II/base/cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace Utilities
{
/**
* A namespace for utility structures for CUDA.
*
* @ingroup utilities
*/
namespace CUDA
{
Expand Down