StdMpi.cc 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
16
#include "StdMpi.h"

namespace AMDiS {

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  MPI_Datatype StdMpiHelper<int>::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::vector<int> >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::vector<double> >::mpiDataType = MPI_DOUBLE;
  MPI_Datatype StdMpiHelper<std::vector<std::vector<double> > >::mpiDataType = MPI_DOUBLE;
  MPI_Datatype StdMpiHelper<std::vector<MeshStructure> >::mpiDataType = MPI_UNSIGNED_LONG;
  MPI_Datatype StdMpiHelper<std::vector<AtomicBoundary> >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::vector<std::pair<int, int> > >::mpiDataType = MPI_INT;
  MPI_Datatype StdMpiHelper<std::vector<WorldVector<double> > >::mpiDataType = MPI_DOUBLE;
  MPI_Datatype StdMpiHelper<std::map<WorldVector<double>, int> >::mpiDataType = MPI_DOUBLE;


  // T = int

  int StdMpiHelper<int>::getBufferSize(int &data)
32
33
34
35
  {
    return 1;
  }

36
  void StdMpiHelper<int>::createBuffer(int &data, int *buf)
37
  {
38
    buf[0] = data;
39
40
  }

41
  void StdMpiHelper<int>::makeFromBuffer(int &data, int *buf, int bufSize)
42
  {
43
    data = buf[0];
44
45
  }

46
47


48
49
50
  // T = std::vector<int>

  int StdMpiHelper<std::vector<int> >::getBufferSize(std::vector<int> &data)
51
  {
52
    return data.size();
53
54
  }

55
  int StdMpiHelper<std::vector<int> >::getBufferSize(std::vector<const int*> &data)
56
57
58
59
  {
    return data.size();
  }

60
  void StdMpiHelper<std::vector<int> >::createBuffer(std::vector<int> &data, int *buf)
61
  {
62
63
    for (unsigned int i = 0; i < data.size(); i++)
      buf[i] = data[i];
64
  }
65
66

  void StdMpiHelper<std::vector<int> >::makeFromBuffer(std::vector<int> &data, int *buf, int bufSize)
67
  {
68
69
70
71
    data.resize(bufSize);

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

74
75
76
77
78


  // T = std::vector<double>

  int StdMpiHelper<std::vector<double> >::getBufferSize(std::vector<double> &data)
79
  {
80
    return data.size();
81
82
  }

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

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

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

97
98
99
100
101


  // T = std::vector<std::vector<double> >

  int StdMpiHelper<std::vector<std::vector<double> > >::getBufferSize(std::vector<std::vector<double> > &data)
102
103
104
105
106
107
108
109
110
  {
    int size = 1;

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

111
  void StdMpiHelper<std::vector<std::vector<double> > >::createBuffer(std::vector<std::vector<double> > &data, double *buf)
112
  {
113
114
    buf[0] = data.size();
    int counter = 1;
115

116
117
118
119
    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];
120
121
122
    }
  }

123
  void StdMpiHelper<std::vector<std::vector<double> > >::makeFromBuffer(std::vector<std::vector<double> > &data, double *buf, int bufSize)
124
  {
125
126
    data.resize(static_cast<unsigned int>(buf[0]));
    int counter = 1;
127

128
129
130
131
132
    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++];
133
134
135
136
137
    }
  }



138
  // T = std::vector<MeshStructure>
139

140
  int StdMpiHelper<std::vector<MeshStructure> >::getBufferSize(std::vector<MeshStructure> &data)
141
  {
142
143
144
    int size = 0;
    for (unsigned int i = 0; i < data.size(); i++)
      size += data[i].getCode().size() + 2;
145

146
    return size;
147
148
  }

149
  void StdMpiHelper<std::vector<MeshStructure> >::createBuffer(std::vector<MeshStructure> &data, uint64_t *buf)
150
151
152
153
154
155
156
157
158
159
160
  {    
    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];
    }
  }

