13# ifdef STORMM_USE_CUDA
14# include <cuda_runtime.h>
18#include "Constants/behavior.h"
19#include "Constants/scaling.h"
20#include "DataTypes/common_types.h"
21#include "DataTypes/stormm_vector_types.h"
22#include "Math/rounding.h"
23#include "Parsing/polynumeric.h"
24#include "Reporting/error_format.h"
25#include "gpu_enumerators.h"
27# include "hpc_hybrid.h"
33using constants::ExceptionResponse;
37using stmath::getPaddedMemorySize;
38using parse::PolyNumeric;
42constexpr char expedited_code =
'X';
43constexpr char decoupled_code =
'L';
44constexpr char unified_code =
'U';
45constexpr char host_only_code =
'L';
46constexpr char devc_only_code =
'D';
47constexpr char host_mounted_code =
'H';
51constexpr llint allocating = 1;
52constexpr llint deallocating = -1;
53constexpr ulint hybrid_byte_increment = 128;
55constexpr HybridFormat default_hpc_format = HybridFormat::EXPEDITED;
57constexpr HybridFormat default_hpc_format = HybridFormat::HOST_ONLY;
61constexpr ulint tag_name_length = 23;
68 char name[tag_name_length];
133 size_t element_size,
int allocations);
148 std::vector<LedgerEntry>
getEntry(
const std::string &name)
const;
178 llint total_expedited;
179 llint total_decoupled;
181 llint total_host_only;
182 llint total_devc_only;
183 llint total_host_mounted;
187 std::vector<LedgerEntry> entries;
190 std::vector<int> free_slots;
217 explicit Hybrid(
size_t length_in = 0,
const char* tag_in =
nullptr,
218 const HybridFormat format_in = default_hpc_format,
219 const HybridKind kind_in = HybridKind::ARRAY);
221 explicit Hybrid(
const std::vector<T> &S_in,
const char* tag_in =
nullptr,
222 const HybridFormat format_in = default_hpc_format);
224 explicit Hybrid(
const HybridKind kind_in,
const char* tag_in =
nullptr,
225 const HybridFormat format_in = default_hpc_format,
size_t length_in = 0);
297 const T*
data(
const HybridTargetLevel tier = HybridTargetLevel::HOST)
const;
298 T*
data(
const HybridTargetLevel tier = HybridTargetLevel::HOST);
315 std::vector<T>
readHost(
size_t offset,
size_t count)
const;
317 void readHost(T* v,
size_t offset,
size_t count)
const;
341 void putHost(
const std::vector<T> &values,
size_t offset,
size_t count);
342 void putHost(
const std::vector<T> &values);
343 size_t putHost(
Hybrid<T> *target,
const std::vector<T> &values,
size_t offset = 0,
344 size_t padding = 0,
size_t count = 0);
348 void upload(
size_t start_pos = 0,
size_t length_copy = 0);
351 void download(
size_t start_pos = 0,
size_t length_copy = 0);
369 T readDevice(
size_t index)
const;
370 std::vector<T> readDevice(
size_t offset,
size_t count)
const;
371 std::vector<T> readDevice()
const;
372 void readDevice(T* v,
size_t offset,
size_t count)
const;
392 void putDevice(
const T value,
size_t index);
393 void putDevice(
const T* values,
size_t offset,
size_t count);
394 void putDevice(
const std::vector<T> &values,
size_t offset,
size_t count);
395 void putDevice(
const std::vector<T> &values);
417 void pushBack(
const T* elements,
const size_t element_count);
418 void pushBack(
const std::vector<T> &elements);
434 void resize(
size_t new_length, T value);
476 const char* tag_in =
nullptr)
const;
477 Hybrid<T> getPointer(
size_t position = 0,
size_t new_length = 0,
const char* tag_in =
nullptr);
480# ifdef STORMM_USE_CUDA
490 const T* getDeviceValidHostPointer()
const;
491 T* getDeviceValidHostPointer();
518 size_t growth_increment;
525 size_t pointer_index;
544 int target_serial_number;
552 void enforceDataTypeLimits();
561 void reallocate(
size_t new_length,
size_t new_capacity = 0);
575 template <
typename Treturn>
577 const char* output_name);
579 template <
typename Treturn>
581 const char* output_name);
605 const char* output_name =
nullptr);
609 const char* output_name =
nullptr);
An evolution of GpuBuffer in pmemd.cuda, the Composite array has elements that are accessible from ei...
Definition hybrid.h:202
void pushBack(const T element)
Mimic the C++ std::vector push_back functionality. A bounds check is followed by extension of the dat...
T readHost(size_t index) const
Get data from a given index of the host_data array. This substitutes for direct array access via the ...
size_t capacity() const
Return the number of elements in the Hybrid object's data array(s)
void setPointer(Hybrid< T > *target, size_t position=0, llint new_length=-1LL)
Set a Hybrid pointer struct to a segment of a Hybrid array struct.
void putHost(const T value, size_t index)
Put data into the host_data array. This substitutes for direct array access via the [ ] operator,...
int getTargetSerialNumber() const
Produce the serial number of the target (only valid for POINTER-kind objects). This function is neede...
int getAllocations() const
Return the number of times this object has been allocated.
HybridKind getKind() const
Get the object kind (pointer or array)
Hybrid(const Hybrid &original)
Copy constructor handles the reassignment of the underlying raw pointers.
size_t getElementSize() const
Return the size of an individual element in the data array(s)
bool verifyTarget() const
Verify that the target of a HybridKind::POINTER object remains in the state it was in when the pointe...
~Hybrid()
Destructor frees data with no re-allocation.
Hybrid & operator=(const Hybrid &other)
Copy assignment constructor handles the reassignment of the underlying raw pointers.
void shrinkToFit()
Trim a Hybrid array to the exact size of its data.
void resize(size_t new_length)
A std::vector-like resize() feature.
void swapTarget(Hybrid< T > *new_target, ExceptionResponse policy=ExceptionResponse::SILENT)
Swap the target of a POINTER-kind Hybrid object, transferring its starting index and current length t...
int getSerialNumber() const
Return the object's identifying serial number.
size_t getPointerIndex() const
Return the pointer start position, targeting another ARRAY-kind Hybrid object (relevant for POINTER-k...
size_t size() const
Return the number of elements in the Hybrid object's data array(s)
HybridFormat getFormat() const
Get the memory format.
const T * data(const HybridTargetLevel tier=HybridTargetLevel::HOST) const
Get a pointer directly to the GpuArray's data on either the host or the device.
HybridLabel getLabel() const
Return the object's identifying label.
Hybrid(Hybrid &&original)
The move constructor handles migration of a Hybrid object.
Hybrid(size_t length_in=0, const char *tag_in=nullptr, const HybridFormat format_in=default_hpc_format, const HybridKind kind_in=HybridKind::ARRAY)
Constructors vary based on whether CUDA is part of the compilation.
Hybrid & operator=(Hybrid &&other)
The move assignment operator must likewise handle transfer of the underlying data.
const Hybrid< T > getPointer(size_t position=0, size_t new_length=0, const char *tag_in=nullptr) const
Get a pointer to a Hybrid object at a specific location. This is the way to get const Hybrid POINTER-...
A struct for storing records of all active Hybrid memory arrays. This will also store tables of ident...
Definition hybrid.h:93
Ledger()
Constructor initializes the number of entries and allocations.
Definition hybrid.cpp:22
llint getTotalExpedited() const
Retrieve the total allocation of decoupled memory, in bytes.
Definition hybrid.cpp:53
llint getTotalUnified() const
Retrieve the total allocation of unified memory, in bytes.
Definition hybrid.cpp:63
int getActiveEntryCount() const
Retrieve the number of entries for Hybrid memory objects currently stored.
Definition hybrid.cpp:33
LedgerEntry getEntry(int index) const
Get information about a single entry of a ledger.
Definition hybrid.cpp:117
void unsetEntry(int serno)
Remove a Hybrid object from the catalog. This is to be called by the Hybrid object's destructor.
Definition hybrid.cpp:105
void logMemory(size_t capacity, size_t element_size, HybridFormat fmt, const HybridLabel hlbl, llint multiplier)
Log each tagged Hybrid array in the ledger.
Definition hybrid.cpp:134
~Ledger()=default
Default destructor.
void setEntry(int serno, HybridKind kind, const HybridLabel hlbl, size_t length, size_t element_size, int allocations)
Begin a catalog for the place of a Hybrid object among many others. This is to be called once for eac...
Definition hybrid.cpp:83
int getAllocations(int serno)
Query the number of times the Hybrid object catalogged in a particular entry has had its data arrays ...
Definition hybrid.cpp:195
llint getTotalDevcOnly() const
Retrieve the total allocation of device-only memory, in bytes.
Definition hybrid.cpp:73
bool testActive(int serno)
Query an index in the ledger to determine whether it catalogs an active Hybrid object.
Definition hybrid.cpp:187
llint getTotalHostOnly() const
Retrieve the total allocation of host-only memory, in bytes.
Definition hybrid.cpp:68
llint getTotalHostMounted() const
Retrieve the total allocation of host-mounted memory, in bytes.
Definition hybrid.cpp:78
llint getTotalDecoupled() const
Retrieve the total allocation of decoupled memory, in bytes.
Definition hybrid.cpp:58
void printMemoryProfile(int n_display=8, llint display_threshold=mega)
Print the current memory usage of all Hybrid objects. The function will print totals followed by spec...
Definition hybrid.cpp:209
int getSerialNumber()
Produce a number corresponding to some unused entry in the ledger. This will be either the lowest pos...
Definition hybrid.cpp:38
A Hybrid object's immutable identifying information, stored as a const member variable within the Hyb...
Definition hybrid.h:65
char format
Shorthand for the Hybrid object's format.
Definition hybrid.h:71
char name[tag_name_length]
A descriptive name for the Hybrid object.
Definition hybrid.h:68
int serial_number
Definition hybrid.h:75
Entries of the memory ledger recording not just a copy of the immutable type data but also allocation...
Definition hybrid.h:80