StdMpi.cc 22.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
#include "StdMpi.h"

24
25
using namespace std;

26
namespace AMDiS { namespace Parallel {
27

28
  MPI_Datatype StdMpiHelper<int>::mpiDataType = MPI_INT;
29
30
31
  MPI_Datatype StdMpiHelper<vector<int> >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::set<int> >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<vector<std::set<int> > >::mpiDataType = MPI_INT;
32
  MPI_Datatype StdMpiHelper<std::set<std::pair<int, int> > >::mpiDataType = MPI_INT;
33
34
  MPI_Datatype StdMpiHelper<vector<double> >::mpiDataType = MPI_DOUBLE;
  MPI_Datatype StdMpiHelper<vector<vector<double> > >::mpiDataType = MPI_DOUBLE;
35
  MPI_Datatype StdMpiHelper<vector<MeshStructure> >::mpiDataType = MPI_UNSIGNED_LONG_LONG;
36
37
38
39
  MPI_Datatype StdMpiHelper<vector<AtomicBoundary> >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<vector<std::pair<int, int> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<vector<WorldVector<double> > >::mpiDataType = MPI_DOUBLE;
Praetorius, Simon's avatar
Praetorius, Simon committed
40
41
  MPI_Datatype StdMpiHelper<vector<WorldVector<int> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<vector<Vector<int> > >::mpiDataType = MPI_INT;
42
  MPI_Datatype StdMpiHelper<vector<WorldMatrix<double> > >::mpiDataType = MPI_DOUBLE;
Praetorius, Simon's avatar
Praetorius, Simon committed
43
44
45
  MPI_Datatype StdMpiHelper<vector<WorldVector<WorldVector<double> > > >::mpiDataType = MPI_DOUBLE;
  MPI_Datatype StdMpiHelper<vector<WorldMatrix<int> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<vector<Matrix<int> > >::mpiDataType = MPI_INT;
46
  MPI_Datatype StdMpiHelper<map<WorldVector<double>, int> >::mpiDataType = MPI_DOUBLE;
47
  MPI_Datatype StdMpiHelper<map<int, WorldVector<double> > >::mpiDataType = MPI_DOUBLE;
48
  MPI_Datatype StdMpiHelper<vector<vector<WorldVector<double> > > >::mpiDataType = MPI_DOUBLE;
49
50
51
52

  // T = int

