26.1 MemoryRegion

26.1.1 Description

MemoryRegion is a templated class containing a pointer to a MemoryStorage class. The MemoryStorage class should never be used directly. Instead the MemoryRegion class should be used as a higher level memory management object encapsulating the functionalities of the MemoryStorage class. The MemoryRegion class has a default constructor and a default destructor but no copy constructor and only move semantics is allowed after object instantiation.

1template <typename TData> class MemoryRegion 
2{ 
3    ... 
4 
5    MemoryRegion() = default; // Default removes implicit moves 
6    MemoryRegion(const MemoryRegion &) = delete; 
7    ~MemoryRegion() = default; // Default removes implicit moves 
8 
9    ... 
10 
11protected: 
12    ... 
13 
14    // Member variables: 
15    std::unique_ptr<MemoryStorage<TData>> m_storage = nullptr; 
16};

26.1.2 Instantiation

An instance of MemoryRegion object should be created using the Create member function. The size argument indicates the number of elements of type TData to allocate memory for, while the alignment argument indicates the desired memory alignment.

1static MemoryRegion<TData> Create( 
2    const std::string name, const size_t size, const size_t alignment, 
3    const size_t device_rank                = 0) 
4{ 
5    ... 
6}

For example, an instance of MemoryRegion object MR can be instantiated as follow:

1auto MR = MemoryRegion<TData>::Create(name, size, alignment);

26.1.3 Interoperability

To allow interoperability with legacy Nektar++, a FromArray templated member function is provided for the instantion of a MemoryRegion object with data copy from an Nektar::Array<Nektar::OneD, TDataIn> object.

1template <typename MemSpace, typename TDataIn> 
2static MemoryRegion<TData> FromArray( 
3    const std::string name, 
4    Nektar::Array<Nektar::OneD, TDataIn> const &array, 
5    const size_t alignment, const size_t device_rank = 0) 
6{ 
7    ... 
8}

For example, a MemoryRegion object MR can be instantiated from a Nektar::Array<Nektar::OneD, TDataIn> object array as follow:

1auto MR = MemoryRegion<TData>::template FromArray<NektarSpaces::HostSpace>(array, alignment);

Similiar capabilities are provided for a std::vector<TDataIn, Alloc> with the FromVector templated member function.

1template <typename MemSpace, typename TDataIn, 
2          class Alloc = std::allocator<TDataIn>> 
3static MemoryRegion<TData> FromVector( 
4    const std::string name, std::vector<TDataIn, Alloc> const &array, 
5    const size_t alignment, const size_t device_rank = 0) 
6{ 
7    ... 
8}

Additionally, the CopyArray and CopyVector templated member functions are provided to allow data copy from respectively a Nektar::Array<Nektar::OneD, TDataIn> or a std::vector<TDataIn, Alloc> to an already instantiated MemoryRegion object.

1template <typename MemSpace, typename TDataIn, 
2          MemCopyType MemCopy = eHostToDevice> 
3void CopyArray(const Nektar::Array<Nektar::OneD, TDataIn> &array) 
4{ 
5    ... 
6}
1template <typename MemSpace, typename TDataIn, 
2          MemCopyType MemCopy = eHostToDevice, 
3          class Alloc         = std::allocator<TDataIn>> 
4void CopyVector(const std::vector<TDataIn, Alloc> &array) 
5{ 
6    ... 
7}

For example, an Nektar::Array<Nektar::OneD, TDataIn> object array can be copied to a MemoryRegion object MR as follow

1MR.template CopyArray<NektarSpaces::HostSpace>(array);

To further provide interoperability, the ToArray and the ToVector allow respectively the creation of a new instance of a Nektar::Array<Nektar::OneD, TDataOut> or a std::vector<TDataOut, Alloc> object with data copy from a MemoryRegion object.

1template <typename TDataOut = TData> 
2Nektar::Array<Nektar::OneD, TDataOut> ToArray() 
3{ 
4    ... 
5}
1template <typename TDataOut = TData, class Alloc = std::allocator<TData>> 
2std::vector<TDataOut, Alloc> ToVector() 
3{ 
4    ... 
5}

For example, a new instance of a Nektar::Array<Nektar::OneD, TDataOut> object array can be created from a MemoryRegion object MR as follow:

1Nektar::Array<Nektar::OneD, TDataOut> array = MR.ToArray();

26.1.4 Memory Access

Three types of memory access qualifier are used along the MemoryRegion class: ReadOnly, WriteOnly, and ReadWrite. Memory access is performed by calling a GetPtr<MemSpace, MemAccess>() member function, where MemSpace is again a template parameter indicating on which memory space (host or device) the data access is required, while MemAccess is the template parameter for the memory access qualifier (ReadOnly, WriteOnly, and ReadWrite). For example, the various memrory access mode can be requested on the device as follow:

1auto ptr0 = MR.template GetPtr<NektarSpaces::DeviceSpace, ReadOnly>; 
2auto ptr1 = MR.template GetPtr<NektarSpaces::DeviceSpace, WriteOnly>; 
3auto ptr2 = MR.template GetPtr<NektarSpaces::DeviceSpace, ReadWrite>;

The ReadOnly memory access qualifier should be used when the data is not modified on the requested memory space. Memory copy occurs only if necessary. The requested memory space is then marked as valid. This memory access qualifier prevent a data copy if the data is subsequently requested on the opposite memory space.

The WriteOnly memory access qualifier prevents a copy to the requested memory space as it is assumed that the data is overwritten. The requested memory space is then marked as valid while the opposite memory space is marked as invalid. Memory copy will occur if the data is subsequently requested on the opposite memory space.

The ReadWrite memory access qualifier should be used when the data is both modified on the requested memory space and read access is necessary. Memory copy occurs only if necessary. The requested memory space is then marked as valid while the opposite memory space is marked as invalid. Memory copy will occur if the data is subsequently requested on the opposite memory space.