1 #ifndef LIBGEODECOMP_IO_MPIIOWRITER_H
2 #define LIBGEODECOMP_IO_MPIIOWRITER_H
3 
4 #include <libgeodecomp/config.h>
5 #ifdef LIBGEODECOMP_WITH_MPI
6 
7 #include <libgeodecomp/io/mpiio.h>
8 #include <libgeodecomp/io/writer.h>
9 #include <libgeodecomp/communication/typemaps.h>
10 #include <libgeodecomp/misc/clonable.h>
11 
12 namespace LibGeoDecomp {
13 
14 /**
15  * This writer uses MPI I/O to dump simulation snapshots to disk. Use
16  * MPIIOInitializer for restarting from a checkpoint. Also consider
17  * ParallelMPIIOWriter for large-scale runs.
18  */
19 template<typename CELL_TYPE>
20 class MPIIOWriter : public Clonable<Writer<CELL_TYPE>, MPIIOWriter<CELL_TYPE> >
21 {
22 public:
23     friend class MPIIOWriterTest;
24     friend class MPIIOInitializerTest;
25 
26     typedef typename Writer<CELL_TYPE>::GridType GridType;
27     typedef typename Writer<CELL_TYPE>::Topology Topology;
28 
29     static const int DIM = Topology::DIM;
30 
31     MPIIOWriter(
32         const std::string& prefix,
33         const unsigned period,
34         const unsigned maxSteps,
35         const MPI_Comm& communicator = MPI_COMM_WORLD,
36         MPI_Datatype mpiDatatype = Typemaps::lookup<CELL_TYPE>()) :
37         Clonable<Writer<CELL_TYPE>, MPIIOWriter<CELL_TYPE> >(prefix, period),
38         maxSteps(maxSteps),
39         comm(communicator),
40         datatype(mpiDatatype)
41     {}
42 
stepFinished(const GridType & grid,unsigned step,WriterEvent event)43     virtual void stepFinished(const GridType& grid, unsigned step, WriterEvent event)
44     {
45         if ((event == WRITER_STEP_FINISHED) && (step % period != 0)) {
46             return;
47         }
48 
49         Region<DIM> region;
50         region << grid.boundingBox();
51 
52         MPIIO<CELL_TYPE>::writeRegion(
53             grid,
54             grid.dimensions(),
55             step,
56             maxSteps,
57             filename(step),
58             region,
59             datatype,
60             comm);
61     }
62 
63 private:
64     using Writer<CELL_TYPE>::period;
65     using Writer<CELL_TYPE>::prefix;
66     unsigned maxSteps;
67     MPI_Comm comm;
68     MPI_Datatype datatype;
69 
filename(const unsigned & step)70     std::string filename(const unsigned& step) const
71     {
72         std::ostringstream buf;
73         buf << prefix << std::setfill('0') << std::setw(5) << step << ".mpiio";
74         return buf.str();
75     }
76 };
77 
78 }
79 
80 #endif
81 #endif
82