/[escript]/branches/split/escriptcore/src/SplitWorld.cpp
ViewVC logotype

Annotation of /branches/split/escriptcore/src/SplitWorld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4796 - (hide annotations)
Mon Mar 24 04:57:35 2014 UTC (5 years, 5 months ago) by jfenwick
File size: 9752 byte(s)
Some work towards merge functions
1 jfenwick 4730 /*****************************************************************************
2     *
3     * Copyright (c) 2014 by 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     #include "esysUtils/Esys_MPI.h"
17 jfenwick 4734 #include "SplitWorld.h"
18 jfenwick 4730 #include "AbstractDomain.h"
19 jfenwick 4731 #include "SplitWorldException.h"
20 jfenwick 4734 #include "SplitWorldException.h"
21 jfenwick 4730
22 jfenwick 4731 #include <iostream>
23 jfenwick 4796 #include <sstream>
24 jfenwick 4731
25 jfenwick 4730 using namespace boost::python;
26     using namespace escript;
27    
28 jfenwick 4734 SplitWorld::SplitWorld(unsigned int numgroups, MPI_Comm global)
29 jfenwick 4747 :localworld((SubWorld*)0), swcount(numgroups>0?numgroups:1), jobcounter(1)
30 jfenwick 4730 {
31 jfenwick 4747 globalcom=esysUtils::makeInfo(global);
32    
33 jfenwick 4746 int grank=0;
34     int wsize=1; // each world has this many processes
35     #ifdef ESYS_MPI
36 jfenwick 4747 int gsize=globalcom->size;
37     grank=globalcom->rank;
38 jfenwick 4746 if (gsize%swcount!=0)
39     {
40     throw SplitWorldException("SplitWorld error: requested number of groups is not a factor of global communicator size.");
41     }
42     wsize=gsize/swcount; // each world has this many processes
43 jfenwick 4747 MPI_Comm sub;
44     int res=MPI_Comm_split(MPI_COMM_WORLD, grank/wsize, grank%wsize, &sub);
45 jfenwick 4746 if (res!=MPI_SUCCESS)
46     {
47     throw SplitWorldException("SplitWorld error: Unable to form communicator.");
48     }
49 jfenwick 4747 subcom=esysUtils::makeInfo(sub,true);
50     #else
51     subcom=esysUtils::makeInfo(0);
52 jfenwick 4746 #endif
53 jfenwick 4730 localworld=SubWorld_ptr(new SubWorld(subcom));
54 jfenwick 4731 localid=grank/wsize;
55 jfenwick 4730 }
56    
57 jfenwick 4734 SplitWorld::~SplitWorld()
58 jfenwick 4730 {
59 jfenwick 4747 // communicator cleanup handled by the MPI_Info
60 jfenwick 4730 }
61    
62    
63     // The boost wrapper will ensure that there is at least one entry in the tuple
64 jfenwick 4734 object SplitWorld::buildDomains(tuple t, dict kwargs)
65 jfenwick 4730 {
66     int tsize=len(t);
67     // get the callable that we will invoke in a sec
68     object tocall=t[0];
69     // make a new tuple without the first element
70     tuple ntup=tuple(t.slice(1,tsize));
71     // now add the subworld to the kwargs
72     kwargs["escriptworld"]=localworld;
73    
74     // pass the whole package to the python call
75     object dobj=tocall(*ntup, **kwargs);
76     extract<Domain_ptr> ex1(dobj);
77     Domain_ptr dptr=ex1();
78    
79     // now do a sanity check to see if the domain has respected the communicator info we passed it.
80 jfenwick 4747 if (dptr->getMPIComm()!=localworld->getMPI()->comm)
81 jfenwick 4730 {
82 jfenwick 4734 throw SplitWorldException("The newly constructed domain is not using the correct communicator.");
83 jfenwick 4730 }
84     localworld->setDomain(dptr);
85     return object(); // return None
86     }
87    
88 jfenwick 4745
89 jfenwick 4773 namespace
90     {
91    
92     // Throw all values in and get the maximum
93     // This is not an AllReduce because we need to use Tagged Communication so as not to interfere with
94     // other messages / collective ops which the SubWorld's Jobs might be using
95     // This is implemented by sending to rank 0
96     bool checkResultInt(int res, int& mres, esysUtils::JMPI& info)
97     {
98     const int leader=0;
99     const int BIGTAG=esysUtils::getSubWorldTag();
100     if (info->size==1)
101     {
102     mres=res;
103     return true;
104     }
105     else
106     {
107     if (info->rank!=leader)
108     {
109     if (MPI_Send(&res, 1, MPI_INT, leader, BIGTAG, info->comm)!=MPI_SUCCESS)
110     {
111     return false;
112     }
113     if (MPI_Recv(&mres, 1, MPI_INT, leader, BIGTAG, info->comm,0)!=MPI_SUCCESS)
114     {
115     return false;
116     }
117     }
118     else
119     {
120     MPI_Request* reqs=new MPI_Request[info->size-1];
121     int* eres=new int[info->size-1];
122     for (int i=0;i<info->size-1;++i)
123     {
124     MPI_Irecv(eres+i, 1, MPI_INT, i+1, BIGTAG, info->comm, reqs+i);
125     }
126     if (MPI_Waitall(info->size-1, reqs, 0)!=MPI_SUCCESS)
127     {
128     delete[] reqs;
129     return false;
130     }
131     // now we have them all, find the max
132     mres=res;
133     for (int i=0;i<info->size-1;++i)
134     {
135     if (mres<eres[i])
136     {
137     mres=eres[i];
138     }
139     }
140     // now we know what the result should be
141     // send it to the others
142     for (int i=0;i<info->size-1;++i)
143     {
144     MPI_Isend(&mres, 1, MPI_INT, i+1, BIGTAG, info->comm, reqs+i);
145     }
146     if (MPI_Waitall(info->size-1, reqs,0)!=MPI_SUCCESS)
147     {
148     return false;
149     }
150     }
151    
152     }
153     return true;
154     }
155    
156    
157     }
158    
159 jfenwick 4745 // Executes all pending jobs on all subworlds
160     void SplitWorld::runJobs()
161 jfenwick 4731 {
162 jfenwick 4745 distributeJobs();
163 jfenwick 4734 int mres=0;
164 jfenwick 4746 std::string err;
165 jfenwick 4734 do
166     {
167     // now we actually need to run the jobs
168     // everybody will be executing their localworld's jobs
169 jfenwick 4746 int res=localworld->runJobs(err);
170 jfenwick 4734 // now we find out about the other worlds
171 jfenwick 4773 if (!checkResultInt(res, mres, globalcom))
172 jfenwick 4734 {
173     throw SplitWorldException("MPI appears to have failed.");
174     }
175     } while (mres==1);
176     if (mres==2)
177     {
178     throw SplitWorldException("At least one Job's work() function did not return True/False.");
179     }
180     else if (mres==3)
181     {
182 jfenwick 4746 char* resultstr=0;
183 jfenwick 4773 // now we ship around the error message - This should be safe since
184     // eveyone must have finished their Jobs to get here
185 jfenwick 4747 if (!esysUtils::shipString(err.c_str(), &resultstr, globalcom->comm))
186 jfenwick 4746 {
187     throw SplitWorldException("MPI appears to have failed.");
188     }
189     //throw SplitWorldException("At least one Job's work() function raised an exception.");
190     std::string s("At least one Job's work() function raised the following exception:\n");
191     s+=resultstr;
192     throw SplitWorldException(s);
193 jfenwick 4734 }
194 jfenwick 4731 }
195    
196 jfenwick 4745 /**
197     stores the constructor/factory to make Jobs and the parameters.
198     */
199     void SplitWorld::addJob(boost::python::object creator, boost::python::tuple tup, boost::python::dict kw)
200     {
201     create.push_back(creator);
202     tupargs.push_back(tup);
203     kwargs.push_back(kw);
204     }
205    
206 jfenwick 4796 // At some point, we may need there to be more isolation here
207     // and trap any python exceptions etc, but for now I'll just call the constructor
208     void SplitWorld::addVariable(std::string name, boost::python::object creator, boost::python::tuple ntup, boost::python::dict kwargs)
209     {
210     if (varmap.find(name)!=varmap.end())
211     {
212     std::ostringstream oss;
213     oss << "There is already a variable called " << name;
214     throw SplitWorldException(oss.str());
215     }
216     object red=creator(*ntup, **kwargs);
217     extract<Reducer_ptr> ex(red);
218     if (!ex.check())
219     {
220     throw SplitWorldException("Creator function did not produce a reducer.");
221     }
222     Reducer_ptr rp=ex();
223     varmap[name]=rp;
224     }
225    
226    
227     void SplitWorld::removeVariable(std::string name)
228     {
229     varmap.erase(name);
230     }
231    
232 jfenwick 4745 void SplitWorld::clearPendingJobs()
233     {
234     create.clear();
235     tupargs.clear();
236     kwargs.clear();
237     }
238    
239     void SplitWorld::clearActiveJobs()
240     {
241     localworld->clearJobs();
242     }
243    
244     // All the job params are known on all the ranks.
245     void SplitWorld::distributeJobs()
246     {
247     unsigned int numjobs=create.size()/swcount;
248     unsigned int start=create.size()/swcount*localid;
249     if (localid<create.size()%swcount)
250     {
251     numjobs++;
252     start+=localid;
253     }
254     else
255     {
256     start+=create.size()%swcount;
257     }
258 jfenwick 4746 int errstat=0;
259 jfenwick 4745 try
260     {
261     // No other subworld will be looking at this portion of the array
262     // so jobs will only be created on one subworld
263     for (unsigned int i=start;i<start+numjobs;++i)
264     {
265     // we need to add some things to the kw map
266     kwargs[i]["domain"]=localworld->getDomain();
267     kwargs[i]["jobid"]=object(jobcounter+i);
268     object job=create[i](*(tupargs[i]), **(kwargs[i]));
269     localworld->addJob(job);
270     }
271     }
272     catch (boost::python::error_already_set e)
273     {
274     errstat=1;
275     }
276     jobcounter+=create.size();
277     clearPendingJobs();
278    
279     // MPI check to ensure that it worked for everybody
280 jfenwick 4746 int mstat=0;
281 jfenwick 4747 if (!esysUtils::checkResult(errstat, mstat, globalcom->comm))
282 jfenwick 4745 {
283     throw SplitWorldException("MPI appears to have failed.");
284     }
285    
286     if (errstat==1)
287     {
288     throw SplitWorldException("distributeJobs: Job creation failed.");
289     clearActiveJobs();
290     }
291     }
292    
293    
294 jfenwick 4730 namespace escript
295     {
296    
297     boost::python::object raw_buildDomains(boost::python::tuple t, boost::python::dict kwargs)
298     {
299     int l=len(t);
300     if (l<2)
301     {
302 jfenwick 4734 throw SplitWorldException("Insufficient parameters to buildDomains.");
303 jfenwick 4730 }
304 jfenwick 4734 extract<SplitWorld&> exw(t[0]);
305 jfenwick 4730 if (!exw.check())
306     {
307 jfenwick 4734 throw SplitWorldException("First parameter to buildDomains must be a SplitWorld.");
308 jfenwick 4730 }
309 jfenwick 4734 SplitWorld& ws=exw();
310 jfenwick 4730 tuple ntup=tuple(t.slice(1,l)); // strip off the object param
311     return ws.buildDomains(ntup, kwargs);
312     }
313    
314 jfenwick 4745 boost::python::object raw_addJob(boost::python::tuple t, boost::python::dict kwargs)
315     {
316     int l=len(t);
317     if (l<2)
318     {
319     throw SplitWorldException("Insufficient parameters to addJob.");
320     }
321     extract<SplitWorld&> exw(t[0]);
322     if (!exw.check())
323     {
324     throw SplitWorldException("First parameter to addJob must be a SplitWorld.");
325     }
326     SplitWorld& ws=exw();
327     object creator=t[1];
328     tuple ntup=tuple(t.slice(2,l)); // strip off the object param
329     ws.addJob(creator, ntup, kwargs);
330     return object();
331 jfenwick 4730 }
332 jfenwick 4745
333 jfenwick 4796 // expects, splitworld, name of var, constructor function for the reducer, any constructor params
334     boost::python::object raw_addVariable(boost::python::tuple t, boost::python::dict kwargs)
335     {
336     int l=len(t);
337     if (l<3)
338     {
339     throw SplitWorldException("Insufficient parameters to addReducer.");
340     }
341     extract<SplitWorld&> exw(t[0]);
342     if (!exw.check())
343     {
344     throw SplitWorldException("First parameter to addVariable must be a SplitWorld.");
345     }
346     SplitWorld& ws=exw();
347     object pname=t[1];
348     extract<std::string> ex2(pname);
349     if (!ex2.check())
350     {
351     throw SplitWorldException("Second parameter to addVariable must be a string");
352     }
353     std::string name=ex2();
354     object creator=t[2];
355     tuple ntup=tuple(t.slice(3,l)); // strip off the object param
356     ws.addVariable(name, creator, ntup, kwargs);
357     return object();
358     }
359 jfenwick 4745
360 jfenwick 4796
361    
362 jfenwick 4745 }

  ViewVC Help
Powered by ViewVC 1.1.26