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

IO fixes (tfield; checkpoint) #319

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
561b916
tfield: fix obsolete tfield_average_length
liangwang-phys Jun 26, 2023
747f028
fix tfield flag logic
liangwang-phys Jun 26, 2023
3a06670
Move writer_checkpoint function to the Checkpointing class
liangwang-phys Jun 27, 2023
343097f
checkpointing: add beginStep and endStep to support bp5
liangwang-phys Jun 28, 2023
af53ca2
TestIOAdios2: add .bp suffix; seems needed to pass bp4 tests
liangwang-phys Jun 30, 2023
617741a
setup_particles: consolidate init_npt funcs
Nov 10, 2022
2cdf867
setup_particles/distribution: move rng to latter
Nov 13, 2022
8335e87
setup_particles/distribution: impl normal dist
Nov 13, 2022
3d3c9e3
setup_particles: cleanup get_n_in_cell
Nov 13, 2022
d265c46
setup_particles: cleanup setupParticle
Nov 13, 2022
b6670ed
setup_particles: mv p gen to psc_particle_np
Nov 14, 2022
36f4a14
setup_particles: add helper npt -> np conversion
Nov 14, 2022
44cfc84
setup_particles: rm redundant setupParticle
Nov 14, 2022
4cd0ee8
setup_particles: make & use np in op_cellwise
Nov 14, 2022
36417b4
setup_particles: mv maxwellian creation new func
Nov 14, 2022
56bb0ae
setup_particles: rm npt_to_np
Nov 14, 2022
52fb454
setup_particles: introduce InitNpFunc
Nov 14, 2022
971095f
setup_particles: add methods accepting InitNpFuncs
Nov 14, 2022
3b1c990
distribution: impl cdf inverter
Nov 16, 2022
651885f
distribution: interpolate in get()
Nov 16, 2022
67a7a12
distribution: limit n_iter
Nov 16, 2022
5773df3
distribution: fix seeds
Nov 16, 2022
c629930
distribution: fix finding pdf support
Dec 19, 2022
88445e8
distribution: add default Uniform
May 24, 2023
fe58c77
distribution: fix includes
May 24, 2023
0545dea
distribution: rm base class
May 24, 2023
fcffa24
distribution, *: rename to rng
May 24, 2023
01804a4
fix formatting
liangwang-phys Jul 1, 2023
97e84ad
TestIOAdios2: add beginStep/endStep pairs to support bp5
liangwang-phys Jul 1, 2023
eaa1999
checkpoint: check if adios2 is available
liangwang-phys Jul 2, 2023
0d4f783
Merge branch 'main' into liang20230630
liangwang0734 Jul 2, 2023
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
13 changes: 7 additions & 6 deletions src/include/OutputFieldsDefault.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ struct OutputFieldsItemParams
int pfield_first = 0;
int tfield_interval = 0;
int tfield_first = 0;
int tfield_average_length = 1000000;
int tfield_average_every = 1;
Int3 rn = {};
Int3 rx = {10000000, 10000000, 10000000};
Expand Down Expand Up @@ -114,16 +113,18 @@ class OutputFieldsItem : public OutputFieldsItemParams
if (timestep != 0) {
pfield_next_ = timestep + pfield_interval;
tfield_next_ = timestep + tfield_interval;

tfield_next_ = timestep / tfield_interval * tfield_interval;
if (timestep % tfield_interval > 0)
tfield_next_ += tfield_interval;
}
}

bool do_pfield = pfield_interval > 0 && timestep >= pfield_next_;
bool do_tfield = tfield_interval > 0 && timestep >= tfield_next_;
bool doaccum_tfield =
tfield_interval > 0 &&
(((timestep >= (tfield_next_ - tfield_average_length + 1)) &&
timestep % tfield_average_every == 0) ||
timestep == 0);
bool doaccum_tfield = timestep >= (tfield_next_ - tfield_average_every + 1);
doaccum_tfield = doaccum_tfield && (tfield_interval > 0);
doaccum_tfield = doaccum_tfield || (timestep == 0);

