1 /* Copyright (c) 2007-2019. 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){
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());
47 fullname.insert(0, mount);
50 file_= new simgrid::s4u::File(fullname, nullptr);
52 if (comm_->rank() == 0) {
53 int size= comm_->size() + FP_SIZE;
54 list_ = new char[size];
55 errhandler_= SMPI_default_File_Errhandler;
56 memset(list_, 0, size);
57 shared_file_pointer_ = new MPI_Offset();
58 shared_mutex_ = s4u::Mutex::create();
59 *shared_file_pointer_ = 0;
60 win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
62 win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
64 simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
65 simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
66 if(comm_->rank() != 0)
67 intrusive_ptr_add_ref(&*shared_mutex_);
71 if(comm_->rank() == 0){
72 delete shared_file_pointer_;
77 if (info_ != MPI_INFO_NULL)
78 simgrid::smpi::Info::unref(info_);
81 int File::close(MPI_File *fh){
82 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
84 if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
85 (*fh)->file_->unlink();
90 int File::del(const char* filename, MPI_Info)
92 //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
93 File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
98 int File::get_position(MPI_Offset* offset){
99 *offset=file_->tell();
103 int File::get_position_shared(MPI_Offset* offset){
104 shared_mutex_->lock();
105 *offset=*shared_file_pointer_;
106 shared_mutex_->unlock();
110 int File::seek(MPI_Offset offset, int whence){
113 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
114 file_->seek(offset,SEEK_SET);
117 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
118 file_->seek(offset,SEEK_CUR);
121 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
122 file_->seek(offset,SEEK_END);
130 int File::seek_shared(MPI_Offset offset, int whence){
131 shared_mutex_->lock();
133 *shared_file_pointer_=offset;
134 shared_mutex_->unlock();
138 int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
140 //get position first as we may be doing non contiguous reads and it will probably be updated badly
141 MPI_Offset position = fh->file_->tell();
142 MPI_Offset movesize = datatype->get_extent()*count;
143 MPI_Offset readsize = datatype->size()*count;
144 XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
145 MPI_Offset read = fh->file_->read(readsize);
146 XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
147 if(readsize!=movesize){
148 fh->file_->seek(position+movesize, SEEK_SET);
150 XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
151 if(status != MPI_STATUS_IGNORE)
152 status->count=count*datatype->size();
156 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
157 /* @InProceedings{10.1007/11557265_15,*/
158 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
159 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
160 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
162 /* publisher="Springer Berlin Heidelberg",*/
163 /* address="Berlin, Heidelberg",*/
166 int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
167 fh->shared_mutex_->lock();
168 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
169 read(fh, buf, count, datatype, status);
170 *(fh->shared_file_pointer_)=fh->file_->tell();
171 fh->shared_mutex_->unlock();
175 int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
176 //0 needs to get the shared pointer value
178 if(fh->comm_->rank()==0){
179 val=*(fh->shared_file_pointer_);
181 val=count*datatype->size();
185 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
186 fh->seek(result, MPI_SEEK_SET);
187 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
188 if(fh->comm_->rank()==fh->comm_->size()-1){
189 fh->shared_mutex_->lock();
190 *(fh->shared_file_pointer_)=fh->file_->tell();
191 fh->shared_mutex_->unlock();
194 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
198 int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
200 //get position first as we may be doing non contiguous reads and it will probably be updated badly
201 MPI_Offset position = fh->file_->tell();
202 MPI_Offset movesize = datatype->get_extent()*count;
203 MPI_Offset writesize = datatype->size()*count;
204 XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
205 MPI_Offset write = fh->file_->write(writesize, 1);
206 XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
207 if(writesize!=movesize){
208 fh->file_->seek(position+movesize, SEEK_SET);
210 XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
211 if(status != MPI_STATUS_IGNORE)
212 status->count=count*datatype->size();
216 int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
217 fh->shared_mutex_->lock();
218 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
219 write(fh, const_cast<void*>(buf), count, datatype, status);
220 *(fh->shared_file_pointer_)=fh->file_->tell();
221 fh->shared_mutex_->unlock();
225 int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
226 //0 needs to get the shared pointer value
228 if(fh->comm_->rank()==0){
229 val=*(fh->shared_file_pointer_);
231 val=count*datatype->size();
234 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
235 fh->seek(result, MPI_SEEK_SET);
236 int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
237 if(fh->comm_->rank()==fh->comm_->size()-1){
238 fh->shared_mutex_->lock();
239 *(fh->shared_file_pointer_)=fh->file_->tell();
240 fh->shared_mutex_->unlock();
243 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
247 int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, MPI_Info)
251 datarep_=std::string(datarep);
252 seek_shared(0,MPI_SEEK_SET);
256 int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep)
260 snprintf(datarep, MPI_MAX_NAME_STRING+1, "%s", datarep_.c_str());
265 return file_->size();
274 return simgrid::smpi::colls::barrier(comm_);
277 MPI_Info File::info()
279 if (info_ == MPI_INFO_NULL)
285 void File::set_info(MPI_Info info)
287 if (info_ != MPI_INFO_NULL)
288 simgrid::smpi::Info::unref(info_);
290 if (info_ != MPI_INFO_NULL)
294 MPI_Comm File::comm(){
298 MPI_Errhandler File::errhandler(){
302 void File::set_errhandler(MPI_Errhandler errhandler){
303 errhandler_=errhandler;
304 if(errhandler_!= MPI_ERRHANDLER_NULL)