HydroCouple  2.0.0-alpha.1
HydroCouple Interface Definitions
Loading...
Searching...
No Matches
hydrocouplehelpers.h
Go to the documentation of this file.
1
23
24#ifndef HYDROCOUPLEHELPERS_H
25#define HYDROCOUPLEHELPERS_H
26
27#include "hydrocouple.h"
28
29namespace HydroCouple
30{
32 namespace Helpers
33 {
39 constexpr int64_t dataKindSize(DataKind kind)
40 {
41 switch (kind)
42 {
43 case DataKind::Int8:
44 case DataKind::UInt8:
45 case DataKind::Boolean: return 1;
46 case DataKind::Int16:
47 case DataKind::UInt16: return 2;
48 case DataKind::Int32:
50 case DataKind::Float32: return 4;
51 case DataKind::Int64:
53 case DataKind::Float64: return 8;
54 default: return 0;
55 }
56 }
57
62 template <typename T>
63 struct DataKindOf { static constexpr DataKind kind = DataKind::Opaque; };
64 template <> struct DataKindOf<int8_t> { static constexpr DataKind kind = DataKind::Int8; };
65 template <> struct DataKindOf<uint8_t> { static constexpr DataKind kind = DataKind::UInt8; };
66 template <> struct DataKindOf<int16_t> { static constexpr DataKind kind = DataKind::Int16; };
67 template <> struct DataKindOf<uint16_t> { static constexpr DataKind kind = DataKind::UInt16; };
68 template <> struct DataKindOf<int32_t> { static constexpr DataKind kind = DataKind::Int32; };
69 template <> struct DataKindOf<uint32_t> { static constexpr DataKind kind = DataKind::UInt32; };
70 template <> struct DataKindOf<int64_t> { static constexpr DataKind kind = DataKind::Int64; };
71 template <> struct DataKindOf<uint64_t> { static constexpr DataKind kind = DataKind::UInt64; };
72 template <> struct DataKindOf<float> { static constexpr DataKind kind = DataKind::Float32; };
73 template <> struct DataKindOf<double> { static constexpr DataKind kind = DataKind::Float64; };
74 template <> struct DataKindOf<bool> { static constexpr DataKind kind = DataKind::Boolean; };
75 template <> struct DataKindOf<std::string> { static constexpr DataKind kind = DataKind::String; };
76
81 [[nodiscard]] constexpr int64_t elementCount(const BufferDescriptor &d)
82 {
83 int64_t n = 1;
84 for (int32_t k = 0; k < d.rank; ++k)
85 n *= d.shape[k];
86 return n;
87 }
88
93 [[nodiscard]] constexpr bool isContiguous(const BufferDescriptor &d)
94 {
95 if (d.stridesBytes == nullptr)
96 return true;
97 int64_t expected = dataKindSize(d.kind);
98 for (int32_t k = d.rank - 1; k >= 0; --k)
99 {
100 if (d.shape[k] != 1 && d.stridesBytes[k] != expected)
101 return false;
102 expected *= d.shape[k];
103 }
104 return true;
105 }
106
112 [[nodiscard]] constexpr int64_t byteOffset(const BufferDescriptor &d, std::span<const int64_t> index)
113 {
114 int64_t offset = 0;
115 if (d.stridesBytes == nullptr)
116 {
117 int64_t stride = dataKindSize(d.kind);
118 for (int32_t k = d.rank - 1; k >= 0; --k)
119 {
120 offset += index[k] * stride;
121 stride *= d.shape[k];
122 }
123 }
124 else
125 {
126 for (int32_t k = 0; k < d.rank; ++k)
127 offset += index[k] * d.stridesBytes[k];
128 }
129 return offset;
130 }
131
136 [[nodiscard]] constexpr int64_t contiguousByteSize(const BufferDescriptor &d)
137 {
138 return elementCount(d) * dataKindSize(d.kind);
139 }
140
147 [[nodiscard]] constexpr BufferDescriptor makeContiguous(void *data, DataKind kind, std::span<const int64_t> shape)
148 {
150 d.data = data;
151 d.kind = kind;
152 d.rank = static_cast<int32_t>(shape.size());
153 d.shape = shape.data();
154 return d;
155 }
156
167 {
169 switch (from)
170 {
171 case CS::Created: return to == CS::Initializing;
172 case CS::Initializing: return to == CS::Initialized || to == CS::Failed;
173 case CS::Initialized: return to == CS::Validating || to == CS::Initializing;
174 case CS::Validating: return to == CS::Valid || to == CS::Invalid;
175 case CS::Valid: return to == CS::Preparing || to == CS::Validating;
176 case CS::Invalid: return to == CS::Validating;
177 case CS::Preparing: return to == CS::Updated || to == CS::Failed;
178 case CS::Updating: return to == CS::Updated || to == CS::Done ||
179 to == CS::WaitingForData || to == CS::Failed;
180 case CS::WaitingForData: return to == CS::Updating || to == CS::Failed;
181 case CS::Updated: return to == CS::Updating || to == CS::Checkpointing ||
182 to == CS::Finishing;
183 case CS::Checkpointing: return to == CS::Updated || to == CS::Failed;
184 case CS::Done: return to == CS::Finishing;
185 case CS::Finishing: return to == CS::Finished || to == CS::Created;
186 case CS::Finished: return false;
187 case CS::Failed: return to == CS::Initializing || to == CS::Finishing;
188 }
189 return false;
190 }
191
200 template <typename T>
201 [[nodiscard]] bool getValue(const IComponentDataItem &item, T &value, std::span<const int64_t> index)
202 {
203 std::vector<int64_t> ones(index.size(), 1);
204 const int64_t one = 1;
205 return item.getValuesInto(makeContiguous(&value, DataKindOf<T>::kind, std::span<const int64_t>(&one, 1)),
206 index, ones);
207 }
208
217 template <typename T>
218 [[nodiscard]] bool setValue(IComponentDataItem &item, const T &value, std::span<const int64_t> index)
219 {
220 std::vector<int64_t> ones(index.size(), 1);
221 const int64_t one = 1;
222 return item.setValuesFrom(makeContiguous(const_cast<T *>(&value), DataKindOf<T>::kind, std::span<const int64_t>(&one, 1)),
223 index, ones);
224 }
225
235 template <typename T>
236 [[nodiscard]] bool getValues(const IComponentDataItem &item, std::span<T> destination,
237 std::span<const int64_t> start, std::span<const int64_t> count)
238 {
239 return item.getValuesInto(makeContiguous(destination.data(), DataKindOf<T>::kind, count), start, count);
240 }
241
251 template <typename T>
252 [[nodiscard]] bool setValues(IComponentDataItem &item, std::span<const T> source,
253 std::span<const int64_t> start, std::span<const int64_t> count)
254 {
255 return item.setValuesFrom(makeContiguous(const_cast<T *>(source.data()), DataKindOf<T>::kind, count), start, count);
256 }
257 }
258}
259
260#endif // HYDROCOUPLEHELPERS_H
IComponentDataItem is a fundamental unit of data for a component.
Definition hydrocouple.h:1466
virtual bool getValuesInto(const BufferDescriptor &destination, std::span< const int64_t > start, std::span< const int64_t > count, std::string *message=nullptr) const =0
Copies a hyperslab of this item's values into a caller-described buffer.
virtual bool setValuesFrom(const BufferDescriptor &source, std::span< const int64_t > start, std::span< const int64_t > count, std::string *message=nullptr)=0
Copies values from a caller-described buffer into a hyperslab of this item.
ComponentStatus
HydroCouple::ComponentStatus is an enumerator that describes the status of a component over the cours...
Definition hydrocouple.h:541
Core interface definitions for the HydroCouple component-based modeling framework.
Non-normative convenience helpers for the HydroCouple interface standard.
Definition hydrocouplehelpers.h:33
bool setValues(IComponentDataItem &item, std::span< const T > source, std::span< const int64_t > start, std::span< const int64_t > count)
Typed convenience: writes a hyperslab from a flat contiguous span in C order.
Definition hydrocouplehelpers.h:252
constexpr int64_t contiguousByteSize(const BufferDescriptor &d)
Total byte extent of a C-contiguous buffer of the descriptor's shape and kind.
Definition hydrocouplehelpers.h:136
constexpr bool isContiguous(const BufferDescriptor &d)
Checks whether a BufferDescriptor's layout is C-contiguous (row-major, packed).
Definition hydrocouplehelpers.h:93
constexpr int64_t dataKindSize(DataKind kind)
Size in bytes of one element of the given DataKind.
Definition hydrocouplehelpers.h:39
bool setValue(IComponentDataItem &item, const T &value, std::span< const int64_t > index)
Typed convenience: writes the single element at the given index of a data item.
Definition hydrocouplehelpers.h:218
bool getValues(const IComponentDataItem &item, std::span< T > destination, std::span< const int64_t > start, std::span< const int64_t > count)
Typed convenience: reads a hyperslab into a flat contiguous span in C order.
Definition hydrocouplehelpers.h:236
constexpr bool isValidComponentStatusTransition(IModelComponent::ComponentStatus from, IModelComponent::ComponentStatus to)
Encodes the normative component lifecycle state machine.
Definition hydrocouplehelpers.h:165
constexpr int64_t byteOffset(const BufferDescriptor &d, std::span< const int64_t > index)
Byte offset from a descriptor's base address of the element at the given index.
Definition hydrocouplehelpers.h:112
constexpr BufferDescriptor makeContiguous(void *data, DataKind kind, std::span< const int64_t > shape)
Builds a C-contiguous host BufferDescriptor.
Definition hydrocouplehelpers.h:147
constexpr int64_t elementCount(const BufferDescriptor &d)
Number of elements a BufferDescriptor describes (product of shape; 1 for rank 0).
Definition hydrocouplehelpers.h:81
bool getValue(const IComponentDataItem &item, T &value, std::span< const int64_t > index)
Typed convenience: reads the single element at the given index of a data item.
Definition hydrocouplehelpers.h:201
HydroCouple namespace contains the core interface specifications for the HydroCouple component-based ...
Definition hydrocouple.h:58
DataKind
DataKind identifies the element type of a typed data buffer.
Definition hydrocouple.h:87
@ Float32
float
Definition hydrocouple.h:97
@ String
std::string (host memory only)
Definition hydrocouple.h:100
@ Boolean
bool (stored one element per byte)
Definition hydrocouple.h:99
@ Int16
int16_t
Definition hydrocouple.h:91
@ Int8
int8_t
Definition hydrocouple.h:89
@ UInt16
uint16_t
Definition hydrocouple.h:92
@ UInt64
uint64_t
Definition hydrocouple.h:96
@ UInt8
uint8_t
Definition hydrocouple.h:90
@ Int32
int32_t
Definition hydrocouple.h:93
@ Float64
double
Definition hydrocouple.h:98
@ UInt32
uint32_t
Definition hydrocouple.h:94
@ Opaque
Implementation-defined bytes (element size must be agreed out of band).
Definition hydrocouple.h:101
@ Int64
int64_t
Definition hydrocouple.h:95
BufferDescriptor describes a typed, possibly strided, possibly device-resident multi-dimensional arra...
Definition hydrocouple.h:134
DataKind kind
Element type.
Definition hydrocouple.h:136
const int64_t * stridesBytes
Byte step per dimension; nullptr => C-contiguous.
Definition hydrocouple.h:139
int32_t rank
Number of dimensions; 0 denotes a scalar.
Definition hydrocouple.h:137
const int64_t * shape
Extent per dimension; length == rank.
Definition hydrocouple.h:138
void * data
Base address of element (0, 0, ..., 0).
Definition hydrocouple.h:135
static constexpr DataKind kind
Definition hydrocouplehelpers.h:74
static constexpr DataKind kind
Definition hydrocouplehelpers.h:73
static constexpr DataKind kind
Definition hydrocouplehelpers.h:72
static constexpr DataKind kind
Definition hydrocouplehelpers.h:66
static constexpr DataKind kind
Definition hydrocouplehelpers.h:68
static constexpr DataKind kind
Definition hydrocouplehelpers.h:70
static constexpr DataKind kind
Definition hydrocouplehelpers.h:64
static constexpr DataKind kind
Definition hydrocouplehelpers.h:75
static constexpr DataKind kind
Definition hydrocouplehelpers.h:67
static constexpr DataKind kind
Definition hydrocouplehelpers.h:69
static constexpr DataKind kind
Definition hydrocouplehelpers.h:71
static constexpr DataKind kind
Definition hydrocouplehelpers.h:65
DataKindOf maps a C++ type to its DataKind at compile time.
Definition hydrocouplehelpers.h:63
static constexpr DataKind kind
Definition hydrocouplehelpers.h:63