Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_File_read_shared, MPI_File_write_shared, MPI_File_read_ordered, MPI_File_write_or...
[simgrid.git] / src / smpi / mpi / smpi_file.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */\r
2 \r
3 /* This program is free software; you can redistribute it and/or modify it\r
4  * under the terms of the license (GNU LGPL) which comes with this package. */\r
5 #include "private.hpp"\r
6 \r
7 #include "smpi_comm.hpp"\r
8 #include "smpi_coll.hpp"\r
9 #include "smpi_datatype.hpp"\r
10 #include "smpi_info.hpp"\r
11 #include "smpi_win.hpp"\r
12 #include "smpi_file.hpp"\r
13 #include "smpi_status.hpp"\r
14 #include "simgrid/plugins/file_system.h"\r
15 \r
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");\r
17 #define FP_SIZE sizeof(MPI_Offset)\r
18 \r
19 \r
20 namespace simgrid{\r
21 namespace smpi{\r
22 \r
23   File::File(MPI_Comm comm, char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info), shared_file_pointer_(0) {\r
24     file_= new simgrid::s4u::File(filename, nullptr);\r
25     list_=nullptr;\r
26     if (comm_->rank() == 0) {\r
27       int size= comm_->size() + FP_SIZE;\r
28       list_ = new char[size];\r
29       memset(list_, 0, size);\r
30       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);\r
31     }else{\r
32       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);\r
33     }\r
34   }\r
35 \r
36   File::~File(){\r
37     delete file_;\r
38   }\r
39 \r
40   int File::close(MPI_File *fh){\r
41     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());\r
42     (*fh)->sync();\r
43     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)\r
44       (*fh)->file_->unlink();\r
45     delete (*fh);\r
46     return MPI_SUCCESS;\r
47   }\r
48 \r
49   int File::del(char *filename, MPI_Info info){\r
50     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it\r
51     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);\r
52     close(&f);\r
53     return MPI_SUCCESS;\r
54   }\r
55 \r
56   int File::get_position(MPI_Offset* offset){\r
57     *offset=file_->tell();\r
58     return MPI_SUCCESS;\r
59   }\r
60 \r
61   int File::get_position_shared(MPI_Offset* offset){\r
62     lock();\r
63     *offset=shared_file_pointer_;\r
64     unlock();\r
65     return MPI_SUCCESS;\r
66   }\r
67 \r
68   int File::seek(MPI_Offset offset, int whence){\r
69     switch(whence){\r
70       case(MPI_SEEK_SET):\r
71         XBT_DEBUG("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);\r
72         file_->seek(offset,SEEK_SET);\r
73         break;\r
74       case(MPI_SEEK_CUR):\r
75         XBT_DEBUG("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);\r
76         file_->seek(offset,SEEK_CUR);\r
77         break;\r
78       case(MPI_SEEK_END):\r
79         XBT_DEBUG("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);\r
80         file_->seek(offset,SEEK_END);\r
81         break;\r
82       default:\r
83         return MPI_ERR_FILE;\r
84     }\r
85     return MPI_SUCCESS;\r
86   }\r
87 \r
88   int File::seek_shared(MPI_Offset offset, int whence){\r
89     lock();\r
90     seek(offset,whence);\r
91     shared_file_pointer_=file_->tell();\r
92     unlock();\r
93     return MPI_SUCCESS;\r
94   }\r
95 \r
96   int File::read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
97     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
98     MPI_Offset position = fh->file_->tell();\r
99     MPI_Offset movesize = datatype->get_extent()*count;\r
100     MPI_Offset readsize = datatype->size()*count;\r
101     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
102     MPI_Offset read = fh->file_->read(readsize);\r
103     XBT_DEBUG("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);\r
104     if(readsize!=movesize){\r
105       fh->file_->seek(position+movesize, SEEK_SET);\r
106     }\r
107     XBT_DEBUG("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
108     status->count=count*datatype->size();\r
109     return MPI_SUCCESS;\r
110   }\r
111 \r
112   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
113     fh->lock();\r
114     fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);\r
115     read(fh, buf, count, datatype, status);\r
116     fh->shared_file_pointer_=fh->file_->tell();\r
117     fh->unlock();\r
118     return MPI_SUCCESS;\r
119   }\r
120 \r
121   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
122     //0 needs to get the shared pointer value\r
123     if(fh->comm_->rank()==0){\r
124       fh->lock();\r
125       fh->unlock();\r
126     }else{\r
127       fh->shared_file_pointer_=count*datatype->size();\r
128     }\r
129     MPI_Offset result;\r
130     simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);\r
131     fh->seek(result, MPI_SEEK_SET);\r
132     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);\r
133     if(fh->comm_->rank()==fh->comm_->size()-1){\r
134       fh->lock();\r
135       fh->unlock();\r
136     }\r
137     char c;\r
138     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);\r
139     return ret;\r
140   }\r
141 \r
142   int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
143     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
144     MPI_Offset position = fh->file_->tell();\r
145     MPI_Offset movesize = datatype->get_extent()*count;\r
146     MPI_Offset writesize = datatype->size()*count;\r
147     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
148     MPI_Offset write = fh->file_->write(writesize);\r
149     XBT_DEBUG("Write in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);\r
150     if(writesize!=movesize){\r
151       fh->file_->seek(position+movesize, SEEK_SET);\r
152     }\r
153     XBT_DEBUG("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
154     status->count=count*datatype->size();\r
155     return MPI_SUCCESS;\r
156   }\r
157 \r
158   int File::write_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
159     fh->lock();\r
160     fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);\r
161     write(fh, buf, count, datatype, status);\r
162     fh->shared_file_pointer_=fh->file_->tell();\r
163     fh->unlock();\r
164     return MPI_SUCCESS;\r
165   }\r
166 \r
167   int File::write_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
168     //0 needs to get the shared pointer value\r
169     if(fh->comm_->rank()==0){\r
170       fh->lock();\r
171       fh->unlock();\r
172     }else{\r
173       fh->shared_file_pointer_=count*datatype->size();\r
174     }\r
175     MPI_Offset result;\r
176     simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);\r
177     fh->seek(result, MPI_SEEK_SET);\r
178     int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);\r
179     if(fh->comm_->rank()==fh->comm_->size()-1){\r
180       fh->lock();\r
181       fh->unlock();\r
182     }\r
183     char c;\r
184     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);\r
185     return ret;\r
186   }\r
187 \r
188   int File::size(){\r
189     return file_->size();\r
190   }\r
191 \r
192   int File::flags(){\r
193     return flags_;\r
194   }\r
195 \r
196   int File::sync(){\r
197     //no idea\r
198     return simgrid::smpi::Colls::barrier(comm_);\r
199   }\r
200 \r
201   int File::lock()\r
202 {\r
203   int rank = comm_->rank();\r
204   int size = comm_->size();\r
205   char waitlist[size];\r
206   char lock = 1;\r
207   int tag=444;\r
208   int i;\r
209   win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);\r
210   win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);\r
211   win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);\r
212   win_->get(&shared_file_pointer_ , 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);\r
213   win_->unlock(0);\r
214   for (i = 0; i < size; i++) {\r
215     if (waitlist[i] == 1 && i != rank) {\r
216       // wait for the lock\r
217       MPI_Recv(&lock, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, comm_, MPI_STATUS_IGNORE);\r
218       break;\r
219     }\r
220   }\r
221   return 0;\r
222 }\r
223 \r
224 int File::unlock()\r
225 {\r
226   int rank = comm_->rank();\r
227   int size = comm_->size();\r
228   char waitlist[size];\r
229   char lock = 0;\r
230   int tag=444;\r
231   int i, next;\r
232   win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);\r
233   win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);\r
234   win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);\r
235   shared_file_pointer_=file_->tell();\r
236   win_->put(&shared_file_pointer_, 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);\r
237 \r
238   win_->unlock(0);\r
239   next = (rank + 1 + size) % size;\r
240   for (i = 0; i < size; i++, next = (next + 1) % size) {\r
241     if (waitlist[next] == 1) {\r
242       MPI_Send(&lock, 1, MPI_CHAR, next, tag, comm_);\r
243       break;\r
244     }\r
245   }\r
246   return 0;\r
247 }\r
248 \r
249 MPI_Info File::info(){\r
250   if(info_== MPI_INFO_NULL)\r
251     info_ = new Info();\r
252   info_->ref();\r
253   return info_;\r
254 }\r
255 \r
256 void File::set_info(MPI_Info info){\r
257   if(info_!= MPI_INFO_NULL)\r
258     info->ref();\r
259   info_=info;\r
260 }\r
261 \r
262 }\r
263 }\r