ParallelProblem.h 12.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file ParallelProblem.h */

#ifndef AMDIS_PARALLELPROBLEM_H
#define AMDIS_PARALLELPROBLEM_H

#include "ProblemTimeInterface.h"
#include "ProblemIterationInterface.h"
#include "AdaptInfo.h"
#include "mpi.h"
#include <map>
#include <vector>
#include <set>

namespace AMDiS {

  // =========================================================================
  // ===== class ParallelProblemInterface ====================================
  // =========================================================================
  
  /** \brief
   * Interface for parallel problems
   */
  class ParallelProblemInterface
  {
  public:
    virtual void initParallelization(AdaptInfo *adaptInfo) = 0;
    virtual void exitParallelization(AdaptInfo *adaptInfo) = 0;
  };

  // =========================================================================
  // ===== class ParallelProblemBase =========================================
  // =========================================================================

  /** \brief
   * Schablonen Klasse
   */
  class ParallelProblemBase : public ParallelProblemInterface,
			      public ProblemIterationInterface,
			      public ProblemTimeInterface
  {
  public:
    ParallelProblemBase(ProblemIterationInterface *iterationIF,
			ProblemTimeInterface *timeIF);

    virtual ~ParallelProblemBase() {};

    virtual bool doPartitioning(AdaptInfo *adaptInfo, double localWeightSum) = 0;
    virtual bool doBuildGlobalSolution(AdaptInfo *adaptInfo) = 0;
    virtual double setElemWeights(AdaptInfo *adaptInfo) = 0;
    virtual void partitionMesh(AdaptInfo *adaptInfo) = 0;
    virtual void refineOverlap(AdaptInfo *adaptInfo) = 0;    
    virtual void globalRefineOutOfPartition(AdaptInfo *adaptInfo) = 0;    
    virtual void createOverlap(AdaptInfo *adaptInfo) = 0;
    virtual void exchangeDOFVectors(AdaptInfo *adaptInfo) = 0;
    virtual void coarsenOutOfPartition(AdaptInfo *adaptInfo) = 0;
    virtual void synchronizeMeshes(AdaptInfo *adaptInfo) = 0;
    virtual void exchangeRankSolutions(AdaptInfo *adaptInfo) = 0;
    virtual void buildGlobalSolution(AdaptInfo *adaptInfo) = 0;

    virtual void exitParallelization(AdaptInfo *adaptInfo)
    {
      if (!timeIF_) 
	closeTimestep(adaptInfo);
    };

    virtual void setTime(AdaptInfo *adaptInfo) 
    {
      if (timeIF_) 
	timeIF_->setTime(adaptInfo);
    };

    virtual void initTimestep(AdaptInfo *adaptInfo)
    {
      if (timeIF_) 
	timeIF_->initTimestep(adaptInfo);
    };

    virtual void closeTimestep(AdaptInfo *adaptInfo)
    {
      if (mpiSize_ > 1 && doBuildGlobalSolution(adaptInfo)) {
	synchronizeMeshes(adaptInfo);
 	exchangeRankSolutions(adaptInfo);
 	buildGlobalSolution(adaptInfo);
      }
      if (timeIF_) 
	timeIF_->closeTimestep(adaptInfo);
    };

    virtual void solveInitialProblem(AdaptInfo *adaptInfo)
    {
      if (timeIF_)
	timeIF_->solveInitialProblem(adaptInfo);
    };
  
    virtual void transferInitialSolution(AdaptInfo *adaptInfo)
    {
      if (timeIF_) 
	timeIF_->transferInitialSolution(adaptInfo);
    };  


    virtual void beginIteration(AdaptInfo *adaptInfo)
    {
      iterationIF_->beginIteration(adaptInfo);
    };

    virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION)
    {
      Flag flag;

      if(mpiSize_ > 1 && toDo.isSet(MARK | ADAPT)) {
 	flag = iterationIF_->oneIteration(adaptInfo, MARK | ADAPT);

	double localWeightSum = setElemWeights(adaptInfo);
	if(doPartitioning(adaptInfo, localWeightSum)) {

	  clock_t partitioningStart = clock();

	  synchronizeMeshes(adaptInfo);
	  partitionMesh(adaptInfo);
	  refineOverlap(adaptInfo);
	  createOverlap(adaptInfo);
	  synchronizeMeshes(adaptInfo);
	  exchangeDOFVectors(adaptInfo);
	  coarsenOutOfPartition(adaptInfo);

	  clock_t partitioningEnd = clock();
	  partitioningTime = TIME_USED(partitioningStart, 
				       partitioningEnd);
	  computationStart = partitioningEnd;
	}

	flag |= iterationIF_->oneIteration(adaptInfo, toDo & ~(MARK | ADAPT));
      } else {
	flag = iterationIF_->oneIteration(adaptInfo, toDo);
      }

      // synchronize adaption flag
      unsigned long *flagBuffer = GET_MEMORY(unsigned long, mpiSize_);

      unsigned long localFlag = flag.getFlags();

      MPI::COMM_WORLD.Allgather(&localFlag, 1, MPI_UNSIGNED_LONG,
				flagBuffer, 1, MPI_UNSIGNED_LONG);
      int i;
      for(i = 0; i < mpiSize_; i++) {
	flag.setFlag(flagBuffer[i]);
      }
      FREE_MEMORY(flagBuffer, unsigned long, mpiSize_);
      return flag;
    };

