23template <
class T0,
class T1>
27 operator() (T0* d, T1 s)
const noexcept
29 *d =
static_cast<T0
>(s);
33template <
class T0,
class T1>
37 operator() (T0* d, T1 s)
const noexcept
39 *d +=
static_cast<T0
>(s);
43template <
class T0,
class T1>
49 operator() (U0* d, T1 s)
const noexcept
51 Gpu::Atomic::AddNoRet(d,
static_cast<U0
>(s));
55template <
class T0,
class T1,
class F>
57fab_to_fab (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
int dcomp,
int ncomp,
60 TagVector<Array4CopyTag<T0, T1>> tv{copy_tags};
62 detail::ParallelFor_doit(tv,
64 int icell,
int ncells,
int i,
int j,
int k, Array4CopyTag<T0, T1>
const& tag)
noexcept
67 for (
int n = 0; n < ncomp; ++n) {
68 f(&(tag.dfab(i,j,k,n+dcomp)),
69 tag.sfab(i+tag.offset.x,j+tag.offset.y,k+tag.offset.z,n+scomp));
75template <
class TagType,
class F>
77fab_to_fab_store (Vector<TagType>
const& tags,
int scomp,
int dcomp,
int ncomp,
F&&f)
84 for (
int n = 0; n < ncomp; ++n) {
85 f(&(tag.dfab(i,j,k,n+dcomp)),
86 tag.sfab(i+tag.offset.x,j+tag.offset.y,k+tag.offset.z,n+scomp));
92template <
class TagType,
class F>
94fab_to_fab_other (Vector<TagType>
const& tags,
int scomp,
int dcomp,
int ncomp,
F&&f)
99 int* m = &(tag.mask(i,j,k));
100 bool my_turn =
false;
102#if defined(AMREX_USE_SYCL)
108#if defined(AMREX_USE_SYCL)
109 sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::device);
113 for (
int n = 0; n < ncomp; ++n) {
114 f(&(tag.dfab(i,j,k,n+dcomp)),
115 tag.sfab(i+tag.offset.x,j+tag.offset.y,k+tag.offset.z,n+scomp));
117#if defined(AMREX_USE_SYCL)
118 sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::device);
125#if defined(AMREX_USE_CUDA)
127#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 700)
130 for (
int c = 0; c < 2; ++c) {
134 for (
int c = 0; c < 2; ++c) {
135 __asm__
volatile(
"");
142#elif defined(AMREX_USE_HIP)
144 __builtin_amdgcn_s_sleep(1);
146#elif defined(AMREX_USE_SYCL)
148 for (
int c = 0; c < 2; ++c) {
149 __asm__
volatile(
"");
158template <
class T0,
class T1,
class F>
160fab_to_fab (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
int dcomp,
161 int ncomp,
F && f, Vector<Array4Tag<int> >
const& masks)
163 using TagType = Array4MaskCopyTag<T0, T1>;
164 Vector<TagType> tags;
165 const int N = copy_tags.size();
167 for (
int i = 0; i < N; ++i) {
168 tags.push_back(TagType{.dfab = copy_tags[i].dfab, .sfab = copy_tags[i].sfab,
169 .mask = masks[i].dfab, .dbox = copy_tags[i].dbox,
170 .offset = copy_tags[i].offset});
173 if constexpr (std::is_same_v<F, CellStore<T0,T1>>)
175 fab_to_fab_store(tags, scomp, dcomp, ncomp, std::forward<F>(f));
179 fab_to_fab_other(tags, scomp, dcomp, ncomp, std::forward<F>(f));
184template <
typename T0,
typename T1>
187fab_to_fab_atomic_cpy (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
188 int dcomp,
int ncomp, Vector<Array4Tag<int> >
const&)
190 fab_to_fab<T0, T1>(copy_tags, scomp, dcomp, ncomp, CellStore<T0, T1>());
193template <
typename T0,
typename T1>
196fab_to_fab_atomic_cpy (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
197 int dcomp,
int ncomp, Vector<Array4Tag<int> >
const& masks)
199 fab_to_fab(copy_tags, scomp, dcomp, ncomp, CellStore<T0, T1>(), masks);
202template <
typename T0,
typename T1>
205fab_to_fab_atomic_add (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
206 int dcomp,
int ncomp, Vector<Array4Tag<int> >
const&)
208 fab_to_fab(copy_tags, scomp, dcomp, ncomp, CellAtomicAdd<T0, T1>());
211template <
typename T0,
typename T1>
214fab_to_fab_atomic_add (Vector<Array4CopyTag<T0, T1> >
const& copy_tags,
int scomp,
215 int dcomp,
int ncomp, Vector<Array4Tag<int> >
const& masks)
217 fab_to_fab(copy_tags, scomp, dcomp, ncomp, CellAdd<T0, T1>(), masks);
220template <
typename T0,
typename T1,
class F>
221void deterministic_fab_to_fab (Vector<Array4CopyTag<T0,T1>>
const& a_tags,
int scomp,
222 int dcomp,
int ncomp,
F const& f)
224 if (a_tags.empty()) {
return; }
226 using TagType = Array4CopyTag<T0,T1>;
230 std::pair<int,Box> dindex_tilebox;
231 bool operator< (TiledTag
const& rhs)
const noexcept {
232 return this->dindex_tilebox < rhs.dindex_tilebox;
234 bool operator!= (TiledTag
const& rhs)
const noexcept {
235 return this->dindex_tilebox != rhs.dindex_tilebox;
238 Vector<TiledTag> tiled_tags;
240 auto const ixtype = a_tags[0].dbox.ixType();
242 constexpr int tile_size = 64;
243 for (
int itag = 0; itag < a_tags.size(); ++itag) {
244 auto const& tag = a_tags[itag];
245 auto const& dlo = tag.dbox.smallEnd();
246 auto const& dhi = tag.dbox.bigEnd();
248 amrex::coarsen<tile_size>(dlo[1]),
249 amrex::coarsen<tile_size>(dlo[2])));
251 amrex::coarsen<tile_size>(dhi[1]),
252 amrex::coarsen<tile_size>(dhi[2])));
253#if (AMREX_SPACEDIM == 3)
254 for (
int kt = tlo[2]; kt <= thi[2]; ++kt)
257#if (AMREX_SPACEDIM >= 2)
258 for (
int jt = tlo[1]; jt <= thi[1]; ++jt)
261 for (
int it = tlo[0]; it <= thi[0]; ++it)
266 tiled_tags.push_back(TiledTag{
268 .dindex_tilebox = std::make_pair(tag.dindex,
Box(lo, lo+(tile_size-1), ixtype))
275 std::sort(tiled_tags.begin(), tiled_tags.end());
277 Gpu::HostVector<unsigned int> h_ntags;
278 Gpu::HostVector<TagType> h_tags;
279 h_tags.reserve(tiled_tags.size());
281 for (
unsigned int itag = 0; itag < tiled_tags.size(); ++itag) {
283 h_ntags.push_back(0);
284 }
else if (tiled_tags[itag-1] != tiled_tags[itag]) {
285 h_ntags.push_back(itag);
287 auto const& ttag = tiled_tags[itag];
288 auto const& btag = a_tags[ttag.tag_index];
289 h_tags.push_back(TagType{.dfab = btag.dfab, .dindex = btag.dindex, .sfab = btag.sfab,
290 .dbox = btag.dbox & ttag.dindex_tilebox.second,
291 .offset = btag.offset});
293 h_ntags.push_back((
unsigned int)tiled_tags.size());
295 Gpu::DeviceVector<TagType> d_tags(h_tags.size());
296 Gpu::DeviceVector<unsigned int> d_ntags(h_ntags.size());
299 auto const* ptag = d_tags.data();
300 auto const* pntags = d_ntags.data();
301 auto const nblocks =
int(h_ntags.size()-1);
302 constexpr auto nthreads = 256;
306 [[sycl::reqd_work_group_size(nthreads)]]
312 Dim1 blockIdx{item.get_group_linear_id()};
313 Dim1 threadIdx{item.get_local_linear_id()};
316 for (
unsigned int itag = pntags[blockIdx.x]; itag < pntags[blockIdx.x+1]; ++itag) {
317 auto const tag = ptag[itag];
318 auto ncells =
int(tag.dbox.numPts());
321 for (
int icell =
int(threadIdx.x); icell < ncells; icell += nthreads) {
322 int k = icell / (len.x*len.y);
323 int j = (icell - k*(len.x*len.y)) / len.x;
324 int i = (icell - k*(len.x*len.y)) - j*len.x;
328 for (
int n = 0; n < ncomp; ++n) {
329 f(tag.dfab.ptr(i,j,k,n+dcomp),
330 tag.sfab(i + tag.offset.x,
332 k + tag.offset.z, n+scomp));
336 if (itag+1 < pntags[blockIdx.x+1]) {
338 sycl::group_barrier(item.get_group());
348template <
typename B,
typename V,
typename TT>
350void unpack_recv_buffer_gpu_atomic_add (
char* pbuffer, TagVector<TT>
const& tv,
351 int dcomp,
int ncomp)
353 detail::ParallelFor_doit(tv,
355 int icell,
int ncells,
int i,
int j,
int k, TT
const& tag)
noexcept
357 if (icell < ncells) {
358 Array4<B const> sfab{(B
const*)(pbuffer+tag.poff),
360 for (
int n = 0; n < ncomp; ++n) {
368template <
typename B,
typename V,
typename TT>
370void unpack_recv_buffer_gpu_atomic_add (
char* pbuffer, TagVector<TT>
const& tv,
371 int dcomp,
int ncomp)
374 amrex::Abort(
"unpack_recv_buffer_gpu: should NOT get here");
386 auto const& LocTags = *(TheFB.
m_LocTags);
387 auto N_locs =
static_cast<int>(LocTags.size());
388 if (N_locs == 0) {
return; }
393#pragma omp parallel for
395 for (
int i = 0; i < N_locs; ++i)
404 dfab->template copy<RunOn::Host>(*sfab, tag.
sbox, scomp, tag.
dbox, scomp, ncomp);
410 for (
int i = 0; i < N_locs; ++i)
417 loc_copy_tags[tag.
dstIndex].push_back
425 const auto& tags = loc_copy_tags[mfi];
426 auto dfab = this->array(mfi);
427 for (
auto const & tag : tags)
429 auto const sfab = tag.sfab->array();
430 const auto offset = tag.offset.dim3();
432 [=] (
int i,
int j,
int k,
int n)
noexcept
434 dfab(i,j,k,n+scomp) = sfab(i+offset.x,j+offset.y,k+offset.z,n+scomp);
445 auto const& LocTags = *(TheFB.
m_LocTags);
446 auto N_locs =
static_cast<int>(LocTags.size());
447 if (N_locs == 0) {
return; }
451 std::vector<FAB> src_fabs(N_locs);
452 for (
int itag = 0; itag < N_locs; ++itag) {
454 src_fabs[itag].resize(tag.
sbox,ncomp);
455 loc_copy_tags[tag.
dstIndex].push_back
461#pragma omp parallel for
463 for (
int itag = 0; itag < N_locs; ++itag) {
465 src_fabs[itag].template copy<RunOn::Host>(this->
operator[](tag.
srcIndex), scomp, 0, ncomp);
473 const auto& tags = loc_copy_tags[mfi];
474 const auto& dfab = this->array(mfi);
475 for (
auto const & tag : tags)
477 auto const sfab = tag.sfab->array();
478 const auto offset = tag.offset.dim3();
480 [&] (
int i,
int j,
int k,
int n)
noexcept
482 dfab(i,j,k,n+scomp) += sfab(i+offset.x,j+offset.y,k+offset.z,n);
494 auto const& LocTags = *(TheFB.m_LocTags);
495 int N_locs = LocTags.size();
500 if (
auto it = m_fb_local_copy_handler.find(TheFB.m_id);
501 it != m_fb_local_copy_handler.end())
503 tv = it->second.get();
506 loc_copy_tags.reserve(N_locs);
508 for (
int i = 0; i < N_locs; ++i)
515 int li = this->localindex(tag.
dstIndex);
516 loc_copy_tags.push_back
517 (TagType{.dfab = this->atLocalIdx(li).array(),
519 .sfab = this->fabPtr(tag.
srcIndex)->const_array(),
524 auto utv = std::make_unique<TagVector<TagType>>(loc_copy_tags);
526 m_fb_local_copy_handler[TheFB.m_id] = std::move(utv);
536 auto const& LocTags = *(TheFB.
m_LocTags);
537 int N_locs = LocTags.size();
538 if (N_locs == 0) {
return; }
545 auto* tv = FB_get_local_copy_tag_vector(TheFB);
547 detail::ParallelFor_doit(*tv,
549 int icell,
int ncells,
int i,
int j,
int k, TagType
const& tag)
noexcept
551 if (icell < ncells) {
552 for (
int n = 0; n < ncomp; ++n) {
553 tag.dfab(i,j,k,n+scomp) = tag.sfab(i+tag.offset.x,
555 k+tag.offset.z,n+scomp);
563 loc_copy_tags.reserve(N_locs);
567 masks_unique.reserve(this->local_size());
569 masks.reserve(N_locs);
571 for (
int i = 0; i < N_locs; ++i)
578 int li = this->localindex(tag.
dstIndex);
579 loc_copy_tags.push_back
580 (TagType{.dfab = this->atLocalIdx(li).array(),
582 .sfab = this->fabPtr(tag.
srcIndex)->const_array(),
586 if (!maskfabs[li].isAllocated()) {
587 maskfabs[li].resize(this->atLocalIdx(li).box());
599 detail::fab_to_fab_atomic_cpy<value_type, value_type>(
600 loc_copy_tags, scomp, scomp, ncomp, masks);
608 auto const& LocTags = *(TheFB.
m_LocTags);
609 int N_locs = LocTags.size();
610 if (N_locs == 0) {
return; }
615 loc_copy_tags_1.reserve(N_locs);
616 loc_copy_tags_2.reserve(N_locs);
619 for (
int itag = 0; itag < N_locs; ++itag) {
621 src_fabs[itag].resize(tag.
sbox,ncomp);
622 loc_copy_tags_1.push_back(
623 TagType{.dfab = src_fabs[itag].array(), .dindex = -1,
624 .sfab = this->const_array(tag.
srcIndex,scomp), .dbox = tag.
sbox,
625 .offset =
Dim3{.x = 0, .y = 0, .z = 0}});
626 loc_copy_tags_2.push_back(
628 .sfab = src_fabs[itag].const_array(), .dbox = tag.
dbox,
636 detail::fab_to_fab(loc_copy_tags_1, 0, 0, ncomp,
637 detail::CellStore<value_type, value_type>{});
639 detail::deterministic_fab_to_fab(loc_copy_tags_2, 0, 0, ncomp,
640 detail::CellAdd<value_type,value_type>{});
643 detail::fab_to_fab(loc_copy_tags_2, 0, 0, ncomp,
644 detail::CellAtomicAdd<value_type, value_type>{});
657 auto const& LocTags = *(thecmd.
m_LocTags);
658 int N_locs = LocTags.
size();
659 if (N_locs == 0) {
return; }
664 loc_setval_tags.reserve(N_locs);
668 for (
int i = 0; i < N_locs; ++i)
672 loc_setval_tags.push_back(TagType{.dfab = this->array(tag.
dstIndex), .dbox = tag.
dbox});
676 [
x,scomp]
AMREX_GPU_DEVICE (
int i,
int j,
int k,
int n, TagType
const& tag)
noexcept
678 tag.dfab(i,j,k,n+scomp) =
x;
687 auto const& RcvTags = *(thecmd.
m_RcvTags);
693 for (
auto it = RcvTags.begin(); it != RcvTags.end(); ++it) {
694 for (
auto const& tag: it->second) {
695 rcv_setval_tags.push_back(TagType{.dfab = this->array(tag.dstIndex), .dbox = tag.dbox});
699 if (rcv_setval_tags.empty()) {
return; }
704 [
x,scomp]
AMREX_GPU_DEVICE (
int i,
int j,
int k,
int n, TagType
const& tag)
noexcept
706 tag.dfab(i,j,k,n+scomp) =
x;
710#if defined(__CUDACC__) && defined (AMREX_USE_CUDA)
715 const int N_locs = (*TheFB.m_LocTags).size();
717 for (
int i = 0; i < N_locs; ++i)
719 const CopyComTag& tag = (*TheFB.m_LocTags)[i];
724 loc_copy_tags[tag.dstIndex].push_back
725 ({this->fabPtr(tag.srcIndex), tag.dbox, tag.sbox.smallEnd()-tag.dbox.smallEnd()});
729 if ( !(TheFB.m_localCopy.ready()) )
731 const_cast<FB&
>(TheFB).m_localCopy.resize(N_locs);
735 for (MFIter mfi(*
this, MFItInfo().DisableDeviceSync()); mfi.isValid(); ++mfi)
737 amrex::Gpu::Device::startGraphRecording( (mfi.LocalIndex() == 0),
738 const_cast<FB&
>(TheFB).m_localCopy.getHostPtr(0),
739 (TheFB).m_localCopy.getDevicePtr(0),
740 std::size_t(
sizeof(CopyMemory)*N_locs) );
742 const auto& tags = loc_copy_tags[mfi];
743 for (
auto const & tag : tags)
745 const auto offset = tag.offset.dim3();
746 CopyMemory* cmem = TheFB.m_localCopy.getDevicePtr(idx++);
750 auto const dst = cmem->getDst<value_type>();
751 auto const src = cmem->getSrc<value_type>();
752 for (int n = 0; n < cmem->ncomp; ++n) {
753 dst(i,j,k,(cmem->scomp)+n) = src(i+offset.x,j+offset.y,k+offset.z,(cmem->scomp)+n);
758 bool last_iter = mfi.LocalIndex() == (this->local_size()-1);
759 cudaGraphExec_t graphExec = amrex::Gpu::Device::stopGraphRecording(last_iter);
760 if (last_iter) {
const_cast<FB&
>(TheFB).m_localCopy.setGraph( graphExec ); }
769 for (MFIter mfi(*
this); mfi.isValid(); ++mfi)
771 auto const dst_array = this->array(mfi);
772 const auto& tags = loc_copy_tags[mfi];
773 for (
auto const & tag : tags)
775 const_cast<FB&
>(TheFB).m_localCopy.setParams(idx++, makeCopyMemory(tag.sfab->array(),
782 TheFB.m_localCopy.executeGraph();
788FabArray<FAB>::FB_local_copy_cuda_graph_n (
const FB& TheFB,
int scomp,
int ncomp)
790 const int N_locs = TheFB.m_LocTags->size();
794 for (
int i = 0; i < N_locs; ++i)
796 const CopyComTag& tag = (*TheFB.m_LocTags)[i];
798 BL_ASSERT(ParallelDescriptor::sameTeam(distributionMap[tag.dstIndex]));
799 BL_ASSERT(ParallelDescriptor::sameTeam(distributionMap[tag.srcIndex]));
801 if (distributionMap[tag.dstIndex] == ParallelDescriptor::MyProc())
803 loc_copy_tags[tag.dstIndex].push_back
804 ({this->fabPtr(tag.srcIndex), tag.dbox, tag.sbox.smallEnd()-tag.dbox.smallEnd()});
811 if ( !(TheFB.m_localCopy.ready()) )
813 const_cast<FB&
>(TheFB).m_localCopy.resize(launches);
817 for (MFIter mfi(*
this, MFItInfo().DisableDeviceSync()); mfi.isValid(); ++mfi)
819 const auto& tags = loc_copy_tags[mfi];
820 for (
int t = 0; t<tags.size(); ++t)
822 Gpu::Device::setStreamIndex(cuda_stream++);
823 amrex::Gpu::Device::startGraphRecording( (idx == 0),
824 const_cast<FB&
>(TheFB).m_localCopy.getHostPtr(0),
825 (TheFB).m_localCopy.getDevicePtr(0),
826 std::size_t(
sizeof(CopyMemory)*launches) );
828 const auto& tag = tags[t];
829 const Dim3
offset = tag.offset.dim3();
831 CopyMemory* cmem = TheFB.m_localCopy.getDevicePtr(idx++);
834 auto const dst = cmem->getDst<value_type>();
835 auto const src = cmem->getSrc<value_type>();
836 for (int n = 0; n < cmem->ncomp; ++n) {
837 dst(i,j,k,(cmem->scomp)+n) = src(i+offset.x,j+offset.y,k+offset.z,(cmem->scomp)+n);
841 bool last_iter = idx == launches;
842 cudaGraphExec_t graphExec = Gpu::Device::stopGraphRecording(last_iter);
843 if (last_iter) {
const_cast<FB&
>(TheFB).m_localCopy.setGraph( graphExec ); }
851 for (MFIter mfi(*
this); mfi.isValid(); ++mfi)
853 const auto& dst_array = this->array(mfi);
854 const auto& tags = loc_copy_tags[mfi];
855 for (
auto const & tag : tags)
857 const_cast<FB&
>(TheFB).m_localCopy.setParams(idx++, makeCopyMemory(tag.sfab->array(),
864 TheFB.m_localCopy.executeGraph(
false);
876#if defined(__CUDACC__) && defined(AMREX_USE_CUDA)
880FabArray<FAB>::FB_pack_send_buffer_cuda_graph (
const FB& TheFB,
int scomp,
int ncomp,
881 Vector<char*>& send_data,
882 Vector<std::size_t>
const& send_size,
883 Vector<
typename FabArray<FAB>::CopyComTagsContainer
const*>
const& send_cctc)
885 const int N_snds = send_data.size();
886 if (N_snds == 0) {
return; }
888 if ( !(TheFB.m_copyToBuffer.ready()) )
893 for (
int send = 0; send < N_snds; ++send) {
894 if (send_size[send] > 0) {
895 launches += send_cctc[send]->size();
898 const_cast<FB&
>(TheFB).m_copyToBuffer.resize(launches);
902 for (Gpu::StreamIter sit(N_snds,Gpu::StreamItInfo().DisableDeviceSync());
903 sit.isValid(); ++sit)
905 amrex::Gpu::Device::startGraphRecording( (sit() == 0),
906 const_cast<FB&
>(TheFB).m_copyToBuffer.getHostPtr(0),
907 (TheFB).m_copyToBuffer.getDevicePtr(0),
908 std::size_t(
sizeof(CopyMemory)*launches) );
911 if (send_size[j] > 0)
913 auto const& cctc = *send_cctc[j];
914 for (
auto const& tag : cctc)
916 const Box& bx = tag.sbox;
917 CopyMemory* cmem = TheFB.m_copyToBuffer.getDevicePtr(idx++);
920 auto const pfab = cmem->getDst<value_type>();
921 auto const sfab = cmem->getSrc<value_type>();
922 for (
int n = 0; n < cmem->ncomp; ++n)
924 pfab(ii,jj,kk,n) = sfab(ii,jj,kk,n+(cmem->scomp));
930 bool last_iter = sit() == (N_snds-1);
931 cudaGraphExec_t graphExec = amrex::Gpu::Device::stopGraphRecording(last_iter);
932 if (last_iter) {
const_cast<FB&
>(TheFB).m_copyToBuffer.setGraph( graphExec ); }
938 for (
int send = 0; send < N_snds; ++send)
941 if (send_size[j] > 0)
943 char* dptr = send_data[j];
944 auto const& cctc = *send_cctc[j];
945 for (
auto const& tag : cctc)
947 const_cast<FB&
>(TheFB).m_copyToBuffer.setParams(idx++, makeCopyMemory(this->array(tag.srcIndex),
953 dptr += (tag.sbox.numPts() * ncomp *
sizeof(value_type));
956 BL_ASSERT(dptr <= send_data[j] + send_size[j]);
961 TheFB.m_copyToBuffer.executeGraph();
966FabArray<FAB>::FB_unpack_recv_buffer_cuda_graph (
const FB& TheFB,
int dcomp,
int ncomp,
967 Vector<char*>
const& recv_data,
968 Vector<std::size_t>
const& recv_size,
969 Vector<CopyComTagsContainer const*>
const& recv_cctc,
972 const int N_rcvs = recv_cctc.size();
973 if (N_rcvs == 0) {
return; }
977 for (
int k = 0; k < N_rcvs; ++k)
979 if (recv_size[k] > 0)
981 const char* dptr = recv_data[k];
982 auto const& cctc = *recv_cctc[k];
983 for (
auto const& tag : cctc)
985 recv_copy_tags[tag.dstIndex].push_back(VoidCopyTag{.p = dptr, .dbox = tag.dbox});
986 dptr += tag.dbox.numPts() * ncomp *
sizeof(value_type);
990 BL_ASSERT(dptr <= recv_data[k] + recv_size[k]);
994 if ( !(TheFB.m_copyFromBuffer.ready()) )
996 const_cast<FB&
>(TheFB).m_copyFromBuffer.resize(launches);
999 for (MFIter mfi(*
this, MFItInfo().DisableDeviceSync()); mfi.isValid(); ++mfi)
1001 amrex::Gpu::Device::startGraphRecording( (mfi.LocalIndex() == 0),
1002 const_cast<FB&
>(TheFB).m_copyFromBuffer.getHostPtr(0),
1003 (TheFB).m_copyFromBuffer.getDevicePtr(0),
1004 std::size_t(
sizeof(CopyMemory)*launches) );
1006 const auto& tags = recv_copy_tags[mfi];
1007 for (
auto const & tag : tags)
1009 CopyMemory* cmem = TheFB.m_copyFromBuffer.getDevicePtr(idx++);
1012 auto const pfab = cmem->getSrc<value_type>();
1013 auto const dfab = cmem->getDst<value_type>();
1014 for (int n = 0; n < cmem->ncomp; ++n)
1016 dfab(i,j,k,n+(cmem->scomp)) = pfab(i,j,k,n);
1021 bool last_iter = mfi.LocalIndex() == (this->local_size()-1);
1022 cudaGraphExec_t graphExec = amrex::Gpu::Device::stopGraphRecording(last_iter);
1023 if (last_iter) {
const_cast<FB&
>(TheFB).m_copyFromBuffer.setGraph( graphExec ); }
1029 for (MFIter mfi(*
this); mfi.isValid(); ++mfi)
1031 auto dst_array = this->array(mfi);
1032 const auto & tags = recv_copy_tags[mfi];
1033 for (
auto const & tag : tags)
1035 const_cast<FB&
>(TheFB).m_copyFromBuffer.setParams(idx++, makeCopyMemory(
amrex::makeArray4((value_type*)(tag.p),
1044 TheFB.m_copyFromBuffer.executeGraph();
1050template <
typename BUF>
1055 int ncomp, std::uint64_t
id)
const
1060 auto kit = std::find_if(send_cctc.begin(), send_cctc.end(),
1062 if (kit == send_cctc.end()) {
1069 char* pbuf = send_data[0];
1070 const int N_snds = send_data.
size();
1071 for (
int j = 0; j < N_snds; ++j)
1073 if (send_size[j] > 0)
1075 char* dptr = send_data[j];
1076 auto const& cctc = *send_cctc[j];
1077 for (
auto const& tag : cctc)
1079 snd_copy_tags.emplace_back
1080 (TagType{.sfab = this->const_array(tag.srcIndex), .poff = dptr-pbuf, .bx = tag.sbox});
1081 dptr += (tag.sbox.numPts() * ncomp *
sizeof(BUF));
1085 return snd_copy_tags;
1089 std::tuple<std::uint64_t,std::size_t,int> key{id,
sizeof(BUF), ncomp};
1091 if (
auto it = m_send_copy_handler.find(key); it != m_send_copy_handler.end()) {
1092 tv = it->second.get();
1094 if (m_send_copy_handler.size() > 32) {
1099 m_send_copy_handler.clear();
1101 auto snd_copy_tags = get_tags();
1102 auto utv = std::make_unique<TagVector<TagType>>(snd_copy_tags);
1104 m_send_copy_handler[key] = std::move(utv);
1111template <
typename BUF>
1119 const int N_snds = send_data.
size();
1120 if (N_snds == 0) {
return; }
1124 auto* tv = src.template get_send_copy_tag_vector<BUF>
1125 (send_data, send_size, send_cctc, ncomp,
id);
1126 if (tv ==
nullptr) {
return; }
1128 char* pbuffer = send_data[0];
1130 detail::ParallelFor_doit(*tv,
1132 int icell,
int ncells,
int i,
int j,
int k, TagType
const& tag)
noexcept
1134 if (icell < ncells) {
1137 for (
int n = 0; n < ncomp; ++n) {
1138 dfab(i,j,k,n) = (BUF)tag.sfab(i,j,k,n+scomp);
1143 Gpu::streamSynchronize();
1147template <
typename BUF>
1152 int ncomp, std::uint64_t
id)
1157 auto kit = std::find_if(recv_cctc.begin(), recv_cctc.end(),
1159 if (kit == recv_cctc.end()) {
1166 char* pbuf = recv_data[0];
1167 const int N_rcvs = recv_cctc.
size();
1168 for (
int k = 0; k < N_rcvs; ++k)
1170 if (recv_size[k] > 0)
1172 char* dptr = recv_data[k];
1173 auto const& cctc = *recv_cctc[k];
1174 for (
auto const& tag : cctc)
1176 const int li = this->localindex(tag.dstIndex);
1177 recv_copy_tags.emplace_back
1178 (TagType{.dfab = this->atLocalIdx(li).array(), .poff = dptr-pbuf, .bx = tag.dbox});
1179 dptr += tag.dbox.numPts() * ncomp *
sizeof(BUF);
1183 return recv_copy_tags;
1187 std::tuple<std::uint64_t,std::size_t,int> key{id,
sizeof(BUF), ncomp};
1189 if (
auto it = m_recv_copy_handler.find(key); it != m_recv_copy_handler.end()) {
1190 tv = it->second.get();
1192 if (m_recv_copy_handler.size() > 32) {
1197 m_recv_copy_handler.clear();
1199 auto recv_copy_tags = get_tags();
1200 auto utv = std::make_unique<TagVector<TagType>>(recv_copy_tags);
1202 m_recv_copy_handler[key] = std::move(utv);
1209template <
typename BUF>
1215 CpOp op,
bool is_thread_safe, std::uint64_t
id,
1218 const int N_rcvs = recv_cctc.
size();
1219 if (N_rcvs == 0) {
return; }
1221 bool use_mask =
false;
1222 if (!is_thread_safe)
1225 (op == FabArrayBase::ADD && !amrex::HasAtomicAdd <value_type>::value))
1236 tags.reserve(N_rcvs);
1237 for (
int k = 0; k < N_rcvs; ++k) {
1238 if (recv_size[k] > 0) {
1239 char const* dptr = recv_data[k];
1240 auto const& cctc = *recv_cctc[k];
1241 for (
auto const& tag : cctc) {
1243 TagType{.dfab = dst.
array(tag.dstIndex), .dindex = tag.dstIndex,
1248 .offset =
Dim3{.
x = 0, .y = 0, .z = 0}});
1249 dptr += tag.dbox.numPts() * ncomp *
sizeof(BUF);
1254 detail::deterministic_fab_to_fab<value_type,BUF>
1255 (tags, 0, dcomp, ncomp, detail::CellAdd<value_type,BUF>{});
1263 auto* tv = dst.template get_recv_copy_tag_vector<BUF>
1264 (recv_data, recv_size, recv_cctc, ncomp,
id);
1265 if (tv ==
nullptr) {
return; }
1267 char* pbuffer = recv_data[0];
1269 if (op == FabArrayBase::COPY)
1271 detail::ParallelFor_doit(*tv,
1273 int icell,
int ncells,
int i,
int j,
int k, TagType
const& tag)
noexcept
1275 if (icell < ncells) {
1278 for (
int n = 0; n < ncomp; ++n) {
1279 tag.dfab(i,j,k,n+dcomp) = (
value_type)sfab(i,j,k,n);
1286 if (is_thread_safe) {
1287 detail::ParallelFor_doit(*tv,
1289 int icell,
int ncells,
int i,
int j,
int k, TagType
const& tag)
noexcept
1291 if (icell < ncells) {
1294 for (
int n = 0; n < ncomp; ++n) {
1295 tag.dfab(i,j,k,n+dcomp) += (
value_type)sfab(i,j,k,n);
1300 detail::unpack_recv_buffer_gpu_atomic_add<BUF, value_type>
1301 (pbuffer, *tv, dcomp, ncomp);
1304 Gpu::streamSynchronize();
1308 char* pbuffer = recv_data[0];
1312 recv_copy_tags.reserve(N_rcvs);
1319 for (
int k = 0; k < N_rcvs; ++k)
1321 if (recv_size[k] > 0)
1323 std::size_t
offset = recv_data[k]-recv_data[0];
1324 const char* dptr = pbuffer +
offset;
1325 auto const& cctc = *recv_cctc[k];
1326 for (
auto const& tag : cctc)
1329 recv_copy_tags.emplace_back(TagType{
1330 .dfab = dst.
atLocalIdx(li).array(), .dindex = tag.dstIndex,
1333 .offset =
Dim3{.
x = 0, .y = 0, .z = 0}
1335 dptr += tag.dbox.numPts() * ncomp *
sizeof(BUF);
1337 if (!maskfabs[li].isAllocated()) {
1338 maskfabs[li].resize(dst.
atLocalIdx(li).box());
1350 msk.
dfab(i,j,k) = 0;
1353 if (op == FabArrayBase::COPY)
1355 detail::fab_to_fab_atomic_cpy<value_type, BUF>(
1356 recv_copy_tags, 0, dcomp, ncomp, masks);
1360 detail::fab_to_fab_atomic_add<value_type, BUF>(
1361 recv_copy_tags, 0, dcomp, ncomp, masks);
1371template <
typename BUF>
1380 auto const N_snds =
static_cast<int>(send_data.
size());
1381 if (N_snds == 0) {
return; }
1384#pragma omp parallel for
1386 for (
int j = 0; j < N_snds; ++j)
1388 if (send_size[j] > 0)
1390 char* dptr = send_data[j];
1391 auto const& cctc = *send_cctc[j];
1392 for (
auto const& tag : cctc)
1394 const Box& bx = tag.sbox;
1395 auto const sfab = src.
array(tag.srcIndex);
1398 [=] (
int ii,
int jj,
int kk,
int n)
noexcept
1400 pfab(ii,jj,kk,n) =
static_cast<BUF
>(sfab(ii,jj,kk,n+scomp));
1402 dptr += (bx.
numPts() * ncomp *
sizeof(BUF));
1404 BL_ASSERT(dptr <= send_data[j] + send_size[j]);
1410template <
typename BUF>
1416 CpOp op,
bool is_thread_safe)
1420 auto const N_rcvs =
static_cast<int>(recv_cctc.
size());
1421 if (N_rcvs == 0) {
return; }
1426#pragma omp parallel for
1428 for (
int k = 0; k < N_rcvs; ++k)
1430 if (recv_size[k] > 0)
1432 const char* dptr = recv_data[k];
1433 auto const& cctc = *recv_cctc[k];
1434 for (
auto const& tag : cctc)
1436 const Box& bx = tag.dbox;
1437 FAB& dfab = dst[tag.dstIndex];
1438 if (op == FabArrayBase::COPY)
1440 dfab.template copyFromMem<RunOn::Host, BUF>(bx, dcomp, ncomp, dptr);
1444 dfab.template addFromMem<RunOn::Host, BUF>(tag.dbox, dcomp, ncomp, dptr);
1446 dptr += bx.
numPts() * ncomp *
sizeof(BUF);
1448 BL_ASSERT(dptr <= recv_data[k] + recv_size[k]);
1456 for (
int k = 0; k < N_rcvs; ++k)
1458 if (recv_size[k] > 0)
1460 const char* dptr = recv_data[k];
1461 auto const& cctc = *recv_cctc[k];
1462 for (
auto const& tag : cctc)
1464 recv_copy_tags[tag.dstIndex].push_back(
VoidCopyTag{.
p = dptr, .dbox = tag.dbox});
1465 dptr += tag.dbox.numPts() * ncomp *
sizeof(BUF);
1467 BL_ASSERT(dptr <= recv_data[k] + recv_size[k]);
1476 const auto& tags = recv_copy_tags[mfi];
1477 auto dfab = dst.
array(mfi);
1478 for (
auto const & tag : tags)
1481 if (op == FabArrayBase::COPY)
1484 [=] (
int i,
int j,
int k,
int n)
noexcept
1486 dfab(i,j,k,n+dcomp) = pfab(i,j,k,n);
1492 [=] (
int i,
int j,
int k,
int n)
noexcept
1494 dfab(i,j,k,n+dcomp) += pfab(i,j,k,n);
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_HOST_DEVICE_FOR_3D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:106
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1139
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:364
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:112
CopyComTag::CopyComTagsContainer CopyComTagsContainer
Definition AMReX_FabArrayBase.H:220
int localindex(int K) const noexcept
Return local index in the vector of FABs.
Definition AMReX_FabArrayBase.H:119
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:131
int local_size() const noexcept
Return the number of local FABs in the FabArray.
Definition AMReX_FabArrayBase.H:113
CpOp
parallel copy or add
Definition AMReX_FabArrayBase.H:394
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition AMReX_FabArrayBase.H:95
An Array of FortranArrayBox(FAB)-like Objects.
Definition AMReX_FabArray.H:344
typename std::conditional_t< IsBaseFab< FAB >::value, FAB, FABType >::value_type value_type
Definition AMReX_FabArray.H:355
void CMD_remote_setVal_gpu(value_type x, const CommMetaData &thecmd, int scomp, int ncomp)
Definition AMReX_FBI.H:684
void FB_local_add_cpu(const FB &TheFB, int scomp, int ncomp)
Definition AMReX_FBI.H:443
void FB_local_add_gpu(const FB &TheFB, int scomp, int ncomp, bool deterministic)
Definition AMReX_FBI.H:606
void FB_local_copy_gpu(const FB &TheFB, int scomp, int ncomp)
Definition AMReX_FBI.H:534
void CMD_local_setVal_gpu(value_type x, const CommMetaData &thecmd, int scomp, int ncomp)
Definition AMReX_FBI.H:654
Array4< typename FabArray< FAB >::value_type const > array(const MFIter &mfi) const noexcept
Definition AMReX_FabArray.H:561
void FB_local_copy_cpu(const FB &TheFB, int scomp, int ncomp)
Definition AMReX_FBI.H:384
TagVector< Array4CopyTag< value_type > > const * FB_get_local_copy_tag_vector(const FB &TheFB)
Definition AMReX_FBI.H:491
FAB & atLocalIdx(int L) noexcept
Return a reference to the FAB associated with local index L.
Definition AMReX_FabArray.H:532
a one-thingy-per-box distributed object
Definition AMReX_LayoutData.H:13
void define(const BoxArray &a_grids, const DistributionMapping &a_dm)
Definition AMReX_LayoutData.H:25
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:88
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:172
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
__host__ __device__ Dim3 length(Array4< T > const &a) noexcept
Return the spatial extents of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1373
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Return the inclusive lower bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1345
int MyProc() noexcept
Definition AMReX_ParallelDescriptor.H:128
__host__ __device__ AMREX_FORCE_INLINE T Exch(T *address, T val) noexcept
Definition AMReX_GpuAtomic.H:487
__host__ __device__ AMREX_FORCE_INLINE T CAS(T *const address, T compare, T const val) noexcept
Definition AMReX_GpuAtomic.H:513
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:283
__host__ __device__ AMREX_FORCE_INLINE T Add(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:200
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:228
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:105
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:291
Definition AMReX_Amr.cpp:50
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
__host__ __device__ Array4< T > makeArray4(T *p, Box const &bx, int ncomp) noexcept
Definition AMReX_BaseFab.H:92
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2867
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:2018
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
void LoopConcurrentOnCpu(Dim3 lo, Dim3 hi, F const &f) noexcept
Definition AMReX_Loop.H:388
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:241
const int[]
Definition AMReX_BLProfiler.cpp:1664
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:2028
BoxArray const & boxArray(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2862
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get I'th element of IntVectND<dim>
Definition AMReX_IntVect.H:1334
Definition AMReX_TagParallelFor.H:58
Definition AMReX_TagParallelFor.H:26
Definition AMReX_TagParallelFor.H:50
Array4< T > dfab
Definition AMReX_TagParallelFor.H:51
A multidimensional array accessor.
Definition AMReX_Array4.H:285
Definition AMReX_TagParallelFor.H:106
Definition AMReX_TagParallelFor.H:116
Definition AMReX_Dim3.H:13
int x
Definition AMReX_Dim3.H:13
Used by a bunch of routines when communicating via MPI.
Definition AMReX_FabArrayBase.H:195
Box sbox
Definition AMReX_FabArrayBase.H:197
int srcIndex
Definition AMReX_FabArrayBase.H:199
Box dbox
Definition AMReX_FabArrayBase.H:196
int dstIndex
Definition AMReX_FabArrayBase.H:198
FillBoundary.
Definition AMReX_FabArrayBase.H:488
IntVect offset
Definition AMReX_FBI.H:10
Box dbox
Definition AMReX_FBI.H:9
FAB const * sfab
Definition AMReX_FBI.H:8
Definition AMReX_TypeTraits.H:51
Definition AMReX_TypeTraits.H:61
Definition AMReX_TypeTraits.H:277
Definition AMReX_TagParallelFor.H:156
Definition AMReX_FBI.H:13
Box dbox
Definition AMReX_FBI.H:15
char const * p
Definition AMReX_FBI.H:14