DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
io.hpp
1 #ifndef DIY_MPI_IO_HPP
2 #define DIY_MPI_IO_HPP
3 
4 #include <vector>
5 #include <string>
6 
7 namespace diy
8 {
9 namespace mpi
10 {
11 namespace io
12 {
13  typedef MPI_Offset offset;
14 
16  class file
17  {
18  public:
19  enum
20  {
21  rdonly = MPI_MODE_RDONLY,
22  rdwr = MPI_MODE_RDWR,
23  wronly = MPI_MODE_WRONLY,
24  create = MPI_MODE_CREATE,
25  exclusive = MPI_MODE_EXCL,
26  delete_on_close = MPI_MODE_DELETE_ON_CLOSE,
27  unique_open = MPI_MODE_UNIQUE_OPEN,
28  sequential = MPI_MODE_SEQUENTIAL,
29  append = MPI_MODE_APPEND
30  };
31 
32  public:
33  file(const communicator& comm,
34  const std::string& filename,
35  int mode):
36  comm_(comm) { MPI_File_open(comm, const_cast<char*>(filename.c_str()), mode, MPI_INFO_NULL, &fh); }
37  ~file() { close(); }
38  void close() { if (fh != MPI_FILE_NULL) MPI_File_close(&fh); }
39 
40  offset size() const { offset sz; MPI_File_get_size(fh, &sz); return sz; }
41  void resize(offset size) { MPI_File_set_size(fh, size); }
42 
43  inline void read_at(offset o, char* buffer, size_t size);
44  inline void read_at_all(offset o, char* buffer, size_t size);
45  inline void write_at(offset o, const char* buffer, size_t size);
46  inline void write_at_all(offset o, const char* buffer, size_t size);
47 
48  template<class T>
49  inline void read_at(offset o, std::vector<T>& data);
50 
51  template<class T>
52  inline void read_at_all(offset o, std::vector<T>& data);
53 
54  template<class T>
55  inline void write_at(offset o, const std::vector<T>& data);
56 
57  template<class T>
58  inline void write_at_all(offset o, const std::vector<T>& data);
59 
60  const communicator&
61  comm() const { return comm_; }
62 
63  MPI_File& handle() { return fh; }
64 
65  private:
66  const communicator& comm_;
67  MPI_File fh;
68  };
69 }
70 }
71 }
72 
73 void
74 diy::mpi::io::file::
75 read_at(offset o, char* buffer, size_t size)
76 {
77  status s;
78  MPI_File_read_at(fh, o, buffer, size, detail::get_mpi_datatype<char>(), &s.s);
79 }
80 
81 template<class T>
82 void
83 diy::mpi::io::file::
84 read_at(offset o, std::vector<T>& data)
85 {
86  read_at(o, &data[0], data.size()*sizeof(T));
87 }
88 
89 void
90 diy::mpi::io::file::
91 read_at_all(offset o, char* buffer, size_t size)
92 {
93  status s;
94  MPI_File_read_at_all(fh, o, buffer, size, detail::get_mpi_datatype<char>(), &s.s);
95 }
96 
97 template<class T>
98 void
99 diy::mpi::io::file::
100 read_at_all(offset o, std::vector<T>& data)
101 {
102  read_at_all(o, (char*) &data[0], data.size()*sizeof(T));
103 }
104 
105 void
106 diy::mpi::io::file::
107 write_at(offset o, const char* buffer, size_t size)
108 {
109  status s;
110  MPI_File_write_at(fh, o, (void *)buffer, size, detail::get_mpi_datatype<char>(), &s.s);
111 }
112 
113 template<class T>
114 void
115 diy::mpi::io::file::
116 write_at(offset o, const std::vector<T>& data)
117 {
118  write_at(o, (const char*) &data[0], data.size()*sizeof(T));
119 }
120 
121 void
122 diy::mpi::io::file::
123 write_at_all(offset o, const char* buffer, size_t size)
124 {
125  status s;
126  MPI_File_write_at_all(fh, o, (void *)buffer, size, detail::get_mpi_datatype<char>(), &s.s);
127 }
128 
129 template<class T>
130 void
131 diy::mpi::io::file::
132 write_at_all(offset o, const std::vector<T>& data)
133 {
134  write_at_all(o, &data[0], data.size()*sizeof(T));
135 }
136 
137 #endif
Wraps MPI file IO.
Definition: io.hpp:16
Simple wrapper around MPI_Comm.
Definition: communicator.hpp:8