  int StdMpiHelper<int>::getBufferSize(int &data)
53
54
55
56
  {
    return 1;
  }

57
  void StdMpiHelper<int>::createBuffer(int &data, int *buf)
58
  {
59
    buf[0] = data;
60
61
  }

62
  void StdMpiHelper<int>::makeFromBuffer(int &data, int *buf, int bufSize)
63
  {
64
    data = buf[0];
65
66
  }

67
68


69
  // T = vector<int>
70

71
  int StdMpiHelper<vector<int> >::getBufferSize(vector<int> &data)
72
  {
73
    return data.size();
74
75
  }

76
  int StdMpiHelper<vector<int> >::getBufferSize(vector<const int*> &data)
77
78
79
80
  {
    return data.size();
  }

81
  void StdMpiHelper<vector<int> >::createBuffer(vector<int> &data, int *buf)
82
  {
83
84
    for (unsigned int i = 0; i < data.size(); i++)
      buf[i] = data[i];
85
  }
86

87
  void StdMpiHelper<vector<int> >::makeFromBuffer(vector<int> &data, int *buf, int bufSize)
88
  {
89
90
91
92
    data.resize(bufSize);

    for (int i = 0; i < bufSize; i++)
      data[i] = buf[i];
93
94
  }

95
96


97
  // T = std::set<int>
98

99
  int StdMpiHelper<std::set<int> >::getBufferSize(std::set<int> &data)
100
  {
101
    return data.size();
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
  void StdMpiHelper<std::set<int> >::createBuffer(std::set<int> &data, int *buf)
  {
    int i = 0;
    for (std::set<int>::iterator it = data.begin(); it != data.end(); ++it)
      buf[i++] = *it;
  }

  void StdMpiHelper<std::set<int> >::makeFromBuffer(std::set<int> &data, int *buf, int bufSize)
  {
    data.clear();

    for (int i = 0; i < bufSize; i++)
      data.insert(buf[i]);
  }


  // T = vector<std::set<int> >

  int StdMpiHelper<vector<std::set<int> > >::getBufferSize(vector<std::set<int> > &data)
  {
    int dataSize = 1;
    for (unsigned int i = 0; i < data.size(); i++)
      dataSize += data[i].size() + 1;

    return dataSize;
  }

131
132
  void StdMpiHelper<vector<std::set<int> > >::createBuffer(vector<std::set<int> > &data, 
							   int *buf)
133
134
135
136
137
138
139
140
141
142
143
  {
    int counter = 1;
    buf[0] = data.size();

    for (unsigned int i = 0; i < data.size(); i++) {
      buf[counter++] = data[i].size();
      for (std::set<int>::iterator it = data[i].begin(); it != data[i].end(); ++it)
	buf[counter++] = *it;
    }
  }

144
145
146
  void StdMpiHelper<vector<std::set<int> > >::makeFromBuffer(vector<std::set<int> > &data, 
							     int *buf, 
							     int bufSize)
147
  {
148
    FUNCNAME("StdMpiHelper<vector<set<int> > >::makeFromBuffer()");
149
150
151
152
153
154
155

    int counter = 1;
    data.resize(buf[0]);

    for (int i = 0; i < buf[0]; i++) {
      data[i].clear();
      int setSize = buf[counter++];
156
157
158
      TEST_EXIT_DBG(setSize <= bufSize - counter)
	("Should not happen: %d %d %d\n", setSize, bufSize, counter);

159
160
161
162
163
164
165
166
      for (int j = 0; j < setSize; j++)
	data[i].insert(buf[counter++]);
    }

    TEST_EXIT(counter == bufSize)("Counter is %d, but buffer size is %d!\n",
				  counter, bufSize);
  }

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
  // T = std::set<std::pair<int, int> > >
  
  int StdMpiHelper<std::set<std::pair<int, int> > >::getBufferSize(std::set<std::pair<int, int> > &data)
  {
    return data.size() * 2;
  }
  
  void StdMpiHelper<std::set<std::pair<int, int> > >::createBuffer(std::set<std::pair<int, int> > &data,
								   int *buf)
  {
    int i = 0;
      
    for(std::set<std::pair<int, int> >::iterator it = data.begin(); it != data.end(); ++it) {
      buf[i++] = it->first;
      buf[i++] = it->second;
    }
  }
  
  void StdMpiHelper<std::set<std::pair<int, int> > >::makeFromBuffer(std::set<std::pair<int, int> > &data,
								     int *buf,
								     int bufSize)
  {
    data.clear();
	
    for (int i = 0; i < bufSize; i += 2) 
      data.insert(std::make_pair(buf[i], buf[i + 1]));
  }
								     
  
196
197
198
199
200
201
202
203

  // T = vector<double>

  int StdMpiHelper<vector<double> >::getBufferSize(vector<double> &data)
  {
    return data.size();
  }

204
205
  void StdMpiHelper<vector<double> >::createBuffer(vector<double> &data, 
						   double *buf)
206
  {
207
208
    for (unsigned int i = 0; i < data.size(); i++)
      buf[i] = data[i];
209
210
  }

211
  void StdMpiHelper<vector<double> >::makeFromBuffer(vector<double> &data, double *buf, int bufSize)
212
  {
213
214
215
216
    data.resize(bufSize);

    for (int i = 0; i < bufSize; i++)
      data[i] = buf[i];
217
218
  }

219
220


221
  // T = vector<vector<double> >
222

223
  int StdMpiHelper<vector<vector<double> > >::getBufferSize(vector<vector<double> > &data)
224
225
226
227
228
229
230
231
232
  {
    int size = 1;

    for (unsigned int i = 0; i < data.size(); i++)
      size += data[i].size() + 1;
    
    return size;
  }

233
234
  void StdMpiHelper<vector<vector<double> > >::createBuffer(vector<vector<double> > &data, 
							    double *buf)
235
  {
236
237
    buf[0] = data.size();
    int counter = 1;
238

239
240
241
242
    for (unsigned int i = 0; i < data.size(); i++) {
      buf[counter++] = data[i].size();
      for (unsigned int j = 0; j < data[i].size(); j++)
	buf[counter++] = data[i][j];
243
244
245
    }
  }

246
247
248
  void StdMpiHelper<vector<vector<double> > >::makeFromBuffer(vector<vector<double> > &data, 
							      double *buf, 
							      int bufSize)
249
  {
250
251
    data.resize(static_cast<unsigned int>(buf[0]));
    int counter = 1;
252

253
254
255
256
257
    for (unsigned int i = 0; i < data.size(); i++) {
      data[i].resize(static_cast<unsigned int>(buf[counter++]));
      
      for (unsigned int j = 0; j < data[i].size(); j++)
	data[i][j] = buf[counter++];
258
259
260
261
262
    }
  }



263
  // T = vector<MeshStructure>
264

265
  int StdMpiHelper<vector<MeshStructure> >::getBufferSize(vector<MeshStructure> &data)
266
  {
267
268
    FUNCNAME("StdMpiHelper<vector<MeshStructure> >::getBufferSize()");

269
270
271
    int size = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      size += data[i].getCode().size() + 2;
272

273
    return size;
274
275
  }

276
  void StdMpiHelper<vector<MeshStructure> >::createBuffer(vector<MeshStructure> &data, uint64_t *buf)
277
  {    
278
279
    FUNCNAME("StdMpiHelper<vector<MeshStructure> >::createBuffer()");

280
281
282
283
284
285
286
287
288
289
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++) {
      buf[pos++] = data[i].getCode().size();
      buf[pos++] = data[i].getNumElements();

      for (unsigned int j = 0; j < data[i].getCode().size(); j++)
	buf[pos++] = data[i].getCode()[j];
    }
  }

290
  void StdMpiHelper<vector<MeshStructure> >::makeFromBuffer(vector<MeshStructure> &data, 
291
							    uint64_t *buf, int bufSize)
292
  {
293
294
    FUNCNAME("StdMpiHelper<vector<MeshStructure> >::makeFromBuffer()");

295
296
    int pos = 0;

297
    while (pos < bufSize) {
298
299
      int codeSize = buf[pos++];
      int nElements = buf[pos++];
300
      vector<uint64_t> code;
301
302
303
304
305
306
307
308
309
310
311
312
313
      code.resize(codeSize);
      for (int i = 0; i < codeSize; i++)
	code[i] = buf[pos++];

      MeshStructure meshCode;
      meshCode.init(code, nElements);

      data.push_back(meshCode);
    }	
  }



314
  // T = vector<AtomicBoundary>
315

316
  int StdMpiHelper<vector<AtomicBoundary> >::getBufferSize(vector<AtomicBoundary> &data)
317
  {
318
    return data.size() * 6;
319
320
  }

321
  void StdMpiHelper<vector<AtomicBoundary> >::createBuffer(vector<AtomicBoundary> &data, int *buf)
322
323
  {
    for (unsigned int i = 0; i < data.size(); i++) {
324
325
326
327
328
329
      buf[i * 6] = data[i].rankObj.elIndex;
      buf[i * 6 + 1] = data[i].rankObj.subObj;
      buf[i * 6 + 2] = data[i].rankObj.ithObj;
      buf[i * 6 + 3] = data[i].neighObj.elIndex;
      buf[i * 6 + 4] = data[i].neighObj.subObj;
      buf[i * 6 + 5] = data[i].neighObj.ithObj;
330
331
332
    }
  }

333
  void StdMpiHelper<vector<AtomicBoundary> >::makeFromBuffer(vector<AtomicBoundary> &data, int *buf, int bufSize)
334
335
336
337
  {
    if (bufSize == 0)
      return;

338
    TEST_EXIT(bufSize % 6 == 0)("This should not happen!\n");    
339

340
341
342
343
344
345
346
347
    data.resize(bufSize / 6);
    for (int i = 0; i < bufSize / 6; i++) {
      data[i].rankObj.elIndex = buf[i * 6];
      data[i].rankObj.subObj = static_cast<GeoIndex>(buf[i * 6 + 1]);
      data[i].rankObj.ithObj = buf[i * 6 + 2];
      data[i].neighObj.elIndex = buf[i * 6 + 3];
      data[i].neighObj.subObj = static_cast<GeoIndex>(buf[i * 6 + 4]);
      data[i].neighObj.ithObj = buf[i * 6 + 5];
348
349
350
351
352
    }
  }



353
  // T = map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> >
354

355
  int StdMpiHelper<map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > >::getBufferSize(map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > &data)
356
  {
357
    int size = 1;
358

359
    for (map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> >::iterator it = data.begin();
360
361
	 it != data.end(); ++it) {
      size += 2 + it->second.size() * 2;
362
363
    }

364
    return size;
365
366
  }

367
  void StdMpiHelper<map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > >::createBuffer(map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > &data, int *buf)
368
369
370
371
  {
    buf[0] = data.size();
    int counter = 1;

372
    for (map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> >::iterator it = data.begin();
373
374
375
376
	 it != data.end(); ++it) {
      buf[counter++] = it->first;
      buf[counter++] = it->second.size();

377
      for (map<DegreeOfFreedom, DegreeOfFreedom>::iterator it2 = it->second.begin();
378
379
380
381
382
383
384
	   it2 != it->second.end(); ++it2) {
	buf[counter++] = it2->first;
	buf[counter++] = it2->second;
      }
    }
  }

385
  void StdMpiHelper<map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > >::makeFromBuffer(map<BoundaryType, map<DegreeOfFreedom, DegreeOfFreedom> > &data, int *buf, int bufSize)
386
387
388
389
390
391
392
393
394
395
  {
    data.clear();

    if (bufSize == 0)
      return;

    int counter = 1;
    
    for (int i = 0; i < buf[0]; i++) {
      BoundaryType bound = buf[counter++];
396
      map<DegreeOfFreedom, DegreeOfFreedom> dofs;
397
398
399
400
401
402
403
404
405
406
407
408
409
410

      int nDofs = buf[counter++];
      for (int j = 0; j < nDofs; j++) {
	DegreeOfFreedom dof0, dof1;
	dof0 = buf[counter++];
	dof1 = buf[counter++];
	dofs[dof0] = dof1;
      }

      data[bound] = dofs;
    }

    TEST_EXIT(bufSize == counter)("Should not happen!\n");
  }
411
412
413



414
  // T = vector<std::pair<int, int> >
415

416
  int StdMpiHelper<vector<std::pair<int, int> > >::getBufferSize(vector<std::pair<int, int> > &data)
417
418
419
420
  {
    return data.size() * 2;
  }

421
  void StdMpiHelper<vector<std::pair<int, int> > >::createBuffer(vector<std::pair<int, int> > &data, int *buf)
422
423
424
425
426
427
428
  {
    for (unsigned int i = 0; i < data.size(); i++) {
      buf[i * 2] = data[i].first;
      buf[i * 2 + 1] = data[i].second;
    }
  }

429
  void StdMpiHelper<vector<std::pair<int, int> > >::makeFromBuffer(vector<std::pair<int, int> > &data, 
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
									int *buf, int bufSize)
  {    
    if (bufSize == 0)
      return;

    TEST_EXIT(bufSize % 2 == 0)("This should not happen!\n");

    data.clear();
    data.reserve(bufSize / 2);

    for (int i = 0; i < (bufSize / 2); i++)
      data.push_back(std::make_pair(buf[i * 2], buf[i * 2 + 1]));
  }



446
  // T = vector<WorldVector<double> >
447

448
  int StdMpiHelper<vector<WorldVector<double> > >::getBufferSize(vector<WorldVector<double> > &data)
449
450
451
452
  {
    return data.size() * Global::getGeo(WORLD);
  }

453
  void StdMpiHelper<vector<WorldVector<double> > >::createBuffer(vector<WorldVector<double> > &data, double *buf)
454
455
456
457
458
459
460
461
  {
    int dimOfWorld = Global::getGeo(WORLD);
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	buf[pos++] = data[i][j];
  }

462
  void StdMpiHelper<vector<WorldVector<double> > >::makeFromBuffer(vector<WorldVector<double> > &data, double *buf, int bufSize)
463
464
465
466
467
468
469
470
471
472
473
474
475
  {
    int dimOfWorld = Global::getGeo(WORLD);
    TEST_EXIT(bufSize % Global::getGeo(WORLD) == 0)("This should not happen!\n");

    int pos = 0;
    data.resize(bufSize / Global::getGeo(WORLD));
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	data[i][j] = buf[pos++];
  }



Praetorius, Simon's avatar
Praetorius, Simon committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  // T = vector<WorldVector<int> >

