23#ifndef VELOCITY_BLOCK_CONTAINER_H
24#define VELOCITY_BLOCK_CONTAINER_H
32#if defined(DEBUG_VLASIATOR) || defined(DEBUG_SPATIAL_CELL)
46 #include "include/splitvector/splitvec.h"
69 void clear(
bool shrink=
true);
95 void updateCachedSize();
96 void updateCachedCapacity();
100 void print_addresses();
118 size_t cachedCapacity;
121 std::vector<Realf,aligned_allocator<Realf,WID3> >
block_data;
122 std::vector<Real,aligned_allocator<Real,BlockParams::N_VELOCITY_BLOCK_PARAMS> >
parameters;
133 block_data = std::vector<Realf,aligned_allocator<Realf,WID3>>(
WID3);
146 block_data = split::SplitVector<Realf>(other.cachedCapacity*
WID3);
152 cachedSize = other.cachedSize;
153 cachedCapacity = other.cachedCapacity;
156 parameters = std::vector<Real,aligned_allocator<Real,BlockParams::N_VELOCITY_BLOCK_PARAMS>>(other.
parameters);
170 cachedSize = other.cachedSize;
171 cachedCapacity = other.cachedCapacity;
185 if (currentCapacity != cachedCapacity) {
186 printf(
"VBC CHECK ERROR: capacity %lu vs cached value %lu in %s : %d\n",currentCapacity,cachedCapacity,__FILE__,__LINE__);
189 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
190 return cachedCapacity;
203 if (currentCapacity != cachedCapacity) {
204 printf(
"VBC CHECK ERROR: capacity, %lu vs cached value %lu in %s : %d\n",currentCapacity,cachedCapacity,__FILE__,__LINE__);
207 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
229 block_data = std::vector<Realf,aligned_allocator<Realf,WID3>>(
WID3);
237 std::cerr<<
"VBC CLEAR FAILED"<<std::endl;
244 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
259 if (source >= numberOfBlocks) ok =
false;
260 if (source >= currentCapacity) ok =
false;
261 if (source >= numberOfBlocksP) ok =
false;
262 if (source >= currentCapacityP) ok =
false;
263 if (target >= numberOfBlocks) ok =
false;
264 if (target >= currentCapacity) ok =
false;
265 if (numberOfBlocks > currentCapacity) ok =
false;
266 if (source != numberOfBlocks-1) ok =
false;
267 if (source != numberOfBlocksP-1) ok =
false;
269 if (cachedCapacity != currentCapacity) ok =
false;
271 if (currentCapacityP != currentCapacity) ok =
false;
272 if (numberOfBlocksP != numberOfBlocks) ok =
false;
274 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
275 std::stringstream ss;
276 ss <<
"VBC ERROR: invalid source LID=" << source <<
" in copy, target=" << target <<
" #blocks=" << numberOfBlocks <<
" capacity=" << currentCapacity << std::endl;
277 ss <<
"or sizes are wrong, data->size()=" <<
block_data.size() <<
" parameters.size()=" <<
parameters.size() << std::endl;
278 std::cerr << ss.str();
282 printf(
"VBC error: invalid source LID=%u in copy, target=%u #blocks=%u capacity=%u \n or sizes are wrong, data->size()=%u parameters.size()=%u \n",
289 for (
unsigned int i=0;
i<
WID3; ++
i) {
309 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
310 std::stringstream ss;
311 ss <<
"Process " << rank <<
' ';
312 ss <<
"Invalid localID " << localID <<
" used in function '" << funcName <<
"' max allowed value is " << numberOfBlocks << std::endl;
313 std::cerr << ss.str();
321 printf(
"Invalid localID %u used in VBC; max allowed value is %u\n",localID,numberOfBlocks);
341 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
342 if (blockLID >= numberOfBlocks) {
346 if (blockLID >= numberOfBlocks) {
357 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
358 if (blockLID >= numberOfBlocks) {
362 if (blockLID >= numberOfBlocks) {
381 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
382 if (blockLID >= numberOfBlocks) {
386 if (blockLID >= numberOfBlocks) {
400 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
401 if (blockLID >= numberOfBlocks) {
405 if (blockLID >= numberOfBlocks) {
418 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
427 if (numberOfBlocks == 0) {
442 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
453 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
455 if (newIndex >= currentCapacityD) {
456 assert(0 &&
"ERROR! Attempting to grow block container on-device beyond capacity (::push_back).");
460 #elif defined(USE_GPU)
473 if (newIndex >= currentCapacity || newIndex >= currentCapacityP) {
474 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
475 std::stringstream ss;
476 ss <<
"VBC ERROR in push_back, LID=" << newIndex <<
" for new block is out of bounds" << std::endl;
477 ss <<
"\t data->size()=" <<
block_data.size() <<
" parameters.size()=" <<
parameters.size() << std::endl;
478 ss <<
"\t data->capacity()=" <<
block_data.capacity() <<
" parameters.capacity()=" <<
parameters.capacity() << std::endl;
479 std::cerr << ss.str();
483 printf(
"VBC ERROR in device push_back, LID=%u for new block is out of bounds\n data->size()=%u parameters.size()=%u\n",
499 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
510 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
512 if (newIndex >= currentCapacityD) {
513 assert(0 &&
"ERROR! Attempting to grow block container on-device beyond capacity (::push_back_and_zero).");
517 #elif defined(USE_GPU)
530 if (newIndex >= currentCapacity || newIndex >= currentCapacityP) {
531 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
532 std::stringstream ss;
533 ss <<
"VBC ERROR in push_back_and_zero, LID=" << newIndex <<
" for new block is out of bounds" << std::endl;
534 ss <<
"\t data->size()=" <<
block_data.size() <<
" parameters.size()=" <<
parameters.size() << std::endl;
535 ss <<
"\t data->capacity()=" <<
block_data.capacity() <<
" parameters.capacity()=" <<
parameters.capacity() << std::endl;
536 std::cerr << ss.str();
540 printf(
"VBC ERROR in device push_back_and_zero, LID=%u for new block is out of bounds \n data->size()=%u parameters.size()=%u \n",
547 for (
size_t i=0;
i<
WID3; ++
i) {
562 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
576 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
577 if (newIndex + N_blocks >= currentCapacity-1) {
578 assert(0 &&
"ERROR! Attempting to grow block container on-device beyond capacity (::push_back N_blocks).");
582 #elif defined(USE_GPU)
593 cachedSize += N_blocks;
601 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
615 #if defined(USE_GPU) && (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__))
616 if (newIndex + N_blocks >= currentCapacity-1) {
617 assert(0 &&
"ERROR! Attempting to grow block container on-device beyond capacity (::push_back_and_zero N_blocks).");
621 #elif defined(USE_GPU)
631 #if defined(USE_GPU) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
642 for (
size_t i=0;
i<
WID3*N_blocks; ++
i) {
651 cachedSize += N_blocks;
673 if (currentCapacity >= newCapacity) {
676 if (newCapacity < numberOfBlocks) {
677 std::cerr<<
" ERROR! Trying to recapacitate to "<<newCapacity<<
" when VBC already contains "<<numberOfBlocks<<
" blocks!"<<std::endl;
691 cachedCapacity = newCapacity;
702 if (reqCapacity < numberOfBlocks) {
703 std::cerr<<
" ERROR! Trying to recapacitate to "<<reqCapacity<<
" when VBC already contains "<<numberOfBlocks<<
" blocks!"<<std::endl;
714 cachedCapacity = newCapacity;
717 std::vector<Realf,aligned_allocator<Realf,WID3>> block_data_new(newCapacity*
WID3);
719 block_data_new.resize(numberOfBlocks*
WID3);
721 for (
size_t i=0;
i<numberOfBlocks*
WID3; ++
i) {
740 #if !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
747 assert(newSize <= currentCapacity &&
"ERROR! Attempting to grow block container on-device beyond capacity (::setNewSize).");
758 cachedSize = newSize;
767 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
772 if (currentSize != cachedSize) {
773 printf(
"VBC CHECK ERROR: cached size mismatch, %lu vs %lu in %s : %d\n",currentSize,cachedSize,__FILE__,__LINE__);
785 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
790 if (currentSize != cachedSize) {
791 printf(
"VBC CHECK ERROR: cached size mismatch, %lu vs %lu in %s : %d\n",currentSize,cachedSize,__FILE__,__LINE__);
808 inline void VelocityBlockContainer::setNewCachedSize(
const vmesh::LocalID newSize) {
810 cachedSize = newSize;
812 inline void VelocityBlockContainer::updateCachedSize() {
816 inline void VelocityBlockContainer::updateCachedCapacity() {
820 inline void VelocityBlockContainer::print_addresses() {
823 inline void VelocityBlockContainer::gpu_prefetchHost(
gpuStream_t stream=0) {
831 inline void VelocityBlockContainer::gpu_prefetchDevice(
gpuStream_t stream=0) {
848 if (blockLID >= numberOfBlocks) {
855 std::stringstream ss;
856 ss <<
"VBC ERROR: out of bounds in getData, LID=" << blockLID <<
" cell=" << cell <<
" #blocks=" << numberOfBlocks <<
" data->size()=" <<
block_data.size() << std::endl;
857 std::cerr << ss.str();
870 if (blockLID >= numberOfBlocks) {
877 std::stringstream ss;
878 ss <<
"VBC ERROR: out of bounds in getParameters, LID=" << blockLID <<
" cell=" << cell <<
" #blocks=" << numberOfBlocks <<
" parameters.size()=" <<
parameters.size() << std::endl;
879 std::cerr << ss.str();
886 inline void VelocityBlockContainer::setData(
const vmesh::LocalID blockLID,
const unsigned int cell,
const Realf value) {
892 if (blockLID >= numberOfBlocks) {
899 std::stringstream ss;
900 ss <<
"VBC ERROR: out of bounds in setData, LID=" << blockLID <<
" cell=" << cell <<
" #blocks=" << numberOfBlocks <<
" data->size()=" <<
block_data.size() << std::endl;
901 std::cerr << ss.str();
#define gpuStreamSynchronize
bool setNewCapacity(const vmesh::LocalID capacity)
static ARCH_HOSTDEV double getBlockAllocationFactor()
void exitInvalidLocalID(const vmesh::LocalID localID, const std::string &funcName) const
ARCH_HOSTDEV vmesh::LocalID capacity() const
ARCH_HOSTDEV vmesh::LocalID push_back_and_zero()
~VelocityBlockContainer()
ARCH_HOSTDEV size_t sizeInBytes() const
ARCH_HOSTDEV vmesh::LocalID push_back()
bool setNewCapacityShrink(const vmesh::LocalID reqCapacity)
std::vector< Realf, aligned_allocator< Realf, WID3 > > block_data
ARCH_HOSTDEV vmesh::LocalID size() const
void clear(bool shrink=true)
ARCH_HOSTDEV size_t capacityInBytes() const
std::vector< Real, aligned_allocator< Real, BlockParams::N_VELOCITY_BLOCK_PARAMS > > parameters
ARCH_HOSTDEV Real * getParameters()
ARCH_HOSTDEV Realf * getData()
ARCH_HOSTDEV void resize(const vmesh::LocalID newSize)
ARCH_HOSTDEV void move(const vmesh::LocalID source, const vmesh::LocalID target)
const VelocityBlockContainer & operator=(const VelocityBlockContainer &other)
ARCH_HOSTDEV bool setNewSize(const vmesh::LocalID newSize)
__host__ gpuStream_t gpu_getStream()
static const double BLOCK_ALLOCATION_PADDING
static const uint INIT_VMESH_SIZE(32768/WID3)
static const double BLOCK_ALLOCATION_FACTOR
@ N_VELOCITY_BLOCK_PARAMS