Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/AMReX-Codes/amrex in…
Browse files Browse the repository at this point in the history
…to solve_bicgstab-consistent-RT-casting
  • Loading branch information
eebasso committed Nov 14, 2023
2 parents ad35b04 + af1e1be commit ba82da0
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 137 deletions.
19 changes: 11 additions & 8 deletions Src/Base/AMReX_CArena.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include <AMReX_Arena.H>

#include <cstddef>
#include <set>
#include <vector>
#include <functional>
#include <iosfwd>
#include <map>
#include <mutex>
#include <unordered_set>
#include <functional>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

namespace amrex {

Expand Down Expand Up @@ -57,7 +58,7 @@ public:
* Try to shrink in-place
*/
[[nodiscard]] void*
shrink_in_place (void* pt, std::size_t sz) final;
shrink_in_place (void* pt, std::size_t new_size) final;

/**
* \brief Free up allocated memory. Merge neighboring free memory chunks
Expand Down Expand Up @@ -164,15 +165,15 @@ protected:
MemStat* m_stat;
};

//! The list of blocks allocated via ::operator new().
std::vector<std::pair<void*,std::size_t> > m_alloc;

/**
* \brief The type of our freelist and blocklist.
* We use a set sorted from lo to hi memory addresses.
*/
using NL = std::set<Node>;

//! The list of blocks allocated via ::operator new().
std::vector<std::pair<void*,std::size_t> > m_alloc;

/**
* \brief The free list of allocated but not currently used blocks.
* Maintained in lo to hi memory sorted order.
Expand All @@ -198,6 +199,8 @@ protected:


std::mutex carena_mutex;

friend std::ostream& operator<< (std::ostream& os, const CArena& arena);
};

}
Expand Down
96 changes: 94 additions & 2 deletions Src/Base/AMReX_CArena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace amrex {

#include <utility>
#include <cstring>
#include <iostream>

namespace amrex {

Expand Down Expand Up @@ -203,9 +204,61 @@ CArena::alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax)
}

void*
CArena::shrink_in_place (void* /*pt*/, std::size_t sz)
CArena::shrink_in_place (void* pt, std::size_t new_size)
{
return alloc(sz); // xxxxx TODO
if ((pt == nullptr) || (new_size == 0)) { return nullptr; }

new_size = Arena::align(new_size);

std::lock_guard<std::mutex> lock(carena_mutex);

auto busy_it = m_busylist.find(Node(pt,nullptr,0));
if (busy_it == m_busylist.end()) {
amrex::Abort("CArena::shrink_in_place: unknown pointer");
return nullptr;
}
AMREX_ASSERT(m_freelist.find(*busy_it) == m_freelist.end());

auto const old_size = busy_it->size();

if (new_size > old_size) {
amrex::Abort("CArena::shrink_in_place: wrong size. Cannot shrink to a larger size.");
return nullptr;
} else if (new_size == old_size) {
return pt;
} else {
auto const leftover_size = old_size - new_size;

void* pt2 = static_cast<char*>(pt) + new_size;
Node new_free_node(pt2, busy_it->owner(), leftover_size);

void* pt_end = static_cast<char*>(pt) + old_size;
auto free_it = m_freelist.find(Node(pt_end,nullptr,0));
if ((free_it == m_freelist.end()) || ! new_free_node.coalescable(*free_it)) {
m_freelist.insert(free_it, new_free_node);
} else {
auto& node = const_cast<Node&>(*free_it);
// This is safe because the free list is std::set and the
// modification of `block` does not change the order of elements
// in the container, even though Node's operator< uses block.
node.block(pt2);
node.size(leftover_size + node.size());
}

const_cast<Node&>(*busy_it).size(new_size);

m_actually_used -= leftover_size;

#ifdef AMREX_TINY_PROFILING
if (m_do_profiling) {
TinyProfiler::memory_free(old_size, busy_it->mem_stat());
auto* stat = TinyProfiler::memory_alloc(new_size, m_profiling_stats);
const_cast<Node&>(*busy_it).mem_stat(stat);
}
#endif

return pt;
}
}