  int StdMpiHelper<vector<WorldVector<int> > >::getBufferSize(vector<WorldVector<int> > &data)
  {
    return data.size() * Global::getGeo(WORLD);
  }

  void StdMpiHelper<vector<WorldVector<int> > >::createBuffer(vector<WorldVector<int> > &data, int *buf)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	buf[pos++] = data[i][j];
  }

  void StdMpiHelper<vector<WorldVector<int> > >::makeFromBuffer(vector<WorldVector<int> > &data, int *buf, int bufSize)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    TEST_EXIT(bufSize % Global::getGeo(WORLD) == 0)("This should not happen!\n");

    int pos = 0;
    data.resize(bufSize / Global::getGeo(WORLD));
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	data[i][j] = buf[pos++];
  }



  // T = vector<Vector<int> >

  int StdMpiHelper<vector<Vector<int> > >::getBufferSize(vector<Vector<int> > &data)
  {
    return data.size() > 0 ? data.size() * data[0].getSize() : 0;
  }

  void StdMpiHelper<vector<Vector<int> > >::createBuffer(vector<Vector<int> > &data, int *buf)
  {
    int pos = 0;
    buf[pos++] = data.size() > 0 ? data[0].getSize() : 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < data[i].getSize(); j++)
	buf[pos++] = data[i][j];
  }

  void StdMpiHelper<vector<Vector<int> > >::makeFromBuffer(vector<Vector<int> > &data, int *buf, int bufSize)
  {
    int pos = 0;
    int size = buf[pos++];
    data.resize(bufSize / size);
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < size; j++)
	data[i][j] = buf[pos++];
  }


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
  // T = vector<WorldMatrix<double> >

  int StdMpiHelper<vector<WorldMatrix<double> > >::getBufferSize(vector<WorldMatrix<double> > &data)
  {
    return data.size() * sqr(Global::getGeo(WORLD));
  }

  void StdMpiHelper<vector<WorldMatrix<double> > >::createBuffer(vector<WorldMatrix<double> > &data, double *buf)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  buf[pos++] = data[i][j][k];
  }

  void StdMpiHelper<vector<WorldMatrix<double> > >::makeFromBuffer(vector<WorldMatrix<double> > &data, double *buf, int bufSize)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    TEST_EXIT(bufSize % sqr(Global::getGeo(WORLD)) == 0)("This should not happen!\n");

    int pos = 0;
    data.resize(bufSize / sqr(dimOfWorld));
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  data[i][j][k] = buf[pos++];
  }


