A Field is conceptually a list of blocks representing all the elements of a computational
domain, where a block is a collection of elements of the same shape, order, quadrature details
and deformation. The member variables of the Field object are indicated below:
1template <typename TData = default_fp_type, FieldState TState = DefaultState> 2class Field 3{ 4 ... 5 6protected: 7 // Member variables: 8 std::string m_name; 9 std::vector<std::string> m_var_names; 10 TData *m_host = nullptr; 11 size_t m_alignment = __STDCPP_DEFAULT_NEW_ALIGNMENT__; 12 size_t m_num_device = 1; 13 std::vector<BlockAccessor<TData>> m_block_accessors; 14 std::vector<MemoryRegion<TData>> m_memory_regions; 15 std::vector<size_t> m_blk_to_mr_offset; 16 std::vector<size_t> m_blk_to_mr_mapping; 17};
An instance of Field object can be created using the Create function. The blockAttr input
argument is a vector of BlockAttributes while nvar is the number of variables associated with
the Field.
1static Field<TData, TState> Create( 2 const std::string name, const std::vector<BlockAttributes> blockAttr, 3 const int nvar, const size_t alignment, 4 [[maybe_unused]] const bool device_only = false) 5{ 6 ... 7}
Alternatively, a Field object can be also instantiated by providing a vector of component
names:
1static Field<TData, TState> Create( 2 const std::string name, const std::vector<BlockAttributes> blockAttr, 3 const std::vector<std::string> components, const size_t alignment, 4 const bool device_only = false) 5{ 6 ... 7}
To allow interoperability with legacy Nektar++, the CopyArray and CopyVector templated
member functions can be used to copy data from a Nektar::Array<Nektar::OneD, TDataOut> or
std::vector<TDataOut, Alloc> object to a Field object. Those member function internally
distribute the contiguous memory from a Nektar::Array<Nektar::OneD, TDataOut> or
std::vector<TDataOut, Alloc> object to the various blocks of the Field object, with consideration
for padding elements.
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}
Again, to allow interoperability with legacy Nektar++, the ToArray and ToVector templated
member functions can be used to instantiated a Nektar::Array<Nektar::OneD, TDataOut> or
std::vector<TDataOut, Alloc> object with a contiguous memory allocation and with the padding
elements removed.
1template <typename TDataOut = TData> 2Nektar::Array<Nektar::OneD, TDataOut> ToArray() 3{ 4 ... 5}
1template <typename TDataOut = TData, class Alloc = std::allocator<TDataOut>> 2std::vector<TDataOut, Alloc> ToVector() 3{ 4 ... 5}
The Field class provides memory access to the blocks by returning a reference to the vector of
BlockAccessor objects throught the GetBlocks member function.
1std::vector<BlockAccessor<TData>> &GetBlocks() 2{ 3 return m_block_accessors; 4}
Each individual blocks of a Field object can then be accessed as follow:
1for (size_t blk = 0; blk < field.GetBlocks().size(); ++blk) 2{ 3 auto ptr = field.GetBlocks()[blk].template GetPtr<NektarSpaces::DeviceSpace, ReadOnly>; 4 ... 5}
Or alternatively as follow:
1for (auto &block : field.GetBlocks()) 2{ 3 auto ptr = block.template GetPtr<NektarSpaces::DeviceSpace, ReadOnly>; 4 ... 5}
In terms of memory management, the various blocks are represented using a vector of
MemoryRegion objects. Two designs are currently considered for the mapping between the blocks
and the MemoryRegion objects. For the first design, each block is managed by a individual
MemoryRegion (MR) object which is responsible of the host/device memory allocation and data
transfer. Each memory region can then be assigned to different GPU/compute unit in a
round-robin manner or using any strategy to optimize load balancing. This design is
illustrated by Figure 27.1. For the second desgin, each compute unit (e.g. GPU) is managed by
a single MemoryRegion object. Different blocks are then collectively managed for a
given compute unite by the single MemoryRegion. This design is illustrated by Figure
27.2.
The mapping between the blocks and the MemoryRegion objects is determined during the
creation of the Field object by the member function SetBlockToMemoryRegionMapping.
1static Field<TData, TState> Create( 2 const std::vector<BlockAttributes> blockAttr, const int nvar, 3 const size_t alignment, const bool device_only = false) 4{ 5 return Field<TData, TState>::Create("", blockAttr, nvar, alignment, 6 device_only); 7} 8 9static void SetBlockToMemoryRegionMapping( 10 Field<TData, TState> &field, 11 const std::vector<BlockAttributes> blockAttr, const size_t alignment, 12 const bool device_only = false) 13{ 14defined(NEKTAR_USE_SINGLE_MEMORY_REGION_PER_DEVICE) 15 std::vector<size_t> offset(field.m_num_device, 0); 16 std::vector<size_t> size(field.m_num_device, 0); 17 size_t hsize = 0; 18 for (size_t blk = 0; blk < blockAttr.size(); ++blk) 19 { 20 // Set one-to-one MemoryRegion to device mapping. 21 auto mr = blk % field.m_num_device; 22 auto nsize = blockAttr[blk].size() * field.GetNumComponents(); 23 field.m_blk_to_mr_mapping.push_back(mr); 24 field.m_blk_to_mr_offset.push_back(offset[mr]); 25 offset[mr] += nsize; 26 27 // Compute MemoryRegion memory size. 28 size[mr] += nsize; 29 hsize += nsize; 30 } 31 32 // Allocate contiguous memory on the host. 33 if (!device_only) 34 { 35 field.m_host = static_cast<TData *>(::operator new[]( 36 hsize * sizeof(TData), std::align_val_t(field.m_alignment))); 37 std::memset((void *)field.m_host, 0, hsize * sizeof(TData)); 38 } 39 40 auto hsrc = field.m_host; 41 for (size_t mr = 0; mr < field.m_num_device; ++mr) 42 { 43 // Create memory region. 44 auto device_rank = mr; 45 if (!device_only) 46 { 47 field.m_memory_regions.push_back( 48 MemoryRegion<TData>::CreateFromHostPtr( 49 field.m_name + std::to_string(mr), hsrc, size[mr], 50 alignment, device_rank)); 51 hsrc += size; 52 } 53 else 54 { 55 field.m_memory_regions.push_back(MemoryRegion<TData>::Create( 56 field.m_name + std::to_string(mr), size[mr], alignment, 57 device_rank)); 58 } 59 } 60e 61 size_t hsize = 0; 62 for (size_t blk = 0; blk < blockAttr.size(); ++blk) 63 { 64 // Set one-to-one block to MemoryRegion mapping. 65 auto nsize = blockAttr[blk].size() * field.GetNumComponents(); 66 field.m_blk_to_mr_mapping.push_back(blk); 67 field.m_blk_to_mr_offset.push_back(0); 68 hsize += nsize; 69 } 70 71 // Allocate contiguous memory on the host. 72 if (!device_only) 73 { 74 field.m_host = static_cast<TData *>(::operator new[]( 75 hsize * sizeof(TData), std::align_val_t(field.m_alignment))); 76 std::memset((void *)field.m_host, 0, hsize * sizeof(TData)); 77 } 78 79 auto hsrc = field.m_host; 80 for (size_t blk = 0; blk < blockAttr.size(); ++blk) 81 { 82 // Create memory region. 83 auto device_rank = blk % field.m_num_device; 84 auto size = blockAttr[blk].size() * field.GetNumComponents(); 85 if (!device_only) 86 { 87 field.m_memory_regions.push_back( 88 MemoryRegion<TData>::CreateFromHostPtr( 89 field.m_name + std::to_string(blk), hsrc, size, 90 alignment, device_rank)); 91 hsrc += size; 92 } 93 else 94 { 95 field.m_memory_regions.push_back(MemoryRegion<TData>::Create( 96 field.m_name + std::to_string(blk), size, alignment, 97 device_rank)); 98 } 99 } 100if 101 for (size_t blk = 0; blk < blockAttr.size(); ++blk) 102 { 103 field.m_block_accessors.push_back(BlockAccessor( 104 blockAttr[blk], 105 field.m_memory_regions[field.m_blk_to_mr_mapping[blk]], 106 field.m_var_names.size(), field.m_blk_to_mr_offset[blk])); 107 } 108} 109 110 111 112brief Templated initialize method. 113 114 115late <typename MemSpace> void Initialize(const TData val) 116 117for (size_t mr = 0; mr < m_memory_regions.size(); ++mr) 118{ 119 m_memory_regions[mr].template Initialize<MemSpace>(val); 120}
The default implementation is the one MemoryRegion per block mapping. The one
MemoryRegion per compute unit mapping can be enable using the follow cmake command
1cmake ../ NEKTAR_USE_SINGLE_MEMORY_REGION_PER_DEVICE=ON
To guarantee contiguous memory allocation on the host side for the whole Field object, a
single contiguous memory chunk is explictly allocated during object construction:
1if (!device_only) 2{ 3 field.m_host = static_cast<TData *>(::operator new[]( 4 hsize * sizeof(TData), std::align_val_t(field.m_alignment))); 5 std::memset((void *)field.m_host, 0, hsize * sizeof(TData)); 6}
The specialized CreateFromHostPtr creator function is used to create MemoryRegion objects
without host memory allocation by using previously allocated host pointers as argument
parameter.
1static MemoryRegion<TData> CreateFromHostPtr(const std::string name, 2 TData *h_src, 3 const size_t size, 4 const size_t alignment, 5 const size_t device_rank = 0) 6{ 7 ... 8 return mr; 9}
Within CreateFromHostPtr, specialized MemoryStorage constructor is used:
1MemoryStorage(const std::string name, TData *h_src, const size_t size, 2 const size_t alignment, const unsigned int device_rank) 3{ 4 m_owned = false; 5 m_name = name; 6 m_size = size; 7 m_alignment = alignment; 8 m_device_rank = device_rank; 9 10 m_host = h_src; 11 m_device = nullptr; 12 m_host_valid = true; 13 m_device_valid = false; 14}
This specialized constructor simply borrows a host pointer, without additional host memory
allocation. The Field object conserves ownership of the memory and no host memory is
deallocated during MemoryStorage object destruction
1~MemoryStorage() 2{ 3 if (m_host && m_owned) 4 { 5 operator delete[](m_host, std::align_val_t(m_alignment)); 6 } 7 8 ... 9}
Instead, the host memory is deallocated by the Field object destructor, as the later has
ownership of the host memory.
1~Field() 2{ 3 if (m_host) 4 { 5 operator delete[](m_host, std::align_val_t(m_alignment)); 6 } 7 m_host = nullptr; 8}