PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
meshPartitioning.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "meshPartitioning.h"
12#include "mesh.h"
13#include "util/methods.h" // declares std::chrono and defines timeDiff()
14
15#include <metis.h>
16#include <fmt/format.h>
17
18void fe::metisGraphPartition(std::string partitionMethod,
19 const std::vector<std::vector<size_t>> &nodeNeighs,
20 std::vector<size_t> &nodePartition,
21 size_t nPartitions) {
22 // record time
23 auto t1 = steady_clock::now();
24 idx_t nvtxs = nodeNeighs.size();
25 idx_t ncon = 1; // # of balancing constraints (at least 1)
26 idx_t objval;
27 int metis_return;
28 idx_t nWeights = 1;
29 std::vector<idx_t> part(nvtxs, 0);
30 std::vector<idx_t> vwgt(nvtxs * nWeights, 0);
31 auto nParts = idx_t(nPartitions);
32
33 // create adjacency data based on nodeNeighs
34 std::vector<idx_t> xadj(nvtxs, 0);
35 std::vector<idx_t> adjncy;
36 for (size_t i=0; i<nvtxs; i++) {
37 adjncy.insert(adjncy.end(), nodeNeighs[i].begin(), nodeNeighs[i].end());
38 xadj[i+1] = xadj[i] + idx_t(nodeNeighs[i].size());
39 }
40 std::cout << fmt::format("adjcny size = {}, xadj[end] = {}\n",
41 adjncy.size(), xadj[nvtxs]);
42
43 std::cout << "\nmetisGraphPartition():\n";
44 if (partitionMethod == "metis_recursive") {
45 std::cout << " METIS_PartGraphRecursive partitions a graph into K parts\n";
46 std::cout << " using multilevel recursive bisection.\n";
47
48 metis_return = METIS_PartGraphRecursive(&nvtxs, &ncon, xadj.data(),
49 adjncy.data(), NULL, NULL,
50 NULL, &nParts, NULL, NULL, NULL, &objval,
51 part.data());
52 } else if (partitionMethod == "metis_kway") {
53 std::cout << " METIS_PartGraphKway partitions a graph into K parts\n";
54 std::cout << " using multilevel K-way partition.\n";
55
56 metis_return = METIS_PartGraphKway(&nvtxs, &ncon, xadj.data(),
57 adjncy.data(), NULL, NULL,
58 NULL, &nParts, NULL, NULL, NULL, &objval,
59 part.data());
60 } else {
61 std::cerr << "Argument partitionMethod = "
62 << partitionMethod << " is invalid.\n"
63 << "Valid values are {'metis_recursive', 'metis_kway'}.\n";
64 exit(1);
65 }
66
67 // record time
68 auto t2 = steady_clock::now();
69
70 std::cout << fmt::format("\n Return code = {}\n"
71 " Edge cuts for partition = {}\n"
72 " Partition calculation time (ms) = {}\n",
73 metis_return, (int) objval,
74 util::methods::timeDiff(t1, t2, "microseconds"));
75
76 // cast the part vector into nodePartition vector
77 nodePartition.resize(0);
78 nodePartition.insert(nodePartition.end(), part.begin(), part.end());
79}
80
81void fe::metisGraphPartition(std::string partitionMethod,
82 fe::Mesh *mesh_p,
83 const std::vector<std::vector<size_t>> &nodeNeighs,
84 size_t nPartitions) {
85 mesh_p->d_nPart = nPartitions;
86 mesh_p->d_partitionMethod = partitionMethod;
87 fe::metisGraphPartition(partitionMethod, nodeNeighs,
88 mesh_p->d_nodePartition, nPartitions);
89}
A class for mesh data.
Definition mesh.h:51
std::vector< size_t > d_nodePartition
Node partition information. For each node i, d_nodePartition[i] specifies the partition number,...
Definition mesh.h:463
size_t d_nPart
Number of partitions.
Definition mesh.h:449
std::string d_partitionMethod
Partitioning method. It could be either empty string or "metis_recursive" or "metis_kway".
Definition mesh.h:454
void metisGraphPartition(std::string partitionMethod, const std::vector< std::vector< size_t > > &nodeNeighs, std::vector< size_t > &nodePartition, size_t nPartitions)
Partitions the nodes based on node neighborlist supplied. Function first creates a graph with nodes a...
float timeDiff(std::chrono::steady_clock::time_point begin, std::chrono::steady_clock::time_point end, std::string unit="microseconds")
Returns difference between two times.
Definition methods.h:304