1 |
|
2 |
/***************************************************************************** |
3 |
* |
4 |
* Copyright (c) 2003-2016 by The University of Queensland |
5 |
* http://www.uq.edu.au |
6 |
* |
7 |
* Primary Business: Queensland, Australia |
8 |
* Licensed under the Open Software License version 3.0 |
9 |
* http://www.opensource.org/licenses/osl-3.0.php |
10 |
* |
11 |
* Development until 2012 by Earth Systems Science Computational Center (ESSCC) |
12 |
* Development 2012-2013 by School of Earth Sciences |
13 |
* Development from 2014 by Centre for Geoscience Computing (GeoComp) |
14 |
* |
15 |
*****************************************************************************/ |
16 |
|
17 |
#include "NodeMapping.h" |
18 |
#include "Util.h" |
19 |
|
20 |
namespace dudley { |
21 |
|
22 |
Dudley_NodeMapping *Dudley_NodeMapping_alloc(dim_t numNodes, index_t * target, index_t unused) |
23 |
{ |
24 |
dim_t i; |
25 |
index_t min_target, numTargets, max_target; |
26 |
Dudley_NodeMapping *out = NULL; |
27 |
/* allocate the return value */ |
28 |
min_target = Dudley_Util_getFlaggedMinInt(1, numNodes, target, unused); |
29 |
if (min_target < 0) |
30 |
throw DudleyException("Dudley_NodeMapping_alloc: target has negative entry."); |
31 |
|
32 |
/* now we assume min_target=0! */ |
33 |
max_target = Dudley_Util_getFlaggedMaxInt(1, numNodes, target, unused); |
34 |
numTargets = min_target <= max_target ? max_target + 1 : 0; |
35 |
out = new Dudley_NodeMapping; |
36 |
out->reference_counter = 1; |
37 |
out->unused = unused; |
38 |
out->numNodes = numNodes; |
39 |
out->numTargets = numTargets; |
40 |
out->map = new index_t[numTargets]; |
41 |
out->target = new index_t[numNodes]; |
42 |
#pragma omp parallel |
43 |
{ |
44 |
#pragma omp for private(i) |
45 |
for (i = 0; i < numTargets; ++i) |
46 |
out->map[i] = -1; |
47 |
#pragma omp for private(i) |
48 |
for (i = 0; i < numNodes; ++i) { |
49 |
out->target[i] = target[i]; |
50 |
if (target[i] != unused) |
51 |
out->map[out->target[i]] = i; |
52 |
} |
53 |
#pragma omp for private(i) |
54 |
for (i = 0; i < numTargets; ++i) { |
55 |
if (out->map[i] == -1) { |
56 |
throw DudleyException("Dudley_NodeMapping_alloc: target does not define a continuous labeling."); |
57 |
} |
58 |
} |
59 |
} |
60 |
return out; |
61 |
} |
62 |
|
63 |
void Dudley_NodeMapping_free(Dudley_NodeMapping* in) |
64 |
{ |
65 |
if (in != NULL) { |
66 |
in->reference_counter--; |
67 |
if (in->reference_counter <= 0) { |
68 |
delete[] in->target; |
69 |
delete[] in->map; |
70 |
delete in; |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
Dudley_NodeMapping *Dudley_NodeMapping_getReference(Dudley_NodeMapping* in) |
76 |
{ |
77 |
if (in != NULL) |
78 |
in->reference_counter++; |
79 |
return in; |
80 |
} |
81 |
|
82 |
} // namespace dudley |
83 |
|