-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParallelVertexFilter.h
477 lines (393 loc) · 13.4 KB
/
ParallelVertexFilter.h
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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* @file
* This file is part of XdmfWriter
*
* @author Sebastian Rettenberger <[email protected]>
*
* @copyright Copyright (c) 2014-2017, Technische Universitaet Muenchen.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef XDMFWRITER_PARALLELVERTEXFILTER_H
#define XDMFWRITER_PARALLELVERTEXFILTER_H
#ifdef USE_MPI
#include <mpi.h>
#endif // USE_MPI
#include <algorithm>
#include <cassert>
#include <cstring>
#include <limits>
#include <vector>
#include "utils/logger.h"
#include "FloatUnion.h"
#include "IndexSort.h"
#include "scorep_wrapper.h"
namespace xdmfwriter
{
namespace internal
{
/**
* Filters duplicate vertices in parallel
*/
template<typename T>
class ParallelVertexFilter
{
private:
#ifdef USE_MPI
/** The communicator we use */
MPI_Comm m_comm;
#endif // USE_MPI
/** Our rank */
int m_rank;
/** #Processes */
int m_numProcs;
/** The number of vertices after filtering */
unsigned int m_numFilterVertices;
/** The global id for each vertex */
unsigned long* m_globalIds;
/** A list of local vertex ids that are duplicates */
unsigned int* m_duplicates;
public:
ParallelVertexFilter()
: m_rank(0),
m_numProcs(1),
m_numFilterVertices(0),
m_globalIds(0L),
m_duplicates(0L)
{
#ifdef USE_MPI
setComm(MPI_COMM_WORLD);
#endif // USE_MPI
}
virtual ~ParallelVertexFilter()
{
delete [] m_globalIds;
delete [] m_duplicates;
}
#ifdef USE_MPI
/**
* Call this before calling {@link filter} to use a different communicator than
* MPI_COMM_WORLD.
*/
void setComm(MPI_Comm comm)
{
m_comm = comm;
MPI_Comm_rank(comm, &m_rank);
MPI_Comm_size(comm, &m_numProcs);
}
#endif // USE_MPI
/**
* @param vertices Vertices that should be filtered, must have the size <code>numVertices * 3</code>
*/
void filter(unsigned int numVertices, const T *vertices)
{
SCOREP_USER_REGION("ParallelVertexFilter_Filter", SCOREP_USER_REGION_TYPE_FUNCTION);
#ifdef USE_MPI
// MPI data type consisting of three doubles/floats
MPI_Datatype vertexType;
MPI_Type_contiguous(3, mpiFloatType(), &vertexType);
MPI_Type_commit(&vertexType);
// Chop the last 4 bits to avoid numerical errors
T *roundVertices = new T[numVertices*3];
removeRoundError(vertices, numVertices*3, roundVertices);
// Create indices and sort them locally
unsigned int *sortIndices = new unsigned int[numVertices];
IndexSort<UNSTABLE, T>::sort(roundVertices, numVertices, sortIndices);
// Select BUCKETS_PER_RANK-1 splitter elements
T localSplitters[BUCKETS_PER_RANK-1];
#if 0 // Use omp only if we create a larger amount of buckets
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
#endif
for (int i = 0; i < BUCKETS_PER_RANK-1; i++) {
unsigned long vrtxIndex = static_cast<unsigned long>(i)
* static_cast<unsigned long>(numVertices)
/ static_cast<unsigned long>(BUCKETS_PER_RANK-1);
assert(vrtxIndex < numVertices);
localSplitters[i] = roundVertices[sortIndices[vrtxIndex]*3];
}
// Collect all splitter elements on rank 0
T *allSplitters = 0L;
if (m_rank == 0)
allSplitters = new T[m_numProcs * (BUCKETS_PER_RANK-1)];
MPI_Gather(localSplitters, BUCKETS_PER_RANK-1, mpiFloatType(),
allSplitters, BUCKETS_PER_RANK-1, mpiFloatType(),
0, m_comm);
// Sort splitter elements
if (m_rank == 0)
std::sort(allSplitters, allSplitters + (m_numProcs * (BUCKETS_PER_RANK-1)));
// Distribute splitter to all processes
T *splitters = new T[m_numProcs-1];
if (m_rank == 0) {
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < m_numProcs-1; i++) {
unsigned long spltIndex = (i+1) * (BUCKETS_PER_RANK-1);
assert(spltIndex < static_cast<unsigned int>(m_numProcs * (BUCKETS_PER_RANK-1)));
splitters[i] = allSplitters[spltIndex];
}
}
MPI_Bcast(splitters, m_numProcs-1, mpiFloatType(), 0, m_comm);
delete [] allSplitters;
// Determine the bucket for each vertex
unsigned int *bucket = new unsigned int[numVertices];
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (unsigned int i = 0; i < numVertices; i++) {
T* ub = std::upper_bound(splitters, splitters+m_numProcs-1, roundVertices[i*3]);
bucket[i] = ub-splitters;
}
delete [] splitters;
// Determine the (local and total) bucket size
int *bucketSize = new int[m_numProcs];
memset(bucketSize, 0, sizeof(int)*m_numProcs);
for (unsigned int i = 0; i < numVertices; i++) {
assert(bucket[i] < static_cast<unsigned int>(m_numProcs));
bucketSize[bucket[i]]++;
}
delete [] bucket;
// Tell all processes what we are going to send them
int *recvSize = new int[m_numProcs];
MPI_Alltoall(bucketSize, 1, MPI_INT, recvSize, 1, MPI_INT, m_comm);
unsigned int numSortVertices = 0;
#if defined(_OPENMP) && !defined(__NVCOMPILER)
// [email protected] compiler corrupts the executable code near the omp `reduction` clause.
// Therefore, we compute `numSortVertices` without OpenMP.
#pragma omp parallel for schedule(static) reduction(+: numSortVertices)
#endif
for (int i = 0; i < m_numProcs; i++)
numSortVertices += recvSize[i];
// Create sorted send buffer
T *sendVertices = new T[3 * numVertices];
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (unsigned int i = 0; i < numVertices; i++) {
memcpy(&sendVertices[i*3], &roundVertices[sortIndices[i]*3], sizeof(T)*3);
}
delete [] roundVertices;
// Allocate buffer for the vertices and exchange them
T *sortVertices = new T[3 * numSortVertices];
int *sDispls = new int[m_numProcs];
int *rDispls = new int[m_numProcs];
sDispls[0] = 0;
rDispls[0] = 0;
for (int i = 1; i < m_numProcs; i++) {
sDispls[i] = sDispls[i-1] + bucketSize[i-1];
rDispls[i] = rDispls[i-1] + recvSize[i-1];
}
MPI_Alltoallv(sendVertices, bucketSize, sDispls, vertexType,
sortVertices, recvSize, rDispls, vertexType, m_comm);
delete [] sendVertices;
// Create indices and sort them (such that the vertices are sorted)
unsigned int *sortSortIndices = new unsigned int[numSortVertices];
IndexSort<STABLE, T>::sort(sortVertices, numSortVertices, sortSortIndices);
// A list that indicates which vertex is a duplicate (with use char instead of bool to work with MPI)
char* sortDuplicate = new char[numSortVertices];
if (numSortVertices > 0) {
sortDuplicate[sortSortIndices[0]] = 0;
for (unsigned int i = 1; i < numSortVertices; i++) {
if (equals(&sortVertices[sortSortIndices[i-1]*3], &sortVertices[sortSortIndices[i]*3]))
sortDuplicate[sortSortIndices[i]] = 1;
else
sortDuplicate[sortSortIndices[i]] = 0;
}
}
delete [] sortVertices;
// Send back the duplicate information
char* duplicate = new char[numVertices];
MPI_Alltoallv(sortDuplicate, recvSize, rDispls, MPI_CHAR,
duplicate, bucketSize, sDispls, MPI_CHAR, m_comm);
// Count the number of duplicates
m_numFilterVertices = std::count_if(duplicate, duplicate+numVertices, isZero);
assert(m_numFilterVertices <= numVertices);
// Get the vertices offset
unsigned long offset = m_numFilterVertices;
MPI_Scan(MPI_IN_PLACE, &offset, 1, MPI_UNSIGNED_LONG, MPI_SUM, m_comm);
offset -= m_numFilterVertices;
// Create the inverse sortIndex
unsigned int* invSortIndices = new unsigned int[numVertices];
for (unsigned int i = 0; i < numVertices; i++) {
assert(sortIndices[i] < numVertices);
invSortIndices[sortIndices[i]] = i;
}
// Set the global ids for all non duplicates and create the list of duplicates
delete [] m_globalIds;
m_globalIds = new unsigned long[numVertices];
delete [] m_duplicates;
m_duplicates = new unsigned int[numVertices - m_numFilterVertices];
unsigned int dupPos = 0;
for (unsigned int i = 0; i < numVertices; i++) {
assert(invSortIndices[i] < numVertices);
if (duplicate[invSortIndices[i]]) {
m_globalIds[i] = std::numeric_limits<unsigned long>::max();
assert(dupPos < numVertices - m_numFilterVertices);
m_duplicates[dupPos++] = i;
} else {
m_globalIds[i] = offset++;
}
}
delete [] invSortIndices;
// Sent the global ids to the sorting ranks
unsigned long* sendrecvGlobalIds = new unsigned long[numVertices];
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (unsigned int i = 0; i < numVertices; i++) {
assert(sortIndices[i] < numVertices);
sendrecvGlobalIds[i] = m_globalIds[sortIndices[i]];
}
unsigned long* sortGlobalIds = new unsigned long[numSortVertices];
MPI_Alltoallv(sendrecvGlobalIds, bucketSize, sDispls, MPI_UNSIGNED_LONG,
sortGlobalIds, recvSize, rDispls, MPI_UNSIGNED_LONG, m_comm);
// Fill missing global ids
for (unsigned int i = 1; i < numSortVertices; i++) {
assert(sortSortIndices[i] < numSortVertices);
if (sortDuplicate[sortSortIndices[i]]) {
assert(sortGlobalIds[sortSortIndices[i]] == std::numeric_limits<unsigned long>::max());
sortGlobalIds[sortSortIndices[i]] = sortGlobalIds[sortSortIndices[i-1]];
}
}
delete [] sortDuplicate;
delete [] sortSortIndices;
// Send back the global ids (including ids for duplicate verices)
MPI_Alltoallv(sortGlobalIds, recvSize, rDispls, MPI_UNSIGNED_LONG,
sendrecvGlobalIds, bucketSize, sDispls, MPI_UNSIGNED_LONG, m_comm);
delete [] sortGlobalIds;
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (unsigned int i = 0; i < numVertices; i++) {
assert(sortIndices[i] < numVertices);
assert(m_globalIds[sortIndices[i]] == std::numeric_limits<unsigned long>::max()
|| m_globalIds[sortIndices[i]] == sendrecvGlobalIds[i]);
m_globalIds[sortIndices[i]] = sendrecvGlobalIds[i];
}
delete [] sendrecvGlobalIds;
delete [] bucketSize;
delete [] recvSize;
delete [] sDispls;
delete [] rDispls;
delete [] sortIndices;
MPI_Type_free(&vertexType);
#endif // USE_MPI
}
/**
* @return Number of vertices this process is responsible for after filtering
*/
unsigned int numLocalVertices() const
{
return m_numFilterVertices;
}
/**
* @return The list of the global identifiers after filtering
*/
const unsigned long* globalIds() const
{
return m_globalIds;
}
/**
* @return The list of local vertex ids that are duplicates. The list has length
* <code>numVertices</code> - {@link numLocalVertices}.
*/
const unsigned int* duplicates() const
{
return m_duplicates;
}
private:
#ifdef USE_MPI
/**
* @return The MPI datatype for <code>T</code>
*/
static MPI_Datatype mpiFloatType();
#endif // USE_MPI
/**
* Removes round errors of double values by setting the last 4 bits
* (of the significand) to zero.
*
* @warning Only works if <code>value</code> ist not nan or infinity
* @todo This should work for arbitrary precision
*/
static T removeRoundError(T value)
{
FloatUnion<T> result;
result.f = value;
result.bits &= ~0xFF;
return result.f;
}
/**
* Removes the round errors using {@link removeRoundError(double)}
*
* @param values The list of floating point values
* @param count Number of values
* @param[out] roundValues The list of rounded values
* (the caller is responsible for allocating the memory)
*/
static void removeRoundError(const T* values, unsigned int count, T* roundValues)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (unsigned int i = 0; i < count; i++)
roundValues[i] = removeRoundError(values[i]);
}
/**
* Compares to vertices for equality
* Assumes that the rounding errors are removed.
*/
static bool equals(const T* vertexA, const T* vertexB)
{
return vertexA[0] == vertexB[0]
&& vertexA[1] == vertexB[1]
&& vertexA[2] == vertexB[2];
}
static bool isZero(char c)
{
return c == 0;
}
/** The total buckets we create is <code>BUCKETS_PER_RANK * numProcs</code> */
const static int BUCKETS_PER_RANK = 8;
};
#ifdef USE_MPI
template<> inline
MPI_Datatype ParallelVertexFilter<float>::mpiFloatType()
{
return MPI_FLOAT;
}
template<> inline
MPI_Datatype ParallelVertexFilter<double>::mpiFloatType()
{
return MPI_DOUBLE;
}
#endif // USE_MPI
}
}
#endif // XDMFWRITER_PARALLELVERTEXFILTER_H