Praetorius, Simon's avatar
Praetorius, Simon committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
  // T = vector<WorldMatrix<double> >

  int StdMpiHelper<vector<WorldVector<WorldVector<double> > > >::getBufferSize(vector<WorldVector<WorldVector<double> > > &data)
  {
    return data.size() * sqr(Global::getGeo(WORLD));
  }

  void StdMpiHelper<vector<WorldVector<WorldVector<double> > > >::createBuffer(vector<WorldVector<WorldVector<double> > > &data, double *buf)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  buf[pos++] = data[i][j][k];
  }

  void StdMpiHelper<vector<WorldVector<WorldVector<double> > > >::makeFromBuffer(vector<WorldVector<WorldVector<double> > > &data, double *buf, int bufSize)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    TEST_EXIT(bufSize % sqr(Global::getGeo(WORLD)) == 0)("This should not happen!\n");

    int pos = 0;
    data.resize(bufSize / sqr(dimOfWorld));
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  data[i][j][k] = buf[pos++];
  }



  // T = vector<WorldMatrix<int> >

  int StdMpiHelper<vector<WorldMatrix<int> > >::getBufferSize(vector<WorldMatrix<int> > &data)
  {
    return data.size() * sqr(Global::getGeo(WORLD));
  }

  void StdMpiHelper<vector<WorldMatrix<int> > >::createBuffer(vector<WorldMatrix<int> > &data, int *buf)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    int pos = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  buf[pos++] = data[i][j][k];
  }

  void StdMpiHelper<vector<WorldMatrix<int> > >::makeFromBuffer(vector<WorldMatrix<int> > &data, int *buf, int bufSize)
  {
    int dimOfWorld = Global::getGeo(WORLD);
    TEST_EXIT(bufSize % sqr(Global::getGeo(WORLD)) == 0)("This should not happen!\n");

    int pos = 0;
    data.resize(bufSize / sqr(dimOfWorld));
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < dimOfWorld; j++)
	for (int k = 0; k < dimOfWorld; k++)
	  data[i][j][k] = buf[pos++];
  }


  // T = vector<Matrix<int> >

  int StdMpiHelper<vector<Matrix<int> > >::getBufferSize(vector<Matrix<int> > &data)
  {
    return data.size() > 0 ? data.size() * data[0].getNumRows() * data[0].getNumCols() + 2 : 0;
  }

  void StdMpiHelper<vector<Matrix<int> > >::createBuffer(vector<Matrix<int> > &data, int *buf)
  {
636
//     int dimOfWorld = Global::getGeo(WORLD);
Praetorius, Simon's avatar
Praetorius, Simon committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    int pos = 0;
    buf[pos++] = data.size() > 0 ? data[0].getNumRows() : 0;
    buf[pos++] = data.size() > 0 ? data[0].getNumCols() : 0;
    for (unsigned int i = 0; i < data.size(); i++)
      for (int j = 0; j < data[i].getNumRows(); j++)
	for (int k = 0; k < data[i].getNumCols(); k++)
	  buf[pos++] = data[i][j][k];
  }

  void StdMpiHelper<vector<Matrix<int> > >::makeFromBuffer(vector<Matrix<int> > &data, int *buf, int bufSize)
  {
    int pos = 0;
    int n_rows = buf[pos++];
    int n_cols = buf[pos++];
    data.resize(bufSize / (n_rows * n_cols));
    for (unsigned int i = 0; i < data.size(); i++) {
      data[i].resize(n_rows, n_cols);
      for (int j = 0; j < n_rows; j++)
	for (int k = 0; k < n_cols; k++)
	  data[i][j][k] = buf[pos++];
    }
  }


