14 #ifndef USERINPUTFORTESTS
15 #define USERINPUTFORTESTS
21 #include <Tpetra_MultiVector.hpp>
22 #include <Tpetra_CrsMatrix.hpp>
23 #include <Tpetra_Map.hpp>
24 #include <Xpetra_Vector.hpp>
25 #include <Xpetra_CrsMatrix.hpp>
26 #include <Xpetra_CrsGraph.hpp>
28 #include <MatrixMarket_Tpetra.hpp>
30 #ifdef HAVE_ZOLTAN2_GALERI
31 #include <Galeri_XpetraProblemFactory.hpp>
32 #include <Galeri_XpetraParameters.hpp>
35 #include <Tpetra_KokkosCompat_DefaultNode.hpp>
41 #include <TpetraExt_MatrixMatrix_def.hpp>
48 using Teuchos::ArrayRCP;
49 using Teuchos::ArrayView;
54 using Teuchos::rcp_const_cast;
55 using Teuchos::ParameterList;
56 using namespace Zoltan2_TestingFramework;
94 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
95 typedef Tpetra::Export<zlno_t, zgno_t, znode_t>
export_t;
96 typedef Tpetra::Import<zlno_t, zgno_t, znode_t>
import_t;
120 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
121 bool distributeInput=
true);
140 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
141 bool distributeInput=
true);
159 const RCP<
const Comm<int> > &c);
163 static void getUIRandomData(
unsigned int seed,
zlno_t length,
166 RCP<tMVector_t> getUICoordinates();
168 RCP<tMVector_t> getUIWeights();
170 RCP<tMVector_t> getUIEdgeWeights();
172 RCP<tcrsMatrix_t> getUITpetraCrsMatrix();
174 RCP<tcrsGraph_t> getUITpetraCrsGraph();
176 RCP<tVector_t> getUITpetraVector();
178 RCP<tMVector_t> getUITpetraMultiVector(
int nvec);
180 RCP<xcrsMatrix_t> getUIXpetraCrsMatrix();
182 RCP<xcrsGraph_t> getUIXpetraCrsGraph();
184 RCP<xVector_t> getUIXpetraVector();
186 RCP<xMVector_t> getUIXpetraMultiVector(
int nvec);
188 #ifdef HAVE_ZOLTAN2_PAMGEN
189 PamgenMesh * getPamGenMesh(){
return this->pamgen_mesh.operator->();}
192 #ifdef HAVE_EPETRA_DATA_TYPES
193 RCP<Epetra_CrsGraph> getUIEpetraCrsGraph();
195 RCP<Epetra_CrsMatrix> getUIEpetraCrsMatrix();
197 RCP<Epetra_Vector> getUIEpetraVector();
199 RCP<Epetra_MultiVector> getUIEpetraMultiVector(
int nvec);
203 bool hasInputDataType(
const string &input_type);
205 bool hasUICoordinates();
209 bool hasUIEdgeWeights();
211 bool hasUITpetraCrsMatrix();
213 bool hasUITpetraCrsGraph();
215 bool hasUITpetraVector();
217 bool hasUITpetraMultiVector();
219 bool hasUIXpetraCrsMatrix();
221 bool hasUIXpetraCrsGraph();
223 bool hasUIXpetraVector();
225 bool hasUIXpetraMultiVector();
227 bool hasPamgenMesh();
228 #ifdef HAVE_EPETRA_DATA_TYPES
229 bool hasUIEpetraCrsGraph();
231 bool hasUIEpetraCrsMatrix();
233 bool hasUIEpetraVector();
235 bool hasUIEpetraMultiVector();
243 const RCP<const Comm<int> > tcomm_;
246 #ifdef HAVE_ZOLTAN2_PAMGEN
247 RCP<PamgenMesh> pamgen_mesh;
250 RCP<tcrsMatrix_t> M_;
251 RCP<xcrsMatrix_t> xM_;
253 RCP<tMVector_t> xyz_;
254 RCP<tMVector_t> vtxWeights_;
255 RCP<tMVector_t> edgWeights_;
257 #ifdef HAVE_EPETRA_DATA_TYPES
258 RCP<const Epetra_Comm> ecomm_;
259 RCP<Epetra_CrsMatrix> eM_;
260 RCP<Epetra_CrsGraph> eG_;
268 void readMatrixMarketFile(
string path,
string testData,
bool distributeInput =
true);
274 bool distributeInput);
280 void readZoltanTestData(
string path,
string testData,
281 bool distributeInput);
284 RCP<tcrsMatrix_t> modifyMatrixGIDs(RCP<tcrsMatrix_t> &in);
285 inline zgno_t newID(
const zgno_t id) {
return id * 2 + 10001; }
288 void getUIChacoGraph(FILE *fptr,
bool haveAssign, FILE *assignFile,
289 string name,
bool distributeInput);
292 void getUIChacoCoords(FILE *fptr,
string name);
298 static const int CHACO_LINE_LENGTH=200;
299 char chaco_line[CHACO_LINE_LENGTH];
304 double chaco_read_val(FILE* infile,
int *end_flag);
305 int chaco_read_int(FILE* infile,
int *end_flag);
306 void chaco_flush_line(FILE*);
309 int chaco_input_graph(FILE *fin,
const char *inname,
int **start,
310 int **adjacency,
int *nvtxs,
int *nVwgts,
311 float **vweights,
int *nEwgts,
float **eweights);
314 int chaco_input_geom(FILE *fingeom,
const char *geomname,
int nvtxs,
315 int *igeom,
double **x,
double **y,
double **z);
318 int chaco_input_assign(FILE *finassign,
const char *assignname,
int nvtxs,
326 void readGeometricGenTestData(
string path,
string testData);
330 ParameterList &geoparams);
335 const string& delimiters =
" \f\n\r\t\v" );
338 const string& delimiters =
" \f\n\r\t\v" );
341 const string& delimiters =
" \f\n\r\t\v" );
345 void readPamgenMeshFile(
string path,
string testData);
346 #ifdef HAVE_ZOLTAN2_PAMGEN
347 void setPamgenAdjacencyGraph();
348 void setPamgenCoordinateMV();
353 const RCP<
const Comm<int> > &c,
354 bool debugInfo,
bool distributeInput):
355 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
356 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
357 #ifdef HAVE_EPETRA_DATA_TYPES
358 ecomm_(), eM_(), eG_(),
360 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
362 bool zoltan1 =
false;
363 string::size_type loc = path.find(
"/zoltan/test/");
364 if (loc != string::npos)
368 readZoltanTestData(path, testData, distributeInput);
370 readMatrixMarketFile(path, testData);
372 #ifdef HAVE_EPETRA_DATA_TYPES
373 ecomm_ = Xpetra::toEpetra(c);
379 const RCP<
const Comm<int> > &c,
381 bool distributeInput):
382 verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
383 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
384 #ifdef HAVE_EPETRA_DATA_TYPES
385 ecomm_(), eM_(), eG_(),
387 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
389 if (matrixType.size() == 0){
395 matrixType = string(
"Laplace1D");
397 matrixType = string(
"Laplace2D");
399 matrixType = string(
"Laplace3D");
401 throw std::runtime_error(
"input");
403 if (verbose_ && tcomm_->getRank() == 0)
404 std::cout <<
"UserInputForTests, Matrix type : " << matrixType << std::endl;
407 buildCrsMatrix(x, y, z, matrixType, distributeInput);
409 #ifdef HAVE_EPETRA_DATA_TYPES
410 ecomm_ = Xpetra::toEpetra(c);
415 const RCP<
const Comm<int> > &c):
416 tcomm_(c), havePamgenMesh(false),
417 M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
418 #ifdef HAVE_EPETRA_DATA_TYPES
419 ecomm_(), eM_(), eG_(),
421 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
425 bool distributeInput =
true, debugInfo =
true;
427 if(pList.isParameter(
"distribute input"))
428 distributeInput = pList.get<
bool>(
"distribute input");
430 if(pList.isParameter(
"debug"))
431 debugInfo = pList.get<
bool>(
"debug");
432 this->verbose_ = debugInfo;
434 if(pList.isParameter(
"input file"))
439 if(pList.isParameter(
"input path"))
440 path = pList.get<
string>(
"input path");
442 string testData = pList.get<
string>(
"input file");
448 if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Geometric Generator")
450 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Pamgen")
454 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Chaco")
458 switch (file_format) {
459 case GEOMGEN: readGeometricGenTestData(path,testData);
break;
460 case PAMGEN: readPamgenMeshFile(path,testData);
break;
461 case CHACO: readZoltanTestData(path, testData, distributeInput);
break;
462 default: readMatrixMarketFile(path, testData, distributeInput);
break;
465 }
else if(pList.isParameter(
"x") || pList.isParameter(
"y") || pList.isParameter(
"z")){
469 if(pList.isParameter(
"x")) x = pList.get<
int>(
"x");
470 if(pList.isParameter(
"y")) y = pList.get<
int>(
"y");
471 if(pList.isParameter(
"z")) z = pList.get<
int>(
"z");
473 string problemType =
"";
474 if(pList.isParameter(
"equation type")) problemType = pList.get<
string>(
"equation type");
476 if (problemType.size() == 0){
482 problemType = string(
"Laplace1D");
484 problemType = string(
"Laplace2D");
486 problemType = string(
"Laplace3D");
488 throw std::runtime_error(
"input");
490 if (verbose_ && tcomm_->getRank() == 0)
491 std::cout <<
"UserInputForTests, Matrix type : " << problemType << std::endl;
495 buildCrsMatrix(x, y, z, problemType, distributeInput);
498 std::cerr <<
"Input file block undefined!" << std::endl;
501 #ifdef HAVE_EPETRA_DATA_TYPES
502 ecomm_ = Xpetra::toEpetra(c);
511 throw std::runtime_error(
"could not read coord file");
528 throw std::runtime_error(
"could not read mtx file");
535 throw std::runtime_error(
"could not read mtx file");
536 return rcp_const_cast<
tcrsGraph_t>(M_->getCrsGraph());
541 RCP<tVector_t> V = rcp(
new tVector_t(M_->getRowMap(), 1));
549 RCP<tMVector_t> mV = rcp(
new tMVector_t(M_->getRowMap(), nvec));
558 throw std::runtime_error(
"could not read mtx file");
565 throw std::runtime_error(
"could not read mtx file");
566 return rcp_const_cast<
xcrsGraph_t>(xM_->getCrsGraph());
580 #ifdef HAVE_EPETRA_DATA_TYPES
581 RCP<Epetra_CrsGraph> UserInputForTests::getUIEpetraCrsGraph()
584 throw std::runtime_error(
"could not read mtx file");
585 RCP<const tcrsGraph_t> tgraph = M_->getCrsGraph();
586 RCP<const Tpetra::Map<zlno_t, zgno_t> > trowMap = tgraph->getRowMap();
587 RCP<const Tpetra::Map<zlno_t, zgno_t> > tcolMap = tgraph->getColMap();
589 int nElts =
static_cast<int>(trowMap->getGlobalNumElements());
590 int nMyElts =
static_cast<int>(trowMap->getLocalNumElements());
592 ArrayView<const int> gids = trowMap->getLocalElementList();
594 Epetra_BlockMap erowMap(nElts, nMyElts,
595 gids.getRawPtr(), 1, base, *ecomm_);
597 Array<int> rowSize(nMyElts);
598 for (
int i=0; i < nMyElts; i++){
599 rowSize[i] =
static_cast<int>(M_->getNumEntriesInLocalRow(i));
602 size_t maxRow = M_->getLocalMaxNumRowEntries();
603 Array<int> colGids(maxRow);
605 typename tcrsGraph_t::local_inds_host_view_type colLid;
606 eG_ = rcp(
new Epetra_CrsGraph(Copy, erowMap,
607 rowSize.getRawPtr(),
true));
609 for (
int i=0; i < nMyElts; i++){
610 tgraph->getLocalRowView(i, colLid);
611 for (
size_t j=0; j < colLid.extent(0); j++)
612 colGids[j] = tcolMap->getGlobalElement(colLid[j]);
613 eG_->InsertGlobalIndices(gids[i], rowSize[i], colGids.getRawPtr());
619 RCP<Epetra_CrsMatrix> UserInputForTests::getUIEpetraCrsMatrix()
622 throw std::runtime_error(
"could not read mtx file");
623 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
624 eM_ = rcp(
new Epetra_CrsMatrix(Copy, *egraph));
626 size_t maxRow = M_->getLocalMaxNumRowEntries();
627 int nrows = egraph->NumMyRows();
628 const Epetra_BlockMap &rowMap = egraph->RowMap();
629 const Epetra_BlockMap &colMap = egraph->ColMap();
630 Array<int> colGid(maxRow);
631 for (
int i=0; i < nrows; i++){
632 typename tcrsMatrix_t::local_inds_host_view_type colLid;
633 typename tcrsMatrix_t::values_host_view_type nz;
634 M_->getLocalRowView(i, colLid, nz);
635 size_t rowSize = colLid.size();
636 int rowGid = rowMap.GID(i);
637 for (
size_t j=0; j < rowSize; j++){
638 colGid[j] = colMap.GID(colLid[j]);
640 eM_->InsertGlobalValues(rowGid, (
int)rowSize, nz.data(), colGid.getRawPtr());
646 RCP<Epetra_Vector> UserInputForTests::getUIEpetraVector()
648 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
649 RCP<Epetra_Vector> V = rcp(
new Epetra_Vector(egraph->RowMap()));
654 RCP<Epetra_MultiVector> UserInputForTests::getUIEpetraMultiVector(
int nvec)
656 RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
657 RCP<Epetra_MultiVector> mV =
658 rcp(
new Epetra_MultiVector(egraph->RowMap(), nvec));
668 this->hasUITpetraCrsMatrix() || \
669 this->hasUITpetraCrsGraph() || \
670 this->hasPamgenMesh();
675 if(input_type ==
"coordinates")
677 else if(input_type ==
"tpetra_vector")
679 else if(input_type ==
"tpetra_multivector")
681 else if(input_type ==
"tpetra_crs_graph")
683 else if(input_type ==
"tpetra_crs_matrix")
685 else if(input_type ==
"xpetra_vector")
687 else if(input_type ==
"xpetra_multivector")
689 else if(input_type ==
"xpetra_crs_graph")
691 else if(input_type ==
"xpetra_crs_matrix")
693 #ifdef HAVE_EPETRA_DATA_TYPES
694 else if(input_type ==
"epetra_vector")
695 return this->hasUIEpetraVector();
696 else if(input_type ==
"epetra_multivector")
697 return this->hasUIEpetraMultiVector();
698 else if(input_type ==
"epetra_crs_graph")
699 return this->hasUIEpetraCrsGraph();
700 else if(input_type ==
"epetra_crs_matrix")
701 return this->hasUIEpetraCrsMatrix();
709 return xyz_.is_null() ?
false :
true;
714 return vtxWeights_.is_null() ?
false :
true;
719 return edgWeights_.is_null() ?
false :
true;
724 return M_.is_null() ?
false :
true;
729 return M_.is_null() ?
false :
true;
744 return M_.is_null() ?
false :
true;
749 return M_.is_null() ?
false :
true;
764 return this->havePamgenMesh;
767 #ifdef HAVE_EPETRA_DATA_TYPES
768 bool UserInputForTests::hasUIEpetraCrsGraph()
770 return M_.is_null() ?
false :
true;
773 bool UserInputForTests::hasUIEpetraCrsMatrix()
775 return hasUIEpetraCrsGraph();
778 bool UserInputForTests::hasUIEpetraVector()
780 return hasUIEpetraCrsGraph();
783 bool UserInputForTests::hasUIEpetraMultiVector()
785 return hasUIEpetraCrsGraph();
791 ArrayView<ArrayRCP<zscalar_t > > data)
796 size_t dim = data.size();
797 for (
size_t i=0; i < dim; i++){
800 throw (std::bad_alloc());
801 data[i] = Teuchos::arcp(tmp, 0, length,
true);
804 zscalar_t scalingFactor = (max-min) / RAND_MAX;
806 for (
size_t i=0; i < dim; i++){
808 for (
zlno_t j=0; j < length; j++)
809 *x++ = min + (
zscalar_t(rand()) * scalingFactor);
815 string UserInputForTests::trim_right_copy(
817 const string& delimiters)
819 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
822 string UserInputForTests::trim_left_copy(
824 const string& delimiters)
826 return s.substr( s.find_first_not_of( delimiters ) );
829 string UserInputForTests::trim_copy(
831 const string& delimiters)
833 return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
836 void UserInputForTests::readGeometricGenTestData(
string path,
840 std::ostringstream
fname;
841 fname << path <<
"/" << testData <<
".txt";
843 if (verbose_ && tcomm_->getRank() == 0)
844 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
846 Teuchos::ParameterList geoparams(
"geo params");
847 readGeoGenParams(fname.str(),geoparams);
852 int coord_dim = gg->getCoordinateDimension();
853 int numWeightsPerCoord = gg->getNumWeights();
854 zlno_t numLocalPoints = gg->getNumLocalCoords();
855 zgno_t numGlobalPoints = gg->getNumGlobalCoords();
859 for(
int i = 0; i < coord_dim; ++i){
860 coords[i] =
new zscalar_t[numLocalPoints];
864 gg->getLocalCoordinatesCopy(coords);
868 if (numWeightsPerCoord) {
870 weight =
new zscalar_t * [numWeightsPerCoord];
871 for(
int i = 0; i < numWeightsPerCoord; ++i){
872 weight[i] =
new zscalar_t[numLocalPoints];
876 gg->getLocalWeightsCopy(weight);
883 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp =
884 rcp(
new Tpetra::Map<zlno_t, zgno_t, znode_t>(numGlobalPoints, numLocalPoints, 0, this->tcomm_));
887 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
888 for (
int i=0; i < coord_dim; i++){
889 if(numLocalPoints > 0){
890 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
894 Teuchos::ArrayView<const zscalar_t> a;
900 xyz_ = RCP<tMVector_t>(
new
905 if (numWeightsPerCoord) {
907 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > weightView(numWeightsPerCoord);
908 for (
int i=0; i < numWeightsPerCoord; i++){
909 if(numLocalPoints > 0){
910 Teuchos::ArrayView<const zscalar_t> a(weight[i], numLocalPoints);
914 Teuchos::ArrayView<const zscalar_t> a;
919 vtxWeights_ = RCP<tMVector_t>(
new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
920 numWeightsPerCoord));
924 void UserInputForTests::readGeoGenParams(
string paramFileName,
925 ParameterList &geoparams){
929 std::string input =
"";
931 for(
int i = 0; i < 25000; ++i){
936 if(this->tcomm_->getRank() == 0){
938 std::fstream inParam(paramFileName.c_str());
945 std::string tmp =
"";
946 getline (inParam,tmp);
947 while (!inParam.eof()){
949 tmp = trim_copy(tmp);
954 getline (inParam,tmp);
957 for (
size_t i = 0; i < input.size(); ++i){
965 int size = (int)input.size();
969 this->tcomm_->broadcast(0,
sizeof(
int), (
char*) &size);
971 throw "File " + paramFileName +
" cannot be opened.";
973 this->tcomm_->broadcast(0, size, inp);
974 std::istringstream inParam(inp);
976 getline (inParam,str);
977 while (!inParam.eof()){
978 if(str[0] != param_comment){
979 size_t pos = str.find(
'=');
980 if(pos == string::npos){
981 throw "Invalid Line:" + str +
" in parameter file";
983 string paramname = trim_copy(str.substr(0,pos));
984 string paramvalue = trim_copy(str.substr(pos + 1));
985 geoparams.set(paramname, paramvalue);
987 getline (inParam,str);
992 RCP<tcrsMatrix_t> UserInputForTests::modifyMatrixGIDs(
993 RCP<tcrsMatrix_t> &inMatrix
1002 RCP<const map_t> inMap = inMatrix->getRowMap();
1004 size_t nRows = inMap->getLocalNumElements();
1005 auto inRows = inMap->getMyGlobalIndices();
1006 Teuchos::Array<zgno_t> outRows(nRows);
1007 for (
size_t i = 0; i < nRows; i++) {
1008 outRows[i] = newID(inRows[i]);
1012 RCP<map_t> outMap = rcp(
new map_t(nGlobalRows, outRows(), 0,
1015 #ifdef INCLUDE_LENGTHY_OUTPUT
1018 std::cout << inMap->getComm()->getRank() <<
" KDDKDD "
1019 <<
"nGlobal " << inMap->getGlobalNumElements() <<
" "
1020 << outMap->getGlobalNumElements() <<
"; "
1021 <<
"nLocal " << inMap->getLocalNumElements() <<
" "
1022 << outMap->getLocalNumElements() <<
"; "
1024 std::cout << inMap->getComm()->getRank() <<
" KDDKDD ";
1025 for (
size_t i = 0; i < nRows; i++)
1026 std::cout <<
"(" << inMap->getMyGlobalIndices()[i] <<
", "
1027 << outMap->getMyGlobalIndices()[i] <<
") ";
1028 std::cout << std::endl;
1030 #endif // INCLUDE_LENGTHY_OUTPUT
1034 size_t rowLen = inMatrix->getLocalMaxNumRowEntries();
1035 RCP<tcrsMatrix_t> outMatrix = rcp(
new tcrsMatrix_t(outMap, rowLen));
1037 typename tcrsMatrix_t::nonconst_global_inds_host_view_type indices(
"Indices", rowLen);
1038 typename tcrsMatrix_t::nonconst_values_host_view_type values(
"Values", rowLen);
1040 for (
size_t i = 0; i < nRows; i++) {
1042 zgno_t inGid = inMap->getGlobalElement(i);
1043 inMatrix->getGlobalRowCopy(inGid, indices, values, nEntries);
1044 for (
size_t j = 0; j < nEntries; j++)
1045 indices[j] = newID(indices[j]);
1047 zgno_t outGid = outMap->getGlobalElement(i);
1048 ArrayView<zgno_t> Indices(indices.data(), nEntries);
1049 ArrayView<zscalar_t> Values(values.data(), nEntries);
1050 outMatrix->insertGlobalValues(outGid, Indices(0, nEntries),
1051 Values(0, nEntries));
1053 outMatrix->fillComplete();
1055 #ifdef INCLUDE_LENGTHY_OUTPUT
1058 std::cout << inMap->getComm()->getRank() <<
" KDDKDD Rows "
1059 <<
"nGlobal " << inMatrix->getGlobalNumRows() <<
" "
1060 << outMatrix->getGlobalNumRows() <<
"; "
1061 <<
"nLocal " << inMatrix->getLocalNumRows() <<
" "
1062 << outMatrix->getLocalNumRows() << std::endl;
1063 std::cout << inMap->getComm()->getRank() <<
" KDDKDD NNZS "
1064 <<
"nGlobal " << inMatrix->getGlobalNumEntries() <<
" "
1065 << outMatrix->getGlobalNumEntries() <<
"; "
1066 <<
"nLocal " << inMatrix->getLocalNumEntries() <<
" "
1067 << outMatrix->getLocalNumEntries() << std::endl;
1070 Teuchos::Array<zgno_t> in(rowLen), out(rowLen);
1071 Teuchos::Array<zscalar_t> inval(rowLen), outval(rowLen);
1073 for (
size_t i = 0; i < nRows; i++) {
1074 std::cout << inMap->getComm()->getRank() <<
" KDDKDD " << i <<
" nnz(";
1075 inMatrix->getGlobalRowCopy(inMap->getGlobalElement(i), in, inval, nIn);
1076 outMatrix->getGlobalRowCopy(outMap->getGlobalElement(i), out, outval,
1079 std::cout << nIn <<
", " << nOut <<
"): ";
1080 for (
size_t j = 0; j < nIn; j++) {
1081 std::cout <<
"(" << in[j] <<
" " << inval[j] <<
", "
1082 << out[j] <<
" " << outval[j] <<
") ";
1084 std::cout << std::endl;
1087 #endif // INCLUDE_LENGTHY_OUTPUT
1094 void UserInputForTests::readMatrixMarketFile(
1097 bool distributeInput
1100 std::ostringstream
fname;
1101 fname << path <<
"/" << testData <<
".mtx";
1103 if (verbose_ && tcomm_->getRank() == 0)
1104 std::cout <<
"UserInputForTests, Read: " << fname.str() << std::endl;
1109 RCP<tcrsMatrix_t> toMatrix;
1110 RCP<tcrsMatrix_t> fromMatrix;
1113 typedef Tpetra::MatrixMarket::Reader<tcrsMatrix_t> reader_type;
1114 fromMatrix = reader_type::readSparseFile(fname.str(), tcomm_,
1115 true,
false,
false);
1116 #ifdef KDD_NOT_READY_YET
1119 fromMatrix = modifyMatrixGIDs(fromMatrix);
1122 if(!distributeInput)
1124 if (verbose_ && tcomm_->getRank() == 0)
1125 std::cout <<
"Constructing serial distribution of matrix" << std::endl;
1127 RCP<const map_t> fromMap = fromMatrix->getRowMap();
1129 size_t numGlobalCoords = fromMap->getGlobalNumElements();
1130 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
1131 RCP<const map_t> toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, 0, tcomm_));
1133 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1135 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1136 toMatrix->fillComplete();
1139 toMatrix = fromMatrix;
1141 }
catch (std::exception &e) {
1142 if (tcomm_->getRank() == 0) {
1143 std::cout <<
"UserInputForTests unable to read matrix market file:"
1144 << fname.str() << std::endl;
1145 std::cout << e.what() << std::endl;
1150 "UserInputForTests unable to read matrix market file");
1153 #ifdef INCLUDE_LENGTHY_OUTPUT
1154 std::cout << tcomm_->getRank() <<
" KDDKDD " << M_->getLocalNumRows()
1155 <<
" " << M_->getGlobalNumRows()
1156 <<
" " << M_->getLocalNumEntries()
1157 <<
" " << M_->getGlobalNumEntries() << std::endl;
1158 #endif // INCLUDE_LENGTHY_OUTPUT
1165 fname << path <<
"/" << testData <<
"_coord.mtx";
1167 size_t coordDim = 0, numGlobalCoords = 0;
1168 size_t msg[2]={0,0};
1169 ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1170 std::ifstream coordFile;
1172 if (tcomm_->getRank() == 0){
1175 std::cout <<
"UserInputForTests, Read: " <<
1176 fname.str() << std::endl;
1180 coordFile.open(fname.str().c_str());
1182 catch (std::exception &){
1193 while (!done && !fail && coordFile.good()){
1194 coordFile.getline(c, 256);
1197 else if (c[0] ==
'%')
1201 std::istringstream s(c);
1202 s >> numGlobalCoords >> coordDim;
1203 if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1212 xyz = Teuchos::arcp(
new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1214 for (
size_t dim=0; !fail && dim < coordDim; dim++){
1220 xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1222 for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1223 coordFile.getline(c, 256);
1224 std::istringstream s(c);
1228 if (idx < numGlobalCoords)
1234 ArrayRCP<zscalar_t> emptyArray;
1235 for (
size_t dim=0; dim < coordDim; dim++)
1236 xyz[dim] = emptyArray;
1249 msg[1] = numGlobalCoords;
1253 Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1256 numGlobalCoords = msg[1];
1262 RCP<const map_t> toMap;
1265 const RCP<const map_t> &mapM = M_->getRowMap();
1269 if (verbose_ && tcomm_->getRank() == 0)
1271 std::cout <<
"Matrix was null. ";
1272 std::cout <<
"Constructing distribution map for coordinate vector."
1276 if(!distributeInput)
1278 if (verbose_ && tcomm_->getRank() == 0)
1279 std::cout <<
"Constructing serial distribution map for coordinates."
1282 size_t numLocalCoords = this->tcomm_->getRank()==0 ? numGlobalCoords : 0;
1283 toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1285 toMap = rcp(
new map_t(numGlobalCoords, base, tcomm_));
1293 ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1295 if (tcomm_->getRank() == 0){
1297 for (
size_t dim=0; dim < coordDim; dim++)
1298 coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1302 throw std::bad_alloc();
1304 ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1306 #ifdef KDD_NOT_READY_YET
1315 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1316 rowIds.view(0, numGlobalCoords),
1319 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1323 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1327 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1328 ArrayView<zgno_t>(), base, tcomm_));
1330 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1334 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1338 void UserInputForTests::buildCrsMatrix(
int xdim,
int ydim,
int zdim,
1339 string problemType,
bool distributeInput)
1341 #ifdef HAVE_ZOLTAN2_GALERI
1342 Teuchos::CommandLineProcessor tclp;
1343 Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1344 xdim, ydim, zdim, problemType);
1346 RCP<const Tpetra::Map<zlno_t, zgno_t> > map;
1347 if (distributeInput)
1348 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(params.GetNumGlobalElements(),
1352 size_t nGlobalElements = params.GetNumGlobalElements();
1353 size_t nLocalElements = ((tcomm_->getRank() == 0) ? nGlobalElements : 0);
1354 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(nGlobalElements, nLocalElements, 0,
1358 if (verbose_ && tcomm_->getRank() == 0){
1360 std::cout <<
"Matrix is " << (distributeInput ?
"" :
"not");
1361 std::cout <<
"distributed." << std::endl;
1363 std::cout <<
"UserInputForTests, Create matrix with " << problemType;
1364 std::cout <<
" (and " << xdim;
1366 std::cout <<
" x " << ydim <<
" x " << zdim;
1368 std::cout <<
" x" << ydim <<
" x 1";
1370 std::cout <<
"x 1 x 1";
1372 std::cout <<
" mesh)" << std::endl;
1378 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 =
1379 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> >
1380 (params.GetMatrixType(), map, params.GetParameterList());
1381 M_ = Pr->BuildMatrix();
1383 catch (std::exception &) {
1387 "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1393 if (verbose_ && tcomm_->getRank() == 0)
1395 "UserInputForTests, Implied matrix row coordinates computed" <<
1398 ArrayView<const zgno_t> gids = map->getLocalElementList();
1401 size_t pos = problemType.find(
"2D");
1402 if (pos != string::npos)
1404 else if (problemType ==
string(
"Laplace1D") ||
1405 problemType ==
string(
"Identity"))
1411 for (
int i=0; i < dim; i++){
1414 throw(std::bad_alloc());
1415 coordinates[i] = Teuchos::arcp(c, 0, count,
true);
1422 zgno_t xySize = xdim * ydim;
1423 for (
zlno_t i=0; i < count; i++){
1424 zgno_t iz = gids[i] / xySize;
1425 zgno_t xy = gids[i] - iz*xySize;
1434 for (
zlno_t i=0; i < count; i++){
1441 for (
zlno_t i=0; i < count; i++)
1446 Array<ArrayView<const zscalar_t> > coordView(dim);
1448 for (
int i=0; i < dim; i++)
1451 xyz_ = rcp(
new tMVector_t(map, coordView.view(0, dim), dim));
1453 throw std::runtime_error(
"Galeri input requested but Trilinos is "
1454 "not built with Galeri.");
1458 void UserInputForTests::readZoltanTestData(
string path,
string testData,
1459 bool distributeInput)
1461 int rank = tcomm_->getRank();
1462 FILE *graphFile = NULL;
1463 FILE *coordFile = NULL;
1464 FILE *assignFile = NULL;
1467 for (
int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] =
'\0';
1471 std::ostringstream chGraphFileName;
1472 chGraphFileName << path <<
"/" << testData <<
".graph";
1475 std::ostringstream chCoordFileName;
1476 chCoordFileName << path <<
"/" << testData <<
".coords";
1479 std::ostringstream chAssignFileName;
1480 chAssignFileName << path <<
"/" << testData <<
".assign";
1483 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1487 chGraphFileName.str(
"");
1488 chCoordFileName.str(
"");
1490 chGraphFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".graph";
1491 chCoordFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".coords";
1492 chAssignFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".assign";
1495 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1498 memset(fileInfo, 0,
sizeof(
int) * 3);
1501 if (verbose_ && tcomm_->getRank() == 0)
1502 std::cout <<
"UserInputForTests, open " <<
1503 chGraphFileName.str () << std::endl;
1505 coordFile = fopen(chCoordFileName.str().c_str(),
"r");
1508 if (verbose_ && tcomm_->getRank() == 0)
1509 std::cout <<
"UserInputForTests, open " <<
1510 chCoordFileName.str () << std::endl;
1513 assignFile = fopen(chAssignFileName.str().c_str(),
"r");
1516 if (verbose_ && tcomm_->getRank() == 0)
1517 std::cout <<
"UserInputForTests, open " <<
1518 chAssignFileName.str () << std::endl;
1521 if (verbose_ && tcomm_->getRank() == 0){
1522 std::cout <<
"UserInputForTests, unable to open file: ";
1523 std::cout << chGraphFileName.str() << std::endl;
1529 Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1531 bool haveGraph = (fileInfo[0] == 1);
1532 bool haveCoords = (fileInfo[1] == 1);
1533 bool haveAssign = (fileInfo[2] == 1);
1538 getUIChacoGraph(graphFile, haveAssign, assignFile,
1539 testData, distributeInput);
1546 getUIChacoCoords(coordFile, testData);
1555 void UserInputForTests::getUIChacoGraph(FILE *fptr,
bool haveAssign,
1556 FILE *assignFile,
string fname,
1557 bool distributeInput)
1559 int rank = tcomm_->getRank();
1561 int nvtxs=0, nedges=0;
1562 int nVwgts=0, nEwgts=0;
1563 int *start = NULL, *adj = NULL;
1564 float *ewgts = NULL, *vwgts = NULL;
1565 size_t *nzPerRow = NULL;
1566 size_t maxRowLen = 0;
1568 ArrayRCP<const size_t> rowSizes;
1570 bool haveEdges =
true;
1574 memset(graphCounts, 0, 5*
sizeof(
int));
1577 fail = chaco_input_graph(fptr, fname.c_str(), &start, &adj,
1578 &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1589 std::cout <<
"UserInputForTests, " << nvtxs <<
" vertices,";
1591 std::cout << start[nvtxs] <<
" edges,";
1593 std::cout <<
"no edges,";
1594 std::cout << nVwgts <<
" vertex weights, ";
1595 std::cout << nEwgts <<
" edge weights" << std::endl;
1602 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1603 throw std::runtime_error(
"Unable to read chaco file");
1607 nedges = start[nvtxs];
1609 nzPerRow =
new size_t [nvtxs];
1611 throw std::bad_alloc();
1612 rowSizes = arcp(nzPerRow, 0, nvtxs,
true);
1615 for (
int i=0; i < nvtxs; i++){
1616 nzPerRow[i] = start[i+1] - start[i];
1617 if (nzPerRow[i] > maxRowLen)
1618 maxRowLen = nzPerRow[i];
1622 memset(nzPerRow, 0,
sizeof(
size_t) * nvtxs);
1629 for (
int i=0; i < nedges; i++)
1633 graphCounts[0] = nvtxs;
1634 graphCounts[1] = nedges;
1635 graphCounts[2] = nVwgts;
1636 graphCounts[3] = nEwgts;
1637 graphCounts[4] = (int)maxRowLen;
1640 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1642 if (graphCounts[0] == 0)
1643 throw std::runtime_error(
"Unable to read chaco file");
1645 haveEdges = (graphCounts[1] > 0);
1647 RCP<tcrsMatrix_t> fromMatrix;
1648 RCP<const map_t> fromMap;
1652 fromMap = rcp(
new map_t(nvtxs, nvtxs, base, tcomm_));
1660 if (nedges && !edgeIds)
1661 throw std::bad_alloc();
1662 for (
int i=0; i < nedges; i++)
1663 edgeIds[i] = adj[i];
1668 zgno_t *nextId = edgeIds;
1669 Array<zscalar_t> values(nedges, 1.0);
1670 if (nedges > 0 && nEwgts > 0) {
1671 for (
int i=0; i < nedges; i++)
1672 values[i] = ewgts[i];
1677 for (
int i=0; i < nvtxs; i++){
1678 if (nzPerRow[i] > 0){
1679 ArrayView<const zgno_t> rowNz(nextId, nzPerRow[i]);
1680 fromMatrix->insertGlobalValues(i, rowNz, values.view(start[i], start[i+1] - start[i]));
1681 nextId += nzPerRow[i];
1689 fromMatrix->fillComplete();
1692 nvtxs = graphCounts[0];
1693 nedges = graphCounts[1];
1694 nVwgts = graphCounts[2];
1695 nEwgts = graphCounts[3];
1696 maxRowLen = graphCounts[4];
1700 fromMap = rcp(
new map_t(nvtxs, 0, base, tcomm_));
1705 fromMatrix->fillComplete();
1710 size_t sz = fromMatrix->getLocalMaxNumRowEntries();
1711 Teuchos::Array<zgno_t> indices(sz);
1712 Teuchos::Array<zscalar_t> values(sz);
1713 for (
size_t i = 0; i < fromMatrix->getLocalNumRows(); i++) {
1714 zgno_t gid = fromMatrix->getRowMap()->getGlobalElement(i);
1716 fromMatrix->getGlobalRowCopy(gid, indices(), values(), num);
1717 std::cout <<
"ROW " << gid <<
": ";
1718 for (
size_t j = 0; j < num; j++)
1719 std::cout << indices[j] <<
" ";
1720 std::cout << std::endl;
1725 RCP<const map_t> toMap;
1726 RCP<tcrsMatrix_t> toMatrix;
1727 RCP<import_t> importer;
1729 if (distributeInput) {
1732 short *assignments =
new short[nvtxs];
1734 fail = chaco_input_assign(assignFile, fname.c_str(), nvtxs, assignments);
1737 Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1740 Teuchos::Array<zgno_t> mine;
1741 for (
int i = 0; i < nvtxs; i++) {
1742 if (assignments[i] == rank)
1747 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1748 toMap = rcp(
new map_t(dummy, mine(), base, tcomm_));
1749 delete [] assignments;
1753 toMap = rcp(
new map_t(nvtxs, base, tcomm_));
1758 importer = rcp(
new import_t(fromMap, toMap));
1759 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1760 toMatrix->fillComplete();
1764 toMatrix = fromMatrix;
1771 typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1775 ArrayRCP<zscalar_t> weightBuf;
1776 ArrayView<const zscalar_t> *wgts =
new ArrayView<const zscalar_t> [nVwgts];
1779 size_t len = nVwgts * nvtxs;
1781 if (!buf)
throw std::bad_alloc();
1782 weightBuf = arcp(buf, 0, len,
true);
1784 for (
int widx=0; widx < nVwgts; widx++){
1785 wgts[widx] = ArrayView<const zscalar_t>(buf, nvtxs);
1786 float *vw = vwgts + widx;
1787 for (
int i=0; i < nvtxs; i++, vw += nVwgts)
1796 arrayArray_t vweights = arcp(wgts, 0, nVwgts,
true);
1798 RCP<tMVector_t> fromVertexWeights =
1799 rcp(
new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1801 RCP<tMVector_t> toVertexWeights;
1802 if (distributeInput) {
1803 toVertexWeights = rcp(
new tMVector_t(toMap, nVwgts));
1804 toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1807 toVertexWeights = fromVertexWeights;
1809 vtxWeights_ = toVertexWeights;
1814 if (haveEdges && nEwgts > 0){
1864 toMap = rcp(
new map_t(nedges, M_->getLocalNumEntries(), base, tcomm_));
1865 edgWeights_ = rcp(
new tMVector_t(toMap, nEwgts));
1867 size_t maxSize = M_->getLocalMaxNumRowEntries();
1868 tcrsMatrix_t::nonconst_local_inds_host_view_type colind(
"colind", maxSize);
1869 tcrsMatrix_t::nonconst_values_host_view_type vals(
"values", maxSize);
1872 for (
size_t i = 0, idx = 0; i < M_->getLocalNumRows(); i++) {
1873 M_->getLocalRowCopy(i, colind, vals, nEntries);
1874 for (
size_t j = 0; j < nEntries; j++) {
1875 edgWeights_->replaceLocalValue(idx, 0, vals[j]);
1887 void UserInputForTests::getUIChacoCoords(FILE *fptr,
string fname)
1889 int rank = tcomm_->getRank();
1891 double *x=NULL, *y=NULL, *z=NULL;
1894 size_t globalNumVtx = M_->getGlobalNumRows();
1901 fail = chaco_input_geom(fptr, fname.c_str(), (int)globalNumVtx,
1908 std::cout <<
"UserInputForTests, read " << globalNumVtx;
1909 std::cout <<
" " << ndim <<
"-dimensional coordinates." << std::endl;
1913 Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1916 throw std::runtime_error(
"Can't read coordinate file");
1918 ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1923 for (
int dim=0; dim < ndim; dim++){
1926 throw std::bad_alloc();
1927 coords[dim] = arcp<const zscalar_t>(v, 0, globalNumVtx,
true);
1928 double *val = (dim==0 ? x : (dim==1 ? y : z));
1929 for (
size_t i=0; i < globalNumVtx; i++)
1935 len =
static_cast<zlno_t>(globalNumVtx);
1938 RCP<const map_t> fromMap = rcp(
new map_t(globalNumVtx, len, 0, tcomm_));
1939 RCP<const map_t> toMap = M_->getRowMap();
1940 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1942 Array<ArrayView<const zscalar_t> > coordData;
1943 for (
int dim=0; dim < ndim; dim++)
1944 coordData.push_back(coords[dim].view(0, len));
1946 RCP<tMVector_t> fromCoords =
1947 rcp(
new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1949 RCP<tMVector_t> toCoords = rcp(
new tMVector_t(toMap, ndim));
1951 toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
1960 double UserInputForTests::chaco_read_val(
1976 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1977 if (chaco_offset >= chaco_break_pnt) {
1978 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1980 ptr = &chaco_line[chaco_save_pnt];
1981 for (i=length_left; i; i--) *ptr2++ = *ptr++;
1982 length = chaco_save_pnt + 1;
1985 length = CHACO_LINE_LENGTH;
1990 ptr2 = fgets(&chaco_line[length_left], length, infile);
1992 if (ptr2 == (
char *) NULL) {
1994 return((
double) 0.0);
1997 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
1998 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2000 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2001 chaco_save_pnt = chaco_break_pnt;
2006 if (chaco_line[chaco_break_pnt] !=
'\0') {
2007 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2009 chaco_save_pnt = chaco_break_pnt + 1;
2013 else if (white_seen) {
2020 chaco_break_pnt = CHACO_LINE_LENGTH;
2026 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2027 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2029 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2030 chaco_flush_line(infile);
2032 return((
double) 0.0);
2035 ptr = &(chaco_line[chaco_offset]);
2036 val = strtod(ptr, &ptr2);
2041 return((
double) 0.0);
2044 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2051 int UserInputForTests::chaco_read_int(
2067 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2068 if (chaco_offset >= chaco_break_pnt) {
2069 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2071 ptr = &chaco_line[chaco_save_pnt];
2072 for (i=length_left; i; i--) *ptr2++ = *ptr++;
2073 length = chaco_save_pnt + 1;
2076 length = CHACO_LINE_LENGTH;
2081 ptr2 = fgets(&chaco_line[length_left], length, infile);
2083 if (ptr2 == (
char *) NULL) {
2088 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
2089 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2091 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2092 chaco_save_pnt = chaco_break_pnt;
2097 if (chaco_line[chaco_break_pnt] !=
'\0') {
2098 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
2100 chaco_save_pnt = chaco_break_pnt + 1;
2104 else if (white_seen) {
2111 chaco_break_pnt = CHACO_LINE_LENGTH;
2117 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
2118 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
2120 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2121 chaco_flush_line(infile);
2126 ptr = &(chaco_line[chaco_offset]);
2127 val = (int) strtol(ptr, &ptr2, 10);
2135 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
2141 void UserInputForTests::chaco_flush_line(
2148 while (c !=
'\n' && c !=
'\f')
2152 int UserInputForTests::chaco_input_graph(
2202 while (end_flag == 1) {
2203 *nvtxs = chaco_read_int(fin, &end_flag);
2207 printf(
"ERROR in graph file `%s':", inname);
2208 printf(
" Invalid number of vertices (%d).\n", *nvtxs);
2213 narcs = chaco_read_int(fin, &end_flag);
2215 printf(
"ERROR in graph file `%s':", inname);
2216 printf(
" Invalid number of expected edges (%d).\n", narcs);
2223 option = chaco_read_int(fin, &end_flag);
2225 using_ewgts = option - 10 * (option / 10);
2227 using_vwgts = option - 10 * (option / 10);
2229 vtxnums = option - 10 * (option / 10);
2232 (*nVwgts) = using_vwgts;
2233 (*nEwgts) = using_ewgts;
2236 if (!end_flag && using_vwgts==1){
2237 j = chaco_read_int(fin, &end_flag);
2238 if (!end_flag) (*nVwgts) = j;
2240 if (!end_flag && using_ewgts==1){
2241 j = chaco_read_int(fin, &end_flag);
2242 if (!end_flag) (*nEwgts) = j;
2247 j = chaco_read_int(fin, &end_flag);
2250 *start = (
int *) malloc((
unsigned) (*nvtxs + 1) *
sizeof(
int));
2252 *adjacency = (
int *) malloc((
unsigned) (2 * narcs + 1) *
sizeof(
int));
2257 *vweights = (
float *) malloc((
unsigned) (*nvtxs) * (*nVwgts) *
sizeof(float));
2262 *eweights = (
float *)
2263 malloc((
unsigned) (2 * narcs + 1) * (*nEwgts) *
sizeof(float));
2267 adjptr = *adjacency;
2276 while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2281 j = chaco_read_int(fin, &end_flag);
2283 if (vertex == *nvtxs)
2285 printf(
"ERROR in graph file `%s':", inname);
2286 printf(
" no vertex number in line %d.\n", line_num);
2290 if (j != vertex && j != vertex + 1) {
2291 printf(
"ERROR in graph file `%s':", inname);
2292 printf(
" out-of-order vertex number in line %d.\n", line_num);
2306 if (vertex > *nvtxs)
2310 if (using_vwgts && new_vertex) {
2311 for (j=0; j<(*nVwgts); j++){
2312 weight = chaco_read_val(fin, &end_flag);
2314 printf(
"ERROR in graph file `%s':", inname);
2315 printf(
" not enough weights for vertex %d.\n", vertex);
2319 (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2326 neighbor = chaco_read_int(fin, &end_flag);
2332 for (j=0; j<(*nEwgts); j++){
2333 eweight = chaco_read_val(fin, &end_flag);
2336 printf(
"ERROR in graph file `%s':", inname);
2337 printf(
" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2350 if (++nedges > 2*narcs) {
2351 printf(
"ERROR in graph file `%s':", inname);
2352 printf(
" at least %d adjacencies entered, but nedges = %d\n",
2357 *adjptr++ = neighbor;
2362 neighbor = chaco_read_int(fin, &end_flag);
2366 (*start)[vertex] = sum_edges;
2371 while (!flag && end_flag != -1) {
2372 chaco_read_int(fin, &end_flag);
2377 (*start)[*nvtxs] = sum_edges;
2385 if (*adjacency != NULL)
2388 if (*eweights != NULL)
2397 if (*adjacency != NULL)
2399 if (*vweights != NULL)
2401 if (*eweights != NULL)
2409 return (error_flag);
2413 int UserInputForTests::chaco_input_geom(
2415 const char *geomname,
2423 double xc, yc, zc =0;
2431 *x = *y = *z = NULL;
2434 while (end_flag == 1) {
2435 xc = chaco_read_val(fingeom, &end_flag);
2439 if (end_flag == -1) {
2440 printf(
"No values found in geometry file `%s'\n", geomname);
2446 yc = chaco_read_val(fingeom, &end_flag);
2447 if (end_flag == 0) {
2449 zc = chaco_read_val(fingeom, &end_flag);
2450 if (end_flag == 0) {
2452 chaco_read_val(fingeom, &end_flag);
2454 printf(
"Too many values on input line of geometry file `%s'\n",
2457 printf(
" Maximum dimensionality is 3\n");
2466 *x = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2469 *y = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2473 *z = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2477 for (nread = 1; nread < nvtxs; nread++) {
2480 i = fscanf(fingeom,
"%lf", &((*x)[nread]));
2482 else if (ndims == 2) {
2483 i = fscanf(fingeom,
"%lf%lf", &((*x)[nread]), &((*y)[nread]));
2485 else if (ndims == 3) {
2486 i = fscanf(fingeom,
"%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2491 printf(
"Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2496 else if (i != ndims) {
2497 printf(
"Wrong number of values in line %d of geometry file `%s'\n",
2498 line_num, geomname);
2507 while (!flag && end_flag != -1) {
2508 chaco_read_val(fingeom, &end_flag);
2520 int UserInputForTests::chaco_input_assign(
2522 const char *inassignname,
2533 while (end_flag == 1) {
2534 assignment[0] = chaco_read_int(finassign, &end_flag);
2537 if (assignment[0] < 0) {
2538 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2539 1, inassignname, assignment[0]);
2544 if (end_flag == -1) {
2545 printf(
"ERROR: No values found in assignment file `%s'\n", inassignname);
2551 int np = this->tcomm_->getSize();
2552 if (assignment[0] >= np) flag = assignment[0];
2553 srand(this->tcomm_->getRank());
2554 for (i = 1; i < nvtxs; i++) {
2555 j = fscanf(finassign,
"%hd", &(assignment[i]));
2557 printf(
"ERROR: Too few values in assignment file `%s'.\n", inassignname);
2561 if (assignment[i] < 0) {
2562 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2563 i+1, inassignname, assignment[i]);
2567 if (assignment[i] >= np) {
2569 if (assignment[i] > flag)
2570 flag = assignment[i];
2571 assignment[i] = rand() % np;
2577 printf(
"WARNING: Possible error in assignment file `%s'\n",
2579 printf(
" Max assignment set (%d) greater than "
2580 "max processor rank (%d)\n", flag, np-1);
2581 printf(
" Some vertices given random initial assignments\n");
2587 while (!flag && end_flag != -1) {
2588 chaco_read_int(finassign, &end_flag);
2593 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2594 printf(
" Numerical data found after expected end of file\n");
2602 void UserInputForTests::readPamgenMeshFile(
string path,
string testData)
2604 #ifdef HAVE_ZOLTAN2_PAMGEN
2605 int rank = this->tcomm_->getRank();
2606 if (verbose_ && tcomm_->getRank() == 0)
2607 std::cout <<
"UserInputForTestsBD::readPamgenFile, Read: " << testData << std::endl;
2614 std::ostringstream meshFileName;
2615 meshFileName << path <<
"/" << testData <<
".pmgen";
2618 file.open(meshFileName.str(), std::ios::in);
2622 if(verbose_ && tcomm_->getRank() == 0)
2624 std::cout <<
"Unable to open pamgen mesh: ";
2625 std::cout << meshFileName.str();
2626 std::cout <<
"\nPlease check file path and name." << std::endl;
2632 file.seekg (0,file.end);
2639 while(std::getline(file,line))
2641 if( line.find(
"nz") != std::string::npos ||
2642 line.find(
"nphi") != std::string::npos)
2650 file.seekg(0, std::ios::beg);
2655 this->tcomm_->broadcast(0,
sizeof(
int), (
char *)&dimension);
2656 this->tcomm_->broadcast(0,
sizeof(
size_t),(
char *)&len);
2657 this->tcomm_->barrier();
2660 if(verbose_ && tcomm_->getRank() == 0)
2661 std::cout <<
"Pamgen Mesh file size == 0, exiting UserInputForTests early." << std::endl;
2665 char * file_data =
new char[len+1];
2666 file_data[len] =
'\0';
2668 file.read(file_data,len);
2672 this->tcomm_->broadcast(0,(
int)len+1,file_data);
2673 this->tcomm_->barrier();
2677 this->pamgen_mesh = rcp(
new PamgenMesh);
2678 this->havePamgenMesh =
true;
2679 pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2682 pamgen_mesh->storeMesh();
2683 this->tcomm_->barrier();
2686 this->setPamgenCoordinateMV();
2689 this->setPamgenAdjacencyGraph();
2691 this->tcomm_->barrier();
2692 if(rank == 0) file.close();
2693 delete [] file_data;
2695 throw std::runtime_error(
"Pamgen requested but Trilinos "
2696 "not built with Pamgen");
2700 #ifdef HAVE_ZOLTAN2_PAMGEN
2701 void UserInputForTests::setPamgenCoordinateMV()
2703 int dimension = pamgen_mesh->num_dim;
2707 zgno_t numelements = pamgen_mesh->num_elem;
2708 zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2711 for(
int i = 0; i < dimension; ++i){
2712 elem_coords[i] =
new zscalar_t[numelements];
2713 double *tmp = &pamgen_mesh->element_coord[i*numelements];
2714 for (
int j = 0; j < numelements; j++) elem_coords[i][j] = tmp[j];
2718 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2722 Array<zgno_t> elementList(numelements);
2723 for (Array<zgno_t>::size_type k = 0; k < numelements; ++k) {
2724 elementList[k] = pamgen_mesh->element_order_map[k];
2727 mp = rcp (
new map_t (numGlobalElements, elementList, 0, this->tcomm_));
2731 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(dimension);
2732 for (
int i = 0; i < dimension; i++){
2733 if(numelements > 0){
2734 Teuchos::ArrayView<const zscalar_t> a(elem_coords[i], numelements);
2738 Teuchos::ArrayView<const zscalar_t> a;
2744 xyz_ = RCP<tMVector_t>(
new
2749 void UserInputForTests::setPamgenAdjacencyGraph()
2759 size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2760 size_t local_els = (
size_t)this->pamgen_mesh->num_elem;
2762 size_t global_els = (size_t)this->pamgen_mesh->num_elems_global;
2763 size_t global_nodes = (
size_t)this->pamgen_mesh->num_nodes_global;
2767 RCP<const map_t> rowMap = rcp(
new map_t(global_els,0,this->tcomm_));
2768 RCP<const map_t> rangeMap = rowMap;
2771 RCP<const map_t> domainMap = rcp(
new map_t(global_nodes,0,this->tcomm_));
2774 int blks = this->pamgen_mesh->num_elem_blk;
2775 int max_nodes_per_el = 0;
2776 for(
int i = 0; i < blks; i++)
2777 if (this->pamgen_mesh->nodes_per_element[i] > max_nodes_per_el)
2778 max_nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2781 Teuchos::RCP<tcrsMatrix_t> C = rcp(
new tcrsMatrix_t(rowMap,max_nodes_per_el));
2783 Array<zgno_t> g_el_ids(local_els);
2784 for (
size_t k = 0; k < local_els; ++k) {
2785 g_el_ids[k] = pamgen_mesh->global_element_numbers[k]-1;
2788 Array<zgno_t> g_node_ids(local_nodes);
2789 for (
size_t k = 0; k < local_nodes; ++k) {
2790 g_node_ids[k] = pamgen_mesh->global_node_numbers[k]-1;
2797 for(
int i = 0; i < blks; i++)
2799 int el_per_block = this->pamgen_mesh->elements[i];
2800 int nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2801 int * connect = this->pamgen_mesh->elmt_node_linkage[i];
2803 for(
int j = 0; j < el_per_block; j++)
2805 const zgno_t gid =
static_cast<zgno_t>(g_el_ids[el_no]);
2806 for(
int k = 0; k < nodes_per_el; k++)
2808 int g_node_i = g_node_ids[connect[j*nodes_per_el+k]-1];
2809 C->insertGlobalValues(gid,
2810 Teuchos::tuple<zgno_t>(g_node_i),
2811 Teuchos::tuple<zscalar_t>(one));
2817 C->fillComplete(domainMap, rangeMap);
2823 Tpetra::MatrixMatrix::Multiply(*C,
false, *C,
true, *A);
2828 this->M_ = rcp(
new tcrsMatrix_t(rowMap, A->getGlobalMaxNumRowEntries()));
2831 Teuchos::ArrayView<const zgno_t> rowMapElementList =
2832 rowMap->getLocalElementList();
2833 for (Teuchos_Ordinal ii = 0; ii < rowMapElementList.size(); ii++)
2835 zgno_t gid = rowMapElementList[ii];
2836 size_t numEntriesInRow = A->getNumEntriesInGlobalRow (gid);
2837 typename tcrsMatrix_t::nonconst_global_inds_host_view_type rowinds(
"Indices", numEntriesInRow);
2838 typename tcrsMatrix_t::nonconst_values_host_view_type rowvals(
"Values", numEntriesInRow);
2841 Array<zscalar_t> mod_rowvals;
2842 Array<zgno_t> mod_rowinds;
2843 A->getGlobalRowCopy (gid, rowinds, rowvals, numEntriesInRow);
2844 for (
size_t i = 0; i < numEntriesInRow; i++) {
2847 if (rowvals[i] >= 1)
2849 mod_rowvals.push_back(one);
2850 mod_rowinds.push_back(rowinds[i]);
2853 this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2856 this->M_->fillComplete();
keep typedefs that commonly appear in many places localized
#define TEST_FAIL_AND_THROW(comm, ok, s)
#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.
int buildCrsMatrix(int xdim, int ydim, int zdim, std::string problemType, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, Teuchos::RCP< Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, nod_t >> &M_)
common code used by tests
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
Xpetra::CrsGraph< zlno_t, zgno_t, znode_t > xcrsGraph_t
Tpetra::Vector< zscalar_t, zlno_t, zgno_t, znode_t > tVector_t
Tpetra::Map::local_ordinal_type zlno_t
static const std::string fail
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Tpetra::CrsGraph< zlno_t, zgno_t, znode_t > tcrsGraph_t
string trim_left_copy(const string &s, const string &delimiters=" \f\n\r\t\v")
Tpetra::global_size_t global_size_t
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Tpetra::Map::global_ordinal_type zgno_t