Logo AND Algorithmique Numérique Distribuée

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