27.2 BlockAccessor

BlockAccessor is a derived class of the BlockAttributes base class. In addition of storing the basic information of a block, it also contains a reference to the MemoryRegion object responsible of its memory management. An offset member variable is also used to allow multiple blocks to map into a single MemoryRegion as described later.

1template <typename TData> class BlockAccessor : public BlockAttributes 
2{ 
3public: 
4    BlockAccessor(const BlockAttributes blockAttr, 
5                  MemoryRegion<TData> &memory_region, const size_t offset) 
6        : BlockAttributes(blockAttr), m_memory_region(memory_region), 
7          m_offset(offset) 
8    { 
9    } 
10 
11    BlockAccessor(const size_t num_elements, 
12                  const size_t num_elements_with_padding, const size_t num_data, 
13                  const size_t interleave_width, 
14                  MemoryRegion<TData> &memory_region, const size_t offset) 
15        : BlockAttributes(num_elements, num_elements_with_padding, num_data, 
16                          interleave_width), 
17          m_memory_region(memory_region), m_offset(offset) 
18    { 
19    } 
20 
21    ... 
22private: 
23    MemoryRegion<TData> &m_memory_region; 
24    size_t m_offset = 0; 
25};

Memory access to the private MemoryRegion member variable is obtained through a templated GetPtr() member function replicating the behavior of the MemoryRegion class.

1template <typename MemSpace, typename MemAccess> 
2typename const_if<std::is_same_v<MemAccess, ReadOnly>, TData>::type *GetPtr() 
3{ 
4    return m_memory_region.template GetPtr<MemSpace, MemAccess>() + 
5           m_offset; 
6}

Hence, various memory access mode of a BlockAccessor object block can be obtained as follow:

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