1 #ifndef AMREX_ALG_VECTOR_H_
2 #define AMREX_ALG_VECTOR_H_
3 #include <AMReX_Config.H>
13 #include <type_traits>
17 template <
typename T,
typename Allocator = DefaultAllocator<T> >
38 void define (Long global_size);
80 void scale (T scale_factor);
83 [[nodiscard]] T
sum (
bool local =
false)
const;
85 [[nodiscard]] T
norminf (
bool local =
false)
const;
86 [[nodiscard]] T
norm2 (
bool local =
false)
const;
88 template <
typename FAB,
89 std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
90 std::is_same_v<T,typename FAB::value_type>,
int> = 0>
93 template <
typename FAB,
94 std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
95 std::is_same_v<T,typename FAB::value_type>,
int> = 0>
107 template <
typename T,
typename Allocator>
109 : m_partition(global_size),
110 m_begin(m_partition[ParallelDescriptor::
MyProc()]),
111 m_end(m_partition[ParallelDescriptor::
MyProc()+1]),
112 m_data(m_end-m_begin)
114 static_assert(std::is_floating_point<T>::value,
"AlgVector is for floating point type only");
117 template <
typename T,
typename Allocator>
119 : m_partition(std::move(partition)),
120 m_begin(m_partition[ParallelDescriptor::
MyProc()]),
121 m_end(m_partition[ParallelDescriptor::
MyProc()+1]),
122 m_data(m_end-m_begin)
124 static_assert(std::is_floating_point<T>::value,
"AlgVector is for floating point type only");
127 template <
typename T,
typename Allocator>
130 m_partition.define(global_size);
133 m_data.resize(m_end-m_begin);
136 template <
typename T,
typename Allocator>
139 m_partition = std::move(partition);
142 m_data.resize(m_end-m_begin);
145 template <
typename T,
typename Allocator>
152 template <
typename T,
typename Allocator>
155 Long n = m_data.size();
156 T* p = m_data.data();
160 template <
typename T,
typename Allocator>
167 template <
typename T,
typename Allocator>
170 Long n = m_data.size();
172 T* dst = m_data.data();
173 T
const* src = rhs.
data();
181 template <
typename T,
typename Allocator>
188 template <
typename T,
typename Allocator>
191 Long n = m_data.size();
193 T* dst = m_data.data();
194 T
const* src = rhs.
data();
198 template <
typename T,
typename Allocator>
201 scaleAsync(scale_factor);
205 template <
typename T,
typename Allocator>
208 Long n = m_data.size();
209 T* p = m_data.data();
213 template <
typename T,
typename Allocator>
216 Long n = m_data.size();
217 T
const* p = m_data.data();
228 template <
typename T,
typename Allocator>
231 Long n = m_data.size();
232 T
const* p = m_data.data();
243 template <
typename T,
typename Allocator>
246 Long n = m_data.size();
247 T
const* p = m_data.data();
258 template <
typename T,
typename Allocator>
259 template <typename FAB, std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
260 std::is_same_v<T,typename FAB::value_type>,
int> >
266 T* p = m_data.data();
269 p += mfi.validbox().numPts();
272 #if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
276 fa[mfi].template copyToMem<RunOn::Device>(mfi.validbox(), 0, 1, dptrs[mfi]);
280 template <
typename T,
typename Allocator>
281 template <typename FAB, std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
282 std::is_same_v<T,typename FAB::value_type>,
int> >
288 T
const* p = m_data.data();
291 p += mfi.validbox().numPts();
294 #if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
298 fa[mfi].template copyFromMem<RunOn::Device>(mfi.validbox(), 0, 1, dptrs[mfi]);
302 template <
typename T,
typename Allocator>
306 ofs << m_begin <<
" " << m_end <<
"\n";
311 T
const* p = hv.
data();
315 for (Long i = 0, N = m_data.size(); i < N; ++i) {
316 ofs << i+m_begin <<
" " << p[i] <<
"\n";
320 template <
class V,
class Enable =
void>
struct IsAlgVector : std::false_type {};
323 struct IsAlgVector<V, std::enable_if_t<std::is_same_v<AlgVector<typename V::value_type,
324 typename V::allocator_type>,
328 template <
typename V1,
typename F>
329 std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value>
332 Long n =
x.numLocalRows();
340 template <
typename V1,
typename V2,
typename F>
341 std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
342 IsAlgVector<std::decay_t<V2> >::value>
346 Long n =
x.numLocalRows();
355 template <
typename V1,
typename V2,
typename V3,
typename F>
356 std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
357 IsAlgVector<std::decay_t<V2> >::value &&
358 IsAlgVector<std::decay_t<V3> >::value>
363 Long n =
x.numLocalRows();
369 f(px[i], py[i], pz[i]);
373 template <
typename V1,
typename V2,
typename V3,
typename V4,
typename F>
374 std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
375 IsAlgVector<std::decay_t<V2> >::value &&
376 IsAlgVector<std::decay_t<V3> >::value &&
377 IsAlgVector<std::decay_t<V4> >::value>
378 ForEach (V1 & x, V2 & y, V3 & z, V4 & a, F
const&
f)
383 Long n =
x.numLocalRows();
390 f(px[i], py[i], pz[i], pa[i]);
394 template <
typename V1,
typename V2,
typename V3,
typename V4,
typename V5,
typename F>
395 std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
396 IsAlgVector<std::decay_t<V2> >::value &&
397 IsAlgVector<std::decay_t<V3> >::value &&
398 IsAlgVector<std::decay_t<V4> >::value &&
399 IsAlgVector<std::decay_t<V5> >::value>
400 ForEach (V1 & x, V2 & y, V3 & z, V4 & a, V5 & b, F
const&
f)
406 Long n =
x.numLocalRows();
414 f(px[i], py[i], pz[i], pa[i], pb[i]);
418 template <
typename T>
422 Long n =
x.numLocalRows();
423 auto const* px =
x.data();
424 auto const* py = y.
data();
427 return px[i] * py[i];
435 template <
typename T>
442 template <
typename T>
#define AMREX_ASSERT(EX)
Definition: AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_RESTRICT
Definition: AMReX_Extension.H:37
#define AMREX_GPU_DEVICE
Definition: AMReX_GpuQualifiers.H:18
Definition: AMReX_AlgPartition.H:14
Long numGlobalRows() const
Definition: AMReX_AlgPartition.H:28
bool empty() const
Definition: AMReX_AlgPartition.H:25
Definition: AMReX_AlgVector.H:19
void setVal(T val)
Definition: AMReX_AlgVector.H:146
Long numLocalRows() const
Definition: AMReX_AlgVector.H:45
T norminf(bool local=false) const
Definition: AMReX_AlgVector.H:229
AlgPartition const & partition() const
Definition: AMReX_AlgVector.H:43
void setValAsync(T val)
Definition: AMReX_AlgVector.H:153
AMREX_FORCE_INLINE Table1D< T, Long > view()
Definition: AMReX_AlgVector.H:67
Long globalBegin() const
Inclusive global index begin.
Definition: AMReX_AlgVector.H:49
bool empty() const
Definition: AMReX_AlgVector.H:41
void plus(AlgVector< T > const &rhs)
Definition: AMReX_AlgVector.H:182
AlgPartition m_partition
Definition: AMReX_AlgVector.H:101
AlgVector(AlgVector< T, Allocator > &&) noexcept=default
Long m_begin
Definition: AMReX_AlgVector.H:102
Long m_end
Definition: AMReX_AlgVector.H:103
void scale(T scale_factor)
Definition: AMReX_AlgVector.H:199
AlgVector & operator=(AlgVector< T, Allocator > const &)=delete
void copy(AlgVector< T > const &rhs)
Definition: AMReX_AlgVector.H:161
Long globalEnd() const
Exclusive global index end.
Definition: AMReX_AlgVector.H:51
void copyFrom(FabArray< FAB > const &fa)
Definition: AMReX_AlgVector.H:261
Long numGlobalRows() const
Definition: AMReX_AlgVector.H:46
void plusAsync(AlgVector< T > const &rhs)
Definition: AMReX_AlgVector.H:189
T * data()
Definition: AMReX_AlgVector.H:54
T norm2(bool local=false) const
Definition: AMReX_AlgVector.H:244
T const * data() const
Definition: AMReX_AlgVector.H:53
void define(Long global_size)
Definition: AMReX_AlgVector.H:128
void scaleAsync(T scale_factor)
Definition: AMReX_AlgVector.H:206
Vec m_data
Definition: AMReX_AlgVector.H:104
AlgVector(AlgVector< T, Allocator > const &)=delete
T sum(bool local=false) const
Definition: AMReX_AlgVector.H:214
AMREX_FORCE_INLINE Table1D< T const, Long > const_view() const
Definition: AMReX_AlgVector.H:62
AMREX_FORCE_INLINE Table1D< T const, Long > view() const
Definition: AMReX_AlgVector.H:57
Allocator allocator_type
Definition: AMReX_AlgVector.H:22
void copyTo(FabArray< FAB > &fa) const
Definition: AMReX_AlgVector.H:283
void copyAsync(AlgVector< T > const &rhs)
Definition: AMReX_AlgVector.H:168
T value_type
Definition: AMReX_AlgVector.H:21
void printToFile(std::string const &file) const
Definition: AMReX_AlgVector.H:303
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition: AMReX_FabArrayBase.H:94
bool is_cell_centered() const noexcept
This tests on whether the FabArray is cell-centered.
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition: AMReX_FabArrayBase.H:130
An Array of FortranArrayBox(FAB)-like Objects.
Definition: AMReX_FabArray.H:344
a one-thingy-per-box distributed object
Definition: AMReX_LayoutData.H:13
Definition: AMReX_MFIter.H:57
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition: AMReX_MFIter.H:141
Definition: AMReX_PODVector.H:246
size_type size() const noexcept
Definition: AMReX_PODVector.H:575
T * data() noexcept
Definition: AMReX_PODVector.H:593
@ FAB
Definition: AMReX_AmrvisConstants.H:86
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition: AMReX_GpuDevice.H:279
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition: AMReX_GpuContainers.H:233
void streamSynchronize() noexcept
Definition: AMReX_GpuDevice.H:237
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition: AMReX_GpuDevice.H:265
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void * memcpy(void *dest, const void *src, std::size_t count)
Definition: AMReX_GpuUtility.H:214
int MyProc()
Definition: AMReX_MPMD.cpp:117
void Sum(T &v, MPI_Comm comm)
Definition: AMReX_ParallelReduce.H:204
void Max(KeyValuePair< K, V > &vi, MPI_Comm comm)
Definition: AMReX_ParallelReduce.H:126
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition: AMReX_ParallelContext.H:70
int MyProc() noexcept
return the rank number local to the current Parallel Context
Definition: AMReX_ParallelDescriptor.H:125
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
Definition: AMReX_Amr.cpp:49
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition: AMReX_CTOParallelForImpl.H:200
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T abs(const GpuComplex< T > &a_z) noexcept
Return the absolute value of a complex number.
Definition: AMReX_GpuComplex.H:356
void Axpy(AlgVector< T > &y, T a, AlgVector< T > const &x, bool async=false)
Definition: AMReX_AlgVector.H:436
void LinComb(MF &dst, typename MF::value_type a, MF const &src_a, int acomp, typename MF::value_type b, MF const &src_b, int bcomp, int dcomp, int ncomp, IntVect const &nghost)
dst = a*src_a + b*src_b
Definition: AMReX_FabArrayUtility.H:1662
constexpr void ForEach(TypeList< Ts... >, F &&f)
For each type t in TypeList, call f(t)
Definition: AMReX_TypeList.H:78
FAB::value_type Dot(FabArray< FAB > const &x, int xcomp, FabArray< FAB > const &y, int ycomp, int ncomp, IntVect const &nghost, bool local=false)
Compute dot products of two FabArrays.
Definition: AMReX_FabArrayUtility.H:1554
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Return the square root of a complex number.
Definition: AMReX_GpuComplex.H:373
Definition: AMReX_AlgVector.H:320
Definition: AMReX_TableData.H:20