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 RCP<tcrsMatrix_t> modifyMatrixGIDs(RCP<tcrsMatrix_t> &in);
322 inline zgno_t newID(
const zgno_t id) {
return id * 2 + 10001; }
325 void getUIChacoGraph(FILE *fptr,
bool haveAssign, FILE *assignFile,
326 string name,
bool distributeInput);
329 void getUIChacoCoords(FILE *fptr,
string name);
335 static const int CHACO_LINE_LENGTH=200;
336 char chaco_line[CHACO_LINE_LENGTH];
341 double chaco_read_val(FILE* infile,
int *end_flag);
342 int chaco_read_int(FILE* infile,
int *end_flag);
343 void chaco_flush_line(FILE*);
346 int chaco_input_graph(FILE *fin,
const char *inname,
int **start,
347 int **adjacency,
int *nvtxs,
int *nVwgts,
348 float **vweights,
int *nEwgts,
float **eweights);
351 int chaco_input_geom(FILE *fingeom,
const char *geomname,
int nvtxs,
352 int *igeom,
double **x,
double **y,
double **z);
355 int chaco_input_assign(FILE *finassign,
const char *assignname,
int nvtxs,
363 void readGeometricGenTestData(
string path,
string testData);
367 ParameterList &geoparams);
372 const string& delimiters =
" \f\n\r\t\v" );
375 const string& delimiters =
" \f\n\r\t\v" );
378 const string& delimiters =
" \f\n\r\t\v" );
382 void readPamgenMeshFile(
string path,
string testData);
383 #ifdef HAVE_ZOLTAN2_PAMGEN 384 void setPamgenAdjacencyGraph();
385 void setPamgenCoordinateMV();
390 const RCP<
const Comm<int> > &c,
391 bool debugInfo,
bool distributeInput):
392 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
393 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
395 ecomm_(), eM_(), eG_(),
397 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
399 bool zoltan1 =
false;
400 string::size_type loc = path.find(
"/zoltan/test/");
401 if (loc != string::npos)
405 readZoltanTestData(path, testData, distributeInput);
407 readMatrixMarketFile(path, testData);
409 #ifdef HAVE_EPETRA_DATA_TYPES 410 ecomm_ = Xpetra::toEpetra(c);
416 const RCP<
const Comm<int> > &c,
418 bool distributeInput):
419 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
420 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
422 ecomm_(), eM_(), eG_(),
424 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
426 if (matrixType.size() == 0){
432 matrixType = string(
"Laplace1D");
434 matrixType = string(
"Laplace2D");
436 matrixType = string(
"Laplace3D");
438 throw std::runtime_error(
"input");
440 if (verbose_ && tcomm_->getRank() == 0)
441 std::cout <<
"UserInputForTests, Matrix type : " << matrixType << std::endl;
444 buildCrsMatrix(x, y, z, matrixType, distributeInput);
446 #ifdef HAVE_EPETRA_DATA_TYPES 447 ecomm_ = Xpetra::toEpetra(c);
452 const RCP<
const Comm<int> > &c):
453 tcomm_(c), havePamgenMesh(false),
454 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
456 ecomm_(), eM_(), eG_(),
458 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
462 bool distributeInput =
true, debugInfo =
true;
464 if(pList.isParameter(
"distribute input"))
465 distributeInput = pList.get<
bool>(
"distribute input");
467 if(pList.isParameter(
"debug"))
468 debugInfo = pList.get<
bool>(
"debug");
469 this->verbose_ = debugInfo;
471 if(pList.isParameter(
"input file"))
476 if(pList.isParameter(
"input path"))
477 path = pList.get<
string>(
"input path");
479 string testData = pList.get<
string>(
"input file");
485 if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Geometric Generator")
487 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Pamgen")
491 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Chaco")
495 switch (file_format) {
496 case GEOMGEN: readGeometricGenTestData(path,testData);
break;
497 case PAMGEN: readPamgenMeshFile(path,testData);
break;
498 case CHACO: readZoltanTestData(path, testData, distributeInput);
break;
499 default: readMatrixMarketFile(path, testData, distributeInput);
break;
502 }
else if(pList.isParameter(
"x") || pList.isParameter(
"y") || pList.isParameter(
"z")){
506 if(pList.isParameter(
"x")) x = pList.get<
int>(
"x");
507 if(pList.isParameter(
"y")) y = pList.get<
int>(
"y");
508 if(pList.isParameter(
"z")) z = pList.get<
int>(
"z");
510 string problemType =
"";
511 if(pList.isParameter(
"equation type")) problemType = pList.get<
string>(
"equation type");
513 if (problemType.size() == 0){
519 problemType = string(
"Laplace1D");
521 problemType = string(
"Laplace2D");
523 problemType = string(
"Laplace3D");
525 throw std::runtime_error(
"input");
527 if (verbose_ && tcomm_->getRank() == 0)
528 std::cout <<
"UserInputForTests, Matrix type : " << problemType << std::endl;
532 buildCrsMatrix(x, y, z, problemType, distributeInput);
535 std::cerr <<
"Input file block undefined!" << std::endl;
538 #ifdef HAVE_EPETRA_DATA_TYPES 539 ecomm_ = Xpetra::toEpetra(c);
548 throw std::runtime_error(
"could not read coord file");
565 throw std::runtime_error(
"could not read mtx file");
572 throw std::runtime_error(
"could not read mtx file");
573 return rcp_const_cast<
tcrsGraph_t>(M_->getCrsGraph());
578 RCP<tVector_t> V = rcp(
new tVector_t(M_->getRowMap(), 1));
586 RCP<tMVector_t> mV = rcp(
new tMVector_t(M_->getRowMap(), nvec));
595 throw std::runtime_error(
"could not read mtx file");
602 throw std::runtime_error(
"could not read mtx file");
603 return rcp_const_cast<
xcrsGraph_t>(xM_->getCrsGraph());
617 #ifdef HAVE_EPETRA_DATA_TYPES 618 RCP<Epetra_CrsGraph> UserInputForTests::getUIEpetraCrsGraph()
621 throw std::runtime_error(
"could not read mtx file");
622 RCP<const tcrsGraph_t> tgraph = M_->getCrsGraph();
623 RCP<const Tpetra::Map<zlno_t, zgno_t> > trowMap = tgraph->getRowMap();
624 RCP<const Tpetra::Map<zlno_t, zgno_t> > tcolMap = tgraph->getColMap();
626 int nElts =
static_cast<int>(trowMap->getGlobalNumElements());
627 int nMyElts =
static_cast<int>(trowMap->getNodeNumElements());
629 ArrayView<const int> gids = trowMap->getNodeElementList();
631 Epetra_BlockMap erowMap(nElts, nMyElts,
632 gids.getRawPtr(), 1, base, *ecomm_);
634 Array<int> rowSize(nMyElts);
635 for (
int i=0; i < nMyElts; i++){
636 rowSize[i] =
static_cast<int>(M_->getNumEntriesInLocalRow(i));
639 size_t maxRow = M_->getNodeMaxNumRowEntries();
640 Array<int> colGids(maxRow);
641 ArrayView<const int> colLid;
643 eG_ = rcp(
new Epetra_CrsGraph(Copy, erowMap,
644 rowSize.getRawPtr(),
true));
646 for (
int i=0; i < nMyElts; i++){
647 tgraph->getLocalRowView(i, colLid);
648 for (
int j=0; j < colLid.size(); j++)
649 colGids[j] = tcolMap->getGlobalElement(colLid[j]);
650 eG_->InsertGlobalIndices(gids[i], rowSize[i], colGids.getRawPtr());
656 RCP<Epetra_CrsMatrix> UserInputForTests::getUIEpetraCrsMatrix()
659 throw std::runtime_error(
"could not read mtx file");
660 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
661 eM_ = rcp(
new Epetra_CrsMatrix(Copy, *egraph));
663 size_t maxRow = M_->getNodeMaxNumRowEntries();
664 int nrows = egraph->NumMyRows();
665 const Epetra_BlockMap &rowMap = egraph->RowMap();
666 const Epetra_BlockMap &colMap = egraph->ColMap();
667 Array<int> colGid(maxRow);
669 for (
int i=0; i < nrows; i++){
670 ArrayView<const int> colLid;
671 ArrayView<const zscalar_t> nz;
672 M_->getLocalRowView(i, colLid, nz);
673 size_t rowSize = colLid.size();
674 int rowGid = rowMap.GID(i);
675 for (
size_t j=0; j < rowSize; j++){
676 colGid[j] = colMap.GID(colLid[j]);
678 eM_->InsertGlobalValues(rowGid, (
int)rowSize, nz.getRawPtr(), colGid.getRawPtr());
684 RCP<Epetra_Vector> UserInputForTests::getUIEpetraVector()
686 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
687 RCP<Epetra_Vector> V = rcp(
new Epetra_Vector(egraph->RowMap()));
692 RCP<Epetra_MultiVector> UserInputForTests::getUIEpetraMultiVector(
int nvec)
694 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
695 RCP<Epetra_MultiVector> mV =
696 rcp(
new Epetra_MultiVector(egraph->RowMap(), nvec));
706 this->hasUITpetraCrsMatrix() || \
707 this->hasUITpetraCrsGraph() || \
708 this->hasPamgenMesh();
713 if(input_type ==
"coordinates")
715 else if(input_type ==
"tpetra_vector")
717 else if(input_type ==
"tpetra_multivector")
719 else if(input_type ==
"tpetra_crs_graph")
721 else if(input_type ==
"tpetra_crs_matrix")
723 else if(input_type ==
"xpetra_vector")
725 else if(input_type ==
"xpetra_multivector")
727 else if(input_type ==
"xpetra_crs_graph")
729 else if(input_type ==
"xpetra_crs_matrix")
731 #ifdef HAVE_EPETRA_DATA_TYPES 732 else if(input_type ==
"epetra_vector")
733 return this->hasUIEpetraVector();
734 else if(input_type ==
"epetra_multivector")
735 return this->hasUIEpetraMultiVector();
736 else if(input_type ==
"epetra_crs_graph")
737 return this->hasUIEpetraCrsGraph();
738 else if(input_type ==
"epetra_crs_matrix")
739 return this->hasUIEpetraCrsMatrix();
747 return xyz_.is_null() ? false :
true;
752 return vtxWeights_.is_null() ? false :
true;
757 return edgWeights_.is_null() ? false :
true;
762 return M_.is_null() ? false :
true;
767 return M_.is_null() ? false :
true;
782 return M_.is_null() ? false :
true;
787 return M_.is_null() ? false :
true;
802 return this->havePamgenMesh;
805 #ifdef HAVE_EPETRA_DATA_TYPES 806 bool UserInputForTests::hasUIEpetraCrsGraph()
808 return M_.is_null() ? false :
true;
811 bool UserInputForTests::hasUIEpetraCrsMatrix()
813 return hasUIEpetraCrsGraph();
816 bool UserInputForTests::hasUIEpetraVector()
818 return hasUIEpetraCrsGraph();
821 bool UserInputForTests::hasUIEpetraMultiVector()
823 return hasUIEpetraCrsGraph();
829 ArrayView<ArrayRCP<zscalar_t > > data)
834 size_t dim = data.size();
835 for (
size_t i=0; i < dim; i++){
838 throw (std::bad_alloc());
839 data[i] = Teuchos::arcp(tmp, 0, length,
true);
842 zscalar_t scalingFactor = (max-min) / RAND_MAX;
844 for (
size_t i=0; i < dim; i++){
846 for (
zlno_t j=0; j < length; j++)
847 *x++ = min + (
zscalar_t(rand()) * scalingFactor);
853 string UserInputForTests::trim_right_copy(
855 const string& delimiters)
857 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
860 string UserInputForTests::trim_left_copy(
862 const string& delimiters)
864 return s.substr( s.find_first_not_of( delimiters ) );
867 string UserInputForTests::trim_copy(
869 const string& delimiters)
871 return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
874 void UserInputForTests::readGeometricGenTestData(
string path,
878 std::ostringstream fname;
879 fname << path <<
"/" << testData <<
".txt";
881 if (verbose_ && tcomm_->getRank() == 0)
882 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
884 Teuchos::ParameterList geoparams(
"geo params");
885 readGeoGenParams(fname.str(),geoparams);
890 int coord_dim = gg->getCoordinateDimension();
891 int numWeightsPerCoord = gg->getNumWeights();
892 zlno_t numLocalPoints = gg->getNumLocalCoords();
893 zgno_t numGlobalPoints = gg->getNumGlobalCoords();
897 for(
int i = 0; i < coord_dim; ++i){
898 coords[i] =
new zscalar_t[numLocalPoints];
902 gg->getLocalCoordinatesCopy(coords);
906 if (numWeightsPerCoord) {
908 weight =
new zscalar_t * [numWeightsPerCoord];
909 for(
int i = 0; i < numWeightsPerCoord; ++i){
910 weight[i] =
new zscalar_t[numLocalPoints];
914 gg->getLocalWeightsCopy(weight);
921 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp =
922 rcp(
new Tpetra::Map<zlno_t, zgno_t, znode_t>(numGlobalPoints, numLocalPoints, 0, this->tcomm_));
925 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
926 for (
int i=0; i < coord_dim; i++){
927 if(numLocalPoints > 0){
928 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
932 Teuchos::ArrayView<const zscalar_t> a;
938 xyz_ = RCP<tMVector_t>(
new 943 if (numWeightsPerCoord) {
945 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > weightView(numWeightsPerCoord);
946 for (
int i=0; i < numWeightsPerCoord; i++){
947 if(numLocalPoints > 0){
948 Teuchos::ArrayView<const zscalar_t> a(weight[i], numLocalPoints);
952 Teuchos::ArrayView<const zscalar_t> a;
957 vtxWeights_ = RCP<tMVector_t>(
new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
958 numWeightsPerCoord));
962 void UserInputForTests::readGeoGenParams(
string paramFileName,
963 ParameterList &geoparams){
967 std::string input =
"";
969 for(
int i = 0; i < 25000; ++i){
974 if(this->tcomm_->getRank() == 0){
976 fstream inParam(paramFileName.c_str());
983 std::string tmp =
"";
984 getline (inParam,tmp);
985 while (!inParam.eof()){
987 tmp = trim_copy(tmp);
992 getline (inParam,tmp);
995 for (
size_t i = 0; i < input.size(); ++i){
1003 int size = (int)input.size();
1007 this->tcomm_->broadcast(0,
sizeof(
int), (
char*) &size);
1009 throw "File " + paramFileName +
" cannot be opened.";
1011 this->tcomm_->broadcast(0, size, inp);
1012 istringstream inParam(inp);
1014 getline (inParam,str);
1015 while (!inParam.eof()){
1016 if(str[0] != param_comment){
1017 size_t pos = str.find(
'=');
1018 if(pos == string::npos){
1019 throw "Invalid Line:" + str +
" in parameter file";
1021 string paramname = trim_copy(str.substr(0,pos));
1022 string paramvalue = trim_copy(str.substr(pos + 1));
1023 geoparams.set(paramname, paramvalue);
1025 getline (inParam,str);
1030 RCP<tcrsMatrix_t> UserInputForTests::modifyMatrixGIDs(
1031 RCP<tcrsMatrix_t> &inMatrix
1040 RCP<const map_t> inMap = inMatrix->getRowMap();
1042 size_t nRows = inMap->getNodeNumElements();
1043 auto inRows = inMap->getMyGlobalIndices();
1044 Teuchos::Array<zgno_t> outRows(nRows);
1045 for (
size_t i = 0; i < nRows; i++) {
1046 outRows[i] = newID(inRows[i]);
1050 RCP<map_t> outMap = rcp(
new map_t(nGlobalRows, outRows(), 0,
1053 #ifdef INCLUDE_LENGTHY_OUTPUT 1056 std::cout << inMap->getComm()->getRank() <<
" KDDKDD " 1057 <<
"nGlobal " << inMap->getGlobalNumElements() <<
" " 1058 << outMap->getGlobalNumElements() <<
"; " 1059 <<
"nLocal " << inMap->getNodeNumElements() <<
" " 1060 << outMap->getNodeNumElements() <<
"; " 1062 std::cout << inMap->getComm()->getRank() <<
" KDDKDD ";
1063 for (
size_t i = 0; i < nRows; i++)
1064 std::cout <<
"(" << inMap->getMyGlobalIndices()[i] <<
", " 1065 << outMap->getMyGlobalIndices()[i] <<
") ";
1066 std::cout << std::endl;
1068 #endif // INCLUDE_LENGTHY_OUTPUT 1072 size_t rowLen = inMatrix->getNodeMaxNumRowEntries();
1073 RCP<tcrsMatrix_t> outMatrix = rcp(
new tcrsMatrix_t(outMap, rowLen));
1075 Teuchos::Array<zgno_t> indices(rowLen);
1076 Teuchos::Array<zscalar_t> values(rowLen);
1078 for (
size_t i = 0; i < nRows; i++) {
1080 zgno_t inGid = inMap->getGlobalElement(i);
1081 inMatrix->getGlobalRowCopy(inGid, indices, values, nEntries);
1082 for (
size_t j = 0; j < nEntries; j++)
1083 indices[j] = newID(indices[j]);
1085 zgno_t outGid = outMap->getGlobalElement(i);
1086 outMatrix->insertGlobalValues(outGid, indices(0, nEntries),
1087 values(0, nEntries));
1089 outMatrix->fillComplete();
1091 #ifdef INCLUDE_LENGTHY_OUTPUT 1094 std::cout << inMap->getComm()->getRank() <<
" KDDKDD Rows " 1095 <<
"nGlobal " << inMatrix->getGlobalNumRows() <<
" " 1096 << outMatrix->getGlobalNumRows() <<
"; " 1097 <<
"nLocal " << inMatrix->getNodeNumRows() <<
" " 1098 << outMatrix->getNodeNumRows() << std::endl;
1099 std::cout << inMap->getComm()->getRank() <<
" KDDKDD NNZS " 1100 <<
"nGlobal " << inMatrix->getGlobalNumEntries() <<
" " 1101 << outMatrix->getGlobalNumEntries() <<
"; " 1102 <<
"nLocal " << inMatrix->getNodeNumEntries() <<
" " 1103 << outMatrix->getNodeNumEntries() << std::endl;
1106 Teuchos::Array<zgno_t> in(rowLen), out(rowLen);
1107 Teuchos::Array<zscalar_t> inval(rowLen), outval(rowLen);
1109 for (
size_t i = 0; i < nRows; i++) {
1110 std::cout << inMap->getComm()->getRank() <<
" KDDKDD " << i <<
" nnz(";
1111 inMatrix->getGlobalRowCopy(inMap->getGlobalElement(i), in, inval, nIn);
1112 outMatrix->getGlobalRowCopy(outMap->getGlobalElement(i), out, outval,
1115 std::cout << nIn <<
", " << nOut <<
"): ";
1116 for (
size_t j = 0; j < nIn; j++) {
1117 std::cout <<
"(" << in[j] <<
" " << inval[j] <<
", " 1118 << out[j] <<
" " << outval[j] <<
") ";
1120 std::cout << std::endl;
1123 #endif // INCLUDE_LENGTHY_OUTPUT 1130 void UserInputForTests::readMatrixMarketFile(
1133 bool distributeInput
1136 std::ostringstream fname;
1137 fname << path <<
"/" << testData <<
".mtx";
1139 if (verbose_ && tcomm_->getRank() == 0)
1140 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
1145 RCP<tcrsMatrix_t> toMatrix;
1146 RCP<tcrsMatrix_t> fromMatrix;
1149 typedef Tpetra::MatrixMarket::Reader<tcrsMatrix_t> reader_type;
1150 fromMatrix = reader_type::readSparseFile(fname.str(), tcomm_,
1151 true,
false,
false);
1152 #ifdef KDD_NOT_READY_YET 1155 fromMatrix = modifyMatrixGIDs(fromMatrix);
1158 if(!distributeInput)
1160 if (verbose_ && tcomm_->getRank() == 0)
1161 std::cout <<
"Constructing serial distribution of matrix" << std::endl;
1163 RCP<const map_t> fromMap = fromMatrix->getRowMap();
1165 size_t numGlobalCoords = fromMap->getGlobalNumElements();
1166 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
1167 RCP<const map_t> toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, 0, tcomm_));
1169 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1171 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1172 toMatrix->fillComplete();
1175 toMatrix = fromMatrix;
1177 }
catch (std::exception &e) {
1178 if (tcomm_->getRank() == 0) {
1179 std::cout <<
"UserInputForTests unable to read matrix market file:" 1180 << fname.str() << std::endl;
1181 std::cout << e.what() << std::endl;
1186 "UserInputForTests unable to read matrix market file");
1189 #ifdef INCLUDE_LENGTHY_OUTPUT 1190 std::cout << tcomm_->getRank() <<
" KDDKDD " << M_->getNodeNumRows()
1191 <<
" " << M_->getGlobalNumRows()
1192 <<
" " << M_->getNodeNumEntries()
1193 <<
" " << M_->getGlobalNumEntries() << std::endl;
1194 #endif // INCLUDE_LENGTHY_OUTPUT 1201 fname << path <<
"/" << testData <<
"_coord.mtx";
1203 size_t coordDim = 0, numGlobalCoords = 0;
1204 size_t msg[2]={0,0};
1205 ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1206 std::ifstream coordFile;
1208 if (tcomm_->getRank() == 0){
1211 std::cout <<
"UserInputForTests, Read: " <<
1212 fname.str() << std::endl;
1216 coordFile.open(fname.str().c_str());
1218 catch (std::exception &e){
1229 while (!done && !fail && coordFile.good()){
1230 coordFile.getline(c, 256);
1233 else if (c[0] ==
'%')
1237 std::istringstream s(c);
1238 s >> numGlobalCoords >> coordDim;
1239 if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1248 xyz = Teuchos::arcp(
new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1250 for (
size_t dim=0; !fail && dim < coordDim; dim++){
1256 xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1258 for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1259 coordFile.getline(c, 256);
1260 std::istringstream s(c);
1264 if (idx < numGlobalCoords)
1270 ArrayRCP<zscalar_t> emptyArray;
1271 for (
size_t dim=0; dim < coordDim; dim++)
1272 xyz[dim] = emptyArray;
1285 msg[1] = numGlobalCoords;
1289 Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1292 numGlobalCoords = msg[1];
1298 RCP<const map_t> toMap;
1301 const RCP<const map_t> &mapM = M_->getRowMap();
1305 if (verbose_ && tcomm_->getRank() == 0)
1307 std::cout <<
"Matrix was null. ";
1308 std::cout <<
"Constructing distribution map for coordinate vector." 1312 if(!distributeInput)
1314 if (verbose_ && tcomm_->getRank() == 0)
1315 std::cout <<
"Constructing serial distribution map for coordinates." 1318 size_t numLocalCoords = this->tcomm_->getRank()==0 ? numGlobalCoords : 0;
1319 toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1321 toMap = rcp(
new map_t(numGlobalCoords, base, tcomm_));
1329 ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1331 if (tcomm_->getRank() == 0){
1333 for (
size_t dim=0; dim < coordDim; dim++)
1334 coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1338 throw std::bad_alloc();
1340 ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1342 #ifdef KDD_NOT_READY_YET 1347 for (zgno_t
id=0;
id <
zgno_t(numGlobalCoords);
id++)
1351 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1352 rowIds.view(0, numGlobalCoords),
1355 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1359 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1363 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1364 ArrayView<zgno_t>(), base, tcomm_));
1366 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1370 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1374 void UserInputForTests::buildCrsMatrix(
int xdim,
int ydim,
int zdim,
1375 string problemType,
bool distributeInput)
1377 #ifdef HAVE_ZOLTAN2_GALERI 1378 Teuchos::CommandLineProcessor tclp;
1379 Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1380 xdim, ydim, zdim, problemType);
1382 RCP<const Tpetra::Map<zlno_t, zgno_t> > map;
1383 if (distributeInput)
1384 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(params.GetNumGlobalElements(),
1388 size_t nGlobalElements = params.GetNumGlobalElements();
1389 size_t nLocalElements = ((tcomm_->getRank() == 0) ? nGlobalElements : 0);
1390 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(nGlobalElements, nLocalElements, 0,
1394 if (verbose_ && tcomm_->getRank() == 0){
1396 std::cout <<
"Matrix is " << (distributeInput ?
"" :
"not");
1397 std::cout <<
"distributed." << endl;
1399 std::cout <<
"UserInputForTests, Create matrix with " << problemType;
1400 std::cout <<
" (and " << xdim;
1402 std::cout <<
" x " << ydim <<
" x " << zdim;
1404 std::cout <<
" x" << ydim <<
" x 1";
1406 std::cout <<
"x 1 x 1";
1408 std::cout <<
" mesh)" << std::endl;
1414 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 =
1415 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> >
1416 (params.GetMatrixType(), map, params.GetParameterList());
1417 M_ = Pr->BuildMatrix();
1419 catch (std::exception &e) {
1423 "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1429 if (verbose_ && tcomm_->getRank() == 0)
1431 "UserInputForTests, Implied matrix row coordinates computed" <<
1434 ArrayView<const zgno_t> gids = map->getNodeElementList();
1437 size_t pos = problemType.find(
"2D");
1438 if (pos != string::npos)
1440 else if (problemType ==
string(
"Laplace1D") ||
1441 problemType ==
string(
"Identity"))
1447 for (
int i=0; i < dim; i++){
1450 throw(std::bad_alloc());
1451 coordinates[i] = Teuchos::arcp(c, 0, count,
true);
1455 zscalar_t *x = coordinates[0].getRawPtr();
1456 zscalar_t *y = coordinates[1].getRawPtr();
1457 zscalar_t *z = coordinates[2].getRawPtr();
1458 zgno_t xySize = xdim * ydim;
1459 for (
zlno_t i=0; i < count; i++){
1460 zgno_t iz = gids[i] / xySize;
1461 zgno_t xy = gids[i] - iz*xySize;
1468 zscalar_t *x = coordinates[0].getRawPtr();
1469 zscalar_t *y = coordinates[1].getRawPtr();
1470 for (
zlno_t i=0; i < count; i++){
1476 zscalar_t *x = coordinates[0].getRawPtr();
1477 for (
zlno_t i=0; i < count; i++)
1482 Array<ArrayView<const zscalar_t> > coordView(dim);
1484 for (
int i=0; i < dim; i++)
1485 coordView[i] = coordinates[i].view(0,count);
1487 xyz_ = rcp(
new tMVector_t(map, coordView.view(0, dim), dim));
1489 throw std::runtime_error(
"Galeri input requested but Trilinos is " 1490 "not built with Galeri.");
1494 void UserInputForTests::readZoltanTestData(
string path,
string testData,
1495 bool distributeInput)
1497 int rank = tcomm_->getRank();
1498 FILE *graphFile = NULL;
1499 FILE *coordFile = NULL;
1500 FILE *assignFile = NULL;
1503 for (
int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] =
'\0';
1507 std::ostringstream chGraphFileName;
1508 chGraphFileName << path <<
"/" << testData <<
".graph";
1511 std::ostringstream chCoordFileName;
1512 chCoordFileName << path <<
"/" << testData <<
".coords";
1515 std::ostringstream chAssignFileName;
1516 chAssignFileName << path <<
"/" << testData <<
".assign";
1519 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1523 chGraphFileName.str(
"");
1524 chCoordFileName.str(
"");
1526 chGraphFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".graph";
1527 chCoordFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".coords";
1528 chAssignFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".assign";
1531 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1534 memset(fileInfo, 0,
sizeof(
int) * 3);
1537 if (verbose_ && tcomm_->getRank() == 0)
1538 std::cout <<
"UserInputForTests, open " <<
1539 chGraphFileName.str () << std::endl;
1541 coordFile = fopen(chCoordFileName.str().c_str(),
"r");
1544 if (verbose_ && tcomm_->getRank() == 0)
1545 std::cout <<
"UserInputForTests, open " <<
1546 chCoordFileName.str () << std::endl;
1549 assignFile = fopen(chAssignFileName.str().c_str(),
"r");
1552 if (verbose_ && tcomm_->getRank() == 0)
1553 std::cout <<
"UserInputForTests, open " <<
1554 chAssignFileName.str () << std::endl;
1557 if (verbose_ && tcomm_->getRank() == 0){
1558 std::cout <<
"UserInputForTests, unable to open file: ";
1559 std::cout << chGraphFileName.str() << std::endl;
1565 Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1567 bool haveGraph = (fileInfo[0] == 1);
1568 bool haveCoords = (fileInfo[1] == 1);
1569 bool haveAssign = (fileInfo[2] == 1);
1574 getUIChacoGraph(graphFile, haveAssign, assignFile,
1575 testData, distributeInput);
1582 getUIChacoCoords(coordFile, testData);
1591 void UserInputForTests::getUIChacoGraph(FILE *fptr,
bool haveAssign,
1592 FILE *assignFile,
string fname,
1593 bool distributeInput)
1595 int rank = tcomm_->getRank();
1597 int nvtxs=0, nedges=0;
1598 int nVwgts=0, nEwgts=0;
1599 int *start = NULL, *adj = NULL;
1600 float *ewgts = NULL, *vwgts = NULL;
1601 size_t *nzPerRow = NULL;
1602 size_t maxRowLen = 0;
1604 ArrayRCP<const size_t> rowSizes;
1606 bool haveEdges =
true;
1610 memset(graphCounts, 0, 5*
sizeof(
int));
1613 fail = chaco_input_graph(fptr, fname.c_str(), &start, &adj,
1614 &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1625 std::cout <<
"UserInputForTests, " << nvtxs <<
" vertices,";
1627 std::cout << start[nvtxs] <<
" edges,";
1629 std::cout <<
"no edges,";
1630 std::cout << nVwgts <<
" vertex weights, ";
1631 std::cout << nEwgts <<
" edge weights" << std::endl;
1638 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1639 throw std::runtime_error(
"Unable to read chaco file");
1643 nedges = start[nvtxs];
1645 nzPerRow =
new size_t [nvtxs];
1647 throw std::bad_alloc();
1648 rowSizes = arcp(nzPerRow, 0, nvtxs,
true);
1651 for (
int i=0; i < nvtxs; i++){
1652 nzPerRow[i] = start[i+1] - start[i];
1653 if (nzPerRow[i] > maxRowLen)
1654 maxRowLen = nzPerRow[i];
1658 memset(nzPerRow, 0,
sizeof(
size_t) * nvtxs);
1664 int chbase = adj[0];
1665 for (
int i=1; i < nedges; i++)
1666 if (adj[i] < chbase)
1670 for (
int i=0; i < nedges; i++)
1675 graphCounts[0] = nvtxs;
1676 graphCounts[1] = nedges;
1677 graphCounts[2] = nVwgts;
1678 graphCounts[3] = nEwgts;
1679 graphCounts[4] = (int)maxRowLen;
1682 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1684 if (graphCounts[0] == 0)
1685 throw std::runtime_error(
"Unable to read chaco file");
1687 haveEdges = (graphCounts[1] > 0);
1689 RCP<tcrsMatrix_t> fromMatrix;
1690 RCP<const map_t> fromMap;
1694 fromMap = rcp(
new map_t(nvtxs, nvtxs, base, tcomm_));
1697 rcp(
new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
1702 if (nedges && !edgeIds)
1703 throw std::bad_alloc();
1704 for (
int i=0; i < nedges; i++)
1705 edgeIds[i] = adj[i];
1710 zgno_t *nextId = edgeIds;
1711 Array<zscalar_t> values(nedges, 1.0);
1712 if (nedges > 0 && nEwgts > 0) {
1713 for (
int i=0; i < nedges; i++)
1714 values[i] = ewgts[i];
1719 for (
int i=0; i < nvtxs; i++){
1720 if (nzPerRow[i] > 0){
1721 ArrayView<const zgno_t> rowNz(nextId, nzPerRow[i]);
1722 fromMatrix->insertGlobalValues(i, rowNz, values.view(start[i], start[i+1] - start[i]));
1723 nextId += nzPerRow[i];
1731 fromMatrix->fillComplete();
1734 nvtxs = graphCounts[0];
1735 nedges = graphCounts[1];
1736 nVwgts = graphCounts[2];
1737 nEwgts = graphCounts[3];
1738 maxRowLen = graphCounts[4];
1742 fromMap = rcp(
new map_t(nvtxs, 0, base, tcomm_));
1745 rcp(
new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
1747 fromMatrix->fillComplete();
1752 size_t sz = fromMatrix->getNodeMaxNumRowEntries();
1753 Teuchos::Array<zgno_t> indices(sz);
1754 Teuchos::Array<zscalar_t> values(sz);
1755 for (
size_t i = 0; i < fromMatrix->getNodeNumRows(); i++) {
1756 zgno_t gid = fromMatrix->getRowMap()->getGlobalElement(i);
1758 fromMatrix->getGlobalRowCopy(gid, indices(), values(), num);
1759 std::cout <<
"ROW " << gid <<
": ";
1760 for (
size_t j = 0; j < num; j++)
1761 std::cout << indices[j] <<
" ";
1762 std::cout << std::endl;
1767 RCP<const map_t> toMap;
1768 RCP<tcrsMatrix_t> toMatrix;
1769 RCP<import_t> importer;
1771 if (distributeInput) {
1774 short *assignments =
new short[nvtxs];
1776 fail = chaco_input_assign(assignFile, fname.c_str(), nvtxs, assignments);
1779 Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1782 Teuchos::Array<zgno_t> mine;
1783 for (
int i = 0; i < nvtxs; i++) {
1784 if (assignments[i] == rank)
1789 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1790 toMap = rcp(
new map_t(dummy, mine(), base, tcomm_));
1791 delete [] assignments;
1795 toMap = rcp(
new map_t(nvtxs, base, tcomm_));
1800 importer = rcp(
new import_t(fromMap, toMap));
1801 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1802 toMatrix->fillComplete();
1806 toMatrix = fromMatrix;
1813 typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1817 ArrayRCP<zscalar_t> weightBuf;
1818 ArrayView<const zscalar_t> *wgts =
new ArrayView<const zscalar_t> [nVwgts];
1821 size_t len = nVwgts * nvtxs;
1823 if (!buf)
throw std::bad_alloc();
1824 weightBuf = arcp(buf, 0, len,
true);
1826 for (
int widx=0; widx < nVwgts; widx++){
1827 wgts[widx] = ArrayView<const zscalar_t>(buf, nvtxs);
1828 float *vw = vwgts + widx;
1829 for (
int i=0; i < nvtxs; i++, vw += nVwgts)
1838 arrayArray_t vweights = arcp(wgts, 0, nVwgts,
true);
1840 RCP<tMVector_t> fromVertexWeights =
1841 rcp(
new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1843 RCP<tMVector_t> toVertexWeights;
1844 if (distributeInput) {
1845 toVertexWeights = rcp(
new tMVector_t(toMap, nVwgts));
1846 toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1849 toVertexWeights = fromVertexWeights;
1851 vtxWeights_ = toVertexWeights;
1856 if (haveEdges && nEwgts > 0){
1906 toMap = rcp(
new map_t(nedges, M_->getNodeNumEntries(), base, tcomm_));
1907 edgWeights_ = rcp(
new tMVector_t(toMap, nEwgts));
1909 size_t maxSize = M_->getNodeMaxNumRowEntries();
1910 Array<zlno_t> colind(maxSize);
1911 Array<zscalar_t>
vals(maxSize);
1914 for (
size_t i = 0, idx = 0; i < M_->getNodeNumRows(); i++) {
1915 M_->getLocalRowCopy(i, colind, vals, nEntries);
1916 for (
size_t j = 0; j < nEntries; j++) {
1917 edgWeights_->replaceLocalValue(idx, 0, vals[j]);
1929 void UserInputForTests::getUIChacoCoords(FILE *fptr,
string fname)
1931 int rank = tcomm_->getRank();
1933 double *x=NULL, *y=NULL, *z=NULL;
1936 size_t globalNumVtx = M_->getGlobalNumRows();
1943 fail = chaco_input_geom(fptr, fname.c_str(), (int)globalNumVtx,
1950 std::cout <<
"UserInputForTests, read " << globalNumVtx;
1951 std::cout <<
" " << ndim <<
"-dimensional coordinates." << std::endl;
1955 Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1958 throw std::runtime_error(
"Can't read coordinate file");
1960 ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1965 for (
int dim=0; dim < ndim; dim++){
1968 throw std::bad_alloc();
1969 coords[dim] = arcp<const zscalar_t>(v, 0, globalNumVtx,
true);
1970 double *val = (dim==0 ? x : (dim==1 ? y : z));
1971 for (
size_t i=0; i < globalNumVtx; i++)
1977 len =
static_cast<zlno_t>(globalNumVtx);
1980 RCP<const map_t> fromMap = rcp(
new map_t(globalNumVtx, len, 0, tcomm_));
1981 RCP<const map_t> toMap = M_->getRowMap();
1982 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1984 Array<ArrayView<const zscalar_t> > coordData;
1985 for (
int dim=0; dim < ndim; dim++)
1986 coordData.push_back(coords[dim].view(0, len));
1988 RCP<tMVector_t> fromCoords =
1989 rcp(
new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1991 RCP<tMVector_t> toCoords = rcp(
new tMVector_t(toMap, ndim));
1993 toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
2002 double UserInputForTests::chaco_read_val(
2018 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2019 if (chaco_offset >= chaco_break_pnt) {
2020 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2022 ptr = &chaco_line[chaco_save_pnt];
2023 for (i=length_left; i; i--) *ptr2++ = *ptr++;
2024 length = chaco_save_pnt + 1;
2027 length = CHACO_LINE_LENGTH;
2032 ptr2 = fgets(&chaco_line[length_left], length, infile);
2034 if (ptr2 == (
char *) NULL) {
2036 return((
double) 0.0);
2039 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2040 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2042 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2043 chaco_save_pnt = chaco_break_pnt;
2048 if (chaco_line[chaco_break_pnt] !=
'\0') {
2049 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2051 chaco_save_pnt = chaco_break_pnt + 1;
2055 else if (white_seen) {
2062 chaco_break_pnt = CHACO_LINE_LENGTH;
2068 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2069 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2071 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2072 chaco_flush_line(infile);
2074 return((
double) 0.0);
2077 ptr = &(chaco_line[chaco_offset]);
2078 val = strtod(ptr, &ptr2);
2083 return((
double) 0.0);
2086 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2093 int UserInputForTests::chaco_read_int(
2109 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2110 if (chaco_offset >= chaco_break_pnt) {
2111 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2113 ptr = &chaco_line[chaco_save_pnt];
2114 for (i=length_left; i; i--) *ptr2++ = *ptr++;
2115 length = chaco_save_pnt + 1;
2118 length = CHACO_LINE_LENGTH;
2123 ptr2 = fgets(&chaco_line[length_left], length, infile);
2125 if (ptr2 == (
char *) NULL) {
2130 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2131 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2133 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2134 chaco_save_pnt = chaco_break_pnt;
2139 if (chaco_line[chaco_break_pnt] !=
'\0') {
2140 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2142 chaco_save_pnt = chaco_break_pnt + 1;
2146 else if (white_seen) {
2153 chaco_break_pnt = CHACO_LINE_LENGTH;
2159 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2160 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2162 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2163 chaco_flush_line(infile);
2168 ptr = &(chaco_line[chaco_offset]);
2169 val = (int) strtol(ptr, &ptr2, 10);
2177 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2183 void UserInputForTests::chaco_flush_line(
2190 while (c !=
'\n' && c !=
'\f')
2194 int UserInputForTests::chaco_input_graph(
2244 while (end_flag == 1) {
2245 *nvtxs = chaco_read_int(fin, &end_flag);
2249 printf(
"ERROR in graph file `%s':", inname);
2250 printf(
" Invalid number of vertices (%d).\n", *nvtxs);
2255 narcs = chaco_read_int(fin, &end_flag);
2257 printf(
"ERROR in graph file `%s':", inname);
2258 printf(
" Invalid number of expected edges (%d).\n", narcs);
2265 option = chaco_read_int(fin, &end_flag);
2267 using_ewgts = option - 10 * (option / 10);
2269 using_vwgts = option - 10 * (option / 10);
2271 vtxnums = option - 10 * (option / 10);
2274 (*nVwgts) = using_vwgts;
2275 (*nEwgts) = using_ewgts;
2278 if (!end_flag && using_vwgts==1){
2279 j = chaco_read_int(fin, &end_flag);
2280 if (!end_flag) (*nVwgts) = j;
2282 if (!end_flag && using_ewgts==1){
2283 j = chaco_read_int(fin, &end_flag);
2284 if (!end_flag) (*nEwgts) = j;
2289 j = chaco_read_int(fin, &end_flag);
2292 *start = (
int *) malloc((
unsigned) (*nvtxs + 1) *
sizeof(
int));
2294 *adjacency = (
int *) malloc((
unsigned) (2 * narcs + 1) *
sizeof(
int));
2299 *vweights = (
float *) malloc((
unsigned) (*nvtxs) * (*nVwgts) *
sizeof(float));
2304 *eweights = (
float *)
2305 malloc((
unsigned) (2 * narcs + 1) * (*nEwgts) *
sizeof(float));
2309 adjptr = *adjacency;
2318 while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2323 j = chaco_read_int(fin, &end_flag);
2325 if (vertex == *nvtxs)
2327 printf(
"ERROR in graph file `%s':", inname);
2328 printf(
" no vertex number in line %d.\n", line_num);
2332 if (j != vertex && j != vertex + 1) {
2333 printf(
"ERROR in graph file `%s':", inname);
2334 printf(
" out-of-order vertex number in line %d.\n", line_num);
2348 if (vertex > *nvtxs)
2352 if (using_vwgts && new_vertex) {
2353 for (j=0; j<(*nVwgts); j++){
2354 weight = chaco_read_val(fin, &end_flag);
2356 printf(
"ERROR in graph file `%s':", inname);
2357 printf(
" not enough weights for vertex %d.\n", vertex);
2361 (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2368 neighbor = chaco_read_int(fin, &end_flag);
2374 for (j=0; j<(*nEwgts); j++){
2375 eweight = chaco_read_val(fin, &end_flag);
2378 printf(
"ERROR in graph file `%s':", inname);
2379 printf(
" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2392 if (++nedges > 2*narcs) {
2393 printf(
"ERROR in graph file `%s':", inname);
2394 printf(
" at least %d adjacencies entered, but nedges = %d\n",
2399 *adjptr++ = neighbor;
2404 neighbor = chaco_read_int(fin, &end_flag);
2408 (*start)[vertex] = sum_edges;
2413 while (!flag && end_flag != -1) {
2414 chaco_read_int(fin, &end_flag);
2419 (*start)[*nvtxs] = sum_edges;
2427 if (*adjacency != NULL)
2430 if (*eweights != NULL)
2439 if (*adjacency != NULL)
2441 if (*vweights != NULL)
2443 if (*eweights != NULL)
2451 return (error_flag);
2455 int UserInputForTests::chaco_input_geom(
2457 const char *geomname,
2465 double xc, yc, zc =0;
2473 *x = *y = *z = NULL;
2476 while (end_flag == 1) {
2477 xc = chaco_read_val(fingeom, &end_flag);
2481 if (end_flag == -1) {
2482 printf(
"No values found in geometry file `%s'\n", geomname);
2488 yc = chaco_read_val(fingeom, &end_flag);
2489 if (end_flag == 0) {
2491 zc = chaco_read_val(fingeom, &end_flag);
2492 if (end_flag == 0) {
2494 chaco_read_val(fingeom, &end_flag);
2496 printf(
"Too many values on input line of geometry file `%s'\n",
2499 printf(
" Maximum dimensionality is 3\n");
2508 *x = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2511 *y = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2515 *z = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2519 for (nread = 1; nread < nvtxs; nread++) {
2522 i = fscanf(fingeom,
"%lf", &((*x)[nread]));
2524 else if (ndims == 2) {
2525 i = fscanf(fingeom,
"%lf%lf", &((*x)[nread]), &((*y)[nread]));
2527 else if (ndims == 3) {
2528 i = fscanf(fingeom,
"%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2533 printf(
"Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2538 else if (i != ndims) {
2539 printf(
"Wrong number of values in line %d of geometry file `%s'\n",
2540 line_num, geomname);
2549 while (!flag && end_flag != -1) {
2550 chaco_read_val(fingeom, &end_flag);
2562 int UserInputForTests::chaco_input_assign(
2564 const char *inassignname,
2575 while (end_flag == 1) {
2576 assignment[0] = chaco_read_int(finassign, &end_flag);
2579 if (assignment[0] < 0) {
2580 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2581 1, inassignname, assignment[0]);
2586 if (end_flag == -1) {
2587 printf(
"ERROR: No values found in assignment file `%s'\n", inassignname);
2593 if (assignment[0] > nvtxs)
2594 flag = assignment[1];
2595 for (i = 1; i < nvtxs; i++) {
2596 j = fscanf(finassign,
"%hd", &(assignment[i]));
2598 printf(
"ERROR: Too few values in assignment file `%s'.\n", inassignname);
2602 if (assignment[i] < 0) {
2603 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2604 i+1, inassignname, assignment[i]);
2608 if (assignment[i] > nvtxs) {
2609 if (assignment[i] > flag)
2610 flag = assignment[i];
2615 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2616 printf(
" More assignment sets (%d) than vertices (%d)\n", flag, nvtxs);
2622 while (!flag && end_flag != -1) {
2623 chaco_read_int(finassign, &end_flag);
2628 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2629 printf(
" Numerical data found after expected end of file\n");
2637 void UserInputForTests::readPamgenMeshFile(
string path,
string testData)
2639 #ifdef HAVE_ZOLTAN2_PAMGEN 2640 int rank = this->tcomm_->getRank();
2641 if (verbose_ && tcomm_->getRank() == 0)
2642 std::cout <<
"UserInputForTestsBD::readPamgenFile, Read: " << testData << std::endl;
2649 std::ostringstream meshFileName;
2650 meshFileName << path <<
"/" << testData <<
".pmgen";
2653 file.open(meshFileName.str(), ios::in);
2657 if(verbose_ && tcomm_->getRank() == 0)
2659 std::cout <<
"Unable to open pamgen mesh: ";
2660 std::cout << meshFileName.str();
2661 std::cout <<
"\nPlease check file path and name." << std::endl;
2667 file.seekg (0,file.end);
2674 while(std::getline(file,line))
2676 if( line.find(
"nz") != std::string::npos ||
2677 line.find(
"nphi") != std::string::npos)
2685 file.seekg(0, ios::beg);
2690 this->tcomm_->broadcast(0,
sizeof(
int), (
char *)&dimension);
2691 this->tcomm_->broadcast(0,
sizeof(
size_t),(
char *)&len);
2692 this->tcomm_->barrier();
2695 if(verbose_ && tcomm_->getRank() == 0)
2696 std::cout <<
"Pamgen Mesh file size == 0, exiting UserInputForTests early." << endl;
2700 char * file_data =
new char[len+1];
2701 file_data[len] =
'\0';
2703 file.read(file_data,len);
2707 this->tcomm_->broadcast(0,(
int)len+1,file_data);
2708 this->tcomm_->barrier();
2712 this->pamgen_mesh = rcp(
new PamgenMesh);
2713 this->havePamgenMesh =
true;
2714 pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2717 pamgen_mesh->storeMesh();
2718 this->tcomm_->barrier();
2721 this->setPamgenCoordinateMV();
2724 this->setPamgenAdjacencyGraph();
2726 this->tcomm_->barrier();
2727 if(rank == 0) file.close();
2728 delete [] file_data;
2730 throw std::runtime_error(
"Pamgen requested but Trilinos " 2731 "not built with Pamgen");
2735 #ifdef HAVE_ZOLTAN2_PAMGEN 2736 void UserInputForTests::setPamgenCoordinateMV()
2738 int dimension = pamgen_mesh->num_dim;
2742 zgno_t numelements = pamgen_mesh->num_elem;
2743 zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2746 for(
int i = 0; i < dimension; ++i){
2747 elem_coords[i] =
new zscalar_t[numelements];
2748 memcpy(elem_coords[i],&pamgen_mesh->element_coord[i*numelements],
sizeof(
double) * numelements);
2752 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2753 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2757 Array<zgno_t> elementList(numelements);
2758 for (Array<zgno_t>::size_type k = 0; k < numelements; ++k) {
2759 elementList[k] = pamgen_mesh->element_order_map[k];
2762 mp = rcp (
new map_t (numGlobalElements, elementList, 0, this->tcomm_));
2766 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(dimension);
2767 for (
int i = 0; i < dimension; i++){
2768 if(numelements > 0){
2769 Teuchos::ArrayView<const zscalar_t> a(elem_coords[i], numelements);
2773 Teuchos::ArrayView<const zscalar_t> a;
2779 xyz_ = RCP<tMVector_t>(
new 2784 void UserInputForTests::setPamgenAdjacencyGraph()
2792 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2795 size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2796 size_t local_els = (
size_t)this->pamgen_mesh->num_elem;
2798 size_t global_els = (size_t)this->pamgen_mesh->num_elems_global;
2799 size_t global_nodes = (
size_t)this->pamgen_mesh->num_nodes_global;
2803 RCP<const map_t> rowMap = rcp(
new map_t(global_els,0,this->tcomm_));
2804 RCP<const map_t> rangeMap = rowMap;
2807 RCP<const map_t> domainMap = rcp(
new map_t(global_nodes,0,this->tcomm_));
2810 Teuchos::RCP<tcrsMatrix_t> C = rcp(
new tcrsMatrix_t(rowMap,0));
2813 Array<zgno_t> g_el_ids(local_els);
2814 for (
size_t k = 0; k < local_els; ++k) {
2815 g_el_ids[k] = pamgen_mesh->global_element_numbers[k]-1;
2818 Array<zgno_t> g_node_ids(local_nodes);
2819 for (
size_t k = 0; k < local_nodes; ++k) {
2820 g_node_ids[k] = pamgen_mesh->global_node_numbers[k]-1;
2823 int blks = this->pamgen_mesh->num_elem_blk;
2829 for(
int i = 0; i < blks; i++)
2831 int el_per_block = this->pamgen_mesh->elements[i];
2832 int nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2833 int * connect = this->pamgen_mesh->elmt_node_linkage[i];
2835 for(
int j = 0; j < el_per_block; j++)
2837 const zgno_t gid =
static_cast<zgno_t>(g_el_ids[el_no]);
2838 for(
int k = 0; k < nodes_per_el; k++)
2840 int g_node_i = g_node_ids[connect[j*nodes_per_el+k]-1];
2841 C->insertGlobalValues(gid,
2842 Teuchos::tuple<zgno_t>(g_node_i),
2843 Teuchos::tuple<zscalar_t>(one));
2849 C->fillComplete(domainMap, rangeMap);
2855 Tpetra::MatrixMatrix::Multiply(*C,
false, *C,
true, *A);
2863 for(
zgno_t gid : rowMap->getNodeElementList())
2865 size_t numEntriesInRow = A->getNumEntriesInGlobalRow (gid);
2866 Array<zscalar_t> rowvals (numEntriesInRow);
2867 Array<zgno_t> rowinds (numEntriesInRow);
2870 Array<zscalar_t> mod_rowvals;
2871 Array<zgno_t> mod_rowinds;
2872 A->getGlobalRowCopy (gid, rowinds (), rowvals (), numEntriesInRow);
2873 for (
size_t i = 0; i < numEntriesInRow; i++) {
2876 if (rowvals[i] >= 1)
2878 mod_rowvals.push_back(one);
2879 mod_rowinds.push_back(rowinds[i]);
2882 this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2885 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=" \\\)
Tpetra::global_size_t global_size_t
string trim_left_copy(const string &s, const string &delimiters=" \\\)