28 #include "Teuchos_RCP.hpp"
29 #include "Teuchos_XMLParameterListHelpers.hpp"
30 #include "Teuchos_Hashtable.hpp"
33 #ifdef HAVE_ZOLTAN2_PARMA
45 using Teuchos::ParameterList;
47 using Teuchos::ArrayView;
50 template <
typename Adapter>
54 typedef typename Adapter::scalar_t scalar_t;
55 typedef typename Adapter::offset_t offset_t;
58 ArrayView<const gno_t> Ids;
59 ArrayView<input_t> wgts;
61 ArrayView<bool> isOwner;
64 ArrayView<const gno_t> pins;
65 ArrayView<const offset_t> offsets;
73 std::set<gno_t> ghosts;
77 if (gids.find(pin)==gids.end()) {
79 if (ghosts.find(pin)==ghosts.end())
83 std::cout<<
"[METRIC] " << PCU_Comm_Self() <<
" Total number of ghosts in the hypergraph: " << num_ghosts <<
"\n"
84 <<
"[METRIC] " << PCU_Comm_Self() <<
" Number of unique ghosts: " << ghosts.size() <<
"\n";
85 gno_t unique_ghosts =ghosts.size();
86 gno_t owned_and_ghosts =unique_ghosts+numOwned;
87 gno_t max_o_and_g,min_o_and_g;
88 gno_t max_ghosts,max_u_ghosts;
89 gno_t min_ghosts,min_u_ghosts;
90 max_ghosts = min_ghosts = num_ghosts;
91 max_u_ghosts = min_u_ghosts = unique_ghosts;
92 max_o_and_g = min_o_and_g = owned_and_ghosts;
93 double avg_ghosts,avg_u_ghosts,avg_o_and_g;
94 PCU_Add_Ints(&num_ghosts,1);
95 PCU_Add_Ints(&unique_ghosts,1);
96 PCU_Add_Ints(&owned_and_ghosts,1);
97 PCU_Max_Ints(&max_ghosts,1);
98 PCU_Max_Ints(&max_u_ghosts,1);
99 PCU_Max_Ints(&max_o_and_g,1);
100 PCU_Min_Ints(&min_ghosts,1);
101 PCU_Min_Ints(&min_u_ghosts,1);
102 PCU_Min_Ints(&min_o_and_g,1);
103 avg_ghosts = num_ghosts*1.0/PCU_Comm_Peers();
104 avg_u_ghosts = unique_ghosts*1.0/PCU_Comm_Peers();
105 avg_o_and_g = owned_and_ghosts*1.0/PCU_Comm_Peers();
106 if (!PCU_Comm_Self())
107 std::cout<<
"[METRIC] Global ghosts in the hypergraph (tot max min avg imb): "
108 << num_ghosts<<
" "<<max_ghosts<<
" "<<min_ghosts<<
" "<<avg_ghosts<<
" "
109 <<max_ghosts/avg_ghosts <<
"\n"
110 <<
"[METRIC] Global unique ghosts (tot max min avg imb): "
111 << unique_ghosts<<
" "<<max_u_ghosts<<
" "<<min_u_ghosts<<
" "<<avg_u_ghosts<<
" "
112 <<max_u_ghosts/avg_u_ghosts <<
"\n"
113 <<
"[METRIC] Global owned and ghosts (tot max min avg imb): "
114 << owned_and_ghosts<<
" "<<max_o_and_g<<
" "<<min_o_and_g<<
" "<<avg_o_and_g<<
" "
115 <<max_o_and_g/avg_o_and_g <<
"\n";
123 int main(
int narg,
char *arg[]) {
125 Tpetra::ScopeGuard tscope(&narg, &arg);
126 Teuchos::RCP<const Teuchos::Comm<int> > CommT = Tpetra::getDefaultComm();
128 int me = CommT->getRank();
133 <<
"====================================================================\n"
135 <<
"| Example: Partition APF Mesh |\n"
137 <<
"| Questions? Contact Karen Devine (kddevin@sandia.gov), |\n"
138 <<
"| Erik Boman (egboman@sandia.gov), |\n"
139 <<
"| Siva Rajamanickam (srajama@sandia.gov). |\n"
141 <<
"| Zoltan2's website: http://trilinos.sandia.gov/packages/zoltan2 |\n"
142 <<
"| Trilinos website: http://trilinos.sandia.gov |\n"
144 <<
"====================================================================\n";
150 std::cout <<
"PARALLEL executable \n";
154 std::cout <<
"SERIAL executable \n";
163 std::string meshFileName(
"4/");
164 std::string modelFileName(
"torus.dmg");
165 std::string action(
"parma");
166 std::string parma_method(
"VtxElm");
167 std::string output_loc(
"");
168 int nParts = CommT->getSize();
169 double imbalance = 1.1;
171 int ghost_metric=
false;
173 Teuchos::CommandLineProcessor cmdp (
false,
false);
174 cmdp.setOption(
"meshfile", &meshFileName,
175 "Mesh file with APF specifications (.smb file(s))");
176 cmdp.setOption(
"modelfile", &modelFileName,
177 "Model file with APF specifications (.dmg file)");
178 cmdp.setOption(
"action", &action,
179 "Method to use: mj, scotch, zoltan_rcb, parma or color");
180 cmdp.setOption(
"parma_method", &parma_method,
181 "Method to use: Vertex, Element, VtxElm, VtxEdgeElm, Ghost, Shape, or Centroid ");
182 cmdp.setOption(
"nparts", &nParts,
183 "Number of parts to create");
184 cmdp.setOption(
"imbalance", &imbalance,
185 "Target imbalance for the partitioning method");
186 cmdp.setOption(
"output", &output_loc,
187 "Location of new partitioned apf mesh. Ex: 4/torus.smb");
188 cmdp.setOption(
"layers", &layers,
189 "Number of layers for ghosting");
190 cmdp.setOption(
"ghost_metric", &ghost_metric,
191 "0 does not compute ghost metric otherwise compute both before and after");
192 cmdp.parse(narg, arg);
205 #ifdef HAVE_ZOLTAN2_PARMA
207 if (me == 0) std::cout <<
"Generating mesh ... \n\n";
214 apf::Mesh2* m = apf::loadMdsMesh(modelFileName.c_str(),meshFileName.c_str());
218 std::string primary=
"region";
219 std::string adjacency=
"face";
220 if (m->getDimension()==2) {
224 bool needSecondAdj=
false;
227 if (me == 0) std::cout <<
"Creating parameter list ... \n\n";
229 Teuchos::ParameterList params(
"test params");
230 params.set(
"timer_output_stream" ,
"std::cout");
232 bool do_partitioning =
false;
233 if (action ==
"mj") {
234 do_partitioning =
true;
235 params.set(
"debug_level",
"basic_status");
236 params.set(
"imbalance_tolerance", imbalance);
237 params.set(
"num_global_parts", nParts);
238 params.set(
"algorithm",
"multijagged");
239 params.set(
"rectilinear",
"yes");
241 else if (action ==
"scotch") {
242 do_partitioning =
true;
243 params.set(
"debug_level",
"no_status");
244 params.set(
"imbalance_tolerance", imbalance);
245 params.set(
"num_global_parts", nParts);
246 params.set(
"partitioning_approach",
"partition");
247 params.set(
"objects_to_partition",
"mesh_elements");
248 params.set(
"algorithm",
"scotch");
251 else if (action ==
"zoltan_rcb") {
252 do_partitioning =
true;
253 params.set(
"debug_level",
"verbose_detailed_status");
254 params.set(
"imbalance_tolerance", imbalance);
255 params.set(
"num_global_parts", nParts);
256 params.set(
"partitioning_approach",
"partition");
257 params.set(
"algorithm",
"zoltan");
259 else if (action ==
"parma") {
260 do_partitioning =
true;
261 params.set(
"debug_level",
"no_status");
262 params.set(
"imbalance_tolerance", imbalance);
263 params.set(
"algorithm",
"parma");
264 Teuchos::ParameterList &pparams = params.sublist(
"parma_parameters",
false);
265 pparams.set(
"parma_method",parma_method);
266 pparams.set(
"step_size",1.1);
267 if (parma_method==
"Ghost") {
268 pparams.set(
"ghost_layers",layers);
269 pparams.set(
"ghost_bridge",m->getDimension()-1);
273 else if (action==
"zoltan_hg") {
274 do_partitioning =
true;
275 params.set(
"debug_level",
"no_status");
276 params.set(
"imbalance_tolerance", imbalance);
277 params.set(
"algorithm",
"zoltan");
278 params.set(
"num_global_parts", nParts);
279 Teuchos::ParameterList &zparams = params.sublist(
"zoltan_parameters",
false);
280 zparams.set(
"LB_METHOD",
"HYPERGRAPH");
281 zparams.set(
"LB_APPROACH",
"PARTITION");
285 else if (action==
"hg_ghost") {
286 do_partitioning =
true;
287 params.set(
"debug_level",
"no_status");
288 params.set(
"imbalance_tolerance", imbalance);
289 params.set(
"algorithm",
"zoltan");
290 params.set(
"num_global_parts", nParts);
291 params.set(
"hypergraph_model_type",
"ghosting");
292 params.set(
"ghost_layers",layers);
293 Teuchos::ParameterList &zparams = params.sublist(
"zoltan_parameters",
false);
294 zparams.set(
"LB_METHOD",
"HYPERGRAPH");
295 zparams.set(
"LB_APPROACH",
"PARTITION");
296 zparams.set(
"PHG_EDGE_SIZE_THRESHOLD",
"1.0");
301 else if (action ==
"color") {
302 params.set(
"debug_level",
"verbose_detailed_status");
303 params.set(
"debug_output_file",
"kdd");
304 params.set(
"debug_procs",
"all");
306 Parma_PrintPtnStats(m,
"before");
309 if (me == 0) std::cout <<
"Creating mesh adapter ... \n\n";
314 double time_1=PCU_Time();
316 new inputAdapter_t(*CommT, m,primary,adjacency,needSecondAdj);
317 double time_2=PCU_Time();
320 inputAdapter_t::scalar_t* arr =
321 new inputAdapter_t::scalar_t[ia->getLocalNumOf(ia->getPrimaryEntityType())];
322 for (
size_t i=0;i<ia->getLocalNumOf(ia->getPrimaryEntityType());i++) {
323 arr[i]=PCU_Comm_Self()+1;
326 const inputAdapter_t::scalar_t*
weights=arr;
327 ia->setWeights(ia->getPrimaryEntityType(),
weights,1);
331 const baseMeshAdapter_t *base_ia =
dynamic_cast<const baseMeshAdapter_t*
>(ia);
333 RCP<Zoltan2::Environment> env;
339 RCP<const Zoltan2::Environment> envConst = Teuchos::rcp_const_cast<
const Zoltan2::Environment>(env);
341 RCP<const baseMeshAdapter_t> baseInputAdapter_(base_ia,
false);
348 double time_3 = PCU_Time();
349 if (do_partitioning) {
350 if (me == 0) std::cout <<
"Creating partitioning problem ... \n\n";
355 if (me == 0) std::cout <<
"Calling the partitioner ... \n\n";
361 if (me==0) std::cout <<
"Applying Solution to Mesh\n\n";
362 apf::Mesh2** new_mesh = &m;
363 ia->applyPartitioningSolution(m,new_mesh,problem.
getSolution());
366 RCP<quality_t> metricObject =
370 metricObject->printMetrics(std::cout);
374 if (me == 0) std::cout <<
"Creating coloring problem ... \n\n";
379 if (me == 0) std::cout <<
"Calling the coloring algorithm ... \n\n";
388 double time_4=PCU_Time();
396 inputAdapter_t ia2(*CommT, m,primary,adjacency,
true);
397 const baseMeshAdapter_t *base_ia =
dynamic_cast<const baseMeshAdapter_t*
>(&ia2);
400 RCP<Zoltan2::Environment> env;
405 RCP<const Zoltan2::Environment> envConst = Teuchos::rcp_const_cast<
const Zoltan2::Environment>(env);
406 RCP<const baseMeshAdapter_t> baseInputAdapter_(base_ia,
false);
414 if (output_loc!=
"") {
415 m->writeNative(output_loc.c_str());
419 if (me == 0) std::cout <<
"Deleting the mesh ... \n\n";
422 PCU_Max_Doubles(&time_2,1);
423 PCU_Max_Doubles(&time_4,1);
425 std::cout<<
"\nConstruction time: "<<time_2<<
"\n"
426 <<
"Problem time: " << time_4<<
"\n\n";
436 std::cout <<
"PASS" << std::endl;
ColoringProblem sets up coloring problems for the user.
Defines the ColoringProblem class.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
MeshAdapter defines the interface for mesh input.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
map_t::global_ordinal_type gno_t
Zoltan2::EvaluatePartition< matrixAdapter_t > quality_t
int main(int narg, char **arg)
Defines the APFMeshAdapter class.
size_t getOwnedList(ArrayView< bool > &isOwner) const
Sets pointer to the ownership of this processes vertices.
size_t getEdgeList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process' hyperedge Ids and their weights.
void PrintGhostMetrics(Zoltan2::HyperGraphModel< Adapter > &mdl)
The StridedData class manages lists of weights or coordinates.
map_t::local_ordinal_type lno_t
void printTimers() const
Return the communicator passed to the problem.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
size_t getLocalNumPins() const
Returns the local number of pins.
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
Defines the HyperGraphModel interface.
Defines the Environment class.
size_t getPinList(ArrayView< const gno_t > &pinIds, ArrayView< const offset_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process' pins global Ids based on the centric view given by getCentricView() ...
size_t getLocalNumVertices() const
Returns the number vertices on this process.
Defines the PartitioningProblem class.
A class that computes and returns quality metrics.
HyperGraphModel defines the interface required for hyper graph models.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
size_t getLocalNumOwnedVertices() const
Returns the number vertices on this process that are owned.