22# define DNDS_ARRAY_HPP
95 template <
class T, rowsize _row_size = 1, rowsize _row_max = _row_size, rowsize _align = NoAlign>
460 "Resize(index nSize) is invalid call");
476 template <
class TFRowSize>
483 _pRowStart = std::make_shared<typename decltype(_pRowStart)::element_type>(
nSize + 1);
489 static_assert(
_dataLayout ==
CSR,
"Only Non Uniform, CSR for now");
490 static_assert(std::is_invocable_r_v<rowsize, TFRowSize, index>,
"Call invalid");
577 "query position i[{}] out of range [0, {}), array: {}",
581 "query position j[{}] out of range [0, {}), array: {}",
609 return const_cast<T &
>(
at(iRow,
iCol));
632 return this->
view().operator[](iRow);
666 return static_cast<const T *
>(
const_cast<self_type *
>(
this)->
operator[](iRow));
682 return _data.dataDevice();
690 if (this->
Size() == 0)
712 if constexpr (std::is_trivially_copyable_v<T>)
722 template <
class TRowSizeFunc>
725 static_assert(
_dataLayout ==
CSR,
"ResizeRowsAndCompress only for CSR");
774 o <<
A(i,
j) <<
"\t";
795 this->_pRowSizes =
R._pRowSizes;
796 this->_pRowStart =
R._pRowStart;
797 this->_dataUncompressed =
R._dataUncompressed;
800 this->deviceBackend =
R.deviceBackend;
861 if constexpr (std::is_same_v<T, index>)
866 else if constexpr (std::is_same_v<T, real>)
868 if (!std::dynamic_pointer_cast<Serializer::SerializerJSON>(
serializerP))
898 if constexpr (std::is_same_v<T, index>)
904 else if constexpr (std::is_same_v<T, real>)
906 if (!std::dynamic_pointer_cast<Serializer::SerializerJSON>(
serializerP))
967 "CSR collective write requires isDist dataOffset from ParArray");
1028 "Array::ReadSerializer must not receive EvenSplit; ParArray should resolve it first");
1039 if (_row_size !=
rsR)
1046 else if (offset.
isDist())
1138 "CSR collective read requires isDist offset from ParArray");
1204 fmt::format(
"CSR need compressing before to_host: {}",
1217 fmt::format(
"CSR need compressing before to_device: {}",
1219 _data.to_device(backend);
1230 _data.clear_device();
1239 template <DeviceBackend B>
1242 template <DeviceBackend B>
1248 template <DeviceBackend B>
1254 fmt::format(
"{}: not on device {}, currently on {}",
1270 template <DeviceBackend B>
1276 fmt::format(
"{}: not on device {}, currently on {}",
1300 template <DeviceBackend B>
1326 template <DeviceBackend B>
1333 template <DeviceBackend B>
Array layout descriptors, non-owning views, row views, and iterator base.
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_DEVICE_CALLABLE
Device memory abstraction layer with backend-specific storage and factory creation.
Non-owning device-side views of Array objects for host and CUDA backends.
#define DNDS_assert_info(expr, info)
Debug-only assertion with an extra std::string info message.
#define DNDS_assert(expr)
Debug-only assertion (compiled out when DNDS_NDEBUG is defined). Prints the expression + file/line + ...
#define DNDS_check_throw_info(expr, info)
Same as DNDS_check_throw but attaches a user-supplied info message to the thrown std::runtime_error.
#define DNDS_check_throw(expr)
Runtime check active in both debug and release builds. Throws std::runtime_error if expr evaluates to...
Base types and abstract interface for array serialization.
Per-rank JSON serializer implementing the SerializerBase interface.
Host-device vector types with optional GPU storage and device-side views.
Non-owning device-callable view of an Array, specialised per DeviceBackend.
CRTP base for row-granularity iterators over an Array / ArrayView.
std::ptrdiff_t difference_type
Compile-time layout descriptor deducing the concrete DataLayout from element type and row-size templa...
static bool ArraySignatureIsCompatible(const std::string &v)
Whether a stored signature can be read into this array type.
static std::tuple< int, int, int, int > ParseArraySignatureTuple(const std::string &v)
Parse a signature string into (sizeof_T, row_size, row_max, align).
static std::string GetArraySignatureRelaxed()
Signature with _row_size / _row_max replaced by DynamicSize.
static const DataLayout _dataLayout
static constexpr DataLayout _GetDataLayout()
Deduce the DataLayout tag from the template parameters.
static const size_t sizeof_T
static std::string GetArrayName()
Human-readable type identifier including element typeid, sizes, and alignment.
static std::string GetArraySignature()
Compiler-independent identifier used by serializers to tag an array.
Non-owning view of a single row: {pointer, size}.
const T & at(index iRow, rowsize iCol) const
Bounds-checked element read (not device-callable because CSR decompressed uses std::vector::at which ...
DNDS_DEVICE_CALLABLE index Size() const
Number of rows in the viewed array.
DNDS_DEVICE_CALLABLE T * data()
Raw pointer to the start of the flat data buffer.
DNDS_DEVICE_CALLABLE rowsize RowSize() const
Uniform row width for fixed layouts (asserts otherwise).
std::conditional_t< _dataLayout==TABLE_Max||_dataLayout==TABLE_Fixed, rowsize, EmptyNoDefault > _row_size_dynamic
Random-access iterator over rows for a given device backend.
DNDS_DEVICE_CALLABLE reference operator*()
DNDS_DEVICE_CALLABLE reference operator*() const
std::random_access_iterator_tag iterator_category
DNDS_DEVICE_CALLABLE iterator(const iterator &)=default
DNDS_DEVICE_CALLABLE ~iterator()=default
DNDS_DEVICE_CALLABLE iterator(const view_type &n_view, index n_iRow)
Core 2D variable-length array container, the storage foundation of DNDSR.
t_pRowStart getRowStart()
Shared pointer to the row-start index (CSR layout only).
rowsize RowSizeField(index iRow) const
Per-row "field" size for CSR (= actual row width). Invalid elsewhere.
const T & operator()(index iRow, rowsize iCol=0) const
Bounds-checked 2D element access (read-only).
t_DataUncompressed _dataUncompressed
friend std::ostream & operator<<(std::ostream &o, const Array< T, _row_size, _row_max, _align > &A)
Pretty-print rows, one per line, tab-separated.
void to_device(DeviceBackend backend=DeviceBackend::Host)
Mirror the flat/structural buffers to a target device (e.g. CUDA).
std::size_t hash()
Combined hash of size, structural arrays, and data.
DeviceBackend deviceBackend
void Resize(index nSize, rowsize nRow_size_dynamic)
Resize the array, setting a uniform or maximum row width.
void CopyData(const self_type &R)
Deep copy alias. Currently delegates to clone; kept for API compatibility and to allow a future true ...
void ReadSerializer(Serializer::SerializerBaseSSP serializerP, const std::string &name, Serializer::ArrayGlobalOffset &offset, Serializer::ArrayGlobalOffset &dataOffset)
Deserialize (read) array data from a serializer.
const T * operator[](index iRow) const
Const row pointer, see the non-const overload.
size_t FullSizeBytes() const
Total footprint in bytes including structural arrays.
T * operator[](index iRow)
Return a raw pointer to the start of row iRow.
rowsize DataStride() const
Number of T elements per row in flat storage (data stride). For TABLE_StaticMax/TABLE_Max,...
void Decompress()
Layout-polymorphic decompress: no-op for non-CSR, calls CSRDecompress for CSR.
void ReserveRow(index iRow, rowsize nRowSize)
Reserve capacity for a CSR decompressed row without changing its size.
rowsize RowSize() const
Uniform row width for fixed layouts (no row index needed).
t_deviceView< B > deviceView()
Mutable device-callable view (Eigen::Map-style row access on GPU).
rowsize _row_size_dynamic
rowsize RowSize(index iRow) const
Width used by row iRow in number of T elements.
void __WriteSerializerData(const Serializer::SerializerBaseSSP &serializerP, Serializer::ArrayGlobalOffset offset)
bool IfCompressed_() const
void WriteSerializer(Serializer::SerializerBaseSSP serializerP, const std::string &name, Serializer::ArrayGlobalOffset offset, Serializer::ArrayGlobalOffset dataOffset=Serializer::ArrayGlobalOffset_Unknown)
Serialize (write) array data to a serializer.
static constexpr bool IsCSR()
t_Data & RawDataVector()
Access to the underlying flat buffer (host_device_vector<T>).
const T & at(index iRow, rowsize iCol) const
Bounds-checked element access.
DeviceBackend device() const
Current device backend the data is mirrored on, or Unknown if host-only.
bool IfCompressed() const
(CSR only) Whether the array is in packed / flat form.
static constexpr DataLayout GetDataLayoutStatic()
Compile-time layout tag (one of TABLE_StaticFixed, TABLE_Fixed, TABLE_StaticMax, TABLE_Max,...
void to_host()
Mirror the flat/structural buffers back to host memory.
t_View view()
Produce a lightweight, device-agnostic view onto the array.
ArrayDeviceView< B, T, _row_size, _row_max, _align > t_deviceView
ArrayView< T, _row_size, _row_max, _align > t_View
iterator< B > begin()
Iterator to the first row, viewed on device backend B.
void SwapData(self_type &R)
Swap the storage of two arrays in-place.
void ResizeRowsAndCompress(TRowSizeFunc &&rowSizeFunc)
Set per-row sizes from a source, applying an index mapping, then compress. For CSR layout only....
t_pRowSizes getRowSizes()
Shared pointer to the per-row size vector (TABLE_Max / TABLE_StaticMax).
self_type & operator=(const self_type &R)
Copy-assignment; implemented via clone with self-assign guard.
void Resize(index nSize)
Resize using only the row count (layouts with an implicit row width).
void CopyRowFrom(index dstRow, const self_type &src, index srcRow)
Copy raw row data from another Array of the same type. Works for all layouts (StaticFixed,...
ReadSerializerMetaResult ReadSerializerMeta(Serializer::SerializerBaseSSP serializerP, const std::string &name)
Reads only metadata from a serialized array without reading data.
void __ReadSerializerStructuralAndResolveDataOffset(const Serializer::SerializerBaseSSP &serializerP, Serializer::ArrayGlobalOffset &offset, Serializer::ArrayGlobalOffset &dataOffset)
Reads structural data (pRowStart / pRowSizes) and resolves dataOffset.
size_t DataSizeBytes() const
Flat buffer size in bytes (= DataSize() * sizeof(T)).
Array(ObjName objName)
Named constructor: sets the object name for tracing/debugging. Delegates to the default constructor,...
std::vector< std::vector< value_type > > t_DataUncompressed
void ResizeRow(index iRow, rowsize nRowSize)
Change the width of a single row.
size_t DataSize() const
Total number of T elements currently stored in the flat buffer.
void CSRCompress()
(CSR only) Pack the nested-vector representation into a flat buffer plus _pRowStart....
void clear_device()
Release any device-side mirror of this array's buffers.
void Resize(index nSize, TFRowSize &&FRowSize)
Resize a CSR array directly to the compressed form via a width functor.
Array()=default
Default-constructed array: empty, no storage.
Array(const self_type &R)
Copy constructor (same semantics as clone).
T * data(DeviceBackend B=DeviceBackend::Unknown)
Raw pointer to the flat data buffer.
T & operator()(index iRow, rowsize iCol=0)
Bounds-checked 2D element access (writable).
void Compress()
Layout-polymorphic compress: no-op for non-CSR, calls CSRCompress for CSR.
rowsize RowSizeMax() const
Maximum allowed row width for TABLE_Max / TABLE_StaticMax.
t_deviceViewConst< B > deviceView() const
Const device-callable view. See non-const overload.
void __ReadSerializerData(const Serializer::SerializerBaseSSP &serializerP, Serializer::ArrayGlobalOffset &offset)
void CSRDecompress()
(CSR only) Switch to the uncompressed (nested vector) representation.
index Size() const
Number of rows currently stored. O(1).
void clone(const self_type &R)
Shallow clone: copies all metadata and shares structural/data storage.
rowsize RowSizeField() const
"Logical" row-field width used by derived (Eigen) arrays: max for padded layouts, uniform width for f...
iterator< B > end()
Iterator one past the last row, viewed on device backend B.
void ReadSerializer(Serializer::SerializerBaseSSP serializerP, const std::string &name, Serializer::ArrayGlobalOffset &offset)
Convenience overload that discards the dataOffset output.
constexpr DataLayout GetDataLayout()
Runtime accessor for the layout tag (constexpr-folded).
void __ReadSerializerDataAndPropagateOffset(const Serializer::SerializerBaseSSP &serializerP, Serializer::ArrayGlobalOffset &offset, Serializer::ArrayGlobalOffset &dataOffset)
Reads flat data and propagates resolved offsets back to the caller.
Mixin base class providing a runtime instance name for tracing/debugging.
std::string getObjectIdentity(const std::string &sig) const
void setObjectName(const std::string &name)
Describes one rank's window into a globally-distributed dataset.
index offset() const
Global offset of this rank's data (or a sentinel value, see Offset_Parts etc.).
index size() const
Local size this rank owns (in element units of the caller's choosing).
bool isDist() const
Whether this descriptor carries a real distributed offset (rather than a sentinel like Offset_Parts).
ssp< SerializerBase > SerializerBaseSSP
the host side operators are provided as implemented
DeviceBackend
Enumerates the backends a DeviceStorage / Array can live on.
@ Unknown
Unset / sentinel.
const char * device_backend_name(DeviceBackend B)
Canonical string name for a DeviceBackend (used in log messages).
int32_t rowsize
Row-width / per-row element-count type (signed 32-bit).
DNDS_CONSTANT const rowsize DynamicSize
Template parameter flag: "row width is set at runtime but uniform".
DataLayout
Enumeration of the five concrete data layouts supported by Array.
@ TABLE_StaticFixed
Fixed row width, known at compile time.
@ TABLE_Max
Padded variable rows; max width set at runtime.
@ CSR
Compressed Sparse Row (flat buffer + row-start index).
@ TABLE_StaticMax
Padded variable rows; max width fixed at compile time.
@ TABLE_Fixed
Fixed row width, set at runtime (uniform across rows).
DNDS_CONSTANT const rowsize NonUniformSize
Template parameter flag: "each row has an independent width".
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Tag type for naming objects created via make_ssp.