Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
966a5fc30d80d69e18ba02402c65a852fa9d03e5
[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     std::string fullname=filename;
31     if (simgrid::s4u::Host::current()->get_disks().empty())
32       xbt_die("SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
33
34     size_t found=fullname.find('/');
35     //in case no fullpath is provided ... just pick the first mountpoint.
36     if(found==std::string::npos){
37       auto disk = simgrid::s4u::Host::current()->get_disks().front();
38       std::string mount;
39       if (disk->get_host() != simgrid::s4u::Host::current())
40         mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
41       else
42         mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
43       XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
44       fullname.insert(0, mount);
45     }
46
47     file_= new simgrid::s4u::File(fullname, nullptr);
48     list_=nullptr;
49     if (comm_->rank() == 0) {
50       int size= comm_->size() + FP_SIZE;
51       list_ = new char[size];
52       errhandler_= SMPI_default_File_Errhandler;
53       memset(list_, 0, size);
54       shared_file_pointer_ = new MPI_Offset();
55       shared_mutex_ = s4u::Mutex::create();
56       *shared_file_pointer_ = 0;
57       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
58     }else{
59       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
60     }
61     simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
62     simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
63     if(comm_->rank() != 0)
64       intrusive_ptr_add_ref(&*shared_mutex_);
65   }
66
67   File::~File(){
68     if(comm_->rank() == 0){
69       delete shared_file_pointer_;
70       delete[] list_;
71     }
72     delete win_;
73     delete file_;
74   }
75
76   int File::close(MPI_File *fh){
77     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
78     (*fh)->sync();
79     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
80       (*fh)->file_->unlink();
81     delete (*fh);
82     return MPI_SUCCESS;
83   }
84
85   int File::del(const char* filename, MPI_Info)
86   {
87     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
88     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
89     close(&f);
90     return MPI_SUCCESS;
91   }
92
93   int File::get_position(MPI_Offset* offset){
94     *offset=file_->tell();
95     return MPI_SUCCESS;
96   }
97
98   int File::get_position_shared(MPI_Offset* offset){
99     shared_mutex_->lock();
100     *offset=*shared_file_pointer_;
101     shared_mutex_->unlock();
102     return MPI_SUCCESS;
103   }
104
105   int File::seek(MPI_Offset offset, int whence){
106     switch(whence){
107       case(MPI_SEEK_SET):
108         XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
109         file_->seek(offset,SEEK_SET);
110         break;
111       case(MPI_SEEK_CUR):
112         XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
113         file_->seek(offset,SEEK_CUR);
114         break;
115       case(MPI_SEEK_END):
116         XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
117         file_->seek(offset,SEEK_END);
118         break;
119       default:
120         return MPI_ERR_FILE;
121     }
122     return MPI_SUCCESS;
123   }
124
125   int File::seek_shared(MPI_Offset offset, int whence){
126     shared_mutex_->lock();
127     seek(offset,whence);
128     *shared_file_pointer_=offset;
129     shared_mutex_->unlock();
130     return MPI_SUCCESS;
131   }
132
133   int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
134   {
135     //get position first as we may be doing non contiguous reads and it will probably be updated badly
136     MPI_Offset position = fh->file_->tell();
137     MPI_Offset movesize = datatype->get_extent()*count;
138     MPI_Offset readsize = datatype->size()*count;
139     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
140     MPI_Offset read = fh->file_->read(readsize);
141     XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
142     if(readsize!=movesize){
143       fh->file_->seek(position+movesize, SEEK_SET);
144     }
145     XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
146     if(status != MPI_STATUS_IGNORE)
147       status->count=count*datatype->size();
148     return MPI_SUCCESS;
149   }
150
151   /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
152   /* @InProceedings{10.1007/11557265_15,*/
153   /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/ 
154   /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
155   /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
156   /* year="2005",*/
157   /* publisher="Springer Berlin Heidelberg",*/
158   /* address="Berlin, Heidelberg",*/
159   /* pages="84--93"*/
160   /* }*/
161   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
162     fh->shared_mutex_->lock();
163     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
164     read(fh, buf, count, datatype, status);
165     *(fh->shared_file_pointer_)=fh->file_->tell();
166     fh->shared_mutex_->unlock();
167     return MPI_SUCCESS;
168   }
169
170   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
171     //0 needs to get the shared pointer value
172     MPI_Offset val;
173     if(fh->comm_->rank()==0){
174       val=*(fh->shared_file_pointer_);
175     }else{
176       val=count*datatype->size();
177     }
178
179     MPI_Offset result;
180     simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
181     fh->seek(result, MPI_SEEK_SET);
182     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
183     if(fh->comm_->rank()==fh->comm_->size()-1){
184       fh->shared_mutex_->lock();
185       *(fh->shared_file_pointer_)=fh->file_->tell();
186       fh->shared_mutex_->unlock();
187     }
188     char c;
189     simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
190     return ret;
191   }
192
193   int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
194   {
195     //get position first as we may be doing non contiguous reads and it will probably be updated badly
196     MPI_Offset position = fh->file_->tell();
197     MPI_Offset movesize = datatype->get_extent()*count;
198     MPI_Offset writesize = datatype->size()*count;
199     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
200     MPI_Offset write = fh->file_->write(writesize, 1);
201     XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
202     if(writesize!=movesize){
203       fh->file_->seek(position+movesize, SEEK_SET);
204     }
205     XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
206     if(status != MPI_STATUS_IGNORE)
207       status->count=count*datatype->size();
208     return MPI_SUCCESS;
209   }
210
211   int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
212     fh->shared_mutex_->lock();
213     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
214     write(fh, const_cast<void*>(buf), count, datatype, status);
215     *(fh->shared_file_pointer_)=fh->file_->tell();
216     fh->shared_mutex_->unlock();
217     return MPI_SUCCESS;
218   }
219
220   int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
221     //0 needs to get the shared pointer value
222     MPI_Offset val;
223     if(fh->comm_->rank()==0){
224       val=*(fh->shared_file_pointer_);
225     }else{
226       val=count*datatype->size();
227     }
228     MPI_Offset result;
229     simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
230     fh->seek(result, MPI_SEEK_SET);
231     int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
232     if(fh->comm_->rank()==fh->comm_->size()-1){
233       fh->shared_mutex_->lock();
234       *(fh->shared_file_pointer_)=fh->file_->tell();
235       fh->shared_mutex_->unlock();
236     }
237     char c;
238     simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
239     return ret;
240   }
241
242   int File::size(){
243     return file_->size();
244   }
245
246   int File::flags(){
247     return flags_;
248   }
249
250   int File::sync(){
251     //no idea
252     return simgrid::smpi::colls::barrier(comm_);
253   }
254
255   MPI_Info File::info(){
256     if(info_== MPI_INFO_NULL)
257       info_ = new Info();
258     info_->ref();
259     return info_;
260   }
261
262   void File::set_info(MPI_Info info){
263     if(info_!= MPI_INFO_NULL)
264       info->ref();
265     info_=info;
266   }
267
268   MPI_Comm File::comm(){
269     return comm_;
270   }
271
272   MPI_Errhandler File::errhandler(){
273     return errhandler_;
274   }
275
276   void File::set_errhandler(MPI_Errhandler errhandler){
277     errhandler_=errhandler;
278     if(errhandler_!= MPI_ERRHANDLER_NULL)
279       errhandler->ref();
280   }
281 }
282 }