diff --git a/examples/main.c b/examples/main.c index e8b4e08a068f7444f0ff1259b6458705ca5d94ed..8dafe31e9b80411bb562c7408c04b14f4fbeaabc 100644 --- a/examples/main.c +++ b/examples/main.c @@ -422,30 +422,10 @@ int main(int argc, char *argv[]) { fflush(stdout); } - int with_outputs = 1; + /* Write the state of the system before starting time integration. */ char baseName[200]; parser_get_param_string(params, "Snapshots:basename", baseName); - if (with_outputs && !dry_run) { - /* Write the state of the system before starting time integration. */ - if (myrank == 0) clocks_gettime(&tic); -#if defined(WITH_MPI) -#if defined(HAVE_PARALLEL_HDF5) - write_output_parallel(&e, baseName, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#else - write_output_serial(&e, baseName, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#endif -#else - write_output_single(&e, baseName, &us); -#endif - if (myrank == 0 && verbose) { - clocks_gettime(&toc); - message("writing particle properties took %.3f %s.", - clocks_diff(&tic, &toc), clocks_getunit()); - fflush(stdout); - } - } + if (!dry_run) engine_dump_snapshot(&e, &us, baseName); /* Now that everything is ready, no need for the parameters any more */ free(params); @@ -507,27 +487,8 @@ int main(int argc, char *argv[]) { /* Take a step. */ engine_step(&e); - if (with_outputs && j % 10 == 0) { - - if (myrank == 0) clocks_gettime(&tic); -#if defined(WITH_MPI) -#if defined(HAVE_PARALLEL_HDF5) - write_output_parallel(&e, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#else - write_output_serial(&e, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#endif -#else - write_output_single(&e, baseName, &us); -#endif - if (myrank == 0 && verbose) { - clocks_gettime(&toc); - message("writing particle properties took %.3f %s.", - clocks_diff(&tic, &toc), clocks_getunit()); - fflush(stdout); - } - } + /* Snapshot if need be */ + if (j % 10 == 0) engine_dump_snapshot(&e, &us, baseName); /* Dump the task data using the given frequency. */ if (dump_tasks && (dump_tasks == 1 || j % dump_tasks == 1)) { @@ -610,28 +571,8 @@ int main(int argc, char *argv[]) { (double)runner_hist_bins[k]); #endif - if (with_outputs) { - - if (myrank == 0) clocks_gettime(&tic); -/* Write final output. */ -#if defined(WITH_MPI) -#if defined(HAVE_PARALLEL_HDF5) - write_output_parallel(&e, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#else - write_output_serial(&e, &us, myrank, nr_nodes, MPI_COMM_WORLD, - MPI_INFO_NULL); -#endif -#else - write_output_single(&e, baseName, &us); -#endif - if (myrank == 0 && verbose) { - clocks_gettime(&toc); - message("writing particle properties took %.3f %s.", - clocks_diff(&tic, &toc), clocks_getunit()); - fflush(stdout); - } - } + /* Write final output. */ + engine_dump_snapshot(&e, &us, baseName); #ifdef WITH_MPI if ((res = MPI_Finalize()) != MPI_SUCCESS) diff --git a/src/engine.c b/src/engine.c index 572a1b6be0381db4e260f478d4293943f674e197..44d04364da4df826745ff3f879a41871eac489af 100644 --- a/src/engine.c +++ b/src/engine.c @@ -58,6 +58,9 @@ #include "minmax.h" #include "part.h" #include "partition.h" +#include "parallel_io.h" +#include "serial_io.h" +#include "single_io.h" #include "timers.h" const char *engine_policy_names[13] = { @@ -2324,6 +2327,37 @@ void engine_split(struct engine *e, struct partition *initial_partition) { #endif } +/** + * @brief Writes a snapshot with the current state of the engine + * + * @param e The #engine. + * @param us The unit system to use for the snapshots. + * @param baseName The common part of the snapshot file names. + */ +void engine_dump_snapshot(struct engine *e, struct UnitSystem *us, + const char *baseName) { + + struct clocks_time time1, time2; + clocks_gettime(&time1); + +#if defined(WITH_MPI) +#if defined(HAVE_PARALLEL_HDF5) + write_output_parallel(e, baseName, us, myrank, nr_nodes, MPI_COMM_WORLD, + MPI_INFO_NULL); +#else + write_output_serial(e, baseName, us, myrank, nr_nodes, MPI_COMM_WORLD, + MPI_INFO_NULL); +#endif +#else + write_output_single(e, baseName, us); +#endif + + clocks_gettime(&time2); + if (e->verbose) + message("writing particle properties took %.3f %s.", + (float)clocks_diff(&time1, &time2), clocks_getunit()); +} + #if defined(HAVE_LIBNUMA) && defined(_GNU_SOURCE) static bool hyperthreads_present(void) { #ifdef __linux__ diff --git a/src/engine.h b/src/engine.h index 3acc53e012ebeab323fc35f152b84de77c8206aa..8a6f3e04474d322f26457350170ad85166613d3b 100644 --- a/src/engine.h +++ b/src/engine.h @@ -185,6 +185,8 @@ struct engine { /* Function prototypes. */ void engine_barrier(struct engine *e, int tid); +void engine_dump_snapshot(struct engine *e, struct UnitSystem *us, + const char *baseName); void engine_init(struct engine *e, struct space *s, const struct swift_params *params, int nr_nodes, int nodeID, int nr_threads, int policy, int verbose,