Logo AND Algorithmique Numérique Distribuée

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