57#include "system/System.hpp"
61#include <boost/archive/binary_iarchive.hpp>
62#include <boost/archive/binary_oarchive.hpp>
63#include <boost/iostreams/device/array.hpp>
64#include <boost/iostreams/device/back_inserter.hpp>
65#include <boost/iostreams/stream.hpp>
92static bool fatal_error(
char const *msg, std::string
const &fn =
"",
93 std::string
const &extra =
"") {
94 std::stringstream what;
95 what <<
"MPI-IO Error: " << msg;
97 what <<
" \"" << fn <<
"\"";
99 if (not extra.empty()) {
100 what <<
" :" << extra;
103 MPI_Comm_size(MPI_COMM_WORLD, &size);
105 throw std::runtime_error(what.str());
107 fprintf(stderr,
"%s\n", what.str().c_str());
122static bool fatal_error(
char const *msg, std::string
const &fn, MPI_File *fp,
125 char buf[MPI_MAX_ERROR_STRING];
127 MPI_Error_string(errnum, buf, &buf_len);
149 std::size_t len, std::size_t pref,
150 MPI_Datatype MPI_T) {
153 ret = MPI_File_open(MPI_COMM_WORLD,
const_cast<char *
>(fn.c_str()),
155 MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_EXCL,
161 static_cast<MPI_Offset
>(pref) *
static_cast<MPI_Offset
>(
sizeof(T));
162 ret = MPI_File_set_view(
f, offset, MPI_T, MPI_T,
const_cast<char *
>(
"native"),
164 ret |= MPI_File_write_all(
f, arr,
static_cast<int>(len), MPI_T,
166 static_cast<void>(ret and
fatal_error(
"Could not write file", fn, &
f, ret));
176 unsigned long offset = 0ul;
177 MPI_Exscan(&n_items, &offset, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
188static void dump_info(
const std::string &fn,
unsigned fields) {
193 FILE *
f = fopen(fn.c_str(),
"wb");
197 static std::vector<int> npartners;
198 bool success = (fwrite(&fields,
sizeof(fields), 1u,
f) == 1);
201 if (nbonds > npartners.size())
202 npartners.resize(nbonds);
204 auto npartners_it = npartners.begin();
208 success = success && (fwrite(&nbonds,
sizeof(std::size_t), 1u,
f) == 1);
210 success && (fwrite(npartners.data(),
sizeof(
int), nbonds,
f) == nbonds);
212 static_cast<void>(success or
fatal_error(
"Could not write file", fn));
217 auto const nlocalpart =
static_cast<unsigned long>(particles.
size());
221 static std::vector<double>
pos, vel;
222 static std::vector<int> id, type;
225 if (nlocalpart >
id.size())
226 id.resize(nlocalpart);
228 pos.resize(3ul * nlocalpart);
230 vel.resize(3ul * nlocalpart);
232 type.resize(nlocalpart);
235 auto id_it =
id.begin();
236 auto type_it = type.begin();
237 auto pos_it =
pos.begin();
238 auto vel_it = vel.begin();
239 for (
auto const &p : particles) {
243 std::copy_n(std::begin(p.pos()), 3u, pos_it);
247 std::copy_n(std::begin(p.v()), 3u, vel_it);
257 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
260 auto const pref_offset =
static_cast<unsigned long>(rank);
261 mpiio_dump_array<unsigned long>(prefix +
".pref", &offset, 1ul, pref_offset,
263 mpiio_dump_array<int>(prefix +
".id",
id.data(), nlocalpart, offset, MPI_INT);
265 mpiio_dump_array<double>(prefix +
".pos",
pos.data(), 3ul * nlocalpart,
266 3ul * offset, MPI_DOUBLE);
268 mpiio_dump_array<double>(prefix +
".vel", vel.data(), 3ul * nlocalpart,
269 3ul * offset, MPI_DOUBLE);
271 mpiio_dump_array<int>(prefix +
".type", type.data(), nlocalpart, offset,
275 std::vector<char> bonds;
279 namespace io = boost::iostreams;
280 io::stream_buffer<io::back_insert_device<std::vector<char>>> os{
281 io::back_inserter(bonds)};
282 boost::archive::binary_oarchive bond_archiver{os};
284 for (
auto const &p : particles) {
285 bond_archiver << p.bonds();
290 auto const bonds_size =
static_cast<unsigned long>(bonds.size());
293 mpiio_dump_array<unsigned long>(prefix +
".boff", &bonds_size, 1ul,
294 pref_offset, MPI_UNSIGNED_LONG);
295 mpiio_dump_array<char>(prefix +
".bond", bonds.data(), bonds.size(),
296 bonds_offset, MPI_CHAR);
308static unsigned long get_num_elem(
const std::string &fn, std::size_t elem_sz) {
313 if (stat(fn.c_str(), &st) != 0) {
314 auto const reason = strerror(errno);
315 fatal_error(
"Could not get file size of", fn, reason);
317 return static_cast<unsigned long>(st.st_size) / elem_sz;
333 std::size_t pref, MPI_Datatype MPI_T) {
336 ret = MPI_File_open(MPI_COMM_WORLD,
const_cast<char *
>(fn.c_str()),
337 MPI_MODE_RDONLY, MPI_INFO_NULL, &
f);
342 static_cast<MPI_Offset
>(pref) *
static_cast<MPI_Offset
>(
sizeof(T));
343 ret = MPI_File_set_view(
f, offset, MPI_T, MPI_T,
const_cast<char *
>(
"native"),
346 ret |= MPI_File_read_all(
f, arr,
static_cast<int>(len), MPI_T,
348 static_cast<void>(ret and
fatal_error(
"Could not read file", fn, &
f, ret));
359static unsigned read_head(
const std::string &fn,
int rank) {
360 unsigned n_fields = 0
u;
363 f = fopen(fn.c_str(),
"rb");
364 static_cast<void>(not
f and
fatal_error(
"Could not open file", fn));
365 auto const n = fread(
static_cast<void *
>(&n_fields),
sizeof n_fields, 1,
f);
366 static_cast<void>((n == 1) or
fatal_error(
"Could not read file", fn));
368 MPI_Bcast(&n_fields, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
385static std::tuple<unsigned long, unsigned long>
387 unsigned long nglobalpart) {
388 auto const pref_offset =
static_cast<unsigned long>(rank);
389 unsigned long pref = 0ul;
390 unsigned long nlocalpart = 0ul;
391 mpiio_read_array<unsigned long>(fn, &pref, 1ul, pref_offset,
394 MPI_Send(&pref, 1, MPI_UNSIGNED_LONG, rank - 1, 0, MPI_COMM_WORLD);
396 MPI_Recv(&nlocalpart, 1, MPI_UNSIGNED_LONG, rank + 1, MPI_ANY_TAG,
397 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
399 nlocalpart = nglobalpart;
401 return {pref, nlocalpart};
406 cell_structure.remove_all_particles();
409 MPI_Comm_size(MPI_COMM_WORLD, &size);
410 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
411 auto const nproc =
get_num_elem(prefix +
".pref",
sizeof(
unsigned long));
412 auto const nglobalpart =
get_num_elem(prefix +
".id",
sizeof(
int));
414 if (rank == 0 && nproc !=
static_cast<unsigned long>(size)) {
415 fatal_error(
"Trying to read a file with a different COMM "
416 "size than at point of writing.");
422 auto const avail_fields =
read_head(prefix +
".head", rank);
423 if (rank == 0 && (fields & avail_fields) != fields) {
424 fatal_error(
"Requesting to read fields which were not dumped.");
431 auto const [pref, nlocalpart] =
432 read_prefs(prefix +
".pref", rank, size, nglobalpart);
434 std::vector<Particle> particles(nlocalpart);
439 std::vector<int> id(nlocalpart);
440 auto id_it =
id.begin();
441 mpiio_read_array<int>(prefix +
".id",
id.data(), nlocalpart, pref, MPI_INT);
443 for (
auto &p : particles) {
452 std::vector<double>
pos(3ul * nlocalpart);
453 auto pos_it =
pos.begin();
454 mpiio_read_array<double>(prefix +
".pos",
pos.data(), 3ul * nlocalpart,
455 3ul * pref, MPI_DOUBLE);
457 for (
auto &p : particles) {
458 std::copy_n(pos_it, 3u, std::begin(p.pos()));
466 std::vector<int> type(nlocalpart);
467 auto type_it = type.begin();
468 mpiio_read_array<int>(prefix +
".type", type.data(), nlocalpart, pref,
471 for (
auto &p : particles) {
480 std::vector<double> vel(3ul * nlocalpart);
481 auto vel_it = vel.begin();
482 mpiio_read_array<double>(prefix +
".vel", vel.data(), 3ul * nlocalpart,
483 3ul * pref, MPI_DOUBLE);
485 for (
auto &p : particles) {
486 std::copy_n(vel_it, 3u, std::begin(p.v()));
494 auto const pref_offset =
static_cast<unsigned long>(rank);
495 unsigned long bonds_size = 0
u;
496 mpiio_read_array<unsigned long>(prefix +
".boff", &bonds_size, 1ul,
497 pref_offset, MPI_UNSIGNED_LONG);
502 std::vector<char> bond(bonds_size);
503 mpiio_read_array<char>(prefix +
".bond", bond.data(), bonds_size,
504 bonds_offset, MPI_CHAR);
506 boost::iostreams::array_source src(bond.data(), bond.size());
507 boost::iostreams::stream<boost::iostreams::array_source> ss(src);
508 boost::archive::binary_iarchive ia(ss);
510 for (
auto &p : particles) {
515 for (
auto &p : particles) {
516 cell_structure.add_particle(std::move(p));
Vector implementation and trait types for boost qvm interoperability.
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
BondedInteractionsMap bonded_ia_params
Field containing the parameters of the bonded ia types.
Data structures for bonded interactions.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Return the number of bonded partners for the specified bond.
mapped_type at(key_type const &key) const
auto get_next_key() const
base_type::size_type size() const
std::shared_ptr< CellStructure > cell_structure
void errexit()
exit ungracefully, core dump if switched on.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
static unsigned long mpi_calculate_file_offset(unsigned long n_items)
Calculate the file offset on the local node.
static void dump_info(const std::string &fn, unsigned fields)
Dump the fields and bond information.
static std::tuple< unsigned long, unsigned long > read_prefs(const std::string &fn, int rank, int size, unsigned long nglobalpart)
Read the pref file.
void mpi_mpiio_common_read(const std::string &prefix, unsigned fields)
Parallel binary input using MPI-IO.
static unsigned long get_num_elem(const std::string &fn, std::size_t elem_sz)
Get the number of elements in a file by its file size and elem_sz.
static void mpiio_read_array(const std::string &fn, T *arr, std::size_t len, std::size_t pref, MPI_Datatype MPI_T)
Read a previously dumped array of size len starting from prefix pref of type T using MPI_T as MPI dat...
static void mpiio_dump_array(const std::string &fn, T const *arr, std::size_t len, std::size_t pref, MPI_Datatype MPI_T)
Dump data arr of size len starting from prefix pref of type T using MPI_T as MPI datatype.
static unsigned read_head(const std::string &fn, int rank)
Read the header file and return the first value.
void mpi_mpiio_common_write(const std::string &prefix, unsigned fields, const ParticleRange &particles)
Parallel binary output using MPI-IO.
static bool fatal_error(char const *msg, std::string const &fn="", std::string const &extra="")
Fatal error handler.