33 #ifndef partition_tools_hpp 34 #define partition_tools_hpp 45 #include <ginkgo/ginkgo.hpp> 46 #include <settings.hpp> 55 namespace PartitionTools {
58 template <
typename ValueType,
typename IndexType>
59 void PartitionRegular(
60 const std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>>
62 const unsigned int &n_partitions,
63 std::vector<unsigned int> &partition_indices)
69 template <
typename ValueType,
typename IndexType>
70 void PartitionRegular2D(
71 const std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>>
73 bool write_debug_out,
const unsigned int &n_partitions,
74 std::vector<unsigned int> &partition_indices)
76 auto n = global_matrix->get_size()[0];
77 int sq_n =
static_cast<int>(std::sqrt(n));
78 int sq_partn =
static_cast<int>(std::sqrt(n_partitions));
82 for (
auto j1 = 0; j1 < sq_partn; ++j1) {
83 offset2 = j1 * sq_partn * std::pow(sq_n / sq_partn, 2);
84 for (
auto j2 = 0; j2 < sq_partn; ++j2) {
85 auto my_id = sq_partn * j1 + j2;
86 offset1 = (j2)*sq_n / sq_partn;
87 for (
auto i1 = 0; i1 < sq_n / sq_partn; ++i1) {
88 for (
auto i2 = 0; i2 < sq_n / sq_partn; ++i2) {
89 partition_indices[offset2 + offset1 + (i1 * sq_n) + i2] =
96 if (write_debug_out) {
98 std::string filename =
"part_indices.csv";
100 file <<
"idx,subd\n";
101 for (
auto i = 0; i < partition_indices.size(); ++i) {
102 file << i <<
"," << partition_indices[i] <<
"\n";
109 template <
typename ValueType,
typename IndexType>
112 const std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>>
114 const std::vector<unsigned int> &cell_weights,
115 const unsigned int &n_partitions,
116 std::vector<unsigned int> &partition_indices)
124 idx_t n =
static_cast<idx_t
>(global_matrix->get_size()[0]);
127 static_cast<idx_t
>(n_partitions);
137 nparts = std::min(n, nparts);
140 idx_t options[METIS_NOPTIONS];
141 SCHWARZ_ASSERT_NO_METIS_ERRORS(METIS_SetDefaultOptions(options));
145 options[METIS_OPTION_OBJTYPE] = METIS_OBJTYPE_CUT;
147 options[METIS_OPTION_OBJTYPE] = METIS_OBJTYPE_VOL;
153 std::vector<idx_t> int_rowstart(n + 1);
154 std::vector<idx_t> int_colnums(global_matrix->get_num_stored_elements());
156 for (
auto i = 0; i < n + 1; ++i) {
157 int_rowstart[i] =
static_cast<idx_t
>(global_matrix->get_row_ptrs()[i]);
159 for (gko::size_type i = 0; i < global_matrix->get_num_stored_elements();
161 int_colnums[i] =
static_cast<idx_t
>(global_matrix->get_col_idxs()[i]);
164 std::vector<idx_t> int_partition_indices(global_matrix->get_size()[0]);
167 std::vector<idx_t> int_cell_weights;
168 if (cell_weights.size() > 0) {
169 SCHWARZ_ASSERT_EQ(cell_weights.size(), global_matrix->get_size()[0]);
170 int_cell_weights.resize(cell_weights.size());
171 std::copy(cell_weights.begin(), cell_weights.end(),
172 int_cell_weights.begin());
177 idx_t *
const p_int_cell_weights =
178 (cell_weights.size() > 0 ? int_cell_weights.data() :
nullptr);
183 SCHWARZ_ASSERT_NO_METIS_ERRORS(METIS_PartGraphRecursive(
184 &n, &ncon, int_rowstart.data(), int_colnums.data(),
186 nullptr,
nullptr,
nullptr, &nparts,
nullptr,
nullptr, options,
187 &dummy, int_partition_indices.data()));
191 SCHWARZ_ASSERT_NO_METIS_ERRORS(METIS_PartGraphKway(
192 &n, &ncon, int_rowstart.data(), int_colnums.data(),
194 nullptr,
nullptr,
nullptr, &nparts,
nullptr,
nullptr, options,
195 &dummy, int_partition_indices.data()));
199 std::copy(int_partition_indices.begin(), int_partition_indices.end(),
200 partition_indices.begin());
205 #define DECLARE_FUNCTION(ValueType, IndexType) \ 206 void PartitionMetis( \ 208 const std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>> &, \ 209 const std::vector<unsigned int> &, const unsigned int &, \ 210 std::vector<unsigned int> &) 211 INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(DECLARE_FUNCTION);
212 #undef DECLARE_FUNCTION 214 #define DECLARE_FUNCTION(ValueType, IndexType) \ 215 void PartitionRegular( \ 216 const std::shared_ptr<gko::matrix::Csr<ValueType, IndexType>> &, \ 217 const unsigned int &, std::vector<unsigned int> &) 218 INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(DECLARE_FUNCTION);
219 #undef DECLARE_FUNCTION 224 #endif // partition_tool_hpp std::string metis_objtype
This setting defines the objective type for the metis partitioning.
Definition: settings.hpp:181
The struct that contains the solver settings and the parameters to be set by the user.
Definition: settings.hpp:77
The Schwarz wrappers namespace.
Definition: comm_helpers.hpp:49