54 #include <Tpetra_Map.hpp>
62 int main(
int argc,
char *argv[])
64 #ifdef HAVE_ZOLTAN2_MPI
65 MPI_Init(&argc, &argv);
67 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
68 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
75 typedef Tpetra::Map<> Map_t;
76 typedef Map_t::local_ordinal_type localId_t;
77 typedef Map_t::global_ordinal_type globalId_t;
79 typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
90 size_t localCount = 40;
93 scalar_t *coords =
new scalar_t [dim * localCount];
96 scalar_t *y = x + localCount;
97 scalar_t *z = y + localCount;
102 scalar_t scalingFactor = 10.0 / RAND_MAX;
104 for (
size_t i=0; i < localCount*dim; i++){
105 coords[i] = scalar_t(rand()) * scalingFactor;
110 globalId_t *globalIds =
new globalId_t [localCount];
111 globalId_t offset = rank * localCount;
113 for (
size_t i=0; i < localCount; i++)
114 globalIds[i] = offset++;
119 double tolerance = 1.1;
122 std::cout <<
"Imbalance tolerance is " << tolerance << std::endl;
124 Teuchos::ParameterList params(
"test params");
125 params.set(
"debug_level",
"basic_status");
126 params.set(
"debug_procs",
"0");
127 params.set(
"error_check_level",
"debug_mode_assertions");
129 params.set(
"algorithm",
"rcb");
130 params.set(
"imbalance_tolerance", tolerance );
131 params.set(
"num_global_parts", nprocs);
141 inputAdapter_t *ia1 =
new inputAdapter_t(localCount,globalIds,x,y,z,1,1,1);
154 quality_t *metricObject1 =
new quality_t(ia1, ¶ms,
159 metricObject1->printMetrics(std::cout);
163 scalar_t imb = metricObject1->getObjectCountImbalance();
164 if (imb <= tolerance)
165 std::cout <<
"pass: " << imb << std::endl;
167 std::cout <<
"fail: " << imb << std::endl;
168 std::cout << std::endl;
170 delete metricObject1;
178 scalar_t *
weights =
new scalar_t [localCount];
179 for (
size_t i=0; i < localCount; i++){
180 weights[i] = 1.0 + scalar_t(rank) / scalar_t(nprocs);
185 std::vector<const scalar_t *>coordVec(2);
186 std::vector<int> coordStrides(2);
188 coordVec[0] = x; coordStrides[0] = 1;
189 coordVec[1] = y; coordStrides[1] = 1;
191 std::vector<const scalar_t *>weightVec(1);
192 std::vector<int> weightStrides(1);
194 weightVec[0] =
weights; weightStrides[0] = 1;
196 inputAdapter_t *ia2=
new inputAdapter_t(localCount, globalIds, coordVec,
197 coordStrides,weightVec,weightStrides);
210 #ifdef HAVE_ZOLTAN2_MPI
211 quality_t *metricObject2 =
new quality_t(ia2, ¶ms,
215 quality_t *metricObject2 =
new quality_t(ia2, ¶ms, problem2->
getComm(),
221 metricObject2->printMetrics(std::cout);
225 scalar_t imb = metricObject2->getWeightImbalance(0);
226 if (imb <= tolerance)
227 std::cout <<
"pass: " << imb << std::endl;
229 std::cout <<
"fail: " << imb << std::endl;
230 std::cout << std::endl;
232 delete metricObject2;
247 params.set(
"partitioning_objective",
"multicriteria_minimize_total_weight");
251 weights =
new scalar_t [localCount*3];
254 for (
size_t i=0; i < localCount*3; i+=3){
255 weights[i] = 1.0 + rank / nprocs;
256 weights[i+1] = rank<nprocs/2 ? 1 : 2;
257 weights[i+2] = rand()/RAND_MAX +.5;
263 weightStrides.resize(3);
265 weightVec[0] =
weights; weightStrides[0] = 3;
266 weightVec[1] = weights+1; weightStrides[1] = 3;
267 weightVec[2] = weights+2; weightStrides[2] = 3;
269 inputAdapter_t *ia3=
new inputAdapter_t(localCount, globalIds, coordVec,
270 coordStrides,weightVec,weightStrides);
283 quality_t *metricObject3 =
new quality_t(ia3, ¶ms, problem3->
getComm(),
288 metricObject3->printMetrics(std::cout);
292 scalar_t imb = metricObject3->getWeightImbalance(0);
293 if (imb <= tolerance)
294 std::cout <<
"pass: " << imb << std::endl;
296 std::cout <<
"fail: " << imb << std::endl;
297 std::cout << std::endl;
299 delete metricObject3;
304 bool dataHasChanged =
false;
306 params.set(
"partitioning_objective",
"multicriteria_minimize_maximum_weight");
308 problem3->
solve(dataHasChanged);
312 metricObject3 =
new quality_t(ia3, ¶ms, problem3->
getComm(),
315 metricObject3->printMetrics(std::cout);
316 scalar_t imb = metricObject3->getWeightImbalance(0);
317 if (imb <= tolerance)
318 std::cout <<
"pass: " << imb << std::endl;
320 std::cout <<
"fail: " << imb << std::endl;
321 std::cout << std::endl;
323 delete metricObject3;
325 params.set(
"partitioning_objective",
"multicriteria_balance_total_maximum");
327 problem3->
solve(dataHasChanged);
331 metricObject3 =
new quality_t(ia3, ¶ms, problem3->
getComm(),
334 metricObject3->printMetrics(std::cout);
335 scalar_t imb = metricObject3->getWeightImbalance(0);
336 if (imb <= tolerance)
337 std::cout <<
"pass: " << imb << std::endl;
339 std::cout <<
"fail: " << imb << std::endl;
340 std::cout << std::endl;
342 delete metricObject3;
358 params.set(
"num_global_parts", nprocs*2);
366 scalar_t partSizes[2];
368 partIds[0] = rank*2; partSizes[0] = 0;
369 partIds[1] = rank*2+1; partSizes[1] = 1;
377 dataHasChanged =
false;
379 problem1->
solve(dataHasChanged);
390 int numInEmptyParts = 0;
391 for (
size_t i=0; i < localCount; i++){
392 if (partAssignments[i] % 2 == 0)
397 std::cout <<
"Request that " << nprocs <<
" parts be empty." <<std::endl;
401 metricObject1 =
new quality_t(ia1, ¶ms,
406 metricObject1->printMetrics(std::cout);
410 scalar_t imb = metricObject1->getObjectCountImbalance();
411 if (imb <= tolerance)
412 std::cout <<
"pass: " << imb << std::endl;
414 std::cout <<
"fail: " << imb << std::endl;
415 std::cout << std::endl;
417 delete metricObject1;
432 #ifdef HAVE_ZOLTAN2_MPI
437 std::cout <<
"PASS" << std::endl;
void setPartSizes(int len, part_t *partIds, scalar_t *partSizes, bool makeCopy=true)
Set or reset relative sizes for the parts that Zoltan2 will create.
int main(int narg, char *arg[])
A simple class that can be the User template argument for an InputAdapter.
void resetParameters(ParameterList *params)
Reset the list of parameters.
Defines the PartitioningSolution class.
SparseMatrixAdapter_t::part_t part_t
A PartitioningSolution is a solution to a partitioning problem.
const part_t * getPartListView() const
Returns the part list corresponding to the global ID list.
RCP< const Comm< int > > getComm()
Return the communicator used by the problem.
BasicVectorAdapter represents a vector (plus optional weights) supplied by the user as pointers to st...
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
Defines the PartitioningProblem class.
A class that computes and returns quality metrics.
Defines the BasicVectorAdapter class.
void solve(bool updateInputData=true)
Direct the problem to create a solution.