/[escript]/trunk/escriptcore/src/MPIDataReducer.cpp
ViewVC logotype

Contents of /trunk/escriptcore/src/MPIDataReducer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5637 - (show annotations)
Wed Jun 10 03:34:10 2015 UTC (4 years, 1 month ago) by jfenwick
File size: 9162 byte(s)
further
1 /*****************************************************************************
2 *
3 * Copyright (c) 2014-2015 by The University of Queensland
4 * http://www.uq.edu.au
5 *
6 * Primary Business: Queensland, Australia
7 * Licensed under the Open Software License version 3.0
8 * http://www.opensource.org/licenses/osl-3.0.php
9 *
10 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 * Development 2012-2013 by School of Earth Sciences
12 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 *
14 *****************************************************************************/
15
16 #define ESNEEDPYTHON
17 #include "esysUtils/first.h"
18
19
20 #include <sstream>
21 #include <limits>
22 #include <boost/python/extract.hpp>
23 #include <boost/scoped_array.hpp>
24
25 #include "MPIDataReducer.h"
26 #include "SplitWorldException.h"
27
28 using namespace boost::python;
29 using namespace escript;
30
31
32 namespace escript
33 {
34 Reducer_ptr makeDataReducer(std::string type)
35 {
36 MPI_Op op;
37 if (type=="SUM")
38 {
39 op=MPI_SUM;
40 }
41 else
42 {
43 throw SplitWorldException("Unsupported operation for makeDataReducer.");
44 }
45 MPIDataReducer* m=new MPIDataReducer(op);
46 return Reducer_ptr(m);
47 }
48
49 }
50
51 namespace
52 {
53
54 void combineData(Data& d1, const Data& d2, MPI_Op op)
55 {
56 if (op==MPI_SUM)
57 {
58 d1+=d2;
59 }
60 }
61
62 }
63
64 MPIDataReducer::MPIDataReducer(MPI_Op op)
65 : reduceop(op)
66 {
67 valueadded=false;
68 if (op==MPI_SUM)
69 {
70 // deliberately left blank
71 }
72 else
73 {
74 throw SplitWorldException("Unsupported MPI_Op");
75 }
76 }
77
78
79 void MPIDataReducer::setDomain(escript::Domain_ptr d)
80 {
81 dom=d;
82 }
83
84 std::string MPIDataReducer::description()
85 {
86 std::string op="SUM";
87 return "Reducer("+op+") for Data objects";
88 }
89
90 bool MPIDataReducer::valueCompatible(boost::python::object v)
91 {
92 extract<Data&> ex(v);
93 if (!ex.check())
94 {
95 return false;
96 }
97 if (dom.get()!=0)
98 {
99 const Data& d=ex();
100 if (d.getDomain().get()!=dom.get())
101 {
102 return false; // the domains don't match
103 }
104 }
105 return true;
106 }
107
108
109 bool MPIDataReducer::reduceLocalValue(boost::python::object v, std::string& errstring)
110 {
111 extract<Data&> ex(v);
112 if (!ex.check())
113 {
114 errstring="reduceLocalValue: expected Data object. Got something else.";
115 return false;
116 }
117 Data& d=ex();
118 if ((d.getDomain()!=dom) && (dom.get()!=0))
119 {
120 errstring="reduceLocalValue: Got a Data object, but it was not using the SubWorld's domain.";
121 return false;
122 }
123 d.expand(); // because I don't want to mess about with types of Data
124 if (!valueadded) // first value so answer becomes this one
125 {
126 value=d;
127 dom=d.getDomain();
128 }
129 else
130 {
131 if (d.getFunctionSpace()!=value.getFunctionSpace())
132 {
133 errstring="reduceLocalValue: FunctionSpaces for Data objects being combined must match.";
134 return false;
135 }
136 combineData(value, d, reduceop);
137 }
138 return true;
139 }
140
141 void MPIDataReducer::reset()
142 {
143 valueadded=false;
144 value=Data();
145 }
146
147 bool MPIDataReducer::checkRemoteCompatibility(esysUtils::JMPI& mpi_info, std::string& errstring)
148 {
149 #ifdef ESYS_MPI
150 // since they can't add it unless it is using the proper domain, we need to check
151
152 std::vector<unsigned> compat(6);
153 getCompatibilityInfo(compat);
154
155 // still need to incorporate domain version into this
156 // or are domains not mutable in any way that matters?
157 int* rbuff=new int[mpi_info->size*compat.size()];
158 boost::scoped_array<int> dummy(rbuff); // to ensure cleanup
159 for (int i=0;i<mpi_info->size;++i)
160 {
161 rbuff[i]=0; // since this won't match any valid value we can use it as a failure check
162 }
163 if (MPI_Allgather(&compat[0], compat.size(), MPI_UNSIGNED, rbuff,
164 compat.size(), MPI_UNSIGNED, mpi_info->comm)!=MPI_SUCCESS)
165 {
166 errstring="MPI failure in checkRemoteCompatibility.";
167 return false;
168 }
169 for (int i=0;i<mpi_info->size-1;++i)
170 {
171 for (int j=0;j<compat.size();++i)
172 {
173 if (rbuff[i*compat.size()+j]!=rbuff[(i+1)*compat.size()+j])
174 {
175 std::ostringstream oss;
176 oss << "Incompatible value found for SubWorld " << i+1 << '.';
177 errstring=oss.str();
178 return false;
179 }
180 }
181 }
182 return true;
183 #else
184 return true;
185 #endif
186 }
187
188 // By the time this function is called, we know that all the values
189 // are compatible
190 bool MPIDataReducer::reduceRemoteValues(esysUtils::JMPI& mpi_info, bool active)
191 {
192 if (!active)
193 {
194 return false; // shutting down this option until I implement it
195 }
196 #ifdef ESYS_MPI
197 DataTypes::ValueType& vr=value.getExpandedVectorReference();
198 Data result(0, value.getDataPointShape(), value.getFunctionSpace(), true);
199 DataTypes::ValueType& rr=value.getExpandedVectorReference();
200 if (MPI_Allreduce(&(vr[0]), &(rr[0]), vr.size(), MPI_DOUBLE, reduceop, mpi_info->comm)!=MPI_SUCCESS)
201 {
202 return false;
203 }
204 return true;
205 #else
206 return true;
207 #endif
208 }
209
210 // populate a vector of ints with enough information to ensure two values are compatible
211 // or to construct a container for incomming data
212 // Format for this:
213 // [0] Type of Data: {0 : error, 1: DataEmpty, 10: constant, 11:tagged, 12:expanded}
214 // [1] Functionspace type code
215 // [2] Only used for tagged --- gives the number of tags (which exist in the data object)
216 // [3..6] Components of the shape
217 void MPIDataReducer::getCompatibilityInfo(std::vector<unsigned>& params)
218 {
219 params.resize(7);
220 for (int i=0;i<7;++i)
221 {
222 params[0]=0;
223 }
224 if (value.isConstant())
225 {
226 params[0]=10;
227 }
228 else if (value.isTagged())
229 {
230 params[0]=11;
231 }
232 else if (value.isExpanded())
233 {
234 params[0]=12;
235 }
236 else // This could be DataEmpty or some other weirdness but we won't allow that
237 {
238 params[0]=0; // invalid type to send
239 }
240 params[1]=value.getFunctionSpace().getTypeCode();
241 params[2]=static_cast<unsigned>(value.getNumberOfTaggedValues());
242 const DataTypes::ShapeType& s=value.getDataPointShape();
243 for (int i=0;i<s.size();++i)
244 {
245 params[3+i]=s[i];
246 }
247 }
248
249
250 // Get a value for this variable from another process
251 // This is not a reduction and will replace any existing value
252 bool MPIDataReducer::recvFrom(Esys_MPI_rank localid, Esys_MPI_rank source, esysUtils::JMPI& mpiinfo)
253 {
254 #ifdef ESYS_MPI
255 // first we need to find out what we are expecting
256 unsigned params[7];
257 MPI_Status stat;
258 if (MPI_Recv(params, 7, MPI_UNSIGNED, source, PARAMTAG, mpiinfo->comm, &stat)!=MPI_SUCCESS)
259 {
260 return false;
261 }
262 if (params[0]<10) // the sender somehow tried to send something invalid
263 {
264 return false;
265 }
266 // now we put the shape object together
267 escript::DataTypes::ShapeType s;
268 for (int i=0;i<4;++i)
269 {
270 if (params[3+i]>0)
271 {
272 s.push_back(params[3+i]);
273 }
274 else
275 {
276 break;
277 }
278 }
279 // Now we need the FunctionSpace
280 FunctionSpace fs=FunctionSpace(dom, static_cast<int>(params[1]));
281 value=Data(0, s, fs, params[0]==12);
282 if (params[0]==11) // The Data is tagged so we need to work out what tags we need
283 {
284 // TODO: Need to ship the tags and names over but for now just make sure there
285 // are the same number of tags
286 value.tag();
287
288 DataVector dv(DataTypes::noValues(s), 0, 1);
289 for (unsigned i=0;i<params[2];++i)
290 {
291 value.setTaggedValueFromCPP(static_cast<int>(i)+1, s, dv, 0);
292 }
293 return false; // because I don't trust this yet
294 }
295 #endif
296 return true;
297 }
298
299 // Send a value to this variable to another process
300 // This is not a reduction and will replace any existing value
301 bool MPIDataReducer::sendTo(Esys_MPI_rank localid, Esys_MPI_rank target, esysUtils::JMPI& mpiinfo)
302 {
303 #ifdef ESYS_MPI
304 // first step is to let the other world know what sort of thing it needs to make
305 if (value.isLazy())
306 {
307 value.resolve();
308 }
309 std::vector<unsigned> params;
310 getCompatibilityInfo(params);
311 if (MPI_Send(&params[0], 6, MPI_UNSIGNED, target, PARAMTAG, mpiinfo->comm)!=MPI_SUCCESS)
312 {
313 return false;
314 }
315 // now we have informed the other end of what happened
316 // are we done or is there actually data to send
317 if (params[0]<10)
318 {
319 return false;
320 }
321 // at this point, we know there is data to send
322 const DataAbstract::ValueType::value_type* vect=value.getDataRO();
323 // now the receiver knows how much data it should be receive
324 // need to make sure that we aren't trying to send data with no local samples
325 if (vect!=0)
326 {
327 // MPI v3 has this first param as a const void* (as it should be)
328 // Version on my machine expects void*
329 if (MPI_Send(const_cast<DataAbstract::ValueType::value_type*>(vect), value.getLength(), MPI_DOUBLE, target, PARAMTAG, mpiinfo->comm)!=MPI_SUCCESS)
330 {
331 return false;
332 }
333 }
334 #endif
335 return true;
336 }
337
338 boost::python::object MPIDataReducer::getPyObj()
339 {
340 throw SplitWorldException("getPyObj Not implemented yet.");
341 }
342
343
344 // send from proc 0 in the communicator to all others
345 bool MPIDataReducer::groupSend(MPI_Comm& com)
346 {
347 throw SplitWorldException("groupSend Not implemented yet.");
348 }
349
350 bool MPIDataReducer::groupReduce(MPI_Comm& com, char mystate)
351 {
352 throw SplitWorldException("groupReduce Not implemented yet.");
353 }
354

  ViewVC Help
Powered by ViewVC 1.1.26