-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathParMETISInterface.cpp
More file actions
186 lines (155 loc) · 7.03 KB
/
ParMETISInterface.cpp
File metadata and controls
186 lines (155 loc) · 7.03 KB
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
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
/**
* @file ParMETISInterface.cpp
*/
#include "ParMETISInterface.hpp"
#include "common/GEOS_RAJA_Interface.hpp"
#include <parmetis.h>
#include <numeric>
#define GEOS_PARMETIS_CHECK( call ) \
do { \
auto const ierr = call; \
GEOS_ERROR_IF_NE_MSG( ierr, METIS_OK, "Error in call to:\n" #call ); \
} while( false )
namespace geos
{
namespace parmetis
{
static_assert( std::is_same< idx_t, pmet_idx_t >::value, "Non-matching index types. ParMETIS must be built with 64-bit indices." );
ArrayOfArrays< idx_t, idx_t >
meshToDual( ArrayOfArraysView< idx_t const, idx_t > const & elemToNodes,
arrayView1d< idx_t const > const & elemDist,
MPI_Comm comm,
int const minCommonNodes )
{
idx_t const numElems = elemToNodes.size();
// `parmetis` awaits the arrays to be allocated as two continuous arrays: one for values, the other for offsets.
// Our `ArrayOfArrays` allows to reserve some extra space for further element insertion,
// but this is not compatible with what `parmetis` requires.
GEOS_ASSERT_EQ_MSG( std::accumulate( elemToNodes.getSizes(), elemToNodes.getSizes() + numElems, 0 ),
elemToNodes.valueCapacity(),
"Internal error. The element to nodes mapping must be strictly allocated for compatibility with a third party library." );
idx_t numflag = 0;
idx_t ncommonnodes = minCommonNodes;
idx_t * xadj;
idx_t * adjncy;
// Technical UB if ParMETIS writes into these arrays; in practice we discard them right after
GEOS_PARMETIS_CHECK( ParMETIS_V3_Mesh2Dual( const_cast< idx_t * >( elemDist.data() ),
const_cast< idx_t * >( elemToNodes.getOffsets() ),
const_cast< idx_t * >( elemToNodes.getValues() ),
&numflag, &ncommonnodes, &xadj, &adjncy, &comm ) );
ArrayOfArrays< idx_t, idx_t > graph;
graph.resizeFromOffsets( numElems, xadj );
// There is no way to direct-copy values into ArrayOfArrays without UB (casting away const)
forAll< parallelHostPolicy >( numElems, [xadj, adjncy, graph = graph.toView()]( localIndex const k )
{
graph.appendToArray( k, adjncy + xadj[k], adjncy + xadj[k+1] );
} );
METIS_Free( xadj );
METIS_Free( adjncy );
return graph;
}
array1d< idx_t >
partition( ArrayOfArraysView< idx_t const, idx_t > const & graph,
arrayView1d< idx_t const > const & vertDist,
idx_t const numParts,
MPI_Comm comm,
int const numRefinements )
{
array1d< idx_t > part( graph.size() ); // all 0 by default
if( numParts == 1 )
{
return part;
}
// Compute tpwgts parameters (target partition weights)
array1d< real_t > tpwgts( numParts );
tpwgts.setValues< serialPolicy >( 1.0f / static_cast< real_t >( numParts ) );
// Set other ParMETIS parameters
idx_t wgtflag = 0;
idx_t numflag = 0;
idx_t ncon = 1;
idx_t npart = numParts;
idx_t options[4] = { 1, 0, 2022, PARMETIS_PSR_UNCOUPLED };
idx_t edgecut = 0;
real_t ubvec = 1.05;
// Technical UB if ParMETIS writes into these arrays; in practice we discard them right after
GEOS_PARMETIS_CHECK( ParMETIS_V3_PartKway( const_cast< idx_t * >( vertDist.data() ),
const_cast< idx_t * >( graph.getOffsets() ),
const_cast< idx_t * >( graph.getValues() ),
nullptr, nullptr, &wgtflag,
&numflag, &ncon, &npart, tpwgts.data(),
&ubvec, options, &edgecut, part.data(), &comm ) );
for( int iter = 0; iter < numRefinements; ++iter )
{
GEOS_PARMETIS_CHECK( ParMETIS_V3_RefineKway( const_cast< idx_t * >( vertDist.data() ),
const_cast< idx_t * >( graph.getOffsets() ),
const_cast< idx_t * >( graph.getValues() ),
nullptr, nullptr, &wgtflag,
&numflag, &ncon, &npart, tpwgts.data(),
&ubvec, options, &edgecut, part.data(), &comm ) );
}
return part;
}
array1d< idx_t >
partitionWeighted( ArrayOfArraysView< idx_t const, idx_t > const & graph,
arrayView1d< idx_t const > const & vertexWeights,
arrayView1d< idx_t const > const & vertDist,
idx_t const numParts,
MPI_Comm comm,
int const numRefinements )
{
array1d< idx_t > part( graph.size() );
if( numParts == 1 )
{
return part;
}
array1d< real_t > tpwgts( numParts );
tpwgts.setValues< serialPolicy >( 1.0f / static_cast< real_t >( numParts ) );
idx_t wgtflag = 2; // vertex weights only
idx_t numflag = 0;
idx_t ncon = 1;
idx_t npart = numParts;
// Options: [use_defaults, log_level, seed, coupling]
// PARMETIS_PSR_UNCOUPLED = 0 (default - uses PartitionSmallGraph)
// PARMETIS_PSR_COUPLED = 1 (forces distributed algorithm)
idx_t options[4] = { 1, 0, 2022, 0 };
idx_t edgecut = 0;
real_t ubvec = 1.05;
GEOS_PARMETIS_CHECK( ParMETIS_V3_PartKway(
const_cast< idx_t * >( vertDist.data() ),
const_cast< idx_t * >( graph.getOffsets() ),
const_cast< idx_t * >( graph.getValues() ),
const_cast< idx_t * >( vertexWeights.data() ),
nullptr, // edge weights
&wgtflag,
&numflag, &ncon, &npart, tpwgts.data(),
&ubvec, options, &edgecut, part.data(), &comm ) );
for( int iter = 0; iter < numRefinements; ++iter )
{
GEOS_PARMETIS_CHECK( ParMETIS_V3_RefineKway(
const_cast< idx_t * >( vertDist.data() ),
const_cast< idx_t * >( graph.getOffsets() ),
const_cast< idx_t * >( graph.getValues() ),
const_cast< idx_t * >( vertexWeights.data() ),
nullptr,
&wgtflag,
&numflag, &ncon, &npart, tpwgts.data(),
&ubvec, options, &edgecut, part.data(), &comm ) );
}
return part;
}
} // namespace parmetis
} // namespace geos