if (do_pfield || doaccum_tfield) {
prof_start(pr_eval);
Expand Down
79 changes: 41 additions & 38 deletions src/include/checkpoint.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,6 @@
#include "particles_simple.inl"
#include <kg/io.h>

// ----------------------------------------------------------------------
// write_checkpoint
//

template <typename Mparticles, typename MfieldsState>
void write_checkpoint(const Grid_t& grid, Mparticles& mprts,
MfieldsState& mflds)
{
static int pr, pr_A;
if (!pr) {
pr = prof_register("cp_write", 1., 0, 0);
pr_A = prof_register("cp_write_barier", 1., 0, 0);
}

prof_start(pr);
mpi_printf(grid.comm(), "**** Writing checkpoint...\n");
#if defined(PSC_HAVE_ADIOS2) && !defined(VPIC)
prof_start(pr_A);
MPI_Barrier(grid.comm()); // not really necessary
prof_stop(pr_A);

std::string filename =
"checkpoint_" + std::to_string(grid.timestep()) + ".bp";

auto io = kg::io::IOAdios2{};
auto writer =
io.open(filename, kg::io::Mode::Write, grid.comm(), "checkpoint");
writer.put("grid", grid);
writer.put("mprts", mprts);
writer.put("mflds", mflds);
writer.close();
#else
std::cerr << "write_checkpoint not available without adios2" << std::endl;
std::abort();
#endif
prof_stop(pr);
}

// ----------------------------------------------------------------------
// read_checkpoint
//
Expand All @@ -59,13 +21,15 @@ inline void read_checkpoint(const std::string& filename, Grid_t& grid,
#ifdef PSC_HAVE_ADIOS2
auto io = kg::io::IOAdios2{};
auto reader = io.open(filename, kg::io::Mode::Read);
reader.beginStep(kg::io::StepMode::Read);
reader.get("grid", grid);
mprts.~Mparticles();
mflds.~MfieldsState();
new (&mprts) Mparticles(grid);
new (&mflds) MfieldsState(grid);
reader.get("mprts", mprts);
reader.get("mflds", mflds);
reader.endStep();
reader.close();

// FIXME, when we read back a rebalanced grid, other existing fields will
Expand Down Expand Up @@ -120,7 +84,46 @@ public:
write_checkpoint(grid, mprts, mflds);
}

template <typename Mparticles, typename MfieldsState>
void write_checkpoint(const Grid_t& grid, Mparticles& mprts,
MfieldsState& mflds)
{
static int pr, pr_A;
if (!pr) {
pr = prof_register("cp_write", 1., 0, 0);
pr_A = prof_register("cp_write_barier", 1., 0, 0);
}

prof_start(pr);
mpi_printf(grid.comm(), "**** Writing checkpoint...\n");
#if defined(PSC_HAVE_ADIOS2) && !defined(VPIC)
prof_start(pr_A);
MPI_Barrier(grid.comm()); // not really necessary
prof_stop(pr_A);

std::string filename =
"checkpoint_" + std::to_string(grid.timestep()) + ".bp";

mpi_printf(MPI_COMM_WORLD, "??? checkpoint IOAdios2 %p\n", &io);
writer = io.open(filename, kg::io::Mode::Write, grid.comm(), "checkpoint");
writer.beginStep(kg::io::StepMode::Append);
writer.put("grid", grid);
writer.put("mprts", mprts);
writer.put("mflds", mflds);
writer.endStep();
writer.close();
#else
std::cerr << "write_checkpoint not available without adios2" << std::endl;
std::abort();
#endif
prof_stop(pr);
}

private:
int interval_; // write checkpoint every so many steps
bool first_time_ = true;
#ifdef PSC_HAVE_ADIOS2
kg::io::IOAdios2 io;
kg::io::Engine writer;
#endif
};
12 changes: 12 additions & 0 deletions src/kg/testing/io/TestIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ TEST(IO, WriteRead)

{
auto writer = io.open("test.bp", kg::io::Mode::Write);
writer.beginStep(kg::io::StepMode::Append);
writer.putLocal("var_double", 99.);
writer.endStep();
writer.close();
}

{
auto reader = io.open("test.bp", kg::io::Mode::Read);
reader.beginStep(kg::io::StepMode::Read);
double dbl;
reader.getLocal("var_double", dbl);
reader.endStep();
reader.close();
EXPECT_EQ(dbl, 99.);
}
Expand All @@ -28,14 +32,18 @@ TEST(IO, WriteReadAttr)

