1 /* Copyright (c) 2007-2020. 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 #define FP_SIZE sizeof(MPI_Offset)
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
24 MPI_Errhandler SMPI_default_File_Errhandler = MPI_ERRORS_RETURN;
29 File::File(MPI_Comm comm, const char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info) {
30 if (info_ != MPI_INFO_NULL)
32 std::string fullname=filename;
33 if (simgrid::s4u::Host::current()->get_disks().empty())
34 xbt_die("SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
36 size_t found=fullname.find('/');
37 //in case no fullpath is provided ... just pick the first mountpoint.
38 if(found==std::string::npos || fullname.rfind("./", 1) != std::string::npos){
39 auto disk = simgrid::s4u::Host::current()->get_disks().front();
41 if (disk->get_host() != simgrid::s4u::Host::current())
42 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
44 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
45 XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
46 if(fullname.rfind("./",1) != std::string::npos)
47 fullname.replace(fullname.begin(), fullname.begin() + 1, mount);
50 fullname.insert(0, mount);
54 file_= new simgrid::s4u::File(fullname, nullptr);
56 if (comm_->rank() == 0) {
57 int size= comm_->size() + FP_SIZE;
58 list_ = new char[size];
59 errhandler_= SMPI_default_File_Errhandler;
61 memset(list_, 0, size);
62 shared_file_pointer_ = new MPI_Offset();
63 shared_mutex_ = s4u::Mutex::create();
64 *shared_file_pointer_ = 0;
65 win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
67 errhandler_ = MPI_ERRHANDLER_NULL;
68 win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
70 simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
71 simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
72 if(comm_->rank() != 0)
73 intrusive_ptr_add_ref(&*shared_mutex_);
77 if(comm_->rank() == 0){
78 delete shared_file_pointer_;
83 if (info_ != MPI_INFO_NULL)
84 simgrid::smpi::Info::unref(info_);
85 if (errhandler_ != MPI_ERRHANDLER_NULL)
86 simgrid::smpi::Errhandler::unref(errhandler_);
89 int File::close(MPI_File *fh){
90 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
92 if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
93 (*fh)->file_->unlink();
98 int File::del(const char* filename, const Info*)
100 //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
101 File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
106 int File::get_position(MPI_Offset* offset) const
108 *offset=file_->tell();
112 int File::get_position_shared(MPI_Offset* offset) const
114 shared_mutex_->lock();
115 *offset=*shared_file_pointer_;
116 shared_mutex_->unlock();
120 int File::seek(MPI_Offset offset, int whence){
123 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
124 file_->seek(offset,SEEK_SET);
127 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
128 file_->seek(offset,SEEK_CUR);
131 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
132 file_->seek(offset,SEEK_END);
140 int File::seek_shared(MPI_Offset offset, int whence){
141 shared_mutex_->lock();
143 *shared_file_pointer_=file_->tell();
144 shared_mutex_->unlock();
148 int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
150 //get position first as we may be doing non contiguous reads and it will probably be updated badly
151 MPI_Offset position = fh->file_->tell();
152 MPI_Offset movesize = datatype->get_extent()*count;
153 MPI_Offset readsize = datatype->size()*count;
154 XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
155 MPI_Offset read = fh->file_->read(readsize);
156 XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
157 if(readsize!=movesize){
158 fh->file_->seek(position+movesize, SEEK_SET);
160 XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
161 if(status != MPI_STATUS_IGNORE)
162 status->count=count*datatype->size();
166 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
167 /* @InProceedings{10.1007/11557265_15,*/
168 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
169 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
170 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
172 /* publisher="Springer Berlin Heidelberg",*/
173 /* address="Berlin, Heidelberg",*/
176 int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
178 fh->shared_mutex_->lock();
179 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
180 read(fh, buf, count, datatype, status);
181 *(fh->shared_file_pointer_)=fh->file_->tell();
182 fh->shared_mutex_->unlock();
186 int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
188 //0 needs to get the shared pointer value
190 if(fh->comm_->rank()==0){
191 val=*(fh->shared_file_pointer_);
193 val=count*datatype->size();
197 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
198 fh->seek(result, MPI_SEEK_SET);
199 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
200 if(fh->comm_->rank()==fh->comm_->size()-1){
201 fh->shared_mutex_->lock();
202 *(fh->shared_file_pointer_)=fh->file_->tell();
203 fh->shared_mutex_->unlock();
206 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
210 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
212 //get position first as we may be doing non contiguous reads and it will probably be updated badly
213 MPI_Offset position = fh->file_->tell();
214 MPI_Offset movesize = datatype->get_extent()*count;
215 MPI_Offset writesize = datatype->size()*count;
216 XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
217 MPI_Offset write = fh->file_->write(writesize, true);
218 XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
219 if(writesize!=movesize){
220 fh->file_->seek(position+movesize, SEEK_SET);
222 XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
223 if(status != MPI_STATUS_IGNORE)
224 status->count=count*datatype->size();
228 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
230 fh->shared_mutex_->lock();
231 XBT_DEBUG("Write shared on %s - Shared ptr before : %lld",fh->file_->get_path(), *(fh->shared_file_pointer_));
232 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
233 write(fh, const_cast<void*>(buf), count, datatype, status);
234 *(fh->shared_file_pointer_)=fh->file_->tell();
235 XBT_DEBUG("Write shared on %s - Shared ptr after : %lld",fh->file_->get_path(), *(fh->shared_file_pointer_));
236 fh->shared_mutex_->unlock();
240 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
242 //0 needs to get the shared pointer value
244 if(fh->comm_->rank()==0){
245 val=*(fh->shared_file_pointer_);
247 val=count*datatype->size();
250 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
251 fh->seek(result, MPI_SEEK_SET);
252 int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
253 if(fh->comm_->rank()==fh->comm_->size()-1){
254 fh->shared_mutex_->lock();
255 *(fh->shared_file_pointer_)=fh->file_->tell();
256 fh->shared_mutex_->unlock();
259 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
263 int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
267 datarep_=std::string(datarep);
268 seek_shared(0,MPI_SEEK_SET);
272 int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
276 snprintf(datarep, MPI_MAX_NAME_STRING+1, "%s", datarep_.c_str());
280 int File::size() const
282 return file_->size();
285 int File::flags() const
292 return simgrid::smpi::colls::barrier(comm_);
295 MPI_Info File::info()
297 if (info_ == MPI_INFO_NULL)
303 void File::set_info(MPI_Info info)
305 if (info_ != MPI_INFO_NULL)
306 simgrid::smpi::Info::unref(info_);
308 if (info_ != MPI_INFO_NULL)
312 MPI_Comm File::comm() const
317 MPI_Errhandler File::errhandler()
319 if (errhandler_ != MPI_ERRHANDLER_NULL)
324 void File::set_errhandler(MPI_Errhandler errhandler)
326 if (errhandler_ != MPI_ERRHANDLER_NULL)
327 simgrid::smpi::Errhandler::unref(errhandler_);
328 errhandler_ = errhandler;
329 if (errhandler_ != MPI_ERRHANDLER_NULL)
333 File* File::f2c(int id){
334 return static_cast<File*>(F2C::f2c(id));