Logo AND Algorithmique Numérique Distribuée

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