8 #include "../types.hpp"
19 typedef std::vector<int> Shape;
27 mpi::io::offset offset = 0):
28 f_(f), offset_(offset) { set_shape(shape); }
30 void set_offset(mpi::io::offset offset) { offset_ = offset; }
33 void set_shape(
const S& shape)
37 for (
unsigned i = 0; i < shape.size(); ++i)
39 shape_.push_back(shape[i]);
42 for (
int i = shape_.size() - 2; i >= 0; --i)
43 stride_[i] = stride_[i+1] * shape_[i+1];
46 const Shape& shape()
const {
return shape_; }
49 void read(
const DiscreteBounds& bounds, T* buffer,
bool collective =
false,
int chunk = 1)
const;
52 void write(
const DiscreteBounds& bounds,
const T* buffer,
bool collective =
false,
int chunk = 1);
63 std::vector<size_t> stride_;
72 read(
const DiscreteBounds& bounds, T* buffer,
bool collective,
int chunk)
const
74 int dim = shape_.size();
76 std::vector<int> subsizes;
77 for (
int i = 0; i < dim; ++i)
79 subsizes.push_back(bounds.max[i] - bounds.min[i] + 1);
80 total *= subsizes.back();
85 T_type = mpi::detail::get_mpi_datatype<T>();
91 int array_of_blocklengths[] = { chunk };
92 MPI_Aint array_of_displacements[] = { 0 };
93 MPI_Datatype array_of_types[] = { mpi::detail::get_mpi_datatype<T>() };
94 MPI_Type_create_struct(1, array_of_blocklengths, array_of_displacements, array_of_types, &T_type);
95 MPI_Type_commit(&T_type);
99 MPI_Type_create_subarray(dim, (
int*) &shape_[0], &subsizes[0], (
int*) &bounds.min[0], MPI_ORDER_C, T_type, &fileblk);
100 MPI_Type_commit(&fileblk);
102 MPI_File_set_view(f_.handle(), offset_, T_type, fileblk, (
char*)
"native", MPI_INFO_NULL);
106 MPI_File_read(f_.handle(), buffer, total, T_type, &s.s);
108 MPI_File_read_all(f_.handle(), buffer, total, T_type, &s.s);
111 MPI_Type_free(&T_type);
112 MPI_Type_free(&fileblk);
118 write(
const DiscreteBounds& bounds,
const T* buffer,
bool collective,
int chunk)
120 write(bounds, buffer, bounds, collective, chunk);
126 write(
const DiscreteBounds& bounds,
const T* buffer,
const DiscreteBounds& core,
bool collective,
int chunk)
128 int dim = shape_.size();
129 std::vector<int> subsizes;
130 std::vector<int> buffer_shape, buffer_start;
131 for (
int i = 0; i < dim; ++i)
133 buffer_shape.push_back(bounds.max[i] - bounds.min[i] + 1);
134 buffer_start.push_back(core.min[i] - bounds.min[i]);
135 subsizes.push_back(core.max[i] - core.min[i] + 1);
140 T_type = mpi::detail::get_mpi_datatype<T>();
144 int array_of_blocklengths[] = { chunk };
145 MPI_Aint array_of_displacements[] = { 0 };
146 MPI_Datatype array_of_types[] = { mpi::detail::get_mpi_datatype<T>() };
147 MPI_Type_create_struct(1, array_of_blocklengths, array_of_displacements, array_of_types, &T_type);
148 MPI_Type_commit(&T_type);
151 MPI_Datatype fileblk, subbuffer;
152 MPI_Type_create_subarray(dim, (
int*) &shape_[0], &subsizes[0], (
int*) &bounds.min[0], MPI_ORDER_C, T_type, &fileblk);
153 MPI_Type_create_subarray(dim, (
int*) &buffer_shape[0], &subsizes[0], (
int*) &buffer_start[0], MPI_ORDER_C, T_type, &subbuffer);
154 MPI_Type_commit(&fileblk);
155 MPI_Type_commit(&subbuffer);
157 MPI_File_set_view(f_.handle(), offset_, T_type, fileblk, (
char*)
"native", MPI_INFO_NULL);
161 MPI_File_write(f_.handle(), (
void*)buffer, 1, subbuffer, &s.s);
163 MPI_File_write_all(f_.handle(), (
void*)buffer, 1, subbuffer, &s.s);
166 MPI_Type_free(&T_type);
167 MPI_Type_free(&fileblk);
168 MPI_Type_free(&subbuffer);
Wraps MPI file IO.
Definition: io.hpp:16