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>
48using Teuchos::ArrayRCP;
49using Teuchos::ArrayView;
54using Teuchos::rcp_const_cast;
55using Teuchos::ParameterList;
92 typedef Tpetra::Map<zlno_t, zgno_t, znode_t>
map_t;
93 typedef Tpetra::Export<zlno_t, zgno_t, znode_t>
export_t;
94 typedef Tpetra::Import<zlno_t, zgno_t, znode_t>
import_t;
118 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
119 bool distributeInput=
true);
138 const RCP<
const Comm<int> > &c,
bool debugInfo=
false,
139 bool distributeInput=
true);
157 const RCP<
const Comm<int> > &c);
186#ifdef HAVE_ZOLTAN2_PAMGEN
187 PamgenMesh * getPamGenMesh(){
return this->pamgen_mesh.operator->();}
222 const RCP<const Comm<int> > tcomm_;
225#ifdef HAVE_ZOLTAN2_PAMGEN
226 RCP<PamgenMesh> pamgen_mesh;
229 RCP<tcrsMatrix_t> M_;
230 RCP<xcrsMatrix_t> xM_;
232 RCP<tMVector_t> xyz_;
233 RCP<tMVector_t> vtxWeights_;
234 RCP<tMVector_t> edgWeights_;
241 void readMatrixMarketFile(
string path,
string testData,
bool distributeInput =
true);
246 void buildCrsMatrix(
int xdim,
int ydim,
int zdim,
string type,
247 bool distributeInput);
253 void readZoltanTestData(
string path,
string testData,
254 bool distributeInput);
257 RCP<tcrsMatrix_t> modifyMatrixGIDs(RCP<tcrsMatrix_t> &in);
258 inline zgno_t newID(
const zgno_t id) {
return id * 2 + 10001; }
261 void getUIChacoGraph(FILE *fptr,
bool haveAssign, FILE *assignFile,
262 string name,
bool distributeInput);
265 void getUIChacoCoords(FILE *fptr,
string name);
271 static const int CHACO_LINE_LENGTH=200;
272 char chaco_line[CHACO_LINE_LENGTH];
277 double chaco_read_val(FILE* infile,
int *end_flag);
278 int chaco_read_int(FILE* infile,
int *end_flag);
279 void chaco_flush_line(FILE*);
282 int chaco_input_graph(FILE *fin,
const char *inname,
int **start,
283 int **adjacency,
int *nvtxs,
int *nVwgts,
284 float **vweights,
int *nEwgts,
float **eweights);
287 int chaco_input_geom(FILE *fingeom,
const char *geomname,
int nvtxs,
288 int *igeom,
double **x,
double **y,
double **z);
291 int chaco_input_assign(FILE *finassign,
const char *assignname,
int nvtxs,
299 void readGeometricGenTestData(
string path,
string testData);
302 void readGeoGenParams(
string paramFileName,
303 ParameterList &geoparams);
307 static string trim_right_copy(
const string& s,
308 const string& delimiters =
" \f\n\r\t\v" );
310 static string trim_left_copy(
const string& s,
311 const string& delimiters =
" \f\n\r\t\v" );
313 static string trim_copy(
const string& s,
314 const string& delimiters =
" \f\n\r\t\v" );
318 void readPamgenMeshFile(
string path,
string testData);
319#ifdef HAVE_ZOLTAN2_PAMGEN
320 void setPamgenAdjacencyGraph();
321 void setPamgenCoordinateMV();
326 const RCP<
const Comm<int> > &c,
327 bool debugInfo,
bool distributeInput):
328verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
329M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
330chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
332 bool zoltan1 =
false;
333 string::size_type loc = path.find(
"/zoltan/test/");
334 if (loc != string::npos)
338 readZoltanTestData(path, testData, distributeInput);
340 readMatrixMarketFile(path, testData);
345 const RCP<
const Comm<int> > &c,
347 bool distributeInput):
348verbose_(debugInfo), tcomm_(c), havePamgenMesh(false),
349M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
350chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
352 if (matrixType.size() == 0){
358 matrixType = string(
"Laplace1D");
360 matrixType = string(
"Laplace2D");
362 matrixType = string(
"Laplace3D");
364 throw std::runtime_error(
"input");
366 if (verbose_ && tcomm_->getRank() == 0)
367 std::cout <<
"UserInputForTests, Matrix type : " << matrixType << std::endl;
370 buildCrsMatrix(x, y, z, matrixType, distributeInput);
374 const RCP<
const Comm<int> > &c):
375tcomm_(c), havePamgenMesh(false),
376M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_(),
377chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
381 bool distributeInput =
true, debugInfo =
true;
383 if(pList.isParameter(
"distribute input"))
384 distributeInput = pList.get<
bool>(
"distribute input");
386 if(pList.isParameter(
"debug"))
387 debugInfo = pList.get<
bool>(
"debug");
388 this->verbose_ = debugInfo;
390 if(pList.isParameter(
"input file"))
395 if(pList.isParameter(
"input path"))
396 path = pList.get<
string>(
"input path");
398 string testData = pList.get<
string>(
"input file");
404 if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Geometric Generator")
406 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Pamgen")
410 else if(pList.isParameter(
"file type") && pList.get<
string>(
"file type") ==
"Chaco")
414 switch (file_format) {
415 case GEOMGEN: readGeometricGenTestData(path,testData);
break;
416 case PAMGEN: readPamgenMeshFile(path,testData);
break;
417 case CHACO: readZoltanTestData(path, testData, distributeInput);
break;
418 default: readMatrixMarketFile(path, testData, distributeInput);
break;
421 }
else if(pList.isParameter(
"x") || pList.isParameter(
"y") || pList.isParameter(
"z")){
425 if(pList.isParameter(
"x")) x = pList.get<
int>(
"x");
426 if(pList.isParameter(
"y")) y = pList.get<
int>(
"y");
427 if(pList.isParameter(
"z")) z = pList.get<
int>(
"z");
429 string problemType =
"";
430 if(pList.isParameter(
"equation type")) problemType = pList.get<
string>(
"equation type");
432 if (problemType.size() == 0){
438 problemType = string(
"Laplace1D");
440 problemType = string(
"Laplace2D");
442 problemType = string(
"Laplace3D");
444 throw std::runtime_error(
"input");
446 if (verbose_ && tcomm_->getRank() == 0)
447 std::cout <<
"UserInputForTests, Matrix type : " << problemType << std::endl;
451 buildCrsMatrix(x, y, z, problemType, distributeInput);
454 std::cerr <<
"Input file block undefined!" << std::endl;
463 throw std::runtime_error(
"could not read coord file");
480 throw std::runtime_error(
"could not read mtx file");
487 throw std::runtime_error(
"could not read mtx file");
488 return rcp_const_cast<tcrsGraph_t>(M_->getCrsGraph());
493 RCP<tVector_t> V = rcp(
new tVector_t(M_->getRowMap(), 1));
501 RCP<tMVector_t> mV = rcp(
new tMVector_t(M_->getRowMap(), nvec));
510 throw std::runtime_error(
"could not read mtx file");
517 throw std::runtime_error(
"could not read mtx file");
518 return rcp_const_cast<xcrsGraph_t>(xM_->getCrsGraph());
543 if(input_type ==
"coordinates")
545 else if(input_type ==
"tpetra_vector")
547 else if(input_type ==
"tpetra_multivector")
549 else if(input_type ==
"tpetra_crs_graph")
551 else if(input_type ==
"tpetra_crs_matrix")
553 else if(input_type ==
"xpetra_vector")
555 else if(input_type ==
"xpetra_multivector")
557 else if(input_type ==
"xpetra_crs_graph")
559 else if(input_type ==
"xpetra_crs_matrix")
567 return xyz_.is_null() ? false :
true;
572 return vtxWeights_.is_null() ? false :
true;
577 return edgWeights_.is_null() ? false :
true;
582 return M_.is_null() ? false :
true;
587 return M_.is_null() ? false :
true;
602 return M_.is_null() ? false :
true;
607 return M_.is_null() ? false :
true;
622 return this->havePamgenMesh;
627 ArrayView<ArrayRCP<zscalar_t > > data)
632 size_t dim = data.size();
633 for (
size_t i=0; i < dim; i++){
636 throw (std::bad_alloc());
637 data[i] = Teuchos::arcp(tmp, 0, length,
true);
640 zscalar_t scalingFactor = (max-min) / RAND_MAX;
642 for (
size_t i=0; i < dim; i++){
644 for (
zlno_t j=0; j < length; j++)
645 *x++ = min + (
zscalar_t(rand()) * scalingFactor);
651string UserInputForTests::trim_right_copy(
653 const string& delimiters)
655 return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
658string UserInputForTests::trim_left_copy(
660 const string& delimiters)
662 return s.substr( s.find_first_not_of( delimiters ) );
665string UserInputForTests::trim_copy(
667 const string& delimiters)
669 return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
672void UserInputForTests::readGeometricGenTestData(
string path,
676 std::ostringstream
fname;
677 fname << path <<
"/" << testData <<
".txt";
679 if (verbose_ && tcomm_->getRank() == 0)
680 std::cout <<
"UserInputForTests, Read: " <<
fname.str() << std::endl;
682 Teuchos::ParameterList geoparams(
"geo params");
683 readGeoGenParams(
fname.str(),geoparams);
695 for(
int i = 0; i < coord_dim; ++i){
696 coords[i] =
new zscalar_t[numLocalPoints];
704 if (numWeightsPerCoord) {
706 weight =
new zscalar_t * [numWeightsPerCoord];
707 for(
int i = 0; i < numWeightsPerCoord; ++i){
708 weight[i] =
new zscalar_t[numLocalPoints];
719 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp =
720 rcp(
new Tpetra::Map<zlno_t, zgno_t, znode_t>(numGlobalPoints, numLocalPoints, 0, this->tcomm_));
723 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(coord_dim);
724 for (
int i=0; i < coord_dim; i++){
725 if(numLocalPoints > 0){
726 Teuchos::ArrayView<const zscalar_t> a(coords[i], numLocalPoints);
730 Teuchos::ArrayView<const zscalar_t> a;
736 xyz_ = RCP<tMVector_t>(
new
741 if (numWeightsPerCoord) {
743 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > weightView(numWeightsPerCoord);
744 for (
int i=0; i < numWeightsPerCoord; i++){
745 if(numLocalPoints > 0){
746 Teuchos::ArrayView<const zscalar_t> a(weight[i], numLocalPoints);
750 Teuchos::ArrayView<const zscalar_t> a;
755 vtxWeights_ = RCP<tMVector_t>(
new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
756 numWeightsPerCoord));
760void UserInputForTests::readGeoGenParams(
string paramFileName,
761 ParameterList &geoparams){
765 std::string input =
"";
767 for(
int i = 0; i < 25000; ++i){
772 if(this->tcomm_->getRank() == 0){
774 std::fstream inParam(paramFileName.c_str());
781 std::string tmp =
"";
782 getline (inParam,tmp);
783 while (!inParam.eof()){
785 tmp = trim_copy(tmp);
790 getline (inParam,tmp);
793 for (
size_t i = 0; i < input.size(); ++i){
801 int size = (int)input.size();
805 this->tcomm_->broadcast(0,
sizeof(
int), (
char*) &size);
807 throw "File " + paramFileName +
" cannot be opened.";
809 this->tcomm_->broadcast(0, size, inp);
810 std::istringstream inParam(inp);
812 getline (inParam,str);
813 while (!inParam.eof()){
815 size_t pos = str.find(
'=');
816 if(pos == string::npos){
817 throw "Invalid Line:" + str +
" in parameter file";
819 string paramname = trim_copy(str.substr(0,pos));
820 string paramvalue = trim_copy(str.substr(pos + 1));
821 geoparams.set(paramname, paramvalue);
823 getline (inParam,str);
828RCP<tcrsMatrix_t> UserInputForTests::modifyMatrixGIDs(
829 RCP<tcrsMatrix_t> &inMatrix
838 RCP<const map_t> inMap = inMatrix->getRowMap();
840 size_t nRows = inMap->getLocalNumElements();
841 auto inRows = inMap->getMyGlobalIndices();
842 Teuchos::Array<zgno_t> outRows(nRows);
843 for (
size_t i = 0; i < nRows; i++) {
844 outRows[i] = newID(inRows[i]);
847 Tpetra::global_size_t nGlobalRows = inMap->getGlobalNumElements();
848 RCP<map_t> outMap = rcp(
new map_t(nGlobalRows, outRows(), 0,
851#ifdef INCLUDE_LENGTHY_OUTPUT
854 std::cout << inMap->getComm()->getRank() <<
" KDDKDD "
855 <<
"nGlobal " << inMap->getGlobalNumElements() <<
" "
856 << outMap->getGlobalNumElements() <<
"; "
857 <<
"nLocal " << inMap->getLocalNumElements() <<
" "
858 << outMap->getLocalNumElements() <<
"; "
860 std::cout << inMap->getComm()->getRank() <<
" KDDKDD ";
861 for (
size_t i = 0; i < nRows; i++)
862 std::cout <<
"(" << inMap->getMyGlobalIndices()[i] <<
", "
863 << outMap->getMyGlobalIndices()[i] <<
") ";
864 std::cout << std::endl;
870 size_t rowLen = inMatrix->getLocalMaxNumRowEntries();
871 RCP<tcrsMatrix_t> outMatrix = rcp(
new tcrsMatrix_t(outMap, rowLen));
873 typename tcrsMatrix_t::nonconst_global_inds_host_view_type indices(
"Indices", rowLen);
874 typename tcrsMatrix_t::nonconst_values_host_view_type values(
"Values", rowLen);
876 for (
size_t i = 0; i < nRows; i++) {
878 zgno_t inGid = inMap->getGlobalElement(i);
879 inMatrix->getGlobalRowCopy(inGid, indices, values, nEntries);
880 for (
size_t j = 0; j < nEntries; j++)
881 indices[j] = newID(indices[j]);
883 zgno_t outGid = outMap->getGlobalElement(i);
884 ArrayView<zgno_t> Indices(indices.data(), nEntries);
885 ArrayView<zscalar_t> Values(values.data(), nEntries);
886 outMatrix->insertGlobalValues(outGid, Indices(0, nEntries),
887 Values(0, nEntries));
889 outMatrix->fillComplete();
891#ifdef INCLUDE_LENGTHY_OUTPUT
894 std::cout << inMap->getComm()->getRank() <<
" KDDKDD Rows "
895 <<
"nGlobal " << inMatrix->getGlobalNumRows() <<
" "
896 << outMatrix->getGlobalNumRows() <<
"; "
897 <<
"nLocal " << inMatrix->getLocalNumRows() <<
" "
898 << outMatrix->getLocalNumRows() << std::endl;
899 std::cout << inMap->getComm()->getRank() <<
" KDDKDD NNZS "
900 <<
"nGlobal " << inMatrix->getGlobalNumEntries() <<
" "
901 << outMatrix->getGlobalNumEntries() <<
"; "
902 <<
"nLocal " << inMatrix->getLocalNumEntries() <<
" "
903 << outMatrix->getLocalNumEntries() << std::endl;
906 Teuchos::Array<zgno_t> in(rowLen), out(rowLen);
907 Teuchos::Array<zscalar_t> inval(rowLen), outval(rowLen);
909 for (
size_t i = 0; i < nRows; i++) {
910 std::cout << inMap->getComm()->getRank() <<
" KDDKDD " << i <<
" nnz(";
911 inMatrix->getGlobalRowCopy(inMap->getGlobalElement(i), in, inval, nIn);
912 outMatrix->getGlobalRowCopy(outMap->getGlobalElement(i), out, outval,
915 std::cout << nIn <<
", " << nOut <<
"): ";
916 for (
size_t j = 0; j < nIn; j++) {
917 std::cout <<
"(" << in[j] <<
" " << inval[j] <<
", "
918 << out[j] <<
" " << outval[j] <<
") ";
920 std::cout << std::endl;
930void UserInputForTests::readMatrixMarketFile(
936 std::ostringstream
fname;
937 fname << path <<
"/" << testData <<
".mtx";
939 if (verbose_ && tcomm_->getRank() == 0)
940 std::cout <<
"UserInputForTests, Read: " <<
fname.str() << std::endl;
945 RCP<tcrsMatrix_t> toMatrix;
946 RCP<tcrsMatrix_t> fromMatrix;
949 typedef Tpetra::MatrixMarket::Reader<tcrsMatrix_t> reader_type;
950 fromMatrix = reader_type::readSparseFile(
fname.str(), tcomm_,
952#ifdef KDD_NOT_READY_YET
955 fromMatrix = modifyMatrixGIDs(fromMatrix);
960 if (verbose_ && tcomm_->getRank() == 0)
961 std::cout <<
"Constructing serial distribution of matrix" << std::endl;
963 RCP<const map_t> fromMap = fromMatrix->getRowMap();
965 size_t numGlobalCoords = fromMap->getGlobalNumElements();
966 size_t numLocalCoords = this->tcomm_->getRank() == 0 ? numGlobalCoords : 0;
967 RCP<const map_t> toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, 0, tcomm_));
969 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
971 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
972 toMatrix->fillComplete();
975 toMatrix = fromMatrix;
977 }
catch (std::exception &e) {
978 if (tcomm_->getRank() == 0) {
979 std::cout <<
"UserInputForTests unable to read matrix market file:"
980 <<
fname.str() << std::endl;
981 std::cout << e.what() << std::endl;
986 "UserInputForTests unable to read matrix market file");
989#ifdef INCLUDE_LENGTHY_OUTPUT
990 std::cout << tcomm_->getRank() <<
" KDDKDD " << M_->getLocalNumRows()
991 <<
" " << M_->getGlobalNumRows()
992 <<
" " << M_->getLocalNumEntries()
993 <<
" " << M_->getGlobalNumEntries() << std::endl;
1001 fname << path <<
"/" << testData <<
"_coord.mtx";
1003 size_t coordDim = 0, numGlobalCoords = 0;
1004 size_t msg[2]={0,0};
1005 ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1006 std::ifstream coordFile;
1008 if (tcomm_->getRank() == 0){
1011 std::cout <<
"UserInputForTests, Read: " <<
1012 fname.str() << std::endl;
1016 coordFile.open(
fname.str().c_str());
1018 catch (std::exception &){
1029 while (!done && !
fail && coordFile.good()){
1030 coordFile.getline(c, 256);
1033 else if (c[0] ==
'%')
1037 std::istringstream s(c);
1038 s >> numGlobalCoords >> coordDim;
1039 if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1048 xyz = Teuchos::arcp(
new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1050 for (
size_t dim=0; !
fail && dim < coordDim; dim++){
1056 xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1058 for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1059 coordFile.getline(c, 256);
1060 std::istringstream s(c);
1064 if (idx < numGlobalCoords)
1070 ArrayRCP<zscalar_t> emptyArray;
1071 for (
size_t dim=0; dim < coordDim; dim++)
1072 xyz[dim] = emptyArray;
1085 msg[1] = numGlobalCoords;
1089 Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1092 numGlobalCoords = msg[1];
1098 RCP<const map_t> toMap;
1101 const RCP<const map_t> &mapM = M_->getRowMap();
1105 if (verbose_ && tcomm_->getRank() == 0)
1107 std::cout <<
"Matrix was null. ";
1108 std::cout <<
"Constructing distribution map for coordinate vector."
1112 if(!distributeInput)
1114 if (verbose_ && tcomm_->getRank() == 0)
1115 std::cout <<
"Constructing serial distribution map for coordinates."
1118 size_t numLocalCoords = this->tcomm_->getRank()==0 ? numGlobalCoords : 0;
1119 toMap = rcp(
new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1121 toMap = rcp(
new map_t(numGlobalCoords, base, tcomm_));
1129 ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1131 if (tcomm_->getRank() == 0){
1133 for (
size_t dim=0; dim < coordDim; dim++)
1134 coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1138 throw std::bad_alloc();
1140 ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1142#ifdef KDD_NOT_READY_YET
1151 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1152 rowIds.view(0, numGlobalCoords),
1155 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1159 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1163 RCP<const map_t> fromMap = rcp(
new map_t(numGlobalCoords,
1164 ArrayView<zgno_t>(), base, tcomm_));
1166 tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1170 xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1174void UserInputForTests::buildCrsMatrix(
int xdim,
int ydim,
int zdim,
1175 string problemType,
bool distributeInput)
1177#ifdef HAVE_ZOLTAN2_GALERI
1178 Teuchos::CommandLineProcessor tclp;
1179 Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1180 xdim, ydim, zdim, problemType);
1182 RCP<const Tpetra::Map<zlno_t, zgno_t> > map;
1183 if (distributeInput)
1184 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(params.GetNumGlobalElements(),
1188 size_t nGlobalElements = params.GetNumGlobalElements();
1189 size_t nLocalElements = ((tcomm_->getRank() == 0) ? nGlobalElements : 0);
1190 map = rcp(
new Tpetra::Map<zlno_t, zgno_t>(nGlobalElements, nLocalElements, 0,
1194 if (verbose_ && tcomm_->getRank() == 0){
1196 std::cout <<
"Matrix is " << (distributeInput ?
"" :
"not");
1197 std::cout <<
"distributed." << std::endl;
1199 std::cout <<
"UserInputForTests, Create matrix with " << problemType;
1200 std::cout <<
" (and " << xdim;
1202 std::cout <<
" x " << ydim <<
" x " << zdim;
1204 std::cout <<
" x" << ydim <<
" x 1";
1206 std::cout <<
"x 1 x 1";
1208 std::cout <<
" mesh)" << std::endl;
1214 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 =
1215 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> >
1216 (params.GetMatrixType(), map, params.GetParameterList());
1217 M_ = Pr->BuildMatrix();
1219 catch (std::exception &) {
1223 "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1229 if (verbose_ && tcomm_->getRank() == 0)
1231 "UserInputForTests, Implied matrix row coordinates computed" <<
1234 ArrayView<const zgno_t> gids = map->getLocalElementList();
1237 size_t pos = problemType.find(
"2D");
1238 if (pos != string::npos)
1240 else if (problemType ==
string(
"Laplace1D") ||
1241 problemType == string(
"Identity"))
1247 for (
int i=0; i < dim; i++){
1250 throw(std::bad_alloc());
1251 coordinates[i] = Teuchos::arcp(c, 0, count,
true);
1258 zgno_t xySize = xdim * ydim;
1259 for (
zlno_t i=0; i < count; i++){
1260 zgno_t iz = gids[i] / xySize;
1261 zgno_t xy = gids[i] - iz*xySize;
1270 for (
zlno_t i=0; i < count; i++){
1277 for (
zlno_t i=0; i < count; i++)
1282 Array<ArrayView<const zscalar_t> > coordView(dim);
1284 for (
int i=0; i < dim; i++)
1287 xyz_ = rcp(
new tMVector_t(map, coordView.view(0, dim), dim));
1289 throw std::runtime_error(
"Galeri input requested but Trilinos is "
1290 "not built with Galeri.");
1294void UserInputForTests::readZoltanTestData(
string path,
string testData,
1295 bool distributeInput)
1297 int rank = tcomm_->getRank();
1298 FILE *graphFile = NULL;
1299 FILE *coordFile = NULL;
1300 FILE *assignFile = NULL;
1303 for (
int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] =
'\0';
1307 std::ostringstream chGraphFileName;
1308 chGraphFileName << path <<
"/" << testData <<
".graph";
1311 std::ostringstream chCoordFileName;
1312 chCoordFileName << path <<
"/" << testData <<
".coords";
1315 std::ostringstream chAssignFileName;
1316 chAssignFileName << path <<
"/" << testData <<
".assign";
1319 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1323 chGraphFileName.str(
"");
1324 chCoordFileName.str(
"");
1326 chGraphFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".graph";
1327 chCoordFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".coords";
1328 chAssignFileName << path <<
"/ch_" << testData <<
"/" << testData <<
".assign";
1331 graphFile = fopen(chGraphFileName.str().c_str(),
"r");
1334 memset(fileInfo, 0,
sizeof(
int) * 3);
1337 if (verbose_ && tcomm_->getRank() == 0)
1338 std::cout <<
"UserInputForTests, open " <<
1339 chGraphFileName.str () << std::endl;
1341 coordFile = fopen(chCoordFileName.str().c_str(),
"r");
1344 if (verbose_ && tcomm_->getRank() == 0)
1345 std::cout <<
"UserInputForTests, open " <<
1346 chCoordFileName.str () << std::endl;
1349 assignFile = fopen(chAssignFileName.str().c_str(),
"r");
1352 if (verbose_ && tcomm_->getRank() == 0)
1353 std::cout <<
"UserInputForTests, open " <<
1354 chAssignFileName.str () << std::endl;
1357 if (verbose_ && tcomm_->getRank() == 0){
1358 std::cout <<
"UserInputForTests, unable to open file: ";
1359 std::cout << chGraphFileName.str() << std::endl;
1365 Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1367 bool haveGraph = (fileInfo[0] == 1);
1368 bool haveCoords = (fileInfo[1] == 1);
1369 bool haveAssign = (fileInfo[2] == 1);
1374 getUIChacoGraph(graphFile, haveAssign, assignFile,
1375 testData, distributeInput);
1382 getUIChacoCoords(coordFile, testData);
1391void UserInputForTests::getUIChacoGraph(FILE *fptr,
bool haveAssign,
1392 FILE *assignFile,
string fname,
1393 bool distributeInput)
1395 int rank = tcomm_->getRank();
1397 int nvtxs=0, nedges=0;
1398 int nVwgts=0, nEwgts=0;
1399 int *start = NULL, *adj = NULL;
1400 float *ewgts = NULL, *vwgts = NULL;
1401 size_t *nzPerRow = NULL;
1402 size_t maxRowLen = 0;
1404 ArrayRCP<const size_t> rowSizes;
1406 bool haveEdges =
true;
1410 memset(graphCounts, 0, 5*
sizeof(
int));
1413 fail = chaco_input_graph(fptr,
fname.c_str(), &start, &adj,
1414 &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1425 std::cout <<
"UserInputForTests, " << nvtxs <<
" vertices,";
1427 std::cout << start[nvtxs] <<
" edges,";
1429 std::cout <<
"no edges,";
1430 std::cout << nVwgts <<
" vertex weights, ";
1431 std::cout << nEwgts <<
" edge weights" << std::endl;
1438 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1439 throw std::runtime_error(
"Unable to read chaco file");
1443 nedges = start[nvtxs];
1445 nzPerRow =
new size_t [nvtxs];
1447 throw std::bad_alloc();
1448 rowSizes = arcp(nzPerRow, 0, nvtxs,
true);
1451 for (
int i=0; i < nvtxs; i++){
1452 nzPerRow[i] = start[i+1] - start[i];
1453 if (nzPerRow[i] > maxRowLen)
1454 maxRowLen = nzPerRow[i];
1458 memset(nzPerRow, 0,
sizeof(
size_t) * nvtxs);
1465 for (
int i=0; i < nedges; i++)
1469 graphCounts[0] = nvtxs;
1470 graphCounts[1] = nedges;
1471 graphCounts[2] = nVwgts;
1472 graphCounts[3] = nEwgts;
1473 graphCounts[4] = (int)maxRowLen;
1476 Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1478 if (graphCounts[0] == 0)
1479 throw std::runtime_error(
"Unable to read chaco file");
1481 haveEdges = (graphCounts[1] > 0);
1483 RCP<tcrsMatrix_t> fromMatrix;
1484 RCP<const map_t> fromMap;
1488 fromMap = rcp(
new map_t(nvtxs, nvtxs, base, tcomm_));
1496 if (nedges && !edgeIds)
1497 throw std::bad_alloc();
1498 for (
int i=0; i < nedges; i++)
1499 edgeIds[i] = adj[i];
1504 zgno_t *nextId = edgeIds;
1505 Array<zscalar_t> values(nedges, 1.0);
1506 if (nedges > 0 && nEwgts > 0) {
1507 for (
int i=0; i < nedges; i++)
1508 values[i] = ewgts[i];
1513 for (
int i=0; i < nvtxs; i++){
1514 if (nzPerRow[i] > 0){
1515 ArrayView<const zgno_t> rowNz(nextId, nzPerRow[i]);
1516 fromMatrix->insertGlobalValues(i, rowNz, values.view(start[i], start[i+1] - start[i]));
1517 nextId += nzPerRow[i];
1525 fromMatrix->fillComplete();
1528 nvtxs = graphCounts[0];
1529 nedges = graphCounts[1];
1530 nVwgts = graphCounts[2];
1531 nEwgts = graphCounts[3];
1532 maxRowLen = graphCounts[4];
1536 fromMap = rcp(
new map_t(nvtxs, 0, base, tcomm_));
1541 fromMatrix->fillComplete();
1546 size_t sz = fromMatrix->getLocalMaxNumRowEntries();
1547 Teuchos::Array<zgno_t> indices(sz);
1548 Teuchos::Array<zscalar_t> values(sz);
1549 for (
size_t i = 0; i < fromMatrix->getLocalNumRows(); i++) {
1550 zgno_t gid = fromMatrix->getRowMap()->getGlobalElement(i);
1552 fromMatrix->getGlobalRowCopy(gid, indices(), values(), num);
1553 std::cout <<
"ROW " << gid <<
": ";
1554 for (
size_t j = 0; j < num; j++)
1555 std::cout << indices[j] <<
" ";
1556 std::cout << std::endl;
1561 RCP<const map_t> toMap;
1562 RCP<tcrsMatrix_t> toMatrix;
1563 RCP<import_t> importer;
1565 if (distributeInput) {
1568 short *assignments =
new short[nvtxs];
1570 fail = chaco_input_assign(assignFile,
fname.c_str(), nvtxs, assignments);
1573 Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1576 Teuchos::Array<zgno_t> mine;
1577 for (
int i = 0; i < nvtxs; i++) {
1578 if (assignments[i] == rank)
1582 Tpetra::global_size_t dummy =
1583 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1584 toMap = rcp(
new map_t(dummy, mine(), base, tcomm_));
1585 delete [] assignments;
1589 toMap = rcp(
new map_t(nvtxs, base, tcomm_));
1594 importer = rcp(
new import_t(fromMap, toMap));
1595 toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1596 toMatrix->fillComplete();
1600 toMatrix = fromMatrix;
1607 typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1611 ArrayRCP<zscalar_t> weightBuf;
1612 ArrayView<const zscalar_t> *wgts =
new ArrayView<const zscalar_t> [nVwgts];
1615 size_t len = nVwgts * nvtxs;
1617 if (!buf)
throw std::bad_alloc();
1618 weightBuf = arcp(buf, 0, len,
true);
1620 for (
int widx=0; widx < nVwgts; widx++){
1621 wgts[widx] = ArrayView<const zscalar_t>(buf, nvtxs);
1622 float *vw = vwgts + widx;
1623 for (
int i=0; i < nvtxs; i++, vw += nVwgts)
1632 arrayArray_t vweights = arcp(wgts, 0, nVwgts,
true);
1634 RCP<tMVector_t> fromVertexWeights =
1635 rcp(
new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1637 RCP<tMVector_t> toVertexWeights;
1638 if (distributeInput) {
1639 toVertexWeights = rcp(
new tMVector_t(toMap, nVwgts));
1640 toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1643 toVertexWeights = fromVertexWeights;
1645 vtxWeights_ = toVertexWeights;
1650 if (haveEdges && nEwgts > 0){
1700 toMap = rcp(
new map_t(nedges, M_->getLocalNumEntries(), base, tcomm_));
1701 edgWeights_ = rcp(
new tMVector_t(toMap, nEwgts));
1703 size_t maxSize = M_->getLocalMaxNumRowEntries();
1704 tcrsMatrix_t::nonconst_local_inds_host_view_type colind(
"colind", maxSize);
1705 tcrsMatrix_t::nonconst_values_host_view_type vals(
"values", maxSize);
1708 for (
size_t i = 0, idx = 0; i < M_->getLocalNumRows(); i++) {
1709 M_->getLocalRowCopy(i, colind, vals, nEntries);
1710 for (
size_t j = 0; j < nEntries; j++) {
1711 edgWeights_->replaceLocalValue(idx, 0, vals[j]);
1723void UserInputForTests::getUIChacoCoords(FILE *fptr,
string fname)
1725 int rank = tcomm_->getRank();
1727 double *x=NULL, *y=NULL, *z=NULL;
1730 size_t globalNumVtx = M_->getGlobalNumRows();
1737 fail = chaco_input_geom(fptr,
fname.c_str(), (
int)globalNumVtx,
1744 std::cout <<
"UserInputForTests, read " << globalNumVtx;
1745 std::cout <<
" " << ndim <<
"-dimensional coordinates." << std::endl;
1749 Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1752 throw std::runtime_error(
"Can't read coordinate file");
1754 ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1759 for (
int dim=0; dim < ndim; dim++){
1762 throw std::bad_alloc();
1763 coords[dim] = arcp<const zscalar_t>(v, 0, globalNumVtx,
true);
1764 double *val = (dim==0 ? x : (dim==1 ? y : z));
1765 for (
size_t i=0; i < globalNumVtx; i++)
1771 len =
static_cast<zlno_t>(globalNumVtx);
1774 RCP<const map_t> fromMap = rcp(
new map_t(globalNumVtx, len, 0, tcomm_));
1775 RCP<const map_t> toMap = M_->getRowMap();
1776 RCP<import_t> importer = rcp(
new import_t(fromMap, toMap));
1778 Array<ArrayView<const zscalar_t> > coordData;
1779 for (
int dim=0; dim < ndim; dim++)
1780 coordData.push_back(coords[dim].view(0, len));
1782 RCP<tMVector_t> fromCoords =
1783 rcp(
new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1785 RCP<tMVector_t> toCoords = rcp(
new tMVector_t(toMap, ndim));
1787 toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
1796double UserInputForTests::chaco_read_val(
1812 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1813 if (chaco_offset >= chaco_break_pnt) {
1814 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1816 ptr = &chaco_line[chaco_save_pnt];
1817 for (i=length_left; i; i--) *ptr2++ = *ptr++;
1818 length = chaco_save_pnt + 1;
1821 length = CHACO_LINE_LENGTH;
1826 ptr2 = fgets(&chaco_line[length_left], length, infile);
1828 if (ptr2 == (
char *) NULL) {
1830 return((
double) 0.0);
1833 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
1834 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
1836 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
1837 chaco_save_pnt = chaco_break_pnt;
1842 if (chaco_line[chaco_break_pnt] !=
'\0') {
1843 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
1845 chaco_save_pnt = chaco_break_pnt + 1;
1849 else if (white_seen) {
1856 chaco_break_pnt = CHACO_LINE_LENGTH;
1862 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
1863 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
1865 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
1866 chaco_flush_line(infile);
1868 return((
double) 0.0);
1871 ptr = &(chaco_line[chaco_offset]);
1872 val = strtod(ptr, &ptr2);
1877 return((
double) 0.0);
1880 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
1887int UserInputForTests::chaco_read_int(
1903 if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1904 if (chaco_offset >= chaco_break_pnt) {
1905 length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1907 ptr = &chaco_line[chaco_save_pnt];
1908 for (i=length_left; i; i--) *ptr2++ = *ptr++;
1909 length = chaco_save_pnt + 1;
1912 length = CHACO_LINE_LENGTH;
1917 ptr2 = fgets(&chaco_line[length_left], length, infile);
1919 if (ptr2 == (
char *) NULL) {
1924 if ((chaco_line[CHACO_LINE_LENGTH - 2] !=
'\n') && (chaco_line[CHACO_LINE_LENGTH - 2] !=
'\f')
1925 && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
1927 chaco_break_pnt = CHACO_LINE_LENGTH - 1;
1928 chaco_save_pnt = chaco_break_pnt;
1933 if (chaco_line[chaco_break_pnt] !=
'\0') {
1934 if (isspace((
int)(chaco_line[chaco_break_pnt]))) {
1936 chaco_save_pnt = chaco_break_pnt + 1;
1940 else if (white_seen) {
1947 chaco_break_pnt = CHACO_LINE_LENGTH;
1953 while (isspace((
int)(chaco_line[chaco_offset])) && chaco_offset < CHACO_LINE_LENGTH) chaco_offset++;
1954 if (chaco_line[chaco_offset] ==
'%' || chaco_line[chaco_offset] ==
'#') {
1956 if (chaco_break_pnt < CHACO_LINE_LENGTH) {
1957 chaco_flush_line(infile);
1962 ptr = &(chaco_line[chaco_offset]);
1963 val = (int) strtol(ptr, &ptr2, 10);
1971 chaco_offset = (int) (ptr2 - chaco_line) /
sizeof(char);
1977void UserInputForTests::chaco_flush_line(
1984 while (c !=
'\n' && c !=
'\f')
1988int UserInputForTests::chaco_input_graph(
2038 while (end_flag == 1) {
2039 *nvtxs = chaco_read_int(fin, &end_flag);
2043 printf(
"ERROR in graph file `%s':", inname);
2044 printf(
" Invalid number of vertices (%d).\n", *nvtxs);
2049 narcs = chaco_read_int(fin, &end_flag);
2051 printf(
"ERROR in graph file `%s':", inname);
2052 printf(
" Invalid number of expected edges (%d).\n", narcs);
2059 option = chaco_read_int(fin, &end_flag);
2061 using_ewgts = option - 10 * (option / 10);
2063 using_vwgts = option - 10 * (option / 10);
2065 vtxnums = option - 10 * (option / 10);
2068 (*nVwgts) = using_vwgts;
2069 (*nEwgts) = using_ewgts;
2072 if (!end_flag && using_vwgts==1){
2073 j = chaco_read_int(fin, &end_flag);
2074 if (!end_flag) (*nVwgts) = j;
2076 if (!end_flag && using_ewgts==1){
2077 j = chaco_read_int(fin, &end_flag);
2078 if (!end_flag) (*nEwgts) = j;
2083 j = chaco_read_int(fin, &end_flag);
2086 *start = (
int *) malloc((
unsigned) (*nvtxs + 1) *
sizeof(
int));
2088 *adjacency = (
int *) malloc((
unsigned) (2 * narcs + 1) *
sizeof(
int));
2093 *vweights = (
float *) malloc((
unsigned) (*nvtxs) * (*nVwgts) *
sizeof(float));
2098 *eweights = (
float *)
2099 malloc((
unsigned) (2 * narcs + 1) * (*nEwgts) *
sizeof(float));
2103 adjptr = *adjacency;
2112 while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2117 j = chaco_read_int(fin, &end_flag);
2119 if (vertex == *nvtxs)
2121 printf(
"ERROR in graph file `%s':", inname);
2122 printf(
" no vertex number in line %d.\n", line_num);
2126 if (j != vertex && j != vertex + 1) {
2127 printf(
"ERROR in graph file `%s':", inname);
2128 printf(
" out-of-order vertex number in line %d.\n", line_num);
2142 if (vertex > *nvtxs)
2146 if (using_vwgts && new_vertex) {
2147 for (j=0; j<(*nVwgts); j++){
2148 weight = chaco_read_val(fin, &end_flag);
2150 printf(
"ERROR in graph file `%s':", inname);
2151 printf(
" not enough weights for vertex %d.\n", vertex);
2155 (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2162 neighbor = chaco_read_int(fin, &end_flag);
2168 for (j=0; j<(*nEwgts); j++){
2169 eweight = chaco_read_val(fin, &end_flag);
2172 printf(
"ERROR in graph file `%s':", inname);
2173 printf(
" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2186 if (++nedges > 2*narcs) {
2187 printf(
"ERROR in graph file `%s':", inname);
2188 printf(
" at least %d adjacencies entered, but nedges = %d\n",
2193 *adjptr++ = neighbor;
2198 neighbor = chaco_read_int(fin, &end_flag);
2202 (*start)[vertex] = sum_edges;
2207 while (!flag && end_flag != -1) {
2208 chaco_read_int(fin, &end_flag);
2213 (*start)[*nvtxs] = sum_edges;
2221 if (*adjacency != NULL)
2224 if (*eweights != NULL)
2233 if (*adjacency != NULL)
2235 if (*vweights != NULL)
2237 if (*eweights != NULL)
2245 return (error_flag);
2249int UserInputForTests::chaco_input_geom(
2251 const char *geomname,
2259 double xc, yc, zc =0;
2267 *x = *y = *z = NULL;
2270 while (end_flag == 1) {
2271 xc = chaco_read_val(fingeom, &end_flag);
2275 if (end_flag == -1) {
2276 printf(
"No values found in geometry file `%s'\n", geomname);
2282 yc = chaco_read_val(fingeom, &end_flag);
2283 if (end_flag == 0) {
2285 zc = chaco_read_val(fingeom, &end_flag);
2286 if (end_flag == 0) {
2288 chaco_read_val(fingeom, &end_flag);
2290 printf(
"Too many values on input line of geometry file `%s'\n",
2293 printf(
" Maximum dimensionality is 3\n");
2302 *x = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2305 *y = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2309 *z = (
double *) malloc((
unsigned) nvtxs *
sizeof(double));
2313 for (nread = 1; nread < nvtxs; nread++) {
2316 i = fscanf(fingeom,
"%lf", &((*x)[nread]));
2318 else if (ndims == 2) {
2319 i = fscanf(fingeom,
"%lf%lf", &((*x)[nread]), &((*y)[nread]));
2321 else if (ndims == 3) {
2322 i = fscanf(fingeom,
"%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2327 printf(
"Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2332 else if (i != ndims) {
2333 printf(
"Wrong number of values in line %d of geometry file `%s'\n",
2334 line_num, geomname);
2343 while (!flag && end_flag != -1) {
2344 chaco_read_val(fingeom, &end_flag);
2356int UserInputForTests::chaco_input_assign(
2358 const char *inassignname,
2369 while (end_flag == 1) {
2370 assignment[0] = chaco_read_int(finassign, &end_flag);
2373 if (assignment[0] < 0) {
2374 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2375 1, inassignname, assignment[0]);
2380 if (end_flag == -1) {
2381 printf(
"ERROR: No values found in assignment file `%s'\n", inassignname);
2387 int np = this->tcomm_->getSize();
2388 if (assignment[0] >= np) flag = assignment[0];
2389 srand(this->tcomm_->getRank());
2390 for (i = 1; i < nvtxs; i++) {
2391 j = fscanf(finassign,
"%hd", &(assignment[i]));
2393 printf(
"ERROR: Too few values in assignment file `%s'.\n", inassignname);
2397 if (assignment[i] < 0) {
2398 printf(
"ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2399 i+1, inassignname, assignment[i]);
2403 if (assignment[i] >= np) {
2405 if (assignment[i] > flag)
2406 flag = assignment[i];
2407 assignment[i] = rand() % np;
2413 printf(
"WARNING: Possible error in assignment file `%s'\n",
2415 printf(
" Max assignment set (%d) greater than "
2416 "max processor rank (%d)\n", flag, np-1);
2417 printf(
" Some vertices given random initial assignments\n");
2423 while (!flag && end_flag != -1) {
2424 chaco_read_int(finassign, &end_flag);
2429 printf(
"WARNING: Possible error in assignment file `%s'\n", inassignname);
2430 printf(
" Numerical data found after expected end of file\n");
2438void UserInputForTests::readPamgenMeshFile(
string path,
string testData)
2440#ifdef HAVE_ZOLTAN2_PAMGEN
2441 int rank = this->tcomm_->getRank();
2442 if (verbose_ && tcomm_->getRank() == 0)
2443 std::cout <<
"UserInputForTestsBD::readPamgenFile, Read: " << testData << std::endl;
2450 std::ostringstream meshFileName;
2451 meshFileName << path <<
"/" << testData <<
".pmgen";
2454 file.open(meshFileName.str(), std::ios::in);
2458 if(verbose_ && tcomm_->getRank() == 0)
2460 std::cout <<
"Unable to open pamgen mesh: ";
2461 std::cout << meshFileName.str();
2462 std::cout <<
"\nPlease check file path and name." << std::endl;
2468 file.seekg (0,file.end);
2475 while(std::getline(file,line))
2477 if( line.find(
"nz") != std::string::npos ||
2478 line.find(
"nphi") != std::string::npos)
2486 file.seekg(0, std::ios::beg);
2491 this->tcomm_->broadcast(0,
sizeof(
int), (
char *)&dimension);
2492 this->tcomm_->broadcast(0,
sizeof(
size_t),(
char *)&len);
2493 this->tcomm_->barrier();
2496 if(verbose_ && tcomm_->getRank() == 0)
2497 std::cout <<
"Pamgen Mesh file size == 0, exiting UserInputForTests early." << std::endl;
2501 char * file_data =
new char[len+1];
2502 file_data[len] =
'\0';
2504 file.read(file_data,len);
2508 this->tcomm_->broadcast(0,(
int)len+1,file_data);
2509 this->tcomm_->barrier();
2513 this->pamgen_mesh = rcp(
new PamgenMesh);
2514 this->havePamgenMesh =
true;
2515 pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2518 pamgen_mesh->storeMesh();
2519 this->tcomm_->barrier();
2522 this->setPamgenCoordinateMV();
2525 this->setPamgenAdjacencyGraph();
2527 this->tcomm_->barrier();
2528 if(rank == 0) file.close();
2529 delete [] file_data;
2531 throw std::runtime_error(
"Pamgen requested but Trilinos "
2532 "not built with Pamgen");
2536#ifdef HAVE_ZOLTAN2_PAMGEN
2537void UserInputForTests::setPamgenCoordinateMV()
2539 int dimension = pamgen_mesh->num_dim;
2543 zgno_t numelements = pamgen_mesh->num_elem;
2544 zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2547 for(
int i = 0; i < dimension; ++i){
2548 elem_coords[i] =
new zscalar_t[numelements];
2549 double *tmp = &pamgen_mesh->element_coord[i*numelements];
2550 for (
int j = 0; j < numelements; j++) elem_coords[i][j] = tmp[j];
2554 RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2558 Array<zgno_t> elementList(numelements);
2559 for (Array<zgno_t>::size_type k = 0; k < numelements; ++k) {
2560 elementList[k] = pamgen_mesh->element_order_map[k];
2563 mp = rcp (
new map_t (numGlobalElements, elementList, 0, this->tcomm_));
2567 Teuchos::Array<Teuchos::ArrayView<const zscalar_t> > coordView(dimension);
2568 for (
int i = 0; i < dimension; i++){
2569 if(numelements > 0){
2570 Teuchos::ArrayView<const zscalar_t> a(elem_coords[i], numelements);
2574 Teuchos::ArrayView<const zscalar_t> a;
2580 xyz_ = RCP<tMVector_t>(
new
2585void UserInputForTests::setPamgenAdjacencyGraph()
2595 size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2596 size_t local_els = (size_t)this->pamgen_mesh->num_elem;
2598 size_t global_els = (size_t)this->pamgen_mesh->num_elems_global;
2599 size_t global_nodes = (size_t)this->pamgen_mesh->num_nodes_global;
2603 RCP<const map_t> rowMap = rcp(
new map_t(global_els,0,this->tcomm_));
2604 RCP<const map_t> rangeMap = rowMap;
2607 RCP<const map_t> domainMap = rcp(
new map_t(global_nodes,0,this->tcomm_));
2610 int blks = this->pamgen_mesh->num_elem_blk;
2611 int max_nodes_per_el = 0;
2612 for(
int i = 0; i < blks; i++)
2613 if (this->pamgen_mesh->nodes_per_element[i] > max_nodes_per_el)
2614 max_nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2617 Teuchos::RCP<tcrsMatrix_t> C = rcp(
new tcrsMatrix_t(rowMap,max_nodes_per_el));
2619 Array<zgno_t> g_el_ids(local_els);
2620 for (
size_t k = 0; k < local_els; ++k) {
2621 g_el_ids[k] = pamgen_mesh->global_element_numbers[k]-1;
2624 Array<zgno_t> g_node_ids(local_nodes);
2625 for (
size_t k = 0; k < local_nodes; ++k) {
2626 g_node_ids[k] = pamgen_mesh->global_node_numbers[k]-1;
2633 for(
int i = 0; i < blks; i++)
2635 int el_per_block = this->pamgen_mesh->elements[i];
2636 int nodes_per_el = this->pamgen_mesh->nodes_per_element[i];
2637 int * connect = this->pamgen_mesh->elmt_node_linkage[i];
2639 for(
int j = 0; j < el_per_block; j++)
2641 const zgno_t gid =
static_cast<zgno_t>(g_el_ids[el_no]);
2642 for(
int k = 0; k < nodes_per_el; k++)
2644 int g_node_i = g_node_ids[connect[j*nodes_per_el+k]-1];
2645 C->insertGlobalValues(gid,
2646 Teuchos::tuple<zgno_t>(g_node_i),
2647 Teuchos::tuple<zscalar_t>(one));
2653 C->fillComplete(domainMap, rangeMap);
2659 Tpetra::MatrixMatrix::Multiply(*C,
false, *C,
true, *A);
2664 this->M_ = rcp(
new tcrsMatrix_t(rowMap, A->getGlobalMaxNumRowEntries()));
2667 Teuchos::ArrayView<const zgno_t> rowMapElementList =
2668 rowMap->getLocalElementList();
2669 for (Teuchos_Ordinal ii = 0; ii < rowMapElementList.size(); ii++)
2671 zgno_t gid = rowMapElementList[ii];
2672 size_t numEntriesInRow = A->getNumEntriesInGlobalRow (gid);
2673 typename tcrsMatrix_t::nonconst_global_inds_host_view_type rowinds(
"Indices", numEntriesInRow);
2674 typename tcrsMatrix_t::nonconst_values_host_view_type rowvals(
"Values", numEntriesInRow);
2677 Array<zscalar_t> mod_rowvals;
2678 Array<zgno_t> mod_rowinds;
2679 A->getGlobalRowCopy (gid, rowinds, rowvals, numEntriesInRow);
2680 for (
size_t i = 0; i < numEntriesInRow; i++) {
2683 if (rowvals[i] >= 1)
2685 mod_rowvals.push_back(one);
2686 mod_rowinds.push_back(rowinds[i]);
2689 this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2692 this->M_->fillComplete();
#define TEST_FAIL_AND_THROW(comm, ok, s)
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
common code used by tests
Tpetra::Map ::local_ordinal_type zlno_t
Tpetra::Map ::global_ordinal_type zgno_t
keep typedefs that commonly appear in many places localized
Traits of Xpetra classes, including migration method.
int getCoordinateDimension()
lno_t getNumLocalCoords()
void getLocalWeightsCopy(scalar_t **w)
gno_t getNumGlobalCoords()
void getLocalCoordinatesCopy(scalar_t **c)
static const std::string fail
GeometricGen::GeometricGenerator< zscalar_t, zlno_t, zgno_t, znode_t > geometricgen_t
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Tpetra::Vector< zscalar_t, zlno_t, zgno_t, znode_t > tVector_t
static RCP< User > convertToXpetra(const RCP< User > &a)
Convert the object to its Xpetra wrapped version.
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t