Logo AND Algorithmique Numérique Distribuée

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