Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix refcounting of smpi::Errhandler.
[simgrid.git] / src / smpi / mpi / smpi_file.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5 #include "private.hpp"
6
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"
13
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"
19
20 #define FP_SIZE sizeof(MPI_Offset)
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
23
24 MPI_Errhandler SMPI_default_File_Errhandler = MPI_ERRORS_RETURN;
25
26 namespace simgrid{
27 namespace smpi{
28
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)
31       info_->ref();
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");
35
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();
40       std::string mount;
41       if (disk->get_host() != simgrid::s4u::Host::current())
42         mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
43       else
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       mount.append("/");
47       fullname.insert(0, mount);
48     }
49
50     file_= new simgrid::s4u::File(fullname, nullptr);
51     list_=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       errhandler_->ref();
57       memset(list_, 0, size);
58       shared_file_pointer_ = new MPI_Offset();
59       shared_mutex_ = s4u::Mutex::create();
60       *shared_file_pointer_ = 0;
61       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
62     }else{
63       errhandler_ = MPI_ERRHANDLER_NULL;
64       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
65     }
66     simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
67     simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
68     if(comm_->rank() != 0)
69       intrusive_ptr_add_ref(&*shared_mutex_);
70   }
71
72   File::~File(){
73     if(comm_->rank() == 0){
74       delete shared_file_pointer_;
75       delete[] list_;
76     }
77     delete win_;
78     delete file_;
79     if (info_ != MPI_INFO_NULL)
80       simgrid::smpi::Info::unref(info_);
81     if (errhandler_ != MPI_ERRHANDLER_NULL)
82       simgrid::smpi::Errhandler::unref(errhandler_);
83   }
84
85   int File::close(MPI_File *fh){
86     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
87     (*fh)->sync();
88     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
89       (*fh)->file_->unlink();
90     delete (*fh);
91     return MPI_SUCCESS;
92   }
93
94   int File::del(const char* filename, MPI_Info)
95   {
96     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
97     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
98     close(&f);
99     return MPI_SUCCESS;
100   }
101
102   int File::get_position(MPI_Offset* offset){
103     *offset=file_->tell();
104     return MPI_SUCCESS;
105   }
106
107   int File::get_position_shared(MPI_Offset* offset){
108     shared_mutex_->lock();
109     *offset=*shared_file_pointer_;
110     shared_mutex_->unlock();
111     return MPI_SUCCESS;
112   }
113
114   int File::seek(MPI_Offset offset, int whence){
115     switch(whence){
116       case(MPI_SEEK_SET):
117         XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
118         file_->seek(offset,SEEK_SET);
119         break;
120       case(MPI_SEEK_CUR):
121         XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
122         file_->seek(offset,SEEK_CUR);
123         break;
124       case(MPI_SEEK_END):
125         XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
126         file_->seek(offset,SEEK_END);
127         break;
128       default:
129         return MPI_ERR_FILE;
130     }
131     return MPI_SUCCESS;
132   }
133
134   int File::seek_shared(MPI_Offset offset, int whence){
135     shared_mutex_->lock();
136     seek(offset,whence);
137     *shared_file_pointer_=offset;
138     shared_mutex_->unlock();
139     return MPI_SUCCESS;
140   }
141
142   int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
143   {
144     //get position first as we may be doing non contiguous reads and it will probably be updated badly
145     MPI_Offset position = fh->file_->tell();
146     MPI_Offset movesize = datatype->get_extent()*count;
147     MPI_Offset readsize = datatype->size()*count;
148     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
149     MPI_Offset read = fh->file_->read(readsize);
150     XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
151     if(readsize!=movesize){
152       fh->file_->seek(position+movesize, SEEK_SET);
153     }
154     XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
155     if(status != MPI_STATUS_IGNORE)
156       status->count=count*datatype->size();
157     return MPI_SUCCESS;
158   }
159
160   /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
161   /* @InProceedings{10.1007/11557265_15,*/
162   /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/ 
163   /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
164   /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
165   /* year="2005",*/
166   /* publisher="Springer Berlin Heidelberg",*/
167   /* address="Berlin, Heidelberg",*/
168   /* pages="84--93"*/
169   /* }*/
170   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
171     fh->shared_mutex_->lock();
172     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
173     read(fh, buf, count, datatype, status);
174     *(fh->shared_file_pointer_)=fh->file_->tell();
175     fh->shared_mutex_->unlock();
176     return MPI_SUCCESS;
177   }
178
179   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
180     //0 needs to get the shared pointer value
181     MPI_Offset val;
182     if(fh->comm_->rank()==0){
183       val=*(fh->shared_file_pointer_);
184     }else{
185       val=count*datatype->size();
186     }
187
188     MPI_Offset result;
189     simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
190     fh->seek(result, MPI_SEEK_SET);
191     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
192     if(fh->comm_->rank()==fh->comm_->size()-1){
193       fh->shared_mutex_->lock();
194       *(fh->shared_file_pointer_)=fh->file_->tell();
195       fh->shared_mutex_->unlock();
196     }
197     char c;
198     simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
199     return ret;
200   }
201
202   int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
203   {
204     //get position first as we may be doing non contiguous reads and it will probably be updated badly
205     MPI_Offset position = fh->file_->tell();
206     MPI_Offset movesize = datatype->get_extent()*count;
207     MPI_Offset writesize = datatype->size()*count;
208     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
209     MPI_Offset write = fh->file_->write(writesize, 1);
210     XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
211     if(writesize!=movesize){
212       fh->file_->seek(position+movesize, SEEK_SET);
213     }
214     XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
215     if(status != MPI_STATUS_IGNORE)
216       status->count=count*datatype->size();
217     return MPI_SUCCESS;
218   }
219
220   int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
221     fh->shared_mutex_->lock();
222     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
223     write(fh, const_cast<void*>(buf), count, datatype, status);
224     *(fh->shared_file_pointer_)=fh->file_->tell();
225     fh->shared_mutex_->unlock();
226     return MPI_SUCCESS;
227   }
228
229   int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
230     //0 needs to get the shared pointer value
231     MPI_Offset val;
232     if(fh->comm_->rank()==0){
233       val=*(fh->shared_file_pointer_);
234     }else{
235       val=count*datatype->size();
236     }
237     MPI_Offset result;
238     simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
239     fh->seek(result, MPI_SEEK_SET);
240     int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
241     if(fh->comm_->rank()==fh->comm_->size()-1){
242       fh->shared_mutex_->lock();
243       *(fh->shared_file_pointer_)=fh->file_->tell();
244       fh->shared_mutex_->unlock();
245     }
246     char c;
247     simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
248     return ret;
249   }
250
251   int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, MPI_Info)
252   {
253     etype_=etype;
254     filetype_=filetype;
255     datarep_=std::string(datarep);
256     seek_shared(0,MPI_SEEK_SET);
257     return MPI_SUCCESS;
258   }
259
260   int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep)
261   {
262     *etype=etype_;
263     *filetype=filetype_;
264     snprintf(datarep, MPI_MAX_NAME_STRING+1, "%s", datarep_.c_str());
265     return MPI_SUCCESS;
266   }
267
268   int File::size(){
269     return file_->size();
270   }
271
272   int File::flags(){
273     return flags_;
274   }
275
276   int File::sync(){
277     //no idea
278     return simgrid::smpi::colls::barrier(comm_);
279   }
280
281   MPI_Info File::info()
282   {
283     if (info_ == MPI_INFO_NULL)
284       info_ = new Info();
285     info_->ref();
286     return info_;
287   }
288
289   void File::set_info(MPI_Info info)
290   {
291     if (info_ != MPI_INFO_NULL)
292       simgrid::smpi::Info::unref(info_);
293     info_ = info;
294     if (info_ != MPI_INFO_NULL)
295       info_->ref();
296   }
297
298   MPI_Comm File::comm(){
299     return comm_;
300   }
301
302   MPI_Errhandler File::errhandler()
303   {
304     if (errhandler_ != MPI_ERRHANDLER_NULL)
305       errhandler_->ref();
306     return errhandler_;
307   }
308
309   void File::set_errhandler(MPI_Errhandler errhandler)
310   {
311     if (errhandler_ != MPI_ERRHANDLER_NULL)
312       simgrid::smpi::Errhandler::unref(errhandler_);
313     errhandler_ = errhandler;
314     if (errhandler_ != MPI_ERRHANDLER_NULL)
315       errhandler_->ref();
316   }
317 }
318 }