26.2 MemoryStorage

MemoryStorage is a templated class designed to manage the memory allocation/deallocation on the host side. As mentionned previously, the MemoryStorage class shall never be used directly and should only be accessed through the MemoryRegion class. As such, the MemoryStorage class is made to be a friend of the MemoryRegion class and all its member functions (except constructor/destructor) and variables are made protected. The member variables of the MemoryStorage class are:

1template <typename TData> class MemoryStorage 
2{ 
3    friend class MemoryRegion<TData>; 
4 
5    ... 
6protected: 
7 
8    ... 
9 
10    bool m_owned = true; // Flag indicating if the host pointer is owned 
11                         // by the current object. 
12    TData *m_host      = nullptr; /// < Host memory pointer 
13    TData *m_device    = nullptr; ///< Device memory pointer 
14    size_t m_size      = 0; 
15    size_t m_alignment = __STDCPP_DEFAULT_NEW_ALIGNMENT__; 
16 
17    bool m_host_valid    = false; // Flag indicating that the host data is valid 
18    bool m_device_valid  = false; ///< Flag indicating the device data is valid 
19    size_t m_device_rank = 0;     // Index indicating device ID. 
20    std::string m_name{""}; 
21    MemAllocType m_memAllocType{eHostDevice}; 
22};

The MemoryStorage templated class does not have neither a default nor a copy constructors. As well, the assignment operator has been deleted and only move semantics is allowed once the MemoryStorage object has been created.

1MemoryStorage()                                    = delete; 
2MemoryStorage(const MemoryStorage &rhs)            = delete; 
3MemoryStorage &operator=(const MemoryStorage &rhs) = delete;

The MemoryStorage templated class has a custom constructor allowing provision to allocate a contiguous memory chunk with a specified alignment and number of elements size of type TData. However, the requested memory is not allocated during construction, but allocated only if a memory access is requested.

1MemoryStorage(const std::string name, const size_t size, 
2              const size_t alignment, const unsigned int device_rank, 
3              const MemAllocType &memAllocType) 
4{ 
5    m_owned        = true; 
6    m_name         = name; 
7    m_size         = size; 
8    m_alignment    = alignment; 
9    m_device_rank  = device_rank; 
10    m_memAllocType = memAllocType; 
11 
12    m_host         = nullptr; 
13    m_device       = nullptr; 
14    m_host_valid   = false; 
15    m_device_valid = false; 
16}

The MemoryStorage class has member functions responsible of host-to-device and device-to-memory copy. Memory copy is only performed if necessary by keeping track of when the data is modified on either the host or the device with the member variables m_host_valid and m_device_valid.

1void HostToDeviceCopy(void) 
2{ 
3    ... 
4}
1void DeviceToHostCopy(void) 
2{ 
3    ... 
4}

Backend-specific memory management functionalities are encapsulated inside functions. For example, the deviceMalloc function is used to allocate memory for the device side and the support for various backends is achieved using macro. As a consequence, only a single device backend can be supported at time for a given compilation configurations.

1template <typename TData> 
2void deviceMalloc(TData *&src, const unsigned int size, 
3                  [[maybe_unused]] const unsigned int device_rank) 
4{ 
5   ... 
6#if defined(NEKTAR_ENABLE_CUDA) 
7   ... 
8#elif defined(NEKTAR_ENABLE_HIP) 
9   ... 
10#elif defined(NEKTAR_ENABLE_SYCL) 
11   ... 
12#elif defined(NEKTAR_ENABLE_KOKKOS) 
13   ... 
14#else 
15   ... 
16#endif 
17}

Similar approach is used for the deviceFree, deviceMemset, deviceFill, and deviceMemcpy functions.

1template <typename TData> 
2void deviceFree(TData *src, [[maybe_unused]] const unsigned int device_rank) 
3{ 
4   ... 
5}
1template <typename TData> 
2void deviceMemset(TData *dst, const int val, const unsigned int size, 
3                  [[maybe_unused]] const unsigned int device_rank) 
4{ 
5   ... 
6}
1template <typename TData> 
2void deviceFill(TData *dst, const TData val, const unsigned int size, 
3                [[maybe_unused]] const unsigned int device_rank) 
4{ 
5   ... 
6}
1template <MemCopyType MemCopy, typename TData> 
2void deviceMemcpy(TData *dst, const TData *src, const unsigned int size, 
3                  [[maybe_unused]] const unsigned int device_rank) 
4{ 
5   ... 
6}

The MemCopy template parameter in deviceMemcpy is a MemCopyType enum value used to determine the direction of the memory copy. The following values are supported eHostToHost, eDeviceToHost, eHostToDevice, and eDeviceToDevice.