29#ifndef HYDROCOUPLEHELPERS_H
30#define HYDROCOUPLEHELPERS_H
45#if defined(__GNUC__) || defined(__clang__)
46#pragma GCC visibility push(default)
85 if constexpr (std::is_same_v<U, bool>)
87 else if constexpr (std::is_integral_v<U>)
92 constexpr bool isSigned = std::is_signed_v<U>;
99 else if constexpr (std::is_same_v<U, float>)
101 else if constexpr (std::is_same_v<U, double>)
103 else if constexpr (std::is_same_v<U, std::string>)
120 template <
typename T>
126 "HydroCouple::Helpers::DataKindOf<T>: T has no DataKind. Use an "
127 "integer, float, double, bool or std::string element type, or "
128 "describe opaque elements with makeOpaqueContiguous().");
155 d.
itemSizeBytes ==
static_cast<int64_t
>(
sizeof(std::string));
167 for (int32_t k = 0; k < d.
rank; ++k)
181 for (int32_t k = d.
rank - 1; k >= 0; --k)
185 expected *= d.
shape[k];
201 for (int32_t k = d.
rank - 1; k >= 0; --k)
203 offset += index[
static_cast<size_t>(k)] * stride;
204 stride *= d.
shape[k];
209 for (int32_t k = 0; k < d.
rank; ++k)
210 offset += index[
static_cast<size_t>(k)] * d.
stridesBytes[k];
235 d.
rank =
static_cast<int32_t
>(shape.size());
236 d.
shape = shape.data();
252 d.
rank =
static_cast<int32_t
>(shape.size());
253 d.
shape = shape.data();
276 if (to == CS::Failed)
277 return from != CS::Finished && from != CS::Failed;
281 case CS::Created:
return to == CS::Initializing;
282 case CS::Initializing:
return to == CS::Initialized || to == CS::Failed;
283 case CS::Initialized:
return to == CS::Validating || to == CS::Initializing ||
285 case CS::Validating:
return to == CS::Valid || to == CS::Invalid;
286 case CS::Valid:
return to == CS::Preparing || to == CS::Validating ||
288 case CS::Invalid:
return to == CS::Validating || to == CS::Finishing;
289 case CS::Preparing:
return to == CS::Updated || to == CS::Failed;
290 case CS::Updating:
return to == CS::Updated || to == CS::Done ||
291 to == CS::WaitingForData || to == CS::Failed;
292 case CS::WaitingForData:
return to == CS::Updating || to == CS::Failed;
293 case CS::Updated:
return to == CS::Updating || to == CS::Checkpointing ||
297 case CS::Checkpointing:
return to == CS::Updated || to == CS::Done || to == CS::Failed;
298 case CS::Done:
return to == CS::Checkpointing || to == CS::Finishing;
299 case CS::Finishing:
return to == CS::Finished || to == CS::Created;
300 case CS::Finished:
return false;
301 case CS::Failed:
return to == CS::Initializing || to == CS::Finishing;
321 if (to == WS::Failed)
322 return from != WS::Finished && from != WS::Failed;
326 case WS::Created:
return to == WS::Initializing;
327 case WS::Initializing:
return to == WS::Initialized || to == WS::Failed;
328 case WS::Initialized:
return to == WS::Validating || to == WS::Initializing ||
330 case WS::Validating:
return to == WS::Validated || to == WS::Invalid;
331 case WS::Validated:
return to == WS::Preparing || to == WS::Validating ||
335 case WS::Invalid:
return to == WS::Validating || to == WS::Finishing;
336 case WS::Preparing:
return to == WS::Prepared || to == WS::Failed;
337 case WS::Prepared:
return to == WS::Updating || to == WS::Finishing;
338 case WS::Updating:
return to == WS::Updated || to == WS::Paused ||
339 to == WS::Done || to == WS::Failed;
340 case WS::Updated:
return to == WS::Updating || to == WS::Finishing;
341 case WS::Paused:
return to == WS::Updated || to == WS::Finishing;
342 case WS::Done:
return to == WS::Finishing;
343 case WS::Finishing:
return to == WS::Finished;
344 case WS::Finished:
return false;
345 case WS::Failed:
return to == WS::Initializing || to == WS::Finishing;
358 template <
typename T>
361 std::vector<int64_t> ones(index.size(), 1);
362 const int64_t one = 1;
375 template <
typename T>
378 std::vector<int64_t> ones(index.size(), 1);
379 const int64_t one = 1;
393 template <
typename T>
395 std::span<const int64_t> start, std::span<const int64_t> count)
409 template <
typename T>
411 std::span<const int64_t> start, std::span<const int64_t> count)
418#if defined(__GNUC__) || defined(__clang__)
419#pragma GCC visibility pop
IComponentDataItem is a fundamental unit of data for a component.
Definition hydrocouple.h:1844
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:707
WorkflowStatus
The WorkflowStatus enum describes the status of a workflow component over the course of its lifetime.
Definition hydrocouple.h:2801
Core interface definitions for the HydroCouple component-based modeling framework.
Definition hydrocouplehelpers.h:80
constexpr DataKind deduceDataKind()
The DataKind of a C++ element type, or Unknown when it has none.
Definition hydrocouplehelpers.h:83
Convenience helpers for the HydroCouple interface standard; non-normative except for the two lifecycl...
Definition hydrocouplehelpers.h:54
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:410
constexpr bool hasValidItemSize(const BufferDescriptor &d)
Checks that a descriptor's element size is consistent with its kind.
Definition hydrocouplehelpers.h:148
constexpr int64_t itemSize(const BufferDescriptor &d)
Size in bytes of one element of a descriptor: its itemSizeBytes when set, otherwise the kind's natura...
Definition hydrocouplehelpers.h:137
constexpr BufferDescriptor makeOpaqueContiguous(void *data, int64_t itemSizeBytes, std::span< const int64_t > shape)
Builds a C-contiguous host BufferDescriptor over opaque elements.
Definition hydrocouplehelpers.h:247
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:219
constexpr bool isContiguous(const BufferDescriptor &d)
Checks whether a BufferDescriptor's layout is C-contiguous (row-major, packed).
Definition hydrocouplehelpers.h:176
constexpr int64_t dataKindSize(DataKind kind)
Size in bytes of one element of the given DataKind.
Definition hydrocouplehelpers.h:60
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:376
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:394
constexpr bool isValidComponentStatusTransition(IModelComponent::ComponentStatus from, IModelComponent::ComponentStatus to)
Encodes the normative component lifecycle state machine.
Definition hydrocouplehelpers.h:266
constexpr bool isValidWorkflowStatusTransition(IWorkflowComponent::WorkflowStatus from, IWorkflowComponent::WorkflowStatus to)
Encodes the normative workflow lifecycle state machine.
Definition hydrocouplehelpers.h:314
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:195
constexpr BufferDescriptor makeContiguous(void *data, DataKind kind, std::span< const int64_t > shape)
Builds a C-contiguous host BufferDescriptor.
Definition hydrocouplehelpers.h:230
constexpr int64_t elementCount(const BufferDescriptor &d)
Number of elements a BufferDescriptor describes (product of shape; 1 for rank 0).
Definition hydrocouplehelpers.h:164
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:359
HydroCouple namespace contains the core interface specifications for the HydroCouple component-based ...
Definition hydrocouple.h:54
DataKind
DataKind identifies the element type of a typed data buffer.
Definition hydrocouple.h:199
@ Float32
float
Definition hydrocouple.h:209
@ String
std::string (host memory only)
Definition hydrocouple.h:212
@ Boolean
bool (stored one element per byte)
Definition hydrocouple.h:211
@ Int16
int16_t
Definition hydrocouple.h:203
@ Int8
int8_t
Definition hydrocouple.h:201
@ Unknown
No/unknown element type.
Definition hydrocouple.h:200
@ UInt16
uint16_t
Definition hydrocouple.h:204
@ UInt64
uint64_t
Definition hydrocouple.h:208
@ UInt8
uint8_t
Definition hydrocouple.h:202
@ Int32
int32_t
Definition hydrocouple.h:205
@ Float64
double
Definition hydrocouple.h:210
@ UInt32
uint32_t
Definition hydrocouple.h:206
@ Opaque
Implementation-defined bytes; meaning agreed out of band, size in BufferDescriptor::itemSizeBytes.
Definition hydrocouple.h:213
@ Int64
int64_t
Definition hydrocouple.h:207
BufferDescriptor describes a typed, possibly strided, possibly device-resident multi-dimensional arra...
Definition hydrocouple.h:279
DataKind kind
Element type.
Definition hydrocouple.h:281
const int64_t * stridesBytes
Byte step per dimension; nullptr => C-contiguous.
Definition hydrocouple.h:284
int64_t itemSizeBytes
Size of one element; 0 => the kind's natural size (illegal for Opaque).
Definition hydrocouple.h:285
int32_t rank
Number of dimensions; 0 denotes a scalar.
Definition hydrocouple.h:282
const int64_t * shape
Extent per dimension; length == rank.
Definition hydrocouple.h:283
void * data
Base address of element (0, 0, ..., 0).
Definition hydrocouple.h:280
DataKindOf maps a C++ type to its DataKind at compile time.
Definition hydrocouplehelpers.h:122
static constexpr DataKind kind
The DataKind of T.
Definition hydrocouplehelpers.h:124