Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_BaseFab.H
Go to the documentation of this file.
1#ifndef AMREX_BASEFAB_H_
2#define AMREX_BASEFAB_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Algorithm.H>
6#include <AMReX_Extension.H>
7#include <AMReX_BLassert.H>
8#include <AMReX_Array.H>
9#include <AMReX_Box.H>
10#include <AMReX_Loop.H>
11#include <AMReX_BoxList.H>
12#include <AMReX_DataAllocator.H>
13#include <AMReX_REAL.H>
14#include <AMReX_BLProfiler.H>
15#include <AMReX_BoxIterator.H>
16#include <AMReX_MakeType.H>
17#include <AMReX_Utility.H>
18#include <AMReX_Reduce.H>
19#include <AMReX_Gpu.H>
20#include <AMReX_Scan.H>
21#include <AMReX_Math.H>
22#include <AMReX_OpenMP.H>
23#include <AMReX_MemPool.H>
24#include <AMReX_TypeTraits.H>
25
26#include <cmath>
27#include <cstdlib>
28#include <algorithm>
29#include <limits>
30#include <climits>
31#include <array>
32#include <type_traits>
33#include <memory>
34#include <atomic>
35#include <utility>
36
41
42namespace amrex
43{
44
46
47extern std::atomic<Long> atomic_total_bytes_allocated_in_fabs;
48extern std::atomic<Long> atomic_total_bytes_allocated_in_fabs_hwm;
49extern std::atomic<Long> atomic_total_cells_allocated_in_fabs;
50extern std::atomic<Long> atomic_total_cells_allocated_in_fabs_hwm;
51extern Long private_total_bytes_allocated_in_fabs;
52extern Long private_total_bytes_allocated_in_fabs_hwm;
53extern Long private_total_cells_allocated_in_fabs;
54extern Long private_total_cells_allocated_in_fabs_hwm;
55#ifdef AMREX_USE_OMP
56#pragma omp threadprivate(private_total_bytes_allocated_in_fabs)
57#pragma omp threadprivate(private_total_bytes_allocated_in_fabs_hwm)
58#pragma omp threadprivate(private_total_cells_allocated_in_fabs)
59#pragma omp threadprivate(private_total_cells_allocated_in_fabs_hwm)
60#endif
61
63
90void update_fab_stats (Long n, Long s, std::size_t szt) noexcept;
91
93void BaseFab_Initialize ();
95void BaseFab_Finalize ();
96
98struct SrcComp {
100 explicit SrcComp (int ai) noexcept : i(ai) {}
101 int i;
102};
103
105struct DestComp {
107 explicit DestComp (int ai) noexcept : i(ai) {}
108 int i;
109};
110
112struct NumComps {
114 explicit NumComps (int an) noexcept : n(an) {}
115 int n;
116};
117
118namespace detail {
122inline void check_array4_index_range (Box const& bx)
123{
124#if defined(AMREX_USE_GPU) && !defined(AMREX_ARRAY4_64BIT_INDEX)
125 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(bx.numPts() <= Long(std::numeric_limits<int>::max()),
126 "BaseFab: box has too many points for 32-bit Array4 indexing; "
127 "build with USE_ARRAY4_64BIT_INDEX=TRUE (GNU Make) or AMReX_ARRAY4_64BIT_INDEX=ON (CMake)");
128#else
130#endif
131}
132}
133
134template <typename T>
137makeArray4 (T* p, Box const& bx, int ncomp) noexcept
138{
139 return Array4<T>{p, amrex::begin(bx), amrex::end(bx), ncomp};
140}
141
150template <typename T>
151void
152placementNew (T* const ptr, Long n, RunOn run_on = RunOn::Device)
153{
154 amrex::ignore_unused(ptr,n,run_on);
155 if constexpr (!std::is_trivially_default_constructible_v<T>) {
157 {
158 new (ptr+i) T;
159 });
160 }
161}
162
171template <typename T>
172void
173placementDelete (T* const ptr, Long n, RunOn run_on = RunOn::Device)
174{
175 amrex::ignore_unused(ptr,n,run_on);
176 if constexpr (!std::is_trivially_destructible_v<T>) {
178 {
179 (ptr+i)->~T();
180 });
181#ifdef AMREX_USE_GPU
182 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
183 // The memory may be freed right after this returns.
185 }
186#endif
187 }
188}
189
240template <class T>
242 : public DataAllocator
243{
244public:
245
246 template <class U> friend class BaseFab;
247
248 using value_type = T;
249
251 BaseFab () noexcept = default;
252
254 explicit BaseFab (Arena* ar) noexcept;
255
257 BaseFab (const Box& bx, int n, Arena* ar);
258
260 explicit BaseFab (const Box& bx, int n = 1, bool alloc = true,
261 bool shared = false, Arena* ar = nullptr);
262
272 BaseFab (const BaseFab<T>& rhs, MakeType make_type, int scomp, int ncomp);
273
279 BaseFab (const Box& bx, int ncomp, T* p);
280 BaseFab (const Box& bx, int ncomp, T const* p);
281
283 explicit BaseFab (Array4<T> const& a) noexcept;
284
286 explicit BaseFab (Array4<T> const& a, IndexType t) noexcept;
287
289 explicit BaseFab (Array4<T const> const& a) noexcept;
290
292 explicit BaseFab (Array4<T const> const& a, IndexType t) noexcept;
293
295 virtual ~BaseFab () noexcept;
296
297 BaseFab (const BaseFab<T>& rhs) = delete;
298 BaseFab<T>& operator= (const BaseFab<T>& rhs) = delete;
299
301 BaseFab (BaseFab<T>&& rhs) noexcept;
303 BaseFab<T>& operator= (BaseFab<T>&& rhs) noexcept;
304
310 template <RunOn run_on AMREX_DEFAULT_RUNON>
311 BaseFab& operator= (T const&) noexcept;
312
314 static void Initialize();
316 static void Finalize();
317
331 void resize (const Box& b, int N = 1, Arena* ar = nullptr);
332
339 template <class U=T>
340 requires (std::is_trivially_destructible_v<U>)
341 [[nodiscard]] Elixir elixir () noexcept;
342
347 void clear ();
348
350 [[nodiscard]] std::unique_ptr<T,DataDeleter> release () noexcept;
351
353 [[nodiscard]] std::size_t nBytes () const noexcept { return this->truesize*sizeof(T); }
354
355 [[nodiscard]] std::size_t nBytesOwned () const noexcept {
356 return (this->ptr_owner) ? nBytes() : 0;
357 }
358
360 [[nodiscard]] std::size_t nBytes (const Box& bx, int ncomps) const noexcept
361 { return bx.numPts() * sizeof(T) * ncomps; }
362
364 [[nodiscard]] int nComp () const noexcept { return this->nvar; }
365
367 [[nodiscard]] const int* nCompPtr() const noexcept {
368 return &(this->nvar);
369 }
370
372 [[nodiscard]] Long numPts () const noexcept { return this->domain.numPts(); }
373
375 [[nodiscard]] Long size () const noexcept { return this->nvar*this->domain.numPts(); }
376
378 [[nodiscard]] const Box& box () const noexcept { return this->domain; }
379
384 [[nodiscard]] IntVect length () const noexcept { return this->domain.length(); }
385
390 [[nodiscard]] const IntVect& smallEnd () const noexcept { return this->domain.smallEnd(); }
391
393 [[nodiscard]] const IntVect& bigEnd () const noexcept { return this->domain.bigEnd(); }
394
403 [[nodiscard]] const int* loVect () const noexcept { return this->domain.loVect(); }
404
413 [[nodiscard]] const int* hiVect () const noexcept { return this->domain.hiVect(); }
414
419 [[nodiscard]] bool contains (const BaseFab<T>& fab) const noexcept
420 {
421 return box().contains(fab.box()) && this->nvar >= fab.nvar;
422 }
423
428 [[nodiscard]] bool contains (const Box& bx) const noexcept { return box().contains(bx); }
429
439 [[nodiscard]] T* dataPtr (int n = 0) noexcept {
440 if (this->dptr) {
441 return &(this->dptr[n*this->domain.numPts()]);
442 } else {
443 return nullptr;
444 }
445 }
446
448 [[nodiscard]] const T* dataPtr (int n = 0) const noexcept {
449 if (this->dptr) {
450 return &(this->dptr[n*this->domain.numPts()]);
451 } else {
452 return nullptr;
453 }
454 }
455
462 [[nodiscard]] T* dataPtr (const IntVect& p, int n = 0) noexcept;
463
470 [[nodiscard]] const T* dataPtr (const IntVect& p, int n = 0) const noexcept;
471
478 void setPtr (T* p, Long sz) noexcept { AMREX_ASSERT(this->dptr == nullptr && this->truesize == 0); this->dptr = p; this->truesize = sz; }
479
484 void prefetchToHost () const noexcept;
489 void prefetchToDevice () const noexcept;
490
495 [[nodiscard]] AMREX_FORCE_INLINE
496 Array4<T const> array () const noexcept
497 {
498 return makeArray4<T const>(this->dptr, this->domain, this->nvar);
499 }
500
507 [[nodiscard]] AMREX_FORCE_INLINE
508 Array4<T const> array (int start_comp) const noexcept
509 {
510 return Array4<T const>(makeArray4<T const>(this->dptr, this->domain, this->nvar),start_comp);
511 }
512
520 [[nodiscard]] AMREX_FORCE_INLINE
521 Array4<T const> array (int start_comp, int num_comps) const noexcept
522 {
523 return Array4<T const>(makeArray4<T const>(this->dptr, this->domain, this->nvar), start_comp, num_comps);
524 }
525
530 [[nodiscard]] AMREX_FORCE_INLINE
531 Array4<T> array () noexcept
532 {
533 return makeArray4<T>(this->dptr, this->domain, this->nvar);
534 }
535
542 [[nodiscard]] AMREX_FORCE_INLINE
543 Array4<T> array (int start_comp) noexcept
544 {
545 return Array4<T>(makeArray4<T>(this->dptr, this->domain, this->nvar),start_comp);
546 }
547
555 [[nodiscard]] AMREX_FORCE_INLINE
556 Array4<T> array (int start_comp, int num_comps) noexcept
557 {
558 return Array4<T>(makeArray4<T>(this->dptr, this->domain, this->nvar), start_comp, num_comps);
559 }
560
567 [[nodiscard]] AMREX_FORCE_INLINE
568 Array4<T const> const_array () const noexcept
569 {
570 return makeArray4<T const>(this->dptr, this->domain, this->nvar);
571 }
572
579 [[nodiscard]] AMREX_FORCE_INLINE
580 Array4<T const> const_array (int start_comp) const noexcept
581 {
582 return Array4<T const>(makeArray4<T const>(this->dptr, this->domain, this->nvar),start_comp);
583 }
584
591 [[nodiscard]] AMREX_FORCE_INLINE
592 Array4<T const> const_array (int start_comp, int num_comps) const noexcept
593 {
594 return Array4<T const>(makeArray4<T const>(this->dptr, this->domain, this->nvar), start_comp, num_comps);
595 }
596
598 [[nodiscard]] bool isAllocated () const noexcept { return this->dptr != nullptr; }
599
606 [[nodiscard]] T& operator() (const IntVect& p, int N) noexcept;
607
609 [[nodiscard]] T& operator() (const IntVect& p) noexcept;
610
612 [[nodiscard]] const T& operator() (const IntVect& p, int N) const noexcept;
613
615 [[nodiscard]] const T& operator() (const IntVect& p) const noexcept;
616
622 void getVal (T* data, const IntVect& pos, int N, int numcomp) const noexcept;
624 void getVal (T* data, const IntVect& pos) const noexcept;
625
626 template <RunOn run_on AMREX_DEFAULT_RUNON>
627 requires (std::is_same_v<T,float> || std::is_same_v<T,double>)
628 void fill_snan () noexcept;
629
636 template <RunOn run_on AMREX_DEFAULT_RUNON>
637 void setVal (T const& x, const Box& bx, int dcomp, int ncomp) noexcept;
639 template <RunOn run_on AMREX_DEFAULT_RUNON>
640 void setVal (T const& x, const Box& bx, int N = 0) noexcept;
642 template <RunOn run_on AMREX_DEFAULT_RUNON>
643 void setVal (T const& x, int N) noexcept;
644
645 template <RunOn run_on AMREX_DEFAULT_RUNON>
646 void setValIfNot (T const& val, const Box& bx, const BaseFab<int>& mask, int nstart, int num) noexcept;
647
653 template <RunOn run_on AMREX_DEFAULT_RUNON>
654 void setComplement (T const& x, const Box& b, int ns, int num) noexcept;
655
672 template <RunOn run_on AMREX_DEFAULT_RUNON>
673 BaseFab<T>& copy (const BaseFab<T>& src, const Box& srcbox, int srccomp,
674 const Box& destbox, int destcomp, int numcomp) noexcept;
675
682 template <RunOn run_on AMREX_DEFAULT_RUNON>
683 BaseFab<T>& copy (const BaseFab<T>& src, int srccomp, int destcomp,
684 int numcomp = 1) noexcept;
691 template <RunOn run_on AMREX_DEFAULT_RUNON>
692 BaseFab<T>& copy (const BaseFab<T>& src, const Box& destbox) noexcept;
693
695 template <RunOn run_on AMREX_DEFAULT_RUNON>
696 std::size_t copyToMem (const Box& srcbox, int srccomp,
697 int numcomp, void* dst) const noexcept;
698
700 template <RunOn run_on AMREX_DEFAULT_RUNON, typename BUF = T>
701 std::size_t copyFromMem (const Box& dstbox, int dstcomp,
702 int numcomp, const void* src) noexcept;
703
705 template <RunOn run_on AMREX_DEFAULT_RUNON, typename BUF = T>
706 std::size_t addFromMem (const Box& dstbox, int dstcomp,
707 int numcomp, const void* src) noexcept;
708
714 BaseFab<T>& shift (const IntVect& v) noexcept;
720 BaseFab<T>& shift (int idir, int n_cell) noexcept;
726 BaseFab<T>& shiftHalf (int dir, int n_cell) noexcept;
732 BaseFab<T>& shiftHalf (const IntVect& v) noexcept;
733
734 template <RunOn run_on AMREX_DEFAULT_RUNON>
735 [[nodiscard]] Real norminfmask (const Box& subbox, const BaseFab<int>& mask, int scomp=0, int ncomp=1) const noexcept;
736
743 template <RunOn run_on AMREX_DEFAULT_RUNON>
744 [[nodiscard]] Real norm (int p, int scomp = 0, int numcomp = 1) const;
745
747 template <RunOn run_on AMREX_DEFAULT_RUNON>
748 [[nodiscard]] Real norm (const Box& subbox, int p, int scomp = 0, int numcomp = 1) const;
750 template <RunOn run_on AMREX_DEFAULT_RUNON>
751 void abs () noexcept;
753 template <RunOn run_on AMREX_DEFAULT_RUNON>
754 void abs (int comp, int numcomp=1) noexcept;
758 template <RunOn run_on AMREX_DEFAULT_RUNON>
759 void abs (const Box& subbox, int comp = 0, int numcomp=1) noexcept;
763 template <RunOn run_on AMREX_DEFAULT_RUNON>
764 [[nodiscard]] T min (int comp = 0) const noexcept;
768 template <RunOn run_on AMREX_DEFAULT_RUNON>
769 [[nodiscard]] T min (const Box& subbox, int comp = 0) const noexcept;
773 template <RunOn run_on AMREX_DEFAULT_RUNON>
774 [[nodiscard]] T max (int comp = 0) const noexcept;
778 template <RunOn run_on AMREX_DEFAULT_RUNON>
779 [[nodiscard]] T max (const Box& subbox, int comp = 0) const noexcept;
783 template <RunOn run_on AMREX_DEFAULT_RUNON>
784 [[nodiscard]] std::pair<T,T> minmax (int comp = 0) const noexcept;
788 template <RunOn run_on AMREX_DEFAULT_RUNON>
789 [[nodiscard]] std::pair<T,T> minmax (const Box& subbox, int comp = 0) const noexcept;
793 template <RunOn run_on AMREX_DEFAULT_RUNON>
794 [[nodiscard]] T maxabs (int comp = 0) const noexcept;
798 template <RunOn run_on AMREX_DEFAULT_RUNON>
799 [[nodiscard]] T maxabs (const Box& subbox, int comp = 0) const noexcept;
800
801 /*(
802 * \return location of given value
803 */
804 template <RunOn run_on AMREX_DEFAULT_RUNON>
805 [[nodiscard]] IntVect indexFromValue (const Box& subbox, int comp, T const& value) const noexcept;
806
810 template <RunOn run_on AMREX_DEFAULT_RUNON>
811 [[nodiscard]] IntVect minIndex (int comp = 0) const noexcept;
816 template <RunOn run_on AMREX_DEFAULT_RUNON>
817 [[nodiscard]] IntVect minIndex (const Box& subbox, int comp = 0) const noexcept;
822 template <RunOn run_on AMREX_DEFAULT_RUNON>
823 void minIndex (const Box& subbox, Real& min_val, IntVect& min_idx, int comp = 0) const noexcept;
824
828 template <RunOn run_on AMREX_DEFAULT_RUNON>
829 [[nodiscard]] IntVect maxIndex (int comp = 0) const noexcept;
834 template <RunOn run_on AMREX_DEFAULT_RUNON>
835 [[nodiscard]] IntVect maxIndex (const Box& subbox, int comp = 0) const noexcept;
840 template <RunOn run_on AMREX_DEFAULT_RUNON>
841 void maxIndex (const Box& subbox, Real& max_value, IntVect& max_idx, int comp = 0) const noexcept;
842
844 template <RunOn run_on AMREX_DEFAULT_RUNON>
845 [[nodiscard]] T sum (int comp, int numcomp = 1) const noexcept;
847 template <RunOn run_on AMREX_DEFAULT_RUNON>
848 [[nodiscard]] T sum (const Box& subbox, int comp, int numcomp = 1) const noexcept;
849
851 template <RunOn run_on AMREX_DEFAULT_RUNON>
852 BaseFab<T>& invert (T const& r, const Box& b, int comp=0, int numcomp=1) noexcept;
854 template <RunOn run_on AMREX_DEFAULT_RUNON>
855 BaseFab<T>& invert (T const& r, int comp, int numcomp=1) noexcept;
856
858 template <RunOn run_on AMREX_DEFAULT_RUNON>
859 BaseFab<T>& negate (const Box& b, int comp=0, int numcomp=1) noexcept;
861 template <RunOn run_on AMREX_DEFAULT_RUNON>
862 BaseFab<T>& negate (int comp, int numcomp=1) noexcept;
863
865 template <RunOn run_on AMREX_DEFAULT_RUNON>
866 BaseFab<T>& plus (T const& r, const Box& b, int comp=0, int numcomp=1) noexcept;
867
869 template <RunOn run_on AMREX_DEFAULT_RUNON>
870 BaseFab<T>& plus (T const& r, int comp, int numcomp=1) noexcept;
871
877 template <RunOn run_on AMREX_DEFAULT_RUNON>
878 BaseFab<T>& plus (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
884 template <RunOn run_on AMREX_DEFAULT_RUNON>
885 BaseFab<T>& plus (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp, int numcomp=1) noexcept;
890 template <RunOn run_on AMREX_DEFAULT_RUNON>
891 BaseFab<T>& plus (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
892 int srccomp, int destcomp, int numcomp=1) noexcept;
893
895 template <RunOn run_on AMREX_DEFAULT_RUNON>
896 BaseFab<T>& atomicAdd (const BaseFab<T>& x) noexcept;
897
903 template <RunOn run_on AMREX_DEFAULT_RUNON>
904 BaseFab<T>& atomicAdd (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
910 template <RunOn run_on AMREX_DEFAULT_RUNON>
911 BaseFab<T>& atomicAdd (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
912 int numcomp=1) noexcept;
917 template <RunOn run_on AMREX_DEFAULT_RUNON>
918 BaseFab<T>& atomicAdd (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
919 int srccomp, int destcomp, int numcomp=1) noexcept;
920
926 template <RunOn run_on AMREX_DEFAULT_RUNON>
927 BaseFab<T>& lockAdd (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
928 int srccomp, int destcomp, int numcomp) noexcept;
929
931 template <RunOn run_on AMREX_DEFAULT_RUNON>
932 BaseFab<T>& saxpy (T a, const BaseFab<T>& x, const Box& srcbox, const Box& destbox,
933 int srccomp, int destcomp, int numcomp=1) noexcept;
935 template <RunOn run_on AMREX_DEFAULT_RUNON>
936 BaseFab<T>& saxpy (T a, const BaseFab<T>& x) noexcept;
937
939 template <RunOn run_on AMREX_DEFAULT_RUNON>
940 BaseFab<T>& xpay (T a, const BaseFab<T>& x, const Box& srcbox, const Box& destbox,
941 int srccomp, int destcomp, int numcomp=1) noexcept;
942
944 template <RunOn run_on AMREX_DEFAULT_RUNON>
945 BaseFab<T>& addproduct (const Box& destbox, int destcomp, int numcomp,
946 const BaseFab<T>& src1, int comp1,
947 const BaseFab<T>& src2, int comp2) noexcept;
948
954 template <RunOn run_on AMREX_DEFAULT_RUNON>
955 BaseFab<T>& minus (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
961 template <RunOn run_on AMREX_DEFAULT_RUNON>
962 BaseFab<T>& minus (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
963 int numcomp=1) noexcept;
968 template <RunOn run_on AMREX_DEFAULT_RUNON>
969 BaseFab<T>& minus (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
970 int srccomp, int destcomp, int numcomp=1) noexcept;
971
973 template <RunOn run_on AMREX_DEFAULT_RUNON>
974 BaseFab<T>& mult (T const& r, int comp, int numcomp=1) noexcept;
976 template <RunOn run_on AMREX_DEFAULT_RUNON>
977 BaseFab<T>& mult (T const& r, const Box& b, int comp=0, int numcomp=1) noexcept;
978
984 template <RunOn run_on AMREX_DEFAULT_RUNON>
985 BaseFab<T>& mult (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
986
992 template <RunOn run_on AMREX_DEFAULT_RUNON>
993 BaseFab<T>& mult (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
994 int numcomp=1) noexcept;
995
1000 template <RunOn run_on AMREX_DEFAULT_RUNON>
1001 BaseFab<T>& mult (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
1002 int srccomp, int destcomp, int numcomp=1) noexcept;
1003
1005 template <RunOn run_on AMREX_DEFAULT_RUNON>
1006 BaseFab<T>& divide (T const& r, int comp, int numcomp=1) noexcept;
1007
1009 template <RunOn run_on AMREX_DEFAULT_RUNON>
1010 BaseFab<T>& divide (T const& r, const Box& b, int comp=0, int numcomp=1) noexcept;
1011
1018 template <RunOn run_on AMREX_DEFAULT_RUNON>
1019 BaseFab<T>& divide (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
1025 template <RunOn run_on AMREX_DEFAULT_RUNON>
1026 BaseFab<T>& divide (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
1027 int numcomp=1) noexcept;
1032 template <RunOn run_on AMREX_DEFAULT_RUNON>
1033 BaseFab<T>& divide (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
1034 int srccomp, int destcomp, int numcomp=1) noexcept;
1038 template <RunOn run_on AMREX_DEFAULT_RUNON>
1039 BaseFab<T>& protected_divide (const BaseFab<T>& src) noexcept;
1040
1048 template <RunOn run_on AMREX_DEFAULT_RUNON>
1049 BaseFab<T>& protected_divide (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp=1) noexcept;
1050
1057 template <RunOn run_on AMREX_DEFAULT_RUNON>
1058 BaseFab<T>& protected_divide (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
1059 int numcomp=1) noexcept;
1060
1066 template <RunOn run_on AMREX_DEFAULT_RUNON>
1067 BaseFab<T>& protected_divide (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
1068 int srccomp, int destcomp, int numcomp=1) noexcept;
1069
1080 template <RunOn run_on AMREX_DEFAULT_RUNON>
1081 BaseFab<T>& linInterp (const BaseFab<T>& f1, const Box& b1, int comp1,
1082 const BaseFab<T>& f2, const Box& b2, int comp2,
1083 Real t1, Real t2, Real t,
1084 const Box& b, int comp, int numcomp = 1) noexcept;
1085
1087 template <RunOn run_on AMREX_DEFAULT_RUNON>
1088 BaseFab<T>& linInterp (const BaseFab<T>& f1, int comp1,
1089 const BaseFab<T>& f2, int comp2,
1090 Real t1, Real t2, Real t,
1091 const Box& b, int comp, int numcomp = 1) noexcept;
1092
1102 template <RunOn run_on AMREX_DEFAULT_RUNON>
1103 BaseFab<T>& linComb (const BaseFab<T>& f1, const Box& b1, int comp1,
1104 const BaseFab<T>& f2, const Box& b2, int comp2,
1105 Real alpha, Real beta, const Box& b,
1106 int comp, int numcomp = 1) noexcept;
1107
1109 template <RunOn run_on AMREX_DEFAULT_RUNON>
1110 [[nodiscard]] T dot (const Box& xbx, int xcomp, const BaseFab<T>& y, const Box& ybx, int ycomp,
1111 int numcomp = 1) const noexcept;
1112
1113 template <RunOn run_on AMREX_DEFAULT_RUNON>
1114 [[nodiscard]] T dotmask (const BaseFab<int>& mask, const Box& xbx, int xcomp,
1115 const BaseFab<T>& y, const Box& ybx, int ycomp,
1116 int numcomp) const noexcept;
1117
1119 void SetBoxType (const IndexType& typ) noexcept { this->domain.setType(typ); }
1120
1121 //
1122 // New interfaces
1123 //
1124
1126 template <RunOn run_on AMREX_DEFAULT_RUNON>
1127 void setVal (T const& val) noexcept;
1128 //
1130 template <RunOn run_on AMREX_DEFAULT_RUNON>
1131 void setVal (T const& x, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1132
1133 template <RunOn run_on AMREX_DEFAULT_RUNON>
1134 void setValIf (T const& val, const BaseFab<int>& mask) noexcept;
1135 //
1137 template <RunOn run_on AMREX_DEFAULT_RUNON>
1138 void setValIf (T const& val, Box const& bx, const BaseFab<int>& mask, DestComp dcomp, NumComps ncomp) noexcept;
1139
1140 template <RunOn run_on AMREX_DEFAULT_RUNON>
1141 void setValIfNot (T const& val, const BaseFab<int>& mask) noexcept;
1142 //
1144 template <RunOn run_on AMREX_DEFAULT_RUNON>
1145 void setValIfNot (T const& val, Box const& bx, const BaseFab<int>& mask, DestComp dcomp, NumComps ncomp) noexcept;
1146
1148 template <RunOn run_on AMREX_DEFAULT_RUNON>
1149 void setComplement (T const& x, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1150
1156 template <RunOn run_on AMREX_DEFAULT_RUNON>
1157 BaseFab<T>& copy (const BaseFab<T>& src) noexcept;
1158 //
1160 template <RunOn run_on AMREX_DEFAULT_RUNON>
1161 BaseFab<T>& copy (const BaseFab<T>& src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept;
1162
1164 template <RunOn run_on AMREX_DEFAULT_RUNON>
1165 BaseFab<T>& plus (T const& val) noexcept;
1166 //
1167 template <RunOn run_on AMREX_DEFAULT_RUNON>
1168 BaseFab<T>& operator+= (T const& val) noexcept;
1169 //
1171 template <RunOn run_on AMREX_DEFAULT_RUNON>
1172 BaseFab<T>& plus (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1178 template <RunOn run_on AMREX_DEFAULT_RUNON>
1179 BaseFab<T>& plus (const BaseFab<T>& src) noexcept;
1180 //
1181 template <RunOn run_on AMREX_DEFAULT_RUNON>
1182 BaseFab<T>& operator+= (const BaseFab<T>& src) noexcept;
1183 //
1185 template <RunOn run_on AMREX_DEFAULT_RUNON>
1186 BaseFab<T>& plus (const BaseFab<T>& src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept;
1187
1189 template <RunOn run_on AMREX_DEFAULT_RUNON>
1190 BaseFab<T>& minus (T const& val) noexcept;
1191 //
1192 template <RunOn run_on AMREX_DEFAULT_RUNON>
1193 BaseFab<T>& operator-= (T const& val) noexcept;
1194 //
1196 template <RunOn run_on AMREX_DEFAULT_RUNON>
1197 BaseFab<T>& minus (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1203 template <RunOn run_on AMREX_DEFAULT_RUNON>
1204 BaseFab<T>& minus (const BaseFab<T>& src) noexcept;
1205 //
1206 template <RunOn run_on AMREX_DEFAULT_RUNON>
1207 BaseFab<T>& operator-= (const BaseFab<T>& src) noexcept;
1208 //
1210 template <RunOn run_on AMREX_DEFAULT_RUNON>
1211 BaseFab<T>& minus (const BaseFab<T>& src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept;
1212
1214 template <RunOn run_on AMREX_DEFAULT_RUNON>
1215 BaseFab<T>& mult (T const& val) noexcept;
1216 //
1217 template <RunOn run_on AMREX_DEFAULT_RUNON>
1218 BaseFab<T>& operator*= (T const& val) noexcept;
1219 //
1221 template <RunOn run_on AMREX_DEFAULT_RUNON>
1222 BaseFab<T>& mult (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1228 template <RunOn run_on AMREX_DEFAULT_RUNON>
1229 BaseFab<T>& mult (const BaseFab<T>& src) noexcept;
1230 //
1231 template <RunOn run_on AMREX_DEFAULT_RUNON>
1232 BaseFab<T>& operator*= (const BaseFab<T>& src) noexcept;
1233 //
1235 template <RunOn run_on AMREX_DEFAULT_RUNON>
1236 BaseFab<T>& mult (const BaseFab<T>& src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept;
1237
1239 template <RunOn run_on AMREX_DEFAULT_RUNON>
1240 BaseFab<T>& divide (T const& val) noexcept;
1241 //
1242 template <RunOn run_on AMREX_DEFAULT_RUNON>
1243 BaseFab<T>& operator/= (T const& val) noexcept;
1244 //
1246 template <RunOn run_on AMREX_DEFAULT_RUNON>
1247 BaseFab<T>& divide (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept;
1253 template <RunOn run_on AMREX_DEFAULT_RUNON>
1254 BaseFab<T>& divide (const BaseFab<T>& src) noexcept;
1255 //
1256 template <RunOn run_on AMREX_DEFAULT_RUNON>
1257 BaseFab<T>& operator/= (const BaseFab<T>& src) noexcept;
1258 //
1260 template <RunOn run_on AMREX_DEFAULT_RUNON>
1261 BaseFab<T>& divide (const BaseFab<T>& src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept;
1262
1264 template <RunOn run_on AMREX_DEFAULT_RUNON>
1265 BaseFab<T>& negate () noexcept;
1266 //
1267 template <RunOn run_on AMREX_DEFAULT_RUNON>
1268 BaseFab<T>& negate (const Box& bx, DestComp dcomp, NumComps ncomp) noexcept;
1269
1271 template <RunOn run_on AMREX_DEFAULT_RUNON>
1272 BaseFab<T>& invert (T const& r) noexcept;
1273 //
1274 template <RunOn run_on AMREX_DEFAULT_RUNON>
1275 BaseFab<T>& invert (T const& r, const Box& bx, DestComp dcomp, NumComps ncomp) noexcept;
1276
1278 template <RunOn run_on AMREX_DEFAULT_RUNON>
1279 [[nodiscard]] T sum (const Box& bx, DestComp dcomp, NumComps ncomp) const noexcept;
1280
1282 template <RunOn run_on AMREX_DEFAULT_RUNON>
1283 [[nodiscard]] T dot (const BaseFab<T>& src, const Box& bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) const noexcept;
1284
1286 template <RunOn run_on AMREX_DEFAULT_RUNON>
1287 [[nodiscard]] T dot (const Box& bx, int destcomp, int numcomp) const noexcept;
1288
1290 template <RunOn run_on AMREX_DEFAULT_RUNON>
1291 [[nodiscard]] T dot (const Box& bx, DestComp dcomp, NumComps ncomp) const noexcept;
1292
1294 template <RunOn run_on AMREX_DEFAULT_RUNON>
1295 [[nodiscard]] T dotmask (const BaseFab<T>& src, const Box& bx, const BaseFab<int>& mask,
1296 SrcComp scomp, DestComp dcomp, NumComps ncomp) const noexcept;
1297
1298protected:
1300 void define ();
1301
1302 T* dptr = nullptr;
1304 int nvar = 0;
1306 bool ptr_owner = false;
1307 bool shared_memory = false;
1308#ifdef AMREX_USE_GPU
1310#endif
1311};
1312
1313template <class T>
1315T*
1316BaseFab<T>::dataPtr (const IntVect& p, int n) noexcept
1317{
1318 AMREX_ASSERT(n >= 0);
1319 AMREX_ASSERT(n < this->nvar);
1320 AMREX_ASSERT(!(this->dptr == nullptr));
1321 AMREX_ASSERT(this->domain.contains(p));
1322
1323 return this->dptr + (this->domain.index(p)+n*this->domain.numPts());
1324}
1325
1326template <class T>
1328const T*
1329BaseFab<T>::dataPtr (const IntVect& p, int n) const noexcept
1330{
1331 AMREX_ASSERT(n >= 0);
1332 AMREX_ASSERT(n < this->nvar);
1333 AMREX_ASSERT(!(this->dptr == nullptr));
1334 AMREX_ASSERT(this->domain.contains(p));
1335
1336 return this->dptr + (this->domain.index(p)+n*this->domain.numPts());
1337}
1338
1339template <class T>
1340void
1342{
1343#ifdef AMREX_USE_GPU
1344 if (this->arena()->isManaged()) {
1345#if defined(AMREX_USE_SYCL)
1346 // xxxxx SYCL todo: prefetchToHost
1347 // std::size_t s = sizeof(T)*this->nvar*this->domain.numPts();
1348 // auto& q = Gpu::Device::streamQueue();
1349 // q.submit([&] (sycl::handler& h) { h.prefetch(this->dptr, s); });
1350#elif defined(AMREX_USE_CUDA) && !defined(_WIN32)
1351 if (Gpu::Device::devicePropMajor() >= 6) {
1352 std::size_t s = sizeof(T)*this->nvar*this->domain.numPts();
1353#if defined(CUDART_VERSION) && (CUDART_VERSION >= 13000)
1354 cudaMemLocation location = {};
1355 location.type = cudaMemLocationTypeHost;
1356 AMREX_CUDA_SAFE_CALL(cudaMemPrefetchAsync(this->dptr, s, location, 0,
1357 Gpu::gpuStream()));
1358#else
1359 AMREX_CUDA_SAFE_CALL(cudaMemPrefetchAsync(this->dptr, s,
1360 cudaCpuDeviceId,
1361 Gpu::gpuStream()));
1362#endif
1363 }
1364#elif defined(AMREX_USE_HIP)
1365 // xxxxx HIP FIX HERE after managed memory is supported
1366#endif
1367 }
1368#endif
1369}
1370
1371template <class T>
1372void
1374{
1375#ifdef AMREX_USE_GPU
1376 if (this->arena()->isManaged()) {
1377#if defined(AMREX_USE_SYCL)
1378 std::size_t s = sizeof(T)*this->nvar*this->domain.numPts();
1379 auto& q = Gpu::Device::streamQueue();
1380 q.submit([&] (sycl::handler& h) { h.prefetch(this->dptr, s); });
1381#elif defined(AMREX_USE_CUDA) && !defined(_WIN32)
1382 if (Gpu::Device::devicePropMajor() >= 6) {
1383 std::size_t s = sizeof(T)*this->nvar*this->domain.numPts();
1384#if defined(CUDART_VERSION) && (CUDART_VERSION >= 13000)
1385 cudaMemLocation location = {};
1386 location.type = cudaMemLocationTypeDevice;
1387 location.id = Gpu::Device::deviceId();
1388 AMREX_CUDA_SAFE_CALL(cudaMemPrefetchAsync(this->dptr, s, location, 0,
1389 Gpu::gpuStream()));
1390#else
1391 AMREX_CUDA_SAFE_CALL(cudaMemPrefetchAsync(this->dptr, s,
1393 Gpu::gpuStream()));
1394#endif
1395 }
1396#elif defined(AMREX_USE_HIP)
1397 // xxxxx HIP FIX HERE after managed memory is supported
1398#endif
1399 }
1400#endif
1401}
1402
1403template <class T>
1405T&
1406BaseFab<T>::operator() (const IntVect& p, int n) noexcept
1407{
1408 AMREX_ASSERT(n >= 0);
1409 AMREX_ASSERT(n < this->nvar);
1410 AMREX_ASSERT(!(this->dptr == nullptr));
1411 AMREX_ASSERT(this->domain.contains(p));
1412
1413 return this->dptr[this->domain.index(p)+n*this->domain.numPts()];
1414}
1415
1416template <class T>
1418T&
1420{
1421 AMREX_ASSERT(!(this->dptr == nullptr));
1422 AMREX_ASSERT(this->domain.contains(p));
1423
1424 return this->dptr[this->domain.index(p)];
1425}
1426
1427template <class T>
1429const T&
1430BaseFab<T>::operator() (const IntVect& p, int n) const noexcept
1431{
1432 AMREX_ASSERT(n >= 0);
1433 AMREX_ASSERT(n < this->nvar);
1434 AMREX_ASSERT(!(this->dptr == nullptr));
1435 AMREX_ASSERT(this->domain.contains(p));
1436
1437 return this->dptr[this->domain.index(p)+n*this->domain.numPts()];
1438}
1439
1440template <class T>
1442const T&
1443BaseFab<T>::operator() (const IntVect& p) const noexcept
1444{
1445 AMREX_ASSERT(!(this->dptr == nullptr));
1446 AMREX_ASSERT(this->domain.contains(p));
1447
1448 return this->dptr[this->domain.index(p)];
1449}
1450
1451template <class T>
1452void
1454 const IntVect& pos,
1455 int n,
1456 int numcomp) const noexcept
1457{
1458 const Long loc = this->domain.index(pos);
1459 const Long sz = this->domain.numPts();
1460
1461 AMREX_ASSERT(!(this->dptr == nullptr));
1462 AMREX_ASSERT(n >= 0 && n + numcomp <= this->nvar);
1463
1464 for (int k = 0; k < numcomp; k++) {
1465 data[k] = this->dptr[loc+(n+k)*sz];
1466 }
1467}
1468
1469template <class T>
1470void
1472 const IntVect& pos) const noexcept
1473{
1474 getVal(data,pos,0,this->nvar);
1475}
1476
1477template <class T>
1479BaseFab<T>::shift (const IntVect& v) noexcept
1480{
1481 this->domain += v;
1482 return *this;
1483}
1484
1485template <class T>
1487BaseFab<T>::shift (int idir, int n_cell) noexcept
1488{
1489 this->domain.shift(idir,n_cell);
1490 return *this;
1491}
1492
1493template <class T>
1494BaseFab<T> &
1496{
1497 this->domain.shiftHalf(v);
1498 return *this;
1499}
1500
1501template <class T>
1502BaseFab<T> &
1503BaseFab<T>::shiftHalf (int idir, int n_cell) noexcept
1504{
1505 this->domain.shiftHalf(idir,n_cell);
1506 return *this;
1507}
1508
1509template <class T>
1510template <RunOn run_on>
1511requires (std::is_same_v<T,float> || std::is_same_v<T,double>)
1512void
1514{
1516}
1517
1518template <class T>
1519template <RunOn run_on>
1520void
1521BaseFab<T>::setVal (T const& x, const Box& bx, int n) noexcept
1522{
1523 this->setVal<run_on>(x, bx, DestComp{n}, NumComps{1});
1524}
1525
1526template <class T>
1527template <RunOn run_on>
1528void
1529BaseFab<T>::setVal (T const& x, int n) noexcept
1530{
1531 this->setVal<run_on>(x, this->domain, DestComp{n}, NumComps{1});
1532}
1533
1534template <class T>
1535template <RunOn run_on>
1536void
1537BaseFab<T>::setVal (T const& x, const Box& bx, int dcomp, int ncomp) noexcept
1538{
1539 this->setVal<run_on>(x, bx, DestComp{dcomp}, NumComps{ncomp});
1540}
1541
1542template <class T>
1543template <RunOn run_on>
1544void
1545BaseFab<T>::setValIfNot (T const& val, const Box& bx, const BaseFab<int>& mask, int ns, int num) noexcept
1546{
1547 this->setValIfNot<run_on>(val, bx, mask, DestComp{ns}, NumComps{num});
1548}
1549
1550template <class T>
1551template <RunOn run_on>
1553BaseFab<T>::copy (const BaseFab<T>& src, const Box& srcbox, int srccomp,
1554 const Box& destbox, int destcomp, int numcomp) noexcept
1555{
1556 AMREX_ASSERT(destbox.ok());
1557 AMREX_ASSERT(srcbox.sameSize(destbox));
1558 AMREX_ASSERT(src.box().contains(srcbox));
1559 AMREX_ASSERT(this->domain.contains(destbox));
1560 AMREX_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
1561 AMREX_ASSERT(destcomp >= 0 && destcomp+numcomp <= this->nvar);
1562
1563 Array4<T> const& d = this->array();
1564 Array4<T const> const& s = src.const_array();
1565 const auto dlo = amrex::lbound(destbox);
1566 const auto slo = amrex::lbound(srcbox);
1567 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
1568
1569 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
1570 {
1571 d(i,j,k,n+destcomp) = s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
1572 });
1573
1574 return *this;
1575}
1576
1577template <class T>
1578template <RunOn run_on>
1580BaseFab<T>::copy (const BaseFab<T>& src, const Box& destbox) noexcept
1581{
1582 return this->copy<run_on>(src, destbox, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
1583}
1584
1585template <class T>
1586template <RunOn run_on>
1588BaseFab<T>::copy (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
1589{
1590 return copy<run_on>(src, this->domain, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
1591}
1592
1593template <class T>
1594void
1596{
1597 AMREX_ASSERT(this->dptr == nullptr);
1598 AMREX_ASSERT(this->domain.numPts() > 0);
1599 AMREX_ASSERT(this->nvar >= 0);
1600 if (this->nvar == 0) { return; }
1601 AMREX_ASSERT(std::numeric_limits<Long>::max()/this->nvar > this->domain.numPts());
1602 detail::check_array4_index_range(this->domain);
1603
1604 // set the members after alloc, which may throw
1605 const Long n = this->nvar*this->domain.numPts();
1606 this->dptr = static_cast<T*>(this->alloc(n*sizeof(T)));
1607 this->truesize = n;
1608 this->ptr_owner = true;
1609#ifdef AMREX_USE_GPU
1610 this->alloc_stream = Gpu::gpuStream();
1611#endif
1612
1613 placementNew(this->dptr, this->truesize,
1614 arena()->isDeviceAccessible() ? RunOn::Device : RunOn::Host);
1615
1616 amrex::update_fab_stats(this->domain.numPts(), this->truesize, sizeof(T));
1617
1618 if constexpr (std::is_same_v<T,float> || std::is_same_v<T,double>) {
1619 if (amrex::InitSNaN() && this->truesize > 0) {
1620#ifdef AMREX_USE_GPU
1621 if (Gpu::inLaunchRegion() && arena()->isDeviceAccessible()) {
1622 this->template fill_snan<RunOn::Device>();
1624 } else
1625#endif
1626 {
1627 this->template fill_snan<RunOn::Host>();
1628 }
1629 }
1630 }
1631}
1632
1633template <class T>
1635 : DataAllocator{ar}
1636{}
1637
1638template <class T>
1639BaseFab<T>::BaseFab (const Box& bx, int n, Arena* ar)
1640 : DataAllocator{ar}, domain(bx), nvar(n)
1641{
1642 define();
1643}
1644
1645template <class T>
1646BaseFab<T>::BaseFab (const Box& bx, int n, bool alloc, bool shared, Arena* ar)
1647 : DataAllocator{ar}, domain(bx), nvar(n), shared_memory(shared)
1648{
1649 if (!this->shared_memory && alloc) { define(); }
1650}
1651
1652template <class T>
1653BaseFab<T>::BaseFab (const BaseFab<T>& rhs, MakeType make_type, int scomp, int ncomp)
1654 : DataAllocator{rhs.arena()},
1655 dptr(const_cast<T*>(rhs.dataPtr(scomp))),
1656 domain(rhs.domain), nvar(ncomp),
1657 truesize(ncomp*rhs.domain.numPts())
1658{
1659 AMREX_ASSERT(scomp+ncomp <= rhs.nComp());
1660 if (make_type == amrex::make_deep_copy)
1661 {
1662 this->dptr = nullptr;
1663 define();
1664#ifdef AMREX_USE_GPU
1665 if (Gpu::inLaunchRegion() && arena()->isDeviceAccessible()) {
1666 this->copy<RunOn::Device>(rhs, this->domain, scomp, this->domain, 0, ncomp);
1667 } else
1668#endif
1669 {
1670 this->copy<RunOn::Host>(rhs, this->domain, scomp, this->domain, 0, ncomp);
1671 }
1672 } else if (make_type == amrex::make_alias) {
1673 ; // nothing to do
1674 } else {
1675 amrex::Abort("BaseFab: unknown MakeType");
1676 }
1677}
1678
1679template<class T>
1680BaseFab<T>::BaseFab (const Box& bx, int ncomp, T* p)
1681 : dptr(p), domain(bx), nvar(ncomp), truesize(bx.numPts()*ncomp)
1682{
1683 detail::check_array4_index_range(bx);
1684}
1685
1686template<class T>
1687BaseFab<T>::BaseFab (const Box& bx, int ncomp, T const* p)
1688 : dptr(const_cast<T*>(p)), domain(bx), nvar(ncomp), truesize(bx.numPts()*ncomp)
1689{
1690 detail::check_array4_index_range(bx);
1691}
1692
1693template<class T>
1695 : dptr(a.p),
1696 domain(IntVect(AMREX_D_DECL(a.begin[0],a.begin[1],a.begin[2])),
1697 IntVect(AMREX_D_DECL(a.end[0]-1,a.end[1]-1,a.end[2]-1))),
1698 nvar(a.nComp()), truesize(a.size())
1699{}
1700
1701template<class T>
1703 : dptr(a.p),
1704 domain(IntVect(AMREX_D_DECL(a.begin[0],a.begin[1],a.begin[2])),
1705 IntVect(AMREX_D_DECL(a.end[0]-1,a.end[1]-1,a.end[2]-1)), t),
1706 nvar(a.nComp()), truesize(a.size())
1707{}
1708
1709template<class T>
1711 : dptr(const_cast<T*>(a.p)),
1712 domain(IntVect(AMREX_D_DECL(a.begin[0],a.begin[1],a.begin[2])),
1713 IntVect(AMREX_D_DECL(a.end[0]-1,a.end[1]-1,a.end[2]-1))),
1714 nvar(a.nComp()), truesize(a.size())
1715{}
1716
1717template<class T>
1719 : dptr(const_cast<T*>(a.p)),
1720 domain(IntVect(AMREX_D_DECL(a.begin[0],a.begin[1],a.begin[2])),
1721 IntVect(AMREX_D_DECL(a.end[0]-1,a.end[1]-1,a.end[2]-1)), t),
1722 nvar(a.nComp()), truesize(a.size())
1723{}
1724
1725template <class T>
1727{
1728 clear();
1729}
1730
1731template <class T>
1733 : DataAllocator{rhs.arena()},
1734 dptr(rhs.dptr), domain(rhs.domain),
1735 nvar(rhs.nvar), truesize(rhs.truesize),
1736 ptr_owner(rhs.ptr_owner), shared_memory(rhs.shared_memory)
1737#ifdef AMREX_USE_GPU
1738 , alloc_stream(rhs.alloc_stream)
1739#endif
1740{
1741 rhs.dptr = nullptr;
1742 rhs.ptr_owner = false;
1743}
1744
1745template <class T>
1746BaseFab<T>&
1748{
1749 if (this != &rhs) {
1750 clear();
1751 DataAllocator::operator=(rhs);
1752 dptr = rhs.dptr;
1753 domain = rhs.domain;
1754 nvar = rhs.nvar;
1755 truesize = rhs.truesize;
1756 ptr_owner = rhs.ptr_owner;
1757 shared_memory = rhs.shared_memory;
1758#ifdef AMREX_USE_GPU
1759 alloc_stream = rhs.alloc_stream;
1760#endif
1761
1762 rhs.dptr = nullptr;
1763 rhs.ptr_owner = false;
1764 }
1765 return *this;
1766}
1767
1768template <class T>
1769template <RunOn run_on>
1771BaseFab<T>::operator= (T const& t) noexcept
1772{
1773 setVal<run_on>(t);
1774 return *this;
1775}
1776
1777template <class T>
1778void
1779BaseFab<T>::resize (const Box& b, int n, Arena* ar)
1780{
1781 // clear() subtracts truesize/nvar cells, so fix the count for the new nvar.
1782 // Lower it first and raise it last so reallocation does not inflate the HWM.
1783 Long dcells = 0;
1784 if (this->dptr && this->ptr_owner && n != this->nvar) {
1785 Long const old_cells = (this->nvar > 0) ? this->truesize/this->nvar : 0;
1786 Long const new_cells = (n > 0) ? this->truesize/n : 0;
1787 dcells = new_cells - old_cells;
1788 }
1789 if (dcells < 0) {
1790 amrex::update_fab_stats(dcells, 0, sizeof(T));
1791 }
1792
1793 this->nvar = n;
1794 this->domain = b;
1795
1796 if (ar == nullptr) {
1797 ar = m_arena;
1798 }
1799
1800 detail::check_array4_index_range(b);
1801
1802 if (arena() != DataAllocator(ar).arena()) {
1803 clear();
1804 m_arena = ar;
1805 define();
1806 }
1807 else if (this->dptr == nullptr || !this->ptr_owner)
1808 {
1809 if (this->shared_memory) {
1810 amrex::Abort("BaseFab::resize: BaseFab in shared memory cannot increase size");
1811 }
1812
1813 this->dptr = nullptr;
1814 define();
1815 }
1816 else if (this->nvar*this->domain.numPts() > this->truesize
1817#ifdef AMREX_USE_GPU
1818 || (arena()->isStreamOrderedArena() && alloc_stream != Gpu::gpuStream())
1819#endif
1820 )
1821 {
1822 if (this->shared_memory) {
1823 amrex::Abort("BaseFab::resize: BaseFab in shared memory cannot increase size");
1824 }
1825
1826 clear();
1827
1828 define();
1829 }
1830
1831 if (dcells > 0) {
1832 amrex::update_fab_stats(dcells, 0, sizeof(T));
1833 }
1834}
1835
1836template <class T>
1837template <class U>
1838requires (std::is_trivially_destructible_v<U>)
1839Elixir
1841{
1842 bool o;
1843 if (Gpu::inLaunchRegion()) {
1844 o = this->ptr_owner;
1845 this->ptr_owner = false;
1846 if (o && this->dptr) {
1847 if (this->nvar > 0) {
1848 amrex::update_fab_stats(-this->truesize/this->nvar, -this->truesize, sizeof(T));
1849 } else {
1850 amrex::update_fab_stats(0, -this->truesize, sizeof(T));
1851 }
1852 }
1853 } else {
1854 o = false;
1855 }
1856 return Elixir((o ? this->dptr : nullptr), this->arena());
1857}
1858
1859template <class T>
1860void
1862{
1863 if (this->dptr)
1864 {
1865 //
1866 // Call T::~T() on the to-be-destroyed memory.
1867 //
1868 if (this->ptr_owner)
1869 {
1870 if (this->shared_memory)
1871 {
1872 amrex::Abort("BaseFab::clear: BaseFab cannot be owner of shared memory");
1873 }
1874
1875 placementDelete(this->dptr, this->truesize,
1876 this->arena()->isDeviceAccessible() ? RunOn::Device : RunOn::Host);
1877
1878#ifdef AMREX_USE_GPU
1879 this->arena()->streamOrderedFree(this->dptr, alloc_stream);
1880#else
1881 this->free(this->dptr);
1882#endif
1883
1884 if (this->nvar > 0) {
1885 amrex::update_fab_stats(-this->truesize/this->nvar, -this->truesize, sizeof(T));
1886 } else {
1887 amrex::update_fab_stats(0, -this->truesize, sizeof(T));
1888 }
1889 }
1890
1891 this->dptr = nullptr;
1892 this->truesize = 0;
1893 }
1894}
1895
1896template <class T>
1897std::unique_ptr<T,DataDeleter>
1899{
1900 std::unique_ptr<T,DataDeleter> r(nullptr, DataDeleter{this->arena()});
1901 if (this->dptr && this->ptr_owner) {
1902 r.reset(this->dptr);
1903 this->ptr_owner = false;
1904 if (this->nvar > 0) {
1905 amrex::update_fab_stats(-this->truesize/this->nvar, -this->truesize, sizeof(T));
1906 } else {
1907 amrex::update_fab_stats(0, -this->truesize, sizeof(T));
1908 }
1909 }
1910 return r;
1911}
1912
1913template <class T>
1914template <RunOn run_on>
1915std::size_t
1917 int srccomp,
1918 int numcomp,
1919 void* dst) const noexcept
1920{
1921 BL_ASSERT(box().contains(srcbox));
1922 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= nComp());
1923
1924 if (srcbox.ok())
1925 {
1926 Array4<T> d(static_cast<T*>(dst),amrex::begin(srcbox),amrex::end(srcbox),numcomp);
1927 Array4<T const> const& s = this->const_array();
1928 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, srcbox, numcomp, i, j, k, n,
1929 {
1930 d(i,j,k,n) = s(i,j,k,n+srccomp);
1931 });
1932 return sizeof(T)*d.size();
1933 }
1934 else
1935 {
1936 return 0;
1937 }
1938}
1939
1940template <class T>
1941template <RunOn run_on, typename BUF>
1942std::size_t
1944 int dstcomp,
1945 int numcomp,
1946 const void* src) noexcept
1947{
1948 BL_ASSERT(box().contains(dstbox));
1949 BL_ASSERT(dstcomp >= 0 && dstcomp+numcomp <= nComp());
1950
1951 if (dstbox.ok())
1952 {
1953 Array4<BUF const> s(static_cast<BUF const*>(src), amrex::begin(dstbox),
1954 amrex::end(dstbox), numcomp);
1955 Array4<T> const& d = this->array();
1956 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, dstbox, numcomp, i, j, k, n,
1957 {
1958 d(i,j,k,n+dstcomp) = static_cast<T>(s(i,j,k,n));
1959 });
1960 return sizeof(BUF)*s.size();
1961 }
1962 else
1963 {
1964 return 0;
1965 }
1966}
1967
1968template <class T>
1969template <RunOn run_on, typename BUF>
1970std::size_t
1972 int dstcomp,
1973 int numcomp,
1974 const void* src) noexcept
1975{
1976 BL_ASSERT(box().contains(dstbox));
1977 BL_ASSERT(dstcomp >= 0 && dstcomp+numcomp <= nComp());
1978
1979 if (dstbox.ok())
1980 {
1981 Array4<BUF const> s(static_cast<BUF const*>(src), amrex::begin(dstbox),
1982 amrex::end(dstbox), numcomp);
1983 Array4<T> const& d = this->array();
1984 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, dstbox, numcomp, i, j, k, n,
1985 {
1986 d(i,j,k,n+dstcomp) += static_cast<T>(s(i,j,k,n));
1987 });
1988 return sizeof(BUF)*s.size();
1989 }
1990 else
1991 {
1992 return 0;
1993 }
1994}
1995
1996template <class T>
1997template <RunOn run_on>
1998void
1999BaseFab<T>::setComplement (T const& x, const Box& b, int ns, int num) noexcept
2000{
2001 this->setComplement<run_on>(x, b, DestComp{ns}, NumComps{num});
2002}
2003
2004template <class T>
2005template <RunOn run_on>
2006void
2008{
2009 this->abs<run_on>(this->domain,0,this->nvar);
2010}
2011
2012template <class T>
2013template <RunOn run_on>
2014void
2015BaseFab<T>::abs (int comp, int numcomp) noexcept
2016{
2017 this->abs<run_on>(this->domain,comp,numcomp);
2018}
2019
2020template <class T>
2021template <RunOn run_on>
2022void
2023BaseFab<T>::abs (const Box& subbox, int comp, int numcomp) noexcept
2024{
2025 Array4<T> const& a = this->array();
2026 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG (run_on, subbox, numcomp, i, j, k, n,
2027 {
2028 a(i,j,k,n+comp) = std::abs(a(i,j,k,n+comp));
2029 });
2030}
2031
2032namespace detail {
2033
2034// CPU reduction of f(i,j,k,n) over bx and ncomp components with the
2035// partial accumulators of Reduce::detail::reduce_row.
2036template <typename Op, typename T, typename F>
2038T basefab_reduce_cpu (Box const& bx, int ncomp, T const& init, F const& f) noexcept
2039{
2040 using Tup = GpuTuple<T>;
2041 Tup rem{init};
2042 Reduce::detail::Lanes<Tup> acc;
2043 Reduce::detail::lanes_set<0>(acc, rem);
2044 auto const lo = amrex::lbound(bx);
2045 auto const hi = amrex::ubound(bx);
2046 for (int n = 0; n < ncomp; ++n) {
2047 for (int k = lo.z; k <= hi.z; ++k) {
2048 for (int j = lo.y; j <= hi.y; ++j) {
2049 Reduce::detail::reduce_row<Op>(lo.x, hi.x, acc, rem,
2050 [&] (int i) { return Tup{static_cast<T>(f(i,j,k,n))}; });
2051 }}}
2052 Reduce::detail::lanes_fold<0,Tup,Op>(acc, rem);
2053 return amrex::get<0>(rem);
2054}
2055
2056}
2057
2058template <class T>
2059template <RunOn run_on>
2060Real
2061BaseFab<T>::norminfmask (const Box& subbox, const BaseFab<int>& mask,
2062 int scomp, int ncomp) const noexcept
2063{
2064 BL_ASSERT(this->domain.contains(subbox));
2065 BL_ASSERT(scomp >= 0 && scomp + ncomp <= this->nvar);
2066
2067 Array4<T const> const& a = this->const_array();
2068 Array4<int const> const& m = mask.const_array();
2069 Real r = 0.0;
2070#ifdef AMREX_USE_GPU
2071 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2072 ReduceOps<ReduceOpMax> reduce_op;
2073 ReduceData<Real> reduce_data(reduce_op);
2074 using ReduceTuple = ReduceData<Real>::Type;
2075 reduce_op.eval(subbox, reduce_data,
2076 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2077 {
2078 Real t = 0.0;
2079 if (m(i,j,k)) {
2080 for (int n = 0; n < ncomp; ++n) {
2081 t = amrex::max(t,static_cast<Real>(std::abs(a(i,j,k,n+scomp))));
2082 }
2083 }
2084 return {t};
2085 });
2086 ReduceTuple hv = reduce_data.value(reduce_op);
2087 r = amrex::get<0>(hv);
2088 } else
2089#endif
2090 {
2091 r = detail::basefab_reduce_cpu<ReduceOpMax>(subbox, ncomp, Real(0.0),
2092 [=] (int i, int j, int k, int n) noexcept
2093 {
2094 auto const t = a(i,j,k,n+scomp);
2095 return static_cast<Real>(m(i,j,k) ? std::abs(t) : T(0));
2096 });
2097 }
2098 return r;
2099}
2100
2101template <class T>
2102template <RunOn run_on>
2103Real
2104BaseFab<T>::norm (int p, int comp, int numcomp) const
2105{
2106 return norm<run_on>(this->domain,p,comp,numcomp);
2107}
2108
2109template <class T>
2110template <RunOn run_on>
2111Real
2112BaseFab<T>::norm (const Box& subbox, int p, int comp, int numcomp) const
2113{
2114 BL_ASSERT(this->domain.contains(subbox));
2115 BL_ASSERT(comp >= 0 && comp + numcomp <= this->nvar);
2116
2117 Array4<T const> const& a = this->const_array();
2118 Real nrm = 0.;
2119#ifdef AMREX_USE_GPU
2120 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2121 if (p == 0) {
2122 ReduceOps<ReduceOpMax> reduce_op;
2123 ReduceData<Real> reduce_data(reduce_op);
2124 using ReduceTuple = ReduceData<Real>::Type;
2125 reduce_op.eval(subbox, reduce_data,
2126 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2127 {
2128 Real t = 0.0;
2129 for (int n = 0; n < numcomp; ++n) {
2130 t = amrex::max(t, static_cast<Real>(std::abs(a(i,j,k,n+comp))));
2131 }
2132 return {t};
2133 });
2134 ReduceTuple hv = reduce_data.value(reduce_op);
2135 nrm = amrex::get<0>(hv);
2136 } else if (p == 1) {
2137 ReduceOps<ReduceOpSum> reduce_op;
2138 ReduceData<Real> reduce_data(reduce_op);
2139 using ReduceTuple = ReduceData<Real>::Type;
2140 reduce_op.eval(subbox, reduce_data,
2141 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2142 {
2143 Real t = 0.0;
2144 for (int n = 0; n < numcomp; ++n) {
2145 t += static_cast<Real>(std::abs(a(i,j,k,n+comp)));
2146 }
2147 return {t};
2148 });
2149 ReduceTuple hv = reduce_data.value(reduce_op);
2150 nrm = amrex::get<0>(hv);
2151 } else if (p == 2) {
2152 ReduceOps<ReduceOpSum> reduce_op;
2153 ReduceData<Real> reduce_data(reduce_op);
2154 using ReduceTuple = ReduceData<Real>::Type;
2155 reduce_op.eval(subbox, reduce_data,
2156 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2157 {
2158 Real t = 0.0;
2159 for (int n = 0; n < numcomp; ++n) {
2160 t += static_cast<Real>(a(i,j,k,n+comp)*a(i,j,k,n+comp));
2161 }
2162 return {t};
2163 });
2164 ReduceTuple hv = reduce_data.value(reduce_op);
2165 nrm = amrex::get<0>(hv);
2166 } else {
2167 amrex::Error("BaseFab<T>::norm: wrong p");
2168 }
2169 } else
2170#endif
2171 {
2172 if (p == 0) {
2173 nrm = detail::basefab_reduce_cpu<ReduceOpMax>(subbox, numcomp, Real(0.0),
2174 [=] (int i, int j, int k, int n) noexcept
2175 {
2176 return static_cast<Real>(std::abs(a(i,j,k,n+comp)));
2177 });
2178 } else if (p == 1) {
2179 nrm = detail::basefab_reduce_cpu<ReduceOpSum>(subbox, numcomp, Real(0.0),
2180 [=] (int i, int j, int k, int n) noexcept
2181 {
2182 return static_cast<Real>(std::abs(a(i,j,k,n+comp)));
2183 });
2184 } else if (p == 2) {
2185 nrm = detail::basefab_reduce_cpu<ReduceOpSum>(subbox, numcomp, Real(0.0),
2186 [=] (int i, int j, int k, int n) noexcept
2187 {
2188 return static_cast<Real>(a(i,j,k,n+comp)*a(i,j,k,n+comp));
2189 });
2190 } else {
2191 amrex::Error("BaseFab<T>::norm: wrong p");
2192 }
2193 }
2194
2195 return nrm;
2196}
2197
2198template <class T>
2199template <RunOn run_on>
2200T
2201BaseFab<T>::min (int comp) const noexcept
2202{
2203 return this->min<run_on>(this->domain,comp);
2204}
2205
2206template <class T>
2207template <RunOn run_on>
2208T
2209BaseFab<T>::min (const Box& subbox, int comp) const noexcept
2210{
2211 Array4<T const> const& a = this->const_array(comp);
2212#ifdef AMREX_USE_GPU
2213 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2214 ReduceOps<ReduceOpMin> reduce_op;
2215 ReduceData<T> reduce_data(reduce_op);
2216 using ReduceTuple = typename decltype(reduce_data)::Type;
2217 reduce_op.eval(subbox, reduce_data,
2218 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2219 {
2220 return { a(i,j,k) };
2221 });
2222 ReduceTuple hv = reduce_data.value(reduce_op);
2223 return amrex::get<0>(hv);
2224 } else
2225#endif
2226 {
2227 return detail::basefab_reduce_cpu<ReduceOpMin>(subbox, 1, std::numeric_limits<T>::max(),
2228 [=] (int i, int j, int k, int) noexcept
2229 {
2230 return a(i,j,k);
2231 });
2232 }
2233}
2234
2235template <class T>
2236template <RunOn run_on>
2237T
2238BaseFab<T>::max (int comp) const noexcept
2239{
2240 return this->max<run_on>(this->domain,comp);
2241}
2242
2243template <class T>
2244template <RunOn run_on>
2245T
2246BaseFab<T>::max (const Box& subbox, int comp) const noexcept
2247{
2248 Array4<T const> const& a = this->const_array(comp);
2249#ifdef AMREX_USE_GPU
2250 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2251 ReduceOps<ReduceOpMax> reduce_op;
2252 ReduceData<T> reduce_data(reduce_op);
2253 using ReduceTuple = typename decltype(reduce_data)::Type;
2254 reduce_op.eval(subbox, reduce_data,
2255 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2256 {
2257 return { a(i,j,k) };
2258 });
2259 ReduceTuple hv = reduce_data.value(reduce_op);
2260 return amrex::get<0>(hv);
2261 } else
2262#endif
2263 {
2264 return detail::basefab_reduce_cpu<ReduceOpMax>(subbox, 1, std::numeric_limits<T>::lowest(),
2265 [=] (int i, int j, int k, int) noexcept
2266 {
2267 return a(i,j,k);
2268 });
2269 }
2270}
2271
2272template <class T>
2273template <RunOn run_on>
2274std::pair<T,T>
2275BaseFab<T>::minmax (int comp) const noexcept
2276{
2277 return this->minmax<run_on>(this->domain,comp);
2278}
2279
2280template <class T>
2281template <RunOn run_on>
2282std::pair<T,T>
2283BaseFab<T>::minmax (const Box& subbox, int comp) const noexcept
2284{
2285 Array4<T const> const& a = this->const_array(comp);
2286#ifdef AMREX_USE_GPU
2287 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2289 ReduceData<T,T> reduce_data(reduce_op);
2290 using ReduceTuple = typename decltype(reduce_data)::Type;
2291 reduce_op.eval(subbox, reduce_data,
2292 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2293 {
2294 auto const x = a(i,j,k);
2295 return { x, x };
2296 });
2297 ReduceTuple hv = reduce_data.value(reduce_op);
2298 return std::make_pair(amrex::get<0>(hv), amrex::get<1>(hv));
2299 } else
2300#endif
2301 {
2302 using MM = GpuTuple<T,T>;
2303 MM rem{std::numeric_limits<T>::max(), std::numeric_limits<T>::lowest()};
2304 Reduce::detail::Lanes<MM> acc;
2305 Reduce::detail::lanes_set<0>(acc, rem);
2306 auto const lo = amrex::lbound(subbox);
2307 auto const hi = amrex::ubound(subbox);
2308 for (int k = lo.z; k <= hi.z; ++k) {
2309 for (int j = lo.y; j <= hi.y; ++j) {
2310 Reduce::detail::reduce_row<ReduceOpMin,ReduceOpMax>(lo.x, hi.x, acc, rem,
2311 [&] (int i) { auto const x = a(i,j,k); return MM{x,x}; });
2312 }}
2313 Reduce::detail::lanes_fold<0,MM,ReduceOpMin,ReduceOpMax>(acc, rem);
2314 return std::make_pair(amrex::get<0>(rem), amrex::get<1>(rem));
2315 }
2316}
2317
2318template <class T>
2319template <RunOn run_on>
2320T
2321BaseFab<T>::maxabs (int comp) const noexcept
2322{
2323 return this->maxabs<run_on>(this->domain,comp);
2324}
2325
2326template <class T>
2327template <RunOn run_on>
2328T
2329BaseFab<T>::maxabs (const Box& subbox, int comp) const noexcept
2330{
2331 Array4<T const> const& a = this->const_array(comp);
2332#ifdef AMREX_USE_GPU
2333 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2334 ReduceOps<ReduceOpMax> reduce_op;
2335 ReduceData<T> reduce_data(reduce_op);
2336 using ReduceTuple = typename decltype(reduce_data)::Type;
2337 reduce_op.eval(subbox, reduce_data,
2338 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2339 {
2340 return { std::abs(a(i,j,k)) };
2341 });
2342 ReduceTuple hv = reduce_data.value(reduce_op);
2343 return amrex::get<0>(hv);
2344 } else
2345#endif
2346 {
2347 return detail::basefab_reduce_cpu<ReduceOpMax>(subbox, 1, T(0),
2348 [=] (int i, int j, int k, int) noexcept
2349 {
2350 return std::abs(a(i,j,k));
2351 });
2352 }
2353}
2354
2355
2356template <class T>
2357template <RunOn run_on>
2358IntVect
2359BaseFab<T>::indexFromValue (Box const& subbox, int comp, T const& value) const noexcept
2360{
2361 Array4<T const> const& a = this->const_array(comp);
2362#ifdef AMREX_USE_GPU
2363 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2364 Array<int,1+AMREX_SPACEDIM> ha{0,AMREX_D_DECL(std::numeric_limits<int>::lowest(),
2365 std::numeric_limits<int>::lowest(),
2366 std::numeric_limits<int>::lowest())};
2367 Gpu::DeviceVector<int> dv(1+AMREX_SPACEDIM);
2368 int* p = dv.data();
2369 Gpu::htod_memcpy_async(p, ha.data(), sizeof(int)*ha.size());
2370 amrex::ParallelFor(subbox, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
2371 {
2372 int* flag = p;
2373 if ((*flag == 0) && (a(i,j,k) == value)) {
2374 if (Gpu::Atomic::Exch(flag,1) == 0) {
2375 AMREX_D_TERM(p[1] = i;,
2376 p[2] = j;,
2377 p[3] = k;);
2378 }
2379 }
2380 });
2381 Gpu::dtoh_memcpy_async(ha.data(), p, sizeof(int)*ha.size());
2383 return IntVect(AMREX_D_DECL(ha[1],ha[2],ha[3]));
2384 } else
2385#endif
2386 {
2387 AMREX_LOOP_3D(subbox, i, j, k,
2388 {
2389 if (a(i,j,k) == value) { return IntVect(AMREX_D_DECL(i,j,k)); }
2390 });
2391 return IntVect::TheMinVector();
2392 }
2393}
2394
2395template <class T>
2396template <RunOn run_on>
2397IntVect
2398BaseFab<T>::minIndex (int comp) const noexcept
2399{
2400 return this->minIndex<run_on>(this->domain,comp);
2401}
2402
2403template <class T>
2404template <RunOn run_on>
2405IntVect
2406BaseFab<T>::minIndex (const Box& subbox, int comp) const noexcept
2407{
2408 T min_val = this->min<run_on>(subbox, comp);
2409 return this->indexFromValue<run_on>(subbox, comp, min_val);
2410}
2411
2412template <class T>
2413template <RunOn run_on>
2414void
2415BaseFab<T>::minIndex (const Box& subbox, Real& min_val, IntVect& min_idx, int comp) const noexcept
2416{
2417 min_val = this->min<run_on>(subbox, comp);
2418 min_idx = this->indexFromValue<run_on>(subbox, comp, min_val);
2419}
2420
2421template <class T>
2422template <RunOn run_on>
2423IntVect
2424BaseFab<T>::maxIndex (int comp) const noexcept
2425{
2426 return this->maxIndex<run_on>(this->domain,comp);
2427}
2428
2429template <class T>
2430template <RunOn run_on>
2431IntVect
2432BaseFab<T>::maxIndex (const Box& subbox, int comp) const noexcept
2433{
2434 T max_val = this->max<run_on>(subbox, comp);
2435 return this->indexFromValue<run_on>(subbox, comp, max_val);
2436}
2437
2438template <class T>
2439template <RunOn run_on>
2440void
2441BaseFab<T>::maxIndex (const Box& subbox, Real& max_val, IntVect& max_idx, int comp) const noexcept
2442{
2443 max_val = this->max<run_on>(subbox, comp);
2444 max_idx = this->indexFromValue<run_on>(subbox, comp, max_val);
2445}
2446
2447template <class T>
2448template <RunOn run_on>
2451{
2452 Box ovlp(this->domain);
2453 ovlp &= x.domain;
2454 return ovlp.ok() ? this->atomicAdd<run_on>(x,ovlp,ovlp,0,0,this->nvar) : *this;
2455}
2456
2457template <class T>
2458template <RunOn run_on>
2460BaseFab<T>::saxpy (T a, const BaseFab<T>& x, const Box& srcbox, const Box& destbox,
2461 int srccomp, int destcomp, int numcomp) noexcept
2462{
2463 BL_ASSERT(srcbox.ok());
2464 BL_ASSERT(x.box().contains(srcbox));
2465 BL_ASSERT(destbox.ok());
2466 BL_ASSERT(box().contains(destbox));
2467 BL_ASSERT(destbox.sameSize(srcbox));
2468 BL_ASSERT( srccomp >= 0 && srccomp+numcomp <= x.nComp());
2469 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2470
2471 Array4<T> const& d = this->array();
2472 Array4<T const> const& s = x.const_array();
2473 const auto dlo = amrex::lbound(destbox);
2474 const auto slo = amrex::lbound(srcbox);
2475 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
2476 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
2477 {
2478 d(i,j,k,n+destcomp) += a * s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
2479 });
2480
2481 return *this;
2482}
2483
2484template <class T>
2485template <RunOn run_on>
2487BaseFab<T>::saxpy (T a, const BaseFab<T>& x) noexcept
2488{
2489 Box ovlp(this->domain);
2490 ovlp &= x.domain;
2491 return ovlp.ok() ? saxpy<run_on>(a,x,ovlp,ovlp,0,0,this->nvar) : *this;
2492}
2493
2494template <class T>
2495template <RunOn run_on>
2498 const Box& srcbox, const Box& destbox,
2499 int srccomp, int destcomp, int numcomp) noexcept
2500{
2501 BL_ASSERT(srcbox.ok());
2502 BL_ASSERT(x.box().contains(srcbox));
2503 BL_ASSERT(destbox.ok());
2504 BL_ASSERT(box().contains(destbox));
2505 BL_ASSERT(destbox.sameSize(srcbox));
2506 BL_ASSERT( srccomp >= 0 && srccomp+numcomp <= x.nComp());
2507 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2508
2509 Array4<T> const& d = this->array();
2510 Array4<T const> const& s = x.const_array();
2511 const auto dlo = amrex::lbound(destbox);
2512 const auto slo = amrex::lbound(srcbox);
2513 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
2514 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
2515 {
2516 d(i,j,k,n+destcomp) = s(i+offset.x,j+offset.y,k+offset.z,n+srccomp) + a*d(i,j,k,n+destcomp);
2517 });
2518
2519 return *this;
2520}
2521
2522template <class T>
2523template <RunOn run_on>
2525BaseFab<T>::addproduct (const Box& destbox, int destcomp, int numcomp,
2526 const BaseFab<T>& src1, int comp1,
2527 const BaseFab<T>& src2, int comp2) noexcept
2528{
2529 BL_ASSERT(destbox.ok());
2530 BL_ASSERT(box().contains(destbox));
2531 BL_ASSERT( comp1 >= 0 && comp1+numcomp <= src1.nComp());
2532 BL_ASSERT( comp2 >= 0 && comp2+numcomp <= src2.nComp());
2533 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2534
2535 Array4<T> const& d = this->array();
2536 Array4<T const> const& s1 = src1.const_array();
2537 Array4<T const> const& s2 = src2.const_array();
2538 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
2539 {
2540 d(i,j,k,n+destcomp) += s1(i,j,k,n+comp1) * s2(i,j,k,n+comp2);
2541 });
2542
2543 return *this;
2544}
2545
2546template <class T>
2547template <RunOn run_on>
2549BaseFab<T>::linComb (const BaseFab<T>& f1, const Box& b1, int comp1,
2550 const BaseFab<T>& f2, const Box& b2, int comp2,
2551 Real alpha, Real beta, const Box& b,
2552 int comp, int numcomp) noexcept
2553{
2554 BL_ASSERT(b1.ok());
2555 BL_ASSERT(f1.box().contains(b1));
2556 BL_ASSERT(b2.ok());
2557 BL_ASSERT(f2.box().contains(b2));
2558 BL_ASSERT(b.ok());
2559 BL_ASSERT(box().contains(b));
2560 BL_ASSERT(b.sameSize(b1));
2561 BL_ASSERT(b.sameSize(b2));
2562 BL_ASSERT(comp1 >= 0 && comp1+numcomp <= f1.nComp());
2563 BL_ASSERT(comp2 >= 0 && comp2+numcomp <= f2.nComp());
2564 BL_ASSERT(comp >= 0 && comp +numcomp <= nComp());
2565
2566 Array4<T> const& d = this->array();
2567 Array4<T const> const& s1 = f1.const_array();
2568 Array4<T const> const& s2 = f2.const_array();
2569 const auto dlo = amrex::lbound(b);
2570 const auto slo1 = amrex::lbound(b1);
2571 const auto slo2 = amrex::lbound(b2);
2572 const Dim3 off1{.x = slo1.x-dlo.x, .y = slo1.y-dlo.y, .z = slo1.z-dlo.z};
2573 const Dim3 off2{.x = slo2.x-dlo.x, .y = slo2.y-dlo.y, .z = slo2.z-dlo.z};
2574
2575 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, b, numcomp, i, j, k, n,
2576 {
2577 d(i,j,k,n+comp) = alpha*s1(i+off1.x,j+off1.y,k+off1.z,n+comp1)
2578 + beta*s2(i+off2.x,j+off2.y,k+off2.z,n+comp2);
2579 });
2580 return *this;
2581}
2582
2583template <class T>
2584template <RunOn run_on>
2585T
2586BaseFab<T>::dot (const Box& xbx, int xcomp,
2587 const BaseFab<T>& y, const Box& ybx, int ycomp,
2588 int numcomp) const noexcept
2589{
2590 BL_ASSERT(xbx.ok());
2591 BL_ASSERT(box().contains(xbx));
2592 BL_ASSERT(y.box().contains(ybx));
2593 BL_ASSERT(xbx.sameSize(ybx));
2594 BL_ASSERT(xcomp >= 0 && xcomp+numcomp <= nComp());
2595 BL_ASSERT(ycomp >= 0 && ycomp+numcomp <= y.nComp());
2596
2597 T r = 0;
2598
2599 const auto xlo = amrex::lbound(xbx);
2600 const auto ylo = amrex::lbound(ybx);
2601 const Dim3 offset{.x = ylo.x-xlo.x, .y = ylo.y-xlo.y, .z = ylo.z-xlo.z};
2602 Array4<T const> const& xa = this->const_array();
2603 Array4<T const> const& ya = y.const_array();
2604
2605#ifdef AMREX_USE_GPU
2606 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2607 ReduceOps<ReduceOpSum> reduce_op;
2608 ReduceData<T> reduce_data(reduce_op);
2609 using ReduceTuple = typename decltype(reduce_data)::Type;
2610 reduce_op.eval(xbx, reduce_data,
2611 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2612 {
2613 T t = 0;
2614 for (int n = 0; n < numcomp; ++n) {
2615 t += xa(i,j,k,n+xcomp) * ya(i+offset.x,j+offset.y,k+offset.z,n+ycomp);
2616 }
2617 return {t};
2618 });
2619 ReduceTuple hv = reduce_data.value(reduce_op);
2620 r = amrex::get<0>(hv);
2621 } else
2622#endif
2623 {
2624 r = detail::basefab_reduce_cpu<ReduceOpSum>(xbx, numcomp, T(0),
2625 [=] (int i, int j, int k, int n) noexcept
2626 {
2627 return xa(i,j,k,n+xcomp) * ya(i+offset.x,j+offset.y,k+offset.z,n+ycomp);
2628 });
2629 }
2630
2631 return r;
2632}
2633
2634template <class T>
2635template <RunOn run_on>
2636T
2637BaseFab<T>::dotmask (const BaseFab<int>& mask, const Box& xbx, int xcomp,
2638 const BaseFab<T>& y, const Box& ybx, int ycomp,
2639 int numcomp) const noexcept
2640{
2641 BL_ASSERT(xbx.ok());
2642 BL_ASSERT(box().contains(xbx));
2643 BL_ASSERT(y.box().contains(ybx));
2644 BL_ASSERT(xbx.sameSize(ybx));
2645 BL_ASSERT(xcomp >= 0 && xcomp+numcomp <= nComp());
2646 BL_ASSERT(ycomp >= 0 && ycomp+numcomp <= y.nComp());
2647
2648 T r = 0;
2649
2650 const auto xlo = amrex::lbound(xbx);
2651 const auto ylo = amrex::lbound(ybx);
2652 const Dim3 offset{.x = ylo.x-xlo.x, .y = ylo.y-xlo.y, .z = ylo.z-xlo.z};
2653
2654 Array4<T const> const& xa = this->const_array();
2655 Array4<T const> const& ya = y.const_array();
2656 Array4<int const> const& ma = mask.const_array();
2657
2658#ifdef AMREX_USE_GPU
2659 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
2660 ReduceOps<ReduceOpSum> reduce_op;
2661 ReduceData<T> reduce_data(reduce_op);
2662 using ReduceTuple = typename decltype(reduce_data)::Type;
2663 reduce_op.eval(xbx, reduce_data,
2664 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
2665 {
2666 int m = static_cast<int>(static_cast<bool>(ma(i,j,k)));
2667 T t = 0;
2668 for (int n = 0; n < numcomp; ++n) {
2669 t += xa(i,j,k,n+xcomp) * ya(i+offset.x,j+offset.y,k+offset.z,n+ycomp) * m;
2670 }
2671 return {t};
2672 });
2673 ReduceTuple hv = reduce_data.value(reduce_op);
2674 r = amrex::get<0>(hv);
2675 } else
2676#endif
2677 {
2678 r = detail::basefab_reduce_cpu<ReduceOpSum>(xbx, numcomp, T(0),
2679 [=] (int i, int j, int k, int n) noexcept
2680 {
2681 int m = static_cast<int>(static_cast<bool>(ma(i,j,k)));
2682 return xa(i,j,k,n+xcomp) * ya(i+offset.x,j+offset.y,k+offset.z,n+ycomp) * m;
2683 });
2684 }
2685
2686 return r;
2687}
2688
2689template <class T>
2690template <RunOn run_on>
2691T
2692BaseFab<T>::sum (int comp, int numcomp) const noexcept
2693{
2694 return this->sum<run_on>(this->domain, DestComp{comp}, NumComps{numcomp});
2695}
2696
2697template <class T>
2698template <RunOn run_on>
2699T
2700BaseFab<T>::sum (const Box& subbox, int comp, int numcomp) const noexcept
2701{
2702 return this->sum<run_on>(subbox, DestComp{comp}, NumComps{numcomp});
2703}
2704
2705template <class T>
2706template <RunOn run_on>
2708BaseFab<T>::negate (int comp, int numcomp) noexcept
2709{
2710 return this->negate<run_on>(this->domain, DestComp{comp}, NumComps{numcomp});
2711}
2712
2713template <class T>
2714template <RunOn run_on>
2716BaseFab<T>::negate (const Box& b, int comp, int numcomp) noexcept
2717{
2718 return this->negate<run_on>(b, DestComp{comp}, NumComps{numcomp});
2719}
2720
2721template <class T>
2722template <RunOn run_on>
2724BaseFab<T>::invert (T const& r, int comp, int numcomp) noexcept
2725{
2726 return this->invert<run_on>(r, this->domain, DestComp{comp}, NumComps{numcomp});
2727}
2728
2729template <class T>
2730template <RunOn run_on>
2732BaseFab<T>::invert (T const& r, const Box& b, int comp, int numcomp) noexcept
2733{
2734 return this->invert<run_on>(r, b, DestComp{comp}, NumComps{numcomp});
2735}
2736
2737template <class T>
2738template <RunOn run_on>
2740BaseFab<T>::plus (T const& r, int comp, int numcomp) noexcept
2741{
2742 return this->plus<run_on>(r, this->domain, DestComp{comp}, NumComps{numcomp});
2743}
2744
2745template <class T>
2746template <RunOn run_on>
2748BaseFab<T>::plus (T const& r, const Box& b, int comp, int numcomp) noexcept
2749{
2750 return this->plus<run_on>(r, b, DestComp{comp}, NumComps{numcomp});
2751}
2752
2753template <class T>
2754template <RunOn run_on>
2756BaseFab<T>::plus (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
2757{
2758 return this->plus<run_on>(src, this->domain, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
2759}
2760
2761template <class T>
2762template <RunOn run_on>
2764BaseFab<T>::atomicAdd (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
2765{
2766 Box ovlp(this->domain);
2767 ovlp &= src.domain;
2768 return ovlp.ok() ? this->atomicAdd<run_on>(src,ovlp,ovlp,srccomp,destcomp,numcomp) : *this;
2769}
2770
2771template <class T>
2772template <RunOn run_on>
2774BaseFab<T>::plus (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
2775 int numcomp) noexcept
2776{
2777 return this->plus<run_on>(src, subbox, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
2778}
2779
2780template <class T>
2781template <RunOn run_on>
2783BaseFab<T>::atomicAdd (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
2784 int numcomp) noexcept
2785{
2786 Box ovlp(this->domain);
2787 ovlp &= src.domain;
2788 ovlp &= subbox;
2789 return ovlp.ok() ? this->atomicAdd<run_on>(src,ovlp,ovlp,srccomp,destcomp,numcomp) : *this;
2790}
2791
2792template <class T>
2793template <RunOn run_on>
2795BaseFab<T>::plus (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
2796 int srccomp, int destcomp, int numcomp) noexcept
2797{
2798 BL_ASSERT(destbox.ok());
2799 BL_ASSERT(src.box().contains(srcbox));
2800 BL_ASSERT(box().contains(destbox));
2801 BL_ASSERT(destbox.sameSize(srcbox));
2802 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
2803 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2804
2805 Array4<T> const& d = this->array();
2806 Array4<T const> const& s = src.const_array();
2807 const auto dlo = amrex::lbound(destbox);
2808 const auto slo = amrex::lbound(srcbox);
2809 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
2810 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
2811 {
2812 d(i,j,k,n+destcomp) += s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
2813 });
2814
2815 return *this;
2816}
2817
2818namespace detail
2819{
2820
2821template <RunOn run_on, typename T>
2822requires (HasAtomicAdd<T>::value)
2823void basefab_atomic_add (BaseFab<T>& dfab, const BaseFab<T>& sfab,
2824 const Box& srcbox, const Box& destbox,
2825 int srccomp, int destcomp, int numcomp) noexcept
2826{
2827 Array4<T> const& d = dfab.array();
2828 Array4<T const> const& s = sfab.const_array();
2829 const auto dlo = amrex::lbound(destbox);
2830 const auto slo = amrex::lbound(srcbox);
2831 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
2832 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
2833 {
2834 T* p = d.ptr(i,j,k,n+destcomp);
2835 HostDevice::Atomic::Add(p, s(i+offset.x,j+offset.y,k+offset.z,n+srccomp));
2836 });
2837}
2838
2839template <RunOn run_on, typename T>
2840requires (!HasAtomicAdd<T>::value)
2841void basefab_atomic_add (BaseFab<T>& dfab, const BaseFab<T>& sfab,
2842 const Box& srcbox, const Box& destbox,
2843 int srccomp, int destcomp, int numcomp)
2844{
2845 amrex::ignore_unused(dfab, sfab, srcbox, destbox, srccomp, destcomp, numcomp);
2846 amrex::Abort("BaseFab: atomicAdd not supported");
2847}
2848
2849}
2850
2851template <class T>
2852template <RunOn run_on>
2853BaseFab<T>&
2854BaseFab<T>::atomicAdd (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
2855 int srccomp, int destcomp, int numcomp) noexcept
2856{
2857 BL_ASSERT(destbox.ok());
2858 BL_ASSERT(src.box().contains(srcbox));
2859 BL_ASSERT(box().contains(destbox));
2860 BL_ASSERT(destbox.sameSize(srcbox));
2861 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
2862 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2863
2864 detail::basefab_atomic_add<run_on>(*this, src, srcbox, destbox,
2865 srccomp, destcomp, numcomp);
2866
2867 return *this;
2868}
2869
2870template <class T>
2871template <RunOn run_on>
2873BaseFab<T>::lockAdd (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
2874 int srccomp, int destcomp, int numcomp) noexcept
2875{
2876#if defined(AMREX_USE_OMP) && (AMREX_SPACEDIM > 1)
2877#if defined(AMREX_USE_GPU)
2878 if (run_on == RunOn::Host || Gpu::notInLaunchRegion()) {
2879#endif
2880 BL_ASSERT(destbox.ok());
2881 BL_ASSERT(src.box().contains(srcbox));
2882 BL_ASSERT(box().contains(destbox));
2883 BL_ASSERT(destbox.sameSize(srcbox));
2884 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
2885 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
2886
2887 Array4<T> const& d = this->array();
2888 Array4<T const> const& s = src.const_array();
2889 auto const& dlo = amrex::lbound(destbox);
2890 auto const& dhi = amrex::ubound(destbox);
2891 auto const& len = amrex::length(destbox);
2892 auto const& slo = amrex::lbound(srcbox);
2893 Dim3 const offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
2894
2895 // The lock dimension must be a property of the destination fab, not of
2896 // destbox, so that concurrent calls into the same fab use the same locks.
2897 int planedim;
2898 int nplanes;
2899 int plo;
2900 if (amrex::length(this->domain).z == 1) {
2901 planedim = 1;
2902 nplanes = len.y;
2903 plo = dlo.y;
2904 } else {
2905 planedim = 2;
2906 nplanes = len.z;
2907 plo = dlo.z;
2908 }
2909
2910 auto* mask = (bool*) amrex_mempool_alloc(sizeof(bool)*nplanes);
2911 for (int ip = 0; ip < nplanes; ++ip) {
2912 mask[ip] = false;
2913 }
2914
2915 int mm = 0;
2916 int planes_left = nplanes;
2917 while (planes_left > 0) {
2918 AMREX_ASSERT(mm < nplanes);
2919 auto const m = mm + plo;
2920 auto* lock = OpenMP::get_lock(m);
2921 if (omp_test_lock(lock))
2922 {
2923 auto lo = dlo;
2924 auto hi = dhi;
2925 if (planedim == 1) {
2926 lo.y = m;
2927 hi.y = m;
2928 } else {
2929 lo.z = m;
2930 hi.z = m;
2931 }
2932
2933 for (int n = 0; n < numcomp; ++n) {
2934 for (int k = lo.z; k <= hi.z; ++k) {
2935 for (int j = lo.y; j <= hi.y; ++j) {
2936 auto * pdst = d.ptr(dlo.x,j ,k ,n+destcomp);
2937 auto const* psrc = s.ptr(slo.x,j+offset.y,k+offset.z,n+ srccomp);
2938#pragma omp simd
2939 for (int ii = 0; ii < len.x; ++ii) {
2940 pdst[ii] += psrc[ii];
2941 }
2942 }
2943 }
2944 }
2945
2946 mask[mm] = true;
2947 --planes_left;
2948 omp_unset_lock(lock);
2949 if (planes_left == 0) { break; }
2950 }
2951
2952 ++mm;
2953 for (int ip = 0; ip < nplanes; ++ip) {
2954 int new_mm = (mm+ip) % nplanes;
2955 if ( ! mask[new_mm] ) {
2956 mm = new_mm;
2957 break;
2958 }
2959 }
2960 }
2961
2962 amrex_mempool_free(mask);
2963
2964 return *this;
2965
2966#if defined(AMREX_USE_GPU)
2967 } else {
2968 return this->template atomicAdd<run_on>(src, srcbox, destbox, srccomp, destcomp, numcomp);
2969 }
2970#endif
2971#else
2972 return this->template atomicAdd<run_on>(src, srcbox, destbox, srccomp, destcomp, numcomp);
2973#endif
2974}
2975
2976template <class T>
2977template <RunOn run_on>
2979BaseFab<T>::minus (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
2980{
2981 return this->minus<run_on>(src, this->domain, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
2982}
2983
2984template <class T>
2985template <RunOn run_on>
2987BaseFab<T>::minus (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp, int numcomp) noexcept
2988{
2989 return this->minus<run_on>(src, subbox, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
2990}
2991
2992template <class T>
2993template <RunOn run_on>
2995BaseFab<T>::minus (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
2996 int srccomp, int destcomp, int numcomp) noexcept
2997{
2998 BL_ASSERT(destbox.ok());
2999 BL_ASSERT(src.box().contains(srcbox));
3000 BL_ASSERT(box().contains(destbox));
3001 BL_ASSERT(destbox.sameSize(srcbox));
3002 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
3003 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
3004
3005 Array4<T> const& d = this->array();
3006 Array4<T const> const& s = src.const_array();
3007 const auto dlo = amrex::lbound(destbox);
3008 const auto slo = amrex::lbound(srcbox);
3009 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
3010 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
3011 {
3012 d(i,j,k,n+destcomp) -= s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
3013 });
3014
3015 return *this;
3016}
3017
3018template <class T>
3019template <RunOn run_on>
3021BaseFab<T>::mult (T const& r, int comp, int numcomp) noexcept
3022{
3023 return this->mult<run_on>(r, this->domain, DestComp{comp}, NumComps{numcomp});
3024}
3025
3026template <class T>
3027template <RunOn run_on>
3029BaseFab<T>::mult (T const& r, const Box& b, int comp, int numcomp) noexcept
3030{
3031 return this->mult<run_on>(r, b, DestComp{comp}, NumComps{numcomp});
3032}
3033
3034template <class T>
3035template <RunOn run_on>
3037BaseFab<T>::mult (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
3038{
3039 return this->mult<run_on>(src, this->domain, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
3040}
3041
3042template <class T>
3043template <RunOn run_on>
3045BaseFab<T>::mult (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp, int numcomp) noexcept
3046{
3047 return this->mult<run_on>(src, subbox, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
3048}
3049
3050template <class T>
3051template <RunOn run_on>
3053BaseFab<T>::mult (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
3054 int srccomp, int destcomp, int numcomp) noexcept
3055{
3056 BL_ASSERT(destbox.ok());
3057 BL_ASSERT(src.box().contains(srcbox));
3058 BL_ASSERT(box().contains(destbox));
3059 BL_ASSERT(destbox.sameSize(srcbox));
3060 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
3061 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
3062
3063 Array4<T> const& d = this->array();
3064 Array4<T const> const& s = src.const_array();
3065 const auto dlo = amrex::lbound(destbox);
3066 const auto slo = amrex::lbound(srcbox);
3067 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
3068 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
3069 {
3070 d(i,j,k,n+destcomp) *= s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
3071 });
3072
3073 return *this;
3074}
3075
3076template <class T>
3077template <RunOn run_on>
3079BaseFab<T>::divide (T const& r, int comp, int numcomp) noexcept
3080{
3081 return this->divide<run_on>(r, this->domain, DestComp{comp}, NumComps{numcomp});
3082}
3083
3084template <class T>
3085template <RunOn run_on>
3087BaseFab<T>::divide (T const& r, const Box& b, int comp, int numcomp) noexcept
3088{
3089 return this->divide<run_on>(r, b, DestComp{comp}, NumComps{numcomp});
3090}
3091
3092template <class T>
3093template <RunOn run_on>
3095BaseFab<T>::divide (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
3096{
3097 return this->divide<run_on>(src, this->domain, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
3098}
3099
3100template <class T>
3101template <RunOn run_on>
3103BaseFab<T>::divide (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp, int numcomp) noexcept
3104{
3105 return this->divide<run_on>(src, subbox, SrcComp{srccomp}, DestComp{destcomp}, NumComps{numcomp});
3106}
3107
3108template <class T>
3109template <RunOn run_on>
3111BaseFab<T>::divide (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
3112 int srccomp, int destcomp, int numcomp) noexcept
3113{
3114 BL_ASSERT(destbox.ok());
3115 BL_ASSERT(src.box().contains(srcbox));
3116 BL_ASSERT(box().contains(destbox));
3117 BL_ASSERT(destbox.sameSize(srcbox));
3118 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
3119 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
3120
3121 Array4<T> const& d = this->array();
3122 Array4<T const> const& s = src.const_array();
3123 const auto dlo = amrex::lbound(destbox);
3124 const auto slo = amrex::lbound(srcbox);
3125 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
3126 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
3127 {
3128 d(i,j,k,n+destcomp) /= s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
3129 });
3130
3131 return *this;
3132}
3133
3134template <class T>
3135template <RunOn run_on>
3138{
3139 Box ovlp(this->domain);
3140 ovlp &= src.domain;
3141 return ovlp.ok() ? this->protected_divide<run_on>(src,ovlp,ovlp,0,0,this->nvar) : *this;
3142}
3143
3144template <class T>
3145template <RunOn run_on>
3147BaseFab<T>::protected_divide (const BaseFab<T>& src, int srccomp, int destcomp, int numcomp) noexcept
3148{
3149 Box ovlp(this->domain);
3150 ovlp &= src.domain;
3151 return ovlp.ok() ? this->protected_divide<run_on>(src,ovlp,ovlp,srccomp,destcomp,numcomp) : *this;
3152}
3153
3154template <class T>
3155template <RunOn run_on>
3157BaseFab<T>::protected_divide (const BaseFab<T>& src, const Box& subbox, int srccomp, int destcomp,
3158 int numcomp) noexcept
3159{
3160 Box ovlp(this->domain);
3161 ovlp &= src.domain;
3162 ovlp &= subbox;
3163 return ovlp.ok() ? this->protected_divide<run_on>(src,ovlp,ovlp,srccomp,destcomp,numcomp) : *this;
3164}
3165
3166template <class T>
3167template <RunOn run_on>
3169BaseFab<T>::protected_divide (const BaseFab<T>& src, const Box& srcbox, const Box& destbox,
3170 int srccomp, int destcomp, int numcomp) noexcept
3171{
3172 BL_ASSERT(destbox.ok());
3173 BL_ASSERT(src.box().contains(srcbox));
3174 BL_ASSERT(box().contains(destbox));
3175 BL_ASSERT(destbox.sameSize(srcbox));
3176 BL_ASSERT(srccomp >= 0 && srccomp+numcomp <= src.nComp());
3177 BL_ASSERT(destcomp >= 0 && destcomp+numcomp <= nComp());
3178
3179 Array4<T> const& d = this->array();
3180 Array4<T const> const& s = src.const_array();
3181 const auto dlo = amrex::lbound(destbox);
3182 const auto slo = amrex::lbound(srcbox);
3183 const Dim3 offset{.x = slo.x-dlo.x, .y = slo.y-dlo.y, .z = slo.z-dlo.z};
3184 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
3185 {
3186 if (s(i+offset.x,j+offset.y,k+offset.z,n+srccomp) != 0) {
3187 d(i,j,k,n+destcomp) /= s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
3188 }
3189 });
3190
3191 return *this;
3192}
3193
3204template <class T>
3205template <RunOn run_on>
3207BaseFab<T>::linInterp (const BaseFab<T>& f1, const Box& b1, int comp1,
3208 const BaseFab<T>& f2, const Box& b2, int comp2,
3209 Real t1, Real t2, Real t,
3210 const Box& b, int comp, int numcomp) noexcept
3211{
3212 if (amrex::almostEqual(t1,t2) || amrex::almostEqual(t1,t)) {
3213 return copy<run_on>(f1,b1,comp1,b,comp,numcomp);
3214 } else if (amrex::almostEqual(t2,t)) {
3215 return copy<run_on>(f2,b2,comp2,b,comp,numcomp);
3216 } else {
3217 Real alpha = (t2-t)/(t2-t1);
3218 Real beta = (t-t1)/(t2-t1);
3219 return linComb<run_on>(f1,b1,comp1,f2,b2,comp2,alpha,beta,b,comp,numcomp);
3220 }
3221}
3222
3223template <class T>
3224template <RunOn run_on>
3226BaseFab<T>::linInterp (const BaseFab<T>& f1, int comp1,
3227 const BaseFab<T>& f2, int comp2,
3228 Real t1, Real t2, Real t,
3229 const Box& b, int comp, int numcomp) noexcept
3230{
3231 if (amrex::almostEqual(t1,t2) || amrex::almostEqual(t1,t)) {
3232 return copy<run_on>(f1,b,comp1,b,comp,numcomp);
3233 } else if (amrex::almostEqual(t2,t)) {
3234 return copy<run_on>(f2,b,comp2,b,comp,numcomp);
3235 } else {
3236 Real alpha = (t2-t)/(t2-t1);
3237 Real beta = (t-t1)/(t2-t1);
3238 return linComb<run_on>(f1,b,comp1,f2,b,comp2,alpha,beta,b,comp,numcomp);
3239 }
3240}
3241
3242//
3243// New interfaces
3244//
3245
3246template <class T>
3247template <RunOn run_on>
3248void
3249BaseFab<T>::setVal (T const& val) noexcept
3250{
3251 this->setVal<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3252}
3253
3254template <class T>
3255template <RunOn run_on>
3256void
3257BaseFab<T>::setVal (T const& x, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept
3258{
3259 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3260 Array4<T> const& a = this->array();
3261 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG (run_on, bx, ncomp.n, i, j, k, n,
3262 {
3263 a(i,j,k,n+dcomp.i) = x;
3264 });
3265}
3266
3267template <class T>
3268template <RunOn run_on>
3269void
3270BaseFab<T>::setValIf (T const& val, const BaseFab<int>& mask) noexcept
3271{
3272 this->setValIf<run_on>(val, this->domain, mask, DestComp{0}, NumComps{this->nvar});
3273}
3274
3275template <class T>
3276template <RunOn run_on>
3277void
3278BaseFab<T>::setValIf (T const& val, Box const& bx, const BaseFab<int>& mask, DestComp dcomp, NumComps ncomp) noexcept
3279{
3280 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3281 Array4<T> const& a = this->array();
3282 Array4<int const> const& m = mask.const_array();
3283 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG (run_on, bx, ncomp.n, i, j, k, n,
3284 {
3285 if (m(i,j,k)) { a(i,j,k,n+dcomp.i) = val; }
3286 });
3287}
3288
3289template <class T>
3290template <RunOn run_on>
3291void
3292BaseFab<T>::setValIfNot (T const& val, const BaseFab<int>& mask) noexcept
3293{
3294 this->setValIfNot<run_on>(val, this->domain, mask, DestComp{0}, NumComps{this->nvar});
3295}
3296
3297template <class T>
3298template <RunOn run_on>
3299void
3300BaseFab<T>::setValIfNot (T const& val, Box const& bx, const BaseFab<int>& mask, DestComp dcomp, NumComps ncomp) noexcept
3301{
3302 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3303 Array4<T> const& a = this->array();
3304 Array4<int const> const& m = mask.const_array();
3305 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG (run_on, bx, ncomp.n, i, j, k, n,
3306 {
3307 if (!m(i,j,k)) { a(i,j,k,n+dcomp.i) = val; }
3308 });
3309}
3310
3311template <class T>
3312template <RunOn run_on>
3313void
3314BaseFab<T>::setComplement (T const& x, const Box& bx, DestComp dcomp, NumComps ncomp) noexcept
3315{
3316#ifdef AMREX_USE_GPU
3317 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
3318 Array4<T> const& a = this->array();
3319 amrex::ParallelFor(this->domain, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
3320 {
3321 if (! bx.contains(IntVect(AMREX_D_DECL(i,j,k)))) {
3322 for (int n = dcomp.i; n < ncomp.n+dcomp.i; ++n) {
3323 a(i,j,k,n) = x;
3324 }
3325 }
3326 });
3327 } else
3328#endif
3329 {
3330 const BoxList b_lst = amrex::boxDiff(this->domain,bx);
3331 for (auto const& b : b_lst) {
3332 this->setVal<RunOn::Host>(x, b, dcomp, ncomp);
3333 }
3334 }
3335}
3336
3337template <class T>
3338template <RunOn run_on>
3340BaseFab<T>::copy (const BaseFab<T>& src) noexcept
3341{
3342 this->copy<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3343 return *this;
3344}
3345
3346template <class T>
3347template <RunOn run_on>
3350 SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
3351{
3352 AMREX_ASSERT(this->domain.sameType(src.domain));
3353 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3354 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3355
3356 bx &= src.domain;
3357
3358 Array4<T> const& d = this->array();
3359 Array4<T const> const& s = src.const_array();
3360 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3361 {
3362 d(i,j,k,n+dcomp.i) = s(i,j,k,n+scomp.i);
3363 });
3364
3365 return *this;
3366}
3367
3368template <class T>
3369template <RunOn run_on>
3371BaseFab<T>::plus (T const& val) noexcept
3372{
3373 return this->plus<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3374}
3375
3376template <class T>
3377template <RunOn run_on>
3379BaseFab<T>::operator+= (T const& val) noexcept
3380{
3381 return this->plus<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3382}
3383
3384template <class T>
3385template <RunOn run_on>
3387BaseFab<T>::plus (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept
3388{
3389 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3390
3391 Array4<T> const& a = this->array();
3392 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3393 {
3394 a(i,j,k,n+dcomp.i) += val;
3395 });
3396
3397 return *this;
3398}
3399
3400template <class T>
3401template <RunOn run_on>
3403BaseFab<T>::plus (const BaseFab<T>& src) noexcept
3404{
3405 return this->plus<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3406}
3407
3408template <class T>
3409template <RunOn run_on>
3412{
3413 return this->plus<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3414}
3415
3416template <class T>
3417template <RunOn run_on>
3420 SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
3421{
3422 AMREX_ASSERT(this->domain.sameType(src.domain));
3423 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3424 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3425
3426 bx &= src.domain;
3427
3428 Array4<T> const& d = this->array();
3429 Array4<T const> const& s = src.const_array();
3430 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3431 {
3432 d(i,j,k,n+dcomp.i) += s(i,j,k,n+scomp.i);
3433 });
3434
3435 return *this;
3436}
3437
3438template <class T>
3439template <RunOn run_on>
3441BaseFab<T>::minus (T const& val) noexcept
3442{
3443 return this->minus<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3444}
3445
3446template <class T>
3447template <RunOn run_on>
3449BaseFab<T>::operator-= (T const& val) noexcept
3450{
3451 return this->minus<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3452}
3453
3454template <class T>
3455template <RunOn run_on>
3457BaseFab<T>::minus (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept
3458{
3459 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3460
3461 Array4<T> const& a = this->array();
3462 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3463 {
3464 a(i,j,k,n+dcomp.i) -= val;
3465 });
3466
3467 return *this;
3468}
3469
3470template <class T>
3471template <RunOn run_on>
3473BaseFab<T>::minus (const BaseFab<T>& src) noexcept
3474{
3475 return this->minus<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3476}
3477
3478template <class T>
3479template <RunOn run_on>
3482{
3483 return this->minus<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3484}
3485
3486template <class T>
3487template <RunOn run_on>
3490 SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
3491{
3492 AMREX_ASSERT(this->domain.sameType(src.domain));
3493 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3494 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3495
3496 bx &= src.domain;
3497
3498 Array4<T> const& d = this->array();
3499 Array4<T const> const& s = src.const_array();
3500 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3501 {
3502 d(i,j,k,n+dcomp.i) -= s(i,j,k,n+scomp.i);
3503 });
3504
3505 return *this;
3506}
3507
3508template <class T>
3509template <RunOn run_on>
3511BaseFab<T>::mult (T const& val) noexcept
3512{
3513 return this->mult<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3514}
3515
3516template <class T>
3517template <RunOn run_on>
3519BaseFab<T>::operator*= (T const& val) noexcept
3520{
3521 return this->mult<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3522}
3523
3524template <class T>
3525template <RunOn run_on>
3527BaseFab<T>::mult (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept
3528{
3529 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3530
3531 Array4<T> const& a = this->array();
3532 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3533 {
3534 a(i,j,k,n+dcomp.i) *= val;
3535 });
3536
3537 return *this;
3538}
3539
3540template <class T>
3541template <RunOn run_on>
3543BaseFab<T>::mult (const BaseFab<T>& src) noexcept
3544{
3545 return this->mult<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3546}
3547
3548template <class T>
3549template <RunOn run_on>
3552{
3553 return this->mult<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3554}
3555
3556template <class T>
3557template <RunOn run_on>
3560 SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
3561{
3562 AMREX_ASSERT(this->domain.sameType(src.domain));
3563 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3564 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3565
3566 bx &= src.domain;
3567
3568 Array4<T> const& d = this->array();
3569 Array4<T const> const& s = src.const_array();
3570 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3571 {
3572 d(i,j,k,n+dcomp.i) *= s(i,j,k,n+scomp.i);
3573 });
3574
3575 return *this;
3576}
3577
3578template <class T>
3579template <RunOn run_on>
3581BaseFab<T>::divide (T const& val) noexcept
3582{
3583 return this->divide<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3584}
3585
3586template <class T>
3587template <RunOn run_on>
3589BaseFab<T>::operator/= (T const& val) noexcept
3590{
3591 return this->divide<run_on>(val, this->domain, DestComp{0}, NumComps{this->nvar});
3592}
3593
3594template <class T>
3595template <RunOn run_on>
3597BaseFab<T>::divide (T const& val, Box const& bx, DestComp dcomp, NumComps ncomp) noexcept
3598{
3599 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3600
3601 Array4<T> const& a = this->array();
3602 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3603 {
3604 a(i,j,k,n+dcomp.i) /= val;
3605 });
3606
3607 return *this;
3608}
3609
3610template <class T>
3611template <RunOn run_on>
3613BaseFab<T>::divide (const BaseFab<T>& src) noexcept
3614{
3615 return this->divide<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3616}
3617
3618template <class T>
3619template <RunOn run_on>
3622{
3623 return this->divide<run_on>(src, this->domain, SrcComp{0}, DestComp{0}, NumComps{this->nvar});
3624}
3625
3626template <class T>
3627template <RunOn run_on>
3630 SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
3631{
3632 AMREX_ASSERT(this->domain.sameType(src.domain));
3633 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3634 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3635
3636 bx &= src.domain;
3637
3638 Array4<T> const& d = this->array();
3639 Array4<T const> const& s = src.const_array();
3640 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3641 {
3642 d(i,j,k,n+dcomp.i) /= s(i,j,k,n+scomp.i);
3643 });
3644
3645 return *this;
3646}
3647
3648template <class T>
3649template <RunOn run_on>
3652{
3653 return this->negate<run_on>(this->domain, DestComp{0}, NumComps{this->nvar});
3654}
3655
3656template <class T>
3657template <RunOn run_on>
3659BaseFab<T>::negate (const Box& bx, DestComp dcomp, NumComps ncomp) noexcept
3660{
3661 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3662
3663 Array4<T> const& a = this->array();
3664 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3665 {
3666 a(i,j,k,n+dcomp.i) = -a(i,j,k,n+dcomp.i);
3667 });
3668
3669 return *this;
3670}
3671
3672template <class T>
3673template <RunOn run_on>
3675BaseFab<T>::invert (T const& r) noexcept
3676{
3677 return this->invert<run_on>(r, this->domain, DestComp{0}, NumComps{this->nvar});
3678}
3679
3680template <class T>
3681template <RunOn run_on>
3683BaseFab<T>::invert (T const& r, const Box& bx, DestComp dcomp, NumComps ncomp) noexcept
3684{
3685 BL_ASSERT(dcomp.i >= 0 && dcomp.i + ncomp.n <= this->nvar);
3686
3687 Array4<T> const& a = this->array();
3688 AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, bx, ncomp.n, i, j, k, n,
3689 {
3690 a(i,j,k,n+dcomp.i) = r / a(i,j,k,n+dcomp.i);
3691 });
3692
3693 return *this;
3694}
3695
3696template <class T>
3697template <RunOn run_on>
3698T
3699BaseFab<T>::sum (const Box& bx, DestComp dcomp, NumComps ncomp) const noexcept
3700{
3701 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3702
3703 T r = 0;
3704 Array4<T const> const& a = this->const_array();
3705#ifdef AMREX_USE_GPU
3706 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
3707 ReduceOps<ReduceOpSum> reduce_op;
3708 ReduceData<T> reduce_data(reduce_op);
3709 using ReduceTuple = typename decltype(reduce_data)::Type;
3710 reduce_op.eval(bx, reduce_data,
3711 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
3712 {
3713 T t = 0;
3714 for (int n = 0; n < ncomp.n; ++n) {
3715 t += a(i,j,k,n+dcomp.i);
3716 }
3717 return { t };
3718 });
3719 ReduceTuple hv = reduce_data.value(reduce_op);
3720 r = amrex::get<0>(hv);
3721 } else
3722#endif
3723 {
3724 r = detail::basefab_reduce_cpu<ReduceOpSum>(bx, ncomp.n, T(0),
3725 [=] (int i, int j, int k, int n) noexcept
3726 {
3727 return a(i,j,k,n+dcomp.i);
3728 });
3729 }
3730
3731 return r;
3732}
3733
3734template <class T>
3735template <RunOn run_on>
3736T
3737BaseFab<T>::dot (const BaseFab<T>& src, const Box& bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) const noexcept
3738{
3739 AMREX_ASSERT(this->domain.sameType(src.domain));
3740 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3741 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3742
3743 T r = 0;
3744 Array4<T const> const& d = this->const_array();
3745 Array4<T const> const& s = src.const_array();
3746#ifdef AMREX_USE_GPU
3747 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
3748 ReduceOps<ReduceOpSum> reduce_op;
3749 ReduceData<T> reduce_data(reduce_op);
3750 using ReduceTuple = typename decltype(reduce_data)::Type;
3751 reduce_op.eval(bx, reduce_data,
3752 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
3753 {
3754 T t = 0;
3755 for (int n = 0; n < ncomp.n; ++n) {
3756 t += d(i,j,k,n+dcomp.i) * s(i,j,k,n+scomp.i);
3757 }
3758 return { t };
3759 });
3760 ReduceTuple hv = reduce_data.value(reduce_op);
3761 r = amrex::get<0>(hv);
3762 } else
3763#endif
3764 {
3765 r = detail::basefab_reduce_cpu<ReduceOpSum>(bx, ncomp.n, T(0),
3766 [=] (int i, int j, int k, int n) noexcept
3767 {
3768 return d(i,j,k,n+dcomp.i) * s(i,j,k,n+scomp.i);
3769 });
3770 }
3771
3772 return r;
3773}
3774
3775template <class T>
3776template <RunOn run_on>
3777T
3778BaseFab<T>::dot (const Box& bx, int destcomp, int numcomp) const noexcept
3779{
3780 return dot<run_on>(bx, DestComp{destcomp}, NumComps{numcomp});
3781}
3782
3783
3784template <class T>
3785template <RunOn run_on>
3786T
3787BaseFab<T>::dot (const Box& bx, DestComp dcomp, NumComps ncomp) const noexcept
3788{
3789 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3790
3791 T r = 0;
3792 Array4<T const> const& a = this->const_array();
3793#ifdef AMREX_USE_GPU
3794 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
3795 ReduceOps<ReduceOpSum> reduce_op;
3796 ReduceData<T> reduce_data(reduce_op);
3797 using ReduceTuple = typename decltype(reduce_data)::Type;
3798 reduce_op.eval(bx, reduce_data,
3799 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
3800 {
3801 T t = 0;
3802 for (int n = 0; n < ncomp.n; ++n) {
3803 t += a(i,j,k,n+dcomp.i)*a(i,j,k,n+dcomp.i);
3804 }
3805 return { t };
3806 });
3807 ReduceTuple hv = reduce_data.value(reduce_op);
3808 r = amrex::get<0>(hv);
3809 } else
3810#endif
3811 {
3812 r = detail::basefab_reduce_cpu<ReduceOpSum>(bx, ncomp.n, T(0),
3813 [=] (int i, int j, int k, int n) noexcept
3814 {
3815 return a(i,j,k,n+dcomp.i)*a(i,j,k,n+dcomp.i);
3816 });
3817 }
3818
3819 return r;
3820}
3821
3822template <class T>
3823template <RunOn run_on>
3824T
3825BaseFab<T>::dotmask (const BaseFab<T>& src, const Box& bx, const BaseFab<int>& mask,
3826 SrcComp scomp, DestComp dcomp, NumComps ncomp) const noexcept
3827{
3828 AMREX_ASSERT(this->domain.sameType(src.domain));
3829 AMREX_ASSERT(this->domain.sameType(mask.domain));
3830 AMREX_ASSERT(scomp.i >= 0 && scomp.i+ncomp.n <= src.nvar);
3831 AMREX_ASSERT(dcomp.i >= 0 && dcomp.i+ncomp.n <= this->nvar);
3832
3833 T r = 0;
3834 Array4<T const> const& d = this->const_array();
3835 Array4<T const> const& s = src.const_array();
3836 Array4<int const> const& m = mask.const_array();
3837#ifdef AMREX_USE_GPU
3838 if (run_on == RunOn::Device && Gpu::inLaunchRegion()) {
3839 ReduceOps<ReduceOpSum> reduce_op;
3840 ReduceData<T> reduce_data(reduce_op);
3841 using ReduceTuple = typename decltype(reduce_data)::Type;
3842 reduce_op.eval(bx, reduce_data,
3843 [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
3844 {
3845 T t = 0;
3846 T mi = static_cast<T>(static_cast<int>(static_cast<bool>(m(i,j,k))));
3847 for (int n = 0; n < ncomp.n; ++n) {
3848 t += d(i,j,k,n+dcomp.i)*s(i,j,k,n+scomp.i)*mi;
3849 }
3850 return { t };
3851 });
3852 ReduceTuple hv = reduce_data.value(reduce_op);
3853 r = amrex::get<0>(hv);
3854 } else
3855#endif
3856 {
3857 r = detail::basefab_reduce_cpu<ReduceOpSum>(bx, ncomp.n, T(0),
3858 [=] (int i, int j, int k, int n) noexcept
3859 {
3860 int mi = static_cast<int>(static_cast<bool>(m(i,j,k)));
3861 return d(i,j,k,n+dcomp.i)*s(i,j,k,n+scomp.i)*mi;
3862 });
3863 }
3864
3865 return r;
3866}
3867
3868}
3869
3870#endif /*BL_BASEFAB_H*/
General-purpose algorithm utilities available on both host and device.
Fixed-size array types for use on GPU and CPU.
Assertion macros used across AMReX for runtime consistency checks.
#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
Iterator over all IntVects in a Box.
A list of Boxes sharing a common IndexType.
Integer-lattice boxes and helpers for defining index-space regions.
Lightweight Arena-backed allocator and deleter helpers.
Compiler- and backend-specific extension macros (e.g., restrict, SIMD, inline).
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:123
#define AMREX_ATTRIBUTE_FLATTEN_FOR
Definition AMReX_Extension.H:155
#define AMREX_DEFAULT_RUNON
Definition AMReX_GpuControl.H:69
#define AMREX_CUDA_SAFE_CALL(call)
Definition AMReX_GpuError.H:73
#define AMREX_HOST_DEVICE_PARALLEL_FOR_1D_FLAG(where_to_run, n, i, block)
Definition AMReX_GpuLaunch.nolint.H:43
#define AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(where_to_run, box, nc, i, j, k, n, block)
Definition AMReX_GpuLaunch.nolint.H:73
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Convenience header for the core AMReX GPU facilities.
#define AMREX_LOOP_3D(bx, i, j, k, block)
void amrex_mempool_free(void *p)
Definition AMReX_MemPool.cpp:80
void * amrex_mempool_alloc(size_t nbytes)
Definition AMReX_MemPool.cpp:74
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
Abstract base class for memory arenas.
Definition AMReX_Arena.H:154
A FortranArrayBox(FAB)-like object.
Definition AMReX_BaseFab.H:243
BaseFab< Long > & saxpy(Long a, const BaseFab< Long > &x, const Box &srcbox, const Box &destbox, int srccomp, int destcomp, int numcomp=1) noexcept
Array4< T const > const_array() const noexcept
Return a const-qualified Array4 view over all components.
Definition AMReX_BaseFab.H:568
BaseFab< T > & copy(const BaseFab< T > &src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx does not intersect with src fab.
Definition AMReX_BaseFab.H:3349
Long sum(int comp, int numcomp=1) const noexcept
gpuStream_t alloc_stream
Definition AMReX_BaseFab.H:1309
Real norminfmask(const Box &subbox, const BaseFab< int > &mask, int scomp=0, int ncomp=1) const noexcept
BaseFab< T > & divide(T const &val) noexcept
Scalar division on the whole domain and all components.
Definition AMReX_BaseFab.H:3581
const int * hiVect() const noexcept
Returns the upper corner of the domain.
Definition AMReX_BaseFab.H:413
BaseFab< T > & minus(const BaseFab< T > &src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx does not intersect with src fab.
Definition AMReX_BaseFab.H:3489
BaseFab< Long > & lockAdd(const BaseFab< Long > &src, const Box &srcbox, const Box &destbox, int srccomp, int destcomp, int numcomp) noexcept
static void Initialize()
std::size_t copyToMem(const Box &srcbox, int srccomp, int numcomp, void *dst) const noexcept
std::size_t addFromMem(const Box &dstbox, int dstcomp, int numcomp, const void *src) noexcept
std::size_t nBytesOwned() const noexcept
Definition AMReX_BaseFab.H:355
BaseFab< T > & copy(const BaseFab< T > &src) noexcept
Definition AMReX_BaseFab.H:3340
BaseFab< Long > & addproduct(const Box &destbox, int destcomp, int numcomp, const BaseFab< Long > &src1, int comp1, const BaseFab< Long > &src2, int comp2) noexcept
BaseFab< T > & minus(T const &val) noexcept
Scalar subtraction on the whole domain and all components.
Definition AMReX_BaseFab.H:3441
BaseFab< Long > & negate(const Box &b, int comp=0, int numcomp=1) noexcept
BaseFab< T > & plus(T const &val) noexcept
Scalar addition on the whole domain and all components.
Definition AMReX_BaseFab.H:3371
BaseFab< T > & mult(T const &val, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3527
BaseFab< T > & mult(const BaseFab< T > &src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx does not intersect with src fab.
Definition AMReX_BaseFab.H:3559
std::size_t nBytes(const Box &bx, int ncomps) const noexcept
Returns bytes used in the Box for those components.
Definition AMReX_BaseFab.H:360
void setPtr(Long *p, Long sz) noexcept
Definition AMReX_BaseFab.H:478
BaseFab< Long > & linComb(const BaseFab< Long > &f1, const Box &b1, int comp1, const BaseFab< Long > &f2, const Box &b2, int comp2, Real alpha, Real beta, const Box &b, int comp, int numcomp=1) noexcept
BaseFab< T > & operator*=(T const &val) noexcept
Definition AMReX_BaseFab.H:3519
void resize(const Box &b, int N=1, Arena *ar=nullptr)
const IntVect & smallEnd() const noexcept
Returns the lower corner of the domain See class Box for analogue.
Definition AMReX_BaseFab.H:390
BaseFab< Long > & mult(Long const &r, int comp, int numcomp=1) noexcept
BaseFab< Long > & atomicAdd(const BaseFab< Long > &x) noexcept
const int * loVect() const noexcept
Returns the lower corner of the domain.
Definition AMReX_BaseFab.H:403
bool contains(const BaseFab< T > &fab) const noexcept
Returns true if the domain of fab is totally contained within the domain of this BaseFab.
Definition AMReX_BaseFab.H:419
bool isAllocated() const noexcept
Returns true if the data for the FAB has been allocated.
Definition AMReX_BaseFab.H:598
std::unique_ptr< Long, DataDeleter > release() noexcept
void setVal(T const &x, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3257
BaseFab< T > & operator-=(T const &val) noexcept
Definition AMReX_BaseFab.H:3449
const Box & box() const noexcept
Returns the domain (box) where the array is defined.
Definition AMReX_BaseFab.H:378
void setValIf(T const &val, Box const &bx, const BaseFab< int > &mask, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3278
static void Finalize()
Array4< T > array() noexcept
Create a mutable Array4 view over all components.
Definition AMReX_BaseFab.H:531
IntVect indexFromValue(const Box &subbox, int comp, Long const &value) const noexcept
BaseFab< T > & mult(const BaseFab< T > &src) noexcept
Definition AMReX_BaseFab.H:3543
bool shared_memory
Definition AMReX_BaseFab.H:1307
void setValIf(T const &val, const BaseFab< int > &mask) noexcept
Definition AMReX_BaseFab.H:3270
BaseFab< T > & plus(const BaseFab< T > &src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx does not intersect with src fab.
Definition AMReX_BaseFab.H:3419
void setValIfNot(T const &val, const BaseFab< int > &mask) noexcept
Definition AMReX_BaseFab.H:3292
BaseFab< Long > & xpay(Long a, const BaseFab< Long > &x, const Box &srcbox, const Box &destbox, int srccomp, int destcomp, int numcomp=1) noexcept
T & operator()(const IntVect &p, int N) noexcept
Returns a reference to the Nth component value defined at position p in the domain....
Definition AMReX_BaseFab.H:1406
std::size_t nBytes() const noexcept
Definition AMReX_BaseFab.H:353
std::size_t copyFromMem(const Box &dstbox, int dstcomp, int numcomp, const void *src) noexcept
BaseFab< T > & negate() noexcept
on the whole domain and all components
Definition AMReX_BaseFab.H:3651
BaseFab< T > & minus(const BaseFab< T > &src) noexcept
Definition AMReX_BaseFab.H:3473
BaseFab< Long > & minus(const BaseFab< Long > &src, int srccomp, int destcomp, int numcomp=1) noexcept
T value_type
Definition AMReX_BaseFab.H:248
void SetBoxType(const IndexType &typ) noexcept
Definition AMReX_BaseFab.H:1119
Array4< Long const > array() const noexcept
Definition AMReX_BaseFab.H:496
Long maxabs(int comp=0) const noexcept
Elixir elixir() noexcept
BaseFab< T > & operator+=(T const &val) noexcept
Definition AMReX_BaseFab.H:3379
void setComplement(T const &x, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
setVal on the complement of bx in the fab's domain
Definition AMReX_BaseFab.H:3314
BaseFab< T > & minus(T const &val, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3457
Long truesize
Definition AMReX_BaseFab.H:1305
void setVal(T const &val) noexcept
Set value on the whole domain and all components.
Definition AMReX_BaseFab.H:3249
const int * nCompPtr() const noexcept
for calls to fortran.
Definition AMReX_BaseFab.H:367
Array4< T const > const_array(int start_comp, int num_comps) const noexcept
Return a const-qualified Array4 view spanning num_comps components.
Definition AMReX_BaseFab.H:592
Box domain
Definition AMReX_BaseFab.H:1303
void fill_snan() noexcept
bool contains(const Box &bx) const noexcept
Returns true if bx is totally contained within the domain of this BaseFab.
Definition AMReX_BaseFab.H:428
Long * dptr
Definition AMReX_BaseFab.H:1302
BaseFab< Long > & shift(const IntVect &v) noexcept
BaseFab< T > & divide(T const &val, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3597
Long max(int comp=0) const noexcept
BaseFab< Long > & copy(const BaseFab< Long > &src, const Box &srcbox, int srccomp, const Box &destbox, int destcomp, int numcomp) noexcept
Array4< T > array(int start_comp, int num_comps) noexcept
Mutable overload spanning num_comps components.
Definition AMReX_BaseFab.H:556
int nvar
Definition AMReX_BaseFab.H:1304
Long dot(const Box &xbx, int xcomp, const BaseFab< Long > &y, const Box &ybx, int ycomp, int numcomp=1) const noexcept
BaseFab< Long > & divide(Long const &r, int comp, int numcomp=1) noexcept
Array4< T const > array(int start_comp) const noexcept
Create a sliced Array4 view beginning at component start_comp.
Definition AMReX_BaseFab.H:508
BaseFab< T > & operator/=(T const &val) noexcept
Definition AMReX_BaseFab.H:3589
std::pair< Long, Long > minmax(int comp=0) const noexcept
Array4< T const > const_array(int start_comp) const noexcept
Return a const-qualified Array4 view beginning at component start_comp.
Definition AMReX_BaseFab.H:580
void setVal(T const &x, const Box &bx, int dcomp, int ncomp) noexcept
The setVal functions set sub-regions in the BaseFab to a constant value. This most general form speci...
Definition AMReX_BaseFab.H:1537
Long size() const noexcept
Returns the total number of points of all components.
Definition AMReX_BaseFab.H:375
BaseFab< Long > & plus(Long const &r, const Box &b, int comp=0, int numcomp=1) noexcept
BaseFab< T > & operator=(const BaseFab< T > &rhs)=delete
void getVal(T *data, const IntVect &pos, int N, int numcomp) const noexcept
This function puts numcomp component values, starting at component N, from position pos in the domain...
Definition AMReX_BaseFab.H:1453
const IntVect & bigEnd() const noexcept
Returns the upper corner of the domain. See class Box for analogue.
Definition AMReX_BaseFab.H:393
Array4< T > array(int start_comp) noexcept
Mutable overload selecting components starting at start_comp.
Definition AMReX_BaseFab.H:543
Long numPts() const noexcept
Returns the number of points.
Definition AMReX_BaseFab.H:372
const T * dataPtr(int n=0) const noexcept
Const-qualified variant of dataPtr().
Definition AMReX_BaseFab.H:448
void setValIfNot(T const &val, Box const &bx, const BaseFab< int > &mask, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3300
BaseFab< T > & mult(T const &val) noexcept
Scalar multiplication on the whole domain and all components.
Definition AMReX_BaseFab.H:3511
BaseFab< T > & divide(const BaseFab< T > &src, Box bx, SrcComp scomp, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx does not intersect with src fab.
Definition AMReX_BaseFab.H:3629
void setValIfNot(Long const &val, const Box &bx, const BaseFab< int > &mask, int nstart, int num) noexcept
BaseFab< Long > & shiftHalf(int dir, int n_cell) noexcept
void setVal(T const &x, const Box &bx, int N=0) noexcept
Set a single component N over bx to x.
Definition AMReX_BaseFab.H:1521
void prefetchToDevice() const noexcept
T * dataPtr(int n=0) noexcept
Returns a pointer to an object of type T that is the value of the Nth component associated with the c...
Definition AMReX_BaseFab.H:439
bool ptr_owner
Definition AMReX_BaseFab.H:1306
virtual ~BaseFab() noexcept
The destructor deletes the array memory.
Definition AMReX_BaseFab.H:1726
IntVect length() const noexcept
Returns a pointer to an array of SPACEDIM integers giving the length of the domain in each direction.
Definition AMReX_BaseFab.H:384
IntVect maxIndex(int comp=0) const noexcept
BaseFab< Long > & protected_divide(const BaseFab< Long > &src) noexcept
friend class BaseFab
Definition AMReX_BaseFab.H:246
BaseFab< Long > & invert(Long const &r, const Box &b, int comp=0, int numcomp=1) noexcept
Array4< T const > array(int start_comp, int num_comps) const noexcept
Create a sliced Array4 view spanning num_comps components.
Definition AMReX_BaseFab.H:521
int nComp() const noexcept
Returns the number of components.
Definition AMReX_BaseFab.H:364
Long dotmask(const BaseFab< int > &mask, const Box &xbx, int xcomp, const BaseFab< Long > &y, const Box &ybx, int ycomp, int numcomp) const noexcept
BaseFab< T > & plus(const BaseFab< T > &src) noexcept
Definition AMReX_BaseFab.H:3403
BaseFab() noexcept=default
Construct an empty BaseFab, which must be resized (see BaseFab::resize) before use.
BaseFab< T > & divide(const BaseFab< T > &src) noexcept
Definition AMReX_BaseFab.H:3613
void prefetchToHost() const noexcept
Advise the runtime to migrate storage to the host.
Definition AMReX_BaseFab.H:1341
Long min(int comp=0) const noexcept
void setComplement(Long const &x, const Box &b, int ns, int num) noexcept
BaseFab< Long > & linInterp(const BaseFab< Long > &f1, const Box &b1, int comp1, const BaseFab< Long > &f2, const Box &b2, int comp2, Real t1, Real t2, Real t, const Box &b, int comp, int numcomp=1) noexcept
IntVect minIndex(int comp=0) const noexcept
T * dataPtr(const IntVect &p, int n=0) noexcept
Return a pointer to the element at spatial index p and component n.
Definition AMReX_BaseFab.H:1316
void abs() noexcept
void getVal(T *data, const IntVect &pos) const noexcept
Copy all components beginning at component 0.
Definition AMReX_BaseFab.H:1471
BaseFab< T > & plus(T const &val, Box const &bx, DestComp dcomp, NumComps ncomp) noexcept
Do nothing if bx is empty.
Definition AMReX_BaseFab.H:3387
Real norm(int p, int scomp=0, int numcomp=1) const
A list of Boxes sharing a common IndexType.
Definition AMReX_BoxList.H:109
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:385
__host__ __device__ bool sameSize(const BoxND &b) const noexcept
Return true is Boxes same size, ie translates of each other,. It is an error if they have different t...
Definition AMReX_Box.H:312
__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__ bool ok() const noexcept
Return true if high bounds are >= low bounds and the index type is valid.
Definition AMReX_Box.H:229
GPU-compatible tuple.
Definition AMReX_Tuple.H:104
static int deviceId() noexcept
Definition AMReX_GpuDevice.cpp:828
static int devicePropMajor() noexcept
Definition AMReX_GpuDevice.H:206
Definition AMReX_GpuElixir.H:13
__host__ static __device__ constexpr IntVectND< dim > TheMinVector() noexcept
Definition AMReX_IntVect.H:848
T * data() noexcept
Definition AMReX_PODVector.H:672
Definition AMReX_Reduce.H:556
Type value()
Definition AMReX_Reduce.H:591
GpuTuple< Ts... > Type
Definition AMReX_Reduce.H:558
Definition AMReX_Reduce.H:715
void eval(MF const &mf, IntVect const &nghost, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:855
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
__host__ __device__ Dim3 ubound(Array4< T > const &a) noexcept
Return the inclusive upper bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1377
__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
std::array< T, N > Array
Definition AMReX_Array.H:31
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
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:54
__host__ __device__ bool almostEqual(T x, T y, int ulp=2)
Definition AMReX_Algorithm.H:140
__host__ __device__ AMREX_FORCE_INLINE T Exch(T *address, T val) noexcept
Definition AMReX_GpuAtomic.H:542
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:454
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
bool notInLaunchRegion() noexcept
Definition AMReX_GpuControl.H:89
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:440
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:310
Definition AMReX_Amr.cpp:50
MakeType
Definition AMReX_MakeType.H:7
@ make_deep_copy
Definition AMReX_MakeType.H:7
@ make_alias
Definition AMReX_MakeType.H:7
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
void placementNew(T *const ptr, Long n, RunOn run_on=RunOn::Device)
Start the lifetime of objects in a preallocated buffer.
Definition AMReX_BaseFab.H:152
@ F
Definition AMReX_Order.H:7
__host__ __device__ Array4< T > makeArray4(T *p, Box const &bx, int ncomp) noexcept
Definition AMReX_BaseFab.H:137
void placementDelete(T *const ptr, Long n, RunOn run_on=RunOn::Device)
Destroy objects previously constructed via placementNew.
Definition AMReX_BaseFab.H:173
RunOn
Definition AMReX_GpuControl.H:65
@ Host
Definition AMReX_GpuControl.H:65
@ Device
Definition AMReX_GpuControl.H:65
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Return the iterator begin coordinate of box as Dim3.
Definition AMReX_Box.H:2241
bool InitSNaN() noexcept
Whether new allocations are filled with signaling NaNs when enabled.
Definition AMReX.cpp:183
void fill_snan(T *p, std::size_t nelems)
Definition AMReX_MemPool.H:24
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
Long TotalBytesAllocatedInFabs() noexcept
Total bytes currently held by all BaseFab instances on this rank.
Definition AMReX_BaseFab.cpp:66
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
void BaseFab_Initialize()
Initialize BaseFab subsystem resources (profilers, stats counters, etc.).
Definition AMReX_BaseFab.cpp:30
void BaseFab_Finalize()
Release any BaseFab subsystem resources.
Definition AMReX_BaseFab.cpp:59
void ResetTotalBytesAllocatedInFabsHWM() noexcept
Reset the byte high-water mark reported by TotalBytesAllocatedInFabsHWM().
Definition AMReX_BaseFab.cpp:134
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:3028
BoxList boxDiff(const Box &b1in, const Box &b2)
Returns a BoxList defining the complement of b2 in b1in.
Definition AMReX_BoxList.cpp:621
@ y
Definition AMReX_Orientation.H:14
@ x
Definition AMReX_Orientation.H:14
@ z
Definition AMReX_Orientation.H:14
IndexTypeND< 3 > IndexType
IndexType is an alias for amrex::IndexTypeND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:41
Long TotalBytesAllocatedInFabsHWM() noexcept
High-water mark of BaseFab bytes since the last reset.
Definition AMReX_BaseFab.cpp:83
__host__ __device__ T abs(const GpuComplex< T > &a_z) noexcept
Return the absolute value of a complex number.
Definition AMReX_GpuComplex.H:361
Long TotalCellsAllocatedInFabsHWM() noexcept
High-water mark of Real BaseFab grid-point count since the last reset.
Definition AMReX_BaseFab.cpp:117
Long TotalCellsAllocatedInFabs() noexcept
Total spatial grid points (numPts) tracked for Real BaseFabs on this rank.
Definition AMReX_BaseFab.cpp:100
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:233
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
cudaStream_t gpuStream_t
Definition AMReX_GpuControl.H:79
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Return the iterator end coordinate of box as Dim3.
Definition AMReX_Box.H:2259
void update_fab_stats(Long n, Long s, size_t szt) noexcept
Definition AMReX_BaseFab.cpp:146
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:2174
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get the Idx-th element of IntVectND<dim>.
Definition AMReX_IntVect.H:1367
__host__ __device__ constexpr std::size_t size() const noexcept
Total number of elements in the ArrayND's index region.
Definition AMReX_Array4.H:659
__host__ __device__ T * ptr(idx... i) const noexcept
Multi-index ptr() for accessing pointer to element.
Definition AMReX_Array4.H:577
DataAllocator() noexcept=default
Default-construct; alloc/free will use The_Arena().
Arena * arena() const noexcept
Return the stored arena, or The_Arena() if none was set.
Definition AMReX_DataAllocator.H:41
void free(void *pt) const noexcept
Return the allocation at pt to the arena.
Definition AMReX_DataAllocator.H:36
Arena * m_arena
Stored arena pointer (may be null).
Definition AMReX_DataAllocator.H:22
void * alloc(std::size_t sz) const
Allocate sz bytes from the arena. Throws OutOfMemoryError on failure.
Definition AMReX_DataAllocator.H:31
Custom deleter that returns memory to an Arena.
Definition AMReX_DataAllocator.H:53
Destination-component descriptor.
Definition AMReX_BaseFab.H:105
int i
Definition AMReX_BaseFab.H:108
__host__ __device__ DestComp(int ai) noexcept
Definition AMReX_BaseFab.H:107
A simple struct holding 3 int values for a 3D index.
Definition AMReX_Dim3.H:24
int x
Definition AMReX_Dim3.H:24
int z
Definition AMReX_Dim3.H:24
int y
Definition AMReX_Dim3.H:24
Definition AMReX_TypeTraits.H:51
Number-of-components descriptor.
Definition AMReX_BaseFab.H:112
__host__ __device__ NumComps(int an) noexcept
Definition AMReX_BaseFab.H:114
int n
Definition AMReX_BaseFab.H:115
Source-component descriptor.
Definition AMReX_BaseFab.H:98
__host__ __device__ SrcComp(int ai) noexcept
Definition AMReX_BaseFab.H:100
int i
Definition AMReX_BaseFab.H:101