Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_PCI.H
Go to the documentation of this file.
1#ifndef AMREX_PCI_H_
2#define AMREX_PCI_H_
3
4namespace amrex {
5
6template <class FAB>
7void
8FabArray<FAB>::PC_local_cpu (const CPC& thecpc, FabArray<FAB> const& src,
9 int scomp, int dcomp, int ncomp, CpOp op)
10{
11 auto const N_locs = static_cast<int>(thecpc.m_LocTags->size());
12 if (N_locs == 0) { return; }
13 bool is_thread_safe = thecpc.m_threadsafe_loc;
14
15 if (is_thread_safe)
16 {
17#ifdef AMREX_USE_OMP
18#pragma omp parallel for
19#endif
20 for (int i = 0; i < N_locs; ++i)
21 {
22 const CopyComTag& tag = (*thecpc.m_LocTags)[i];
23 if (this != &src || tag.dstIndex != tag.srcIndex || tag.sbox != tag.dbox) {
24 // avoid self copy or plus
25 const FAB* sfab = &(src[tag.srcIndex]);
26 FAB* dfab = &(get(tag.dstIndex));
27 if (op == FabArrayBase::COPY)
28 {
29 dfab->template copy<RunOn::Host>(*sfab, tag.sbox, scomp, tag.dbox, dcomp, ncomp);
30 }
31 else
32 {
33 dfab->template plus<RunOn::Host>(*sfab, tag.sbox, tag.dbox, scomp, dcomp, ncomp);
34 }
35 }
36 }
37 }
38 else
39 {
41 for (int i = 0; i < N_locs; ++i)
42 {
43 const CopyComTag& tag = (*thecpc.m_LocTags)[i];
44 if (this != &src || tag.dstIndex != tag.srcIndex || tag.sbox != tag.dbox) {
45 loc_copy_tags[tag.dstIndex].push_back
46 ({src.fabPtr(tag.srcIndex), tag.dbox, tag.sbox.smallEnd()-tag.dbox.smallEnd()});
47 }
48 }
49
50#ifdef AMREX_USE_OMP
51#pragma omp parallel
52#endif
53 for (MFIter mfi(*this); mfi.isValid(); ++mfi)
54 {
55 const auto& tags = loc_copy_tags[mfi];
56 auto dfab = this->array(mfi);
57 if (op == FabArrayBase::COPY)
58 {
59 for (auto const & tag : tags)
60 {
61 auto const sfab = tag.sfab->array();
62 Dim3 offset = tag.offset.dim3();
63 amrex::LoopConcurrentOnCpu (tag.dbox, ncomp,
64 [=] (int i, int j, int k, int n) noexcept
65 {
66 dfab(i,j,k,dcomp+n) = sfab(i+offset.x,j+offset.y,k+offset.z,scomp+n);
67 });
68 }
69 }
70 else
71 {
72 for (auto const & tag : tags)
73 {
74 auto const sfab = tag.sfab->array();
75 Dim3 offset = tag.offset.dim3();
76 amrex::LoopConcurrentOnCpu (tag.dbox, ncomp,
77 [=] (int i, int j, int k, int n) noexcept
78 {
79 dfab(i,j,k,dcomp+n) += sfab(i+offset.x,j+offset.y,k+offset.z,scomp+n);
80 });
81 }
82 }
83 }
84 }
85}
86
87#ifdef AMREX_USE_GPU
88template <class FAB>
89void
91 int scomp, int dcomp, int ncomp, CpOp op,
92 bool deterministic)
93{
94 auto const N_locs = static_cast<int>(thecpc.m_LocTags->size());
95 if (N_locs == 0) { return; }
96 bool is_thread_safe = thecpc.m_threadsafe_loc;
97
98 // Deterministic handling is only implemented for ADD.
99 if (deterministic) { AMREX_ALWAYS_ASSERT(op == FabArrayBase::ADD); }
100
101 using TagType = Array4CopyTag<value_type>;
102 Vector<TagType> loc_copy_tags;
103 loc_copy_tags.reserve(N_locs);
104
105 Vector<BaseFab<int> > maskfabs;
106 Vector<Array4Tag<int> > masks_unique;
107 Vector<Array4Tag<int> > masks;
108 if (!is_thread_safe)
109 {
111 (op == FabArrayBase::ADD && !amrex::HasAtomicAdd <value_type>::value &&
112 !deterministic))
113 {
114 maskfabs.resize(this->local_size());
115 masks_unique.reserve(this->local_size());
116 masks.reserve(N_locs);
117 }
118 }
119
120 for (int i = 0; i < N_locs; ++i)
121 {
122 const CopyComTag& tag = (*thecpc.m_LocTags)[i];
123 if (this != &src || tag.dstIndex != tag.srcIndex || tag.sbox != tag.dbox) {
124 int li = this->localindex(tag.dstIndex);
125 loc_copy_tags.push_back(
126 TagType{.dfab = this->atLocalIdx(li).array(),
127 .dindex = tag.dstIndex,
128 .sfab = src.fabPtr(tag.srcIndex)->const_array(),
129 .dbox = tag.dbox,
130 .offset = (tag.sbox.smallEnd()-tag.dbox.smallEnd()).dim3()});
131
132 if (maskfabs.size() > 0) {
133 if (!maskfabs[li].isAllocated()) {
134 maskfabs[li].resize(this->atLocalIdx(li).box());
135 masks_unique.emplace_back(Array4Tag<int>{.dfab = maskfabs[li].array()});
136 }
137 masks.emplace_back(Array4Tag<int>{.dfab = maskfabs[li].array()});
138 }
139 }
140 }
141
142 if (maskfabs.size() > 0) {
143 amrex::ParallelFor(masks_unique,
144 [=] AMREX_GPU_DEVICE (int i, int j, int k, Array4Tag<int> const& msk) noexcept
145 {
146 msk.dfab(i,j,k) = 0;
147 });
148 }
149
150 if (op == FabArrayBase::COPY)
151 {
152 if (is_thread_safe) {
153 detail::fab_to_fab<value_type, value_type>(loc_copy_tags, scomp,
154 dcomp, ncomp, detail::CellStore<value_type, value_type>());
155 } else {
156 detail::fab_to_fab_atomic_cpy<value_type, value_type>(
157 loc_copy_tags, scomp, dcomp, ncomp, masks);
158 }
159 }
160 else
161 {
162 if (is_thread_safe) {
163 // Non-overlapping destinations: a plain add is already bitwise
164 // reproducible, so no need for the heavier deterministic kernel.
165 detail::fab_to_fab<value_type, value_type>(loc_copy_tags, scomp,
166 dcomp, ncomp, detail::CellAdd<value_type, value_type>());
167 } else if (deterministic) {
168 // Use deterministic_fab_to_fab for ADD operations to ensure bitwise
169 // reproducibility. The atomic add operations (fab_to_fab_atomic_add) are
170 // non-deterministic because the order of atomic additions is not guaranteed.
171 detail::deterministic_fab_to_fab<value_type, value_type>(
172 loc_copy_tags, scomp, dcomp, ncomp, detail::CellAdd<value_type, value_type>());
173 } else {
174 detail::fab_to_fab_atomic_add<value_type, value_type>(
175 loc_copy_tags, scomp, dcomp, ncomp, masks);
176 }
177 }
178}
179#endif
180
181}
182
183#endif
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1129
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:124
CpOp
parallel copy or add
Definition AMReX_FabArrayBase.H:393
@ ADD
Definition AMReX_FabArrayBase.H:393
@ COPY
Definition AMReX_FabArrayBase.H:393
An Array of FortranArrayBox(FAB)-like Objects.
Definition AMReX_FabArray.H:344
void PC_local_cpu(const CPC &thecpc, FabArray< FAB > const &src, int scomp, int dcomp, int ncomp, CpOp op)
Definition AMReX_PCI.H:8
void PC_local_gpu(const CPC &thecpc, FabArray< FAB > const &src, int scomp, int dcomp, int ncomp, CpOp op, bool deterministic)
Definition AMReX_PCI.H:90
FAB * fabPtr(const MFIter &mfi) noexcept
Return pointer to FAB.
Definition AMReX_FabArray.H:1809
a one-thingy-per-box distributed object
Definition AMReX_LayoutData.H:13
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
Definition AMReX_Amr.cpp:50
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2867
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
void LoopConcurrentOnCpu(Dim3 lo, Dim3 hi, F const &f) noexcept
Definition AMReX_Loop.H:388
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:26
Definition AMReX_TagParallelFor.H:50
Array4< T > dfab
Definition AMReX_TagParallelFor.H:51
A simple struct holding 3 int values for a 3D index.
Definition AMReX_Dim3.H:24
parallel copy or add
Definition AMReX_FabArrayBase.H:537
bool m_threadsafe_loc
Definition AMReX_FabArrayBase.H:473
std::unique_ptr< CopyComTagsContainer > m_LocTags
Definition AMReX_FabArrayBase.H:475
Used by a bunch of routines when communicating via MPI.
Definition AMReX_FabArrayBase.H:194
Box sbox
Definition AMReX_FabArrayBase.H:196
int srcIndex
Definition AMReX_FabArrayBase.H:198
Box dbox
Definition AMReX_FabArrayBase.H:195
int dstIndex
Definition AMReX_FabArrayBase.H:197
Definition AMReX_TypeTraits.H:277