Skip to content

Commit 3407e87

Browse files
authored
Add a few free functions for MLMG (#3680)
These are useful when we use Array<MultiFab,AMREX_SPACEDIM> as the data type for MLMG.
1 parent 85462ce commit 3407e87

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed

Src/Base/AMReX_FabArrayBase.H

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ public:
721721

722722
};
723723

724+
[[nodiscard]] int nComp (FabArrayBase const& fa);
725+
[[nodiscard]] IntVect nGrowVect (FabArrayBase const& fa);
726+
[[nodiscard]] BoxArray const& boxArray (FabArrayBase const& fa);
727+
[[nodiscard]] DistributionMapping const& DistributionMap (FabArrayBase const& fa);
728+
724729
#ifdef BL_USE_MPI
725730
bool CheckRcvStats (Vector<MPI_Status>& recv_stats, const Vector<std::size_t>& recv_size, int tag);
726731
#endif

Src/Base/AMReX_FabArrayBase.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,4 +2699,24 @@ FabArrayBase::flushParForCache ()
26992699

27002700
#endif
27012701

2702+
int nComp (FabArrayBase const& fa)
2703+
{
2704+
return fa.nComp();
2705+
}
2706+
2707+
IntVect nGrowVect (FabArrayBase const& fa)
2708+
{
2709+
return fa.nGrowVect();
2710+
}
2711+
2712+
BoxArray const& boxArray (FabArrayBase const& fa)
2713+
{
2714+
return fa.boxArray();
2715+
}
2716+
2717+
DistributionMapping const& DistributionMap (FabArrayBase const& fa)
2718+
{
2719+
return fa.DistributionMap();
2720+
}
2721+
27022722
}

Src/Base/AMReX_FabArrayUtility.H

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,193 @@ Dot (FabArray<FAB> const& x, int xcomp, FabArray<FAB> const& y, int ycomp, int n
16021602
return sm;
16031603
}
16041604

