23#ifndef VLASIATOR_SPATIAL_CELL_KERNELS_HPP
24#define VLASIATOR_SPATIAL_CELL_KERNELS_HPP
26#ifdef USE_WARPACCESSORS
27 #define USE_BATCH_WARPACCESSORS
40 const
vmesh::VelocityBlockContainer* __restrict__ blockContainer,
41 Hashinator::Hashmap<
vmesh::GlobalID,
vmesh::LocalID>* vbwcl_map,
42 Hashinator::Hashmap<
vmesh::GlobalID,
vmesh::LocalID>* vbwncl_map,
43 const
Real velocity_block_min_value
46 const int blocki = blockIdx.x;
47 const uint
ti = threadIdx.x;
50 const uint vlasiBlocksPerWorkUnit = 1;
51 const uint workUnitIndex = 0;
54 const uint b_tid =
ti %
WID3;
55 const uint blockLID = blocki * vlasiBlocksPerWorkUnit + workUnitIndex;
59 const uint nBlocks =
vmesh->size();
60 if (blockLID < nBlocks) {
62 #ifdef DEBUG_SPATIAL_CELL
63 if (blockGID ==
vmesh->invalidGlobalID()) {
65 printf(
"Invalid GID encountered in update_velocity_block_content_lists_kernel!\n");
69 if (blockLID ==
vmesh->invalidLocalID()) {
71 printf(
"Invalid LID encountered in update_velocity_block_content_lists_kernel!\n");
77 const Realf* avgs = blockContainer->getData(blockLID);
78 has_content[
ti] = avgs[b_tid] >= velocity_block_min_value ? 1 : 0;
84 for (
unsigned int s=
WID3/2; s>0; s>>=1) {
89 has_content[
ti] = has_content[
ti] || has_content[
ti + s];
93 #ifdef USE_WARPACCESSORS
97 vbwcl_map->warpInsert(blockGID,blockLID,b_tid);
99 vbwncl_map->warpInsert(blockGID,blockLID,b_tid);
105 if (has_content[0]) {
106 vbwcl_map->set_element(blockGID,blockLID);
108 vbwncl_map->set_element(blockGID,blockLID);
126 const vmesh::GlobalID* __restrict__ velocity_block_with_content_list_data,
127 Hashinator::Hashmap<vmesh::GlobalID,vmesh::LocalID>* dev_velocity_block_with_content_map,
128 Hashinator::Hashmap<vmesh::GlobalID,vmesh::LocalID>* dev_velocity_block_with_no_content_map
131 const int blocki = blockIdx.x;
132 const int ti = threadIdx.x;
140 #ifdef __HIP_PLATFORM_HCC___
143 for (
int i=0;
i<max_i;
i++) {
144 int offsetIndex = offsetIndex1 + 13*
i;
146 if (offsetIndex > 12) {
149 const int offset_vx = (offsetIndex % 3) - 1;
150 const int offset_vy = ((offsetIndex / 3) % 3) - 1;
151 const int offset_vz = (offsetIndex / 9) - 1;
153 const vmesh::GlobalID GID = velocity_block_with_content_list_data[blocki];
155 vmesh->getIndices(GID,ind0,ind1,ind2);
156 const int nind0 = ind0 + offset_vx;
157 const int nind1 = ind1 + offset_vy;
158 const int nind2 = ind2 + offset_vz;
160 =
vmesh->getGlobalID(nind0,nind1,nind2);
161 #ifdef USE_WARPACCESSORS
165 const bool newlyadded = dev_velocity_block_with_content_map->warpInsert_V<
true>(nGID,LID, w_tid);
168 if ( LID !=
vmesh->invalidLocalID()) {
170 dev_velocity_block_with_no_content_map->warpErase(nGID, w_tid);
181 const bool newEntry = dev_velocity_block_with_content_map->set_element<
true>(nGID,LID);
184 if ( LID !=
vmesh->invalidLocalID()) {
186 dev_velocity_block_with_no_content_map->device_erase(nGID);
200 const uint neighbour_count,
203 Hashinator::Hashmap<vmesh::GlobalID,vmesh::LocalID>* dev_velocity_block_with_content_map,
204 Hashinator::Hashmap<vmesh::GlobalID,vmesh::LocalID>* dev_velocity_block_with_no_content_map
208 const int ti = threadIdx.x;
213 for (uint
i=0;
i<neighbour_count;
i++) {
214 if (myindex < (
int)dev_neigh_Nvbwcls[neigh_i]) {
217 myindex -= dev_neigh_Nvbwcls[neigh_i];
221 if (neigh_i < neighbour_count) {
223 #ifdef USE_WARPACCESSORS
227 const bool newlyadded = dev_velocity_block_with_content_map->warpInsert_V<
true>(nGID,LID, w_tid);
230 if ( LID !=
vmesh->invalidLocalID()) {
232 dev_velocity_block_with_no_content_map->warpErase(nGID, w_tid);
243 const bool newEntry = dev_velocity_block_with_content_map->set_element<
true>(nGID,LID);
246 if ( LID !=
vmesh->invalidLocalID()) {
248 dev_velocity_block_with_no_content_map->device_erase(nGID);
268 const int blocki = blockIdx.x;
269 const int blockSize = blockDim.x*blockDim.y*blockDim.z;
270 const uint
ti = threadIdx.z*blockDim.x*blockDim.y + threadIdx.y*blockDim.x + threadIdx.x;
275 Hashinator::Hashmap<vmesh::GlobalID,vmesh::LocalID> *map = dev_vmesh->
gpu_expose_map();
276 split::SplitVector<vmesh::GlobalID> *list = dev_vmesh->
getGrid();
278 #ifdef USE_WARPACCESSORS
282 const uint w_tid = 0;
290 #ifdef USE_WARPACCESSORS
292 map->warpInsert_V<
true>(GID,LID, w_tid);
294 map->set_element<
true>(GID,LID);
309 const split::SplitVector<Hashinator::hash_pair<vmesh::GlobalID,vmesh::LocalID>>* __restrict__ list_delete,
310 const split::SplitVector<Hashinator::hash_pair<vmesh::GlobalID,vmesh::LocalID>>* __restrict__ list_to_replace,
311 const split::SplitVector<Hashinator::hash_pair<vmesh::GlobalID,vmesh::LocalID>>* __restrict__ list_with_replace_old,
313 Realf* gpu_rhoLossAdjust
317 const vmesh::LocalID nToRemove = list_delete->size() + list_to_replace->size();
318 const vmesh::LocalID nBlocksAfterAdjust = nBlocksBeforeAdjust + nToAdd - nToRemove;
319 const vmesh::LocalID nBlocksToChange = nToAdd > nToRemove ? nToAdd : nToRemove;
321 gpu_rhoLossAdjust[0] = 0.0;
322 returnLID[0] = nBlocksBeforeAdjust;
323 returnLID[1] = nBlocksAfterAdjust;
324 returnLID[2] = nBlocksToChange;
326 if (nBlocksAfterAdjust > nBlocksBeforeAdjust) {
327 if ((nBlocksAfterAdjust <= vmesh->capacity()) && (nBlocksAfterAdjust <= blockContainer->capacity())) {
329 vmesh->device_setNewSize(nBlocksAfterAdjust);
330 blockContainer->
setNewSize(nBlocksAfterAdjust);
346 vmesh->device_setNewSize(nBlocksAfterAdjust);
347 blockContainer->
setNewSize(nBlocksAfterAdjust);
353 vmesh::VelocityBlockContainer *blockContainer,
355 const split::SplitVector<Hashinator::hash_pair<
vmesh::GlobalID,
vmesh::LocalID>>* __restrict__ list_delete,
356 const split::SplitVector<Hashinator::hash_pair<
vmesh::GlobalID,
vmesh::LocalID>>* __restrict__ list_to_replace,
357 const split::SplitVector<Hashinator::hash_pair<
vmesh::GlobalID,
vmesh::LocalID>>* __restrict__ list_with_replace_old,
358 const
vmesh::LocalID nBlocksBeforeAdjust,
359 const
vmesh::LocalID nBlocksToChange,
360 const
vmesh::LocalID nBlocksAfterAdjust,
361 Realf* gpu_rhoLossAdjust
366 const uint ti = threadIdx.x;
379 const int b_tid =
ti %
WID3;
384 const vmesh::LocalID n_with_replace_old = list_with_replace_old->size();
395 if (
index < n_delete) {
396 #ifdef DEBUG_SPATIAL_CELL
404 #ifdef DEBUG_SPATIAL_CELL
405 if (rmGID ==
vmesh->invalidGlobalID()) {
406 if (rmLID !=
vmesh->invalidLocalID()) {
409 printf(
"Removing blocks: Valid LID %u but invalid GID!\n",rmLID);
413 printf(
"Removing blocks: Invalid LID and GID!\n");
418 if (rmLID ==
vmesh->invalidLocalID()) {
419 if (rmGID !=
vmesh->invalidGlobalID()) {
422 printf(
"Removing blocks: Valid GID %ul but invalid LID!\n",rmGID);
427 if ((
unsigned long)rmLID >= (
unsigned long)nBlocksBeforeAdjust) {
429 printf(
"Trying to outright remove block which has LID %ul >= nBlocksBeforeAdjust %ul!\n",rmLID,nBlocksBeforeAdjust);
433 if ((
unsigned long)rmLID < (
unsigned long)nBlocksAfterAdjust) {
435 printf(
"Trying to outright remove block which has LID %u smaller than nBlocksAfterAdjust %u!\n",rmLID,nBlocksAfterAdjust);
442 Realf* rm_avgs = blockContainer->getData(rmLID);
443 Real* rm_block_parameters = blockContainer->getParameters(rmLID);
448 massloss[
ti] = rm_avgs[b_tid]*rm_DV3;
451 for (
int s=
WID3/2;
s>0;
s>>=1) {
453 massloss[
ti] += massloss[
ti +
s];
459 Realf old = atomicAdd(gpu_rhoLossAdjust, massloss[
ti]);
464 #ifdef USE_WARPACCESSORS
465 vmesh->warpDeleteBlock(rmGID,rmLID,b_tid);
468 vmesh->deleteBlock(rmGID,rmLID);
480 if (
index < n_to_replace) {
481 #ifdef DEBUG_SPATIAL_CELL
490 #ifdef DEBUG_SPATIAL_CELL
491 if (rmGID ==
vmesh->invalidGlobalID()) {
492 if (rmLID !=
vmesh->invalidLocalID()) {
495 printf(
"Replacing blocks: Valid LID %u but invalid GID!\n",rmLID);
499 printf(
"Replacing blocks: Invalid LID and GID!\n");
504 if (rmLID ==
vmesh->invalidLocalID()) {
505 if (rmGID !=
vmesh->invalidGlobalID()) {
508 printf(
"Replacing blocks: Valid GID %ul but invalid LID!\n",rmGID);
513 if (rmLID >= nBlocksBeforeAdjust) {
515 printf(
"Trying to replace block which has LID %ul >= nBlocksBeforeAdjust %ul!\n",rmLID,nBlocksBeforeAdjust);
522 Realf* rm_avgs = blockContainer->getData(rmLID);
523 Real* rm_block_parameters = blockContainer->getParameters(rmLID);
528 massloss[
ti] = rm_avgs[b_tid]*rm_DV3;
531 for (
int s=
WID3/2;
s>0;
s>>=1) {
533 massloss[
ti] += massloss[
ti +
s];
539 Realf old = atomicAdd(gpu_rhoLossAdjust, massloss[
ti]);
548 if (
index < n_with_replace_old) {
549 #ifdef DEBUG_SPATIAL_CELL
550 replaceGID = (list_with_replace_old->at(
index)).first;
551 replaceLID = (list_with_replace_old->at(
index)).second;
553 replaceGID = ((*list_with_replace_old)[
index]).first;
554 replaceLID = ((*list_with_replace_old)[
index]).second;
557 Realf* repl_avgs = blockContainer->getData(replaceLID);
558 Real* repl_block_parameters = blockContainer->getParameters(replaceLID);
559 rm_avgs[b_tid] = repl_avgs[b_tid];
561 rm_block_parameters[b_tid] = repl_block_parameters[b_tid];
567 #ifdef DEBUG_SPATIAL_CELL
570 replaceGID = (*list_with_replace_new)[
index - n_with_replace_old];
572 replaceLID =
vmesh->invalidLocalID();
582 #ifdef USE_WARPACCESSORS
583 vmesh->warpReplaceBlock(rmGID,rmLID,replaceGID,b_tid);
586 vmesh->replaceBlock(rmGID,rmLID,replaceGID);
590 #ifdef DEBUG_SPATIAL_CELL
592 if (
vmesh->getGlobalID(rmLID) != replaceGID) {
594 printf(
"Error! Replacing did not result in wanted GID at old LID in update_velocity_blocks_kernel! \n");
598 if (
vmesh->getLocalID(replaceGID) != rmLID) {
600 printf(
"Error! Replacing did not result in old LID at replaced GID in update_velocity_blocks_kernel! \n");
608 index -= n_to_replace;
615 const uint add_index =
index + (n_to_replace - n_with_replace_old);
616 if (add_index < n_with_replace_new) {
617 #ifdef DEBUG_SPATIAL_CELL
620 if (
vmesh->getLocalID(addGID) !=
vmesh->invalidLocalID()) {
621 printf(
"Trying to add new GID %u to mesh which already contains it! index=%u addindex=%u\n",addGID,
index,add_index);
630 Realf* add_avgs = blockContainer->getData(addLID);
631 #ifdef DEBUG_SPATIAL_CELL
632 if (addGID ==
vmesh->invalidGlobalID()) {
633 printf(
"Error! invalid addGID!\n");
636 if (addLID ==
vmesh->invalidLocalID()) {
637 printf(
"Error! invalid addLID!\n");
641 Real* add_block_parameters = blockContainer->getParameters(addLID);
651 #ifdef USE_WARPACCESSORS
652 vmesh->warpPlaceBlock(addGID,addLID,b_tid);
655 vmesh->placeBlock(addGID,addLID);
659 #ifdef DEBUG_SPATIAL_CELL
661 if (
vmesh->getGlobalID(addLID) ==
vmesh->invalidGlobalID()) {
662 printf(
"Error! invalid GID after add from addLID!\n");
664 if (
vmesh->getLocalID(addGID) ==
vmesh->invalidLocalID()) {
665 printf(
"Error! invalid LID after add from addGID!\n");
673 printf(
"Error! Fall through in update_velocity_blocks_kernel! index %u nBlocksBeforeAdjust %u nBlocksAfterAdjust %u \n",
674 index,nBlocksBeforeAdjust,nBlocksAfterAdjust);
ARCH_HOSTDEV Real * getParameters()
ARCH_HOSTDEV bool setNewSize(const vmesh::LocalID newSize)
ARCH_HOSTDEV Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > * gpu_expose_map()
std::vector< vmesh::GlobalID > * getGrid()
void getBlockInfo(const vmesh::GlobalID &globalID, Real *array) const
split::SplitVector< vmesh::GlobalID > * list_with_replace_new
const Real *__restrict__ blockParameters
@ N_VELOCITY_BLOCK_PARAMS
__global__ void update_neighbour_halo_kernel(const vmesh::VelocityMesh *__restrict__ vmesh, const uint neighbour_count, const vmesh::GlobalID *__restrict__ const *dev_neigh_vbwcls, const vmesh::LocalID *__restrict__ dev_neigh_Nvbwcls, Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > *dev_velocity_block_with_content_map, Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > *dev_velocity_block_with_no_content_map)
__global__ void __launch_bounds__(WID3, 4) update_velocity_block_content_lists_kernel(const vmesh
__global__ void resize_vbc_kernel_pre(vmesh::VelocityMesh *vmesh, vmesh::VelocityBlockContainer *blockContainer, const split::SplitVector< vmesh::GlobalID > *__restrict__ list_with_replace_new, const split::SplitVector< Hashinator::hash_pair< vmesh::GlobalID, vmesh::LocalID > > *__restrict__ list_delete, const split::SplitVector< Hashinator::hash_pair< vmesh::GlobalID, vmesh::LocalID > > *__restrict__ list_to_replace, const split::SplitVector< Hashinator::hash_pair< vmesh::GlobalID, vmesh::LocalID > > *__restrict__ list_with_replace_old, vmesh::LocalID *returnLID, Realf *gpu_rhoLossAdjust)
__global__ void update_velocity_halo_kernel(const vmesh::VelocityMesh *__restrict__ vmesh, const vmesh::LocalID velocity_block_with_content_list_size, const vmesh::GlobalID *__restrict__ velocity_block_with_content_list_data, Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > *dev_velocity_block_with_content_map, Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > *dev_velocity_block_with_no_content_map)
__global__ void resize_vbc_kernel_post(vmesh::VelocityMesh *vmesh, vmesh::VelocityBlockContainer *blockContainer, const vmesh::LocalID nBlocksAfterAdjust)
__global__ void update_vmesh_and_blockparameters_kernel(vmesh::VelocityMesh *dev_vmesh, vmesh::VelocityBlockContainer *dev_blockContainer, const vmesh::LocalID nLIDs)