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
23
24namespace shamalgs::collective {
25
37 template<class T>
38 void viewed_write_all_fetch(MPI_File fh, T *ptr_data, u64 data_cnt, u64 &file_head_ptr) {
39 auto dtype = get_mpi_type<T>();
40
41 i32 sz;
42 shamcomm::mpi::Type_size(dtype, &sz);
43
44 ViewInfo view = fetch_view(u64(sz) * data_cnt);
45
46 u64 disp = file_head_ptr + view.head_offset;
47
48 shamcomm::mpi::File_set_view(fh, disp, dtype, dtype, "native", MPI_INFO_NULL);
49
50 shamcomm::mpi::File_write_all(fh, ptr_data, data_cnt, dtype, MPI_STATUS_IGNORE);
51
52 file_head_ptr = view.total_byte_count + file_head_ptr;
53 }
54
67 template<class T>
69 MPI_File fh, T *ptr_data, u64 data_cnt, u64 total_cnt, u64 &file_head_ptr) {
70 auto dtype = get_mpi_type<T>();
71
72 i32 sz;
73 shamcomm::mpi::Type_size(dtype, &sz);
74
75 ViewInfo view = fetch_view_known_total(u64(sz) * data_cnt, u64(sz) * total_cnt);
76
77 u64 disp = file_head_ptr + view.head_offset;
78
79 shamcomm::mpi::File_set_view(fh, disp, dtype, dtype, "native", MPI_INFO_NULL);
80
81 shamcomm::mpi::File_write_all(fh, ptr_data, data_cnt, dtype, MPI_STATUS_IGNORE);
82
83 file_head_ptr = view.total_byte_count + file_head_ptr;
84 }
85
95 inline void write_header_raw(MPI_File fh, std::string s, u64 &file_head_ptr) {
96
97 shamcomm::mpi::File_set_view(
98 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
99
100 if (shamcomm::world_rank() == 0) {
101 shamcomm::mpi::File_write(fh, s.c_str(), s.size(), MPI_CHAR, MPI_STATUS_IGNORE);
102 }
103
104 file_head_ptr = file_head_ptr + s.size();
105 }
106
115 inline std::string read_header_raw(MPI_File fh, size_t len, u64 &file_head_ptr) {
116
117 shamcomm::mpi::File_set_view(
118 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
119 std::string s;
120 s.resize(len);
121
122 shamcomm::mpi::File_read(fh, s.data(), s.size(), MPI_CHAR, MPI_STATUS_IGNORE);
123
124 file_head_ptr = file_head_ptr + s.size();
125
126 return s;
127 }
128
136 inline void write_header_val(MPI_File fh, size_t val, u64 &file_head_ptr) {
137
138 shamcomm::mpi::File_set_view(
139 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
140
141 if (shamcomm::world_rank() == 0) {
142 shamcomm::mpi::File_write(fh, &val, 1, get_mpi_type<size_t>(), MPI_STATUS_IGNORE);
143 }
144
145 file_head_ptr = file_head_ptr + sizeof(size_t);
146 }
147
156 template<class T>
157 inline void write_at(MPI_File fh, const void *buf, size_t len, u64 file_head_ptr) {
158
159 shamcomm::mpi::File_write_at(
160 fh, file_head_ptr, buf, len, get_mpi_type<T>(), MPI_STATUS_IGNORE);
161 }
162
171 template<class T>
172 inline void read_at(MPI_File fh, void *buf, size_t len, u64 file_head_ptr) {
173
174 shamcomm::mpi::File_read_at(
175 fh, file_head_ptr, buf, len, get_mpi_type<T>(), MPI_STATUS_IGNORE);
176 }
177
185 inline size_t read_header_val(MPI_File fh, u64 &file_head_ptr) {
186
187 size_t val = 0;
188 shamcomm::mpi::File_set_view(
189 fh, file_head_ptr, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL);
190
191 shamcomm::mpi::File_read(fh, &val, 1, get_mpi_type<size_t>(), MPI_STATUS_IGNORE);
192
193 file_head_ptr = file_head_ptr + sizeof(size_t);
194
195 return val;
196 }
197
206 inline void write_header(MPI_File fh, std::string s, u64 &file_head_ptr) {
207
208 write_header_val(fh, s.size(), file_head_ptr);
209 write_header_raw(fh, s, file_head_ptr);
210 }
211
220 inline std::string read_header(MPI_File fh, u64 &file_head_ptr) {
221
222 size_t len = read_header_val(fh, file_head_ptr);
223 std::string s = read_header_raw(fh, len, file_head_ptr);
224 return s;
225 }
226
227} // namespace shamalgs::collective
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
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:220
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:95
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:115
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:185
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:206
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:157
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:38
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:136
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:68
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:172