Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Apply the default settings of 'smpi/buffering' too
[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/plugins/file_system.h"
17
18 #define FP_SIZE sizeof(MPI_Offset)
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
21
22 namespace simgrid{
23 namespace smpi{
24
25   File::File(MPI_Comm comm, const char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info) {
26     file_= new simgrid::s4u::File(filename, nullptr);
27     list_=nullptr;
28     if (comm_->rank() == 0) {
29       int size= comm_->size() + FP_SIZE;
30       list_ = new char[size];
31       memset(list_, 0, size);
32       shared_file_pointer_ = new MPI_Offset();
33       shared_mutex_ = s4u::Mutex::create();
34       *shared_file_pointer_ = 0;
35       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
36     }else{
37       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
38     }
39     simgrid::smpi::Colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
40     simgrid::smpi::Colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
41     if(comm_->rank() != 0)
42       intrusive_ptr_add_ref(&*shared_mutex_);
43   }
44
45   File::~File(){
46     if(comm_->rank() == 0){
47       delete shared_file_pointer_;
48       delete[] list_;
49     }
50     delete win_;
51     delete file_;
52   }
53
54   int File::close(MPI_File *fh){
55     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
56     (*fh)->sync();
57     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
58       (*fh)->file_->unlink();
59     delete (*fh);
60     return MPI_SUCCESS;
61   }
62
63   int File::del(const char* filename, MPI_Info)
64   {
65     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
66     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
67     close(&f);
68     return MPI_SUCCESS;
69   }
70
71   int File::get_position(MPI_Offset* offset){
72     *offset=file_->tell();
73     return MPI_SUCCESS;
74   }
75
76   int File::get_position_shared(MPI_Offset* offset){
77     shared_mutex_->lock();
78     *offset=*shared_file_pointer_;
79     shared_mutex_->unlock();
80     return MPI_SUCCESS;
81   }
82
83   int File::seek(MPI_Offset offset, int whence){
84     switch(whence){
85       case(MPI_SEEK_SET):
86         XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
87         file_->seek(offset,SEEK_SET);
88         break;
89       case(MPI_SEEK_CUR):
90         XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
91         file_->seek(offset,SEEK_CUR);
92         break;
93       case(MPI_SEEK_END):
94         XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
95         file_->seek(offset,SEEK_END);
96         break;
97       default:
98         return MPI_ERR_FILE;
99     }
100     return MPI_SUCCESS;
101   }
102
103   int File::seek_shared(MPI_Offset offset, int whence){
104     shared_mutex_->lock();
105     seek(offset,whence);
106     *shared_file_pointer_=offset;
107     shared_mutex_->unlock();
108     return MPI_SUCCESS;
109   }
110
111   int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
112   {
113     //get position first as we may be doing non contiguous reads and it will probably be updated badly
114     MPI_Offset position = fh->file_->tell();
115     MPI_Offset movesize = datatype->get_extent()*count;
116     MPI_Offset readsize = datatype->size()*count;
117     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
118     MPI_Offset read = fh->file_->read(readsize);
119     XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
120     if(readsize!=movesize){
121       fh->file_->seek(position+movesize, SEEK_SET);
122     }
123     XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
124     status->count=count*datatype->size();
125     return MPI_SUCCESS;
126   }
127
128   /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
129   /* @InProceedings{10.1007/11557265_15,*/
130   /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/ 
131   /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
132   /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
133   /* year="2005",*/
134   /* publisher="Springer Berlin Heidelberg",*/
135   /* address="Berlin, Heidelberg",*/
136   /* pages="84--93"*/
137   /* }*/
138   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
139     fh->shared_mutex_->lock();
140     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
141     read(fh, buf, count, datatype, status);
142     *(fh->shared_file_pointer_)=fh->file_->tell();
143     fh->shared_mutex_->unlock();
144     return MPI_SUCCESS;
145   }
146
147   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
148     //0 needs to get the shared pointer value
149     MPI_Offset val;
150     if(fh->comm_->rank()==0){
151       val=*(fh->shared_file_pointer_);
152     }else{
153       val=count*datatype->size();
154     }
155
156     MPI_Offset result;
157     simgrid::smpi::Colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
158     fh->seek(result, MPI_SEEK_SET);
159     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
160     if(fh->comm_->rank()==fh->comm_->size()-1){
161       fh->shared_mutex_->lock();
162       *(fh->shared_file_pointer_)=fh->file_->tell();
163       fh->shared_mutex_->unlock();
164     }
165     char c;
166     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
167     return ret;
168   }
169
170   int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
171   {
172     //get position first as we may be doing non contiguous reads and it will probably be updated badly
173     MPI_Offset position = fh->file_->tell();
174     MPI_Offset movesize = datatype->get_extent()*count;
175     MPI_Offset writesize = datatype->size()*count;
176     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
177     MPI_Offset write = fh->file_->write(writesize, 1);
178     XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
179     if(writesize!=movesize){
180       fh->file_->seek(position+movesize, SEEK_SET);
181     }
182     XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
183     status->count=count*datatype->size();
184     return MPI_SUCCESS;
185   }
186
187   int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
188     fh->shared_mutex_->lock();
189     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
190     write(fh, const_cast<void*>(buf), count, datatype, status);
191     *(fh->shared_file_pointer_)=fh->file_->tell();
192     fh->shared_mutex_->unlock();
193     return MPI_SUCCESS;
194   }
195
196   int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
197     //0 needs to get the shared pointer value
198     MPI_Offset val;
199     if(fh->comm_->rank()==0){
200       val=*(fh->shared_file_pointer_);
201     }else{
202       val=count*datatype->size();
203     }
204     MPI_Offset result;
205     simgrid::smpi::Colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
206     fh->seek(result, MPI_SEEK_SET);
207     int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
208     if(fh->comm_->rank()==fh->comm_->size()-1){
209       fh->shared_mutex_->lock();
210       *(fh->shared_file_pointer_)=fh->file_->tell();
211       fh->shared_mutex_->unlock();
212     }
213     char c;
214     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
215     return ret;
216   }
217
218   int File::size(){
219     return file_->size();
220   }
221
222   int File::flags(){
223     return flags_;
224   }
225
226   int File::sync(){
227     //no idea
228     return simgrid::smpi::Colls::barrier(comm_);
229   }
230
231   MPI_Info File::info(){
232     if(info_== MPI_INFO_NULL)
233       info_ = new Info();
234     info_->ref();
235     return info_;
236   }
237
238   void File::set_info(MPI_Info info){
239     if(info_!= MPI_INFO_NULL)
240       info->ref();
241     info_=info;
242   }
243
244   MPI_Comm File::comm(){
245     return comm_;
246   }
247 }
248 }