56 #include <Teuchos_CommandLineProcessor.hpp> 78 using Teuchos::ArrayView;
79 using Teuchos::ArrayRCP;
80 using Teuchos::CommandLineProcessor;
82 typedef Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t>
tMVector_t;
83 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
tMap_t;
85 static ArrayRCP<ArrayRCP<zscalar_t> >
weights;
93 const string& delimiters =
" \f\n\r\t\v" )
95 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
100 const string& delimiters =
" \f\n\r\t\v" )
102 return s.substr( s.find_first_not_of( delimiters ) );
107 const string& delimiters =
" \f\n\r\t\v" )
114 stringstream stream(stringstream::in | stringstream::out);
115 stream << argumentline;
116 getline(stream, argumentid,
'=');
120 stream >> argumentValue;
126 for(
int i = 0; args[i] != 0; i++)
145 int num_wgts,
float *obj_wgts,
int *ierr)
153 memcpy(gids, idsNonConst,
sizeof(
zgno_t) * localLen);
156 for (
size_t i=0; i < localLen; i++)
157 gids[i] = static_cast<zgno_t>(idsNonConst[i]);
161 float *wgts = obj_wgts;
162 for (
size_t i=0; i < localLen; i++)
163 for (
int w=0; w < num_wgts; w++)
164 *wgts++ = static_cast<float>(
weights[w][i]);
170 int dim,
double *coords,
int *ierr)
174 double *val = coords;
178 for (
zlno_t i=0; i < numObj; i++){
179 *val++ =
static_cast<double>(x[i]);
180 *val++ =
static_cast<double>(y[i]);
181 *val++ =
static_cast<double>(z[i]);
195 const RCP<
const Teuchos::Comm<int> > & comm,
202 ArrayRCP<zscalar_t> wgtArray(wgts, 0, len,
true);
206 for (
zlno_t i=0; i < len; i++)
210 for (
int i=0; i < 10; i++){
212 for (
int j=i; j < len; j += 10)
218 for (
zlno_t i=0; i < len; i++)
230 const RCP<
const Teuchos::Comm<int> > & comm,
233 int rank = comm->getRank();
234 int nprocs = comm->getSize();
236 double k = log(numGlobalCoords) / 3;
237 double xdimf = exp(k) + 0.5;
238 zgno_t xdim =
static_cast<int>(floor(xdimf));
240 zgno_t zdim = numGlobalCoords / (xdim*ydim);
241 zgno_t num=xdim*ydim*zdim;
242 zgno_t diff = numGlobalCoords - num;
246 if (zdim > xdim && zdim > ydim){
248 newdiff = diff - (xdim*ydim);
253 else if (ydim > xdim && ydim > zdim){
255 newdiff = diff - (xdim*zdim);
262 newdiff = diff - (ydim*zdim);
272 diff = numGlobalCoords - num;
274 diff /= -numGlobalCoords;
276 diff /= numGlobalCoords;
280 cout <<
"Warning: Difference " << diff*100 <<
" percent" << endl;
281 cout <<
"Mesh size: " << xdim <<
"x" << ydim <<
"x" <<
282 zdim <<
", " << num <<
" vertices." << endl;
287 zgno_t numLocalCoords = num / nprocs;
288 zgno_t leftOver = num % nprocs;
291 if (rank <= leftOver)
292 gid0 =
zgno_t(rank) * (numLocalCoords+1);
294 gid0 = (leftOver * (numLocalCoords+1)) +
295 ((
zgno_t(rank) - leftOver) * numLocalCoords);
300 zgno_t gid1 = gid0 + numLocalCoords;
305 ArrayRCP<zgno_t> idArray(ids, 0, numLocalCoords,
true);
307 for (
zgno_t i=gid0; i < gid1; i++)
310 RCP<const tMap_t> idMap = rcp(
311 new tMap_t(num, idArray.view(0, numLocalCoords), 0, comm));
318 ArrayRCP<zscalar_t> coordArray(x, 0, numLocalCoords*3,
true);
325 zgno_t xyPlane = xdim*ydim;
326 zgno_t zStart = gid0 / xyPlane;
327 zgno_t rem = gid0 % xyPlane;
334 for (
zscalar_t zval=zStart; next < numLocalCoords && zval < zdim; zval++){
335 for (
zscalar_t yval=yStart; next < numLocalCoords && yval < ydim; yval++){
336 for (
zscalar_t xval=xStart; next < numLocalCoords && xval < xdim; xval++){
347 ArrayView<const zscalar_t> xArray(x, numLocalCoords);
348 ArrayView<const zscalar_t> yArray(y, numLocalCoords);
349 ArrayView<const zscalar_t> zArray(z, numLocalCoords);
350 ArrayRCP<ArrayView<const zscalar_t> >
coordinates =
351 arcp(
new ArrayView<const zscalar_t> [3], 0, 3);
352 coordinates[0] = xArray;
353 coordinates[1] = yArray;
354 coordinates[2] = zArray;
356 ArrayRCP<const ArrayView<const zscalar_t> > constCoords =
357 coordinates.getConst();
359 RCP<tMVector_t> meshCoords = rcp(
new tMVector_t(
360 idMap, constCoords.view(0,3), 3));
367 std::string ¶mFile){
369 for(
int i = 0; i < argc; ++i){
371 string identifier =
"";
372 long long int value = -1;
double fval = -1;
374 value = (
long long int) (fval);
375 if(identifier ==
"C"){
379 throw "Invalid argument at " + tmp;
382 else if(identifier ==
"PF"){
383 stringstream stream(stringstream::in | stringstream::out);
385 getline(stream, paramFile,
'=');
389 throw "Invalid argument at " + tmp;
396 void readGeoGenParams(
string paramFileName, Teuchos::ParameterList &geoparams,
const RCP<
const Teuchos::Comm<int> > & comm){
397 std::string input =
"";
399 for(
int i = 0; i < 25000; ++i){
402 if(comm->getRank() == 0){
403 fstream inParam(paramFileName.c_str());
405 std::string tmp =
"";
406 getline (inParam,tmp);
407 while (!inParam.eof()){
414 getline (inParam,tmp);
417 for (
size_t i = 0; i < input.size(); ++i){
423 int size = input.size();
429 comm->broadcast(0,
sizeof(
int), (
char*) &size);
430 comm->broadcast(0, size, inp);
432 istringstream inParam(inp);
434 getline (inParam,str);
435 while (!inParam.eof()){
437 size_t pos = str.find(
'=');
438 if(pos == string::npos){
439 throw "Invalid Line:" + str +
" in parameter file";
441 string paramname =
trim_copy(str.substr(0,pos));
442 string paramvalue =
trim_copy(str.substr(pos + 1));
443 geoparams.set(paramname, paramvalue);
445 getline (inParam,str);
449 int main(
int argc,
char *argv[])
453 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
454 RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
455 int rank = comm->getRank();
456 int nprocs = comm->getSize();
458 MEMORY_CHECK(rank==0 || rank==nprocs-1,
"After initializing MPI");
461 cout <<
"Number of processes: " << nprocs << endl;
467 string memoryOn(
"memoryOn");
468 string memoryOff(
"memoryOff");
471 int numGlobalParts = 512 * 512;
472 string paramFile =
"p2.txt";
479 Teuchos::ParameterList geoparams(
"geo params");
482 #ifdef HAVE_ZOLTAN2_OMP 483 double begin = omp_get_wtime();
485 GeometricGenerator<zscalar_t, zlno_t, zgno_t, znode_t> *gg =
new GeometricGenerator<zscalar_t, zlno_t, zgno_t, znode_t>(geoparams,comm);
486 #ifdef HAVE_ZOLTAN2_OMP 487 double end = omp_get_wtime();
488 cout <<
"GeometricGen Time:" << end - begin << endl;
490 int coord_dim = gg->getCoordinateDimension();
491 int nWeights = gg->getNumWeights();
492 zlno_t numLocalPoints = gg->getNumLocalCoords();
493 zgno_t numGlobalPoints = gg->getNumGlobalCoords();
495 for(
int i = 0; i < coord_dim; ++i){
496 coords[i] =
new zscalar_t[numLocalPoints];
498 gg->getLocalCoordinatesCopy(coords);
502 for(
int i = 0; i < nWeights; ++i){
503 weight[i] =
new zscalar_t[numLocalPoints];
505 gg->getLocalWeightsCopy(weight);
508 zgno_t globalSize =
static_cast<zgno_t>(numGlobalPoints);
511 cout <<
"coord_dim:" << coord_dim <<
" nWeights:" << nWeights <<
" numLocalPoints:" << numLocalPoints <<
" numGlobalPoints:" << numGlobalPoints << endl;
513 CommandLineProcessor commandLine(
false,
true);
514 commandLine.setOption(
"size", &numGlobalPoints,
515 "Approximate number of global coordinates.");
516 commandLine.setOption(
"numParts", &numGlobalParts,
517 "Number of parts (default is one per proc).");
518 commandLine.setOption(
"numWeights", &nWeights,
519 "Number of weights per coordinate, zero implies uniform weights.");
520 commandLine.setOption(
"debug", &debugLevel,
"Zoltan1 debug level");
521 commandLine.setOption(
"timers", &dummyTimer,
"ignored");
522 commandLine.setOption(memoryOn.c_str(), memoryOff.c_str(), &doMemory,
523 "do memory profiling");
525 string balanceCount(
"balance_object_count");
526 string balanceWeight(
"balance_object_weight");
527 string mcnorm1(
"multicriteria_minimize_total_weight");
528 string mcnorm2(
"multicriteria_balance_total_maximum");
529 string mcnorm3(
"multicriteria_minimize_maximum_weight");
531 string objective(balanceWeight);
533 string doc(balanceCount);
534 doc.append(
": ignore weights\n");
536 doc.append(balanceWeight);
537 doc.append(
": balance on first weight\n");
540 doc.append(
": given multiple weights, balance their total.\n");
543 doc.append(
": given multiple weights, balance the maximum for each coordinate.\n");
546 doc.append(
": given multiple weights, balance the L2 norm of the weights.\n");
548 commandLine.setOption(
"objective", &objective, doc.c_str());
550 CommandLineProcessor::EParseCommandLineReturn rc =
551 commandLine.parse(argc, argv);
553 if (rc != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL){
554 if (rc == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED){
556 cout <<
"PASS" << endl;
561 cout <<
"FAIL" << endl;
571 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp = rcp(
572 new Tpetra::Map<zlno_t, zgno_t, znode_t> (numGlobalPoints, numLocalPoints, 0, comm));
574 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
575 for (
int i=0; i < coord_dim; i++){
576 if(numLocalPoints > 0){
577 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
580 Teuchos::ArrayView<const zscalar_t> a;
585 coordinates = RCP< Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t> >(
586 new Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t>( mp, coordView.view(0, coord_dim), coord_dim));
595 size_t numLocalCoords =
coordinates->getLocalLength();
599 for (
int p=0; p < nprocs; p++){
601 cout <<
"Rank " << rank <<
", " << numLocalCoords <<
"coords" << endl;
605 for (
zlno_t i=0; i < numLocalCoords; i++)
606 cout <<
" " << x[i] <<
" " << y[i] <<
" " << z[i] << endl;
615 weights = arcp(
new ArrayRCP<zscalar_t> [nWeights],
617 for(
int i = 0; i < nWeights; ++i){
622 MEMORY_CHECK(doMemory && rank==0,
"After creating input");
627 int aok = Zoltan_Initialize(argc, argv, &ver);
630 printf(
"sorry...\n");
634 struct Zoltan_Struct *zz;
635 zz = Zoltan_Create(MPI_COMM_WORLD);
637 Zoltan_Set_Param(zz,
"LB_METHOD",
"RCB");
638 Zoltan_Set_Param(zz,
"LB_APPROACH",
"PARTITION");
639 Zoltan_Set_Param(zz,
"CHECK_GEOM",
"0");
640 Zoltan_Set_Param(zz,
"NUM_GID_ENTRIES",
"1");
641 Zoltan_Set_Param(zz,
"NUM_LID_ENTRIES",
"0");
642 Zoltan_Set_Param(zz,
"RETURN_LISTS",
"ALL");
645 Zoltan_Set_Param(zz,
"IMBALANCE_TOL",
"1.0");
646 std::ostringstream oss;
647 oss << numGlobalParts;
648 Zoltan_Set_Param(zz,
"NUM_GLOBAL_PARTS", oss.str().c_str());
651 Zoltan_Set_Param(zz,
"DEBUG_LEVEL", oss.str().c_str());
653 if (objective != balanceCount){
656 Zoltan_Set_Param(zz,
"OBJ_WEIGHT_DIM", oss.str().c_str());
658 if (objective == mcnorm1)
659 Zoltan_Set_Param(zz,
"RCB_MULTICRITERIA_NORM",
"1");
660 else if (objective == mcnorm2)
661 Zoltan_Set_Param(zz,
"RCB_MULTICRITERIA_NORM",
"2");
662 else if (objective == mcnorm3)
663 Zoltan_Set_Param(zz,
"RCB_MULTICRITERIA_NORM",
"3");
666 Zoltan_Set_Param(zz,
"OBJ_WEIGHT_DIM",
"0");
669 Zoltan_Set_Num_Obj_Fn(zz,
getNumObj, NULL);
671 Zoltan_Set_Num_Geom_Fn(zz,
getDim, NULL);
672 Zoltan_Set_Geom_Multi_Fn(zz,
getCoords, NULL);
674 int changes, numGidEntries, numLidEntries, numImport, numExport;
675 zgno_t * importGlobalGids, * importLocalGids;
676 zgno_t * exportGlobalGids, * exportLocalGids;
677 int *importProcs, *importToPart, *exportProcs, *exportToPart;
679 MEMORY_CHECK(doMemory && rank==0,
"Before Zoltan_LB_Partition");
682 aok = Zoltan_LB_Partition(zz,
697 MEMORY_CHECK(doMemory && rank==0,
"After Zoltan_LB_Partition");
698 Zoltan_LB_Free_Part(importGlobalGids, importLocalGids, importProcs, importToPart);
699 Zoltan_LB_Free_Part(exportGlobalGids, exportLocalGids, exportProcs, exportToPart);
701 MEMORY_CHECK(doMemory && rank==0,
"After Zoltan_Destroy");
705 std::cout <<
"FAIL" << std::endl;
707 std::cout <<
"PASS" << std::endl;
Defines the PartitioningSolution class.
common code used by tests
Defines the XpetraMultiVectorAdapter.
Defines the EvaluatePartition class.
Defines the PartitioningProblem class.
#define MEMORY_CHECK(iPrint, msg)