void
Expand Down Expand Up @@ -439,4 +492,43 @@ CArena::PrintUsage (std::ostream& os, std::string const& name, std::string const
<< m_busylist.size() << " busy blocks, " << m_freelist.size() << " free blocks\n";
}

std::ostream& operator<< (std::ostream& os, const CArena& arena)
{
os << "CArea:\n"
<< " Hunk size: " << arena.m_hunk << "\n"
<< " Memory allocated: " << arena.m_used << "\n"
<< " Memory actually used: " << arena.m_actually_used << "\n";

if (arena.m_alloc.empty()) {
os << " No memory allocations\n";
} else {
os << " List of memory alloations: (address, size)\n";
for (auto const& a : arena.m_alloc) {
os << " " << a.first << ", " << a.second << "\n";
}
}

if (arena.m_freelist.empty()) {
os << " No free nodes\n";
} else {
os << " List of free nodes: (address, owner, size)\n";
for (auto const& a : arena.m_freelist) {
os << " " << a.block() << ", " << a.owner() << ", "
<< a.size() << "\n";
}
}

if (arena.m_busylist.empty()) {
os << " No busy nodes\n";
} else {
os << " List of busy nodes: (address, owner, size)\n";
for (auto const& a : arena.m_busylist) {
os << " " << a.block() << ", " << a.owner() << ", "
<< a.size() << "\n";
}
}

return os;
}

}
2 changes: 1 addition & 1 deletion Src/Base/AMReX_PlotFileDataImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ PlotFileDataImpl::get (int level, std::string const& varname) noexcept
int gid = mfi.index();
FArrayBox& dstfab = mf[mfi];
std::unique_ptr<FArrayBox> srcfab(m_vismf[level]->readFAB(gid, icomp));
dstfab.copy<RunOn::Host>(*srcfab);
dstfab.copy<RunOn::Device>(*srcfab);
}
}
return mf;
Expand Down
36 changes: 14 additions & 22 deletions Src/LinearSolvers/MLMG/AMReX_MLCGSolver.H
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,18 @@ MLCGSolverT<MF>::solve_bicgstab (MF& sol, const MF& rhs, RT eps_rel, RT eps_abs)

const int ncomp = sol.nComp();

const BoxArray& ba = sol.boxArray();
const DistributionMapping& dm = sol.DistributionMap();
const auto& factory = sol.Factory();

MF ph(ba, dm, ncomp, sol.nGrowVect(), MFInfo(), factory);
MF sh(ba, dm, ncomp, sol.nGrowVect(), MFInfo(), factory);
MF ph = Lp.make(amrlev, mglev, sol.nGrowVect());
MF sh = Lp.make(amrlev, mglev, sol.nGrowVect());
ph.setVal(RT(0.0));
sh.setVal(RT(0.0));

MF sorig(ba, dm, ncomp, nghost, MFInfo(), factory);
MF p (ba, dm, ncomp, nghost, MFInfo(), factory);
MF r (ba, dm, ncomp, nghost, MFInfo(), factory);
MF s (ba, dm, ncomp, nghost, MFInfo(), factory);
MF rh (ba, dm, ncomp, nghost, MFInfo(), factory);
MF v (ba, dm, ncomp, nghost, MFInfo(), factory);
MF t (ba, dm, ncomp, nghost, MFInfo(), factory);
MF sorig = Lp.make(amrlev, mglev, nghost);
MF p = Lp.make(amrlev, mglev, nghost);
MF r = Lp.make(amrlev, mglev, nghost);
MF s = Lp.make(amrlev, mglev, nghost);
MF rh = Lp.make(amrlev, mglev, nghost);
MF v = Lp.make(amrlev, mglev, nghost);
MF t = Lp.make(amrlev, mglev, nghost);

Lp.correctionResidual(amrlev, mglev, r, sol, rhs, MLLinOpT<MF>::BCMode::Homogeneous);

Expand Down Expand Up @@ -260,17 +256,13 @@ MLCGSolverT<MF>::solve_cg (MF& sol, const MF& rhs, RT eps_rel, RT eps_abs)

const int ncomp = sol.nComp();

