1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "smpi_comm.hpp"
8 #include "smpi_coll.hpp"
9 #include "smpi_datatype.hpp"
10 #include "smpi_info.hpp"
11 #include "smpi_win.hpp"
12 #include "smpi_request.hpp"
14 #include "smpi_file.hpp"
15 #include "smpi_status.hpp"
16 #include "simgrid/s4u/Disk.hpp"
17 #include "simgrid/s4u/Host.hpp"
18 #include "simgrid/plugins/file_system.h"
20 #include <mutex> // std::scoped_lock
22 #define FP_SIZE sizeof(MPI_Offset)
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
26 MPI_Errhandler SMPI_default_File_Errhandler = _smpi_cfg_default_errhandler_is_error ? MPI_ERRORS_ARE_FATAL : MPI_ERRORS_RETURN;;
28 namespace simgrid::smpi {
30 File::File(MPI_Comm comm, const char* filename, int amode, MPI_Info info) : comm_(comm), flags_(amode), info_(info)
32 if (info_ != MPI_INFO_NULL)
34 std::string fullname = filename;
35 xbt_assert(not simgrid::s4u::Host::current()->get_disks().empty(),
36 "SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
38 // in case no fullpath is provided ... just pick the first mountpoint.
39 if (size_t found = fullname.find('/'); found == std::string::npos || fullname.rfind("./", 1) != std::string::npos) {
40 const auto* disk = simgrid::s4u::Host::current()->get_disks().front();
42 if (disk->get_host() != simgrid::s4u::Host::current())
43 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
45 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
46 XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
47 if (fullname.rfind("./", 1) != std::string::npos)
48 fullname.replace(fullname.begin(), fullname.begin() + 1, mount);
51 fullname.insert(0, mount);
54 XBT_DEBUG("Opening %s", fullname.c_str());
55 file_ = simgrid::s4u::File::open(fullname, nullptr);
60 if (comm_->rank() == 0) {
61 int size = comm_->size() + FP_SIZE;
62 list_ = new char[size]();
63 errhandler_ = SMPI_default_File_Errhandler;
65 shared_file_pointer_ = new MPI_Offset();
66 shared_mutex_ = s4u::Mutex::create();
67 *shared_file_pointer_ = 0;
68 win_ = new Win(list_, size, 1, MPI_INFO_NULL, comm_);
70 errhandler_ = MPI_ERRHANDLER_NULL;
71 win_ = new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
73 simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
74 simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
75 if (comm_->rank() != 0)
76 intrusive_ptr_add_ref(&*shared_mutex_);
82 if (comm_->rank() == 0) {
83 delete shared_file_pointer_;
86 simgrid::smpi::Win::del(win_);
88 F2C::free_f(this->f2c_id());
89 if (info_ != MPI_INFO_NULL)
90 simgrid::smpi::Info::unref(info_);
91 if (errhandler_ != MPI_ERRHANDLER_NULL)
92 simgrid::smpi::Errhandler::unref(errhandler_);
95 int File::close(MPI_File* fh)
97 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
99 if ((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
100 (*fh)->file_->unlink();
105 int File::del(const char* filename, const Info*)
107 // get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
108 auto* f = new File(MPI_COMM_SELF, filename, MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_RDWR, nullptr);
113 int File::get_position(MPI_Offset* offset) const
115 *offset = file_->tell()/etype_->get_extent();
119 int File::get_position_shared(MPI_Offset* offset) const
121 const std::scoped_lock lock(*shared_mutex_);
122 *offset = *shared_file_pointer_/etype_->get_extent();
126 int File::seek(MPI_Offset offset, int whence)
130 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
131 file_->seek(offset, SEEK_SET);
134 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
135 file_->seek(offset, SEEK_CUR);
138 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
139 file_->seek(offset, SEEK_END);
147 int File::seek_shared(MPI_Offset offset, int whence)
149 const std::scoped_lock lock(*shared_mutex_);
150 seek(offset, whence);
151 *shared_file_pointer_ = file_->tell();
155 int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
157 // get position first as we may be doing non contiguous reads and it will probably be updated badly
158 MPI_Offset position = fh->file_->tell();
159 MPI_Offset movesize = datatype->get_extent() * count;
160 MPI_Offset readsize = datatype->size() * count;
161 XBT_DEBUG("Position before read in MPI_File %s : %llu, size %llu", fh->file_->get_path(), fh->file_->tell(), fh->file_->size());
162 MPI_Offset read = fh->file_->read(readsize);
163 XBT_VERB("Read in MPI_File %s, %lld bytes read, count %d, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, count,
165 if (readsize != movesize) {
166 fh->file_->seek(position + movesize, SEEK_SET);
168 XBT_VERB("Position after read in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
169 if (status != MPI_STATUS_IGNORE)
170 status->count = count * datatype->size();
174 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
175 /* @InProceedings{10.1007/11557265_15,*/
176 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
177 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
178 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
180 /* publisher="Springer Berlin Heidelberg",*/
181 /* address="Berlin, Heidelberg",*/
184 int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
186 if (const std::scoped_lock lock(*fh->shared_mutex_); true) {
187 fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
188 read(fh, buf, count, datatype, status);
189 *(fh->shared_file_pointer_) = fh->file_->tell();
191 fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
195 int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
197 // 0 needs to get the shared pointer value
199 if (fh->comm_->rank() == 0) {
200 val = *(fh->shared_file_pointer_);
202 val = count * datatype->size();
206 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
208 fh->get_position(&prev);
209 fh->seek(result, MPI_SEEK_SET);
210 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
211 if (fh->comm_->rank() == fh->comm_->size() - 1) {
212 const std::scoped_lock lock(*fh->shared_mutex_);
213 *(fh->shared_file_pointer_)=fh->file_->tell();
216 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
217 fh->seek(prev, MPI_SEEK_SET);
221 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
223 // get position first as we may be doing non contiguous reads and it will probably be updated badly
224 MPI_Offset position = fh->file_->tell();
225 MPI_Offset movesize = datatype->get_extent() * count;
226 MPI_Offset writesize = datatype->size() * count;
227 XBT_DEBUG("Position before write in MPI_File %s : %llu, size %llu", fh->file_->get_path(), fh->file_->tell(), fh->file_->size());
228 MPI_Offset write = fh->file_->write(writesize, true);
229 XBT_VERB("Write in MPI_File %s, %lld bytes written, count %d, writesize %lld bytes, movesize %lld", fh->file_->get_path(), write,
230 count, writesize, movesize);
231 if (writesize != movesize) {
232 fh->file_->seek(position + movesize, SEEK_SET);
234 XBT_VERB("Position after write in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
235 if (status != MPI_STATUS_IGNORE)
236 status->count = count * datatype->size();
240 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
242 const std::scoped_lock lock(*fh->shared_mutex_);
243 XBT_DEBUG("Write shared on %s - Shared ptr before : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
244 fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
245 write(fh, const_cast<void*>(buf), count, datatype, status);
246 *(fh->shared_file_pointer_) = fh->file_->tell();
247 XBT_DEBUG("Write shared on %s - Shared ptr after : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
248 fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
252 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
254 // 0 needs to get the shared pointer value
256 if (fh->comm_->rank() == 0) {
257 val = *(fh->shared_file_pointer_);
259 val = count * datatype->size();
262 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
264 fh->get_position(&prev);
265 fh->seek(result, MPI_SEEK_SET);
266 int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
267 if (fh->comm_->rank() == fh->comm_->size() - 1) {
268 const std::scoped_lock lock(*fh->shared_mutex_);
269 *(fh->shared_file_pointer_)=fh->file_->tell();
272 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
273 fh->seek(prev, MPI_SEEK_SET);
277 int File::set_view(MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
280 filetype_ = filetype;
283 if (comm_->rank() == 0){
284 if(disp != MPI_DISPLACEMENT_CURRENT)
285 seek_shared(disp, MPI_SEEK_SET);
287 seek_shared(0, MPI_SEEK_CUR);
293 int File::get_view(MPI_Offset* disp, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
297 *filetype = filetype_;
298 snprintf(datarep, MPI_MAX_NAME_STRING + 1, "%s", datarep_.c_str());
302 int File::size() const
304 return file_->size();
307 void File::set_size(int size)
309 file_->write(size, true);
312 int File::flags() const
317 MPI_Datatype File::etype() const
325 return simgrid::smpi::colls::barrier(comm_);
328 MPI_Info File::info()
333 void File::set_info(MPI_Info info)
335 if (info_ != MPI_INFO_NULL)
336 simgrid::smpi::Info::unref(info_);
338 if (info_ != MPI_INFO_NULL)
342 MPI_Comm File::comm() const
347 MPI_Errhandler File::errhandler()
349 if (errhandler_ != MPI_ERRHANDLER_NULL)
354 void File::set_errhandler(MPI_Errhandler errhandler)
356 if (errhandler_ != MPI_ERRHANDLER_NULL)
357 simgrid::smpi::Errhandler::unref(errhandler_);
358 errhandler_ = errhandler;
359 if (errhandler_ != MPI_ERRHANDLER_NULL)
363 File* File::f2c(int id)
365 return static_cast<File*>(F2C::f2c(id));
368 void File::set_atomicity(bool a){
372 bool File::get_atomicity() const
377 } // namespace simgrid::smpi