{
auto writer = io.open("test.bp", kg::io::Mode::Write);
writer.beginStep(kg::io::StepMode::Append);
writer.put("attr_double", 99.);
writer.endStep();
writer.close();
}

{
auto reader = io.open("test.bp", kg::io::Mode::Read);
reader.beginStep(kg::io::StepMode::Read);
double dbl;
reader.get("attr_double", dbl);
reader.endStep();
reader.close();
EXPECT_EQ(dbl, 99.);
}
Expand Down Expand Up @@ -72,15 +80,19 @@ TEST(IO, WriteReadCustom)

{
auto writer = io.open("test.bp", kg::io::Mode::Write);
writer.beginStep(kg::io::StepMode::Append);
auto c = Custom{3, 99.};
writer.put("var_custom", c);
writer.endStep();
writer.close();
}

{
auto reader = io.open("test.bp", kg::io::Mode::Read);
reader.beginStep(kg::io::StepMode::Read);
auto c = Custom{};
reader.get("var_custom", c);
reader.endStep();
reader.close();
EXPECT_EQ(c.i, 3);
EXPECT_EQ(c.d, 99.);
Expand Down
14 changes: 11 additions & 3 deletions src/kg/testing/io/TestIOAdios2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST(IOAdios2, OpenWriteThenRead)
{
auto io = kg::io::IOAdios2{};
{
auto file = io.openFile("test2", kg::io::Mode::Write);
auto file = io.openFile("test2.bp", kg::io::Mode::Write);
}
{
auto file = io.openFile("test2.bp", kg::io::Mode::Read);
Expand All @@ -33,7 +33,8 @@ TEST(IOAdios2, FilePutGetVariable)
{
auto io = kg::io::IOAdios2{};
{
auto file = io.openFile("test3", kg::io::Mode::Write);
auto file = io.openFile("test3.bp", kg::io::Mode::Write);
file.beginStep(kg::io::StepMode::Append);
auto dbl = std::vector<double>{1., 2., 3., 4., 5.};
file.putVariable("dbl", dbl.data(), kg::io::Mode::NonBlocking, {5},
{{0}, {5}}, {});
Expand All @@ -42,9 +43,11 @@ TEST(IOAdios2, FilePutGetVariable)
file.putVariable("dbl2", dbl.data() + 3, kg::io::Mode::NonBlocking, {5},
{{3}, {2}}, {});
file.performPuts();
file.endStep();
}
{
auto file = io.openFile("test3.bp", kg::io::Mode::Read);
file.beginStep(kg::io::StepMode::Read);

auto shape = file.shapeVariable("dbl");
EXPECT_EQ(shape, kg::io::Dims{5});
Expand All @@ -58,6 +61,7 @@ TEST(IOAdios2, FilePutGetVariable)
file.getVariable("dbl2", dbl2.data(), kg::io::Mode::NonBlocking, {{0}, {5}},
{});
file.performGets();
file.endStep();

EXPECT_EQ(dbl, (std::vector<double>{1., 2., 3., 4., 5.}));
EXPECT_EQ(dbl2, (std::vector<double>{1., 2., 0., 4., 5.}));
Expand All @@ -68,16 +72,20 @@ TEST(IOAdios2, FilePutGetAttribute)
{
auto io = kg::io::IOAdios2{};
{
auto file = io.openFile("test4", kg::io::Mode::Write);
auto file = io.openFile("test4.bp", kg::io::Mode::Write);
file.beginStep(kg::io::StepMode::Append);
auto dbl = std::vector<double>{1., 2., 3., 4., 5.};
file.putAttribute("attr_dbl", dbl.data(), dbl.size());
file.endStep();
}
{
auto file = io.openFile("test4.bp", kg::io::Mode::Read);
file.beginStep(kg::io::StepMode::Read);
auto size = file.sizeAttribute("attr_dbl");
auto dbl = std::vector<double>(size);
file.getAttribute("attr_dbl", dbl.data());
EXPECT_EQ(dbl, (std::vector<double>{1., 2., 3., 4., 5.}));
file.endStep();
}
}

Expand Down
Loading