45template <
typename TData, FieldState TState>
class MultiField;
53template <
typename TData, FieldState TState>
class Field
55 template <
typename TDataField, FieldState TStateField>
78 const unsigned int num_components,
const unsigned int num_homo_modes,
85 for (
unsigned int blk = 0; blk < blockAttr.size(); ++blk)
88 blockAttr[blk].CompSize() * num_components * num_homo_modes;
90 memAllocType, alignment);
93 num_components, num_homo_modes));
108 const unsigned int num_components,
const unsigned int num_homo_modes,
111 :
Field<TData, TState>(
"", blockAttr, num_components, num_homo_modes,
112 memAllocType, alignment)
129 const std::vector<std::string> components,
130 const unsigned int num_homo_modes,
137 for (
unsigned int blk = 0; blk < blockAttr.size(); ++blk)
140 blockAttr[blk].CompSize() * components.size() * num_homo_modes;
142 memAllocType, alignment);
145 components.size(), num_homo_modes));
160 const std::vector<std::string> components,
161 const unsigned int num_homo_modes,
164 :
Field<TData, TState>(
"", blockAttr, components, num_homo_modes,
165 memAllocType, alignment)
189 blocks.m_field =
this;
191 rhs.m_instantiated =
false;
193 rhs.m_component_names.clear();
194 rhs.m_num_homo_modes = 1;
195 rhs.m_host =
nullptr;
196 rhs.m_device =
nullptr;
197 rhs.m_block_accessors.clear();
200 rhs.m_sharedStorageOffset = 0;
232 m_name = std::move(rhs.m_name);
235 m_host = std::move(rhs.m_host);
244 blocks.m_field =
this;
247 rhs.m_instantiated =
false;
249 rhs.m_component_names.clear();
250 rhs.m_num_homo_modes = 1;
251 rhs.m_host =
nullptr;
252 rhs.m_device =
nullptr;
253 rhs.m_block_accessors.clear();
256 rhs.m_sharedStorageOffset = 0;
269 explicit operator bool()
const
279 "Field::GetInterleaveWidth - Object has not been "
280 "instantiated by custom constructor.");
283 std::vector<unsigned int> interleaveWidths;
286 interleaveWidths.push_back(
289 return interleaveWidths;
294 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
296 this->
GetBlocks()[blk].template SetInterleaveWidth<TData>(
303 ASSERTL1(interleaveWidths.size() == this->GetBlocks().size(),
304 "Number of interleave widths does not match number of "
306 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
308 this->
GetBlocks()[blk].template SetInterleaveWidth<TData>(
309 interleaveWidths[blk]);
315 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
317 this->
GetBlocks()[blk].template SetInterleaveWidth<TData>(
318 field.GetBlocks()[blk].GetInterleaveWidth());
323 const std::string &execSpace)
325 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
327 const unsigned int streamID = blk + 1;
328 this->
GetBlocks()[blk].ReshapeStorage(interleaveWidth, execSpace,
334 const std::string &execSpace)
336 ASSERTL1(interleaveWidths.size() == this->GetBlocks().size(),
337 "Number of interleave widths does not match number of "
339 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
341 const unsigned int streamID = blk + 1;
342 this->
GetBlocks()[blk].ReshapeStorage(interleaveWidths[blk],
343 execSpace, streamID);
355 template <
typename MemSpace>
356 void Initialize(
const TData val,
const unsigned int streamID = 0)
361 "Field::Initialize - Object has not been "
362 "instantiated by custom constructor.");
368 AllocateFieldStorage<MemSpace>(
this);
370 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
372 const unsigned int blkStreamID = streamID == 0 ? blk + 1 : streamID;
374 block.m_memory_region.template Initialize<MemSpace>(val, 0, 0,
378 ZeroPadding<MemSpace>(block, blkStreamID);
390 template <
typename TDataOut = TData,
class Alloc = std::allocator<TDataOut>>
396 "Field::ToVector - Object has not been "
397 "instantiated by custom constructor.");
403 AllocateFieldStorage<NektarSpaces::HostSpace>(
this);
405 unsigned int compSize = 0;
408 compSize += block.GetNumData() * block.GetNumElements();
426 template <
typename TDataOut = TData>
432 "Field::ToArray - Object has not been "
433 "instantiated by custom constructor.");
439 AllocateFieldStorage<NektarSpaces::HostSpace>(
this);
441 unsigned int compSize = 0;
444 compSize += block.GetNumData() * block.GetNumElements();
467 "Field::Copy - Object has not been "
468 "instantiated by custom constructor.");
474 AllocateFieldStorage<MemSpace>(&field);
479 AllocateFieldStorage<MemSpace>(
this);
483 std::stringstream msg;
485 msg <<
"Field::Copy - "
486 <<
"Block number mismatch between (" << field.GetName()
487 <<
") and (" << this->
GetName() <<
").";
493 std::stringstream msg;
495 msg <<
"Field::Copy - "
496 <<
"Memory size mismatch between (" << field.GetName()
497 <<
") and (" << this->
GetName() <<
").";
501 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
503 const unsigned int streamID = blk + 1;
504 this->
GetBlocks()[blk].m_memory_region.template Copy<MemSpace>(
505 field.GetBlocks()[blk].m_memory_region, streamID);
506 this->
GetBlocks()[blk].template SetInterleaveWidth<TData>(
507 field.GetBlocks()[blk].GetInterleaveWidth());
519 template <
typename MemSpace,
typename TDataIn,
520 class Alloc = std::allocator<TDataIn>>
526 "Field::CopyVector - Object has not been "
527 "instantiated by custom constructor.");
533 AllocateFieldStorage<MemSpace>(
this);
535 unsigned int compSize = 0;
538 compSize += block.GetNumData() * block.GetNumElements();
544 std::stringstream msg;
546 msg <<
"Field::CopyVector - "
547 <<
"Memory size mismatch between (std::vector) and ("
552 InterleaveFromHostPtr<MemSpace>(array.data(), compSize);
563 template <
typename MemSpace,
typename TDataIn>
569 "Field::CopyArray - Object has not been "
570 "instantiated by custom constructor.");
576 AllocateFieldStorage<MemSpace>(
this);
578 unsigned int compSize = 0;
581 compSize += block.GetNumData() * block.GetNumElements();
587 std::stringstream msg;
589 msg <<
"Field::CopyArray - "
590 <<
"Memory size mismatch between (Nektar::array) and ("
595 InterleaveFromHostPtr<MemSpace>(array.data(), compSize);
608 "Field::GetBlocks - Object has not been "
609 "instantiated by custom constructor.");
625 "Field::size - Object has not been "
626 "instantiated by custom constructor.");
647 "Field::GetName - Object has not been "
648 "instantiated by custom constructor.");
664 "Field::GetNumComponents - Object has not been "
665 "instantiated by custom constructor.");
681 "Field::GetComponentNames - Object has not been "
682 "instantiated by custom constructor.");
698 "Field::GetNumHomoModes - Object has not been "
699 "instantiated by custom constructor.");
726 const unsigned int streamID = 0;
758 template <
typename MemSpace>
761 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
763 size_t alignment_offset = 0;
766 if (!field->m_host && field->m_sharedStorage)
768 auto src = field->m_sharedStorage->template GetPtr<MemSpace>() +
769 field->m_sharedStorageOffset;
772 for (
unsigned int blk = 0; blk < field->GetBlocks().
size();
775 field->GetBlocks()[blk].m_memory_region.SetHostStorage(src);
776 src += field->GetBlocks()[blk].m_memory_region.size();
786 hostMalloc(&field->m_host, field->size() *
sizeof(TData),
792 size_t aligned_bytes_size =
793 field->size() *
sizeof(TData) + field->m_alignment;
796 if ((
size_t)field->m_host % field->m_alignment)
800 (size_t)field->m_host % field->m_alignment;
804 auto src = (TData *)((
size_t)field->m_host + alignment_offset);
805 std::memset((
void *)src, 0, field->size() *
sizeof(TData));
807 for (
unsigned int blk = 0; blk < field->GetBlocks().
size();
810 field->GetBlocks()[blk].m_memory_region.SetHostStorage(src);
811 src += field->GetBlocks()[blk].m_memory_region.size();
815 else if constexpr (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
818 if (!field->m_device && field->m_sharedStorage)
820 auto src = field->m_sharedStorage->template GetPtr<MemSpace>() +
821 field->m_sharedStorageOffset;
822 field->m_device = src;
824 for (
unsigned int blk = 0; blk < field->GetBlocks().
size();
827 field->GetBlocks()[blk].m_memory_region.SetDeviceStorage(
829 src += field->GetBlocks()[blk].m_memory_region.size();
835 if (!field->m_device)
837 const unsigned int streamID = 0;
838 deviceMalloc(&field->m_device, field->size() *
sizeof(TData),
839 streamID, field->m_memAllocType);
840 deviceMemset(field->m_device, 0, field->size() *
sizeof(TData),
844 auto src = field->m_device;
845 for (
unsigned int blk = 0; blk < field->GetBlocks().
size();
848 field->GetBlocks()[blk].m_memory_region.SetDeviceStorage(
850 src += field->GetBlocks()[blk].m_memory_region.size();
864 template <
typename TDataOut>
867 const unsigned int nCompHomo =
872 block.template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
873 const size_t nSize = block.CompSize();
874 const size_t nElmts = block.GetNumElements();
875 const unsigned int npts = block.GetNumData();
876 const unsigned int interleaveWidth = block.GetInterleaveWidth();
877 const unsigned int elmtGroupSize = npts * interleaveWidth;
878 const size_t numElmtGroups =
879 (nElmts + interleaveWidth - 1) / interleaveWidth;
880 for (
unsigned int n = 0; n < nCompHomo; ++n)
882 if (interleaveWidth == 1)
884 std::copy(src, src + nElmts * npts, dst + n * compSize);
888 for (
size_t e = 0; e < numElmtGroups; ++e)
890 for (
unsigned int vecElem = 0;
891 vecElem < interleaveWidth &&
892 e * interleaveWidth + vecElem < nElmts;
895 const TData *elmtSrc =
896 src + e * elmtGroupSize + vecElem;
899 (e * interleaveWidth + vecElem) * npts;
900 for (
unsigned int idx = 0; idx < npts; ++idx)
902 elmtDst[idx] = elmtSrc[idx * interleaveWidth];
909 dst += nElmts * npts;
922 template <
typename MemSpace,
typename TDataIn>
925 const unsigned int nCompHomo =
927 const std::string execSpace =
928 std::is_same_v<MemSpace, NektarSpaces::HostSpace> ?
"Serial"
930 for (
unsigned int blk = 0; blk < this->
GetBlocks().size(); ++blk)
932 const unsigned int streamID = blk + 1;
934 const size_t nSize = block.CompSize();
935 const size_t nElmts = block.GetNumElements();
936 const unsigned int npts = block.GetNumData();
937 const unsigned int interleaveWidth = block.GetInterleaveWidth();
938 for (
unsigned int n = 0; n < nCompHomo; ++n)
940 block.m_memory_region.template CopyFromHostPtr<MemSpace>(
941 src + n * compSize, nElmts * npts, n * nSize, streamID);
943 if (interleaveWidth != 1)
945 block.template SetInterleaveWidth<TData>(1);
946 block.ReshapeStorage(interleaveWidth, execSpace, streamID);
948 src += nElmts * npts;
972 template <
typename MemSpace>
974 const unsigned int streamID)
978 const unsigned int ncomp =
980 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
1019 const std::vector<std::string> components,
1020 const unsigned int num_homo_modes,
1022 const size_t offset)
1023 :
Field<TData, TState>(name, blockAttr, components, num_homo_modes,
1024 storage->GetMemAllocType(),
1025 storage->GetAlignment())
1027 if (offset + this->
size() > storage->size())
1030 "Field - Shared storage too small for the field.");
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Get the BlockAttributes for a given field state from an ExpList. This method basically captures ident...
unsigned int GetNumComponents() const
Gets the number of components.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
MemoryRegion< TData > m_memory_region
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
unsigned int GetInterleaveWidth(void) const
size_t CompSize(void) const
unsigned int GetNumData(void) const
size_t GetNumElements(void) const
A Field represents expansion data to be operated on.
void InterleaveFromHostPtr(const TDataIn *src, const size_t compSize)
Copy data from host memory, laid out component by component and element by element without padding,...
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const std::shared_ptr< SharedFieldStorage< TData > > &storage, const size_t offset)
Construct a new Field object in storage shared with other fields. For use by MultiField only.
static void AllocateFieldStorage(Field< TData, TState > *field)
Allocate contiguous host OR device memory accross all MemoryRegion objects belonging to a Field objec...
Field(const std::vector< BlockAttributes< TState > > blockAttr, const unsigned int num_components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
void CopyArray(const Nektar::Array< Nektar::OneD, TDataIn > &array)
Templated copy method. This method copies data from a Nektar::Array<Nektar::OneD, TDataIn> laid out a...
void CopyVector(const std::vector< TDataIn, Alloc > &array)
Templated copy method. This method copies data from a std::vector laid out as ToVector() returns it,...
void SetInterleaveWidth(Field &field)
size_t size() const
Gets the size of the field.
void ReleaseStorage()
Free the contiguous host and device storage this field owns, allocated by AllocateFieldStorage(),...
static void ZeroPadding(BlockAccessor< TData, TState > &block, const unsigned int streamID)
Zero the padding of block in place, in MemSpace.
friend class BlockAccessor
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
Field(const Field &)=delete
Constructor methods - No copy methods.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
std::shared_ptr< SharedFieldStorage< TData > > m_sharedStorage
Shared storage the field points into, if any.
Field(Field &&rhs)
Construct a new Field object by moving storage from an existing Field object.
size_t m_sharedStorageOffset
Offset of the field in m_sharedStorage.
std::vector< unsigned int > GetInterleaveWidth(void) const
Field & operator=(Field &&rhs)
Move assignment operator.
std::vector< std::string > m_component_names
bool m_instantiated
Flag indicating if the current object has been instantiated.
std::string GetName(void) const
Gets the name of the field.
MemAllocType m_memAllocType
void ReshapeStorage(const std::vector< unsigned int > &interleaveWidths, const std::string &execSpace)
std::vector< TDataOut, Alloc > ToVector()
Copy the data to a std::vector, component by component and element by element, without padding,...
Field & operator=(const Field &)=delete
unsigned int m_num_homo_modes
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
void Copy(Field &field)
Templated copy method. This method copies data from a Field.
std::vector< std::string > GetComponentNames() const
Gets a copy of the components names for a vector field.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
void Initialize(const TData val, const unsigned int streamID=0)
Templated initialize method: set every entry to val, but the padding, which is zero.
void SetInterleaveWidth(const unsigned int &interleaveWidth)
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
void DeInterleaveToHostPtr(TDataOut *dst, const size_t compSize)
Copy the data to host memory, component by component and element by element, without padding....
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
Field(const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
void SetInterleaveWidth(const std::vector< unsigned int > &interleaveWidths)
std::vector< BlockAccessor< TData, TState > > m_block_accessors
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const unsigned int num_components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
A MemoryRegion represents a memory region.
A set of fields stored as the columns of a column-major matrix.
Contiguous host and device storage shared by several Field objects.
static constexpr size_t host_memory_alignment
void deviceFree(TData *src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void hostFreePinned(TData *src)
void hostFree(TData *src, const size_t alignment)
void deviceZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width, const unsigned int streamID)
void deviceMemset(TData *dst, const int val, const size_t size, const unsigned int streamID)
void hostZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width)
static void nekStreamSynchronize(const unsigned int streamID)
void deviceMalloc(TData **src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void hostMallocPinned(TData **src, const size_t size)
void hostMalloc(TData **src, const size_t size, const size_t alignment)