Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
24
25namespace shamalgs::collective {
26
38 template<class T>
39 void viewed_write_all_fetch(MPI_File fh, T *ptr_data, u64 data_cnt, u64 &file_head_ptr) {
40 auto dtype = get_mpi_type<T>();
41
42 i32 sz;
43 shamcomm::mpi::Type_size(dtype, &sz);
44
45 ViewInfo view = fetch_view(u64(sz) * data_cnt);
46
47 u64 disp = file_head_ptr + view.head_offset;
48
49 shamcomm::mpi::File_set_view(fh, disp, dtype, dtype, "native", MPI_INFO_NULL);
50
51 shamcomm::mpi::File_write_all(fh, ptr_data, data_cnt, dtype, MPI_STATUS_IGNORE);
52
53 file_head_ptr = view.total_byte_count + file_head_ptr;
54 }
55
68 template<class T>
70 MPI_File fh, T *ptr_data, u64 data_cnt, u64 total_cnt, u64 &file_head_ptr) {
71 auto dtype = get_mpi_type<T>();
72
73 i32 sz;
74 shamcomm::mpi::Type_size(dtype, &sz);
75
76 ViewInfo view = fetch_view_known_total(u64(sz) * data_cnt, u64(sz) * total_cnt);
77
78 u64 disp = file_head_ptr + view.head_offset;
79
80 shamcomm::mpi::File_set_view(fh, disp, dtype, dtype, "native", MPI_INFO_NULL);
81
82 shamcomm::mpi::File_write_all(fh, ptr_data, data_cnt, dtype, MPI_STATUS_IGNORE);
83
84 file_head_ptr = view.total_byte_count + file_head_ptr;
85 }
86
96 inline void write_header_raw(MPI_File fh, std::string s, u64 &file_head_ptr) {
97
99 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
100
101 if (shamcomm::world_rank() == 0) {
102 shamcomm::mpi::File_write(fh, s.c_str(), s.size(), MPI_CHAR, MPI_STATUS_IGNORE);
103 }
104
105 file_head_ptr = file_head_ptr + s.size();
106 }
107
116 inline std::string read_header_raw(MPI_File fh, size_t len, u64 &file_head_ptr) {
117
119 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
120 std::string s;
121 s.resize(len);
122
123 shamcomm::mpi::File_read(fh, s.data(), s.size(), MPI_CHAR, MPI_STATUS_IGNORE);
124
125 file_head_ptr = file_head_ptr + s.size();
126
127 return s;
128 }
129
137 inline void write_header_val(MPI_File fh, size_t val, u64 &file_head_ptr) {
138
140 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
141
142 if (shamcomm::world_rank() == 0) {
143 shamcomm::mpi::File_write(fh, &val, 1, get_mpi_type<size_t>(), MPI_STATUS_IGNORE);
144 }
145
146 file_head_ptr = file_head_ptr + sizeof(size_t);
147 }
148
157 template<class T>
158 inline void write_at(MPI_File fh, const void *buf, size_t len, u64 file_head_ptr) {
159
161 fh,
162 file_head_ptr,
163 buf,
164 shambase::narrow_or_throw<int>(len),
165 get_mpi_type<T>(),
166 MPI_STATUS_IGNORE);
167 }
168
181 inline void write_at_large(MPI_File fh, const u8 *buf, size_t len, u64 file_head_ptr) {
182
183 size_t max_message = 1 << 30;
184
185 for (size_t offset = 0; offset < len; offset += max_message) {
186 const u8 *buf_ptr = buf + offset;
187 size_t msg_len = std::min(max_message, len - offset);
188 write_at<u8>(fh, buf_ptr, msg_len, file_head_ptr + offset);
189 }
190 }
191
200 template<class T>
201 inline void read_at(MPI_File fh, void *buf, size_t len, u64 file_head_ptr) {
202
204 fh,
205 file_head_ptr,
206 buf,
207 shambase::narrow_or_throw<int>(len),
208 get_mpi_type<T>(),
209 MPI_STATUS_IGNORE);
210 }
211
224 inline void read_at_large(MPI_File fh, u8 *buf, size_t len, u64 file_head_ptr) {
225 size_t max_message = 1 << 30;
226
227 for (size_t offset = 0; offset < len; offset += max_message) {
228 u8 *buf_ptr = buf + offset;
229 size_t msg_len = std::min(max_message, len - offset);
230 read_at<u8>(fh, buf_ptr, msg_len, file_head_ptr + offset);
231 }
232 }
233
241 inline size_t read_header_val(MPI_File fh, u64 &file_head_ptr) {
242
243 size_t val = 0;
245 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
246
247 shamcomm::mpi::File_read(fh, &val, 1, get_mpi_type<size_t>(), MPI_STATUS_IGNORE);
248
249 file_head_ptr = file_head_ptr + sizeof(size_t);
250
251 return val;
252 }
253
262 inline void write_header(MPI_File fh, std::string s, u64 &file_head_ptr) {
263
264 write_header_val(fh, s.size(), file_head_ptr);
265 write_header_raw(fh, s, file_head_ptr);
266 }
267
276 inline std::string read_header(MPI_File fh, u64 &file_head_ptr) {
277
278 size_t len = read_header_val(fh, file_head_ptr);
279 std::string s = read_header_raw(fh, len, file_head_ptr);
280 return s;
281 }
282
283} // namespace shamalgs::collective
std::uint8_t u8
8 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
Utility functions for MPI error checking.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
Utilities for safe type narrowing conversions.
std::string read_header(MPI_File fh, u64 &file_head_ptr)
Reads a string from a file using MPI and updates the file head pointer. The string is preceded by its...
Definition io.hpp:276
void write_header_raw(MPI_File fh, std::string s, u64 &file_head_ptr)
Writes a string to a file using MPI and updates the file head pointer.
Definition io.hpp:96
std::string read_header_raw(MPI_File fh, size_t len, u64 &file_head_ptr)
Reads a string of length len from a file using MPI and updates the file head pointer.
Definition io.hpp:116
size_t read_header_val(MPI_File fh, u64 &file_head_ptr)
Reads a size_t from a file using MPI and updates the file head pointer.
Definition io.hpp:241
void write_header(MPI_File fh, std::string s, u64 &file_head_ptr)
Writes a string to a file using MPI and updates the file head pointer. The string is preceded by its ...
Definition io.hpp:262
void write_at(MPI_File fh, const void *buf, size_t len, u64 file_head_ptr)
Writes data at a given offset in a file using MPI.
Definition io.hpp:158
void write_at_large(MPI_File fh, const u8 *buf, size_t len, u64 file_head_ptr)
Writes a large byte buffer at a given offset in a file using MPI.
Definition io.hpp:181
void viewed_write_all_fetch(MPI_File fh, T *ptr_data, u64 data_cnt, u64 &file_head_ptr)
Writes data to an MPI file in a collective manner.
Definition io.hpp:39
void read_at_large(MPI_File fh, u8 *buf, size_t len, u64 file_head_ptr)
Reads a large byte buffer at a given offset in a file using MPI.
Definition io.hpp:224
void write_header_val(MPI_File fh, size_t val, u64 &file_head_ptr)
Writes a size_t to a file using MPI and updates the file head pointer.
Definition io.hpp:137
void viewed_write_all_fetch_known_total_size(MPI_File fh, T *ptr_data, u64 data_cnt, u64 total_cnt, u64 &file_head_ptr)
Writes data to an MPI file in a collective manner and updates the file head pointer.
Definition io.hpp:69
void read_at(MPI_File fh, void *buf, size_t len, u64 file_head_ptr)
Reads data at a given offset in a file using MPI.
Definition io.hpp:201
void File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char *datarep, MPI_Info info)
MPI wrapper for MPI_File_set_view.
Definition wrapper.cpp:236
void File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_read.
Definition wrapper.cpp:272
void File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write_at.
Definition wrapper.cpp:279
void Type_size(MPI_Datatype type, int *size)
MPI wrapper for MPI_Type_size.
Definition wrapper.cpp:249
void File_write(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write.
Definition wrapper.cpp:264
void File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_read_at.
Definition wrapper.cpp:292
void File_write_all(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write_all.
Definition wrapper.cpp:256