86template <
typename TData, FieldState TState>
class Field;
99 template <
typename TDataField, FieldState TState>
friend class Field;
160 rhs.m_instantiated =
false;
161 rhs.m_host_owned =
true;
162 rhs.m_device_owned =
true;
163 rhs.m_host_valid =
false;
164 rhs.m_device_valid =
false;
165 rhs.m_host =
nullptr;
166 rhs.m_host_aligned =
nullptr;
167 rhs.m_device =
nullptr;
222 rhs.m_instantiated =
false;
223 rhs.m_host_owned =
true;
224 rhs.m_device_owned =
true;
225 rhs.m_host_valid =
false;
226 rhs.m_device_valid =
false;
227 rhs.m_host =
nullptr;
228 rhs.m_host_aligned =
nullptr;
229 rhs.m_device =
nullptr;
243 template <
typename MemSpace,
typename MemAccess>
245 [[maybe_unused]]
const unsigned int streamID = 0)
250 "MemoryRegion::GetPtr - Object has not been "
251 "instantiated by custom constructor.");
254 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
262 if constexpr (std::is_same_v<MemAccess, ReadOnly>)
268 "MemoryRegion::GetPtr - attempt to access host "
270 m_name +
") without it being allocated.");
276 "MemoryRegion::GetPtr - attempt to get a host "
278 m_name +
") before the data is initialized.");
292 else if constexpr (std::is_same_v<MemAccess, WriteOnly>)
300 size_t aligned_bytes_size =
340 else if constexpr (std::is_same_v<MemAccess, ReadWrite>)
347 "MemoryRegion::GetPtr - attempt to access host data (" +
348 m_name +
") without it being allocated.");
354 "MemoryRegion::GetPtr - attempt to get a host "
356 m_name +
") before the data is initialized.");
367 else if constexpr (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
375 if constexpr (std::is_same_v<MemAccess, ReadOnly>)
381 "MemoryRegion::GetPtr - attempt to access device "
383 m_name +
") without it being allocated.");
389 "MemoryRegion::GetPtr - attempt to get a host "
391 m_name +
") before the data is initialized.");
405 else if constexpr (std::is_same_v<MemAccess, WriteOnly>)
429 else if constexpr (std::is_same_v<MemAccess, ReadWrite>)
435 "MemoryRegion::GetPtr - attempt to access device "
437 m_name +
") without it being allocated.");
443 "MemoryRegion::GetPtr - attempt to get a host "
445 m_name +
") before the data is initialized.");
472 template <
typename MemSpace,
typename TDataIn,
473 class Alloc = std::allocator<TDataIn>>
475 const std::string name, std::vector<TDataIn, Alloc>
const &array,
476 const unsigned int streamID = 0,
482 mr.template CopyFromHostPtr<MemSpace>(array.data(), array.size(), 0,
498 template <
typename MemSpace,
typename TDataIn,
499 class Alloc = std::allocator<TDataIn>>
501 std::vector<TDataIn, Alloc>
const &array,
502 const unsigned int streamID = 0,
507 "", array, streamID, memAllocType, alignment);
523 template <
typename MemSpace,
typename TDataIn>
525 const std::string name,
527 const unsigned int streamID = 0,
533 mr.template CopyFromHostPtr<MemSpace>(array.data(), array.size(), 0,
549 template <
typename MemSpace,
typename TDataIn>
552 const unsigned int streamID = 0,
557 "", array, streamID, memAllocType, alignment);
567 template <
typename MemSpace>
569 const size_t offset = 0,
const unsigned int streamID = 0)
574 "MemoryRegion::Initialize - Object has not been "
575 "instantiated by custom constructor.");
578 if (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
586 size_t aligned_bytes_size =
617 if constexpr (std::is_floating_point_v<TData> ||
618 std::is_integral_v<TData>)
623 std::memset(dst, 0,
size *
sizeof(TData));
628 std::fill(dst, dst +
size, val);
633 std::fill(dst, dst +
size, val);
639 else if (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
656 if constexpr (std::is_floating_point_v<TData> ||
657 std::is_integral_v<TData>)
685 template <
typename MemSpace>
687 [[maybe_unused]]
const unsigned int streamID = 0)
691 std::stringstream msg;
693 msg <<
"MemoryRegion::Copy - "
694 <<
"Memory size mismatch between (" << rhs.
GetName()
695 <<
") and (" << this->
GetName() <<
").";
699 auto dst = this->
template GetPtr<MemSpace, WriteOnly>();
700 auto src = rhs.template GetPtr<MemSpace, ReadOnly>();
701 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
703 std::copy(src, src + this->
size(), dst);
705 else if constexpr (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
707 deviceMemcpy<eDeviceToDevice>(
708 dst, src, this->
size() *
sizeof(TData), streamID);
719 template <
typename MemSpace,
typename TDataIn,
720 class Alloc = std::allocator<TDataIn>>
722 const unsigned int streamID = 0)
724 if (this->
size() != array.size())
726 std::stringstream msg;
728 msg <<
"MemoryRegion::CopyVector - "
729 <<
"Memory size mismatch between (std::vector) and ("
734 this->CopyFromHostPtr<MemSpace>(array.data(), array.size(), 0,
745 template <
typename MemSpace,
typename TDataIn>
747 const unsigned int streamID = 0)
749 if (this->
size() != array.size())
751 std::stringstream msg;
753 msg <<
"MemoryRegion::CopyArray - "
754 <<
"Memory size mismatch between (Nektar::array) and ("
759 this->CopyFromHostPtr<MemSpace>(array.data(), array.size(), 0,
768 template <
typename TDataOut = TData,
class Alloc = std::allocator<TDataOut>>
774 "MemoryRegion::ToVector - Object has not been "
775 "instantiated by custom constructor.");
778 if constexpr (std::is_same_v<TDataOut, TData>)
781 this->
template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
782 return std::vector<TDataOut, Alloc>(ptr, ptr +
m_size);
786 std::vector<TDataOut, Alloc> vector(
m_size);
790 this->
template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
791 auto vecPtr = vector.data();
793 std::copy(ptr, ptr +
m_size, vecPtr);
804 template <
typename TDataOut = TData>
810 "MemoryRegion::ToArray - Object has not been instantiated "
811 "by custom constructor.");
814 if constexpr (std::is_same_v<TDataOut, TData>)
818 this->
template GetPtr<NektarSpaces::HostSpace, ReadOnly>());
826 this->
template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
827 auto arrPtr = array.data();
829 std::copy(ptr, ptr +
m_size, arrPtr);
845 "MemoryRegion::GetAlignment - Object has not been "
846 "instantiated by custom constructor.");
871 "MemoryRegion::GetName - Object has not been instantiated "
872 "by custom constructor.");
892 const unsigned int streamID = 0;
931 template <
typename MemSpace,
typename TDataIn>
933 const size_t offset = 0,
934 const unsigned int streamID = 0)
936 if (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
944 size_t aligned_bytes_size =
973 if constexpr (std::is_same_v<TDataIn, TData>)
975 std::memcpy(dst, src,
size *
sizeof(TData));
979 std::vector<TData> tmp(
size);
980 std::copy(src, src +
size, tmp.data());
981 std::memcpy(dst, tmp.data(),
size *
sizeof(TData));
987 else if (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
1002 if constexpr (std::is_same_v<TDataIn, TData>)
1004 deviceMemcpy<eHostToDevice>(dst, src,
size *
sizeof(TData),
1009 std::vector<TData> tmp(
size);
1010 std::copy(src, src +
size, tmp.data());
1011 deviceMemcpy<eHostToDevice>(dst, tmp.data(),
1012 size *
sizeof(TData), streamID);
1034 "MemoryRegion::HostToDeviceCopy - attempt to transfer "
1035 "data from the host (" +
1037 ") without any valid host memory allocated.");
1051 m_size *
sizeof(TData), streamID);
1057 "MemoryRegion::HostToDeviceCopy - attempt to transfer "
1060 ") to the device without any valid host data.");
1076 "MemoryRegion::DeviceToHostCopy - attempt to transfer "
1077 "data from the device (" +
1079 ") without any valid device memory allocated.");
1088 size_t aligned_bytes_size =
1115 m_size *
sizeof(TData), streamID);
1121 "MemoryRegion::DeviceToHostCopy - attempt to transfer "
1124 ") to the host without any valid device data.");
1146 "MemoryRegion::SetHostStorage - Host storage has already "
1171 "MemoryRegion::SetDeviceStorage - Device storage has already "
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
A Field represents expansion data to be operated on.
A MemoryRegion represents a memory region.
TData * m_host_aligned
Host (aligned) memory pointer.
static MemoryRegion< TData > FromArray(const std::string name, Nektar::Array< Nektar::OneD, TDataIn > const &array, const unsigned int streamID=0, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Static templated creation method. This method creates a new MemoryRegion that copies data from a Nekt...
void SetDeviceStorage(TData *src)
Initialize device pointer for an existing (external) pointer. Specialized function used in Field....
static MemoryRegion< TData > FromVector(const std::string name, std::vector< TDataIn, Alloc > const &array, const unsigned int streamID=0, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Static templated creation method. This method creates a new MemoryRegion that copies data from a std:...
MemoryRegion(const MemoryRegion &)=delete
Constructor methods - No copy methods.
void SetHostStorage(TData *src)
Initialize host pointer for an existing (external) pointer. Specialized function used in Field....
std::string GetName() const
Get the storage name.
const_if< std::is_same_v< MemAccess, ReadOnly >, TData >::type * GetPtr(const unsigned int streamID=0)
Get the pointer to the host/device memory.
bool m_host_owned
Flag indicating if the host pointer is owned by the current object.
size_t size() const
Get the storage size.
TData * m_host
Host memory pointer.
void DeviceToHostCopy(const unsigned int streamID)
Perform a device to host copy.
size_t GetAlignment() const
Get the storage memory alignment.
void CopyArray(const Nektar::Array< Nektar::OneD, TDataIn > &array, const unsigned int streamID=0)
Templated copy method. This method copies data from a Nektar::Array<Nektar::OneD, TDataIn>
bool m_host_valid
Flag indicating if the host data is valid.
MemoryRegion(const size_t size, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Constructor methods - create a new memory region.
MemoryRegion & operator=(MemoryRegion &&rhs)
Move assignment operator.
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy data to a Nektar::Array.
std::vector< TDataOut, Alloc > ToVector()
Copy data to a std:vector.
size_t m_size
Storage size (number of elements).
MemoryRegion & operator=(const MemoryRegion &)=delete
static MemoryRegion< TData > FromArray(Nektar::Array< Nektar::OneD, TDataIn > const &array, const unsigned int streamID=0, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Static templated creation method. This method creates a new MemoryRegion that copies data from an Nek...
void ReleaseStorage()
Free the host and device storage this region owns and return it to the default, uninstantiated state.
MemoryRegion(MemoryRegion &&rhs)
Constructor methods - move from another MemoryRegion.
bool m_instantiated
Flag indicating if the current object has been instantiated.
void CopyVector(const std::vector< TDataIn, Alloc > &array, const unsigned int streamID=0)
Templated copy method. This method copies data from a std::vector.
MemoryRegion(const std::string name, const size_t size, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Constructor methods - create a new memory region.
MemAllocType m_memAllocType
Host memory allocation type ( eHostPageable, eHostPinned, eDeviceMemoryPool, eHostPinnedDeviceMemoryP...
void HostToDeviceCopy(const unsigned int streamID)
Perform a host to device copy.
void Copy(MemoryRegion &rhs, const unsigned int streamID=0)
Copy data from one region to another region.
static MemoryRegion< TData > FromVector(std::vector< TDataIn, Alloc > const &array, const unsigned int streamID=0, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Static templated creation method. This method creates a new MemoryRegion that copies data from a std:...
std::string m_name
Name to identify the current object.
bool m_device_valid
Flag indicating if the device data is valid.
bool m_device_owned
Flag indicating if the device pointer is owned by the current object.
TData * m_device
Device memory pointer.
void Initialize(const TData val, const size_t count=0, const size_t offset=0, const unsigned int streamID=0)
initialize the storage memory.
~MemoryRegion()
Destructor method.
void CopyFromHostPtr(const TDataIn *src, const size_t size, const size_t offset=0, const unsigned int streamID=0)
Templated copy method.
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 deviceMemset(TData *dst, const int val, const size_t size, const unsigned int streamID)
static void nekStreamSynchronize(const unsigned int streamID)
void deviceMalloc(TData **src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void deviceFill(TData *dst, const TData val, const size_t size, const unsigned int streamID)
void hostMallocPinned(TData **src, const size_t size)
FieldState
Possible states for Field data.
std::string FieldStateToString(void)
void hostMalloc(TData **src, const size_t size, const size_t alignment)