Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CarpetX: New reduction sumloc #286

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BoxInBox/test/levels1.par
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/levels2.par
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/levels20.par
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/levels3.par
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/regions2.par
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/regions3.par
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions BoxInBox/test/unigrid.par
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
4 changes: 4 additions & 0 deletions CarpetX/param.ccl
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ BOOLEAN out_norm_omit_unstable "Omit unstable norms (e.g. maxloc)" STEERABLE=alw
{
} no

BOOLEAN out_norm_omit_sumloc_for_backward_compatibility "Omit sumloc norms" STEERABLE=always
{
} no



KEYWORD openpmd_format "openPMD file format" STEERABLE=always
Expand Down
3 changes: 3 additions & 0 deletions CarpetX/src/io_meta.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ YAML::Emitter &operator<<(YAML::Emitter &yaml, const reduction_t &reduction) {
case reduction_t::maximum_location:
yaml << "maximum_location";
break;
case reduction_t::sum_location:
yaml << "sum_location";
break;
default:
assert(0);
}
Expand Down
1 change: 1 addition & 0 deletions CarpetX/src/io_meta.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum class reduction_t {
norm_inf,
minimum_location,
maximum_location,
sum_location,
};

struct output_file_description_t {
Expand Down
15 changes: 14 additions & 1 deletion CarpetX/src/io_norm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ void OutputNorms(const cGH *restrict cctkGH) {
reductions.push_back(buf.str());
}
}
if (!out_norm_omit_sumloc_for_backward_compatibility) {
for (int d = 0; d < dim; ++d) {
std::ostringstream buf;
buf << "sumloc[" << d << "]";
reductions.push_back(buf.str());
}
}

if (is_root) {
static std::once_flag create_directory;
Expand Down Expand Up @@ -215,6 +222,10 @@ void OutputNorms(const cGH *restrict cctkGH) {
for (int d = 0; d < dim; ++d)
file << sep << red.maxloc[d];
}
if (!out_norm_omit_sumloc_for_backward_compatibility) {
for (int d = 0; d < dim; ++d)
file << sep << red.sumloc[d];
}
}
}

Expand All @@ -240,8 +251,10 @@ void OutputNorms(const cGH *restrict cctkGH) {
ofd.reductions.push_back(reduction_t::minimum_location);
ofd.reductions.push_back(reduction_t::maximum_location);
}
if (!out_norm_omit_sumloc_for_backward_compatibility)
ofd.reductions.push_back(reduction_t::sum_location);
ofd.format_name = "CarpetX/norms/TSV";
ofd.format_version = {1, 0, 0};
ofd.format_version = {1, 1, 0};

OutputMeta_RegisterOutputFile(std::move(ofd));
}
Expand Down
72 changes: 44 additions & 28 deletions CarpetX/src/reduction.hxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CARPETX_CARPETX_REDUCTION_HXX
#define CARPETX_CARPETX_REDUCTION_HXX

#include "defs.hxx"
#include "vect.hxx"

