1#ifndef AMREX_ALG_VECTOR_H_
2#define AMREX_ALG_VECTOR_H_
3#include <AMReX_Config.H>
17template <
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>
107template <
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");
117template <
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");
127template <
typename T,
typename Allocator>
130 m_partition.define(global_size);
133 m_data.resize(m_end-m_begin);
136template <
typename T,
typename Allocator>
139 m_partition = std::move(partition);
142 m_data.resize(m_end-m_begin);
145template <
typename T,
typename Allocator>
152template <
typename T,
typename Allocator>
155 Long n = m_data.size();
156 T* p = m_data.data();
160template <
typename T,
typename Allocator>
167template <
typename T,
typename Allocator>
170 Long n = m_data.size();
172 T* dst = m_data.data();
173 T
const* src = rhs.
data();
177template <
typename T,
typename Allocator>
184template <
typename T,
typename Allocator>
187 Long n = m_data.size();
189 T* dst = m_data.data();
190 T
const* src = rhs.
data();
194template <
typename T,
typename Allocator>
197 scaleAsync(scale_factor);
201template <
typename T,
typename Allocator>
204 Long n = m_data.size();
205 T* p = m_data.data();
209template <
typename T,
typename Allocator>
212 Long n = m_data.size();
213 T
const* p = m_data.data();
224template <
typename T,
typename Allocator>
227 Long n = m_data.size();
228 T
const* p = m_data.data();
231 return amrex::Math::abs(p[i]);
239template <
typename T,
typename Allocator>
242 Long n = m_data.size();
243 T
const* p = m_data.data();
254template <
typename T,
typename Allocator>
255template <typename FAB, std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
256 std::is_same_v<T,typename FAB::value_type>,
int> >
262 T* p = m_data.data();
265 p += mfi.validbox().numPts();
268#if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
272 fa[mfi].template copyToMem<RunOn::Device>(mfi.validbox(), 0, 1, dptrs[mfi]);
276template <
typename T,
typename Allocator>
277template <typename FAB, std::enable_if_t<amrex::IsBaseFab<FAB>::value &&
278 std::is_same_v<T,typename FAB::value_type>,
int> >
284 T
const* p = m_data.data();
287 p += mfi.validbox().numPts();
290#if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
294 fa[mfi].template copyFromMem<RunOn::Device>(mfi.validbox(), 0, 1, dptrs[mfi]);
298template <
typename T,
typename Allocator>
302 ofs << m_begin <<
" " << m_end <<
"\n";
307 T
const* p = hv.
data();
311 for (Long i = 0, N = m_data.size(); i < N; ++i) {
312 ofs << i+m_begin <<
" " << p[i] <<
"\n";
316template <
class V,
class Enable =
void>
struct IsAlgVector : std::false_type {};
319struct IsAlgVector<V, std::enable_if_t<std::is_same_v<AlgVector<typename V::value_type,
320 typename V::allocator_type>,
324template <
typename V1,
typename F>
325std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value>
328 Long n =
x.numLocalRows();
336template <
typename V1,
typename V2,
typename F>
337std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
338 IsAlgVector<std::decay_t<V2> >::value>
342 Long n =
x.numLocalRows();
351template <
typename V1,
typename V2,
typename V3,
typename F>
352std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
353 IsAlgVector<std::decay_t<V2> >::value &&
354 IsAlgVector<std::decay_t<V3> >::value>
359 Long n =
x.numLocalRows();
365 f(px[i], py[i], pz[i]);
369template <
typename V1,
typename V2,
typename V3,
typename V4,
typename F>
370std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
371 IsAlgVector<std::decay_t<V2> >::value &&
372 IsAlgVector<std::decay_t<V3> >::value &&
373 IsAlgVector<std::decay_t<V4> >::value>
379 Long n =
x.numLocalRows();
386 f(px[i], py[i], pz[i], pa[i]);
390template <
typename V1,
typename V2,
typename V3,
typename V4,
typename V5,
typename F>
391std::enable_if_t<IsAlgVector<std::decay_t<V1> >::value &&
392 IsAlgVector<std::decay_t<V2> >::value &&
393 IsAlgVector<std::decay_t<V3> >::value &&
394 IsAlgVector<std::decay_t<V4> >::value &&
395 IsAlgVector<std::decay_t<V5> >::value>
402 Long n =
x.numLocalRows();
410 f(px[i], py[i], pz[i], pa[i], pb[i]);
418 Long n =
x.numLocalRows();
419 auto const* px =
x.data();
420 auto const* py =
y.data();
423 return px[i] * py[i];
#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:225
Table1D< T, Long > view()
Definition AMReX_AlgVector.H:67
AlgPartition const & partition() const
Definition AMReX_AlgVector.H:43
Table1D< T const, Long > const_view() const
Definition AMReX_AlgVector.H:62
void setValAsync(T val)
Definition AMReX_AlgVector.H:153
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:178
T const * data() const
Definition AMReX_AlgVector.H:53
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:195
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:257
T * data()
Definition AMReX_AlgVector.H:54
Long numGlobalRows() const
Definition AMReX_AlgVector.H:46
void plusAsync(AlgVector< T > const &rhs)
Definition AMReX_AlgVector.H:185
T norm2(bool local=false) const
Definition AMReX_AlgVector.H:240
AlgVector & operator=(AlgVector< T, Allocator > const &)=delete
void define(Long global_size)
Definition AMReX_AlgVector.H:128
void scaleAsync(T scale_factor)
Definition AMReX_AlgVector.H:202
Vec m_data
Definition AMReX_AlgVector.H:104
AlgVector(AlgVector< T, Allocator > const &)=delete
T sum(bool local=false) const
Definition AMReX_AlgVector.H:210
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:279
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:299
bool is_cell_centered() const noexcept
This tests on whether the FabArray is cell-centered.
Definition AMReX_FabArrayBase.cpp:2701
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:131
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition AMReX_FabArrayBase.H:95
An Array of FortranArrayBox(FAB)-like Objects.
Definition AMReX_FabArray.H:345
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:297
size_type size() const noexcept
Definition AMReX_PODVector.H:637
T * data() noexcept
Definition AMReX_PODVector.H:655
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:317
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:260
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:303
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
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:191
void Axpy(AlgVector< T > &y, T a, AlgVector< T > const &x, bool async=false)
Definition AMReX_AlgVector.H:432
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:1957
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:1621
Definition AMReX_AlgVector.H:316
Definition AMReX_TableData.H:20