50 #ifndef USERINPUTFORTESTS 51 #define USERINPUTFORTESTS 57 #include <Tpetra_MultiVector.hpp> 58 #include <Tpetra_CrsMatrix.hpp> 59 #include <Tpetra_Map.hpp> 60 #include <Xpetra_Vector.hpp> 61 #include <Xpetra_CrsMatrix.hpp> 62 #include <Xpetra_CrsGraph.hpp> 64 #include <MatrixMarket_Tpetra.hpp> 66 #ifdef HAVE_ZOLTAN2_GALERI 67 #include <Galeri_XpetraProblemFactory.hpp> 68 #include <Galeri_XpetraParameters.hpp> 71 #include <Kokkos_DefaultNode.hpp> 77 #include <TpetraExt_MatrixMatrix_def.hpp> 84 using Teuchos::ArrayRCP;
85 using Teuchos::ArrayView;
90 using Teuchos::rcp_const_cast;
91 using Teuchos::ParameterList;
131 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
132 typedef Tpetra::Export<zlno_t, zgno_t, znode_t>
export_t;
133 typedef Tpetra::Import<zlno_t, zgno_t, znode_t>
import_t;
157 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
158 bool distributeInput=
true);
177 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
178 bool distributeInput=
true);
196 const RCP<
const Comm<int> > &c);
200 static void getUIRandomData(
unsigned int seed,
zlno_t length,
203 RCP<tMVector_t> getUICoordinates();
205 RCP<tMVector_t> getUIWeights();
207 RCP<tMVector_t> getUIEdgeWeights();
209 RCP<tcrsMatrix_t> getUITpetraCrsMatrix();
211 RCP<tcrsGraph_t> getUITpetraCrsGraph();
213 RCP<tVector_t> getUITpetraVector();
215 RCP<tMVector_t> getUITpetraMultiVector(
int nvec);
217 RCP<xcrsMatrix_t> getUIXpetraCrsMatrix();
219 RCP<xcrsGraph_t> getUIXpetraCrsGraph();
221 RCP<xVector_t> getUIXpetraVector();
223 RCP<xMVector_t> getUIXpetraMultiVector(
int nvec);
225 #ifdef HAVE_ZOLTAN2_PAMGEN 226 PamgenMesh * getPamGenMesh(){
return this->pamgen_mesh.operator->();}
229 #ifdef HAVE_EPETRA_DATA_TYPES 230 RCP<Epetra_CrsGraph> getUIEpetraCrsGraph();
232 RCP<Epetra_CrsMatrix> getUIEpetraCrsMatrix();
234 RCP<Epetra_Vector> getUIEpetraVector();
236 RCP<Epetra_MultiVector> getUIEpetraMultiVector(
int nvec);
240 bool hasInputDataType(
const string &input_type);
242 bool hasUICoordinates();
246 bool hasUIEdgeWeights();
248 bool hasUITpetraCrsMatrix();
250 bool hasUITpetraCrsGraph();
252 bool hasUITpetraVector();
254 bool hasUITpetraMultiVector();
256 bool hasUIXpetraCrsMatrix();
258 bool hasUIXpetraCrsGraph();
260 bool hasUIXpetraVector();
262 bool hasUIXpetraMultiVector();
264 bool hasPamgenMesh();
265 #ifdef HAVE_EPETRA_DATA_TYPES 266 bool hasUIEpetraCrsGraph();
268 bool hasUIEpetraCrsMatrix();
270 bool hasUIEpetraVector();
272 bool hasUIEpetraMultiVector();
280 const RCP<const Comm<int> > tcomm_;
283 #ifdef HAVE_ZOLTAN2_PAMGEN 284 RCP<PamgenMesh> pamgen_mesh;
287 RCP<tcrsMatrix_t> M_;
288 RCP<xcrsMatrix_t> xM_;
290 RCP<tMVector_t> xyz_;
291 RCP<tMVector_t> vtxWeights_;
292 RCP<tMVector_t> edgWeights_;
294 #ifdef HAVE_EPETRA_DATA_TYPES 295 RCP<const Epetra_Comm> ecomm_;
296 RCP<Epetra_CrsMatrix> eM_;
297 RCP<Epetra_CrsGraph> eG_;
305 void readMatrixMarketFile(
string path,
string testData,
bool distributeInput =
true);
310 void buildCrsMatrix(
int xdim,
int ydim,
int zdim,
string type,
311 bool distributeInput);
317 void readZoltanTestData(
string path,
string testData,
318 bool distributeInput);
321 void getUIChacoGraph(FILE *fptr,
bool haveAssign, FILE *assignFile,
322 string name,
bool distributeInput);
325 void getUIChacoCoords(FILE *fptr,
string name);
331 static const int CHACO_LINE_LENGTH=200;
332 char chaco_line[CHACO_LINE_LENGTH];
337 double chaco_read_val(FILE* infile,
int *end_flag);
338 int chaco_read_int(FILE* infile,
int *end_flag);
339 void chaco_flush_line(FILE*);
342 int chaco_input_graph(FILE *fin,
const char *inname,
int **start,
343 int **adjacency,
int *nvtxs,
int *nVwgts,
344 float **vweights,
int *nEwgts,
float **eweights);
347 int chaco_input_geom(FILE *fingeom,
const char *geomname,
int nvtxs,
348 int *igeom,
double **x,
double **y,
double **z);
351 int chaco_input_assign(FILE *finassign,
const char *assignname,
int nvtxs,
359 void readGeometricGenTestData(
string path,
string testData);
363 ParameterList &geoparams);
368 const string& delimiters =
" \f\n\r\t\v" );
371 const string& delimiters =
" \f\n\r\t\v" );
374 const string& delimiters =
" \f\n\r\t\v" );
378 void readPamgenMeshFile(
string path,
string testData);
379 #ifdef HAVE_ZOLTAN2_PAMGEN 380 void setPamgenAdjacencyGraph();
381 void setPamgenCoordinateMV();
386 const RCP<
const Comm<int> > &c,
387 bool debugInfo,
bool distributeInput):
388 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
389 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
391 ecomm_(), eM_(), eG_(),
393 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
395 bool zoltan1 =
false;
396 string::size_type loc = path.find(
"/zoltan/test/");
397 if (loc != string::npos)
401 readZoltanTestData(path, testData, distributeInput);
403 readMatrixMarketFile(path, testData);
405 #ifdef HAVE_EPETRA_DATA_TYPES 406 ecomm_ = Xpetra::toEpetra(c);
412 const RCP<
const Comm<int> > &c,
414 bool distributeInput):
415 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
416 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
418 ecomm_(), eM_(), eG_(),
420 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
422 if (matrixType.size() == 0){
428 matrixType = string(
"Laplace1D");
430 matrixType = string(
"Laplace2D");
432 matrixType = string(
"Laplace3D");
434 throw std::runtime_error(
"input");
436 if (verbose_ && tcomm_->getRank() == 0)
437 std::cout <<
"UserInputForTests, Matrix type : " << matrixType << std::endl;
440 buildCrsMatrix(x, y, z, matrixType, distributeInput);
442 #ifdef HAVE_EPETRA_DATA_TYPES 443 ecomm_ = Xpetra::toEpetra(c);
448 const RCP<
const Comm<int> > &c):
449 tcomm_(c), havePamgenMesh(false),
450 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
452 ecomm_(), eM_(), eG_(),
454 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
458 bool distributeInput =
true, debugInfo =
true;
460 if(pList.isParameter(
"distribute input"))
461 distributeInput = pList.get<
bool>(
"distribute input");
463 if(pList.isParameter(
"debug"))
464 debugInfo = pList.get<
bool>(
"debug");
465 this->verbose_ = debugInfo;
467 if(pList.isParameter(
"input file"))
472 if(pList.isParameter(
"input path"))
473 path = pList.get<
string>(
"input path");
475 string testData = pList.get<
string>(
"input file");
481 if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Geometric Generator")
483 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Pamgen")
487 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Chaco")
491 switch (file_format) {
492 case GEOMGEN: readGeometricGenTestData(path,testData);
break;
493 case PAMGEN: readPamgenMeshFile(path,testData);
break;
494 case CHACO: readZoltanTestData(path, testData, distributeInput);
break;
495 default: readMatrixMarketFile(path, testData, distributeInput);
break;
498 }
else if(pList.isParameter(
"x") || pList.isParameter(
"y") || pList.isParameter(
"z")){
502 if(pList.isParameter(
"x")) x = pList.get<
int>(
"x");
503 if(pList.isParameter(
"y")) y = pList.get<
int>(
"y");
504 if(pList.isParameter(
"z")) z = pList.get<
int>(
"z");
506 string problemType =
"";
507 if(pList.isParameter(
"equation type")) problemType = pList.get<
string>(
"equation type");
509 if (problemType.size() == 0){
515 problemType = string(
"Laplace1D");
517 problemType = string(
"Laplace2D");
519 problemType = string(
"Laplace3D");
521 throw std::runtime_error(
"input");
523 if (verbose_ && tcomm_->getRank() == 0)
524 std::cout <<
"UserInputForTests, Matrix type : " << problemType << std::endl;
528 buildCrsMatrix(x, y, z, problemType, distributeInput);
531 std::cerr <<
"Input file block undefined!" << std::endl;
534 #ifdef HAVE_EPETRA_DATA_TYPES 535 ecomm_ = Xpetra::toEpetra(c);
544 throw std::runtime_error(
"could not read coord file");
561 throw std::runtime_error(
"could not read mtx file");
568 throw std::runtime_error(
"could not read mtx file");
569 return rcp_const_cast<
tcrsGraph_t>(M_->getCrsGraph());
574 RCP<tVector_t> V = rcp(
new tVector_t(M_->getRowMap(), 1));
582 RCP<tMVector_t> mV = rcp(
new tMVector_t(M_->getRowMap(), nvec));
591 throw std::runtime_error(
"could not read mtx file");
598 throw std::runtime_error(
"could not read mtx file");
599 return rcp_const_cast<
xcrsGraph_t>(xM_->getCrsGraph());
613 #ifdef HAVE_EPETRA_DATA_TYPES 614 RCP<Epetra_CrsGraph> UserInputForTests::getUIEpetraCrsGraph()
617 throw std::runtime_error(
"could not read mtx file");
618 RCP<const tcrsGraph_t> tgraph = M_->getCrsGraph();
619 RCP<const Tpetra::Map<zlno_t, zgno_t> > trowMap = tgraph->getRowMap();
620 RCP<const Tpetra::Map<zlno_t, zgno_t> > tcolMap = tgraph->getColMap();
622 int nElts =
static_cast<int>(trowMap->getGlobalNumElements());
623 int nMyElts =
static_cast<int>(trowMap->getNodeNumElements());
624 int base = trowMap->getIndexBase();
625 ArrayView<const int> gids = trowMap->getNodeElementList();
627 Epetra_BlockMap erowMap(nElts, nMyElts,
628 gids.getRawPtr(), 1, base, *ecomm_);
630 Array<int> rowSize(nMyElts);
631 for (
int i=0; i < nMyElts; i++){
632 rowSize[i] =
static_cast<int>(M_->getNumEntriesInLocalRow(i+base));
635 size_t maxRow = M_->getNodeMaxNumRowEntries();
636 Array<int> colGids(maxRow);
637 ArrayView<const int> colLid;
639 eG_ = rcp(
new Epetra_CrsGraph(Copy, erowMap,
640 rowSize.getRawPtr(),
true));
642 for (
int i=0; i < nMyElts; i++){
643 tgraph->getLocalRowView(i+base, colLid);
644 for (
int j=0; j < colLid.size(); j++)
645 colGids[j] = tcolMap->getGlobalElement(colLid[j]);
646 eG_->InsertGlobalIndices(gids[i], rowSize[i], colGids.getRawPtr());
652 RCP<Epetra_CrsMatrix> UserInputForTests::getUIEpetraCrsMatrix()
655 throw std::runtime_error(
"could not read mtx file");
656 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
657 eM_ = rcp(
new Epetra_CrsMatrix(Copy, *egraph));
659 size_t maxRow = M_->getNodeMaxNumRowEntries();
660 int nrows = egraph->NumMyRows();
661 int base = egraph->IndexBase();
662 const Epetra_BlockMap &rowMap = egraph->RowMap();
663 const Epetra_BlockMap &colMap = egraph->ColMap();
664 Array<int> colGid(maxRow);
666 for (
int i=0; i < nrows; i++){
667 ArrayView<const int> colLid;
668 ArrayView<const zscalar_t> nz;
669 M_->getLocalRowView(i+base, colLid, nz);
670 size_t rowSize = colLid.size();
671 int rowGid = rowMap.GID(i+base);
672 for (
size_t j=0; j < rowSize; j++){
673 colGid[j] = colMap.GID(colLid[j]);
675 eM_->InsertGlobalValues(rowGid, (
int)rowSize, nz.getRawPtr(), colGid.getRawPtr());
681 RCP<Epetra_Vector> UserInputForTests::getUIEpetraVector()
683 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
684 RCP<Epetra_Vector> V = rcp(
new Epetra_Vector(egraph->RowMap()));
689 RCP<Epetra_MultiVector> UserInputForTests::getUIEpetraMultiVector(
int nvec)
691 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
692 RCP<Epetra_MultiVector> mV =
693 rcp(
new Epetra_MultiVector(egraph->RowMap(), nvec));
703 this->hasUITpetraCrsMatrix() || \
704 this->hasUITpetraCrsGraph() || \
705 this->hasPamgenMesh();
710 if(input_type ==
"coordinates")
712 else if(input_type ==
"tpetra_vector")
714 else if(input_type ==
"tpetra_multivector")
716 else if(input_type ==
"tpetra_crs_graph")
718 else if(input_type ==
"tpetra_crs_matrix")
720 else if(input_type ==
"xpetra_vector")
722 else if(input_type ==
"xpetra_multivector")
724 else if(input_type ==
"xpetra_crs_graph")
726 else if(input_type ==
"xpetra_crs_matrix")
728 #ifdef HAVE_EPETRA_DATA_TYPES 729 else if(input_type ==
"epetra_vector")
730 return this->hasUIEpetraVector();
731 else if(input_type ==
"epetra_multivector")
732 return this->hasUIEpetraMultiVector();
733 else if(input_type ==
"epetra_crs_graph")
734 return this->hasUIEpetraCrsGraph();
735 else if(input_type ==
"epetra_crs_matrix")
736 return this->hasUIEpetraCrsMatrix();
744 return xyz_.is_null() ? false :
true;
749 return vtxWeights_.is_null() ? false :
true;
754 return edgWeights_.is_null() ? false :
true;
759 return M_.is_null() ? false :
true;
764 return M_.is_null() ? false :
true;
779 return M_.is_null() ? false :
true;
784 return M_.is_null() ? false :
true;
799 return this->havePamgenMesh;
802 #ifdef HAVE_EPETRA_DATA_TYPES 803 bool UserInputForTests::hasUIEpetraCrsGraph()
805 return M_.is_null() ? false :
true;
808 bool UserInputForTests::hasUIEpetraCrsMatrix()
810 return hasUIEpetraCrsGraph();
813 bool UserInputForTests::hasUIEpetraVector()
815 return hasUIEpetraCrsGraph();
818 bool UserInputForTests::hasUIEpetraMultiVector()
820 return hasUIEpetraCrsGraph();
826 ArrayView<ArrayRCP<zscalar_t > > data)
831 size_t dim = data.size();
832 for (
size_t i=0; i < dim; i++){
835 throw (std::bad_alloc());
836 data[i] = Teuchos::arcp(tmp, 0, length,
true);
839 zscalar_t scalingFactor = (max-min) / RAND_MAX;
841 for (
size_t i=0; i < dim; i++){
843 for (
zlno_t j=0; j < length; j++)
844 *x++ = min + (
zscalar_t(rand()) * scalingFactor);
850 string UserInputForTests::trim_right_copy(
852 const string& delimiters)
854 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
857 string UserInputForTests::trim_left_copy(
859 const string& delimiters)
861 return s.substr( s.find_first_not_of( delimiters ) );
864 string UserInputForTests::trim_copy(
866 const string& delimiters)
868 return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
871 void UserInputForTests::readGeometricGenTestData(
string path,
875 std::ostringstream fname;
876 fname << path <<
"/" << testData <<
".txt";
878 if (verbose_ && tcomm_->getRank() == 0)
879 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
881 Teuchos::ParameterList geoparams(
"geo params");
882 readGeoGenParams(fname.str(),geoparams);
887 int coord_dim = gg->getCoordinateDimension();
888 int numWeightsPerCoord = gg->getNumWeights();
889 zlno_t numLocalPoints = gg->getNumLocalCoords();
890 zgno_t numGlobalPoints = gg->getNumGlobalCoords();
894 for(
int i = 0; i < coord_dim; ++i){
895 coords[i] =
new zscalar_t[numLocalPoints];
899 gg->getLocalCoordinatesCopy(coords);
903 if (numWeightsPerCoord) {
905 weight =
new zscalar_t * [numWeightsPerCoord];
906 for(
int i = 0; i < numWeightsPerCoord; ++i){
907 weight[i] =
new zscalar_t[numLocalPoints];
911 gg->getLocalWeightsCopy(weight);
918 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp =
919 rcp(
new Tpetra::Map<zlno_t, zgno_t, znode_t>(numGlobalPoints, numLocalPoints, 0, this->tcomm_));
922 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
923 for (
int i=0; i < coord_dim; i++){
924 if(numLocalPoints > 0){
925 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
929 Teuchos::ArrayView<const zscalar_t> a;
935 xyz_ = RCP<tMVector_t>(
new 940 if (numWeightsPerCoord) {
942 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > weightView(numWeightsPerCoord);
943 for (
int i=0; i < numWeightsPerCoord; i++){
944 if(numLocalPoints > 0){
945 Teuchos::ArrayView<const zscalar_t> a(weight[i], numLocalPoints);
949 Teuchos::ArrayView<const zscalar_t> a;
954 vtxWeights_ = RCP<tMVector_t>(
new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
955 numWeightsPerCoord));
959 void UserInputForTests::readGeoGenParams(
string paramFileName,
960 ParameterList &geoparams){
964 std::string input =
"";
966 for(
int i = 0; i < 25000; ++i){
971 if(this->tcomm_->getRank() == 0){
973 fstream inParam(paramFileName.c_str());
980 std::string tmp =
"";
981 getline (inParam,tmp);
982 while (!inParam.eof()){
984 tmp = trim_copy(tmp);
989 getline (inParam,tmp);
992 for (
size_t i = 0; i < input.size(); ++i){
1000 int size = (int)input.size();
1004 this->tcomm_->broadcast(0,
sizeof(
int), (
char*) &size);
1006 throw "File " + paramFileName +
" cannot be opened.";
1008 this->tcomm_->broadcast(0, size, inp);
1009 istringstream inParam(inp);
1011 getline (inParam,str);
1012 while (!inParam.eof()){
1014 size_t pos = str.find(
'=');
1015 if(pos == string::npos){
1016 throw "Invalid Line:" + str +
" in parameter file";
1018 string paramname = trim_copy(str.substr(0,pos));
1019 string paramvalue = trim_copy(str.substr(pos + 1));
1020 geoparams.set(paramname, paramvalue);
1022 getline (inParam,str);
1026 void UserInputForTests::readMatrixMarketFile(
string path,
string testData,
bool distributeInput)
1028 std::ostringstream fname;
1029 fname << path <<
"/" << testData <<
".mtx";
1031 if (verbose_ && tcomm_->getRank() == 0)
1032 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
1037 RCP<tcrsMatrix_t> toMatrix;
1038 RCP<tcrsMatrix_t> fromMatrix;
1041 typedef Tpetra::MatrixMarket::Reader<tcrsMatrix_t> reader_type;
1042 fromMatrix = reader_type::readSparseFile(fname.str(), tcomm_,
1044 if(!distributeInput)
1046 if (verbose_ && tcomm_->getRank() == 0)
1047 std::cout <<
"Constructing serial distribution of matrix" << std::endl;
1049 RCP<const map_t> fromMap = fromMatrix->getRowMap();
1051 size_t numGlobalCoords = fromMap->getGlobalNumElements();
1052 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
1053 RCP<const map_t> toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, 0, tcomm_));
1055 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1057 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1058 toMatrix->fillComplete();
1061 toMatrix = fromMatrix;
1063 }
catch (std::exception &e) {
1064 if (tcomm_->getRank() == 0)
1065 std::cout <<
"UserInputForTests unable to read matrix market file:" 1066 << fname.str() << std::endl;
1070 "UserInputForTests unable to read matrix market file");
1079 fname << path <<
"/" << testData <<
"_coord.mtx";
1081 size_t coordDim = 0, numGlobalCoords = 0;
1082 size_t msg[2]={0,0};
1083 ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1084 std::ifstream coordFile;
1086 if (tcomm_->getRank() == 0){
1089 std::cout <<
"UserInputForTests, Read: " <<
1090 fname.str() << std::endl;
1094 coordFile.open(fname.str().c_str());
1096 catch (std::exception &e){
1107 while (!done && !
fail && coordFile.good()){
1108 coordFile.getline(c, 256);
1111 else if (c[0] ==
'%')
1115 std::istringstream s(c);
1116 s >> numGlobalCoords >> coordDim;
1117 if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1126 xyz = Teuchos::arcp(
new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1128 for (
size_t dim=0; !
fail && dim < coordDim; dim++){
1134 xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1136 for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1137 coordFile.getline(c, 256);
1138 std::istringstream s(c);
1142 if (idx < numGlobalCoords)
1148 ArrayRCP<zscalar_t> emptyArray;
1149 for (
size_t dim=0; dim < coordDim; dim++)
1150 xyz[dim] = emptyArray;
1163 msg[1] = numGlobalCoords;
1167 Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1170 numGlobalCoords = msg[1];
1176 RCP<const map_t> toMap;
1179 base = M_->getIndexBase();
1180 const RCP<const map_t> &mapM = M_->getRowMap();
1184 if (verbose_ && tcomm_->getRank() == 0)
1186 std::cout <<
"Matrix was null. ";
1187 std::cout <<
"Constructing distribution map for coordinate vector." << std::endl;
1191 if(!distributeInput)
1193 if (verbose_ && tcomm_->getRank() == 0)
1194 std::cout <<
"Constructing serial distribution map for coordinates." << std::endl;
1196 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
1197 toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1199 toMap = rcp(
new map_t(numGlobalCoords, base, tcomm_));
1207 ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1209 if (tcomm_->getRank() == 0){
1211 for (
size_t dim=0; dim < coordDim; dim++)
1212 coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1216 throw std::bad_alloc();
1218 ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1220 zgno_t basePlusNumGlobalCoords = base +
static_cast<zgno_t>(numGlobalCoords);
1221 for (
zgno_t id=base;
id < basePlusNumGlobalCoords;
id++)
1224 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1225 rowIds.view(0, numGlobalCoords), base, tcomm_));
1227 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1231 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1235 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1236 ArrayView<zgno_t>(), base, tcomm_));
1238 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1242 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1246 void UserInputForTests::buildCrsMatrix(
int xdim,
int ydim,
int zdim,
1247 string problemType,
bool distributeInput)
1249 #ifdef HAVE_ZOLTAN2_GALERI 1250 Teuchos::CommandLineProcessor tclp;
1251 Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1252 xdim, ydim, zdim, problemType);
1254 RCP<const Tpetra::Map<zlno_t, zgno_t> > map;
1255 if (distributeInput)
1256 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(params.GetNumGlobalElements(),
1260 size_t nGlobalElements = params.GetNumGlobalElements();
1261 size_t nLocalElements = ((tcomm_->getRank() == 0) ? nGlobalElements : 0);
1262 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(nGlobalElements, nLocalElements, 0,
1266 if (verbose_ && tcomm_->getRank() == 0){
1268 std::cout <<
"Matrix is " << (distributeInput ?
"" :
"not");
1269 std::cout <<
"distributed." << endl;
1271 std::cout <<
"UserInputForTests, Create matrix with " << problemType;
1272 std::cout <<
" (and " << xdim;
1274 std::cout <<
" x " << ydim <<
" x " << zdim;
1276 std::cout <<
" x" << ydim <<
" x 1";
1278 std::cout <<
"x 1 x 1";
1280 std::cout <<
" mesh)" << std::endl;
1286 RCP<Galeri::Xpetra::Problem<Tpetra::Map<zlno_t, zgno_t>, Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t>, Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t> > > Pr =
1287 Galeri::Xpetra::BuildProblem<zscalar_t, zlno_t, zgno_t, Tpetra::Map<zlno_t, zgno_t>, Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t>, Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t> >
1288 (params.GetMatrixType(), map, params.GetParameterList());
1289 M_ = Pr->BuildMatrix();
1291 catch (std::exception &e) {
1295 "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1301 if (verbose_ && tcomm_->getRank() == 0)
1303 "UserInputForTests, Implied matrix row coordinates computed" <<
1306 ArrayView<const zgno_t> gids = map->getNodeElementList();
1309 size_t pos = problemType.find(
"2D");
1310 if (pos != string::npos)
1312 else if (problemType ==
string(
"Laplace1D") ||
1313 problemType ==
string(
"Identity"))
1319 for (
int i=0; i < dim; i++){
1322 throw(std::bad_alloc());
1323 coordinates[i] = Teuchos::arcp(c, 0, count,
true);
1330 zgno_t xySize = xdim * ydim;
1331 for (
zlno_t i=0; i < count; i++){
1332 zgno_t iz = gids[i] / xySize;
1333 zgno_t xy = gids[i] - iz*xySize;
1342 for (
zlno_t i=0; i < count; i++){
1349 for (
zlno_t i=0; i < count; i++)
1354 Array<ArrayView<const zscalar_t> > coordView(dim);
1356 for (
int i=0; i < dim; i++)
1359 xyz_ = rcp(
new tMVector_t(map, coordView.view(0, dim), dim));
1361 throw std::runtime_error(
"Galeri input requested but Trilinos is " 1362 "not built with Galeri.");
1366 void UserInputForTests::readZoltanTestData(
string path,
string testData,
1367 bool distributeInput)
1369 int rank = tcomm_->getRank();
1370 FILE *graphFile = NULL;
1371 FILE *coordFile = NULL;
1372 FILE *assignFile = NULL;
1375 for (
int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] =
'\0';
1379 std::ostringstream chGraphFileName;
1380 chGraphFileName << path <<
"/" << testData <<
".graph";
1383 std::ostringstream chCoordFileName;
1384 chCoordFileName << path <<
"/" << testData <<
".coords";
1387 std::ostringstream chAssignFileName;
1388 chAssignFileName << path <<
"/" << testData <<
".assign";
1391 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1395 chGraphFileName.str(
"");
1396 chCoordFileName.str(
"");
1398 chGraphFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".graph";
1399 chCoordFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".coords";
1400 chAssignFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".assign";
1403 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1406 memset(fileInfo, 0,
sizeof(
int) * 3);
1409 if (verbose_ && tcomm_->getRank() == 0)
1410 std::cout <<
"UserInputForTests, open " <<
1411 chGraphFileName.str () << std::endl;
1413 coordFile = fopen(chCoordFileName.str().c_str(),
"r");
1416 if (verbose_ && tcomm_->getRank() == 0)
1417 std::cout <<
"UserInputForTests, open " <<
1418 chCoordFileName.str () << std::endl;
1421 assignFile = fopen(chAssignFileName.str().c_str(),
"r");
1424 if (verbose_ && tcomm_->getRank() == 0)
1425 std::cout <<
"UserInputForTests, open " <<
1426 chAssignFileName.str () << std::endl;
1429 if (verbose_ && tcomm_->getRank() == 0){
1430 std::cout <<
"UserInputForTests, unable to open file: ";
1431 std::cout << chGraphFileName.str() << std::endl;
1437 Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1439 bool haveGraph = (fileInfo[0] == 1);
1440 bool haveCoords = (fileInfo[1] == 1);
1441 bool haveAssign = (fileInfo[2] == 1);
1446 getUIChacoGraph(graphFile, haveAssign, assignFile,
1447 testData, distributeInput);
1454 getUIChacoCoords(coordFile, testData);
1463 void UserInputForTests::getUIChacoGraph(FILE *fptr,
bool haveAssign,
1464 FILE *assignFile,
string fname,
1465 bool distributeInput)
1467 int rank = tcomm_->getRank();
1469 int nvtxs=0, nedges=0;
1470 int nVwgts=0, nEwgts=0;
1471 int *start = NULL, *adj = NULL;
1472 float *ewgts = NULL, *vwgts = NULL;
1473 size_t *nzPerRow = NULL;
1474 size_t maxRowLen = 0;
1476 ArrayRCP<const size_t> rowSizes;
1478 bool haveEdges =
true;
1482 memset(graphCounts, 0, 5*
sizeof(
int));
1485 fail = chaco_input_graph(fptr, fname.c_str(), &start, &adj,
1486 &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1497 std::cout <<
"UserInputForTests, " << nvtxs <<
" vertices,";
1499 std::cout << start[nvtxs] <<
" edges,";
1501 std::cout <<
"no edges,";
1502 std::cout << nVwgts <<
" vertex weights, ";
1503 std::cout << nEwgts <<
" edge weights" << std::endl;
1510 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1511 throw std::runtime_error(
"Unable to read chaco file");
1515 nedges = start[nvtxs];
1517 nzPerRow =
new size_t [nvtxs];
1519 throw std::bad_alloc();
1520 rowSizes = arcp(nzPerRow, 0, nvtxs,
true);
1523 for (
int i=0; i < nvtxs; i++){
1524 nzPerRow[i] = start[i+1] - start[i];
1525 if (nzPerRow[i] > maxRowLen)
1526 maxRowLen = nzPerRow[i];
1530 memset(nzPerRow, 0,
sizeof(
size_t) * nvtxs);
1536 int chbase = adj[0];
1537 for (
int i=1; i < nedges; i++)
1538 if (adj[i] < chbase)
1542 for (
int i=0; i < nedges; i++)
1547 graphCounts[0] = nvtxs;
1548 graphCounts[1] = nedges;
1549 graphCounts[2] = nVwgts;
1550 graphCounts[3] = nEwgts;
1551 graphCounts[4] = (int)maxRowLen;
1554 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1556 if (graphCounts[0] == 0)
1557 throw std::runtime_error(
"Unable to read chaco file");
1559 haveEdges = (graphCounts[1] > 0);
1561 RCP<tcrsMatrix_t> fromMatrix;
1562 RCP<const map_t> fromMap;
1566 fromMap = rcp(
new map_t(nvtxs, nvtxs, base, tcomm_));
1569 rcp(
new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
1574 if (nedges && !edgeIds)
1575 throw std::bad_alloc();
1576 for (
int i=0; i < nedges; i++)
1577 edgeIds[i] = adj[i];
1582 zgno_t *nextId = edgeIds;
1583 Array<zscalar_t> values(nedges, 1.0);
1584 if (nedges > 0 && nEwgts > 0) {
1585 for (
int i=0; i < nedges; i++)
1586 values[i] = ewgts[i];
1591 for (
int i=0; i < nvtxs; i++){
1592 if (nzPerRow[i] > 0){
1593 ArrayView<const zgno_t> rowNz(nextId, nzPerRow[i]);
1594 fromMatrix->insertGlobalValues(i, rowNz, values.view(start[i], start[i+1] - start[i]));
1595 nextId += nzPerRow[i];
1603 fromMatrix->fillComplete();
1606 nvtxs = graphCounts[0];
1607 nedges = graphCounts[1];
1608 nVwgts = graphCounts[2];
1609 nEwgts = graphCounts[3];
1610 maxRowLen = graphCounts[4];
1614 fromMap = rcp(
new map_t(nvtxs, 0, base, tcomm_));
1617 rcp(
new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
1619 fromMatrix->fillComplete();
1624 size_t sz = fromMatrix->getNodeMaxNumRowEntries();
1625 Teuchos::Array<zgno_t> indices(sz);
1626 Teuchos::Array<zscalar_t> values(sz);
1627 for (
size_t i = 0; i < fromMatrix->getNodeNumRows(); i++) {
1628 zgno_t gid = fromMatrix->getRowMap()->getGlobalElement(i);
1630 fromMatrix->getGlobalRowCopy(gid, indices(), values(), num);
1631 std::cout <<
"ROW " << gid <<
": ";
1632 for (
size_t j = 0; j < num; j++)
1633 std::cout << indices[j] <<
" ";
1634 std::cout << std::endl;
1639 RCP<const map_t> toMap;
1640 RCP<tcrsMatrix_t> toMatrix;
1641 RCP<import_t> importer;
1643 if (distributeInput) {
1646 short *assignments =
new short[nvtxs];
1648 fail = chaco_input_assign(assignFile, fname.c_str(), nvtxs, assignments);
1651 Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1654 Teuchos::Array<zgno_t> mine;
1655 for (
int i = 0; i < nvtxs; i++) {
1656 if (assignments[i] == rank)
1661 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1662 toMap = rcp(
new map_t(dummy, mine(), base, tcomm_));
1663 delete [] assignments;
1667 toMap = rcp(
new map_t(nvtxs, base, tcomm_));
1672 importer = rcp(
new import_t(fromMap, toMap));
1673 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1674 toMatrix->fillComplete();
1678 toMatrix = fromMatrix;
1685 typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1689 ArrayRCP<zscalar_t> weightBuf;
1690 ArrayView<const zscalar_t> *wgts =
new ArrayView<const zscalar_t> [nVwgts];
1693 size_t len = nVwgts * nvtxs;
1695 if (!buf)
throw std::bad_alloc();
1696 weightBuf = arcp(buf, 0, len,
true);
1698 for (
int widx=0; widx < nVwgts; widx++){
1699 wgts[widx] = ArrayView<const zscalar_t>(buf, nvtxs);
1700 float *vw = vwgts + widx;
1701 for (
int i=0; i < nvtxs; i++, vw += nVwgts)
1710 arrayArray_t vweights = arcp(wgts, 0, nVwgts,
true);
1712 RCP<tMVector_t> fromVertexWeights =
1713 rcp(
new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1715 RCP<tMVector_t> toVertexWeights;
1716 if (distributeInput) {
1717 toVertexWeights = rcp(
new tMVector_t(toMap, nVwgts));
1718 toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1721 toVertexWeights = fromVertexWeights;
1723 vtxWeights_ = toVertexWeights;
1728 if (haveEdges && nEwgts > 0){
1778 toMap = rcp(
new map_t(nedges, M_->getNodeNumEntries(), base, tcomm_));
1779 edgWeights_ = rcp(
new tMVector_t(toMap, nEwgts));
1781 size_t maxSize = M_->getNodeMaxNumRowEntries();
1782 Array<zlno_t> colind(maxSize);
1783 Array<zscalar_t>
vals(maxSize);
1786 for (
size_t i = 0, idx = 0; i < M_->getNodeNumRows(); i++) {
1787 M_->getLocalRowCopy(i, colind,
vals, nEntries);
1788 for (
size_t j = 0; j < nEntries; j++) {
1789 edgWeights_->replaceLocalValue(idx, 0,
vals[j]);
1801 void UserInputForTests::getUIChacoCoords(FILE *fptr,
string fname)
1803 int rank = tcomm_->getRank();
1805 double *x=NULL, *y=NULL, *z=NULL;
1808 size_t globalNumVtx = M_->getGlobalNumRows();
1815 fail = chaco_input_geom(fptr, fname.c_str(), (int)globalNumVtx,
1822 std::cout <<
"UserInputForTests, read " << globalNumVtx;
1823 std::cout <<
" " << ndim <<
"-dimensional coordinates." << std::endl;
1827 Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1830 throw std::runtime_error(
"Can't read coordinate file");
1832 ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1837 for (
int dim=0; dim < ndim; dim++){
1840 throw std::bad_alloc();
1841 coords[dim] = arcp<const zscalar_t>(v, 0, globalNumVtx,
true);
1842 double *val = (dim==0 ? x : (dim==1 ? y : z));
1843 for (
size_t i=0; i < globalNumVtx; i++)
1849 len =
static_cast<zlno_t>(globalNumVtx);
1852 RCP<const map_t> fromMap = rcp(
new map_t(globalNumVtx, len, 0, tcomm_));
1853 RCP<const map_t> toMap = M_->getRowMap();
1854 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1856 Array<ArrayView<const zscalar_t> > coordData;
1857 for (
int dim=0; dim < ndim; dim++)
1858 coordData.push_back(coords[dim].view(0, len));
1860 RCP<tMVector_t> fromCoords =
1861 rcp(
new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1863 RCP<tMVector_t> toCoords = rcp(
new tMVector_t(toMap, ndim));
1865 toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
1874 double UserInputForTests::chaco_read_val(
1890 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1891 if (chaco_offset >= chaco_break_pnt) {
1892 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1894 ptr = &chaco_line[chaco_save_pnt];
1895 for (i=length_left; i; i--) *ptr2++ = *ptr++;
1896 length = chaco_save_pnt + 1;
1899 length = CHACO_LINE_LENGTH;
1904 ptr2 = fgets(&chaco_line[length_left], length, infile);
1906 if (ptr2 == (
char *) NULL) {
1908 return((
double) 0.0);
1911 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
1912 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
1914 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
1915 chaco_save_pnt = chaco_break_pnt;
1920 if (chaco_line[chaco_break_pnt] !=
'\0') {
1921 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
1923 chaco_save_pnt = chaco_break_pnt + 1;
1927 else if (white_seen) {
1934 chaco_break_pnt = CHACO_LINE_LENGTH;
1940 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
1941 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
1943 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
1944 chaco_flush_line(infile);
1946 return((
double) 0.0);
1949 ptr = &(chaco_line[chaco_offset]);
1950 val = strtod(ptr, &ptr2);
1955 return((
double) 0.0);
1958 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
1965 int UserInputForTests::chaco_read_int(
1981 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1982 if (chaco_offset >= chaco_break_pnt) {
1983 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1985 ptr = &chaco_line[chaco_save_pnt];
1986 for (i=length_left; i; i--) *ptr2++ = *ptr++;
1987 length = chaco_save_pnt + 1;
1990 length = CHACO_LINE_LENGTH;
1995 ptr2 = fgets(&chaco_line[length_left], length, infile);
1997 if (ptr2 == (
char *) NULL) {
2002 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2003 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2005 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2006 chaco_save_pnt = chaco_break_pnt;
2011 if (chaco_line[chaco_break_pnt] !=
'\0') {
2012 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2014 chaco_save_pnt = chaco_break_pnt + 1;
2018 else if (white_seen) {
2025 chaco_break_pnt = CHACO_LINE_LENGTH;
2031 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2032 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2034 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2035 chaco_flush_line(infile);
2040 ptr = &(chaco_line[chaco_offset]);
2041 val = (int) strtol(ptr, &ptr2, 10);
2049 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2055 void UserInputForTests::chaco_flush_line(
2062 while (c !=
'\n' && c !=
'\f')
2066 int UserInputForTests::chaco_input_graph(
2116 while (end_flag == 1) {
2117 *nvtxs = chaco_read_int(fin, &end_flag);
2121 printf(
"ERROR in graph file `%s':", inname);
2122 printf(
" Invalid number of vertices (%d).\n", *nvtxs);
2127 narcs = chaco_read_int(fin, &end_flag);
2129 printf(
"ERROR in graph file `%s':", inname);
2130 printf(
" Invalid number of expected edges (%d).\n", narcs);
2137 option = chaco_read_int(fin, &end_flag);
2139 using_ewgts = option - 10 * (option / 10);
2141 using_vwgts = option - 10 * (option / 10);
2143 vtxnums = option - 10 * (option / 10);
2146 (*nVwgts) = using_vwgts;
2147 (*nEwgts) = using_ewgts;
2150 if (!end_flag && using_vwgts==1){
2151 j = chaco_read_int(fin, &end_flag);
2152 if (!end_flag) (*nVwgts) = j;
2154 if (!end_flag && using_ewgts==1){
2155 j = chaco_read_int(fin, &end_flag);
2156 if (!end_flag) (*nEwgts) = j;
2161 j = chaco_read_int(fin, &end_flag);
2164 *start = (
int *) malloc((
unsigned) (*nvtxs + 1) *
sizeof(
int));
2166 *adjacency = (
int *) malloc((
unsigned) (2 * narcs + 1) *
sizeof(
int));
2171 *vweights = (
float *) malloc((
unsigned) (*nvtxs) * (*nVwgts) *
sizeof(float));
2176 *eweights = (
float *)
2177 malloc((
unsigned) (2 * narcs + 1) * (*nEwgts) *
sizeof(float));
2181 adjptr = *adjacency;
2190 while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2195 j = chaco_read_int(fin, &end_flag);
2197 if (vertex == *nvtxs)
2199 printf(
"ERROR in graph file `%s':", inname);
2200 printf(
" no vertex number in line %d.\n", line_num);
2204 if (j != vertex && j != vertex + 1) {
2205 printf(
"ERROR in graph file `%s':", inname);
2206 printf(
" out-of-order vertex number in line %d.\n", line_num);
2220 if (vertex > *nvtxs)
2224 if (using_vwgts && new_vertex) {
2225 for (j=0; j<(*nVwgts); j++){
2226 weight = chaco_read_val(fin, &end_flag);
2228 printf(
"ERROR in graph file `%s':", inname);
2229 printf(
" not enough weights for vertex %d.\n", vertex);
2233 (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2240 neighbor = chaco_read_int(fin, &end_flag);
2246 for (j=0; j<(*nEwgts); j++){
2247 eweight = chaco_read_val(fin, &end_flag);
2250 printf(
"ERROR in graph file `%s':", inname);
2251 printf(
" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2264 if (++nedges > 2*narcs) {
2265 printf(
"ERROR in graph file `%s':", inname);
2266 printf(
" at least %d adjacencies entered, but nedges = %d\n",
2271 *adjptr++ = neighbor;
2276 neighbor = chaco_read_int(fin, &end_flag);
2280 (*start)[vertex] = sum_edges;
2285 while (!flag && end_flag != -1) {
2286 chaco_read_int(fin, &end_flag);
2291 (*start)[*nvtxs] = sum_edges;
2299 if (*adjacency != NULL)
2302 if (*eweights != NULL)
2311 if (*adjacency != NULL)
2313 if (*vweights != NULL)
2315 if (*eweights != NULL)
2323 return (error_flag);
2327 int UserInputForTests::chaco_input_geom(
2329 const char *geomname,
2337 double xc, yc, zc =0;
2345 *x = *y = *z = NULL;
2348 while (end_flag == 1) {
2349 xc = chaco_read_val(fingeom, &end_flag);
2353 if (end_flag == -1) {
2354 printf(
"No values found in geometry file `%s'\n", geomname);
2360 yc = chaco_read_val(fingeom, &end_flag);
2361 if (end_flag == 0) {
2363 zc = chaco_read_val(fingeom, &end_flag);
2364 if (end_flag == 0) {
2366 chaco_read_val(fingeom, &end_flag);
2368 printf(
"Too many values on input line of geometry file `%s'\n",
2371 printf(
" Maximum dimensionality is 3\n");
2380 *x = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2383 *y = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2387 *z = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2391 for (nread = 1; nread < nvtxs; nread++) {
2394 i = fscanf(fingeom,
"%lf", &((*x)[nread]));
2396 else if (ndims == 2) {
2397 i = fscanf(fingeom,
"%lf%lf", &((*x)[nread]), &((*y)[nread]));
2399 else if (ndims == 3) {
2400 i = fscanf(fingeom,
"%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2405 printf(
"Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2410 else if (i != ndims) {
2411 printf(
"Wrong number of values in line %d of geometry file `%s'\n",
2412 line_num, geomname);
2421 while (!flag && end_flag != -1) {
2422 chaco_read_val(fingeom, &end_flag);
2434 int UserInputForTests::chaco_input_assign(
2436 const char *inassignname,
2447 while (end_flag == 1) {
2448 assignment[0] = chaco_read_int(finassign, &end_flag);
2451 if (assignment[0] < 0) {
2452 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2453 1, inassignname, assignment[0]);
2458 if (end_flag == -1) {
2459 printf(
"ERROR: No values found in assignment file `%s'\n", inassignname);
2465 if (assignment[0] > nvtxs)
2466 flag = assignment[1];
2467 for (i = 1; i < nvtxs; i++) {
2468 j = fscanf(finassign,
"%hd", &(assignment[i]));
2470 printf(
"ERROR: Too few values in assignment file `%s'.\n", inassignname);
2474 if (assignment[i] < 0) {
2475 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2476 i+1, inassignname, assignment[i]);
2480 if (assignment[i] > nvtxs) {
2481 if (assignment[i] > flag)
2482 flag = assignment[i];
2487 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2488 printf(
" More assignment sets (%d) than vertices (%d)\n", flag, nvtxs);
2494 while (!flag && end_flag != -1) {
2495 chaco_read_int(finassign, &end_flag);
2500 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2501 printf(
" Numerical data found after expected end of file\n");
2509 void UserInputForTests::readPamgenMeshFile(
string path,
string testData)
2511 #ifdef HAVE_ZOLTAN2_PAMGEN 2512 int rank = this->tcomm_->getRank();
2513 if (verbose_ && tcomm_->getRank() == 0)
2514 std::cout <<
"UserInputForTestsBD::readPamgenFile, Read: " << testData << std::endl;
2521 std::ostringstream meshFileName;
2522 meshFileName << path <<
"/" << testData <<
".pmgen";
2525 file.open(meshFileName.str(), ios::in);
2529 if(verbose_ && tcomm_->getRank() == 0)
2531 std::cout <<
"Unable to open pamgen mesh: ";
2532 std::cout << meshFileName.str();
2533 std::cout <<
"\nPlease check file path and name." << std::endl;
2539 file.seekg (0,file.end);
2546 while(std::getline(file,line))
2548 if( line.find(
"nz") != std::string::npos ||
2549 line.find(
"nphi") != std::string::npos)
2557 file.seekg(0, ios::beg);
2562 this->tcomm_->broadcast(0,
sizeof(
int), (
char *)&dimension);
2563 this->tcomm_->broadcast(0,
sizeof(
size_t),(
char *)&len);
2564 this->tcomm_->barrier();
2567 if(verbose_ && tcomm_->getRank() == 0)
2568 std::cout <<
"Pamgen Mesh file size == 0, exiting UserInputForTests early." << endl;
2572 char * file_data =
new char[len];
2573 file_data[len-1] =
'\0';
2575 file.read(file_data,len);
2579 this->tcomm_->broadcast(0,(
int)len,file_data);
2580 this->tcomm_->barrier();
2584 this->pamgen_mesh = rcp(
new PamgenMesh);
2585 this->havePamgenMesh =
true;
2586 pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2589 pamgen_mesh->storeMesh();
2590 this->tcomm_->barrier();
2593 this->setPamgenCoordinateMV();
2596 this->setPamgenAdjacencyGraph();
2598 this->tcomm_->barrier();
2599 if(rank == 0) file.close();
2600 delete [] file_data;
2602 throw std::runtime_error(
"Pamgen requested but Trilinos " 2603 "not built with Pamgen");
2607 #ifdef HAVE_ZOLTAN2_PAMGEN 2608 void UserInputForTests::setPamgenCoordinateMV()
2610 int dimension = pamgen_mesh->num_dim;
2614 zgno_t numelements = pamgen_mesh->num_elem;
2615 zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2618 for(
int i = 0; i < dimension; ++i){
2619 elem_coords[i] =
new zscalar_t[numelements];
2620 memcpy(elem_coords[i],&pamgen_mesh->element_coord[i*numelements],
sizeof(
double) * numelements);
2624 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2625 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2629 Array<zgno_t> elementList(numelements);
2630 for (Array<zgno_t>::size_type k = 0; k < numelements; ++k) {
2631 elementList[k] = pamgen_mesh->element_order_map[k];
2634 mp = rcp (
new map_t (numGlobalElements, elementList, 0, this->tcomm_));
2638 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(dimension);
2639 for (
int i = 0; i < dimension; i++){
2640 if(numelements > 0){
2641 Teuchos::ArrayView<const zscalar_t> a(elem_coords[i], numelements);
2645 Teuchos::ArrayView<const zscalar_t> a;
2651 xyz_ = RCP<tMVector_t>(
new 2656 void UserInputForTests::setPamgenAdjacencyGraph()
2664 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2667 size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2668 size_t local_els = (
size_t)this->pamgen_mesh->num_elem;
2670 size_t global_els = (size_t)this->pamgen_mesh->num_elems_global;
2671 size_t global_nodes = (
size_t)this->pamgen_mesh->num_nodes_global;
2675 RCP<const map_t> rowMap = rcp(
new map_t(global_els,0,this->tcomm_));
2676 RCP<const map_t> rangeMap = rowMap;
2679 RCP<const map_t> domainMap = rcp(
new map_t(global_nodes,0,this->tcomm_));
2682 Teuchos::RCP<tcrsMatrix_t> C = rcp(
new tcrsMatrix_t(rowMap,0));
2685 Array<zgno_t> g_el_ids(local_els);
2686 for (
size_t k = 0; k < local_els; ++k) {
2687 g_el_ids[k] = pamgen_mesh->global_element_numbers[k]-1;
2690 Array<zgno_t> g_node_ids(local_nodes);
2691 for (
size_t k = 0; k < local_nodes; ++k) {
2692 g_node_ids[k] = pamgen_mesh->global_node_numbers[k]-1;
2695 int blks = this->pamgen_mesh->num_elem_blk;
2701 for(
int i = 0; i < blks; i++)
2703 int el_per_block = this->pamgen_mesh->elements[i];
2704 int nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2705 int * connect = this->pamgen_mesh->elmt_node_linkage[i];
2707 for(
int j = 0; j < el_per_block; j++)
2709 const zgno_t gid =
static_cast<zgno_t>(g_el_ids[el_no]);
2710 for(
int k = 0; k < nodes_per_el; k++)
2712 int g_node_i = g_node_ids[connect[j*nodes_per_el+k]-1];
2713 C->insertGlobalValues(gid,
2714 Teuchos::tuple<zgno_t>(g_node_i),
2715 Teuchos::tuple<zscalar_t>(one));
2721 C->fillComplete(domainMap, rangeMap);
2727 Tpetra::MatrixMatrix::Multiply(*C,
false, *C,
true, *A);
2735 for(
zgno_t gid : rowMap->getNodeElementList())
2737 size_t numEntriesInRow = A->getNumEntriesInGlobalRow (gid);
2738 Array<zscalar_t> rowvals (numEntriesInRow);
2739 Array<zgno_t> rowinds (numEntriesInRow);
2742 Array<zscalar_t> mod_rowvals;
2743 Array<zgno_t> mod_rowinds;
2744 A->getGlobalRowCopy (gid, rowinds (), rowvals (), numEntriesInRow);
2745 for (
size_t i = 0; i < numEntriesInRow; i++) {
2748 if (rowvals[i] >= 1)
2750 mod_rowvals.push_back(one);
2751 mod_rowinds.push_back(rowinds[i]);
2754 this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2757 this->M_->fillComplete();
keep typedefs that commonly appear in many places localized
#define TEST_FAIL_AND_THROW(comm, ok, s)
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
void readGeoGenParams(string paramFileName, Teuchos::ParameterList &geoparams, const RCP< const Teuchos::Comm< int > > &comm)
Traits of Xpetra classes, including migration method.
common code used by tests
#define HAVE_EPETRA_DATA_TYPES
static RCP< User > convertToXpetra(const RCP< User > &a)
Convert the object to its Xpetra wrapped version.
GeometricGen::GeometricGenerator< zscalar_t, zlno_t, zgno_t, znode_t > geometricgen_t
Tpetra::CrsGraph< zlno_t, zgno_t, znode_t > tcrsGraph_t
Xpetra::CrsGraph< zlno_t, zgno_t, znode_t > xcrsGraph_t
Tpetra::Vector< zscalar_t, zlno_t, zgno_t, znode_t > tVector_t
static const std::string fail
string trim_right_copy(const string &s, const string &delimiters=" \\\)
string trim_copy(const string &s, const string &delimiters=" \\\)
string trim_left_copy(const string &s, const string &delimiters=" \\\)