Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement MPI_Type_get_envelope and MPI_Type_get_contents
[simgrid.git] / src / smpi / include / smpi_file.hpp
1 /* Copyright (c) 2010-2020. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SMPI_FILE_HPP_INCLUDED
8 #define SMPI_FILE_HPP_INCLUDED
9 #include "simgrid/plugins/file_system.h"
10 #include "smpi_comm.hpp"
11 #include "smpi_coll.hpp"
12 #include "smpi_datatype.hpp"
13 #include "smpi_errhandler.hpp"
14 #include "smpi_info.hpp"
15 #include  <algorithm>
16
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_pmpi);
18
19 namespace simgrid{
20 namespace smpi{
21 class File : public F2C{
22   MPI_Comm comm_;
23   int flags_;
24   simgrid::s4u::File* file_;
25   MPI_Info info_;
26   MPI_Offset* shared_file_pointer_;
27   s4u::MutexPtr shared_mutex_;
28   MPI_Win win_;
29   char* list_;
30   MPI_Errhandler errhandler_;
31   MPI_Datatype etype_;
32   MPI_Datatype filetype_;
33   std::string datarep_;
34
35   public:
36   File(MPI_Comm comm, const char *filename, int amode, MPI_Info info);
37   File(const File&) = delete;
38   File& operator=(const File&) = delete;
39   ~File();
40   int size();
41   int get_position(MPI_Offset* offset);
42   int get_position_shared(MPI_Offset* offset);
43   int flags();
44   MPI_Comm comm();
45   int sync();
46   int seek(MPI_Offset offset, int whence);
47   int seek_shared(MPI_Offset offset, int whence);
48   int set_view(MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info* info);
49   int get_view(MPI_Offset *disp, MPI_Datatype *etype, MPI_Datatype *filetype, char *datarep);
50   MPI_Info info();
51   void set_info( MPI_Info info);
52   static int read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
53   static int read_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
54   static int read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
55   static int write(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
56   static int write_shared(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
57   static int write_ordered(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
58   template <int (*T)(MPI_File, void *, int, MPI_Datatype, MPI_Status *)> int op_all(void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
59   static int close(MPI_File *fh);
60   static int del(const char* filename, const Info* info);
61   MPI_Errhandler errhandler();
62   void set_errhandler( MPI_Errhandler errhandler);
63   static File* f2c(int id);
64 };
65
66   /* Read_all, Write_all : loosely based on */
67   /* @article{Thakur:1996:ETM:245875.245879,*/
68   /* author = {Thakur, Rajeev and Choudhary, Alok},*/
69   /* title = {An Extended Two-phase Method for Accessing Sections of Out-of-core Arrays},*/
70   /* journal = {Sci. Program.},*/
71   /* issue_date = {Winter 1996},*/
72   /* pages = {301--317},*/
73   /* }*/ 
74   template <int (*T)(MPI_File, void *, int, MPI_Datatype, MPI_Status *)>
75   int File::op_all(void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
76     //get min and max offsets from everyone.
77     int size =  comm_->size();
78     int rank = comm_-> rank();
79     MPI_Offset min_offset = file_->tell();
80     MPI_Offset max_offset = min_offset + count * datatype->get_extent();//cheating, as we don't care about exact data location, we can skip extent
81     MPI_Offset* min_offsets = new MPI_Offset[size];
82     MPI_Offset* max_offsets = new MPI_Offset[size];
83     simgrid::smpi::colls::allgather(&min_offset, 1, MPI_OFFSET, min_offsets, 1, MPI_OFFSET, comm_);
84     simgrid::smpi::colls::allgather(&max_offset, 1, MPI_OFFSET, max_offsets, 1, MPI_OFFSET, comm_);
85     MPI_Offset min=min_offset;
86     MPI_Offset max=max_offset;
87     MPI_Offset tot= 0;
88     int empty=1;
89     for(int i=0;i<size;i++){
90       if(min_offsets[i]!=max_offsets[i])
91         empty=0;
92       tot+=(max_offsets[i]-min_offsets[i]);
93       if(min_offsets[i]<min)
94         min=min_offsets[i];
95       if(max_offsets[i]>max)
96         max=max_offsets[i];
97     }
98     
99     XBT_CDEBUG(smpi_pmpi, "my offsets to read : %lld:%lld, global min and max %lld:%lld", min_offset, max_offset, min, max);
100     if(empty==1){
101       delete[] min_offsets;
102       delete[] max_offsets;
103       status->count=0;
104       return MPI_SUCCESS;
105     }
106     MPI_Offset total = max-min;
107     if(total==tot && (datatype->flags() & DT_FLAG_CONTIGUOUS)){
108       delete[] min_offsets;
109       delete[] max_offsets;
110       //contiguous. Just have each proc perform its read
111       if(status != MPI_STATUS_IGNORE)
112         status->count=count * datatype->size();
113       return T(this,buf,count,datatype, status);
114     }
115
116     //Interleaved case : How much do I need to read, and whom to send it ?
117     MPI_Offset my_chunk_start=(max-min+1)/size*rank;
118     MPI_Offset my_chunk_end=((max-min+1)/size*(rank+1));
119     XBT_CDEBUG(smpi_pmpi, "my chunks to read : %lld:%lld", my_chunk_start, my_chunk_end);
120     int* send_sizes = new int[size];
121     int* recv_sizes = new int[size];
122     int* send_disps = new int[size];
123     int* recv_disps = new int[size];
124     int total_sent=0;
125     for(int i=0;i<size;i++){
126       send_sizes[i]=0;
127       send_disps[i]=0;//cheat to avoid issues when send>recv as we use recv buffer
128       if((my_chunk_start>=min_offsets[i] && my_chunk_start < max_offsets[i])||
129           ((my_chunk_end<=max_offsets[i]) && my_chunk_end> min_offsets[i])){
130         send_sizes[i]=(std::min(max_offsets[i]-1, my_chunk_end-1)-std::max(min_offsets[i], my_chunk_start));
131         // store min and max offset to actually read
132         min_offset=std::min(min_offset, min_offsets[i]);
133         total_sent+=send_sizes[i];
134         XBT_CDEBUG(smpi_pmpi, "will have to send %d bytes to %d", send_sizes[i], i);
135       }
136     }
137     min_offset=std::max(min_offset, my_chunk_start);
138
139     //merge the ranges of every process
140     std::vector<std::pair<MPI_Offset, MPI_Offset>> ranges;
141     for(int i=0; i<size; ++i)
142       ranges.push_back(std::make_pair(min_offsets[i],max_offsets[i]));
143     std::sort(ranges.begin(), ranges.end());
144     std::vector<std::pair<MPI_Offset, MPI_Offset>> chunks;
145     chunks.push_back(ranges[0]);
146
147     unsigned int nchunks=0;
148     unsigned int i=1;
149     while(i < ranges.size()){
150       if(ranges[i].second>chunks[nchunks].second){
151         // else range included - ignore
152         if(ranges[i].first>chunks[nchunks].second){
153           //new disjoint range
154           chunks.push_back(ranges[i]);
155           nchunks++;
156         } else {
157           //merge ranges
158           chunks[nchunks].second=ranges[i].second;
159         }
160       }
161       i++;
162     }
163     //what do I need to read ?
164     MPI_Offset totreads=0;
165     for(i=0; i<chunks.size();i++){
166       if(chunks[i].second < my_chunk_start)
167         continue;
168       else if (chunks[i].first > my_chunk_end)
169         continue;
170       else
171         totreads += (std::min(chunks[i].second, my_chunk_end-1)-std::max(chunks[i].first, my_chunk_start));
172     }
173     XBT_CDEBUG(smpi_pmpi, "will have to access %lld from my chunk", totreads);
174
175     unsigned char* sendbuf = smpi_get_tmp_sendbuffer(total_sent);
176
177     if(totreads>0){
178       seek(min_offset, MPI_SEEK_SET);
179       T(this,sendbuf,totreads/datatype->size(),datatype, status);
180     }
181     simgrid::smpi::colls::alltoall(send_sizes, 1, MPI_INT, recv_sizes, 1, MPI_INT, comm_);
182     int total_recv=0;
183     for(int i=0;i<size;i++){
184       recv_disps[i]=total_recv;
185       total_recv+=recv_sizes[i];
186     }
187     //Set buf value to avoid copying dumb data
188     simgrid::smpi::colls::alltoallv(sendbuf, send_sizes, send_disps, MPI_BYTE, buf, recv_sizes, recv_disps, MPI_BYTE,
189                                     comm_);
190     if(status!=MPI_STATUS_IGNORE)
191       status->count=count * datatype->size();
192     smpi_free_tmp_buffer(sendbuf);
193     delete[] send_sizes;
194     delete[] recv_sizes;
195     delete[] send_disps;
196     delete[] recv_disps;
197     delete[] min_offsets;
198     delete[] max_offsets;
199     return MPI_SUCCESS;
200   }
201 }
202 }
203
204 #endif