58#include <boost/archive/binary_iarchive.hpp>
59#include <boost/archive/binary_oarchive.hpp>
60#include <boost/iostreams/device/array.hpp>
61#include <boost/iostreams/device/back_inserter.hpp>
62#include <boost/iostreams/stream.hpp>
83#pragma clang diagnostic push
84#pragma clang diagnostic ignored "-Wunreachable-code-return"
94static bool fatal_error(
char const *msg, std::string
const &fn =
"",
95 std::string
const &extra =
"") {
96 std::stringstream what;
97 what <<
"MPI-IO Error: " << msg;
99 what <<
" \"" << fn <<
"\"";
101 if (not extra.empty()) {
102 what <<
" :" << extra;
105 MPI_Comm_size(MPI_COMM_WORLD, &size);
107 throw std::runtime_error(what.str());
109 fprintf(stderr,
"%s\n", what.str().c_str());
113#if defined(__clang__)
114#pragma clang diagnostic pop
127static bool fatal_error(
char const *msg, std::string
const &fn, MPI_File *fp,
130 char buf[MPI_MAX_ERROR_STRING];
132 MPI_Error_string(errnum, buf, &buf_len);
154 std::size_t len, std::size_t pref,
155 MPI_Datatype MPI_T) {
158 ret = MPI_File_open(MPI_COMM_WORLD,
const_cast<char *
>(fn.c_str()),
160 MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_EXCL,
166 static_cast<MPI_Offset
>(pref) *
static_cast<MPI_Offset
>(
sizeof(T));
167 ret = MPI_File_set_view(f, offset, MPI_T, MPI_T,
const_cast<char *
>(
"native"),
169 ret |= MPI_File_write_all(f, arr,
static_cast<int>(len), MPI_T,
171 static_cast<void>(ret and
fatal_error(
"Could not write file", fn, &f, ret));
181 unsigned long offset = 0ul;
182 MPI_Exscan(&n_items, &offset, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
194static void dump_info(std::string
const &fn,
unsigned fields,
197 auto const nbonds = bonded_ias.
size();
198 assert(
static_cast<std::size_t
>(bonded_ias.
get_next_key()) == nbonds);
200 FILE *f = fopen(fn.c_str(),
"wb");
204 std::vector<int> npartners;
205 bool success = (fwrite(&fields,
sizeof(fields), 1u, f) == 1);
207 npartners.reserve(nbonds);
208 for (
int bond_id = 0; bond_id < bonded_ias.
get_next_key(); ++bond_id) {
213 success &= fwrite(&nbonds,
sizeof(std::size_t), 1u, f) == 1;
214 success &= fwrite(npartners.data(),
sizeof(
int), nbonds, f) == nbonds;
216 static_cast<void>(success or
fatal_error(
"Could not write file", fn));
223 auto const nlocalpart =
static_cast<unsigned long>(particles.
size());
226 auto &pos = buffers.
pos;
227 auto &vel = buffers.
vel;
228 auto &
id = buffers.
id;
229 auto &type = buffers.
type;
232 if (nlocalpart >
id.size())
233 id.resize(nlocalpart);
235 pos.resize(3ul * nlocalpart);
237 vel.resize(3ul * nlocalpart);
239 type.resize(nlocalpart);
242 auto id_it =
id.begin();
243 auto type_it = type.begin();
244 auto pos_it = pos.begin();
245 auto vel_it = vel.begin();
246 for (
auto const &p : particles) {
250 std::copy_n(std::begin(p.pos()), 3u, pos_it);
254 std::copy_n(std::begin(p.v()), 3u, vel_it);
264 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
266 dump_info(prefix +
".head", fields, bonded_ias);
267 auto const pref_offset =
static_cast<unsigned long>(rank);
268 mpiio_dump_array<unsigned long>(prefix +
".pref", &offset, 1ul, pref_offset,
270 mpiio_dump_array<int>(prefix +
".id",
id.data(), nlocalpart, offset, MPI_INT);
272 mpiio_dump_array<double>(prefix +
".pos", pos.data(), 3ul * nlocalpart,
273 3ul * offset, MPI_DOUBLE);
275 mpiio_dump_array<double>(prefix +
".vel", vel.data(), 3ul * nlocalpart,
276 3ul * offset, MPI_DOUBLE);
278 mpiio_dump_array<int>(prefix +
".type", type.data(), nlocalpart, offset,
282 std::vector<char> bonds;
286 namespace io = boost::iostreams;
287 io::stream_buffer<io::back_insert_device<std::vector<char>>> os{
288 io::back_inserter(bonds)};
289 boost::archive::binary_oarchive bond_archiver{os};
291 for (
auto const &p : particles) {
292 bond_archiver << p.bonds();
297 auto const bonds_size =
static_cast<unsigned long>(bonds.size());
300 mpiio_dump_array<unsigned long>(prefix +
".boff", &bonds_size, 1ul,
301 pref_offset, MPI_UNSIGNED_LONG);
302 mpiio_dump_array<char>(prefix +
".bond", bonds.data(), bonds.size(),
303 bonds_offset, MPI_CHAR);
315static unsigned long get_num_elem(
const std::string &fn, std::size_t elem_sz) {
320 if (stat(fn.c_str(), &st) != 0) {
321 auto const reason = strerror(errno);
322 fatal_error(
"Could not get file size of", fn, reason);
324 return static_cast<unsigned long>(st.st_size) / elem_sz;
340 std::size_t pref, MPI_Datatype MPI_T) {
343 ret = MPI_File_open(MPI_COMM_WORLD,
const_cast<char *
>(fn.c_str()),
344 MPI_MODE_RDONLY, MPI_INFO_NULL, &f);
349 static_cast<MPI_Offset
>(pref) *
static_cast<MPI_Offset
>(
sizeof(T));
350 ret = MPI_File_set_view(f, offset, MPI_T, MPI_T,
const_cast<char *
>(
"native"),
353 ret |= MPI_File_read_all(f, arr,
static_cast<int>(len), MPI_T,
355 static_cast<void>(ret and
fatal_error(
"Could not read file", fn, &f, ret));
366static unsigned read_head(
const std::string &fn,
int rank) {
367 unsigned n_fields = 0u;
370 f = fopen(fn.c_str(),
"rb");
371 static_cast<void>(not f and
fatal_error(
"Could not open file", fn));
372 auto const n = fread(
static_cast<void *
>(&n_fields),
sizeof n_fields, 1, f);
373 static_cast<void>((n == 1) or
fatal_error(
"Could not read file", fn));
375 MPI_Bcast(&n_fields, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
392static std::tuple<unsigned long, unsigned long>
394 unsigned long nglobalpart) {
395 auto const pref_offset =
static_cast<unsigned long>(rank);
396 unsigned long pref = 0ul;
397 unsigned long nlocalpart = 0ul;
398 mpiio_read_array<unsigned long>(fn, &pref, 1ul, pref_offset,
401 MPI_Send(&pref, 1, MPI_UNSIGNED_LONG, rank - 1, 0, MPI_COMM_WORLD);
403 MPI_Recv(&nlocalpart, 1, MPI_UNSIGNED_LONG, rank + 1, MPI_ANY_TAG,
404 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
406 nlocalpart = nglobalpart;
408 return {pref, nlocalpart};
416 MPI_Comm_size(MPI_COMM_WORLD, &size);
417 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
418 auto const nproc =
get_num_elem(prefix +
".pref",
sizeof(
unsigned long));
419 auto const nglobalpart =
get_num_elem(prefix +
".id",
sizeof(
int));
421 if (rank == 0 && nproc !=
static_cast<unsigned long>(size)) {
422 fatal_error(
"Trying to read a file with a different COMM "
423 "size than at point of writing.");
429 auto const avail_fields =
read_head(prefix +
".head", rank);
430 if (rank == 0 && (fields & avail_fields) != fields) {
431 fatal_error(
"Requesting to read fields which were not dumped.");
438 auto const [pref, nlocalpart] =
439 read_prefs(prefix +
".pref", rank, size, nglobalpart);
441 std::vector<Particle> particles(nlocalpart);
446 std::vector<int> id(nlocalpart);
447 auto id_it =
id.begin();
448 mpiio_read_array<int>(prefix +
".id",
id.data(), nlocalpart, pref, MPI_INT);
450 for (
auto &p : particles) {
459 std::vector<double> pos(3ul * nlocalpart);
460 auto pos_it = pos.begin();
461 mpiio_read_array<double>(prefix +
".pos", pos.data(), 3ul * nlocalpart,
462 3ul * pref, MPI_DOUBLE);
464 for (
auto &p : particles) {
465 std::copy_n(pos_it, 3u, std::begin(p.pos()));
473 std::vector<int> type(nlocalpart);
474 auto type_it = type.begin();
475 mpiio_read_array<int>(prefix +
".type", type.data(), nlocalpart, pref,
478 for (
auto &p : particles) {
487 std::vector<double> vel(3ul * nlocalpart);
488 auto vel_it = vel.begin();
489 mpiio_read_array<double>(prefix +
".vel", vel.data(), 3ul * nlocalpart,
490 3ul * pref, MPI_DOUBLE);
492 for (
auto &p : particles) {
493 std::copy_n(vel_it, 3u, std::begin(p.v()));
501 auto const pref_offset =
static_cast<unsigned long>(rank);
502 unsigned long bonds_size = 0u;
503 mpiio_read_array<unsigned long>(prefix +
".boff", &bonds_size, 1ul,
504 pref_offset, MPI_UNSIGNED_LONG);
509 std::vector<char> bond(bonds_size);
510 mpiio_read_array<char>(prefix +
".bond", bond.data(), bonds_size,
511 bonds_offset, MPI_CHAR);
513 boost::iostreams::array_source src(bond.data(), bond.size());
514 boost::iostreams::stream<boost::iostreams::array_source> ss(src);
515 boost::archive::binary_iarchive ia(ss);
517 for (
auto &p : particles) {
522 for (
auto &p : particles) {
Data structures for bonded interactions.
int number_of_partners(Bonded_IA_Parameters const &iaparams)
Get the number of bonded partners for the specified bond.
container for bonded interactions.
bool contains(key_type const &key) const
mapped_type const & at(key_type const &key) const
auto get_next_key() const
Describes a cell structure / cell system.
Particle * add_particle(Particle &&p)
Add a particle.
void remove_all_particles()
Remove all particles from the cell system.
base_type::size_type size() const
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.
void mpi_mpiio_common_write(std::string const &prefix, unsigned fields, BondedInteractionsMap const &bonded_ias, ParticleRange const &particles, write_buffers &buffers)
Parallel binary output using MPI-IO.
static std::tuple< unsigned long, unsigned long > read_prefs(const std::string &fn, int rank, int size, unsigned long nglobalpart)
Read the pref file.
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_read(const std::string &prefix, unsigned fields, CellStructure &cell_structure)
Parallel binary input using MPI-IO.
static void dump_info(std::string const &fn, unsigned fields, BondedInteractionsMap const &bonded_ias)
Dump the fields and bond information.
static bool fatal_error(char const *msg, std::string const &fn="", std::string const &extra="")
Fatal error handler.
std::vector< double > pos
std::vector< double > vel