    virtual void endIteration(AdaptInfo *adaptInfo) 
    {
      iterationIF_->endIteration(adaptInfo);
    };

  protected:
    int mpiRank_;
    int mpiSize_;
    ProblemIterationInterface *iterationIF_;
    ProblemTimeInterface *timeIF_;
    clock_t computationStart;
    double partitioningTime;
  };

  // =========================================================================
  // ===== class ParallelProblem =============================================
  // =========================================================================

  class ParMetisPartitioner;
  class RefinementManager;
  class CoarseningManager;
  class ElInfo;
  class MeshStructure;

  class ParallelProblem : public ParallelProblemBase
  {
  public:
    ParallelProblem(const std::string& name,
		    ProblemIterationInterface *iterationIF,
		    ProblemTimeInterface *timeIF,
		    std::vector<DOFVector<double>*> vectors,
		    Mesh *mesh,
		    RefinementManager *rm,
		    CoarseningManager *cm);

    virtual ~ParallelProblem();

    virtual bool doPartitioning(AdaptInfo *adaptInfo, double localWeightSum);

    virtual bool doBuildGlobalSolution(AdaptInfo *adaptInfo);

    virtual void partitionMesh(AdaptInfo *adaptInfo);
    virtual void refineOverlap(AdaptInfo *adaptInfo);
    virtual void createOverlap(AdaptInfo *adaptInfo);    
    virtual void globalRefineOutOfPartition(AdaptInfo *adaptInfo);
    virtual void coarsenOutOfPartition(AdaptInfo *adaptInfo);
    virtual void synchronizeMeshes(AdaptInfo *adaptInfo);
    virtual double setElemWeights(AdaptInfo *adaptInfo);

    virtual void globalRefinements();

    void exchangeRankSolutions(AdaptInfo *adaptInfo,
			       std::vector<DOFVector<double>*> rankSolutions);

    void exchangeDOFVector(AdaptInfo *adaptInfo,
			   DOFVector<double> *vec);

    void buildGlobalSolution(AdaptInfo *adaptInfo,
			     std::vector<DOFVector<double>*> rankSolutions,
			     DOFVector<double> *globalSolution);

    void createOverlap(int level, int overlap, bool openOverlap,
		       std::map<Element*, int> &overlapDistance);

    void fillVertexPartitions(int level, int overlap, bool openOverlap,
			      std::map<Element*, int> &overlapDistance);

    void setRepartitionSteps(int steps) { repartitionSteps_ = steps; };

    void puEveryTimestep(bool pu) { puEveryTimestep_ = pu; };

    void addDOFVector(DOFVector<double> *vec) 
    {
      dofVectors_.push_back(vec);
    };

    void exchangeMeshStructureCodes(MeshStructure *structures);

    static bool writeElement(ElInfo *elInfo);

    //     static void writeRankMacroAndValues(DOFVector<double> *vec, 
    // 					const char *name = NULL, 
    // 					double time = 0.0);


    virtual void serialize(std::ostream&) {};

    virtual void deserialize(std::istream&) {};

  protected:
    double errors2map(std::map<int, double> &errMap, int comp, bool add);

  protected:
    std::string name_;
    Mesh *mesh_;
    RefinementManager *refinementManager_;
    CoarseningManager *coarseningManager_;
    ParMetisPartitioner *partitioner_;
    std::map<int, int> partitionVec_;
    std::map<int, int> oldPartitionVec_;    
    std::map<int, double> elemWeights_;
    std::map<Element*, std::set<int> > elementPartitions_;
    std::map<DegreeOfFreedom, std::set<int> > vertexPartitions_;
    int repartitionSteps_;
    bool puEveryTimestep_;
    std::vector<DOFVector<double>*> dofVectors_;
    double upperPartThreshold_;
    double lowerPartThreshold_;
    int globalCoarseGridLevel_;
    int localCoarseGridLevel_;
    int globalRefinements_;
    std::map<Element*, int> overlapDistance_;

    int adaptiveThresholds_;
    double thresholdIncFactor_;
    double thresholdDecFactor_;
    double repartTimeFactor_;
    double minUpperTH_;
    double maxLowerTH_;
  };

  // =========================================================================
  // ===== class ParallelProblemScal =========================================
  // =========================================================================
  
  class FiniteElemSpace;
  class ProblemScal;
  class ProblemInstatScal;
  class Estimator;
  class Marker;

  class ParallelProblemScal : public ParallelProblem
  {
  public:
    ParallelProblemScal(const std::string& name,
			ProblemScal *problem,
			ProblemInstatScal *problemInstat,
			std::vector<DOFVector<double>*> vectors);

    virtual ~ParallelProblemScal();

    virtual void initParallelization(AdaptInfo *adaptInfo);

    virtual void exitParallelization(AdaptInfo *adaptInfo);

    virtual void exchangeDOFVectors(AdaptInfo *adaptInfo);

    virtual void exchangeRankSolutions(AdaptInfo *adaptInfo);

    virtual void buildGlobalSolution(AdaptInfo *adaptInfo);

    virtual void coarsenOutOfPartition(AdaptInfo *adaptInfo);

    virtual int getNumProblems();

    virtual ProblemStatBase *getProblem(int number = 0);

    virtual ProblemStatBase *getProblem(const std::string& name);

331
332
333
334
335
336
337
    void setEstimator(Estimator *est) { 
      usersEstimator_ = est; 
    };

    void setMarker(Marker *marker) { 
      usersMarker_ = marker; 
    };
338

339
340
341
    inline virtual const ::std::string& getName() { 
      return name_; 
    };
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

  protected:
    ProblemScal *problem_;

    ProblemInstatScal *problemInstat_;

    Estimator *oldEstimator_;

    Marker *oldMarker_;

    std::vector<DOFVector<double>*> rankSolution_;

    Estimator *usersEstimator_;

    Marker *usersMarker_;
  };

  // =========================================================================
  // ===== class ParallelProblemVec ==========================================
  // =========================================================================
  
  class ProblemVec;
  class ProblemInstatVec;
  class SystemVector;

  class ParallelProblemVec : public ParallelProblem
  {
  public:
    ParallelProblemVec(const std::string& name,
		       ProblemVec *problem,
		       ProblemInstatVec *problemInstat,
		       std::vector<DOFVector<double>*> vectors);

    virtual ~ParallelProblemVec();

    virtual void initParallelization(AdaptInfo *adaptInfo);

    virtual void exitParallelization(AdaptInfo *adaptInfo);

    virtual void exchangeDOFVectors(AdaptInfo *adaptInfo);

    virtual void exchangeRankSolutions(AdaptInfo *adaptInfo);

    virtual void buildGlobalSolution(AdaptInfo *adaptInfo);

    virtual void coarsenOutOfPartition(AdaptInfo *adaptInfo);

    virtual int getNumProblems();

    virtual ProblemStatBase *getProblem(int number = 0);

    virtual ProblemStatBase *getProblem(const std::string& name);

    void setEstimator(std::vector<Estimator*> est) { 
      usersEstimator_ = est; 
    };

399
    void setMarker(std::vector<Marker*> marker) {
400
401
402
      usersMarker_ = marker; 
    };

403
404
405
406
    inline virtual const ::std::string& getName() { 
      return name_; 
    };

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  protected:
    ProblemVec *problem_;

    ProblemInstatVec *problemInstat_;

    std::vector<Estimator*> oldEstimator_;

    std::vector<Marker*> oldMarker_;

    std::vector<SystemVector*> rankSolution_;

    std::vector<Estimator*> usersEstimator_;

    std::vector<Marker*> usersMarker_;

    int numComponents_;
  };

}

#endif