161
162
  void StdMpiHelper<std::vector<MeshStructure> >::makeFromBuffer(std::vector<MeshStructure> &data, 
								 uint64_t *buf, int bufSize)
163
164
165
166
167
168
  {
    int pos = 0;

    while (pos < bufSize) {       
      int codeSize = buf[pos++];
      int nElements = buf[pos++];
169
      std::vector<uint64_t> code;
170
171
172
173
174
175
176
177
178
179
180
181
182
      code.resize(codeSize);
      for (int i = 0; i < codeSize; i++)
	code[i] = buf[pos++];

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

      data.push_back(meshCode);
    }	
  }



183
  // T = std::vector<AtomicBoundary>
184

185
  int StdMpiHelper<std::vector<AtomicBoundary> >::getBufferSize(std::vector<AtomicBoundary> &data)
186
  {
187
    return data.size() * 6;
188
189
  }

190
  void StdMpiHelper<std::vector<AtomicBoundary> >::createBuffer(std::vector<AtomicBoundary> &data, int *buf)
191
192
  {
    for (unsigned int i = 0; i < data.size(); i++) {
193
194
195
196
197
198
      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;
199
200
201
    }
  }

202
  void StdMpiHelper<std::vector<AtomicBoundary> >::makeFromBuffer(std::vector<AtomicBoundary> &data, int *buf, int bufSize)
203
204
205
206
  {
    if (bufSize == 0)
      return;

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

209
210
211
212
213
214
215
216
    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];
217
218
219
220
221
    }
  }



222
  // T = std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> >
223

224
  int StdMpiHelper<std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > >::getBufferSize(std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > &data)
225
  {
226
    int size = 1;
227

228
229
230
    for (std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> >::iterator it = data.begin();
	 it != data.end(); ++it) {
      size += 2 + it->second.size() * 2;
231
232
    }

233
    return size;
234
235
  }

236
  void StdMpiHelper<std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > >::createBuffer(std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > &data, int *buf)
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  {
    buf[0] = data.size();
    int counter = 1;

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

      for (std::map<DegreeOfFreedom, DegreeOfFreedom>::iterator it2 = it->second.begin();
	   it2 != it->second.end(); ++it2) {
	buf[counter++] = it2->first;
	buf[counter++] = it2->second;
      }
    }
  }

254
  void StdMpiHelper<std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > >::makeFromBuffer(std::map<BoundaryType, std::map<DegreeOfFreedom, DegreeOfFreedom> > &data, int *buf, int bufSize)
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
  {
    data.clear();

    if (bufSize == 0)
      return;

    int counter = 1;
    
    for (int i = 0; i < buf[0]; i++) {
      BoundaryType bound = buf[counter++];
      std::map<DegreeOfFreedom, DegreeOfFreedom> dofs;

      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");
  }
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
331
332
333
334
335
336
337
338
339
340
341
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



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

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

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

  void StdMpiHelper<std::vector<std::pair<int, int> > >::makeFromBuffer(std::vector<std::pair<int, int> > &data, 
									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]));
  }



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

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

  void StdMpiHelper<std::vector<WorldVector<double> > >::createBuffer(std::vector<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++)
	buf[pos++] = data[i][j];
  }

  void StdMpiHelper<std::vector<WorldVector<double> > >::makeFromBuffer(std::vector<WorldVector<double> > &data, double *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 = std::map<WorldVector<double>, int>

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

  void StdMpiHelper<std::map<WorldVector<double>, int> >::createBuffer(std::map<WorldVector<double>, int> &data, double* buf)
  {
    int i = 0;
    for (std::map<WorldVector<double>, int>::iterator it = data.begin();
	 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);      
    }
  }

  void StdMpiHelper<std::map<WorldVector<double>, int> >::makeFromBuffer(std::map<WorldVector<double>, int> &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[coords] = static_cast<int>(buf[i++]);
    }
  }

384
}