18 #include <Tpetra_Map.hpp>
26 int main(
int argc,
char *argv[])
28 Tpetra::ScopeGuard tscope(&argc, &argv);
29 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
31 int rank = comm->getRank();
32 int nprocs = comm->getSize();
36 typedef Tpetra::Map<> Map_t;
37 typedef Map_t::local_ordinal_type localId_t;
38 typedef Map_t::global_ordinal_type globalId_t;
40 typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
51 size_t localCount = 40;
54 scalar_t *coords =
new scalar_t [dim * localCount];
57 scalar_t *y = x + localCount;
58 scalar_t *z = y + localCount;
63 scalar_t scalingFactor = 10.0 / RAND_MAX;
65 for (
size_t i=0; i < localCount*dim; i++){
66 coords[i] = scalar_t(rand()) * scalingFactor;
71 globalId_t *globalIds =
new globalId_t [localCount];
72 globalId_t offset = rank * localCount;
74 for (
size_t i=0; i < localCount; i++)
75 globalIds[i] = offset++;
80 double tolerance = 1.1;
83 std::cout <<
"Imbalance tolerance is " << tolerance << std::endl;
85 Teuchos::ParameterList params(
"test params");
86 params.set(
"debug_level",
"basic_status");
87 params.set(
"debug_procs",
"0");
88 params.set(
"error_check_level",
"debug_mode_assertions");
90 params.set(
"algorithm",
"rcb");
91 params.set(
"imbalance_tolerance", tolerance );
92 params.set(
"num_global_parts", nprocs);
102 inputAdapter_t *ia1 =
new inputAdapter_t(localCount,globalIds,x,y,z,1,1,1);
115 quality_t *metricObject1 =
new quality_t(ia1, ¶ms,
125 if (imb <= tolerance)
126 std::cout <<
"pass: " << imb << std::endl;
128 std::cout <<
"fail: " << imb << std::endl;
129 std::cout << std::endl;
131 delete metricObject1;
139 scalar_t *
weights =
new scalar_t [localCount];
140 for (
size_t i=0; i < localCount; i++){
141 weights[i] = 1.0 + scalar_t(rank) / scalar_t(nprocs);
146 std::vector<const scalar_t *>coordVec(2);
147 std::vector<int> coordStrides(2);
149 coordVec[0] = x; coordStrides[0] = 1;
150 coordVec[1] = y; coordStrides[1] = 1;
152 std::vector<const scalar_t *>weightVec(1);
153 std::vector<int> weightStrides(1);
155 weightVec[0] =
weights; weightStrides[0] = 1;
157 inputAdapter_t *ia2=
new inputAdapter_t(localCount, globalIds, coordVec,
158 coordStrides,weightVec,weightStrides);
171 #ifdef HAVE_ZOLTAN2_MPI
172 quality_t *metricObject2 =
new quality_t(ia2, ¶ms,
182 metricObject2->printMetrics(std::cout);
186 scalar_t imb = metricObject2->getWeightImbalance(0);
187 if (imb <= tolerance)
188 std::cout <<
"pass: " << imb << std::endl;
190 std::cout <<
"fail: " << imb << std::endl;
191 std::cout << std::endl;
193 delete metricObject2;
208 params.set(
"partitioning_objective",
"multicriteria_minimize_total_weight");
212 weights =
new scalar_t [localCount*3];
215 for (
size_t i=0; i < localCount*3; i+=3){
216 weights[i] = 1.0 + rank / nprocs;
217 weights[i+1] = rank<nprocs/2 ? 1 : 2;
218 weights[i+2] = rand()/RAND_MAX +.5;
224 weightStrides.resize(3);
226 weightVec[0] =
weights; weightStrides[0] = 3;
227 weightVec[1] = weights+1; weightStrides[1] = 3;
228 weightVec[2] = weights+2; weightStrides[2] = 3;
230 inputAdapter_t *ia3=
new inputAdapter_t(localCount, globalIds, coordVec,
231 coordStrides,weightVec,weightStrides);
254 if (imb <= tolerance)
255 std::cout <<
"pass: " << imb << std::endl;
257 std::cout <<
"fail: " << imb << std::endl;
258 std::cout << std::endl;
260 delete metricObject3;
265 bool dataHasChanged =
false;
267 params.set(
"partitioning_objective",
"multicriteria_minimize_maximum_weight");
269 problem3->
solve(dataHasChanged);
278 if (imb <= tolerance)
279 std::cout <<
"pass: " << imb << std::endl;
281 std::cout <<
"fail: " << imb << std::endl;
282 std::cout << std::endl;
284 delete metricObject3;
286 params.set(
"partitioning_objective",
"multicriteria_balance_total_maximum");
288 problem3->
solve(dataHasChanged);
297 if (imb <= tolerance)
298 std::cout <<
"pass: " << imb << std::endl;
300 std::cout <<
"fail: " << imb << std::endl;
301 std::cout << std::endl;
303 delete metricObject3;
319 params.set(
"num_global_parts", nprocs*2);
327 scalar_t partSizes[2];
329 partIds[0] = rank*2; partSizes[0] = 0;
330 partIds[1] = rank*2+1; partSizes[1] = 1;
338 dataHasChanged =
false;
340 problem1->
solve(dataHasChanged);
351 int numInEmptyParts = 0;
352 for (
size_t i=0; i < localCount; i++){
353 if (partAssignments[i] % 2 == 0)
358 std::cout <<
"Request that " << nprocs <<
" parts be empty." <<std::endl;
362 metricObject1 =
new quality_t(ia1, ¶ms,
372 if (imb <= tolerance)
373 std::cout <<
"pass: " << imb << std::endl;
375 std::cout <<
"fail: " << imb << std::endl;
376 std::cout << std::endl;
378 delete metricObject1;
394 std::cout <<
"PASS" << std::endl;
void printMetrics(std::ostream &os) const
Print all metrics.
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.
A simple class that can be the User template argument for an InputAdapter.
Zoltan2::EvaluatePartition< matrixAdapter_t > quality_t
int main(int narg, char **arg)
void resetParameters(ParameterList *params)
Reset the list of parameters.
Defines the PartitioningSolution class.
scalar_t getWeightImbalance(int weightIndex) const
Return the imbalance for the requested weight.
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.
scalar_t getObjectCountImbalance() const
Return the object count imbalance.