1 /* Copyright (c) 2010-2019. The SimGrid Team.
\r
2 * All rights reserved. */
\r
4 /* This program is free software; you can redistribute it and/or modify it
\r
5 * under the terms of the license (GNU LGPL) which comes with this package. */
\r
7 #ifndef SMPI_FILE_HPP_INCLUDED
\r
8 #define SMPI_FILE_HPP_INCLUDED
\r
9 #include "simgrid/plugins/file_system.h"
\r
10 #include "smpi_comm.hpp"
\r
11 #include "smpi_coll.hpp"
\r
12 #include "smpi_datatype.hpp"
\r
13 #include "smpi_info.hpp"
\r
14 #include <algorithm>
\r
22 simgrid::s4u::File* file_;
\r
24 MPI_Offset shared_file_pointer_;
\r
26 File(MPI_Comm comm, char *filename, int amode, MPI_Info info);
\r
29 int get_position(MPI_Offset* offset);
\r
32 int seek(MPI_Offset offset, int whence);
\r
34 void set_info( MPI_Info info);
\r
35 static int read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
\r
36 static int write(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
\r
37 template <int (*T)(MPI_File, void *, int, MPI_Datatype, MPI_Status *)> int op_all(void *buf, int count,MPI_Datatype datatype, MPI_Status *status);
\r
38 static int close(MPI_File *fh);
\r
39 static int del(char *filename, MPI_Info info);
\r
42 template <int (*T)(MPI_File, void *, int, MPI_Datatype, MPI_Status *)>
\r
43 int File::op_all(void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
44 //get min and max offsets from everyone.
\r
45 int size = comm_->size();
\r
46 int rank = comm_-> rank();
\r
47 MPI_Offset min_offset = file_->tell();
\r
48 MPI_Offset max_offset = min_offset + count * datatype->size();//cheating, as we don't care about exact data location, we can skip extent
\r
49 MPI_Offset* min_offsets = xbt_new(MPI_Offset, size);
\r
50 MPI_Offset* max_offsets = xbt_new(MPI_Offset, size);
\r
51 simgrid::smpi::Colls::allgather(&min_offset, 1, MPI_OFFSET, min_offsets, 1, MPI_OFFSET, comm_);
\r
52 simgrid::smpi::Colls::allgather(&max_offset, 1, MPI_OFFSET, max_offsets, 1, MPI_OFFSET, comm_);
\r
53 MPI_Offset min=min_offset;
\r
54 MPI_Offset max=max_offset;
\r
56 for(int i=0;i<size;i++){
\r
57 tot+=(max_offsets[i]-min_offsets[i]);
\r
58 if(min_offsets[i]<min)
\r
60 if(max_offsets[i]>max)
\r
63 MPI_Offset total = max-min;
\r
64 if(total==tot && (datatype->flags() & DT_FLAG_CONTIGUOUS)){
\r
65 //contiguous. Just have each proc perform its read
\r
66 return T(this,buf,count,datatype, status);
\r
69 //Interleaved case : How much do I need to read, and whom to send it ?
\r
70 MPI_Offset my_chunk_start=(max-min)/size*rank;
\r
71 MPI_Offset my_chunk_end=((max-min)/size*(rank+1))-1;
\r
72 int* send_sizes = xbt_new0(int, size);
\r
73 int* recv_sizes = xbt_new(int, size);
\r
74 int* send_disps = xbt_new(int, size);
\r
75 int* recv_disps = xbt_new(int, size);
\r
77 for(int i=0;i<size;i++){
\r
78 if((my_chunk_start>=min_offsets[i] && my_chunk_start < max_offsets[i])||
\r
79 ((my_chunk_end<=max_offsets[i]) && my_chunk_end> min_offsets[i])){
\r
80 send_sizes[i]=(std::min(max_offsets[i], my_chunk_end+1)-std::max(min_offsets[i], my_chunk_start));
\r
81 //store min and max offest to actually read
\r
82 min_offset=std::max(min_offsets[i], my_chunk_start);
\r
83 max_offset=std::min(max_offsets[i], my_chunk_end+1);
\r
84 send_disps[i]=0;//send_sizes[i]; cheat to avoid issues when send>recv as we use recv buffer
\r
85 total_sent+=send_sizes[i];
\r
89 //merge the ranges of every process
\r
90 std::vector<std::pair<MPI_Offset, MPI_Offset>> ranges;
\r
91 for(int i=0; i<size; ++i)
\r
92 ranges.push_back(std::make_pair(min_offsets[i],max_offsets[i]));
\r
93 std::sort(ranges.begin(), ranges.end());
\r
94 std::vector<std::pair<MPI_Offset, MPI_Offset>> chunks;
\r
95 chunks.push_back(ranges[0]);
\r
97 unsigned int nchunks=0;
\r
99 while(i < ranges.size()){
\r
100 if(ranges[i].second>chunks[nchunks].second){
\r
101 // else range included - ignore
\r
102 if(ranges[i].first>chunks[nchunks].second){
\r
103 //new disjoint range
\r
104 chunks.push_back(ranges[i]);
\r
108 chunks[nchunks].second=ranges[i].second;
\r
113 //what do I need to read ?
\r
114 MPI_Offset totreads=0;
\r
115 for(i=0; i<chunks.size();i++){
\r
116 if(chunks[i].second < my_chunk_start)
\r
118 else if (chunks[i].first > my_chunk_end)
\r
121 totreads += (std::min(chunks[i].second, my_chunk_end+1)-std::max(chunks[i].first, my_chunk_start));
\r
123 char* sendbuf= static_cast<char *>(smpi_get_tmp_sendbuffer(totreads));
\r
126 seek(min_offset, MPI_SEEK_SET);
\r
127 T(this,sendbuf,totreads/datatype->size(),datatype, status);
\r
129 simgrid::smpi::Colls::alltoall(send_sizes, 1, MPI_INT, recv_sizes, 1, MPI_INT, comm_);
\r
131 for(int i=0;i<size;i++){
\r
132 recv_disps[i]=total_recv;
\r
133 total_recv+=recv_sizes[i];
\r
135 //Set buf value to avoid copying dumb data
\r
136 simgrid::smpi::Colls::alltoallv(sendbuf, send_sizes, send_disps, MPI_BYTE,
\r
137 buf, recv_sizes, recv_disps, MPI_BYTE, comm_);
\r
138 smpi_free_tmp_buffer(sendbuf);
\r
139 xbt_free(send_sizes);
\r
140 xbt_free(recv_sizes);
\r
141 xbt_free(send_disps);
\r
142 xbt_free(recv_disps);
\r
143 xbt_free(min_offsets);
\r
144 xbt_free(max_offsets);
\r
145 return MPI_SUCCESS;
\r