const BoxArray& ba = sol.boxArray();
const DistributionMapping& dm = sol.DistributionMap();
const auto& factory = sol.Factory();

MF p(ba, dm, ncomp, sol.nGrowVect(), MFInfo(), factory);
MF p = Lp.make(amrlev, mglev, sol.nGrowVect());
p.setVal(RT(0.0));

MF sorig(ba, dm, ncomp, nghost, MFInfo(), factory);
MF r (ba, dm, ncomp, nghost, MFInfo(), factory);
MF z (ba, dm, ncomp, nghost, MFInfo(), factory);
MF q (ba, dm, ncomp, nghost, MFInfo(), factory);
MF sorig = Lp.make(amrlev, mglev, nghost);
MF r = Lp.make(amrlev, mglev, nghost);
MF z = Lp.make(amrlev, mglev, nghost);
MF q = Lp.make(amrlev, mglev, nghost);

sorig.LocalCopy(sol,0,0,ncomp,nghost);

Expand Down
35 changes: 18 additions & 17 deletions Tools/Plotfile/fextrema.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <AMReX.H>
#include <AMReX_Print.H>
#include <AMReX_ParReduce.H>
#include <AMReX_PlotFileUtil.H>
#include <AMReX_MultiFabUtil.H>
#include <algorithm>
Expand Down Expand Up @@ -80,23 +81,23 @@ void main_main()
pf.boxArray(ilev+1), ratio);
for (int ivar = 0; ivar < var_names.size(); ++ivar) {
const MultiFab& mf = pf.get(ilev, var_names[ivar]);
for (MFIter mfi(mf); mfi.isValid(); ++mfi) {
const Box& bx = mfi.validbox();
const auto lo = amrex::lbound(bx);
const auto hi = amrex::ubound(bx);
const auto& ifab = mask.array(mfi);
const auto& fab = mf.array(mfi);
for (int k = lo.z; k <= hi.z; ++k) {
for (int j = lo.y; j <= hi.y; ++j) {
for (int i = lo.x; i <= hi.x; ++i) {
if (ifab(i,j,k) == 0) {
vvmin[ivar] = std::min(fab(i,j,k),vvmin[ivar]);
vvmax[ivar] = std::max(fab(i,j,k),vvmax[ivar]);
}
}
}
}
}
auto const& ma = mf.const_arrays();
auto const& ima = mask.const_arrays();
auto rr = ParReduce(TypeList<ReduceOpMin,ReduceOpMax>{},
TypeList<Real,Real>{}, mf,
[=] AMREX_GPU_DEVICE (int bno, int i, int j, int k)
-> GpuTuple<Real,Real>
{
if (ima[bno](i,j,k) == 0) {
auto x = ma[bno](i,j,k);
return {x,x};
} else {
return {std::numeric_limits<Real>::max(),
std::numeric_limits<Real>::lowest()};
}
});
vvmin[ivar] = std::min(amrex::get<0>(rr), vvmin[ivar]);
vvmax[ivar] = std::max(amrex::get<1>(rr), vvmax[ivar]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tools/Plotfile/fsnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ void main_main()
gmx = std::log10(gmx);
}

BaseFab<unsigned char> intdat;
BaseFab<unsigned char> intdat(The_Pinned_Arena());
for (int idir = ndir_begin; idir < ndir_end; ++idir) {
intdat.resize(finebox[idir],1);
const int width = (idir == 0) ? finebox[idir].length(1) : finebox[idir].length(0);
const int height = (idir == 2) ? finebox[idir].length(1) : finebox[idir].length(2);
const auto& intarr = intdat.array();
const auto& realarr = datamf[idir].array(0);
Real fac = Real(253.999) / (gmx-gmn);
amrex::LoopOnCpu(finebox[idir], [=] (int i, int j, int k)
amrex::ParallelFor(finebox[idir], [=] AMREX_GPU_DEVICE (int i, int j, int k)
{
int jj = (idir == 2) ? height - 1 - j : j; // flip the data in second image direction
int kk = (idir == 2) ? k : height - 1 - k;
Expand Down
Loading

0 comments on commit ba82da0

Please sign in to comment.