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;
92 using namespace Zoltan2_TestingFramework;
130 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
131 typedef Tpetra::Export<zlno_t, zgno_t, znode_t>
export_t;
132 typedef Tpetra::Import<zlno_t, zgno_t, znode_t>
import_t;
156 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
157 bool distributeInput=
true);
176 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
177 bool distributeInput=
true);
195 const RCP<
const Comm<int> > &c);
199 static void getUIRandomData(
unsigned int seed,
zlno_t length,
202 RCP<tMVector_t> getUICoordinates();
204 RCP<tMVector_t> getUIWeights();
206 RCP<tMVector_t> getUIEdgeWeights();
208 RCP<tcrsMatrix_t> getUITpetraCrsMatrix();
210 RCP<tcrsGraph_t> getUITpetraCrsGraph();
212 RCP<tVector_t> getUITpetraVector();
214 RCP<tMVector_t> getUITpetraMultiVector(
int nvec);
216 RCP<xcrsMatrix_t> getUIXpetraCrsMatrix();
218 RCP<xcrsGraph_t> getUIXpetraCrsGraph();
220 RCP<xVector_t> getUIXpetraVector();
222 RCP<xMVector_t> getUIXpetraMultiVector(
int nvec);
224 #ifdef HAVE_ZOLTAN2_PAMGEN
225 PamgenMesh * getPamGenMesh(){
return this->pamgen_mesh.operator->();}
228 #ifdef HAVE_EPETRA_DATA_TYPES
229 RCP<Epetra_CrsGraph> getUIEpetraCrsGraph();
231 RCP<Epetra_CrsMatrix> getUIEpetraCrsMatrix();
233 RCP<Epetra_Vector> getUIEpetraVector();
235 RCP<Epetra_MultiVector> getUIEpetraMultiVector(
int nvec);
239 bool hasInputDataType(
const string &input_type);
241 bool hasUICoordinates();
245 bool hasUIEdgeWeights();
247 bool hasUITpetraCrsMatrix();
249 bool hasUITpetraCrsGraph();
251 bool hasUITpetraVector();
253 bool hasUITpetraMultiVector();
255 bool hasUIXpetraCrsMatrix();
257 bool hasUIXpetraCrsGraph();
259 bool hasUIXpetraVector();
261 bool hasUIXpetraMultiVector();
263 bool hasPamgenMesh();
264 #ifdef HAVE_EPETRA_DATA_TYPES
265 bool hasUIEpetraCrsGraph();
267 bool hasUIEpetraCrsMatrix();
269 bool hasUIEpetraVector();
271 bool hasUIEpetraMultiVector();
279 const RCP<const Comm<int> > tcomm_;
282 #ifdef HAVE_ZOLTAN2_PAMGEN
283 RCP<PamgenMesh> pamgen_mesh;
286 RCP<tcrsMatrix_t> M_;
287 RCP<xcrsMatrix_t> xM_;
289 RCP<tMVector_t> xyz_;
290 RCP<tMVector_t> vtxWeights_;
291 RCP<tMVector_t> edgWeights_;
293 #ifdef HAVE_EPETRA_DATA_TYPES
294 RCP<const Epetra_Comm> ecomm_;
295 RCP<Epetra_CrsMatrix> eM_;
296 RCP<Epetra_CrsGraph> eG_;
304 void readMatrixMarketFile(
string path,
string testData,
bool distributeInput =
true);
309 void buildCrsMatrix(
int xdim,
int ydim,
int zdim,
string type,
310 bool distributeInput);
316 void readZoltanTestData(
string path,
string testData,
317 bool distributeInput);
320 RCP<tcrsMatrix_t> modifyMatrixGIDs(RCP<tcrsMatrix_t> &in);
321 inline zgno_t newID(
const zgno_t id) {
return id * 2 + 10001; }
324 void getUIChacoGraph(FILE *fptr,
bool haveAssign, FILE *assignFile,
325 string name,
bool distributeInput);
328 void getUIChacoCoords(FILE *fptr,
string name);
334 static const int CHACO_LINE_LENGTH=200;
335 char chaco_line[CHACO_LINE_LENGTH];
340 double chaco_read_val(FILE* infile,
int *end_flag);
341 int chaco_read_int(FILE* infile,
int *end_flag);
342 void chaco_flush_line(FILE*);
345 int chaco_input_graph(FILE *fin,
const char *inname,
int **start,
346 int **adjacency,
int *nvtxs,
int *nVwgts,
347 float **vweights,
int *nEwgts,
float **eweights);
350 int chaco_input_geom(FILE *fingeom,
const char *geomname,
int nvtxs,
351 int *igeom,
double **x,
double **y,
double **z);
354 int chaco_input_assign(FILE *finassign,
const char *assignname,
int nvtxs,
362 void readGeometricGenTestData(
string path,
string testData);
366 ParameterList &geoparams);
371 const string& delimiters =
" \f\n\r\t\v" );
374 const string& delimiters =
" \f\n\r\t\v" );
377 const string& delimiters =
" \f\n\r\t\v" );
381 void readPamgenMeshFile(
string path,
string testData);
382 #ifdef HAVE_ZOLTAN2_PAMGEN
383 void setPamgenAdjacencyGraph();
384 void setPamgenCoordinateMV();
389 const RCP<
const Comm<int> > &c,
390 bool debugInfo,
bool distributeInput):
391 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
392 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
394 ecomm_(), eM_(), eG_(),
396 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
398 bool zoltan1 =
false;
399 string::size_type loc = path.find(
"/zoltan/test/");
400 if (loc != string::npos)
404 readZoltanTestData(path, testData, distributeInput);
406 readMatrixMarketFile(path, testData);
408 #ifdef HAVE_EPETRA_DATA_TYPES
409 ecomm_ = Xpetra::toEpetra(c);
415 const RCP<
const Comm<int> > &c,
417 bool distributeInput):
418 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
419 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
421 ecomm_(), eM_(), eG_(),
423 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
425 if (matrixType.size() == 0){
431 matrixType = string(
"Laplace1D");
433 matrixType = string(
"Laplace2D");
435 matrixType = string(
"Laplace3D");
437 throw std::runtime_error(
"input");
439 if (verbose_ && tcomm_->getRank() == 0)
440 std::cout <<
"UserInputForTests, Matrix type : " << matrixType << std::endl;
443 buildCrsMatrix(x, y, z, matrixType, distributeInput);
445 #ifdef HAVE_EPETRA_DATA_TYPES
446 ecomm_ = Xpetra::toEpetra(c);
451 const RCP<
const Comm<int> > &c):
452 tcomm_(c), havePamgenMesh(false),
453 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
455 ecomm_(), eM_(), eG_(),
457 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
461 bool distributeInput =
true, debugInfo =
true;
463 if(pList.isParameter(
"distribute input"))
464 distributeInput = pList.get<
bool>(
"distribute input");
466 if(pList.isParameter(
"debug"))
467 debugInfo = pList.get<
bool>(
"debug");
468 this->verbose_ = debugInfo;
470 if(pList.isParameter(
"input file"))
475 if(pList.isParameter(
"input path"))
476 path = pList.get<
string>(
"input path");
478 string testData = pList.get<
string>(
"input file");
484 if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Geometric Generator")
486 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Pamgen")
490 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Chaco")
494 switch (file_format) {
495 case GEOMGEN: readGeometricGenTestData(path,testData);
break;
496 case PAMGEN: readPamgenMeshFile(path,testData);
break;
497 case CHACO: readZoltanTestData(path, testData, distributeInput);
break;
498 default: readMatrixMarketFile(path, testData, distributeInput);
break;
501 }
else if(pList.isParameter(
"x") || pList.isParameter(
"y") || pList.isParameter(
"z")){
505 if(pList.isParameter(
"x")) x = pList.get<
int>(
"x");
506 if(pList.isParameter(
"y")) y = pList.get<
int>(
"y");
507 if(pList.isParameter(
"z")) z = pList.get<
int>(
"z");
509 string problemType =
"";
510 if(pList.isParameter(
"equation type")) problemType = pList.get<
string>(
"equation type");
512 if (problemType.size() == 0){
518 problemType = string(
"Laplace1D");
520 problemType = string(
"Laplace2D");
522 problemType = string(
"Laplace3D");
524 throw std::runtime_error(
"input");
526 if (verbose_ && tcomm_->getRank() == 0)
527 std::cout <<
"UserInputForTests, Matrix type : " << problemType << std::endl;
531 buildCrsMatrix(x, y, z, problemType, distributeInput);
534 std::cerr <<
"Input file block undefined!" << std::endl;
537 #ifdef HAVE_EPETRA_DATA_TYPES
538 ecomm_ = Xpetra::toEpetra(c);
547 throw std::runtime_error(
"could not read coord file");
564 throw std::runtime_error(
"could not read mtx file");
571 throw std::runtime_error(
"could not read mtx file");
572 return rcp_const_cast<
tcrsGraph_t>(M_->getCrsGraph());
577 RCP<tVector_t> V = rcp(
new tVector_t(M_->getRowMap(), 1));
585 RCP<tMVector_t> mV = rcp(
new tMVector_t(M_->getRowMap(), nvec));
594 throw std::runtime_error(
"could not read mtx file");
601 throw std::runtime_error(
"could not read mtx file");
602 return rcp_const_cast<
xcrsGraph_t>(xM_->getCrsGraph());
616 #ifdef HAVE_EPETRA_DATA_TYPES
617 RCP<Epetra_CrsGraph> UserInputForTests::getUIEpetraCrsGraph()
620 throw std::runtime_error(
"could not read mtx file");
621 RCP<const tcrsGraph_t> tgraph = M_->getCrsGraph();
622 RCP<const Tpetra::Map<zlno_t, zgno_t> > trowMap = tgraph->getRowMap();
623 RCP<const Tpetra::Map<zlno_t, zgno_t> > tcolMap = tgraph->getColMap();
625 int nElts =
static_cast<int>(trowMap->getGlobalNumElements());
626 int nMyElts =
static_cast<int>(trowMap->getNodeNumElements());
628 ArrayView<const int> gids = trowMap->getNodeElementList();
630 Epetra_BlockMap erowMap(nElts, nMyElts,
631 gids.getRawPtr(), 1, base, *ecomm_);
633 Array<int> rowSize(nMyElts);
634 for (
int i=0; i < nMyElts; i++){
635 rowSize[i] =
static_cast<int>(M_->getNumEntriesInLocalRow(i));
638 size_t maxRow = M_->getNodeMaxNumRowEntries();
639 Array<int> colGids(maxRow);
640 ArrayView<const int> colLid;
642 eG_ = rcp(
new Epetra_CrsGraph(Copy, erowMap,
643 rowSize.getRawPtr(),
true));
645 for (
int i=0; i < nMyElts; i++){
646 tgraph->getLocalRowView(i, colLid);
647 for (
int j=0; j < colLid.size(); j++)
648 colGids[j] = tcolMap->getGlobalElement(colLid[j]);
649 eG_->InsertGlobalIndices(gids[i], rowSize[i], colGids.getRawPtr());
655 RCP<Epetra_CrsMatrix> UserInputForTests::getUIEpetraCrsMatrix()
658 throw std::runtime_error(
"could not read mtx file");
659 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
660 eM_ = rcp(
new Epetra_CrsMatrix(Copy, *egraph));
662 size_t maxRow = M_->getNodeMaxNumRowEntries();
663 int nrows = egraph->NumMyRows();
664 const Epetra_BlockMap &rowMap = egraph->RowMap();
665 const Epetra_BlockMap &colMap = egraph->ColMap();
666 Array<int> colGid(maxRow);
668 for (
int i=0; i < nrows; i++){
669 ArrayView<const int> colLid;
670 ArrayView<const zscalar_t> nz;
671 M_->getLocalRowView(i, colLid, nz);
672 size_t rowSize = colLid.size();
673 int rowGid = rowMap.GID(i);
674 for (
size_t j=0; j < rowSize; j++){
675 colGid[j] = colMap.GID(colLid[j]);
677 eM_->InsertGlobalValues(rowGid, (
int)rowSize, nz.getRawPtr(), colGid.getRawPtr());
683 RCP<Epetra_Vector> UserInputForTests::getUIEpetraVector()
685 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
686 RCP<Epetra_Vector> V = rcp(
new Epetra_Vector(egraph->RowMap()));
691 RCP<Epetra_MultiVector> UserInputForTests::getUIEpetraMultiVector(
int nvec)
693 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
694 RCP<Epetra_MultiVector> mV =
695 rcp(
new Epetra_MultiVector(egraph->RowMap(), nvec));
705 this->hasUITpetraCrsMatrix() || \
706 this->hasUITpetraCrsGraph() || \
707 this->hasPamgenMesh();
712 if(input_type ==
"coordinates")
714 else if(input_type ==
"tpetra_vector")
716 else if(input_type ==
"tpetra_multivector")
718 else if(input_type ==
"tpetra_crs_graph")
720 else if(input_type ==
"tpetra_crs_matrix")
722 else if(input_type ==
"xpetra_vector")
724 else if(input_type ==
"xpetra_multivector")
726 else if(input_type ==
"xpetra_crs_graph")
728 else if(input_type ==
"xpetra_crs_matrix")
730 #ifdef HAVE_EPETRA_DATA_TYPES
731 else if(input_type ==
"epetra_vector")
732 return this->hasUIEpetraVector();
733 else if(input_type ==
"epetra_multivector")
734 return this->hasUIEpetraMultiVector();
735 else if(input_type ==
"epetra_crs_graph")
736 return this->hasUIEpetraCrsGraph();
737 else if(input_type ==
"epetra_crs_matrix")
738 return this->hasUIEpetraCrsMatrix();
746 return xyz_.is_null() ?
false :
true;
751 return vtxWeights_.is_null() ?
false :
true;
756 return edgWeights_.is_null() ?
false :
true;
761 return M_.is_null() ?
false :
true;
766 return M_.is_null() ?
false :
true;
781 return M_.is_null() ?
false :
true;
786 return M_.is_null() ?
false :
true;
801 return this->havePamgenMesh;
804 #ifdef HAVE_EPETRA_DATA_TYPES
805 bool UserInputForTests::hasUIEpetraCrsGraph()
807 return M_.is_null() ?
false :
true;
810 bool UserInputForTests::hasUIEpetraCrsMatrix()
812 return hasUIEpetraCrsGraph();
815 bool UserInputForTests::hasUIEpetraVector()
817 return hasUIEpetraCrsGraph();
820 bool UserInputForTests::hasUIEpetraMultiVector()
822 return hasUIEpetraCrsGraph();
828 ArrayView<ArrayRCP<zscalar_t > > data)
833 size_t dim = data.size();
834 for (
size_t i=0; i < dim; i++){
837 throw (std::bad_alloc());
838 data[i] = Teuchos::arcp(tmp, 0, length,
true);
841 zscalar_t scalingFactor = (max-min) / RAND_MAX;
843 for (
size_t i=0; i < dim; i++){
845 for (
zlno_t j=0; j < length; j++)
846 *x++ = min + (
zscalar_t(rand()) * scalingFactor);
852 string UserInputForTests::trim_right_copy(
854 const string& delimiters)
856 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
859 string UserInputForTests::trim_left_copy(
861 const string& delimiters)
863 return s.substr( s.find_first_not_of( delimiters ) );
866 string UserInputForTests::trim_copy(
868 const string& delimiters)
870 return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
873 void UserInputForTests::readGeometricGenTestData(
string path,
877 std::ostringstream fname;
878 fname << path <<
"/" << testData <<
".txt";
880 if (verbose_ && tcomm_->getRank() == 0)
881 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
883 Teuchos::ParameterList geoparams(
"geo params");
884 readGeoGenParams(fname.str(),geoparams);
889 int coord_dim = gg->getCoordinateDimension();
890 int numWeightsPerCoord = gg->getNumWeights();
891 zlno_t numLocalPoints = gg->getNumLocalCoords();
892 zgno_t numGlobalPoints = gg->getNumGlobalCoords();
896 for(
int i = 0; i < coord_dim; ++i){
897 coords[i] =
new zscalar_t[numLocalPoints];
901 gg->getLocalCoordinatesCopy(coords);
905 if (numWeightsPerCoord) {
907 weight =
new zscalar_t * [numWeightsPerCoord];
908 for(
int i = 0; i < numWeightsPerCoord; ++i){
909 weight[i] =
new zscalar_t[numLocalPoints];
913 gg->getLocalWeightsCopy(weight);
920 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp =
921 rcp(
new Tpetra::Map<zlno_t, zgno_t, znode_t>(numGlobalPoints, numLocalPoints, 0, this->tcomm_));
924 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
925 for (
int i=0; i < coord_dim; i++){
926 if(numLocalPoints > 0){
927 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
931 Teuchos::ArrayView<const zscalar_t> a;
937 xyz_ = RCP<tMVector_t>(
new
942 if (numWeightsPerCoord) {
944 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > weightView(numWeightsPerCoord);
945 for (
int i=0; i < numWeightsPerCoord; i++){
946 if(numLocalPoints > 0){
947 Teuchos::ArrayView<const zscalar_t> a(weight[i], numLocalPoints);
951 Teuchos::ArrayView<const zscalar_t> a;
956 vtxWeights_ = RCP<tMVector_t>(
new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
957 numWeightsPerCoord));
961 void UserInputForTests::readGeoGenParams(
string paramFileName,
962 ParameterList &geoparams){
966 std::string input =
"";
968 for(
int i = 0; i < 25000; ++i){
973 if(this->tcomm_->getRank() == 0){
975 std::fstream inParam(paramFileName.c_str());
982 std::string tmp =
"";
983 getline (inParam,tmp);
984 while (!inParam.eof()){
986 tmp = trim_copy(tmp);
991 getline (inParam,tmp);
994 for (
size_t i = 0; i < input.size(); ++i){
1002 int size = (int)input.size();
1006 this->tcomm_->broadcast(0,
sizeof(
int), (
char*) &size);
1008 throw "File " + paramFileName +
" cannot be opened.";
1010 this->tcomm_->broadcast(0, size, inp);
1011 std::istringstream inParam(inp);
1013 getline (inParam,str);
1014 while (!inParam.eof()){
1015 if(str[0] != param_comment){
1016 size_t pos = str.find(
'=');
1017 if(pos == string::npos){
1018 throw "Invalid Line:" + str +
" in parameter file";
1020 string paramname = trim_copy(str.substr(0,pos));
1021 string paramvalue = trim_copy(str.substr(pos + 1));
1022 geoparams.set(paramname, paramvalue);
1024 getline (inParam,str);
1029 RCP<tcrsMatrix_t> UserInputForTests::modifyMatrixGIDs(
1030 RCP<tcrsMatrix_t> &inMatrix
1039 RCP<const map_t> inMap = inMatrix->getRowMap();
1041 size_t nRows = inMap->getNodeNumElements();
1042 auto inRows = inMap->getMyGlobalIndices();
1043 Teuchos::Array<zgno_t> outRows(nRows);
1044 for (
size_t i = 0; i < nRows; i++) {
1045 outRows[i] = newID(inRows[i]);
1049 RCP<map_t> outMap = rcp(
new map_t(nGlobalRows, outRows(), 0,
1052 #ifdef INCLUDE_LENGTHY_OUTPUT
1055 std::cout << inMap->getComm()->getRank() <<
" KDDKDD "
1056 <<
"nGlobal " << inMap->getGlobalNumElements() <<
" "
1057 << outMap->getGlobalNumElements() <<
"; "
1058 <<
"nLocal " << inMap->getNodeNumElements() <<
" "
1059 << outMap->getNodeNumElements() <<
"; "
1061 std::cout << inMap->getComm()->getRank() <<
" KDDKDD ";
1062 for (
size_t i = 0; i < nRows; i++)
1063 std::cout <<
"(" << inMap->getMyGlobalIndices()[i] <<
", "
1064 << outMap->getMyGlobalIndices()[i] <<
") ";
1065 std::cout << std::endl;
1067 #endif // INCLUDE_LENGTHY_OUTPUT
1071 size_t rowLen = inMatrix->getNodeMaxNumRowEntries();
1072 RCP<tcrsMatrix_t> outMatrix = rcp(
new tcrsMatrix_t(outMap, rowLen));
1074 Teuchos::Array<zgno_t> indices(rowLen);
1075 Teuchos::Array<zscalar_t> values(rowLen);
1077 for (
size_t i = 0; i < nRows; i++) {
1079 zgno_t inGid = inMap->getGlobalElement(i);
1080 inMatrix->getGlobalRowCopy(inGid, indices, values, nEntries);
1081 for (
size_t j = 0; j < nEntries; j++)
1082 indices[j] = newID(indices[j]);
1084 zgno_t outGid = outMap->getGlobalElement(i);
1085 outMatrix->insertGlobalValues(outGid, indices(0, nEntries),
1086 values(0, nEntries));
1088 outMatrix->fillComplete();
1090 #ifdef INCLUDE_LENGTHY_OUTPUT
1093 std::cout << inMap->getComm()->getRank() <<
" KDDKDD Rows "
1094 <<
"nGlobal " << inMatrix->getGlobalNumRows() <<
" "
1095 << outMatrix->getGlobalNumRows() <<
"; "
1096 <<
"nLocal " << inMatrix->getNodeNumRows() <<
" "
1097 << outMatrix->getNodeNumRows() << std::endl;
1098 std::cout << inMap->getComm()->getRank() <<
" KDDKDD NNZS "
1099 <<
"nGlobal " << inMatrix->getGlobalNumEntries() <<
" "
1100 << outMatrix->getGlobalNumEntries() <<
"; "
1101 <<
"nLocal " << inMatrix->getNodeNumEntries() <<
" "
1102 << outMatrix->getNodeNumEntries() << std::endl;
1105 Teuchos::Array<zgno_t> in(rowLen), out(rowLen);
1106 Teuchos::Array<zscalar_t> inval(rowLen), outval(rowLen);
1108 for (
size_t i = 0; i < nRows; i++) {
1109 std::cout << inMap->getComm()->getRank() <<
" KDDKDD " << i <<
" nnz(";
1110 inMatrix->getGlobalRowCopy(inMap->getGlobalElement(i), in, inval, nIn);
1111 outMatrix->getGlobalRowCopy(outMap->getGlobalElement(i), out, outval,
1114 std::cout << nIn <<
", " << nOut <<
"): ";
1115 for (
size_t j = 0; j < nIn; j++) {
1116 std::cout <<
"(" << in[j] <<
" " << inval[j] <<
", "
1117 << out[j] <<
" " << outval[j] <<
") ";
1119 std::cout << std::endl;
1122 #endif // INCLUDE_LENGTHY_OUTPUT
1129 void UserInputForTests::readMatrixMarketFile(
1132 bool distributeInput
1135 std::ostringstream fname;
1136 fname << path <<
"/" << testData <<
".mtx";
1138 if (verbose_ && tcomm_->getRank() == 0)
1139 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
1144 RCP<tcrsMatrix_t> toMatrix;
1145 RCP<tcrsMatrix_t> fromMatrix;
1148 typedef Tpetra::MatrixMarket::Reader<tcrsMatrix_t> reader_type;
1149 fromMatrix = reader_type::readSparseFile(fname.str(), tcomm_,
1150 true,
false,
false);
1151 #ifdef KDD_NOT_READY_YET
1154 fromMatrix = modifyMatrixGIDs(fromMatrix);
1157 if(!distributeInput)
1159 if (verbose_ && tcomm_->getRank() == 0)
1160 std::cout <<
"Constructing serial distribution of matrix" << std::endl;
1162 RCP<const map_t> fromMap = fromMatrix->getRowMap();
1164 size_t numGlobalCoords = fromMap->getGlobalNumElements();
1165 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
1166 RCP<const map_t> toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, 0, tcomm_));
1168 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1170 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1171 toMatrix->fillComplete();
1174 toMatrix = fromMatrix;
1176 }
catch (std::exception &e) {
1177 if (tcomm_->getRank() == 0) {
1178 std::cout <<
"UserInputForTests unable to read matrix market file:"
1179 << fname.str() << std::endl;
1180 std::cout << e.what() << std::endl;
1185 "UserInputForTests unable to read matrix market file");
1188 #ifdef INCLUDE_LENGTHY_OUTPUT
1189 std::cout << tcomm_->getRank() <<
" KDDKDD " << M_->getNodeNumRows()
1190 <<
" " << M_->getGlobalNumRows()
1191 <<
" " << M_->getNodeNumEntries()
1192 <<
" " << M_->getGlobalNumEntries() << std::endl;
1193 #endif // INCLUDE_LENGTHY_OUTPUT
1200 fname << path <<
"/" << testData <<
"_coord.mtx";
1202 size_t coordDim = 0, numGlobalCoords = 0;
1203 size_t msg[2]={0,0};
1204 ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1205 std::ifstream coordFile;
1207 if (tcomm_->getRank() == 0){
1210 std::cout <<
"UserInputForTests, Read: " <<
1211 fname.str() << std::endl;
1215 coordFile.open(fname.str().c_str());
1217 catch (std::exception &e){
1228 while (!done && !fail && coordFile.good()){
1229 coordFile.getline(c, 256);
1232 else if (c[0] ==
'%')
1236 std::istringstream s(c);
1237 s >> numGlobalCoords >> coordDim;
1238 if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1247 xyz = Teuchos::arcp(
new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1249 for (
size_t dim=0; !fail && dim < coordDim; dim++){
1255 xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1257 for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1258 coordFile.getline(c, 256);
1259 std::istringstream s(c);
1263 if (idx < numGlobalCoords)
1269 ArrayRCP<zscalar_t> emptyArray;
1270 for (
size_t dim=0; dim < coordDim; dim++)
1271 xyz[dim] = emptyArray;
1284 msg[1] = numGlobalCoords;
1288 Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1291 numGlobalCoords = msg[1];
1297 RCP<const map_t> toMap;
1300 const RCP<const map_t> &mapM = M_->getRowMap();
1304 if (verbose_ && tcomm_->getRank() == 0)
1306 std::cout <<
"Matrix was null. ";
1307 std::cout <<
"Constructing distribution map for coordinate vector."
1311 if(!distributeInput)
1313 if (verbose_ && tcomm_->getRank() == 0)
1314 std::cout <<
"Constructing serial distribution map for coordinates."
1317 size_t numLocalCoords = this->tcomm_->getRank()==0 ? numGlobalCoords : 0;
1318 toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1320 toMap = rcp(
new map_t(numGlobalCoords, base, tcomm_));
1328 ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1330 if (tcomm_->getRank() == 0){
1332 for (
size_t dim=0; dim < coordDim; dim++)
1333 coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1337 throw std::bad_alloc();
1339 ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1341 #ifdef KDD_NOT_READY_YET
1350 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1351 rowIds.view(0, numGlobalCoords),
1354 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1358 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1362 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1363 ArrayView<zgno_t>(), base, tcomm_));
1365 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1369 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1373 void UserInputForTests::buildCrsMatrix(
int xdim,
int ydim,
int zdim,
1374 string problemType,
bool distributeInput)
1376 #ifdef HAVE_ZOLTAN2_GALERI
1377 Teuchos::CommandLineProcessor tclp;
1378 Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1379 xdim, ydim, zdim, problemType);
1381 RCP<const Tpetra::Map<zlno_t, zgno_t> > map;
1382 if (distributeInput)
1383 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(params.GetNumGlobalElements(),
1387 size_t nGlobalElements = params.GetNumGlobalElements();
1388 size_t nLocalElements = ((tcomm_->getRank() == 0) ? nGlobalElements : 0);
1389 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(nGlobalElements, nLocalElements, 0,
1393 if (verbose_ && tcomm_->getRank() == 0){
1395 std::cout <<
"Matrix is " << (distributeInput ?
"" :
"not");
1396 std::cout <<
"distributed." << std::endl;
1398 std::cout <<
"UserInputForTests, Create matrix with " << problemType;
1399 std::cout <<
" (and " << xdim;
1401 std::cout <<
" x " << ydim <<
" x " << zdim;
1403 std::cout <<
" x" << ydim <<
" x 1";
1405 std::cout <<
"x 1 x 1";
1407 std::cout <<
" mesh)" << std::endl;
1413 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 =
1414 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> >
1415 (params.GetMatrixType(), map, params.GetParameterList());
1416 M_ = Pr->BuildMatrix();
1418 catch (std::exception &e) {
1422 "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1428 if (verbose_ && tcomm_->getRank() == 0)
1430 "UserInputForTests, Implied matrix row coordinates computed" <<
1433 ArrayView<const zgno_t> gids = map->getNodeElementList();
1436 size_t pos = problemType.find(
"2D");
1437 if (pos != string::npos)
1439 else if (problemType ==
string(
"Laplace1D") ||
1440 problemType ==
string(
"Identity"))
1446 for (
int i=0; i < dim; i++){
1449 throw(std::bad_alloc());
1450 coordinates[i] = Teuchos::arcp(c, 0, count,
true);
1457 zgno_t xySize = xdim * ydim;
1458 for (
zlno_t i=0; i < count; i++){
1459 zgno_t iz = gids[i] / xySize;
1460 zgno_t xy = gids[i] - iz*xySize;
1469 for (
zlno_t i=0; i < count; i++){
1476 for (
zlno_t i=0; i < count; i++)
1481 Array<ArrayView<const zscalar_t> > coordView(dim);
1483 for (
int i=0; i < dim; i++)
1486 xyz_ = rcp(
new tMVector_t(map, coordView.view(0, dim), dim));
1488 throw std::runtime_error(
"Galeri input requested but Trilinos is "
1489 "not built with Galeri.");
1493 void UserInputForTests::readZoltanTestData(
string path,
string testData,
1494 bool distributeInput)
1496 int rank = tcomm_->getRank();
1497 FILE *graphFile = NULL;
1498 FILE *coordFile = NULL;
1499 FILE *assignFile = NULL;
1502 for (
int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] =
'\0';
1506 std::ostringstream chGraphFileName;
1507 chGraphFileName << path <<
"/" << testData <<
".graph";
1510 std::ostringstream chCoordFileName;
1511 chCoordFileName << path <<
"/" << testData <<
".coords";
1514 std::ostringstream chAssignFileName;
1515 chAssignFileName << path <<
"/" << testData <<
".assign";
1518 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1522 chGraphFileName.str(
"");
1523 chCoordFileName.str(
"");
1525 chGraphFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".graph";
1526 chCoordFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".coords";
1527 chAssignFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".assign";
1530 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1533 memset(fileInfo, 0,
sizeof(
int) * 3);
1536 if (verbose_ && tcomm_->getRank() == 0)
1537 std::cout <<
"UserInputForTests, open " <<
1538 chGraphFileName.str () << std::endl;
1540 coordFile = fopen(chCoordFileName.str().c_str(),
"r");
1543 if (verbose_ && tcomm_->getRank() == 0)
1544 std::cout <<
"UserInputForTests, open " <<
1545 chCoordFileName.str () << std::endl;
1548 assignFile = fopen(chAssignFileName.str().c_str(),
"r");
1551 if (verbose_ && tcomm_->getRank() == 0)
1552 std::cout <<
"UserInputForTests, open " <<
1553 chAssignFileName.str () << std::endl;
1556 if (verbose_ && tcomm_->getRank() == 0){
1557 std::cout <<
"UserInputForTests, unable to open file: ";
1558 std::cout << chGraphFileName.str() << std::endl;
1564 Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1566 bool haveGraph = (fileInfo[0] == 1);
1567 bool haveCoords = (fileInfo[1] == 1);
1568 bool haveAssign = (fileInfo[2] == 1);
1573 getUIChacoGraph(graphFile, haveAssign, assignFile,
1574 testData, distributeInput);
1581 getUIChacoCoords(coordFile, testData);
1590 void UserInputForTests::getUIChacoGraph(FILE *fptr,
bool haveAssign,
1591 FILE *assignFile,
string fname,
1592 bool distributeInput)
1594 int rank = tcomm_->getRank();
1596 int nvtxs=0, nedges=0;
1597 int nVwgts=0, nEwgts=0;
1598 int *start = NULL, *adj = NULL;
1599 float *ewgts = NULL, *vwgts = NULL;
1600 size_t *nzPerRow = NULL;
1601 size_t maxRowLen = 0;
1603 ArrayRCP<const size_t> rowSizes;
1605 bool haveEdges =
true;
1609 memset(graphCounts, 0, 5*
sizeof(
int));
1612 fail = chaco_input_graph(fptr, fname.c_str(), &start, &adj,
1613 &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1624 std::cout <<
"UserInputForTests, " << nvtxs <<
" vertices,";
1626 std::cout << start[nvtxs] <<
" edges,";
1628 std::cout <<
"no edges,";
1629 std::cout << nVwgts <<
" vertex weights, ";
1630 std::cout << nEwgts <<
" edge weights" << std::endl;
1637 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1638 throw std::runtime_error(
"Unable to read chaco file");
1642 nedges = start[nvtxs];
1644 nzPerRow =
new size_t [nvtxs];
1646 throw std::bad_alloc();
1647 rowSizes = arcp(nzPerRow, 0, nvtxs,
true);
1650 for (
int i=0; i < nvtxs; i++){
1651 nzPerRow[i] = start[i+1] - start[i];
1652 if (nzPerRow[i] > maxRowLen)
1653 maxRowLen = nzPerRow[i];
1657 memset(nzPerRow, 0,
sizeof(
size_t) * nvtxs);
1663 int chbase = adj[0];
1664 for (
int i=1; i < nedges; i++)
1665 if (adj[i] < chbase)
1669 for (
int i=0; i < nedges; i++)
1674 graphCounts[0] = nvtxs;
1675 graphCounts[1] = nedges;
1676 graphCounts[2] = nVwgts;
1677 graphCounts[3] = nEwgts;
1678 graphCounts[4] = (int)maxRowLen;
1681 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1683 if (graphCounts[0] == 0)
1684 throw std::runtime_error(
"Unable to read chaco file");
1686 haveEdges = (graphCounts[1] > 0);
1688 RCP<tcrsMatrix_t> fromMatrix;
1689 RCP<const map_t> fromMap;
1693 fromMap = rcp(
new map_t(nvtxs, nvtxs, base, tcomm_));
1696 rcp(
new tcrsMatrix_t(fromMap, rowSizes(), Tpetra::StaticProfile));
1701 if (nedges && !edgeIds)
1702 throw std::bad_alloc();
1703 for (
int i=0; i < nedges; i++)
1704 edgeIds[i] = adj[i];
1709 zgno_t *nextId = edgeIds;
1710 Array<zscalar_t> values(nedges, 1.0);
1711 if (nedges > 0 && nEwgts > 0) {
1712 for (
int i=0; i < nedges; i++)
1713 values[i] = ewgts[i];
1718 for (
int i=0; i < nvtxs; i++){
1719 if (nzPerRow[i] > 0){
1720 ArrayView<const zgno_t> rowNz(nextId, nzPerRow[i]);
1721 fromMatrix->insertGlobalValues(i, rowNz, values.view(start[i], start[i+1] - start[i]));
1722 nextId += nzPerRow[i];
1730 fromMatrix->fillComplete();
1733 nvtxs = graphCounts[0];
1734 nedges = graphCounts[1];
1735 nVwgts = graphCounts[2];
1736 nEwgts = graphCounts[3];
1737 maxRowLen = graphCounts[4];
1741 fromMap = rcp(
new map_t(nvtxs, 0, base, tcomm_));
1744 rcp(
new tcrsMatrix_t(fromMap, rowSizes(), Tpetra::StaticProfile));
1746 fromMatrix->fillComplete();
1751 size_t sz = fromMatrix->getNodeMaxNumRowEntries();
1752 Teuchos::Array<zgno_t> indices(sz);
1753 Teuchos::Array<zscalar_t> values(sz);
1754 for (
size_t i = 0; i < fromMatrix->getNodeNumRows(); i++) {
1755 zgno_t gid = fromMatrix->getRowMap()->getGlobalElement(i);
1757 fromMatrix->getGlobalRowCopy(gid, indices(), values(), num);
1758 std::cout <<
"ROW " << gid <<
": ";
1759 for (
size_t j = 0; j < num; j++)
1760 std::cout << indices[j] <<
" ";
1761 std::cout << std::endl;
1766 RCP<const map_t> toMap;
1767 RCP<tcrsMatrix_t> toMatrix;
1768 RCP<import_t> importer;
1770 if (distributeInput) {
1773 short *assignments =
new short[nvtxs];
1775 fail = chaco_input_assign(assignFile, fname.c_str(), nvtxs, assignments);
1778 Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1781 Teuchos::Array<zgno_t> mine;
1782 for (
int i = 0; i < nvtxs; i++) {
1783 if (assignments[i] == rank)
1788 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1789 toMap = rcp(
new map_t(dummy, mine(), base, tcomm_));
1790 delete [] assignments;
1794 toMap = rcp(
new map_t(nvtxs, base, tcomm_));
1799 importer = rcp(
new import_t(fromMap, toMap));
1800 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1801 toMatrix->fillComplete();
1805 toMatrix = fromMatrix;
1812 typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1816 ArrayRCP<zscalar_t> weightBuf;
1817 ArrayView<const zscalar_t> *wgts =
new ArrayView<const zscalar_t> [nVwgts];
1820 size_t len = nVwgts * nvtxs;
1822 if (!buf)
throw std::bad_alloc();
1823 weightBuf = arcp(buf, 0, len,
true);
1825 for (
int widx=0; widx < nVwgts; widx++){
1826 wgts[widx] = ArrayView<const zscalar_t>(buf, nvtxs);
1827 float *vw = vwgts + widx;
1828 for (
int i=0; i < nvtxs; i++, vw += nVwgts)
1837 arrayArray_t vweights = arcp(wgts, 0, nVwgts,
true);
1839 RCP<tMVector_t> fromVertexWeights =
1840 rcp(
new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1842 RCP<tMVector_t> toVertexWeights;
1843 if (distributeInput) {
1844 toVertexWeights = rcp(
new tMVector_t(toMap, nVwgts));
1845 toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1848 toVertexWeights = fromVertexWeights;
1850 vtxWeights_ = toVertexWeights;
1855 if (haveEdges && nEwgts > 0){
1905 toMap = rcp(
new map_t(nedges, M_->getNodeNumEntries(), base, tcomm_));
1906 edgWeights_ = rcp(
new tMVector_t(toMap, nEwgts));
1908 size_t maxSize = M_->getNodeMaxNumRowEntries();
1909 Array<zlno_t> colind(maxSize);
1910 Array<zscalar_t>
vals(maxSize);
1913 for (
size_t i = 0, idx = 0; i < M_->getNodeNumRows(); i++) {
1914 M_->getLocalRowCopy(i, colind,
vals, nEntries);
1915 for (
size_t j = 0; j < nEntries; j++) {
1916 edgWeights_->replaceLocalValue(idx, 0,
vals[j]);
1928 void UserInputForTests::getUIChacoCoords(FILE *fptr,
string fname)
1930 int rank = tcomm_->getRank();
1932 double *x=NULL, *y=NULL, *z=NULL;
1935 size_t globalNumVtx = M_->getGlobalNumRows();
1942 fail = chaco_input_geom(fptr, fname.c_str(), (int)globalNumVtx,
1949 std::cout <<
"UserInputForTests, read " << globalNumVtx;
1950 std::cout <<
" " << ndim <<
"-dimensional coordinates." << std::endl;
1954 Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1957 throw std::runtime_error(
"Can't read coordinate file");
1959 ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1964 for (
int dim=0; dim < ndim; dim++){
1967 throw std::bad_alloc();
1968 coords[dim] = arcp<const zscalar_t>(v, 0, globalNumVtx,
true);
1969 double *val = (dim==0 ? x : (dim==1 ? y : z));
1970 for (
size_t i=0; i < globalNumVtx; i++)
1976 len =
static_cast<zlno_t>(globalNumVtx);
1979 RCP<const map_t> fromMap = rcp(
new map_t(globalNumVtx, len, 0, tcomm_));
1980 RCP<const map_t> toMap = M_->getRowMap();
1981 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1983 Array<ArrayView<const zscalar_t> > coordData;
1984 for (
int dim=0; dim < ndim; dim++)
1985 coordData.push_back(coords[dim].view(0, len));
1987 RCP<tMVector_t> fromCoords =
1988 rcp(
new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1990 RCP<tMVector_t> toCoords = rcp(
new tMVector_t(toMap, ndim));
1992 toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
2001 double UserInputForTests::chaco_read_val(
2017 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2018 if (chaco_offset >= chaco_break_pnt) {
2019 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2021 ptr = &chaco_line[chaco_save_pnt];
2022 for (i=length_left; i; i--) *ptr2++ = *ptr++;
2023 length = chaco_save_pnt + 1;
2026 length = CHACO_LINE_LENGTH;
2031 ptr2 = fgets(&chaco_line[length_left], length, infile);
2033 if (ptr2 == (
char *) NULL) {
2035 return((
double) 0.0);
2038 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2039 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2041 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2042 chaco_save_pnt = chaco_break_pnt;
2047 if (chaco_line[chaco_break_pnt] !=
'\0') {
2048 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2050 chaco_save_pnt = chaco_break_pnt + 1;
2054 else if (white_seen) {
2061 chaco_break_pnt = CHACO_LINE_LENGTH;
2067 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2068 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2070 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2071 chaco_flush_line(infile);
2073 return((
double) 0.0);
2076 ptr = &(chaco_line[chaco_offset]);
2077 val = strtod(ptr, &ptr2);
2082 return((
double) 0.0);
2085 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2092 int UserInputForTests::chaco_read_int(
2108 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2109 if (chaco_offset >= chaco_break_pnt) {
2110 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2112 ptr = &chaco_line[chaco_save_pnt];
2113 for (i=length_left; i; i--) *ptr2++ = *ptr++;
2114 length = chaco_save_pnt + 1;
2117 length = CHACO_LINE_LENGTH;
2122 ptr2 = fgets(&chaco_line[length_left], length, infile);
2124 if (ptr2 == (
char *) NULL) {
2129 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2130 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2132 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2133 chaco_save_pnt = chaco_break_pnt;
2138 if (chaco_line[chaco_break_pnt] !=
'\0') {
2139 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2141 chaco_save_pnt = chaco_break_pnt + 1;
2145 else if (white_seen) {
2152 chaco_break_pnt = CHACO_LINE_LENGTH;
2158 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2159 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2161 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2162 chaco_flush_line(infile);
2167 ptr = &(chaco_line[chaco_offset]);
2168 val = (int) strtol(ptr, &ptr2, 10);
2176 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2182 void UserInputForTests::chaco_flush_line(
2189 while (c !=
'\n' && c !=
'\f')
2193 int UserInputForTests::chaco_input_graph(
2243 while (end_flag == 1) {
2244 *nvtxs = chaco_read_int(fin, &end_flag);
2248 printf(
"ERROR in graph file `%s':", inname);
2249 printf(
" Invalid number of vertices (%d).\n", *nvtxs);
2254 narcs = chaco_read_int(fin, &end_flag);
2256 printf(
"ERROR in graph file `%s':", inname);
2257 printf(
" Invalid number of expected edges (%d).\n", narcs);
2264 option = chaco_read_int(fin, &end_flag);
2266 using_ewgts = option - 10 * (option / 10);
2268 using_vwgts = option - 10 * (option / 10);
2270 vtxnums = option - 10 * (option / 10);
2273 (*nVwgts) = using_vwgts;
2274 (*nEwgts) = using_ewgts;
2277 if (!end_flag && using_vwgts==1){
2278 j = chaco_read_int(fin, &end_flag);
2279 if (!end_flag) (*nVwgts) = j;
2281 if (!end_flag && using_ewgts==1){
2282 j = chaco_read_int(fin, &end_flag);
2283 if (!end_flag) (*nEwgts) = j;
2288 j = chaco_read_int(fin, &end_flag);
2291 *start = (
int *) malloc((
unsigned) (*nvtxs + 1) *
sizeof(
int));
2293 *adjacency = (
int *) malloc((
unsigned) (2 * narcs + 1) *
sizeof(
int));
2298 *vweights = (
float *) malloc((
unsigned) (*nvtxs) * (*nVwgts) *
sizeof(float));
2303 *eweights = (
float *)
2304 malloc((
unsigned) (2 * narcs + 1) * (*nEwgts) *
sizeof(float));
2308 adjptr = *adjacency;
2317 while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2322 j = chaco_read_int(fin, &end_flag);
2324 if (vertex == *nvtxs)
2326 printf(
"ERROR in graph file `%s':", inname);
2327 printf(
" no vertex number in line %d.\n", line_num);
2331 if (j != vertex && j != vertex + 1) {
2332 printf(
"ERROR in graph file `%s':", inname);
2333 printf(
" out-of-order vertex number in line %d.\n", line_num);
2347 if (vertex > *nvtxs)
2351 if (using_vwgts && new_vertex) {
2352 for (j=0; j<(*nVwgts); j++){
2353 weight = chaco_read_val(fin, &end_flag);
2355 printf(
"ERROR in graph file `%s':", inname);
2356 printf(
" not enough weights for vertex %d.\n", vertex);
2360 (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2367 neighbor = chaco_read_int(fin, &end_flag);
2373 for (j=0; j<(*nEwgts); j++){
2374 eweight = chaco_read_val(fin, &end_flag);
2377 printf(
"ERROR in graph file `%s':", inname);
2378 printf(
" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2391 if (++nedges > 2*narcs) {
2392 printf(
"ERROR in graph file `%s':", inname);
2393 printf(
" at least %d adjacencies entered, but nedges = %d\n",
2398 *adjptr++ = neighbor;
2403 neighbor = chaco_read_int(fin, &end_flag);
2407 (*start)[vertex] = sum_edges;
2412 while (!flag && end_flag != -1) {
2413 chaco_read_int(fin, &end_flag);
2418 (*start)[*nvtxs] = sum_edges;
2426 if (*adjacency != NULL)
2429 if (*eweights != NULL)
2438 if (*adjacency != NULL)
2440 if (*vweights != NULL)
2442 if (*eweights != NULL)
2450 return (error_flag);
2454 int UserInputForTests::chaco_input_geom(
2456 const char *geomname,
2464 double xc, yc, zc =0;
2472 *x = *y = *z = NULL;
2475 while (end_flag == 1) {
2476 xc = chaco_read_val(fingeom, &end_flag);
2480 if (end_flag == -1) {
2481 printf(
"No values found in geometry file `%s'\n", geomname);
2487 yc = chaco_read_val(fingeom, &end_flag);
2488 if (end_flag == 0) {
2490 zc = chaco_read_val(fingeom, &end_flag);
2491 if (end_flag == 0) {
2493 chaco_read_val(fingeom, &end_flag);
2495 printf(
"Too many values on input line of geometry file `%s'\n",
2498 printf(
" Maximum dimensionality is 3\n");
2507 *x = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2510 *y = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2514 *z = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2518 for (nread = 1; nread < nvtxs; nread++) {
2521 i = fscanf(fingeom,
"%lf", &((*x)[nread]));
2523 else if (ndims == 2) {
2524 i = fscanf(fingeom,
"%lf%lf", &((*x)[nread]), &((*y)[nread]));
2526 else if (ndims == 3) {
2527 i = fscanf(fingeom,
"%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2532 printf(
"Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2537 else if (i != ndims) {
2538 printf(
"Wrong number of values in line %d of geometry file `%s'\n",
2539 line_num, geomname);
2548 while (!flag && end_flag != -1) {
2549 chaco_read_val(fingeom, &end_flag);
2561 int UserInputForTests::chaco_input_assign(
2563 const char *inassignname,
2574 while (end_flag == 1) {
2575 assignment[0] = chaco_read_int(finassign, &end_flag);
2578 if (assignment[0] < 0) {
2579 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2580 1, inassignname, assignment[0]);
2585 if (end_flag == -1) {
2586 printf(
"ERROR: No values found in assignment file `%s'\n", inassignname);
2592 int np = this->tcomm_->getSize();
2593 if (assignment[0] >= np) flag = assignment[0];
2594 srand(this->tcomm_->getRank());
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] >= np) {
2610 if (assignment[i] > flag)
2611 flag = assignment[i];
2612 assignment[i] = rand() % np;
2618 printf(
"WARNING: Possible error in assignment file `%s'\n",
2620 printf(
" Max assignment set (%d) greater than "
2621 "max processor rank (%d)\n", flag, np-1);
2622 printf(
" Some vertices given random initial assignments\n");
2628 while (!flag && end_flag != -1) {
2629 chaco_read_int(finassign, &end_flag);
2634 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2635 printf(
" Numerical data found after expected end of file\n");
2643 void UserInputForTests::readPamgenMeshFile(
string path,
string testData)
2645 #ifdef HAVE_ZOLTAN2_PAMGEN
2646 int rank = this->tcomm_->getRank();
2647 if (verbose_ && tcomm_->getRank() == 0)
2648 std::cout <<
"UserInputForTestsBD::readPamgenFile, Read: " << testData << std::endl;
2655 std::ostringstream meshFileName;
2656 meshFileName << path <<
"/" << testData <<
".pmgen";
2659 file.open(meshFileName.str(), std::ios::in);
2663 if(verbose_ && tcomm_->getRank() == 0)
2665 std::cout <<
"Unable to open pamgen mesh: ";
2666 std::cout << meshFileName.str();
2667 std::cout <<
"\nPlease check file path and name." << std::endl;
2673 file.seekg (0,file.end);
2680 while(std::getline(file,line))
2682 if( line.find(
"nz") != std::string::npos ||
2683 line.find(
"nphi") != std::string::npos)
2691 file.seekg(0, std::ios::beg);
2696 this->tcomm_->broadcast(0,
sizeof(
int), (
char *)&dimension);
2697 this->tcomm_->broadcast(0,
sizeof(
size_t),(
char *)&len);
2698 this->tcomm_->barrier();
2701 if(verbose_ && tcomm_->getRank() == 0)
2702 std::cout <<
"Pamgen Mesh file size == 0, exiting UserInputForTests early." << std::endl;
2706 char * file_data =
new char[len+1];
2707 file_data[len] =
'\0';
2709 file.read(file_data,len);
2713 this->tcomm_->broadcast(0,(
int)len+1,file_data);
2714 this->tcomm_->barrier();
2718 this->pamgen_mesh = rcp(
new PamgenMesh);
2719 this->havePamgenMesh =
true;
2720 pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2723 pamgen_mesh->storeMesh();
2724 this->tcomm_->barrier();
2727 this->setPamgenCoordinateMV();
2730 this->setPamgenAdjacencyGraph();
2732 this->tcomm_->barrier();
2733 if(rank == 0) file.close();
2734 delete [] file_data;
2736 throw std::runtime_error(
"Pamgen requested but Trilinos "
2737 "not built with Pamgen");
2741 #ifdef HAVE_ZOLTAN2_PAMGEN
2742 void UserInputForTests::setPamgenCoordinateMV()
2744 int dimension = pamgen_mesh->num_dim;
2748 zgno_t numelements = pamgen_mesh->num_elem;
2749 zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2752 for(
int i = 0; i < dimension; ++i){
2753 elem_coords[i] =
new zscalar_t[numelements];
2754 double *tmp = &pamgen_mesh->element_coord[i*numelements];
2755 for (
int j = 0; j < numelements; j++) elem_coords[i][j] = tmp[j];
2759 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2760 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2764 Array<zgno_t> elementList(numelements);
2765 for (Array<zgno_t>::size_type k = 0; k < numelements; ++k) {
2766 elementList[k] = pamgen_mesh->element_order_map[k];
2769 mp = rcp (
new map_t (numGlobalElements, elementList, 0, this->tcomm_));
2773 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(dimension);
2774 for (
int i = 0; i < dimension; i++){
2775 if(numelements > 0){
2776 Teuchos::ArrayView<const zscalar_t> a(elem_coords[i], numelements);
2780 Teuchos::ArrayView<const zscalar_t> a;
2786 xyz_ = RCP<tMVector_t>(
new
2791 void UserInputForTests::setPamgenAdjacencyGraph()
2799 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
2802 size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2803 size_t local_els = (
size_t)this->pamgen_mesh->num_elem;
2805 size_t global_els = (size_t)this->pamgen_mesh->num_elems_global;
2806 size_t global_nodes = (
size_t)this->pamgen_mesh->num_nodes_global;
2810 RCP<const map_t> rowMap = rcp(
new map_t(global_els,0,this->tcomm_));
2811 RCP<const map_t> rangeMap = rowMap;
2814 RCP<const map_t> domainMap = rcp(
new map_t(global_nodes,0,this->tcomm_));
2817 int blks = this->pamgen_mesh->num_elem_blk;
2818 int max_nodes_per_el = 0;
2819 for(
int i = 0; i < blks; i++)
2820 if (this->pamgen_mesh->nodes_per_element[i] > max_nodes_per_el)
2821 max_nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2824 Teuchos::RCP<tcrsMatrix_t> C = rcp(
new tcrsMatrix_t(rowMap,max_nodes_per_el));
2826 Array<zgno_t> g_el_ids(local_els);
2827 for (
size_t k = 0; k < local_els; ++k) {
2828 g_el_ids[k] = pamgen_mesh->global_element_numbers[k]-1;
2831 Array<zgno_t> g_node_ids(local_nodes);
2832 for (
size_t k = 0; k < local_nodes; ++k) {
2833 g_node_ids[k] = pamgen_mesh->global_node_numbers[k]-1;
2840 for(
int i = 0; i < blks; i++)
2842 int el_per_block = this->pamgen_mesh->elements[i];
2843 int nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2844 int * connect = this->pamgen_mesh->elmt_node_linkage[i];
2846 for(
int j = 0; j < el_per_block; j++)
2848 const zgno_t gid =
static_cast<zgno_t>(g_el_ids[el_no]);
2849 for(
int k = 0; k < nodes_per_el; k++)
2851 int g_node_i = g_node_ids[connect[j*nodes_per_el+k]-1];
2852 C->insertGlobalValues(gid,
2853 Teuchos::tuple<zgno_t>(g_node_i),
2854 Teuchos::tuple<zscalar_t>(one));
2860 C->fillComplete(domainMap, rangeMap);
2866 Tpetra::MatrixMatrix::Multiply(*C,
false, *C,
true, *A);
2871 this->M_ = rcp(
new tcrsMatrix_t(rowMap, A->getGlobalMaxNumRowEntries()));
2874 Teuchos::ArrayView<const zgno_t> rowMapElementList =
2875 rowMap->getNodeElementList();
2876 for (Teuchos_Ordinal ii = 0; ii < rowMapElementList.size(); ii++)
2878 zgno_t gid = rowMapElementList[ii];
2879 size_t numEntriesInRow = A->getNumEntriesInGlobalRow (gid);
2880 Array<zscalar_t> rowvals (numEntriesInRow);
2881 Array<zgno_t> rowinds (numEntriesInRow);
2884 Array<zscalar_t> mod_rowvals;
2885 Array<zgno_t> mod_rowinds;
2886 A->getGlobalRowCopy (gid, rowinds (), rowvals (), numEntriesInRow);
2887 for (
size_t i = 0; i < numEntriesInRow; i++) {
2890 if (rowvals[i] >= 1)
2892 mod_rowvals.push_back(one);
2893 mod_rowinds.push_back(rowinds[i]);
2896 this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2899 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.
string trim_right_copy(const string &s, const string &delimiters=" \f\n\r\t\v")
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.
string trim_copy(const string &s, const string &delimiters=" \f\n\r\t\v")
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_left_copy(const string &s, const string &delimiters=" \f\n\r\t\v")
Tpetra::global_size_t global_size_t