#include <cctk.h>
Expand All @@ -14,28 +15,16 @@ namespace CarpetX {
using namespace std;
using namespace Arith;

template <typename T> constexpr T pow21(T x) noexcept { return x * x; }

template <typename T> constexpr T fmax1(T x, T y) noexcept {
if (isnan(x))
return x;
if (isnan(y))
return y;
return fmax(x, y);
}
template <typename T> constexpr T fmin1(T x, T y) noexcept {
if (isnan(x))
return x;
if (isnan(y))
return y;
return fmin(x, y);
}

template <typename T, int D> struct reduction {
// TODO: contains_inf, contains_nan?
T min, max, sum, sum2;
T vol, maxabs, sumabs, sum2abs;
vect<T, D> minloc, maxloc;
vect<T, D> minloc, maxloc, sumloc;

// We currently omit minloc/maxloc/sumloc (TODO: fix this)
using tuple_type = amrex::GpuTuple<T, T, T, T, T, T, T, T>;
constexpr reduction(tuple_type);
constexpr operator tuple_type() const;

constexpr reduction();
constexpr reduction(const vect<T, D> &p, const T &V, const T &x);
Expand All @@ -49,9 +38,9 @@ template <typename T, int D> struct reduction {

constexpr T avg() const noexcept { return sum / vol; }
constexpr T sdv() const noexcept {
// Splitting pow21(sum/vol) improves floating-point accuracy
// return sqrt(fmax1(T(0), sum2 / vol - pow21(sum / vol)));
return sqrt(fmax1(T(0), sum2 / vol - pow21(sum) / pow21(vol)));
// Splitting pow2(sum/vol) improves floating-point accuracy
// return sqrt(max1(T(0), sum2 / vol - pow2(sum / vol)));
return sqrt(max1(T(0), sum2 / vol - pow2(sum) / pow2(vol)));
}
constexpr T norm0() const noexcept { return vol; }
constexpr T norm1() const noexcept { return sumabs / vol; }
Expand All @@ -62,27 +51,42 @@ template <typename T, int D> struct reduction {
friend ostream &operator<<(ostream &os, const reduction<T1, D1> &red);
};

template <typename T, int D>
constexpr reduction<T, D>::reduction(tuple_type tuple)
: min(amrex::get<0>(tuple)), max(amrex::get<1>(tuple)),
sum(amrex::get<2>(tuple)), sum2(amrex::get<3>(tuple)),
vol(amrex::get<4>(tuple)), maxabs(amrex::get<5>(tuple)),
sumabs(amrex::get<6>(tuple)), sum2abs(amrex::get<7>(tuple)), minloc{},
maxloc{}, sumloc{} {}

template <typename T, int D>
constexpr reduction<T, D>::operator reduction<T, D>::tuple_type() const {
return tuple_type{min, max, sum, sum2, vol, maxabs, sumabs, sum2abs};
}

template <typename T, int D>
constexpr reduction<T, D>::reduction()
: min(1.0 / 0.0), max(-1.0 / 0.0), sum(0.0), sum2(0.0), vol(0.0),
maxabs(0.0), sumabs(0.0), sum2abs(0.0),
minloc(vect<T, D>::pure(0.0 / 0.0)), maxloc(vect<T, D>::pure(0.0 / 0.0)) {
}
minloc(vect<T, D>::pure(0.0 / 0.0)), maxloc(vect<T, D>::pure(0.0 / 0.0)),
sumloc(vect<T, D>::pure(0.0)) {}

template <typename T, int D>
constexpr reduction<T, D>::reduction(const vect<T, D> &p, const T &V,
const T &x)
: min(x), max(x), sum(V * x), sum2(V * pow21(x)), vol(V), maxabs(fabs(x)),
sumabs(V * fabs(x)), sum2abs(V * pow21(fabs(x))), minloc(p), maxloc(p) {}
: min(x), max(x), sum(V * x), sum2(V * pow2(x)), vol(V), maxabs(fabs(x)),
sumabs(V * fabs(x)), sum2abs(V * pow2(fabs(x))), minloc(p), maxloc(p),
sumloc(x * p) {}

template <typename T, int D>
constexpr reduction<T, D>::reduction(const reduction &x, const reduction &y)
: min(fmin1(x.min, y.min)), max(fmax1(x.max, y.max)), sum(x.sum + y.sum),
: min(min1(x.min, y.min)), max(max1(x.max, y.max)), sum(x.sum + y.sum),
sum2(x.sum2 + y.sum2), vol(x.vol + y.vol),
maxabs(fmax1(x.maxabs, y.maxabs)), sumabs(x.sumabs + y.sumabs),
maxabs(max1(x.maxabs, y.maxabs)), sumabs(x.sumabs + y.sumabs),
sum2abs(x.sum2abs + y.sum2abs),
minloc(x.min <= y.min ? x.minloc : y.minloc),
maxloc(x.max >= y.max ? x.maxloc : y.maxloc) {}
maxloc(x.max >= y.max ? x.maxloc : y.maxloc),
sumloc(x.sumloc + y.sumloc) {}

template <typename T, int D>
ostream &operator<<(ostream &os, const reduction<T, D> &red) {
Expand All @@ -97,6 +101,7 @@ ostream &operator<<(ostream &os, const reduction<T, D> &red) {
<< " sum2abs: " << red.sum2abs << "\n"
<< " minloc: " << red.minloc << "\n"
<< " maxloc: " << red.maxloc << "\n"
<< " sumloc: " << red.sumloc << "\n"
<< " avg: " << red.avg() << "\n"
<< " sdv: " << red.sdv() << "\n"
<< " norm1: " << red.norm1() << "\n"
Expand All @@ -105,6 +110,17 @@ ostream &operator<<(ostream &os, const reduction<T, D> &red) {
<< "}\n";
}

template <typename T, int D> struct combine {
using value_type = typename reduction<T, D>::tuple_type;
constexpr AMREX_GPU_DEVICE value_type init() const {
return (value_type)reduction<T, D>();
}
constexpr AMREX_GPU_DEVICE value_type operator()(const value_type &x,
const value_type &y) const {
return (value_type)reduction<T, D>(x), reduction<T, D>(y);
}
};

typedef reduction<CCTK_REAL, dim> reduction_CCTK_REAL;
#pragma omp declare reduction(reduction:reduction_CCTK_REAL : omp_out += omp_in)

Expand Down
1 change: 1 addition & 0 deletions FluxWaveToyX/test/radiative.par
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
FluxWaveToyX::state
Expand Down
1 change: 1 addition & 0 deletions FluxWaveToyX/test/reflecting.par
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
FluxWaveToyX::state
Expand Down
1 change: 1 addition & 0 deletions FluxWaveToyX/test/standing.par
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
FluxWaveToyX::state
Expand Down
1 change: 1 addition & 0 deletions MovingBoxToy/test/circle.par
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions SIMDWaveToyX/test/standing.par
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
SIMDWaveToyX::state
Expand Down
1 change: 1 addition & 0 deletions SpacetimeWaveToyX/test/standing.par
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ IO::out_every = $out_every
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
SpacetimeWaveToyX::ustate
Expand Down
1 change: 1 addition & 0 deletions StaggeredWaveToyX/test/standing.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ IO::out_every = $out_every
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
StaggeredWaveToyX::ustate
Expand Down
1 change: 1 addition & 0 deletions TestNorms/test/norms.par
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-constant.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-dp87.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-euler.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-rk2.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-rk3.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-rk4.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-rkf78.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
2 changes: 2 additions & 0 deletions TestODESolvers/test/test-ssprk3.par
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ IO::parfile_write = "no"
IO::out_every = 1
CarpetX::out_metadata = no
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

# TestODESolvers::corder requires larger tolerances (~1e-5)
CarpetX::out_norm_vars = "
CarpetX::regrid_error
Expand Down
1 change: 1 addition & 0 deletions WaveToyX/test/radiative.par
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ IO::parfile_write = no
CarpetX::out_metadata = no
CarpetX::out_norm_vars = "all"
CarpetX::out_norm_omit_unstable = yes
CarpetX::out_norm_omit_sumloc_for_backward_compatibility = yes

CarpetX::out_tsv_vars = "
WaveToyX::state
Expand Down
Loading
Loading