1605+
//! dst = val
1606+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1607+
void setVal (MF& dst, typename MF::value_type val)
1608+
{
1609+
dst.setVal(val);
1610+
}
1611+
1612+
//! dst = val in ghost cells.
1613+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1614+
void setBndry (MF& dst, typename MF::value_type val, int scomp, int ncomp)
1615+
{
1616+
dst.setBndry(val, scomp, ncomp);
1617+
}
1618+
1619+
//! dst = src
1620+
template <class DMF, class SMF,
1621+
std::enable_if_t<IsMultiFabLike_v<DMF> &&
1622+
IsMultiFabLike_v<SMF>, int> = 0>
1623+
void LocalCopy (DMF& dst, SMF const& src, int scomp, int dcomp,
1624+
int ncomp, IntVect const& nghost)
1625+
{
1626+
amrex::Copy(dst, src, scomp, dcomp, ncomp, nghost);
1627+
}
1628+
1629+
//! dst += src
1630+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1631+
void LocalAdd (MF& dst, MF const& src, int scomp, int dcomp,
1632+
int ncomp, IntVect const& nghost)
1633+
{
1634+
amrex::Add(dst, src, scomp, dcomp, ncomp, nghost);
1635+
}
1636+
1637+
//! dst += a * src
1638+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1639+
void Saxpy (MF& dst, typename MF::value_type a, MF const& src, int scomp, int dcomp,
1640+
int ncomp, IntVect const& nghost)
1641+
{
1642+
MF::Saxpy(dst, a, src, scomp, dcomp, ncomp, nghost);
1643+
}
1644+
1645+
//! dst = src + a * dst
1646+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1647+
void Xpay (MF& dst, typename MF::value_type a, MF const& src, int scomp, int dcomp,
1648+
int ncomp, IntVect const& nghost)
1649+
{
1650+
MF::Xpay(dst, a, src, scomp, dcomp, ncomp, nghost);
1651+
}
1652+
1653+
//! dst = src w/ MPI communication
1654+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>, int> = 0>
1655+
void ParallelCopy (MF& dst, MF const& src, int scomp, int dcomp, int ncomp,
1656+
IntVect const& ng_src = IntVect(0),
1657+
IntVect const& ng_dst = IntVect(0),
1658+
Periodicity const& period = Periodicity::NonPeriodic())
1659+
{
1660+
dst.ParallelCopy(src, scomp, dcomp, ncomp, ng_src, ng_dst, period);
1661+
}
1662+
1663+
template <class MF, std::enable_if_t<IsMultiFabLike_v<MF>, int> = 0>
1664+
[[nodiscard]] typename MF::value_type
1665+
norminf (MF const& mf, int scomp, int ncomp, IntVect const& nghost,
1666+
bool local = false)
1667+
{
1668+
return mf.norminf(scomp, ncomp, nghost, local);
1669+
}
1670+
1671+
//! dst = val
1672+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1673+
void setVal (Array<MF,N>& dst, typename MF::value_type val)
1674+
{
1675+
for (auto& mf: dst) {
1676+
mf.setVal(val);
1677+
}
1678+
}
1679+
1680+
//! dst = val in ghost cells.
1681+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1682+
void setBndry (Array<MF,N>& dst, typename MF::value_type val, int scomp, int ncomp)
1683+
{
1684+
for (auto& mf : dst) {
1685+
mf.setBndry(val, scomp, ncomp);
1686+
}
1687+
}
1688+
1689+
//! dst = src
1690+
template <class DMF, class SMF, std::size_t N,
1691+
std::enable_if_t<IsMultiFabLike_v<DMF> &&
1692+
IsMultiFabLike_v<SMF>, int> = 0>
1693+
void LocalCopy (Array<DMF,N>& dst, Array<SMF,N> const& src, int scomp, int dcomp,
1694+
int ncomp, IntVect const& nghost)
1695+
{
1696+
for (std::size_t i = 0; i < N; ++i) {
1697+
amrex::Copy(dst[i], src[i], scomp, dcomp, ncomp, nghost);
1698+
}
1699+
}
1700+
1701+
//! dst += src
1702+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1703+
void LocalAdd (Array<MF,N>& dst, Array<MF,N> const& src, int scomp, int dcomp,
1704+
int ncomp, IntVect const& nghost)
1705+
{
1706+
for (std::size_t i = 0; i < N; ++i) {
1707+
amrex::Add(dst[i], src[i], scomp, dcomp, ncomp, nghost);
1708+
}
1709+
}
1710+
1711+
//! dst += a * src
1712+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1713+
void Saxpy (Array<MF,N>& dst, typename MF::value_type a,
1714+
Array<MF,N> const& src, int scomp, int dcomp, int ncomp,
1715+
IntVect const& nghost)
1716+
{
1717+
for (std::size_t i = 0; i < N; ++i) {
1718+
MF::Saxpy(dst[i], a, src[i], scomp, dcomp, ncomp, nghost);
1719+
}
1720+
}
1721+
1722+
//! dst = src + a * dst
1723+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>,int> = 0>
1724+
void Xpay (Array<MF,N>& dst, typename MF::value_type a,
1725+
Array<MF,N> const& src, int scomp, int dcomp, int ncomp,
1726+
IntVect const& nghost)
1727+
{
1728+
for (std::size_t i = 0; i < N; ++i) {
1729+
MF::Xpay(dst[i], a, src[i], scomp, dcomp, ncomp, nghost);
1730+
}
1731+
}
1732+
1733+
//! dst = src w/ MPI communication
1734+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>, int> = 0>
1735+
void ParallelCopy (Array<MF,N>& dst, Array<MF,N> const& src,
1736+
int scomp, int dcomp, int ncomp,
1737+
IntVect const& ng_src = IntVect(0),
1738+
IntVect const& ng_dst = IntVect(0),
1739+
Periodicity const& period = Periodicity::NonPeriodic())
1740+
{
1741+
for (std::size_t i = 0; i < N; ++i) {
1742+
dst[i].ParallelCopy(src[i], scomp, dcomp, ncomp, ng_src, ng_dst, period);
1743+
}
1744+
}
1745+
1746+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF>, int> = 0>
1747+
[[nodiscard]] typename MF::value_type
1748+
norminf (Array<MF,N> const& mf, int scomp, int ncomp, IntVect const& nghost,
1749+
bool local = false)
1750+
{
1751+
auto r = typename MF::value_type(0);
1752+
for (std::size_t i = 0; i < N; ++i) {
1753+
auto tmp = mf[i].norminf(scomp, ncomp, nghost, true);
1754+
r = std::max(r,tmp);
1755+
}
1756+
if (!local) {
1757+
ParallelAllReduce::Max(r, ParallelContext::CommunicatorSub());
1758+
}
1759+
return r;
1760+
}
1761+
1762+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF> && (N > 0),
1763+
int> = 0>
1764+
[[nodiscard]] int nComp (Array<MF,N> const& mf)
1765+
{
1766+
return mf[0].nComp();
1767+
}
1768+
1769+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF> && (N > 0),
1770+
int> = 0>
1771+
[[nodiscard]] IntVect nGrowVect (Array<MF,N> const& mf)
1772+
{
1773+
return mf[0].nGrowVect();
1774+
}
1775+
1776+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF> && (N > 0),
1777+
int> = 0>
1778+
[[nodiscard]] BoxArray const&
1779+
boxArray (Array<MF,N> const& mf)
1780+
{
1781+
return mf[0].boxArray();
1782+
}
1783+
1784+
template <class MF, std::size_t N, std::enable_if_t<IsMultiFabLike_v<MF> && (N > 0),
1785+
int> = 0>
1786+
[[nodiscard]] DistributionMapping const&
1787+
DistributionMap (Array<MF,N> const& mf)
1788+
{
1789+
return mf[0].DistributionMap();
1790+
}
1791+
16051792
}
16061793

16071794
#endif

Src/Base/AMReX_TypeTraits.H

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ namespace amrex
3737
template <class A>
3838
inline constexpr bool IsFabArray_v = IsFabArray<A>::value;
3939

40+
template <class M, class Enable = void>
41+
struct IsMultiFabLike : std::false_type {};
42+
//
43+
template <class M>
44+
struct IsMultiFabLike<M, std::enable_if_t<IsFabArray_v<M> &&
45+
IsBaseFab_v<typename M::fab_type> > >
46+
: std::true_type {};
47+
//
48+
template <class M>
49+
inline constexpr bool IsMultiFabLike_v = IsMultiFabLike<M>::value;
50+
51+
4052
template <bool B, class T = void>
4153
using EnableIf_t = typename std::enable_if<B,T>::type;
4254

0 commit comments

Comments
 (0)