1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
\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
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 //setup here, because we have templates in smpi_file we want to log
\r
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
\r
15 #include "smpi_file.hpp"
\r
16 #include "smpi_status.hpp"
\r
17 #include "simgrid/plugins/file_system.h"
\r
19 #define FP_SIZE sizeof(MPI_Offset)
\r
25 File::File(MPI_Comm comm, char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info), shared_file_pointer_(0) {
\r
26 file_= new simgrid::s4u::File(filename, nullptr);
\r
28 if (comm_->rank() == 0) {
\r
29 int size= comm_->size() + FP_SIZE;
\r
30 list_ = new char[size];
\r
31 memset(list_, 0, size);
\r
32 win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
\r
34 win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
\r
42 int File::close(MPI_File *fh){
\r
43 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
\r
45 if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
\r
46 (*fh)->file_->unlink();
\r
51 int File::del(char *filename, MPI_Info info){
\r
52 //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
\r
53 File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
\r
58 int File::get_position(MPI_Offset* offset){
\r
59 *offset=file_->tell();
\r
63 int File::get_position_shared(MPI_Offset* offset){
\r
65 *offset=shared_file_pointer_;
\r
70 int File::seek(MPI_Offset offset, int whence){
\r
73 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
\r
74 file_->seek(offset,SEEK_SET);
\r
77 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
\r
78 file_->seek(offset,SEEK_CUR);
\r
81 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
\r
82 file_->seek(offset,SEEK_END);
\r
85 return MPI_ERR_FILE;
\r
90 int File::seek_shared(MPI_Offset offset, int whence){
\r
92 seek(offset,whence);
\r
93 shared_file_pointer_=file_->tell();
\r
98 int File::read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
99 //get position first as we may be doing non contiguous reads and it will probably be updated badly
\r
100 MPI_Offset position = fh->file_->tell();
\r
101 MPI_Offset movesize = datatype->get_extent()*count;
\r
102 MPI_Offset readsize = datatype->size()*count;
\r
103 XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
\r
104 MPI_Offset read = fh->file_->read(readsize);
\r
105 XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
\r
106 if(readsize!=movesize){
\r
107 fh->file_->seek(position+movesize, SEEK_SET);
\r
109 XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
\r
110 status->count=count*datatype->size();
\r
111 return MPI_SUCCESS;
\r
114 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
\r
115 /* @InProceedings{10.1007/11557265_15,*/
\r
116 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
\r
117 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
\r
118 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
\r
120 /* publisher="Springer Berlin Heidelberg",*/
\r
121 /* address="Berlin, Heidelberg",*/
\r
122 /* pages="84--93"*/
\r
124 int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
126 fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);
\r
127 read(fh, buf, count, datatype, status);
\r
128 fh->shared_file_pointer_=fh->file_->tell();
\r
130 return MPI_SUCCESS;
\r
133 int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
134 //0 needs to get the shared pointer value
\r
135 if(fh->comm_->rank()==0){
\r
139 fh->shared_file_pointer_=count*datatype->size();
\r
142 simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
\r
143 fh->seek(result, MPI_SEEK_SET);
\r
144 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
\r
145 if(fh->comm_->rank()==fh->comm_->size()-1){
\r
150 simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
\r
154 int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
155 //get position first as we may be doing non contiguous reads and it will probably be updated badly
\r
156 MPI_Offset position = fh->file_->tell();
\r
157 MPI_Offset movesize = datatype->get_extent()*count;
\r
158 MPI_Offset writesize = datatype->size()*count;
\r
159 XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
\r
160 MPI_Offset write = fh->file_->write(writesize);
\r
161 XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
\r
162 if(writesize!=movesize){
\r
163 fh->file_->seek(position+movesize, SEEK_SET);
\r
165 XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
\r
166 status->count=count*datatype->size();
\r
167 return MPI_SUCCESS;
\r
170 int File::write_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
172 fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);
\r
173 write(fh, buf, count, datatype, status);
\r
174 fh->shared_file_pointer_=fh->file_->tell();
\r
176 return MPI_SUCCESS;
\r
179 int File::write_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
180 //0 needs to get the shared pointer value
\r
181 if(fh->comm_->rank()==0){
\r
185 fh->shared_file_pointer_=count*datatype->size();
\r
188 simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
\r
189 fh->seek(result, MPI_SEEK_SET);
\r
190 int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
\r
191 if(fh->comm_->rank()==fh->comm_->size()-1){
\r
196 simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
\r
201 return file_->size();
\r
210 return simgrid::smpi::Colls::barrier(comm_);
\r
215 int rank = comm_->rank();
\r
216 int size = comm_->size();
\r
217 char waitlist[size];
\r
221 win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);
\r
222 win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);
\r
223 win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);
\r
224 win_->get(&shared_file_pointer_ , 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);
\r
226 for (i = 0; i < size; i++) {
\r
227 if (waitlist[i] == 1 && i != rank) {
\r
228 // wait for the lock
\r
229 MPI_Recv(&lock, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, comm_, MPI_STATUS_IGNORE);
\r
238 int rank = comm_->rank();
\r
239 int size = comm_->size();
\r
240 char waitlist[size];
\r
244 win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);
\r
245 win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);
\r
246 win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);
\r
247 shared_file_pointer_=file_->tell();
\r
248 win_->put(&shared_file_pointer_, 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);
\r
251 next = (rank + 1 + size) % size;
\r
252 for (i = 0; i < size; i++, next = (next + 1) % size) {
\r
253 if (waitlist[next] == 1) {
\r
254 MPI_Send(&lock, 1, MPI_CHAR, next, tag, comm_);
\r
261 MPI_Info File::info(){
\r
262 if(info_== MPI_INFO_NULL)
\r
263 info_ = new Info();
\r
268 void File::set_info(MPI_Info info){
\r
269 if(info_!= MPI_INFO_NULL)
\r