Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_MultiFabUtil.H
Go to the documentation of this file.
1#ifndef AMREX_MultiFabUtil_H_
2#define AMREX_MultiFabUtil_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Concepts.H>
6#include <AMReX_MultiFab.H>
7#include <AMReX_iMultiFab.H>
8#include <AMReX_LayoutData.H>
9#include <AMReX_MFIter.H>
10#include <AMReX_Array.H>
11#include <AMReX_Vector.H>
12#include <AMReX_MultiFabUtil_C.H>
13
14#include <AMReX_MultiFabUtilI.H>
15
16namespace amrex
17{
19 void average_node_to_cellcenter (MultiFab& cc, int dcomp,
20 const MultiFab& nd, int scomp,
21 int ncomp, int ngrow = 0);
22 void average_node_to_cellcenter (MultiFab& cc, int dcomp,
23 const MultiFab& nd, int scomp,
24 int ncomp, IntVect const& ng_vect);
25
32 void average_edge_to_cellcenter (MultiFab& cc, int dcomp,
33 const Vector<const MultiFab*>& edge,
34 int ngrow = 0);
35 void average_edge_to_cellcenter (MultiFab& cc, int dcomp,
36 const Vector<const MultiFab*>& edge,
37 IntVect const& ng_vect);
38
45 void average_face_to_cellcenter (MultiFab& cc, int dcomp,
47 int ngrow = 0);
48 void average_face_to_cellcenter (MultiFab& cc, int dcomp,
50 IntVect const& ng_vect);
51
53 template <FabArrayType CMF, FabArrayType FMF>
54 void average_face_to_cellcenter (CMF& cc, int dcomp,
56 int ngrow = 0);
57
58 template <FabArrayType CMF, FabArrayType FMF>
59 void average_face_to_cellcenter (CMF& cc, int dcomp,
61 IntVect const& ng_vect);
62
66 const Geometry& geom);
70 const Geometry& geom);
80 const MultiFab& cc,
81 const Geometry& geom,
82 int ncomp = 1,
83 bool use_harmonic_averaging = false,
84 int ngrow = 0);
87 const MultiFab& cc,
88 const Geometry& geom,
89 int ncomp = 1,
90 bool use_harmonic_averaging = false,
91 int ngrow = 0);
92
94 const MultiFab& cc,
95 const Geometry& geom,
96 int ncomp,
97 bool use_harmonic_averaging,
98 const Array<IntVect, AMREX_SPACEDIM>& ng_vects);
99
101 template <FabArrayType MF>
102 void average_down_faces (const Vector<const MF*>& fine,
103 const Vector<MF*>& crse,
104 const IntVect& ratio,
105 int ngcrse = 0);
107 template <FabArrayType MF>
108 void average_down_faces (const Vector<const MF*>& fine,
109 const Vector<MF*>& crse,
110 int ratio,
111 int ngcrse = 0);
113 template <FabArrayType MF>
115 const Array<MF*,AMREX_SPACEDIM>& crse,
116 const IntVect& ratio,
117 int ngcrse = 0);
119 template <FabArrayType MF>
121 const Array<MF*,AMREX_SPACEDIM>& crse,
122 int ratio,
123 int ngcrse = 0);
130 template <BaseFabType FAB>
131 void average_down_faces (const FabArray<FAB>& fine, FabArray<FAB>& crse,
132 const IntVect& ratio, int ngcrse=0);
133
134 // This version takes periodicity into account.
135 template <FabArrayType MF>
137 const Array<MF*,AMREX_SPACEDIM>& crse,
138 const IntVect& ratio, const Geometry& crse_geom);
139 // This version takes periodicity into account.
140 template <BaseFabType FAB>
141 void average_down_faces (const FabArray<FAB>& fine, FabArray<FAB>& crse,
142 const IntVect& ratio, const Geometry& crse_geom);
143
146 const Vector<MultiFab*>& crse,
147 const IntVect& ratio,
148 int ngcrse = 0);
151 const IntVect& ratio,
152 int ngcrse = 0);
156 void average_down_edges (const MultiFab& fine, MultiFab& crse,
157 const IntVect& ratio, int ngcrse=0);
158
160 template <BaseFabType FAB>
161 void average_down_nodal (const FabArray<FAB>& S_fine,
162 FabArray<FAB>& S_crse,
163 const IntVect& ratio,
164 int ngcrse = 0,
165 bool mfiter_is_definitely_safe=false);
166
173 void average_down (const MultiFab& S_fine, MultiFab& S_crse,
174 const Geometry& fgeom, const Geometry& cgeom,
175 int scomp, int ncomp, const IntVect& ratio);
176 void average_down (const MultiFab& S_fine, MultiFab& S_crse,
177 const Geometry& fgeom, const Geometry& cgeom,
178 int scomp, int ncomp, int rr);
179
183 template <BaseFabType FAB>
184 void average_down (const FabArray<FAB>& S_fine, FabArray<FAB>& S_crse,
185 int scomp, int ncomp, const IntVect& ratio);
186 template <BaseFabType FAB>
187 void average_down (const FabArray<FAB>& S_fine, FabArray<FAB>& S_crse,
188 int scomp, int ncomp, int rr);
189
192 void sum_fine_to_coarse (const MultiFab& S_Fine, MultiFab& S_crse,
193 int scomp, int ncomp,
194 const IntVect& ratio,
195 const Geometry& cgeom, const Geometry& fgeom);
196
198 void print_state (const MultiFab& mf, const IntVect& cell, int n=-1,
199 const IntVect& ng = IntVect::TheZeroVector());
200
202 void writeFabs (const MultiFab& mf, const std::string& name);
203 void writeFabs (const MultiFab& mf, int comp, int ncomp, const std::string& name);
204
207 std::unique_ptr<MultiFab> get_slice_data(int dir, Real coord,
208 const MultiFab& cc,
209 const Geometry& geom, int start_comp, int ncomp,
210 bool interpolate=false,
211 RealBox const& bnd_rbx = RealBox());
212
220 template <FabArrayType MF>
221 Vector<typename MF::value_type> get_cell_data (MF const& mf, IntVect const& cell);
222
229 template <FabArrayType MF>
230 MF get_line_data (MF const& mf, int dir, IntVect const& cell, Box const& bnd_bx = Box());
231
235 template <BaseFabType FAB>
236 iMultiFab makeFineMask (const FabArray<FAB>& cmf, const BoxArray& fba, const IntVect& ratio,
237 int crse_value = 0, int fine_value = 1,
238 MFInfo const& info = MFInfo());
239 iMultiFab makeFineMask (const BoxArray& cba, const DistributionMapping& cdm,
240 const BoxArray& fba, const IntVect& ratio,
241 int crse_value = 0, int fine_value = 1,
242 MFInfo const& info = MFInfo());
243 template <BaseFabType FAB>
244 iMultiFab makeFineMask (const FabArray<FAB>& cmf, const BoxArray& fba, const IntVect& ratio,
245 Periodicity const& period, int crse_value, int fine_value,
246 MFInfo const& info = MFInfo());
247 iMultiFab makeFineMask (const BoxArray& cba, const DistributionMapping& cdm,
248 const IntVect& cnghost, const BoxArray& fba, const IntVect& ratio,
249 Periodicity const& period, int crse_value, int fine_value,
250 MFInfo const& info = MFInfo());
251 template <BaseFabType FAB>
252 iMultiFab makeFineMask (const FabArray<FAB>& cmf, const FabArray<FAB>& fmf,
253 const IntVect& cnghost, const IntVect& ratio,
254 Periodicity const& period, int crse_value, int fine_value,
255 MFInfo const& info = MFInfo());
256 template <BaseFabType FAB>
257 iMultiFab makeFineMask (const FabArray<FAB>& cmf, const FabArray<FAB>& fmf,
258 const IntVect& cnghost, const IntVect& ratio,
259 Periodicity const& period, int crse_value, int fine_value,
260 LayoutData<int>& has_cf,
261 MFInfo const& info = MFInfo());
262
263 MultiFab makeFineMask (const BoxArray& cba, const DistributionMapping& cdm,
264 const BoxArray& fba, const IntVect& ratio,
265 Real crse_value, Real fine_value,
266 MFInfo const& info = MFInfo());
267
271 const Geometry& geom);
272
275 const Geometry& geom);
276
278 MultiFab ToMultiFab (const iMultiFab& imf);
281
283 MultiFab periodicShift (MultiFab const& mf, IntVect const& offset,
284 Periodicity const& period);
285
287 template <typename T, typename U>
288 T cast (U const& mf_in)
289 {
290 T mf_out(mf_in.boxArray(), mf_in.DistributionMap(), mf_in.nComp(), mf_in.nGrowVect());
291
292#ifdef AMREX_USE_OMP
293#pragma omp parallel if (Gpu::notInLaunchRegion())
294#endif
295 for (MFIter mfi(mf_in); mfi.isValid(); ++mfi)
296 {
297 const Long n = mfi.fabbox().numPts() * mf_in.nComp();
298 auto * pdst = mf_out[mfi].dataPtr();
299 auto const* psrc = mf_in [mfi].dataPtr();
301 {
302 pdst[i] = static_cast<typename T::value_type>(psrc[i]); // NOLINT(bugprone-signed-char-misuse)
303 });
304 }
305 return mf_out;
306 }
307
308
368 template <typename Op, typename T, BaseFabType FAB, typename F>
369#ifndef AMREX_USE_CUDA
370 requires (IsCallableR<T,F,int,int,int,int>::value)
371#endif
372 BaseFab<T>
373 ReduceToPlane (int direction, Box const& domain, FabArray<FAB> const& mf, F const& f);
374
419 template <typename Op, MultiFabLike FA, typename F>
420#ifndef AMREX_USE_CUDA
421 requires (IsCallableR<typename FA::value_type,
422 F,int,int,int,int>::value)
423#endif
424 FA ReduceToPlaneMF (int direction, Box const& domain, FA const& mf, F const& f);
425
480 template <typename Op, MultiFabLike FA, typename F>
481#ifndef AMREX_USE_CUDA
482 requires (IsCallableR<typename FA::value_type,
483 F,int,int,int,int>::value)
484#endif
485 std::pair<FA,FA>
486 ReduceToPlaneMF2 (int direction, Box const& domain, FA const& mf, F const& f,
487 IntVect const& nghost = IntVect(0),
488 Periodicity const& period = Periodicity::NonPeriodic());
489
517 template <typename Op, MultiFabLike FA, typename F>
518#ifndef AMREX_USE_CUDA
519 requires (IsCallableR<typename FA::value_type,
520 F,int,int,int,int>::value)
521#endif
522 std::pair<FA,FA>
523 ReduceToPlaneMF2Patchy (int direction, Box const& domain, FA const& mf,
524 IntVect const& plane_max_grid_size, F const& f);
525
543 Gpu::HostVector<Real> sumToLine (MultiFab const& mf, int icomp, int ncomp,
544 Box const& domain, int direction, bool local = false);
545
554 Real volumeWeightedSum (Vector<MultiFab const*> const& mf, int icomp,
555 Vector<Geometry> const& geom,
556 Vector<IntVect> const& ratio,
557 bool local = false);
558
572 void FourthOrderInterpFromFineToCoarse (MultiFab& cmf, int scomp, int ncomp,
573 MultiFab const& fmf,
574 IntVect const& ratio);
575
586 void FillRandom (MultiFab& mf, int scomp, int ncomp);
587
599 void FillRandomNormal (MultiFab& mf, int scomp, int ncomp, Real mean, Real stddev);
600
612 [[nodiscard]] Vector<MultiFab> convexify (Vector<MultiFab const*> const& mf,
613 Vector<IntVect> const& refinement_ratio);
614}
615
616namespace amrex {
617
618template <BaseFabType FAB>
620makeFineMask (const FabArray<FAB>& cmf, const BoxArray& fba, const IntVect& ratio,
621 int crse_value, int fine_value, MFInfo const& info)
622{
623 return makeFineMask(cmf.boxArray(), cmf.DistributionMap(), cmf.nGrowVect(),
624 fba, ratio, Periodicity::NonPeriodic(), crse_value, fine_value,
625 info);
626}
627
628template <BaseFabType FAB>
629iMultiFab
630makeFineMask (const FabArray<FAB>& cmf, const BoxArray& fba, const IntVect& ratio,
631 Periodicity const& period, int crse_value, int fine_value,
632 MFInfo const& info)
633{
634 return makeFineMask(cmf.boxArray(), cmf.DistributionMap(), cmf.nGrowVect(),
635 fba, ratio, period, crse_value, fine_value, info);
636}
637
638template <BaseFabType FAB>
639iMultiFab
640makeFineMask (const FabArray<FAB>& cmf, const FabArray<FAB>& fmf,
641 const IntVect& cnghost, const IntVect& ratio,
642 Periodicity const& period, int crse_value, int fine_value,
643 MFInfo const& info)
644{
645 iMultiFab mask(cmf.boxArray(), cmf.DistributionMap(), 1, cnghost, info);
646 mask.setVal(crse_value);
647
648 iMultiFab foo(amrex::coarsen(fmf.boxArray(),ratio), fmf.DistributionMap(),
649 1, 0, MFInfo().SetAlloc(false));
650 const FabArrayBase::CPC& cpc = mask.getCPC(cnghost,foo,IntVect::TheZeroVector(),period);
651 mask.setVal(fine_value, cpc, 0, 1);
652
653 return mask;
654}
655
656template <BaseFabType FAB>
657iMultiFab
658makeFineMask (const FabArray<FAB>& cmf, const FabArray<FAB>& fmf,
659 const IntVect& cnghost, const IntVect& ratio,
660 Periodicity const& period, int crse_value, int fine_value,
661 LayoutData<int>& has_cf, MFInfo const& info)
662{
663 iMultiFab mask(cmf.boxArray(), cmf.DistributionMap(), 1, cnghost, info);
664 mask.setVal(crse_value);
665
666 iMultiFab foo(amrex::coarsen(fmf.boxArray(),ratio), fmf.DistributionMap(),
667 1, 0, MFInfo().SetAlloc(false));
668 const FabArrayBase::CPC& cpc = mask.getCPC(cnghost,foo,IntVect::TheZeroVector(),period);
669 mask.setVal(fine_value, cpc, 0, 1);
670
671 has_cf = mask.RecvLayoutMask(cpc);
672
673 return mask;
674}
675
676
678template <BaseFabType FAB>
679void average_down_nodal (const FabArray<FAB>& fine, FabArray<FAB>& crse,
680 const IntVect& ratio, int ngcrse, bool mfiter_is_definitely_safe)
681{
682 AMREX_ASSERT(fine.is_nodal());
683 AMREX_ASSERT(crse.is_nodal());
684 AMREX_ASSERT(crse.nComp() == fine.nComp());
685
686 int ncomp = crse.nComp();
687 using value_type = typename FAB::value_type;
688
689 if (mfiter_is_definitely_safe || isMFIterSafe(fine, crse))
690 {
691#ifdef AMREX_USE_OMP
692#pragma omp parallel if (Gpu::notInLaunchRegion())
693#endif
694 for (MFIter mfi(crse,TilingIfNotGPU()); mfi.isValid(); ++mfi)
695 {
696 const Box& bx = mfi.growntilebox(ngcrse);
697 Array4<value_type> const& crsearr = crse.array(mfi);
698 Array4<value_type const> const& finearr = fine.const_array(mfi);
699
701 {
702 amrex_avgdown_nodes(tbx,crsearr,finearr,0,0,ncomp,ratio);
703 });
704 }
705 }
706 else
707 {
708 FabArray<FAB> ctmp(amrex::coarsen(fine.boxArray(),ratio), fine.DistributionMap(),
709 ncomp, ngcrse, MFInfo().SetArena(The_Async_Arena()));
710 average_down_nodal(fine, ctmp, ratio, ngcrse);
711 crse.ParallelCopy(ctmp,0,0,ncomp,ngcrse,ngcrse);
712 }
713}
714
715// *************************************************************************************************************
716
717// Average fine cell-based MultiFab onto crse cell-centered MultiFab.
718// We do NOT assume that the coarse layout is a coarsened version of the fine layout.
719// This version does NOT use volume-weighting
720template <BaseFabType FAB>
721void average_down (const FabArray<FAB>& S_fine, FabArray<FAB>& S_crse, int scomp, int ncomp, int rr)
723 average_down(S_fine,S_crse,scomp,ncomp,rr*IntVect::TheUnitVector());
724}
725
726template <BaseFabType FAB>
727void average_down (const FabArray<FAB>& S_fine, FabArray<FAB>& S_crse,
728 int scomp, int ncomp, const IntVect& ratio)
729{
730 BL_PROFILE("amrex::average_down");
731 AMREX_ASSERT(S_crse.nComp() == S_fine.nComp());
732 AMREX_ASSERT((S_crse.is_cell_centered() && S_fine.is_cell_centered()) ||
733 (S_crse.is_nodal() && S_fine.is_nodal()));
734
735 using value_type = typename FAB::value_type;
736
737 bool is_cell_centered = S_crse.is_cell_centered();
738
739 //
740 // Coarsen() the fine stuff on processors owning the fine data.
741 //
742 BoxArray crse_S_fine_BA = S_fine.boxArray(); crse_S_fine_BA.coarsen(ratio);
743
744 if (crse_S_fine_BA == S_crse.boxArray() && S_fine.DistributionMap() == S_crse.DistributionMap())
745 {
746#ifdef AMREX_USE_GPU
747 if (Gpu::inLaunchRegion() && S_crse.isFusingCandidate()) {
748 auto const& crsema = S_crse.arrays();
749 auto const& finema = S_fine.const_arrays();
750 if (is_cell_centered) {
751 ParallelFor(S_crse, IntVect(0), ncomp,
752 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
753 {
754 amrex_avgdown(i,j,k,n,crsema[box_no],finema[box_no],scomp,scomp,ratio);
755 });
756 } else {
757 ParallelFor(S_crse, IntVect(0), ncomp,
758 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
759 {
760 amrex_avgdown_nodes(i,j,k,n,crsema[box_no],finema[box_no],scomp,scomp,ratio);
761 });
762 }
763 if (!Gpu::inNoSyncRegion()) {
765 }
766 } else
767#endif
768 {
769#ifdef AMREX_USE_OMP
770#pragma omp parallel if (Gpu::notInLaunchRegion())
771#endif
772 for (MFIter mfi(S_crse,TilingIfNotGPU()); mfi.isValid(); ++mfi)
773 {
774 // NOTE: The tilebox is defined at the coarse level.
775 const Box& bx = mfi.tilebox();
776 Array4<value_type> const& crsearr = S_crse.array(mfi);
777 Array4<value_type const> const& finearr = S_fine.const_array(mfi);
778
779 if (is_cell_centered) {
780 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
781 {
782 amrex_avgdown(i,j,k,n,crsearr,finearr,scomp,scomp,ratio);
783 });
784 } else {
785 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
786 {
787 amrex_avgdown_nodes(i,j,k,n,crsearr,finearr,scomp,scomp,ratio);
788 });
789 }
790 }
791 }
792 }
793 else
794 {
795 FabArray<FAB> crse_S_fine(crse_S_fine_BA, S_fine.DistributionMap(), ncomp, 0,
797
798#ifdef AMREX_USE_GPU
799 if (Gpu::inLaunchRegion() && crse_S_fine.isFusingCandidate()) {
800 auto const& crsema = crse_S_fine.arrays();
801 auto const& finema = S_fine.const_arrays();
802 if (is_cell_centered) {
803 ParallelFor(crse_S_fine, IntVect(0), ncomp,
804 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
805 {
806 amrex_avgdown(i,j,k,n,crsema[box_no],finema[box_no],0,scomp,ratio);
807 });
808 } else {
809 ParallelFor(crse_S_fine, IntVect(0), ncomp,
810 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
811 {
812 amrex_avgdown_nodes(i,j,k,n,crsema[box_no],finema[box_no],0,scomp,ratio);
813 });
814 }
815 if (!Gpu::inNoSyncRegion()) {
817 }
818 } else
819#endif
820 {
821#ifdef AMREX_USE_OMP
822#pragma omp parallel if (Gpu::notInLaunchRegion())
823#endif
824 for (MFIter mfi(crse_S_fine,TilingIfNotGPU()); mfi.isValid(); ++mfi)
825 {
826 // NOTE: The tilebox is defined at the coarse level.
827 const Box& bx = mfi.tilebox();
828 Array4<value_type> const& crsearr = crse_S_fine.array(mfi);
829 Array4<value_type const> const& finearr = S_fine.const_array(mfi);
830
831 // NOTE: We copy from component scomp of the fine fab into component 0 of the crse fab
832 // because the crse fab is a temporary which was made starting at comp 0, it is
833 // not part of the actual crse multifab which came in.
834
835 if (is_cell_centered) {
836 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
837 {
838 amrex_avgdown(i,j,k,n,crsearr,finearr,0,scomp,ratio);
839 });
840 } else {
841 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
842 {
843 amrex_avgdown_nodes(i,j,k,n,crsearr,finearr,0,scomp,ratio);
844 });
845 }
846 }
847 }
848
849 S_crse.ParallelCopy(crse_S_fine,0,scomp,ncomp);
850 }
851}
852
853
854
855
856
864template <typename F>
865Real
866NormHelper (const MultiFab& x, int xcomp,
867 const MultiFab& y, int ycomp,
868 F const& f,
869 int numcomp, IntVect nghost, bool local)
870{
871 BL_ASSERT(x.boxArray() == y.boxArray());
872 BL_ASSERT(x.DistributionMap() == y.DistributionMap());
873 BL_ASSERT(x.nGrowVect().allGE(nghost) && y.nGrowVect().allGE(nghost));
874
875 Real sm = Real(0.0);
876#ifdef AMREX_USE_GPU
877 if (Gpu::inLaunchRegion()) {
878 auto const& xma = x.const_arrays();
879 auto const& yma = y.const_arrays();
881 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) noexcept -> GpuTuple<Real>
882 {
883 Real t = Real(0.0);
884 auto const& xfab = xma[box_no];
885 auto const& yfab = yma[box_no];
886 for (int n = 0; n < numcomp; ++n) {
887 t += f(xfab(i,j,k,xcomp+n) , yfab(i,j,k,ycomp+n));
888 }
889 return t;
890 });
891 } else
892#endif
893 {
894#ifdef AMREX_USE_OMP
895#pragma omp parallel if (!system::regtest_reduction) reduction(+:sm)
896#endif
897 for (MFIter mfi(x,true); mfi.isValid(); ++mfi)
898 {
899 Box const& bx = mfi.growntilebox(nghost);
900 Array4<Real const> const& xfab = x.const_array(mfi);
901 Array4<Real const> const& yfab = y.const_array(mfi);
902 AMREX_LOOP_4D(bx, numcomp, i, j, k, n,
903 {
904 sm += f(xfab(i,j,k,xcomp+n) , yfab(i,j,k,ycomp+n));
905 });
906 }
907 }
908
909 if (!local) {
911 }
912
913 return sm;
914}
915
916
924template <typename MMF, typename Pred, typename F>
925Real
926NormHelper (const MMF& mask,
927 const MultiFab& x, int xcomp,
928 const MultiFab& y, int ycomp,
929 Pred const& pf,
930 F const& f,
931 int numcomp, IntVect nghost, bool local)
932{
933 BL_ASSERT(x.boxArray() == y.boxArray());
934 BL_ASSERT(x.boxArray() == mask.boxArray());
935 BL_ASSERT(x.DistributionMap() == y.DistributionMap());
936 BL_ASSERT(x.DistributionMap() == mask.DistributionMap());
937 BL_ASSERT(x.nGrowVect().allGE(nghost) && y.nGrowVect().allGE(nghost));
938 BL_ASSERT(mask.nGrowVect().allGE(nghost));
939
940 Real sm = Real(0.0);
941#ifdef AMREX_USE_GPU
942 if (Gpu::inLaunchRegion()) {
943 auto const& xma = x.const_arrays();
944 auto const& yma = y.const_arrays();
945 auto const& mma = mask.const_arrays();
947 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) noexcept -> GpuTuple<Real>
948 {
949 Real t = Real(0.0);
950 if (pf(mma[box_no](i,j,k))) {
951 auto const& xfab = xma[box_no];
952 auto const& yfab = yma[box_no];
953 for (int n = 0; n < numcomp; ++n) {
954 t += f(xfab(i,j,k,xcomp+n) , yfab(i,j,k,ycomp+n));
955 }
956 }
957 return t;
958 });
959 } else
960#endif
961 {
962#ifdef AMREX_USE_OMP
963#pragma omp parallel if (!system::regtest_reduction) reduction(+:sm)
964#endif
965 for (MFIter mfi(x,true); mfi.isValid(); ++mfi)
966 {
967 Box const& bx = mfi.growntilebox(nghost);
968 Array4<Real const> const& xfab = x.const_array(mfi);
969 Array4<Real const> const& yfab = y.const_array(mfi);
970 auto const& mfab = mask.const_array(mfi);
971 AMREX_LOOP_4D(bx, numcomp, i, j, k, n,
972 {
973 if (pf(mfab(i,j,k))) {
974 sm += f(xfab(i,j,k,xcomp+n) , yfab(i,j,k,ycomp+n));
975 }
976 });
977 }
978 }
979
980 if (!local) {
982 }
983
984 return sm;
985}
986
987template <FabArrayType CMF, FabArrayType FMF>
988void average_face_to_cellcenter (CMF& cc, int dcomp,
990 int ngrow)
991{
992 IntVect ng_vect(ngrow);
993 average_face_to_cellcenter(cc, dcomp, fc, ng_vect);
994}
995
996template <FabArrayType CMF, FabArrayType FMF>
997void average_face_to_cellcenter (CMF& cc, int dcomp,
999 IntVect const& ng_vect)
1000{
1001 AMREX_ASSERT(cc.nComp() >= dcomp + AMREX_SPACEDIM);
1002 AMREX_ASSERT(fc[0]->nComp() == 1);
1003
1004#ifdef AMREX_USE_GPU
1005 if (Gpu::inLaunchRegion() && cc.isFusingCandidate()) {
1006 auto const& ccma = cc.arrays();
1007 AMREX_D_TERM(auto const& fxma = fc[0]->const_arrays();,
1008 auto const& fyma = fc[1]->const_arrays();,
1009 auto const& fzma = fc[2]->const_arrays(););
1010 ParallelFor(cc, ng_vect,
1011 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k) noexcept
1012 {
1013#if (AMREX_SPACEDIM == 1)
1014 GeometryData gd{};
1015 gd.coord = 0;
1016#endif
1017 amrex_avg_fc_to_cc(i,j,k, ccma[box_no], AMREX_D_DECL(fxma[box_no],
1018 fyma[box_no],
1019 fzma[box_no]),
1020 dcomp
1021#if (AMREX_SPACEDIM == 1)
1022 , gd
1023#endif
1024 );
1025 });
1026 if (!Gpu::inNoSyncRegion()) {
1028 }
1029 } else
1030#endif
1031 {
1032#ifdef AMREX_USE_OMP
1033#pragma omp parallel if (Gpu::notInLaunchRegion())
1034#endif
1035 for (MFIter mfi(cc,TilingIfNotGPU()); mfi.isValid(); ++mfi)
1036 {
1037 const Box bx = mfi.growntilebox(ng_vect);
1038 auto const& ccarr = cc.array(mfi);
1039 AMREX_D_TERM(auto const& fxarr = fc[0]->const_array(mfi);,
1040 auto const& fyarr = fc[1]->const_array(mfi);,
1041 auto const& fzarr = fc[2]->const_array(mfi););
1042
1043#if (AMREX_SPACEDIM == 1)
1045 {
1046 GeometryData gd;
1047 gd.coord = 0;
1048 amrex_avg_fc_to_cc(i,j,k, ccarr, fxarr, dcomp, gd);
1049 });
1050#else
1052 {
1053 amrex_avg_fc_to_cc(i,j,k, ccarr, AMREX_D_DECL(fxarr,fyarr,fzarr), dcomp);
1054 });
1055#endif
1056 }
1057 }
1058}
1059
1060template <FabArrayType MF>
1061void average_down_faces (const Vector<const MF*>& fine,
1062 const Vector<MF*>& crse,
1063 const IntVect& ratio, int ngcrse)
1064{
1065 AMREX_ASSERT(fine.size() == AMREX_SPACEDIM && crse.size() == AMREX_SPACEDIM);
1067 {{AMREX_D_DECL(fine[0],fine[1],fine[2])}},
1069 {{AMREX_D_DECL(crse[0],crse[1],crse[2])}},
1070 ratio, ngcrse);
1071}
1072
1073template <FabArrayType MF>
1074void average_down_faces (const Vector<const MF*>& fine,
1075 const Vector<MF*>& crse, int ratio, int ngcrse)
1076{
1077 average_down_faces(fine,crse,IntVect{ratio},ngcrse);
1078}
1079
1080template <FabArrayType MF>
1083 int ratio, int ngcrse)
1084{
1085 average_down_faces(fine,crse,IntVect{ratio},ngcrse);
1086}
1087
1088template <FabArrayType MF>
1091 const IntVect& ratio, int ngcrse)
1092{
1093 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1094 average_down_faces(*fine[idim], *crse[idim], ratio, ngcrse);
1095 }
1096}
1097
1098template <BaseFabType FAB>
1099void average_down_faces (const FabArray<FAB>& fine, FabArray<FAB>& crse,
1100 const IntVect& ratio, int ngcrse)
1101{
1102 BL_PROFILE("average_down_faces");
1103
1104 AMREX_ASSERT(crse.nComp() == fine.nComp());
1105 AMREX_ASSERT(fine.ixType() == crse.ixType());
1106 const auto type = fine.ixType();
1107 int dir;
1108 for (dir = 0; dir < AMREX_SPACEDIM; ++dir) {
1109 if (type.nodeCentered(dir)) { break; }
1110 }
1111 auto tmptype = type;
1112 tmptype.unset(dir);
1113 if (dir >= AMREX_SPACEDIM || !tmptype.cellCentered()) {
1114 amrex::Abort("average_down_faces: not face index type");
1115 }
1116 const int ncomp = crse.nComp();
1117 if (isMFIterSafe(fine, crse))
1118 {
1119#ifdef AMREX_USE_GPU
1120 if (Gpu::inLaunchRegion() && crse.isFusingCandidate()) {
1121 auto const& crsema = crse.arrays();
1122 auto const& finema = fine.const_arrays();
1123 ParallelFor(crse, IntVect(ngcrse), ncomp,
1124 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
1125 {
1126 amrex_avgdown_faces(i,j,k,n, crsema[box_no], finema[box_no], 0, 0, ratio, dir);
1127 });
1128 if (!Gpu::inNoSyncRegion()) {
1130 }
1131 } else
1132#endif
1133 {
1134#ifdef AMREX_USE_OMP
1135#pragma omp parallel if (Gpu::notInLaunchRegion())
1136#endif
1137 for (MFIter mfi(crse,TilingIfNotGPU()); mfi.isValid(); ++mfi)
1138 {
1139 const Box& bx = mfi.growntilebox(ngcrse);
1140 auto const& crsearr = crse.array(mfi);
1141 auto const& finearr = fine.const_array(mfi);
1142 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
1143 {
1144 amrex_avgdown_faces(i,j,k,n, crsearr, finearr, 0, 0, ratio, dir);
1145 });
1146 }
1147 }
1148 }
1149 else
1150 {
1151 FabArray<FAB> ctmp(amrex::coarsen(fine.boxArray(),ratio), fine.DistributionMap(),
1152 ncomp, ngcrse, MFInfo().SetArena(The_Async_Arena()), DefaultFabFactory<FAB>());
1153 average_down_faces(fine, ctmp, ratio, ngcrse);
1154 crse.ParallelCopy(ctmp,0,0,ncomp,ngcrse,ngcrse);
1155 }
1156}
1157
1158template <FabArrayType MF>
1161 const IntVect& ratio, const Geometry& crse_geom)
1162{
1163 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1164 average_down_faces(*fine[idim], *crse[idim], ratio, crse_geom);
1165 }
1166}
1167
1168template <BaseFabType FAB>
1169void average_down_faces (const FabArray<FAB>& fine, FabArray<FAB>& crse,
1170 const IntVect& ratio, const Geometry& crse_geom)
1171{
1172 FabArray<FAB> ctmp(amrex::coarsen(fine.boxArray(),ratio), fine.DistributionMap(),
1173 crse.nComp(), 0, MFInfo().SetArena(The_Async_Arena()));
1174 average_down_faces(fine, ctmp, ratio, 0);
1175 crse.ParallelCopy(ctmp,0,0,crse.nComp(),0,0,crse_geom.periodicity());
1176}
1177
1178template <FabArrayType MF>
1179Vector<typename MF::value_type> get_cell_data (MF const& mf, IntVect const& cell)
1181 using T = typename MF::value_type;
1182 const int ncomp = mf.nComp();
1183 Gpu::DeviceVector<T> dv(ncomp);
1184 auto* dp = dv.data();
1185 bool found = false;
1186 auto loc = cell.dim3();
1187 for (MFIter mfi(mf); mfi.isValid() && !found; ++mfi)
1188 {
1189 Box const& box = mfi.validbox();
1190 if (box.contains(cell)) {
1191 found = true;
1192 auto const& fab = mf.const_array(mfi);
1193 amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) noexcept
1194 {
1195 for (int n = 0; n < ncomp; ++n) {
1196 dp[n] = fab(loc.x,loc.y,loc.z,n);
1197 }
1198 });
1199 }
1200 }
1201 Vector<T> hv;
1202 if (found) {
1203 hv.resize(ncomp);
1204 Gpu::copy(Gpu::deviceToHost, dv.begin(), dv.end(), hv.begin());
1205 }
1206 return hv;
1207}
1208
1209template <FabArrayType MF>
1210MF get_line_data (MF const& mf, int dir, IntVect const& cell, Box const& bnd_bx)
1212 bool do_bnd = (!bnd_bx.isEmpty());
1213
1214 BoxArray const& ba = mf.boxArray();
1215 DistributionMapping const& dm = mf.DistributionMap();
1216 const auto nboxes = static_cast<int>(ba.size());
1217
1218 BoxList bl(ba.ixType());
1219 Vector<int> procmap;
1220 Vector<int> index_map;
1221 if (!do_bnd) {
1222 for (int i = 0; i < nboxes; ++i) {
1223 Box const& b = ba[i];
1224 IntVect lo = cell;
1225 lo[dir] = b.smallEnd(dir);
1226 if (b.contains(lo)) {
1227 IntVect hi = lo;
1228 hi[dir] = b.bigEnd(dir);
1229 Box b1d(lo,hi,b.ixType());
1230 bl.push_back(b1d);
1231 procmap.push_back(dm[i]);
1232 index_map.push_back(i);
1233 }
1234 }
1235 } else {
1236 for (int i = 0; i < nboxes; ++i) {
1237 Box const& b = ba[i];
1238 Box const& b1d = bnd_bx & b;
1239 if (b1d.ok()) {
1240 bl.push_back(b1d);
1241 procmap.push_back(dm[i]);
1242 index_map.push_back(i);
1243 }
1244 }
1245 }
1246
1247 if (bl.isEmpty()) {
1248 return MF();
1249 } else {
1250 BoxArray rba(std::move(bl));
1251 DistributionMapping rdm(std::move(procmap));
1252 MF rmf(rba, rdm, mf.nComp(), IntVect(0),
1253 MFInfo().SetArena(mf.arena()));
1254#ifdef AMREX_USE_OMP
1255#pragma omp parallel if (Gpu::notInLaunchRegion())
1256#endif
1257 for (MFIter mfi(rmf); mfi.isValid(); ++mfi) {
1258 Box const& b = mfi.validbox();
1259 auto const& dfab = rmf.array(mfi);
1260 auto const& sfab = mf.const_array(index_map[mfi.index()]);
1261 amrex::ParallelFor(b, mf.nComp(),
1262 [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
1263 {
1264 dfab(i,j,k,n) = sfab(i,j,k,n);
1265 });
1266 }
1267 return rmf;
1268 }
1269}
1270
1271
1272namespace detail {
1273template <typename Op, typename T, typename F>
1274void reduce_to_plane (Array4<T> const& ar, int direction, Box const& bx, int box_no,
1275 F const& f)
1276{
1277#if defined(AMREX_USE_GPU)
1278 Box b2d = bx;
1279 b2d.setRange(direction,0);
1280 const auto blo = amrex::lbound(bx);
1281 const auto len = amrex::length(bx);
1282 constexpr int nthreads = 128;
1283 auto nblocks = static_cast<int>(b2d.numPts());
1284#ifdef AMREX_USE_SYCL
1285 constexpr std::size_t shared_mem_bytes = sizeof(T)*Gpu::Device::warp_size;
1286 amrex::launch<nthreads>(nblocks, shared_mem_bytes, Gpu::gpuStream(),
1287 [=] AMREX_GPU_DEVICE (Gpu::Handler const& h)
1288 {
1289 int bid = h.blockIdx();
1290 int tid = h.threadIdx();
1291#else
1293 [=] AMREX_GPU_DEVICE ()
1294 {
1295 int bid = blockIdx.x;
1296 int tid = threadIdx.x;
1297#endif
1298 T tmp;
1299 Op().init(tmp);
1300 T* p;
1301 if (direction == 0) {
1302 int k = bid / len.y;
1303 int j = bid - k*len.y;
1304 k += blo.z;
1305 j += blo.y;
1306 for (int i = blo.x + tid; i < blo.x+len.x; i += nthreads) {
1307 Op().local_update(tmp, f(box_no,i,j,k));
1308 }
1309 p = ar.ptr(0,j,k);
1310 } else if (direction == 1) {
1311 int k = bid / len.x;
1312 int i = bid - k*len.x;
1313 k += blo.z;
1314 i += blo.x;
1315 for (int j = blo.y + tid; j < blo.y+len.y; j += nthreads) {
1316 Op().local_update(tmp, f(box_no,i,j,k));
1317 }
1318 p = ar.ptr(i,0,k);
1319 } else {
1320 int j = bid / len.x;
1321 int i = bid - j*len.x;
1322 j += blo.y;
1323 i += blo.x;
1324 for (int k = blo.z + tid; k < blo.z+len.z; k += nthreads) {
1325 Op().local_update(tmp, f(box_no,i,j,k));
1326 }
1327 p = ar.ptr(i,j,0);
1328 }
1329#ifdef AMREX_USE_SYCL
1330 Op().template parallel_update<T>(*p, tmp, h);
1331#else
1332 Op().template parallel_update<T,nthreads>(*p, tmp);
1333#endif
1334 });
1335#else
1336 // CPU
1337 if (direction == 0) {
1338 AMREX_LOOP_3D(bx, i, j, k,
1339 {
1340 Op().local_update(ar(0,j,k), f(box_no,i,j,k));
1341 });
1342 } else if (direction == 1) {
1343 AMREX_LOOP_3D(bx, i, j, k,
1344 {
1345 Op().local_update(ar(i,0,k), f(box_no,i,j,k));
1346 });
1347 } else {
1348 AMREX_LOOP_3D(bx, i, j, k,
1349 {
1350 Op().local_update(ar(i,j,0), f(box_no,i,j,k));
1351 });
1352 }
1353#endif
1354}
1355}
1357
1358template <typename Op, typename T, BaseFabType FAB, typename F>
1359#ifndef AMREX_USE_CUDA
1360requires (IsCallableR<T,F,int,int,int,int>::value)
1361#endif
1362BaseFab<T>
1363ReduceToPlane (int direction, Box const& a_domain, FabArray<FAB> const& mf, F const& f)
1365 Box const domain = amrex::convert(a_domain, mf.ixType());
1366
1367 Box domain2d = domain;
1368 domain2d.setRange(direction, 0);
1369
1370 T initval;
1371 Op().init(initval);
1372
1373 BaseFab<T> r(domain2d);
1374 r.template setVal<RunOn::Device>(initval);
1375 auto const& ar = r.array();
1376
1377 for (MFIter mfi(mf,MFItInfo().UseDefaultStream().DisableDeviceSync());
1378 mfi.isValid(); ++mfi)
1379 {
1380 Box bx = mfi.validbox() & domain;
1381 if (bx.ok()) {
1382 int box_no = mfi.LocalIndex();
1383 detail::reduce_to_plane<Op, T>(ar, direction, bx, box_no, f);
1384 }
1385 }
1386 if (!Gpu::inNoSyncRegion()) {
1388 }
1389
1390 return r;
1391}
1392
1393
1394namespace detail {
1395template <typename Op, MultiFabLike FA, typename F>
1396FA reduce_to_plane (int direction, Box const& domain, FA const& mf, F const& f,
1397 IntVect const& nghost = IntVect(0))
1398{
1399 using T = typename FA::value_type;
1400
1401 Box const ndomain = amrex::convert(domain, mf.ixType());
1402
1403 auto npts = amrex::convert(mf.boxArray(),IntVect(0)).numPts();
1404 if (npts != amrex::convert(domain, amrex::IntVect(0)).numPts()) {
1405 amrex::Abort("ReduceToPlaneMF: mf's BoxArray must have a rectangular domain.");
1406 }
1407
1408 // the reduction reads mf's guard cells, so it cannot include more guard
1409 // cells than mf actually has
1410 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nghost.allLE(mf.nGrowVect()),
1411 "ReduceToPlaneMF2: nghost must not exceed mf.nGrowVect().");
1412
1413 T initval;
1414 Op().init(initval);
1415
1416 // guard cells in the reduction direction are summed into the plane; guard
1417 // cells in the transverse directions are stored in the result's ghost cells
1418 // (and folded into the valid region by the caller's ParallelAdd)
1419 IntVect ng2d = nghost;
1420 ng2d[direction] = 0;
1421 Box const gdomain = amrex::grow(ndomain, nghost);
1422
1423 BoxList bl = mf.boxArray().boxList();
1424 for (auto& b : bl) {
1425 b.setRange(direction, 0);
1426 }
1427 BoxArray ba(std::move(bl));
1428 FA tmpfa(ba, mf.DistributionMap(), 1, ng2d);
1429 tmpfa.setVal(initval);
1430
1431 for (MFIter mfi(mf); mfi.isValid(); ++mfi)
1432 {
1433 Box bx = amrex::grow(mfi.validbox(), nghost) & gdomain;
1434 if (bx.ok()) {
1435 int box_no = mfi.LocalIndex();
1436 detail::reduce_to_plane<Op, T>(tmpfa.array(mfi), direction, bx, box_no, f);
1437 }
1438 }
1439
1440 return tmpfa;
1441}
1442
1443}
1445
1446template <typename Op, MultiFabLike FA, typename F>
1447#ifndef AMREX_USE_CUDA
1448requires (IsCallableR<typename FA::value_type,
1449 F,int,int,int,int>::value)
1450#endif
1451FA ReduceToPlaneMF (int direction, Box const& domain, FA const& mf, F const& f)
1453 auto [fa3, fa2] = ReduceToPlaneMF2<Op>(direction, domain, mf, f);
1454 fa3.ParallelCopy(fa2);
1455#ifdef AMREX_USE_GPU
1456 // fa2 is about to be freed
1458#endif
1459 return std::move(fa3);
1460}
1461
1462template <typename Op, MultiFabLike FA, typename F>
1463#ifndef AMREX_USE_CUDA
1464requires (IsCallableR<typename FA::value_type,
1465 F,int,int,int,int>::value)
1466#endif
1467std::pair<FA,FA>
1468ReduceToPlaneMF2 (int direction, Box const& domain, FA const& mf, F const& f,
1469 IntVect const& nghost, Periodicity const& period)
1470{
1471 using T = typename FA::value_type;
1472
1473 T initval;
1474 Op().init(initval);
1475
1476 auto tmpmf = detail::reduce_to_plane<Op>(direction, domain, mf, f, nghost);
1477
1478 BoxList bl2d(mf.ixType());
1479 Vector<int> procmap2d;
1480 auto const& ba3d = mf.boxArray();
1481 auto const& dm3d = mf.DistributionMap();
1482 int dlo = domain.smallEnd(direction);
1483 for (int i = 0, N = mf.size(); i < N; ++i) {
1484 Box b = ba3d[i];
1485 if (b.smallEnd(direction) <= dlo && dlo <= b.bigEnd(direction)) {
1486 b.setRange(direction, 0);
1487 bl2d.push_back(b);
1488 procmap2d.push_back(dm3d[i]);
1489 }
1490 }
1491
1492 BoxArray ba2d(std::move(bl2d));
1493 DistributionMapping dm2d(std::move(procmap2d));
1494
1495 FA mf2d(ba2d, dm2d, 1, 0);
1496 mf2d.setVal(initval);
1497
1498 static_assert(std::is_same_v<Op, ReduceOpSum>, "Currently only ReduceOpSum is supported.");
1499 // fold the transverse guard cells of the local results into the valid
1500 // region of the unique result (sums the quantity across box seams)
1501 IntVect ng2d = nghost;
1502 ng2d[direction] = 0;
1503 mf2d.ParallelAdd(tmpmf, 0, 0, 1, ng2d, IntVect(0), IntVect(0), period);
1504
1505 return std::make_pair(std::move(tmpmf), std::move(mf2d));
1506}
1507
1508template <typename Op, MultiFabLike FA, typename F>
1509#ifndef AMREX_USE_CUDA
1510requires (IsCallableR<typename FA::value_type,
1511 F,int,int,int,int>::value)
1512#endif
1513std::pair<FA,FA>
1514ReduceToPlaneMF2Patchy (int direction, Box const& domain, FA const& mf,
1515 IntVect const& plane_max_grid_size, F const& f)
1516{
1517 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(mf.is_cell_centered(),
1518 "ReduceToPlaneMF2Patchy: only cell-centered data is supported.");
1519
1520 Box const ndomain = amrex::convert(domain, mf.ixType());
1521 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ndomain.contains(mf.boxArray().minimalBox()),
1522 "ReduceToPlaneMF2Patchy: subdomains are not supported; domain must cover the full extent of mf.boxArray().");
1523
1524 using T = typename FA::value_type;
1525 T initval;
1526 Op().init(initval);
1527
1528 BoxList bl_patch2d = mf.boxArray().boxList();
1529 for (auto& b : bl_patch2d) {
1530 b.setRange(direction, 0);
1531 }
1532 BoxArray ba_patch2d(std::move(bl_patch2d));
1533 FA tmpmf(ba_patch2d, mf.DistributionMap(), 1, 0);
1534 tmpmf.setVal(initval);
1535
1536 for (MFIter mfi(mf); mfi.isValid(); ++mfi)
1537 {
1538 Box bx = mfi.validbox() & ndomain;
1539 if (bx.ok()) {
1540 int box_no = mfi.LocalIndex();
1541 detail::reduce_to_plane<Op, T>(tmpmf.array(mfi), direction, bx, box_no, f);
1542 }
1543 }
1544
1545 BoxList bl_unique = ba_patch2d.boxList();
1546 bl_unique.simplify(); // reduces work for .removeOverlap()
1547 BoxArray ba2d(std::move(bl_unique));
1548 ba2d.removeOverlap();
1549
1550 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(plane_max_grid_size.allGE(1),
1551 "ReduceToPlaneMF2Patchy: plane_max_grid_size must be >= 1 in every direction.");
1552 IntVect mgs2d = plane_max_grid_size;
1553 mgs2d[direction] = 1;
1554 ba2d.maxSize(mgs2d);
1555
1556 DistributionMapping dm2d(ba2d);
1557
1558 FA mf2d(ba2d, dm2d, 1, 0);
1559 mf2d.setVal(initval);
1560
1561 static_assert(std::is_same_v<Op, ReduceOpSum>, "Currently only ReduceOpSum is supported.");
1562 mf2d.ParallelAdd(tmpmf);
1563
1564 return std::make_pair(std::move(tmpmf), std::move(mf2d));
1565}
1566
1567}
1568
1569#endif
Fixed-size array types for use on GPU and CPU.
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_HOST_DEVICE_PARALLEL_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:109
#define AMREX_HOST_DEVICE_PARALLEL_FOR_3D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:110
#define AMREX_HOST_DEVICE_PARALLEL_FOR_4D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:111
#define AMREX_LAUNCH_HOST_DEVICE_LAMBDA(...)
Definition AMReX_GpuLaunch.nolint.H:16
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_LOOP_3D(bx, i, j, k, block)
#define AMREX_LOOP_4D(bx, ncomp, i, j, k, n, block)
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
A FortranArrayBox(FAB)-like object.
Definition AMReX_BaseFab.H:243
Array4< T const > array() const noexcept
Create an Array4 view over all components.
Definition AMReX_BaseFab.H:496
Reference-counted collection of Boxes.
Definition AMReX_BoxArray.H:681
IndexType ixType() const noexcept
Return index type of this BoxArray.
Definition AMReX_BoxArray.H:1269
BoxList boxList() const
Create a BoxList from this BoxArray.
Definition AMReX_BoxArray.cpp:968
void removeOverlap(bool simplify=true)
Change the BoxArray to one with no overlap and then simplify it (see the simplify function in BoxList...
Definition AMReX_BoxArray.cpp:1477
BoxArray & coarsen(int refinement_ratio)
Coarsen each Box in the BoxArray by refinement_ratio.
Definition AMReX_BoxArray.cpp:685
Long size() const noexcept
Return the number of boxes in the BoxArray.
Definition AMReX_BoxArray.H:758
BoxArray & maxSize(int block_size)
Split any Box whose side length exceeds block_size.
Definition AMReX_BoxArray.cpp:553
A list of Boxes sharing a common IndexType.
Definition AMReX_BoxList.H:109
bool isEmpty() const noexcept
Is this BoxList empty?
Definition AMReX_BoxList.H:202
int simplify(bool best=false)
Merge adjacent Boxes in this BoxList. Return the number of Boxes merged. If "best" is specified we do...
Definition AMReX_BoxList.cpp:676
void push_back(const Box &bn)
Append a Box to this BoxList.
Definition AMReX_BoxList.H:151
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:136
__host__ __device__ bool contains(const IntVectND< dim > &p) const noexcept
Return true if argument is contained within BoxND.
Definition AMReX_Box.H:233
__host__ __device__ IndexTypeND< dim > ixType() const noexcept
Return the indexing type.
Definition AMReX_Box.H:148
__host__ __device__ BoxND & setRange(int dir, int sm_index, int n_cells=1) noexcept
Set the entire range in a given direction, starting at sm_index with length n_cells....
Definition AMReX_Box.H:1143
__host__ __device__ bool ok() const noexcept
Return true if high bounds are >= low bounds and the index type is valid.
Definition AMReX_Box.H:229
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:124
Default FabFactory that calls new / delete on the FAB type directly.
Definition AMReX_FabFactory.H:124
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:55
bool isFusingCandidate() const noexcept
Is this a good candidate for kernel fusing?
Definition AMReX_FabArrayBase.cpp:2795
const CPC & getCPC(const IntVect &dstng, const FabArrayBase &src, const IntVect &srcng, const Periodicity &period, bool to_ghost_cells_only=false, const IntVect &offset=IntVect(0)) const
Retrieve (and cache) the ParallelCopy metadata between two FabArrays.
Definition AMReX_FabArrayBase.cpp:583
int nComp() const noexcept
Return number of variables (aka components) associated with each point.
Definition AMReX_FabArrayBase.H:88
An Array of FortranArrayBox(FAB)-like Objects.
Definition AMReX_FabArray.H:383
LayoutData< int > RecvLayoutMask(const CommMetaData &thecmd)
Definition AMReX_FabArray.H:4258
MultiArray4< typename FabArray< FAB >::value_type > arrays() noexcept
Build, if needed, and return mutable Array4 views for local FABs.
Definition AMReX_FabArray.H:741
void setVal(value_type val)
Set all components in the entire region of each FAB to val.
Definition AMReX_FabArray.H:3068
Array4< typename FabArray< FAB >::value_type const > array(const MFIter &mfi) const noexcept
Read-only Array4 view for the FAB referenced by iterator mfi.
Definition AMReX_FabArray.H:649
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:87
Periodicity periodicity() const noexcept
Return the Periodicity based on the length of the domain.
Definition AMReX_Geometry.H:426
GPU-compatible tuple.
Definition AMReX_Tuple.H:104
static constexpr int warp_size
Definition AMReX_GpuDevice.H:239
__host__ static __device__ constexpr IntVectND< dim > TheUnitVector() noexcept
Definition AMReX_IntVect.H:788
__host__ __device__ constexpr bool allGE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than or equal to argument for all components. NOTE: This is NOT a str...
Definition AMReX_IntVect.H:549
__host__ static __device__ constexpr IntVectND< dim > TheZeroVector() noexcept
Definition AMReX_IntVect.H:778
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:176
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:40
iterator begin() noexcept
Definition AMReX_PODVector.H:680
iterator end() noexcept
Definition AMReX_PODVector.H:684
T * data() noexcept
Definition AMReX_PODVector.H:672
This provides length of period for periodic domains. 0 means it is not periodic in that direction....
Definition AMReX_Periodicity.H:17
static const Periodicity & NonPeriodic() noexcept
Definition AMReX_Periodicity.cpp:52
A Box with real dimensions.
Definition AMReX_RealBox.H:28
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
A Collection of IArrayBoxes.
Definition AMReX_iMultiFab.H:34
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
__host__ __device__ Dim3 length(Array4< T > const &a) noexcept
Return the spatial extents of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1391
ArrayND< T, 4, true > Array4
Convenient alias for a 4D ArrayND with three spatial dimensions plus a component dimension.
Definition AMReX_Array4.H:1352
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Return the inclusive lower bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1363
__host__ __device__ BoxND< dim > convert(const BoxND< dim > &b, const IntVectND< dim > &typ) noexcept
Return a copy of b converted to the nodal flags typ.
Definition AMReX_Box.H:1632
__host__ __device__ BoxND< dim > coarsen(const BoxND< dim > &b, int ref_ratio) noexcept
Return a copy of b coarsened by the isotropic ratio ref_ratio.
Definition AMReX_Box.H:1471
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Return a copy of b grown uniformly by i cells in every direction.
Definition AMReX_Box.H:1328
std::array< T, N > Array
Definition AMReX_Array.H:31
PinnedVector< T > HostVector
A PODVector that uses pinned host memory. Same as PinnedVector. For a vector class that uses std::all...
Definition AMReX_GpuContainers.H:67
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:849
void Sum(Gpu::DeviceVector< T > &v, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:37
void copy(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:128
static constexpr DeviceToHost deviceToHost
Definition AMReX_GpuContainers.H:106
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
bool inNoSyncRegion() noexcept
Definition AMReX_GpuControl.H:148
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:310
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
Definition AMReX_Amr.cpp:50
std::pair< FA, FA > ReduceToPlaneMF2Patchy(int direction, Box const &domain, FA const &mf, IntVect const &plane_max_grid_size, F const &f)
Reduce FabArray/MultiFab data to plane FabArray for patchy BoxArrays.
Definition AMReX_MultiFabUtil.H:1515
void FillRandomNormal(MultiFab &mf, int scomp, int ncomp, Real mean, Real stddev)
Fill MultiFab with random numbers from normal distribution.
Definition AMReX_MultiFabUtil.cpp:1326
void FillRandom(MultiFab &mf, int scomp, int ncomp)
Fill MultiFab with random numbers from uniform distribution.
Definition AMReX_MultiFabUtil.cpp:1313
@ F
Definition AMReX_Order.H:7
ReduceData< Ts... >::Type ParReduce(TypeList< Ops... > operation_list, TypeList< Ts... > type_list, FabArray< FAB > const &fa, IntVect const &nghost, F &&f)
Parallel reduce for MultiFab/FabArray. The reduce result is local and it's the user's responsibility ...
Definition AMReX_ParReduce.H:48
void average_down(const MultiFab &S_fine, MultiFab &S_crse, const Geometry &fgeom, const Geometry &cgeom, int scomp, int ncomp, int rr)
Definition AMReX_MultiFabUtil.cpp:360
__host__ __device__ void cast(BaseFab< Tto > &tofab, BaseFab< Tfrom > const &fromfab, Box const &bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Cast components from one BaseFab to another over a region.
Definition AMReX_BaseFabUtility.H:30
void computeDivergence(MultiFab &divu, const Array< MultiFab const *, 3 > &umac, const Geometry &geom)
Definition AMReX_MultiFabUtil.cpp:744
std::unique_ptr< MultiFab > get_slice_data(int dir, Real coord, const MultiFab &cc, const Geometry &geom, int start_comp, int ncomp, bool interpolate, RealBox const &bnd_rbx)
Definition AMReX_MultiFabUtil.cpp:600
void average_face_to_cellcenter(MultiFab &cc, int dcomp, const Vector< const MultiFab * > &fc, IntVect const &ng_vect)
Definition AMReX_MultiFabUtil.cpp:157
iMultiFab makeFineMask(const BoxArray &cba, const DistributionMapping &cdm, const BoxArray &fba, const IntVect &ratio, int crse_value, int fine_value, MFInfo const &info)
Definition AMReX_MultiFabUtil.cpp:655
Vector< MultiFab > convexify(Vector< MultiFab const * > const &mf, Vector< IntVect > const &refinement_ratio)
Convexify AMR data.
Definition AMReX_MultiFabUtil.cpp:1339
MF get_line_data(MF const &mf, int dir, IntVect const &cell, Box const &bnd_bx=Box())
Get data in a line of MultiFab/FabArray.
Definition AMReX_MultiFabUtil.H:1211
FA ReduceToPlaneMF(int direction, Box const &domain, FA const &mf, F const &f)
Reduce FabArray/MultiFab data to plane FabArray.
Definition AMReX_MultiFabUtil.H:1452
MultiFab periodicShift(MultiFab const &mf, IntVect const &offset, Periodicity const &period)
Periodic shift MultiFab.
Definition AMReX_MultiFabUtil.cpp:828
BaseFab< T > ReduceToPlane(int direction, Box const &domain, FabArray< FAB > const &mf, F const &f)
Reduce FabArray/MultiFab data to a plane Fab.
Definition AMReX_MultiFabUtil.H:1364
void launch(T const &n, L const &f) noexcept
Definition AMReX_GpuLaunchFunctsC.H:122
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
Real volumeWeightedSum(Vector< MultiFab const * > const &mf, int icomp, Vector< Geometry > const &geom, Vector< IntVect > const &ratio, bool local)
Volume weighted sum for a vector of MultiFabs.
Definition AMReX_MultiFabUtil.cpp:1006
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
void average_down_faces(const Vector< const MF * > &fine, const Vector< MF * > &crse, const IntVect &ratio, int ngcrse=0)
Average fine face-based FabArray onto crse face-based FabArray.
Definition AMReX_MultiFabUtil.H:1062
bool isMFIterSafe(const FabArrayBase &x, const FabArrayBase &y)
Definition AMReX_MFIter.H:256
MultiFab ToMultiFab(const iMultiFab &imf)
Convert iMultiFab to MultiFab.
Definition AMReX_MultiFabUtil.cpp:590
Vector< typename MF::value_type > get_cell_data(MF const &mf, IntVect const &cell)
Get data in a cell of MultiFab/FabArray.
Definition AMReX_MultiFabUtil.H:1180
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:3028
void writeFabs(const MultiFab &mf, const std::string &name)
Write each fab individually.
Definition AMReX_MultiFabUtil.cpp:577
Real NormHelper(const MultiFab &x, int xcomp, const MultiFab &y, int ycomp, F const &f, int numcomp, IntVect nghost, bool local)
Returns part of a norm based on two MultiFabs.
Definition AMReX_MultiFabUtil.H:867
void average_down_edges(const Vector< const MultiFab * > &fine, const Vector< MultiFab * > &crse, const IntVect &ratio, int ngcrse)
Average fine edge-based MultiFab onto crse edge-based MultiFab.
Definition AMReX_MultiFabUtil.cpp:495
Gpu::HostVector< Real > sumToLine(MultiFab const &mf, int icomp, int ncomp, Box const &domain, int direction, bool local)
Sum MultiFab data to line.
Definition AMReX_MultiFabUtil.cpp:852
@ y
Definition AMReX_Orientation.H:14
@ x
Definition AMReX_Orientation.H:14
std::pair< FA, FA > ReduceToPlaneMF2(int direction, Box const &domain, FA const &mf, F const &f, IntVect const &nghost=IntVect(0), Periodicity const &period=Periodicity::NonPeriodic())
Reduce FabArray/MultiFab data to plane FabArray.
Definition AMReX_MultiFabUtil.H:1469
void print_state(const MultiFab &mf, const IntVect &cell, const int n, const IntVect &ng)
Output state data for a single zone.
Definition AMReX_MultiFabUtil.cpp:572
bool TilingIfNotGPU() noexcept
Definition AMReX_MFIter.H:12
void sum_fine_to_coarse(const MultiFab &S_fine, MultiFab &S_crse, int scomp, int ncomp, const IntVect &ratio, const Geometry &cgeom, const Geometry &)
Definition AMReX_MultiFabUtil.cpp:440
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
const int[]
Definition AMReX_BLProfiler.cpp:1665
void average_node_to_cellcenter(MultiFab &cc, int dcomp, const MultiFab &nd, int scomp, int ncomp, int ngrow)
Average nodal-based MultiFab onto cell-centered MultiFab.
Definition AMReX_MultiFabUtil.cpp:63
void FourthOrderInterpFromFineToCoarse(MultiFab &cmf, int scomp, int ncomp, MultiFab const &fmf, IntVect const &ratio)
Fourth-order interpolation from fine to coarse level.
Definition AMReX_MultiFabUtil.cpp:1222
void average_cellcenter_to_face(const Vector< MultiFab * > &fc, const MultiFab &cc, const Geometry &geom, int ncomp, bool use_harmonic_averaging, int ngrow)
Average cell-centered MultiFab onto face-based MultiFab with geometric weighting.
Definition AMReX_MultiFabUtil.cpp:242
FabArray< BaseFab< Long > > ToLongMultiFab(const iMultiFab &imf)
Convert iMultiFab to Long.
Definition AMReX_MultiFabUtil.cpp:595
void computeGradient(MultiFab &grad, const Array< MultiFab const *, 3 > &umac, const Geometry &geom)
Computes gradient of face-data stored in the umac MultiFab.
Definition AMReX_MultiFabUtil.cpp:804
void average_edge_to_cellcenter(MultiFab &cc, int dcomp, const Vector< const MultiFab * > &edge, int ngrow)
Average edge-based MultiFab onto cell-centered MultiFab.
Definition AMReX_MultiFabUtil.cpp:106
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:2174
void average_down_nodal(const FabArray< FAB > &S_fine, FabArray< FAB > &S_crse, const IntVect &ratio, int ngcrse=0, bool mfiter_is_definitely_safe=false)
Average fine nodal MultiFab onto coarse nodal MultiFab.
Definition AMReX_MultiFabUtil.H:680
parallel copy or add
Definition AMReX_FabArrayBase.H:651
GPU-safe snapshot of Geometry data suitable for capture in GPU kernels.
Definition AMReX_Geometry.H:38
int coord
Coordinate-system type: 0 for Cartesian, 1 for RZ, and 2 for spherical.
Definition AMReX_Geometry.H:72
FabArray memory allocation information.
Definition AMReX_FabArray.H:73
Definition AMReX_MFIter.H:20
Struct for holding types.
Definition AMReX_TypeList.H:13