661

662
  // T = map<WorldVector<double>, int>
663

664
  int StdMpiHelper<map<WorldVector<double>, int> >::getBufferSize(map<WorldVector<double>, int> &data)
665
666
667
668
  {
    return data.size() * (Global::getGeo(WORLD) + 1);
  }

669
  void StdMpiHelper<map<WorldVector<double>, int> >::createBuffer(map<WorldVector<double>, int> &data, double* buf)
670
671
  {
    int i = 0;
672
    for (map<WorldVector<double>, int>::iterator it = data.begin();
673
674
675
676
677
678
679
	 it != data.end(); ++it) {
      for (int j = 0; j < Global::getGeo(WORLD); j++)
	buf[i++] = it->first[j];
      buf[i++] = static_cast<double>(it->second);      
    }
  }

680
  void StdMpiHelper<map<WorldVector<double>, int> >::makeFromBuffer(map<WorldVector<double>, int> &data, double* buf, int bufSize)
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  {
    if (bufSize == 0)
      return;

    int oneEntrySize = Global::getGeo(WORLD) + 1;
    int nEntry = bufSize / oneEntrySize;

    TEST_EXIT(bufSize % oneEntrySize == 0)("This should not happen!\n");

    data.clear();
    int i = 0;
    WorldVector<double> coords;

    for (int j = 0; j < nEntry; j++) {
      for (int k = 0; k < Global::getGeo(WORLD); k++)
	coords[k] = buf[i++];
      data[coords] = static_cast<int>(buf[i++]);
    }
  }

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
  // T = map<WorldVector<double>, int>

  int StdMpiHelper<map<int, WorldVector<double> > >::getBufferSize(map<int, WorldVector<double> > &data)
  {
    return data.size() * (Global::getGeo(WORLD) + 1);
  }

  void StdMpiHelper<map<int, WorldVector<double> > >::createBuffer(map<int, WorldVector<double> > &data, double* buf)
  {
    int i = 0;
    for (map<int, WorldVector<double> >::iterator it = data.begin();
	 it != data.end(); ++it) {
      for (int j = 0; j < Global::getGeo(WORLD); j++)
	buf[i++] = it->second[j];
      buf[i++] = static_cast<double>(it->first);      
    }
  }

  void StdMpiHelper<map<int, WorldVector<double> > >::makeFromBuffer(map<int, WorldVector<double> > &data, double* buf, int bufSize)
  {
    if (bufSize == 0)
      return;

    int oneEntrySize = Global::getGeo(WORLD) + 1;
    int nEntry = bufSize / oneEntrySize;

    TEST_EXIT(bufSize % oneEntrySize == 0)("This should not happen!\n");

    data.clear();
    int i = 0;
    WorldVector<double> coords;

    for (int j = 0; j < nEntry; j++) {
      for (int k = 0; k < Global::getGeo(WORLD); k++)
	coords[k] = buf[i++];
      data[static_cast<int>(buf[i++])] = coords;
    }
  }
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761

  // T = vector<vector<WorldVector<double> > >

  int StdMpiHelper<vector<vector<WorldVector<double> > > >::getBufferSize(vector<vector<WorldVector<double> > > &data)
  {
    int result = 1;

    for (unsigned int i = 0; i < data.size(); i++)
      result += 1 + (data[i].size() * Global::getGeo(WORLD));

    return result;
  }

  void StdMpiHelper<vector<vector<WorldVector<double> > > >::createBuffer(vector<vector<WorldVector<double> > > &data, 
									  double* buf)
  {
    int counter = 0;

    buf[counter++] = static_cast<double>(data.size());

    for (unsigned int i = 0; i < data.size(); i++) {
      buf[counter++] = data[i].size();
      for (unsigned int j = 0; j < data[i].size(); j++)
762
	for (int k = 0; k < Global::getGeo(WORLD); k++) 
763
764
765
766
767
768
769
770
771
772
773
774
775
	  buf[counter++] = data[i][j][k];
    }
  }

  void StdMpiHelper<vector<vector<WorldVector<double> > > >::makeFromBuffer(vector<vector<WorldVector<double> > > &data, 
									    double* buf, int bufSize)
  {
    int counter = 0;
    data.resize(static_cast<int>(buf[counter++]));

    for (unsigned int i = 0; i < data.size(); i++) {
      data[i].resize(static_cast<int>(buf[counter++]));
      for (unsigned int j = 0; j < data[i].size(); j++) 
776
	for (int k = 0; k < Global::getGeo(WORLD); k++) 
777
778
779
780
781
782
	  data[i][j][k] = buf[counter++];
    }

    TEST_EXIT_DBG(counter == bufSize)("There is something very wrong!\n");
  }

783
} }