Logo AND Algorithmique Numérique Distribuée

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