Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
UserInputForTests.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #ifndef USERINPUTFORTESTS
15 #define USERINPUTFORTESTS
16 
17 #include "Zoltan2_TestHelpers.hpp"
18 #include <Zoltan2_XpetraTraits.hpp>
19 #include <Zoltan2_Typedefs.hpp>
20 
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>
27 
28 #include <MatrixMarket_Tpetra.hpp>
29 
30 #ifdef HAVE_ZOLTAN2_GALERI
31 #include <Galeri_XpetraProblemFactory.hpp>
32 #include <Galeri_XpetraParameters.hpp>
33 #endif
34 
35 #include <Tpetra_KokkosCompat_DefaultNode.hpp>
36 
37 #include "GeometricGenerator.hpp"
38 #include <fstream>
39 #include <string>
40 
41 #include <TpetraExt_MatrixMatrix_def.hpp>
42 
43 // pamgen required includes
45 
46 
47 using Teuchos::RCP;
48 using Teuchos::ArrayRCP;
49 using Teuchos::ArrayView;
50 using Teuchos::Array;
51 using Teuchos::Comm;
52 using Teuchos::rcp;
53 using Teuchos::arcp;
54 using Teuchos::rcp_const_cast;
55 using Teuchos::ParameterList;
56 using namespace Zoltan2_TestingFramework;
57 
89 
91 {
92 public:
93 
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;
97  typedef map_t::node_type default_znode_t;
98 
99 
119  UserInputForTests(string path, string testData,
120  const RCP<const Comm<int> > &c, bool debugInfo=false,
121  bool distributeInput=true);
122 
139  UserInputForTests(int x, int y, int z, string matrixType,
140  const RCP<const Comm<int> > &c, bool debugInfo=false,
141  bool distributeInput=true);
142 
158  UserInputForTests(const ParameterList &pList,
159  const RCP<const Comm<int> > &c);
160 
163  static void getUIRandomData(unsigned int seed, zlno_t length,
164  zscalar_t min, zscalar_t max, ArrayView<ArrayRCP<zscalar_t > > data);
165 
166  RCP<tMVector_t> getUICoordinates();
167 
168  RCP<tMVector_t> getUIWeights();
169 
170  RCP<tMVector_t> getUIEdgeWeights();
171 
172  RCP<tcrsMatrix_t> getUITpetraCrsMatrix();
173 
174  RCP<tcrsGraph_t> getUITpetraCrsGraph();
175 
176  RCP<tVector_t> getUITpetraVector();
177 
178  RCP<tMVector_t> getUITpetraMultiVector(int nvec);
179 
180  RCP<xcrsMatrix_t> getUIXpetraCrsMatrix();
181 
182  RCP<xcrsGraph_t> getUIXpetraCrsGraph();
183 
184  RCP<xVector_t> getUIXpetraVector();
185 
186  RCP<xMVector_t> getUIXpetraMultiVector(int nvec);
187 
188 #ifdef HAVE_ZOLTAN2_PAMGEN
189  PamgenMesh * getPamGenMesh(){return this->pamgen_mesh.operator->();}
190 #endif
191 
192 #ifdef HAVE_EPETRA_DATA_TYPES
193  RCP<Epetra_CrsGraph> getUIEpetraCrsGraph();
194 
195  RCP<Epetra_CrsMatrix> getUIEpetraCrsMatrix();
196 
197  RCP<Epetra_Vector> getUIEpetraVector();
198 
199  RCP<Epetra_MultiVector> getUIEpetraMultiVector(int nvec);
200 #endif
201  bool hasInput();
202 
203  bool hasInputDataType(const string &input_type);
204 
205  bool hasUICoordinates();
206 
207  bool hasUIWeights();
208 
209  bool hasUIEdgeWeights();
210 
211  bool hasUITpetraCrsMatrix();
212 
213  bool hasUITpetraCrsGraph();
214 
215  bool hasUITpetraVector();
216 
217  bool hasUITpetraMultiVector();
218 
219  bool hasUIXpetraCrsMatrix();
220 
221  bool hasUIXpetraCrsGraph();
222 
223  bool hasUIXpetraVector();
224 
225  bool hasUIXpetraMultiVector();
226 
227  bool hasPamgenMesh();
228 #ifdef HAVE_EPETRA_DATA_TYPES
229  bool hasUIEpetraCrsGraph();
230 
231  bool hasUIEpetraCrsMatrix();
232 
233  bool hasUIEpetraVector();
234 
235  bool hasUIEpetraMultiVector();
236 
237 #endif
238 
239 private:
240 
241  bool verbose_;
242 
243  const RCP<const Comm<int> > tcomm_;
244 
245  bool havePamgenMesh;
246 #ifdef HAVE_ZOLTAN2_PAMGEN
247  RCP<PamgenMesh> pamgen_mesh;
248 #endif
249 
250  RCP<tcrsMatrix_t> M_;
251  RCP<xcrsMatrix_t> xM_;
252 
253  RCP<tMVector_t> xyz_;
254  RCP<tMVector_t> vtxWeights_;
255  RCP<tMVector_t> edgWeights_;
256 
257 #ifdef HAVE_EPETRA_DATA_TYPES
258  RCP<const Epetra_Comm> ecomm_;
259  RCP<Epetra_CrsMatrix> eM_;
260  RCP<Epetra_CrsGraph> eG_;
261 #endif
262 
263  // Read a Matrix Market file into M_
264  // using Tpetra::MatrixMarket::Reader.
265  // If there are "Tim Davis" style coordinates
266  // that go with the file, read those into xyz_.
267 
268  void readMatrixMarketFile(string path, string testData,bool distributeInput = true);
269 
270  // Build matrix M_ from a mesh and a problem type
271  // with Galeri::Xpetra.
272 
273  void buildCrsMatrix(int xdim, int ydim, int zdim, string type,
274  bool distributeInput);
275 
276  // Read a Zoltan1 Chaco or Matrix Market file
277  // into M_. If it has geometric coordinates,
278  // read them into xyz_. If it has weights,
279  // read those into vtxWeights_ and edgWeights_.
280  void readZoltanTestData(string path, string testData,
281  bool distributeInput);
282 
283  // Modify the Maps of an input matrix to make them non-contiguous
284  RCP<tcrsMatrix_t> modifyMatrixGIDs(RCP<tcrsMatrix_t> &in);
285  inline zgno_t newID(const zgno_t id) { return id * 2 + 10001; }
286 
287  // Read Zoltan data that is in a .graph file.
288  void getUIChacoGraph(FILE *fptr, bool haveAssign, FILE *assignFile,
289  string name, bool distributeInput);
290 
291  // Read Zoltan data that is in a .coords file.
292  void getUIChacoCoords(FILE *fptr, string name);
293 
294  // Chaco reader code: This code is copied from zoltan/ch.
295  // It might benefit from a rewrite and simplification.
296 
297  // Chaco reader helper functions: copied from zoltan/ch
298  static const int CHACO_LINE_LENGTH=200;
299  char chaco_line[CHACO_LINE_LENGTH]; /* space to hold values */
300  int chaco_offset; /* offset into line for next data */
301  int chaco_break_pnt; /* place in sequence to pause */
302  int chaco_save_pnt; /* place in sequence to save */
303 
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*);
307 
308  // Chaco graph reader: copied from zoltan/ch
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);
312 
313  // Chaco coordinate reader: copied from zoltan/ch
314  int chaco_input_geom(FILE *fingeom, const char *geomname, int nvtxs,
315  int *igeom, double **x, double **y, double **z);
316 
317  // Chaco coordinate reader: copied from zoltan/ch
318  int chaco_input_assign(FILE *finassign, const char *assignname, int nvtxs,
319  short *assignments);
320 
321 
322  // Read a GeomGen.txt file into M_
323  // Read coordinates into xyz_.
324  // If iti has weights read those to vtxWeights_
325  // and edgeWeights_
326  void readGeometricGenTestData(string path, string testData);
327 
328  // Geometry Gnearatory helper function
329  void readGeoGenParams(string paramFileName,
330  ParameterList &geoparams);
331 
332  // utility methods used when reading geom gen files
333 
334  static string trim_right_copy(const string& s,
335  const string& delimiters = " \f\n\r\t\v" );
336 
337  static string trim_left_copy(const string& s,
338  const string& delimiters = " \f\n\r\t\v" );
339 
340  static string trim_copy(const string& s,
341  const string& delimiters = " \f\n\r\t\v" );
342 
343 
344  // Read a pamgen mesh
345  void readPamgenMeshFile(string path, string testData);
346 #ifdef HAVE_ZOLTAN2_PAMGEN
347  void setPamgenAdjacencyGraph();
348  void setPamgenCoordinateMV();
349 #endif
350 };
351 
352 UserInputForTests::UserInputForTests(string path, string testData,
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_(),
359 #endif
360 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
361 {
362  bool zoltan1 = false;
363  string::size_type loc = path.find("/zoltan/test/"); // Zoltan1 data
364  if (loc != string::npos)
365  zoltan1 = true;
366 
367  if (zoltan1)
368  readZoltanTestData(path, testData, distributeInput);
369  else
370  readMatrixMarketFile(path, testData);
371 
372 #ifdef HAVE_EPETRA_DATA_TYPES
373  ecomm_ = Xpetra::toEpetra(c);
374 #endif
375 }
376 
378  string matrixType,
379  const RCP<const Comm<int> > &c,
380  bool debugInfo,
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_(),
386 #endif
387 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
388 {
389  if (matrixType.size() == 0){
390  int dim = 0;
391  if (x > 0) dim++;
392  if (y > 0) dim++;
393  if (z > 0) dim++;
394  if (dim == 1)
395  matrixType = string("Laplace1D");
396  else if (dim == 2)
397  matrixType = string("Laplace2D");
398  else if (dim == 3)
399  matrixType = string("Laplace3D");
400  else
401  throw std::runtime_error("input");
402 
403  if (verbose_ && tcomm_->getRank() == 0)
404  std::cout << "UserInputForTests, Matrix type : " << matrixType << std::endl;
405  }
406 
407  buildCrsMatrix(x, y, z, matrixType, distributeInput);
408 
409 #ifdef HAVE_EPETRA_DATA_TYPES
410  ecomm_ = Xpetra::toEpetra(c);
411 #endif
412 }
413 
414 UserInputForTests::UserInputForTests(const ParameterList &pList,
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_(),
420 #endif
421 chaco_offset(0), chaco_break_pnt(CHACO_LINE_LENGTH)
422 {
423 
424  // get options
425  bool distributeInput = true, debugInfo = true;
426 
427  if(pList.isParameter("distribute input"))
428  distributeInput = pList.get<bool>("distribute input");
429 
430  if(pList.isParameter("debug"))
431  debugInfo = pList.get<bool>("debug");
432  this->verbose_ = debugInfo;
433 
434  if(pList.isParameter("input file"))
435  {
436 
437  // get input path
438  string path(".");
439  if(pList.isParameter("input path"))
440  path = pList.get<string>("input path");
441 
442  string testData = pList.get<string>("input file");
443 
444  // find out if we are working from the zoltan1 test diretory
446 
447  // find out if we are using the geometric generator
448  if(pList.isParameter("file type") && pList.get<string>("file type") == "Geometric Generator")
449  file_format = GEOMGEN;
450  else if(pList.isParameter("file type") && pList.get<string>("file type") == "Pamgen")
451  {
452  file_format = PAMGEN;
453  }
454  else if(pList.isParameter("file type") && pList.get<string>("file type") == "Chaco")
455  file_format = CHACO; // this flag calls read ZoltanTestData, which calls the chaco readers...
456 
457  // read the input file
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;
463  }
464 
465  }else if(pList.isParameter("x") || pList.isParameter("y") || pList.isParameter("z")){
466 
467  int x,y,z;
468  x = y = z = 0;
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");
472 
473  string problemType = "";
474  if(pList.isParameter("equation type")) problemType = pList.get<string>("equation type");
475 
476  if (problemType.size() == 0){
477  int dim = 0;
478  if (x > 0) dim++;
479  if (y > 0) dim++;
480  if (z > 0) dim++;
481  if (dim == 1)
482  problemType = string("Laplace1D");
483  else if (dim == 2)
484  problemType = string("Laplace2D");
485  else if (dim == 3)
486  problemType = string("Laplace3D");
487  else
488  throw std::runtime_error("input");
489 
490  if (verbose_ && tcomm_->getRank() == 0)
491  std::cout << "UserInputForTests, Matrix type : " << problemType << std::endl;
492  }
493 
494 
495  buildCrsMatrix(x, y, z, problemType, distributeInput);
496 
497  }else{
498  std::cerr << "Input file block undefined!" << std::endl;
499  }
500 
501 #ifdef HAVE_EPETRA_DATA_TYPES
502  ecomm_ = Xpetra::toEpetra(c);
503 #endif
504 
505 }
506 
507 
508 RCP<Zoltan2_TestingFramework::tMVector_t> UserInputForTests::getUICoordinates()
509 {
510  if (xyz_.is_null())
511  throw std::runtime_error("could not read coord file");
512  return xyz_;
513 }
514 
515 RCP<Zoltan2_TestingFramework::tMVector_t> UserInputForTests::getUIWeights()
516 {
517  return vtxWeights_;
518 }
519 
520 RCP<Zoltan2_TestingFramework::tMVector_t> UserInputForTests::getUIEdgeWeights()
521 {
522  return edgWeights_;
523 }
524 
525 RCP<Zoltan2_TestingFramework::tcrsMatrix_t> UserInputForTests::getUITpetraCrsMatrix()
526 {
527  if (M_.is_null())
528  throw std::runtime_error("could not read mtx file");
529  return M_;
530 }
531 
532 RCP<Zoltan2_TestingFramework::tcrsGraph_t> UserInputForTests::getUITpetraCrsGraph()
533 {
534  if (M_.is_null())
535  throw std::runtime_error("could not read mtx file");
536  return rcp_const_cast<tcrsGraph_t>(M_->getCrsGraph());
537 }
538 
539 RCP<Zoltan2_TestingFramework::tVector_t> UserInputForTests::getUITpetraVector()
540 {
541  RCP<tVector_t> V = rcp(new tVector_t(M_->getRowMap(), 1));
542  V->randomize();
543 
544  return V;
545 }
546 
547 RCP<Zoltan2_TestingFramework::tMVector_t> UserInputForTests::getUITpetraMultiVector(int nvec)
548 {
549  RCP<tMVector_t> mV = rcp(new tMVector_t(M_->getRowMap(), nvec));
550  mV->randomize();
551 
552  return mV;
553 }
554 
555 RCP<Zoltan2_TestingFramework::xcrsMatrix_t> UserInputForTests::getUIXpetraCrsMatrix()
556 {
557  if (M_.is_null())
558  throw std::runtime_error("could not read mtx file");
559  return xM_;
560 }
561 
562 RCP<Zoltan2_TestingFramework::xcrsGraph_t> UserInputForTests::getUIXpetraCrsGraph()
563 {
564  if (M_.is_null())
565  throw std::runtime_error("could not read mtx file");
566  return rcp_const_cast<xcrsGraph_t>(xM_->getCrsGraph());
567 }
568 
569 RCP<Zoltan2_TestingFramework::xVector_t> UserInputForTests::getUIXpetraVector()
570 {
572 }
573 
574 RCP<Zoltan2_TestingFramework::xMVector_t> UserInputForTests::getUIXpetraMultiVector(int nvec)
575 {
576  RCP<tMVector_t> tMV = getUITpetraMultiVector(nvec);
578 }
579 
580 #ifdef HAVE_EPETRA_DATA_TYPES
581 RCP<Epetra_CrsGraph> UserInputForTests::getUIEpetraCrsGraph()
582 {
583  if (M_.is_null())
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();
588 
589  int nElts = static_cast<int>(trowMap->getGlobalNumElements());
590  int nMyElts = static_cast<int>(trowMap->getLocalNumElements());
591  int base = 0;
592  ArrayView<const int> gids = trowMap->getLocalElementList();
593 
594  Epetra_BlockMap erowMap(nElts, nMyElts,
595  gids.getRawPtr(), 1, base, *ecomm_);
596 
597  Array<int> rowSize(nMyElts);
598  for (int i=0; i < nMyElts; i++){
599  rowSize[i] = static_cast<int>(M_->getNumEntriesInLocalRow(i));
600  }
601 
602  size_t maxRow = M_->getLocalMaxNumRowEntries();
603  Array<int> colGids(maxRow);
604 
605  typename tcrsGraph_t::local_inds_host_view_type colLid;
606  eG_ = rcp(new Epetra_CrsGraph(Copy, erowMap,
607  rowSize.getRawPtr(), true));
608 
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());
614  }
615  eG_->FillComplete();
616  return eG_;
617 }
618 
619 RCP<Epetra_CrsMatrix> UserInputForTests::getUIEpetraCrsMatrix()
620 {
621  if (M_.is_null())
622  throw std::runtime_error("could not read mtx file");
623  RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
624  eM_ = rcp(new Epetra_CrsMatrix(Copy, *egraph));
625 
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]);
639  }
640  eM_->InsertGlobalValues(rowGid, (int)rowSize, nz.data(), colGid.getRawPtr());
641  }
642  eM_->FillComplete();
643  return eM_;
644 }
645 
646 RCP<Epetra_Vector> UserInputForTests::getUIEpetraVector()
647 {
648  RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
649  RCP<Epetra_Vector> V = rcp(new Epetra_Vector(egraph->RowMap()));
650  V->Random();
651  return V;
652 }
653 
654 RCP<Epetra_MultiVector> UserInputForTests::getUIEpetraMultiVector(int nvec)
655 {
656  RCP<Epetra_CrsGraph> egraph = getUIEpetraCrsGraph();
657  RCP<Epetra_MultiVector> mV =
658  rcp(new Epetra_MultiVector(egraph->RowMap(), nvec));
659  mV->Random();
660  return mV;
661 }
662 #endif
663 
665 {
666  // find out if an input source has been loaded
667  return this->hasUICoordinates() || \
668  this->hasUITpetraCrsMatrix() || \
669  this->hasUITpetraCrsGraph() || \
670  this->hasPamgenMesh();
671 }
672 
673 bool UserInputForTests::hasInputDataType(const string &input_type)
674 {
675  if(input_type == "coordinates")
676  return this->hasUICoordinates();
677  else if(input_type == "tpetra_vector")
678  return this->hasUITpetraVector();
679  else if(input_type == "tpetra_multivector")
680  return this->hasUITpetraMultiVector();
681  else if(input_type == "tpetra_crs_graph")
682  return this->hasUITpetraCrsGraph();
683  else if(input_type == "tpetra_crs_matrix")
684  return this->hasUITpetraCrsMatrix();
685  else if(input_type == "xpetra_vector")
686  return this->hasUIXpetraVector();
687  else if(input_type == "xpetra_multivector")
688  return this->hasUIXpetraMultiVector();
689  else if(input_type == "xpetra_crs_graph")
690  return this->hasUIXpetraCrsGraph();
691  else if(input_type == "xpetra_crs_matrix")
692  return this->hasUIXpetraCrsMatrix();
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();
702 #endif
703 
704  return false;
705 }
706 
708 {
709  return xyz_.is_null() ? false : true;
710 }
711 
713 {
714  return vtxWeights_.is_null() ? false : true;
715 }
716 
718 {
719  return edgWeights_.is_null() ? false : true;
720 }
721 
723 {
724  return M_.is_null() ? false : true;
725 }
726 
728 {
729  return M_.is_null() ? false : true;
730 }
731 
733 {
734  return true;
735 }
736 
738 {
739  return true;
740 }
741 
743 {
744  return M_.is_null() ? false : true;
745 }
746 
748 {
749  return M_.is_null() ? false : true;
750 }
751 
753 {
754  return true;
755 }
756 
758 {
759  return true;
760 }
761 
763 {
764  return this->havePamgenMesh;
765 }
766 
767 #ifdef HAVE_EPETRA_DATA_TYPES
768 bool UserInputForTests::hasUIEpetraCrsGraph()
769 {
770  return M_.is_null() ? false : true;
771 }
772 
773 bool UserInputForTests::hasUIEpetraCrsMatrix()
774 {
775  return hasUIEpetraCrsGraph();
776 }
777 
778 bool UserInputForTests::hasUIEpetraVector()
779 {
780  return hasUIEpetraCrsGraph();
781 }
782 
783 bool UserInputForTests::hasUIEpetraMultiVector()
784 {
785  return hasUIEpetraCrsGraph();
786 }
787 #endif
788 
789 void UserInputForTests::getUIRandomData(unsigned int seed, zlno_t length,
790  zscalar_t min, zscalar_t max,
791  ArrayView<ArrayRCP<zscalar_t > > data)
792 {
793  if (length < 1)
794  return;
795 
796  size_t dim = data.size();
797  for (size_t i=0; i < dim; i++){
798  zscalar_t *tmp = new zscalar_t [length];
799  if (!tmp)
800  throw (std::bad_alloc());
801  data[i] = Teuchos::arcp(tmp, 0, length, true);
802  }
803 
804  zscalar_t scalingFactor = (max-min) / RAND_MAX;
805  srand(seed);
806  for (size_t i=0; i < dim; i++){
807  zscalar_t *x = data[i].getRawPtr();
808  for (zlno_t j=0; j < length; j++)
809  *x++ = min + (zscalar_t(rand()) * scalingFactor);
810  }
811 }
812 
813 // utility methods used when reading geom gen files
814 
815 string UserInputForTests::trim_right_copy(
816  const string& s,
817  const string& delimiters)
818 {
819  return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
820 }
821 
822 string UserInputForTests::trim_left_copy(
823  const string& s,
824  const string& delimiters)
825 {
826  return s.substr( s.find_first_not_of( delimiters ) );
827 }
828 
829 string UserInputForTests::trim_copy(
830  const string& s,
831  const string& delimiters)
832 {
833  return trim_left_copy( trim_right_copy( s, delimiters ), delimiters );
834 }
835 
836 void UserInputForTests::readGeometricGenTestData(string path,
837  string testData)
838 {
839 
840  std::ostringstream fname;
841  fname << path << "/" << testData << ".txt";
842 
843  if (verbose_ && tcomm_->getRank() == 0)
844  std::cout << "UserInputForTests, Read: " << fname.str() << std::endl;
845 
846  Teuchos::ParameterList geoparams("geo params");
847  readGeoGenParams(fname.str(),geoparams);
848 
849  geometricgen_t * gg = new geometricgen_t(geoparams, this->tcomm_);
850 
851  // get coordinate and point info
852  int coord_dim = gg->getCoordinateDimension();
853  int numWeightsPerCoord = gg->getNumWeights();
854  zlno_t numLocalPoints = gg->getNumLocalCoords();
855  zgno_t numGlobalPoints = gg->getNumGlobalCoords();
856 
857  // allocate an array of coordinate arrays
858  zscalar_t **coords = new zscalar_t * [coord_dim];
859  for(int i = 0; i < coord_dim; ++i){
860  coords[i] = new zscalar_t[numLocalPoints];
861  }
862 
863  // get a copy of the data
864  gg->getLocalCoordinatesCopy(coords);
865 
866  // get an array of arrays of weight data (if any)
867  zscalar_t **weight = NULL;
868  if (numWeightsPerCoord) {
869  // memory allocation
870  weight = new zscalar_t * [numWeightsPerCoord];
871  for(int i = 0; i < numWeightsPerCoord; ++i){
872  weight[i] = new zscalar_t[numLocalPoints];
873  }
874 
875  // get a copy of the weight data
876  gg->getLocalWeightsCopy(weight);
877  }
878 
879  delete gg; // free up memory from geom gen
880 
881 
882  // make a Tpetra map
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_));
885 
886  // make an array of array views containing the coordinate data
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);
891  coordView[i] = a;
892  }
893  else {
894  Teuchos::ArrayView<const zscalar_t> a;
895  coordView[i] = a;
896  }
897  }
898 
899  // set the xyz_ multivector
900  xyz_ = RCP<tMVector_t>(new
901  tMVector_t(mp, coordView.view(0, coord_dim),
902  coord_dim));
903 
904  // set the vtx weights
905  if (numWeightsPerCoord) {
906  // make an array of array views containing the weight data
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);
911  weightView[i] = a;
912  }
913  else {
914  Teuchos::ArrayView<const zscalar_t> a;
915  weightView[i] = a;
916  }
917  }
918 
919  vtxWeights_ = RCP<tMVector_t>(new tMVector_t(mp, weightView.view(0, numWeightsPerCoord),
920  numWeightsPerCoord));
921  }
922 }
923 
924 void UserInputForTests::readGeoGenParams(string paramFileName,
925  ParameterList &geoparams){
926 
927  const char param_comment = '#';
928 
929  std::string input = "";
930  char inp[25000];
931  for(int i = 0; i < 25000; ++i){
932  inp[i] = 0;
933  }
934 
935  bool fail = false;
936  if(this->tcomm_->getRank() == 0){
937 
938  std::fstream inParam(paramFileName.c_str());
939  if (inParam.fail())
940  {
941  fail = true;
942  }
943  if(!fail)
944  {
945  std::string tmp = "";
946  getline (inParam,tmp);
947  while (!inParam.eof()){
948  if(tmp != ""){
949  tmp = trim_copy(tmp);
950  if(tmp != ""){
951  input += tmp + "\n";
952  }
953  }
954  getline (inParam,tmp);
955  }
956  inParam.close();
957  for (size_t i = 0; i < input.size(); ++i){
958  inp[i] = input[i];
959  }
960  }
961  }
962 
963 
964 
965  int size = (int)input.size();
966  if(fail){
967  size = -1;
968  }
969  this->tcomm_->broadcast(0, sizeof(int), (char*) &size);
970  if(size == -1){
971  throw "File " + paramFileName + " cannot be opened.";
972  }
973  this->tcomm_->broadcast(0, size, inp);
974  std::istringstream inParam(inp);
975  string str;
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";
982  }
983  string paramname = trim_copy(str.substr(0,pos));
984  string paramvalue = trim_copy(str.substr(pos + 1));
985  geoparams.set(paramname, paramvalue);
986  }
987  getline (inParam,str);
988  }
989 }
990 
992 RCP<tcrsMatrix_t> UserInputForTests::modifyMatrixGIDs(
993  RCP<tcrsMatrix_t> &inMatrix
994 )
995 {
996  // Produce a new matrix with the same structure as inMatrix,
997  // but whose row/column GIDs are non-contiguous values.
998  // In this case GID g in inMatrix becomes g*2+1 in outMatrix.
999 
1000  // Create the map for the new matrix: same structure as inMap but with
1001  // the GIDs modified.
1002  RCP<const map_t> inMap = inMatrix->getRowMap();
1003 
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]);
1009  }
1010 
1011  Tpetra::global_size_t nGlobalRows = inMap->getGlobalNumElements();
1012  RCP<map_t> outMap = rcp(new map_t(nGlobalRows, outRows(), 0,
1013  inMap->getComm()));
1014 
1015 #ifdef INCLUDE_LENGTHY_OUTPUT
1016  // Sanity check output
1017  {
1018  std::cout << inMap->getComm()->getRank() << " KDDKDD "
1019  << "nGlobal " << inMap->getGlobalNumElements() << " "
1020  << outMap->getGlobalNumElements() << "; "
1021  << "nLocal " << inMap->getLocalNumElements() << " "
1022  << outMap->getLocalNumElements() << "; "
1023  << std::endl;
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;
1029  }
1030 #endif // INCLUDE_LENGTHY_OUTPUT
1031 
1032  // Create a new matrix using the new map
1033  // Get the length of the longest row; allocate memory.
1034  size_t rowLen = inMatrix->getLocalMaxNumRowEntries();
1035  RCP<tcrsMatrix_t> outMatrix = rcp(new tcrsMatrix_t(outMap, rowLen));
1036 
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);
1039 
1040  for (size_t i = 0; i < nRows; i++) {
1041  size_t nEntries;
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]);
1046 
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));
1052  }
1053  outMatrix->fillComplete();
1054 
1055 #ifdef INCLUDE_LENGTHY_OUTPUT
1056  // Sanity check output
1057  {
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;
1068 
1069  size_t nIn, nOut;
1070  Teuchos::Array<zgno_t> in(rowLen), out(rowLen);
1071  Teuchos::Array<zscalar_t> inval(rowLen), outval(rowLen);
1072 
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,
1077  nOut);
1078 
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] << ") ";
1083  }
1084  std::cout << std::endl;
1085  }
1086  }
1087 #endif // INCLUDE_LENGTHY_OUTPUT
1088 
1089  return outMatrix;
1090 }
1091 
1093 
1094 void UserInputForTests::readMatrixMarketFile(
1095  string path,
1096  string testData,
1097  bool distributeInput
1098 )
1099 {
1100  std::ostringstream fname;
1101  fname << path << "/" << testData << ".mtx";
1102 
1103  if (verbose_ && tcomm_->getRank() == 0)
1104  std::cout << "UserInputForTests, Read: " << fname.str() << std::endl;
1105 
1106  // FIXME (mfh 01 Aug 2016) Tpetra::MatrixMarket::Reader has a graph
1107  // ("pattern" matrix) reader. Call its readSparseGraphFile method.
1108 
1109  RCP<tcrsMatrix_t> toMatrix;
1110  RCP<tcrsMatrix_t> fromMatrix;
1111  bool aok = true;
1112  try{
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
1117  // See note below about modifying coordinate IDs as well.
1118  //if (makeNonContiguous)
1119  fromMatrix = modifyMatrixGIDs(fromMatrix);
1120 #endif
1121 
1122  if(!distributeInput)
1123  {
1124  if (verbose_ && tcomm_->getRank() == 0)
1125  std::cout << "Constructing serial distribution of matrix" << std::endl;
1126  // need to make a serial map and then import the data to redistribute it
1127  RCP<const map_t> fromMap = fromMatrix->getRowMap();
1128 
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_));
1132 
1133  RCP<import_t> importer = rcp(new import_t(fromMap, toMap));
1134  toMatrix = rcp(new tcrsMatrix_t(toMap,0));
1135  toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1136  toMatrix->fillComplete();
1137 
1138  }else{
1139  toMatrix = fromMatrix;
1140  }
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;
1146  }
1147  aok = false;
1148  }
1149  TEST_FAIL_AND_THROW(*tcomm_, aok,
1150  "UserInputForTests unable to read matrix market file");
1151 
1152  M_ = toMatrix;
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
1159 
1161 
1162  // Open the coordinate file.
1163 
1164  fname.str("");
1165  fname << path << "/" << testData << "_coord.mtx";
1166 
1167  size_t coordDim = 0, numGlobalCoords = 0;
1168  size_t msg[2]={0,0};
1169  ArrayRCP<ArrayRCP<zscalar_t> > xyz;
1170  std::ifstream coordFile;
1171 
1172  if (tcomm_->getRank() == 0){
1173 
1174  if (verbose_)
1175  std::cout << "UserInputForTests, Read: " <<
1176  fname.str() << std::endl;
1177 
1178  int fail = 0;
1179  try{
1180  coordFile.open(fname.str().c_str());
1181  }
1182  catch (std::exception &){ // there is no coordinate file
1183  fail = 1;
1184  }
1185 
1186  if (!fail){
1187 
1188  // Read past banner to number and dimension of coordinates.
1189 
1190  char c[256];
1191  bool done=false;
1192 
1193  while (!done && !fail && coordFile.good()){
1194  coordFile.getline(c, 256);
1195  if (!c[0])
1196  fail = 1;
1197  else if (c[0] == '%')
1198  continue;
1199  else {
1200  done=true;
1201  std::istringstream s(c);
1202  s >> numGlobalCoords >> coordDim;
1203  if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
1204  fail=1;
1205  }
1206  }
1207 
1208  if (done){
1209 
1210  // Read in the coordinates.
1211 
1212  xyz = Teuchos::arcp(new ArrayRCP<zscalar_t> [coordDim], 0, coordDim);
1213 
1214  for (size_t dim=0; !fail && dim < coordDim; dim++){
1215  size_t idx;
1216  zscalar_t *tmp = new zscalar_t [numGlobalCoords];
1217  if (!tmp)
1218  fail = 1;
1219  else{
1220  xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
1221 
1222  for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
1223  coordFile.getline(c, 256);
1224  std::istringstream s(c);
1225  s >> tmp[idx];
1226  }
1227 
1228  if (idx < numGlobalCoords)
1229  fail = 1;
1230  }
1231  }
1232 
1233  if (fail){
1234  ArrayRCP<zscalar_t> emptyArray;
1235  for (size_t dim=0; dim < coordDim; dim++)
1236  xyz[dim] = emptyArray; // free the memory
1237 
1238  coordDim = 0;
1239  }
1240  }
1241  else{
1242  fail = 1;
1243  }
1244 
1245  coordFile.close();
1246  }
1247 
1248  msg[0] = coordDim;
1249  msg[1] = numGlobalCoords;
1250  }
1251 
1252  // Broadcast coordinate dimension
1253  Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
1254 
1255  coordDim = msg[0];
1256  numGlobalCoords = msg[1];
1257 
1258  if (coordDim == 0)
1259  return;
1260 
1261  zgno_t base = 0;
1262  RCP<const map_t> toMap;
1263 
1264  if (!M_.is_null()){
1265  const RCP<const map_t> &mapM = M_->getRowMap();
1266  toMap = mapM;
1267  }
1268  else{
1269  if (verbose_ && tcomm_->getRank() == 0)
1270  {
1271  std::cout << "Matrix was null. ";
1272  std::cout << "Constructing distribution map for coordinate vector."
1273  << std::endl;
1274  }
1275 
1276  if(!distributeInput)
1277  {
1278  if (verbose_ && tcomm_->getRank() == 0)
1279  std::cout << "Constructing serial distribution map for coordinates."
1280  << std::endl;
1281 
1282  size_t numLocalCoords = this->tcomm_->getRank()==0 ? numGlobalCoords : 0;
1283  toMap = rcp(new map_t(numGlobalCoords,numLocalCoords, base, tcomm_));
1284  }else{
1285  toMap = rcp(new map_t(numGlobalCoords, base, tcomm_));
1286  }
1287  }
1288 
1289  // Export coordinates to their owners
1290 
1291  xyz_ = rcp(new tMVector_t(toMap, coordDim));
1292 
1293  ArrayRCP<ArrayView<const zscalar_t> > coordLists(coordDim);
1294 
1295  if (tcomm_->getRank() == 0){
1296 
1297  for (size_t dim=0; dim < coordDim; dim++)
1298  coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
1299 
1300  zgno_t *tmp = new zgno_t [numGlobalCoords];
1301  if (!tmp)
1302  throw std::bad_alloc();
1303 
1304  ArrayRCP<const zgno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
1305 
1306 #ifdef KDD_NOT_READY_YET
1307  // TODO if modifyMatrixGIDs, we need to modify ids here as well
1308  for (zgno_t id=0; id < zgno_t(numGlobalCoords); id++)
1309  *tmp++ = newID(id);
1310 #else
1311  for (zgno_t id=0; id < zgno_t(numGlobalCoords); id++)
1312  *tmp++ = id;
1313 #endif
1314 
1315  RCP<const map_t> fromMap = rcp(new map_t(numGlobalCoords,
1316  rowIds.view(0, numGlobalCoords),
1317  base, tcomm_));
1318 
1319  tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1320 
1321  export_t exporter(fromMap, toMap);
1322 
1323  xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1324  }
1325  else{
1326 
1327  RCP<const map_t> fromMap = rcp(new map_t(numGlobalCoords,
1328  ArrayView<zgno_t>(), base, tcomm_));
1329 
1330  tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
1331 
1332  export_t exporter(fromMap, toMap);
1333 
1334  xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
1335  }
1336 }
1337 
1338 void UserInputForTests::buildCrsMatrix(int xdim, int ydim, int zdim,
1339  string problemType, bool distributeInput)
1340 {
1341 #ifdef HAVE_ZOLTAN2_GALERI
1342  Teuchos::CommandLineProcessor tclp;
1343  Galeri::Xpetra::Parameters<zgno_t> params(tclp,
1344  xdim, ydim, zdim, problemType);
1345 
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(),
1349  0, tcomm_));
1350  else {
1351  // All data initially on rank 0
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,
1355  tcomm_));
1356  }
1357 
1358  if (verbose_ && tcomm_->getRank() == 0){
1359 
1360  std::cout << "Matrix is " << (distributeInput ? "" : "not");
1361  std::cout << "distributed." << std::endl;
1362 
1363  std::cout << "UserInputForTests, Create matrix with " << problemType;
1364  std::cout << " (and " << xdim;
1365  if (zdim > 0)
1366  std::cout << " x " << ydim << " x " << zdim;
1367  else if (ydim > 0)
1368  std::cout << " x" << ydim << " x 1";
1369  else
1370  std::cout << "x 1 x 1";
1371 
1372  std::cout << " mesh)" << std::endl;
1373 
1374  }
1375 
1376  bool aok = true;
1377  try{
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();
1382  }
1383  catch (std::exception &) { // Probably not enough memory
1384  aok = false;
1385  }
1386  TEST_FAIL_AND_THROW(*tcomm_, aok,
1387  "UserInputForTests Galeri::Xpetra::BuildProblem failed");
1388 
1390 
1391  // Compute the coordinates for the matrix rows.
1392 
1393  if (verbose_ && tcomm_->getRank() == 0)
1394  std::cout <<
1395  "UserInputForTests, Implied matrix row coordinates computed" <<
1396  std::endl;
1397 
1398  ArrayView<const zgno_t> gids = map->getLocalElementList();
1399  zlno_t count = static_cast<zlno_t>(gids.size());
1400  int dim = 3;
1401  size_t pos = problemType.find("2D");
1402  if (pos != string::npos)
1403  dim = 2;
1404  else if (problemType == string("Laplace1D") ||
1405  problemType == string("Identity"))
1406  dim = 1;
1407 
1408  Array<ArrayRCP<zscalar_t> > coordinates(dim);
1409 
1410  if (count > 0){
1411  for (int i=0; i < dim; i++){
1412  zscalar_t *c = new zscalar_t [count];
1413  if (!c)
1414  throw(std::bad_alloc());
1415  coordinates[i] = Teuchos::arcp(c, 0, count, true);
1416  }
1417 
1418  if (dim==3){
1419  zscalar_t *x = coordinates[0].getRawPtr();
1420  zscalar_t *y = coordinates[1].getRawPtr();
1421  zscalar_t *z = coordinates[2].getRawPtr();
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;
1426  z[i] = zscalar_t(iz);
1427  y[i] = zscalar_t(xy / xdim);
1428  x[i] = zscalar_t(xy % xdim);
1429  }
1430  }
1431  else if (dim==2){
1432  zscalar_t *x = coordinates[0].getRawPtr();
1433  zscalar_t *y = coordinates[1].getRawPtr();
1434  for (zlno_t i=0; i < count; i++){
1435  y[i] = zscalar_t(gids[i] / xdim);
1436  x[i] = zscalar_t(gids[i] % xdim);
1437  }
1438  }
1439  else{
1440  zscalar_t *x = coordinates[0].getRawPtr();
1441  for (zlno_t i=0; i < count; i++)
1442  x[i] = zscalar_t(gids[i]);
1443  }
1444  }
1445 
1446  Array<ArrayView<const zscalar_t> > coordView(dim);
1447  if (count > 0)
1448  for (int i=0; i < dim; i++)
1449  coordView[i] = coordinates[i].view(0,count);
1450 
1451  xyz_ = rcp(new tMVector_t(map, coordView.view(0, dim), dim));
1452 #else
1453  throw std::runtime_error("Galeri input requested but Trilinos is "
1454  "not built with Galeri.");
1455 #endif
1456 }
1457 
1458 void UserInputForTests::readZoltanTestData(string path, string testData,
1459  bool distributeInput)
1460 {
1461  int rank = tcomm_->getRank();
1462  FILE *graphFile = NULL;
1463  FILE *coordFile = NULL;
1464  FILE *assignFile = NULL;
1465  int fileInfo[3];
1466 
1467  for (int i = 0; i < CHACO_LINE_LENGTH; i++) chaco_line[i] = '\0';
1468 
1469  if (rank == 0){
1470  // set chacho graph file name
1471  std::ostringstream chGraphFileName;
1472  chGraphFileName << path << "/" << testData << ".graph";
1473 
1474  // set chaco graph
1475  std::ostringstream chCoordFileName;
1476  chCoordFileName << path << "/" << testData << ".coords";
1477 
1478  // set chaco graph
1479  std::ostringstream chAssignFileName;
1480  chAssignFileName << path << "/" << testData << ".assign";
1481 
1482  // open file
1483  graphFile = fopen(chGraphFileName.str().c_str(), "r");
1484 
1485  if(!graphFile) // maybe the user is using the default zoltan1 path convention
1486  {
1487  chGraphFileName.str("");
1488  chCoordFileName.str("");
1489  // try constructing zoltan1 paths
1490  chGraphFileName << path << "/ch_" << testData << "/" << testData << ".graph";
1491  chCoordFileName << path << "/ch_" << testData << "/" << testData << ".coords";
1492  chAssignFileName << path << "/ch_" << testData << "/" << testData << ".assign";
1493  // try to open the graph file again, if this doesn't open
1494  // the user has not provided a valid path to the file
1495  graphFile = fopen(chGraphFileName.str().c_str(), "r");
1496  }
1497 
1498  memset(fileInfo, 0, sizeof(int) * 3); // set fileinfo to 0's
1499  if (graphFile){
1500  fileInfo[0] = 1;
1501  if (verbose_ && tcomm_->getRank() == 0)
1502  std::cout << "UserInputForTests, open " <<
1503  chGraphFileName.str () << std::endl;
1504 
1505  coordFile = fopen(chCoordFileName.str().c_str(), "r");
1506  if (coordFile){
1507  fileInfo[1] = 1;
1508  if (verbose_ && tcomm_->getRank() == 0)
1509  std::cout << "UserInputForTests, open " <<
1510  chCoordFileName.str () << std::endl;
1511  }
1512 
1513  assignFile = fopen(chAssignFileName.str().c_str(), "r");
1514  if (assignFile){
1515  fileInfo[2] = 1;
1516  if (verbose_ && tcomm_->getRank() == 0)
1517  std::cout << "UserInputForTests, open " <<
1518  chAssignFileName.str () << std::endl;
1519  }
1520  }else{
1521  if (verbose_ && tcomm_->getRank() == 0){
1522  std::cout << "UserInputForTests, unable to open file: ";
1523  std::cout << chGraphFileName.str() << std::endl;
1524  }
1525  }
1526  }
1527 
1528  // broadcast whether we have graphs and coords to all processes
1529  Teuchos::broadcast<int, int>(*tcomm_, 0, 3, fileInfo);
1530 
1531  bool haveGraph = (fileInfo[0] == 1);
1532  bool haveCoords = (fileInfo[1] == 1);
1533  bool haveAssign = (fileInfo[2] == 1);
1534 
1535  if (haveGraph){
1536  // builds M_, vtxWeights_, and edgWeights_ and closes file.
1537  try{
1538  getUIChacoGraph(graphFile, haveAssign, assignFile,
1539  testData, distributeInput);
1540  }
1542 
1543  if (haveCoords){
1544  // builds xyz_ and closes the file.
1545  try{
1546  getUIChacoCoords(coordFile, testData);
1547  }
1549  }
1550  }
1551 
1553 }
1554 
1555 void UserInputForTests::getUIChacoGraph(FILE *fptr, bool haveAssign,
1556  FILE *assignFile, string fname,
1557  bool distributeInput)
1558 {
1559  int rank = tcomm_->getRank();
1560  int graphCounts[5];
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;
1567  zgno_t base = 0;
1568  ArrayRCP<const size_t> rowSizes;
1569  int fail = 0;
1570  bool haveEdges = true;
1571 
1572  if (rank == 0){
1573 
1574  memset(graphCounts, 0, 5*sizeof(int));
1575 
1576  // Reads in the file and closes it when done.
1577  fail = chaco_input_graph(fptr, fname.c_str(), &start, &adj,
1578  &nvtxs, &nVwgts, &vwgts, &nEwgts, &ewgts);
1579 
1580  // There are Zoltan2 test graphs that have no edges.
1581 
1582  // nEwgts must be 1 or 0 - add error
1583 
1584  if (start == NULL)
1585  haveEdges = false;
1586 
1587  if (verbose_)
1588  {
1589  std::cout << "UserInputForTests, " << nvtxs << " vertices,";
1590  if (haveEdges)
1591  std::cout << start[nvtxs] << " edges,";
1592  else
1593  std::cout << "no edges,";
1594  std::cout << nVwgts << " vertex weights, ";
1595  std::cout << nEwgts << " edge weights" << std::endl;
1596  }
1597 
1598  if (nvtxs==0)
1599  fail = true;
1600 
1601  if (fail){
1602  Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1603  throw std::runtime_error("Unable to read chaco file");
1604  }
1605 
1606  if (haveEdges)
1607  nedges = start[nvtxs];
1608 
1609  nzPerRow = new size_t [nvtxs];
1610  if (!nzPerRow)
1611  throw std::bad_alloc();
1612  rowSizes = arcp(nzPerRow, 0, nvtxs, true);
1613 
1614  if (haveEdges){
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];
1619  }
1620  }
1621  else{
1622  memset(nzPerRow, 0, sizeof(size_t) * nvtxs);
1623  }
1624 
1625  // Make sure base gid is zero.
1626 
1627  if (nedges){
1628  int chbase = 1; // chaco input files are one-based
1629  for (int i=0; i < nedges; i++)
1630  adj[i] -= chbase;
1631  }
1632 
1633  graphCounts[0] = nvtxs;
1634  graphCounts[1] = nedges;
1635  graphCounts[2] = nVwgts;
1636  graphCounts[3] = nEwgts;
1637  graphCounts[4] = (int)maxRowLen; // size_t maxRowLen will fit; it is <= (int-int)
1638  }
1639 
1640  Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
1641 
1642  if (graphCounts[0] == 0)
1643  throw std::runtime_error("Unable to read chaco file");
1644 
1645  haveEdges = (graphCounts[1] > 0);
1646 
1647  RCP<tcrsMatrix_t> fromMatrix;
1648  RCP<const map_t> fromMap;
1649 
1650  // Create a Tpetra::CrsMatrix where rank 0 has entire matrix.
1651  if (rank == 0){
1652  fromMap = rcp(new map_t(nvtxs, nvtxs, base, tcomm_));
1653 
1654  fromMatrix =
1655  rcp(new tcrsMatrix_t(fromMap, rowSizes()));
1656 
1657  if (haveEdges){
1658 
1659  zgno_t *edgeIds = new zgno_t [nedges];
1660  if (nedges && !edgeIds)
1661  throw std::bad_alloc();
1662  for (int i=0; i < nedges; i++)
1663  edgeIds[i] = adj[i];
1664 
1665  free(adj);
1666  adj = NULL;
1667 
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];
1673  free(ewgts);
1674  ewgts = NULL;
1675  }
1676 
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];
1682  }
1683  }
1684 
1685  delete [] edgeIds;
1686  edgeIds = NULL;
1687  }
1688 
1689  fromMatrix->fillComplete();
1690  }
1691  else{
1692  nvtxs = graphCounts[0];
1693  nedges = graphCounts[1];
1694  nVwgts = graphCounts[2];
1695  nEwgts = graphCounts[3];
1696  maxRowLen = graphCounts[4];
1697 
1698  // Create a Tpetra::CrsMatrix where rank 0 has entire matrix.
1699 
1700  fromMap = rcp(new map_t(nvtxs, 0, base, tcomm_));
1701 
1702  fromMatrix =
1703  rcp(new tcrsMatrix_t(fromMap, rowSizes()));
1704 
1705  fromMatrix->fillComplete();
1706  }
1707 
1708 #ifdef KDDKDDPRINT
1709  if (rank == 0) {
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);
1715  size_t num;
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;
1721  }
1722  }
1723 #endif
1724 
1725  RCP<const map_t> toMap;
1726  RCP<tcrsMatrix_t> toMatrix;
1727  RCP<import_t> importer;
1728 
1729  if (distributeInput) {
1730  if (haveAssign) {
1731  // Read assignments from Chaco assignment file
1732  short *assignments = new short[nvtxs];
1733  if (rank == 0) {
1734  fail = chaco_input_assign(assignFile, fname.c_str(), nvtxs, assignments);
1735  }
1736  // Broadcast coordinate dimension
1737  Teuchos::broadcast<int, short>(*tcomm_, 0, nvtxs, assignments);
1738 
1739  // Build map with my vertices
1740  Teuchos::Array<zgno_t> mine;
1741  for (int i = 0; i < nvtxs; i++) {
1742  if (assignments[i] == rank)
1743  mine.push_back(i);
1744  }
1745 
1746  Tpetra::global_size_t dummy =
1747  Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1748  toMap = rcp(new map_t(dummy, mine(), base, tcomm_));
1749  delete [] assignments;
1750  }
1751  else {
1752  // Create a Tpetra::Map with default row distribution.
1753  toMap = rcp(new map_t(nvtxs, base, tcomm_));
1754  }
1755  toMatrix = rcp(new tcrsMatrix_t(toMap, maxRowLen));
1756 
1757  // Import the data.
1758  importer = rcp(new import_t(fromMap, toMap));
1759  toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1760  toMatrix->fillComplete();
1761  }
1762  else {
1763  toMap = fromMap;
1764  toMatrix = fromMatrix;
1765  }
1766 
1767  M_ = toMatrix;
1768 
1769  // Vertex weights, if any
1770 
1771  typedef ArrayRCP<const ArrayView<const zscalar_t> > arrayArray_t;
1772 
1773  if (nVwgts > 0){
1774 
1775  ArrayRCP<zscalar_t> weightBuf;
1776  ArrayView<const zscalar_t> *wgts = new ArrayView<const zscalar_t> [nVwgts];
1777 
1778  if (rank == 0){
1779  size_t len = nVwgts * nvtxs;
1780  zscalar_t *buf = new zscalar_t [len];
1781  if (!buf) throw std::bad_alloc();
1782  weightBuf = arcp(buf, 0, len, true);
1783 
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)
1788  buf[i] = *vw;
1789  buf += nvtxs;
1790  }
1791 
1792  free(vwgts);
1793  vwgts = NULL;
1794  }
1795 
1796  arrayArray_t vweights = arcp(wgts, 0, nVwgts, true);
1797 
1798  RCP<tMVector_t> fromVertexWeights =
1799  rcp(new tMVector_t(fromMap, vweights.view(0, nVwgts), nVwgts));
1800 
1801  RCP<tMVector_t> toVertexWeights;
1802  if (distributeInput) {
1803  toVertexWeights = rcp(new tMVector_t(toMap, nVwgts));
1804  toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1805  }
1806  else
1807  toVertexWeights = fromVertexWeights;
1808 
1809  vtxWeights_ = toVertexWeights;
1810  }
1811 
1812  // Edge weights, if any
1813 
1814  if (haveEdges && nEwgts > 0){
1815 
1816  // No longer distributing edge weights; they are the matrix values
1817  /*
1818  ArrayRCP<zscalar_t> weightBuf;
1819  ArrayView<const zscalar_t> *wgts = new ArrayView<const zscalar_t> [nEwgts];
1820 
1821  toMap = rcp(new map_t(nedges, M_->getLocalNumEntries(), base, tcomm_));
1822 
1823  if (rank == 0){
1824  size_t len = nEwgts * nedges;
1825  zscalar_t *buf = new zscalar_t [len];
1826  if (!buf) throw std::bad_alloc();
1827  weightBuf = arcp(buf, 0, len, true);
1828 
1829  for (int widx=0; widx < nEwgts; widx++){
1830  wgts[widx] = ArrayView<const zscalar_t>(buf, nedges);
1831  float *ew = ewgts + widx;
1832  for (int i=0; i < nedges; i++, ew += nEwgts)
1833  buf[i] = *ew;
1834  buf += nedges;
1835  }
1836 
1837  free(ewgts);
1838  ewgts = NULL;
1839  fromMap = rcp(new map_t(nedges, nedges, base, tcomm_));
1840  }
1841  else{
1842  fromMap = rcp(new map_t(nedges, 0, base, tcomm_));
1843  }
1844 
1845  arrayArray_t eweights = arcp(wgts, 0, nEwgts, true);
1846 
1847  RCP<tMVector_t> fromEdgeWeights;
1848  RCP<tMVector_t> toEdgeWeights;
1849  RCP<import_t> edgeImporter;
1850 
1851  if (distributeInput) {
1852  fromEdgeWeights =
1853  rcp(new tMVector_t(fromMap, eweights.view(0, nEwgts), nEwgts));
1854  toEdgeWeights = rcp(new tMVector_t(toMap, nEwgts));
1855  edgeImporter = rcp(new import_t(fromMap, toMap));
1856  toEdgeWeights->doImport(*fromEdgeWeights, *edgeImporter, Tpetra::INSERT);
1857  }
1858  else
1859  toEdgeWeights = fromEdgeWeights;
1860 
1861  edgWeights_ = toEdgeWeights;
1862  */
1863 
1864  toMap = rcp(new map_t(nedges, M_->getLocalNumEntries(), base, tcomm_));
1865  edgWeights_ = rcp(new tMVector_t(toMap, nEwgts));
1866 
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);
1870  size_t nEntries;
1871 
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]); // Assuming nEwgts==1
1876  idx++;
1877  }
1878  }
1879  }
1880 
1881  if (start) {
1882  free(start);
1883  start = NULL;
1884  }
1885 }
1886 
1887 void UserInputForTests::getUIChacoCoords(FILE *fptr, string fname)
1888 {
1889  int rank = tcomm_->getRank();
1890  int ndim=0;
1891  double *x=NULL, *y=NULL, *z=NULL;
1892  int fail = 0;
1893 
1894  size_t globalNumVtx = M_->getGlobalNumRows();
1895 
1896  if (rank == 0){
1897 
1898  // This function is from the Zoltan C-library.
1899 
1900  // Reads in the file and closes it when done.
1901  fail = chaco_input_geom(fptr, fname.c_str(), (int)globalNumVtx,
1902  &ndim, &x, &y, &z);
1903 
1904  if (fail)
1905  ndim = 0;
1906 
1907  if (verbose_){
1908  std::cout << "UserInputForTests, read " << globalNumVtx;
1909  std::cout << " " << ndim << "-dimensional coordinates." << std::endl;
1910  }
1911  }
1912 
1913  Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1914 
1915  if (ndim == 0)
1916  throw std::runtime_error("Can't read coordinate file");
1917 
1918  ArrayRCP<ArrayRCP<const zscalar_t> > coords(ndim);
1919  zlno_t len = 0;
1920 
1921  if (rank == 0){
1922 
1923  for (int dim=0; dim < ndim; dim++){
1924  zscalar_t *v = new zscalar_t [globalNumVtx];
1925  if (!v)
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++)
1930  v[i] = zscalar_t(val[i]);
1931 
1932  free(val);
1933  }
1934 
1935  len = static_cast<zlno_t>(globalNumVtx);
1936  }
1937 
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));
1941 
1942  Array<ArrayView<const zscalar_t> > coordData;
1943  for (int dim=0; dim < ndim; dim++)
1944  coordData.push_back(coords[dim].view(0, len));
1945 
1946  RCP<tMVector_t> fromCoords =
1947  rcp(new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1948 
1949  RCP<tMVector_t> toCoords = rcp(new tMVector_t(toMap, ndim));
1950 
1951  toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
1952 
1953  xyz_ = toCoords;
1954 
1955 }
1956 
1959 
1960 double UserInputForTests::chaco_read_val(
1961  FILE* infile, /* file to read value from */
1962  int *end_flag /* 0 => OK, 1 => EOL, -1 => EOF */
1963 )
1964 {
1965  double val; /* return value */
1966  char *ptr; /* ptr to next string to read */
1967  char *ptr2; /* ptr to next string to read */
1968  int length; /* length of line to read */
1969  int length_left;/* length of line still around */
1970  int white_seen; /* have I detected white space yet? */
1971  int done; /* checking for end of scan */
1972  int i; /* loop counter */
1973 
1974  *end_flag = 0;
1975 
1976  if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
1977  if (chaco_offset >= chaco_break_pnt) { /* Copy rest of line back to beginning. */
1978  length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
1979  ptr2 = chaco_line;
1980  ptr = &chaco_line[chaco_save_pnt];
1981  for (i=length_left; i; i--) *ptr2++ = *ptr++;
1982  length = chaco_save_pnt + 1;
1983  }
1984  else {
1985  length = CHACO_LINE_LENGTH;
1986  length_left = 0;
1987  }
1988 
1989  /* Now read next line, or next segment of current one. */
1990  ptr2 = fgets(&chaco_line[length_left], length, infile);
1991 
1992  if (ptr2 == (char *) NULL) { /* We've hit end of file. */
1993  *end_flag = -1;
1994  return((double) 0.0);
1995  }
1996 
1997  if ((chaco_line[CHACO_LINE_LENGTH - 2] != '\n') && (chaco_line[CHACO_LINE_LENGTH - 2] != '\f')
1998  && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
1999  /* Line too long. Find last safe place in chaco_line. */
2000  chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2001  chaco_save_pnt = chaco_break_pnt;
2002  white_seen = 0;
2003  done = 0;
2004  while (!done) {
2005  --chaco_break_pnt;
2006  if (chaco_line[chaco_break_pnt] != '\0') {
2007  if (isspace((int)(chaco_line[chaco_break_pnt]))) {
2008  if (!white_seen) {
2009  chaco_save_pnt = chaco_break_pnt + 1;
2010  white_seen = 1;
2011  }
2012  }
2013  else if (white_seen) {
2014  done= 1;
2015  }
2016  }
2017  }
2018  }
2019  else {
2020  chaco_break_pnt = CHACO_LINE_LENGTH;
2021  }
2022 
2023  chaco_offset = 0;
2024  }
2025 
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] == '#') {
2028  *end_flag = 1;
2029  if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2030  chaco_flush_line(infile);
2031  }
2032  return((double) 0.0);
2033  }
2034 
2035  ptr = &(chaco_line[chaco_offset]);
2036  val = strtod(ptr, &ptr2);
2037 
2038  if (ptr2 == ptr) { /* End of input line. */
2039  chaco_offset = 0;
2040  *end_flag = 1;
2041  return((double) 0.0);
2042  }
2043  else {
2044  chaco_offset = (int) (ptr2 - chaco_line) / sizeof(char);
2045  }
2046 
2047  return(val);
2048 }
2049 
2050 
2051 int UserInputForTests::chaco_read_int(
2052  FILE *infile, /* file to read value from */
2053  int *end_flag /* 0 => OK, 1 => EOL, -1 => EOF */
2054 )
2055 {
2056  int val; /* return value */
2057  char *ptr; /* ptr to next string to read */
2058  char *ptr2; /* ptr to next string to read */
2059  int length; /* length of line to read */
2060  int length_left; /* length of line still around */
2061  int white_seen; /* have I detected white space yet? */
2062  int done; /* checking for end of scan */
2063  int i; /* loop counter */
2064 
2065  *end_flag = 0;
2066 
2067  if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
2068  if (chaco_offset >= chaco_break_pnt) { /* Copy rest of line back to beginning. */
2069  length_left = CHACO_LINE_LENGTH - chaco_save_pnt - 1;
2070  ptr2 = chaco_line;
2071  ptr = &chaco_line[chaco_save_pnt];
2072  for (i=length_left; i; i--) *ptr2++ = *ptr++;
2073  length = chaco_save_pnt + 1;
2074  }
2075  else {
2076  length = CHACO_LINE_LENGTH;
2077  length_left = 0;
2078  }
2079 
2080  /* Now read next line, or next segment of current one. */
2081  ptr2 = fgets(&chaco_line[length_left], length, infile);
2082 
2083  if (ptr2 == (char *) NULL) { /* We've hit end of file. */
2084  *end_flag = -1;
2085  return(0);
2086  }
2087 
2088  if ((chaco_line[CHACO_LINE_LENGTH - 2] != '\n') && (chaco_line[CHACO_LINE_LENGTH - 2] != '\f')
2089  && (strlen(chaco_line) == CHACO_LINE_LENGTH - 1)){
2090  /* Line too long. Find last safe place in line. */
2091  chaco_break_pnt = CHACO_LINE_LENGTH - 1;
2092  chaco_save_pnt = chaco_break_pnt;
2093  white_seen = 0;
2094  done = 0;
2095  while (!done) {
2096  --chaco_break_pnt;
2097  if (chaco_line[chaco_break_pnt] != '\0') {
2098  if (isspace((int)(chaco_line[chaco_break_pnt]))) {
2099  if (!white_seen) {
2100  chaco_save_pnt = chaco_break_pnt + 1;
2101  white_seen = 1;
2102  }
2103  }
2104  else if (white_seen) {
2105  done= 1;
2106  }
2107  }
2108  }
2109  }
2110  else {
2111  chaco_break_pnt = CHACO_LINE_LENGTH;
2112  }
2113 
2114  chaco_offset = 0;
2115  }
2116 
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] == '#') {
2119  *end_flag = 1;
2120  if (chaco_break_pnt < CHACO_LINE_LENGTH) {
2121  chaco_flush_line(infile);
2122  }
2123  return(0);
2124  }
2125 
2126  ptr = &(chaco_line[chaco_offset]);
2127  val = (int) strtol(ptr, &ptr2, 10);
2128 
2129  if (ptr2 == ptr) { /* End of input chaco_line. */
2130  chaco_offset = 0;
2131  *end_flag = 1;
2132  return(0);
2133  }
2134  else {
2135  chaco_offset = (int) (ptr2 - chaco_line) / sizeof(char);
2136  }
2137 
2138  return(val);
2139 }
2140 
2141 void UserInputForTests::chaco_flush_line(
2142  FILE *infile /* file to read value from */
2143 )
2144 {
2145  char c; /* character being read */
2146 
2147  c = fgetc(infile);
2148  while (c != '\n' && c != '\f')
2149  c = fgetc(infile);
2150 }
2151 
2152 int UserInputForTests::chaco_input_graph(
2153  FILE *fin, /* input file */
2154  const char *inname, /* name of input file */
2155  int **start, /* start of edge list for each vertex */
2156  int **adjacency, /* edge list data */
2157  int *nvtxs, /* number of vertices in graph */
2158  int *nVwgts, /* # of vertex weights per node */
2159  float **vweights, /* vertex weight list data */
2160  int *nEwgts, /* # of edge weights per edge */
2161  float **eweights /* edge weight list data */
2162 )
2163 {
2164  int *adjptr; /* loops through adjacency data */
2165  float *ewptr; /* loops through edge weight data */
2166  int narcs; /* number of edges expected in graph */
2167  int nedges; /* twice number of edges really in graph */
2168  int nedge; /* loops through edges for each vertex */
2169  int flag; /* condition indicator */
2170  int skip_flag; /* should this edge be ignored? */
2171  int end_flag; /* indicates end of line or file */
2172  int vtx; /* vertex in graph */
2173  int line_num; /* line number in input file */
2174  int sum_edges; /* total number of edges read so far */
2175  int option = 0; /* input option */
2176  int using_ewgts; /* are edge weights in input file? */
2177  int using_vwgts; /* are vertex weights in input file? */
2178  int vtxnums; /* are vertex numbers in input file? */
2179  int vertex; /* current vertex being read */
2180  int new_vertex; /* new vertex being read */
2181  float weight; /* weight being read */
2182  float eweight; /* edge weight being read */
2183  int neighbor; /* neighbor of current vertex */
2184  int error_flag; /* error reading input? */
2185  int j; /* loop counters */
2186 
2187  /* Read first line of input (= nvtxs, narcs, option). */
2188  /* The (decimal) digits of the option variable mean: 1's digit not zero => input
2189  edge weights 10's digit not zero => input vertex weights 100's digit not zero
2190  => include vertex numbers */
2191 
2192  *start = NULL;
2193  *adjacency = NULL;
2194  *vweights = NULL;
2195  *eweights = NULL;
2196 
2197  error_flag = 0;
2198  line_num = 0;
2199 
2200  /* Read any leading comment lines */
2201  end_flag = 1;
2202  while (end_flag == 1) {
2203  *nvtxs = chaco_read_int(fin, &end_flag);
2204  ++line_num;
2205  }
2206  if (*nvtxs <= 0) {
2207  printf("ERROR in graph file `%s':", inname);
2208  printf(" Invalid number of vertices (%d).\n", *nvtxs);
2209  fclose(fin);
2210  return(1);
2211  }
2212 
2213  narcs = chaco_read_int(fin, &end_flag);
2214  if (narcs < 0) {
2215  printf("ERROR in graph file `%s':", inname);
2216  printf(" Invalid number of expected edges (%d).\n", narcs);
2217  fclose(fin);
2218  return(1);
2219  }
2220 
2221  /* Check if vertex or edge weights are used */
2222  if (!end_flag) {
2223  option = chaco_read_int(fin, &end_flag);
2224  }
2225  using_ewgts = option - 10 * (option / 10);
2226  option /= 10;
2227  using_vwgts = option - 10 * (option / 10);
2228  option /= 10;
2229  vtxnums = option - 10 * (option / 10);
2230 
2231  /* Get weight info from Chaco option */
2232  (*nVwgts) = using_vwgts;
2233  (*nEwgts) = using_ewgts;
2234 
2235  /* Read numbers of weights if they are specified separately */
2236  if (!end_flag && using_vwgts==1){
2237  j = chaco_read_int(fin, &end_flag);
2238  if (!end_flag) (*nVwgts) = j;
2239  }
2240  if (!end_flag && using_ewgts==1){
2241  j = chaco_read_int(fin, &end_flag);
2242  if (!end_flag) (*nEwgts) = j;
2243  }
2244 
2245  /* Discard rest of line */
2246  while (!end_flag)
2247  j = chaco_read_int(fin, &end_flag);
2248 
2249  /* Allocate space for rows and columns. */
2250  *start = (int *) malloc((unsigned) (*nvtxs + 1) * sizeof(int));
2251  if (narcs != 0)
2252  *adjacency = (int *) malloc((unsigned) (2 * narcs + 1) * sizeof(int));
2253  else
2254  *adjacency = NULL;
2255 
2256  if (using_vwgts)
2257  *vweights = (float *) malloc((unsigned) (*nvtxs) * (*nVwgts) * sizeof(float));
2258  else
2259  *vweights = NULL;
2260 
2261  if (using_ewgts)
2262  *eweights = (float *)
2263  malloc((unsigned) (2 * narcs + 1) * (*nEwgts) * sizeof(float));
2264  else
2265  *eweights = NULL;
2266 
2267  adjptr = *adjacency;
2268  ewptr = *eweights;
2269 
2270  sum_edges = 0;
2271  nedges = 0;
2272  (*start)[0] = 0;
2273  vertex = 0;
2274  vtx = 0;
2275  new_vertex = 1;
2276  while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
2277  ++line_num;
2278 
2279  /* If multiple input lines per vertex, read vertex number. */
2280  if (vtxnums) {
2281  j = chaco_read_int(fin, &end_flag);
2282  if (end_flag) {
2283  if (vertex == *nvtxs)
2284  break;
2285  printf("ERROR in graph file `%s':", inname);
2286  printf(" no vertex number in line %d.\n", line_num);
2287  fclose(fin);
2288  return (1);
2289  }
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);
2293  fclose(fin);
2294  return (1);
2295  }
2296  if (j != vertex) {
2297  new_vertex = 1;
2298  vertex = j;
2299  }
2300  else
2301  new_vertex = 0;
2302  }
2303  else
2304  vertex = ++vtx;
2305 
2306  if (vertex > *nvtxs)
2307  break;
2308 
2309  /* If vertices are weighted, read vertex weight. */
2310  if (using_vwgts && new_vertex) {
2311  for (j=0; j<(*nVwgts); j++){
2312  weight = chaco_read_val(fin, &end_flag);
2313  if (end_flag) {
2314  printf("ERROR in graph file `%s':", inname);
2315  printf(" not enough weights for vertex %d.\n", vertex);
2316  fclose(fin);
2317  return (1);
2318  }
2319  (*vweights)[(vertex-1)*(*nVwgts)+j] = weight;
2320  }
2321  }
2322 
2323  nedge = 0;
2324 
2325  /* Read number of adjacent vertex. */
2326  neighbor = chaco_read_int(fin, &end_flag);
2327 
2328  while (!end_flag) {
2329  skip_flag = 0;
2330 
2331  if (using_ewgts) { /* Read edge weight if it's being input. */
2332  for (j=0; j<(*nEwgts); j++){
2333  eweight = chaco_read_val(fin, &end_flag);
2334 
2335  if (end_flag) {
2336  printf("ERROR in graph file `%s':", inname);
2337  printf(" not enough weights for edge (%d,%d).\n", vertex, neighbor);
2338  fclose(fin);
2339  return (1);
2340  }
2341 
2342  else {
2343  *ewptr++ = eweight;
2344  }
2345  }
2346  }
2347 
2348  /* Add edge to data structure. */
2349  if (!skip_flag) {
2350  if (++nedges > 2*narcs) {
2351  printf("ERROR in graph file `%s':", inname);
2352  printf(" at least %d adjacencies entered, but nedges = %d\n",
2353  nedges, narcs);
2354  fclose(fin);
2355  return (1);
2356  }
2357  *adjptr++ = neighbor;
2358  nedge++;
2359  }
2360 
2361  /* Read number of next adjacent vertex. */
2362  neighbor = chaco_read_int(fin, &end_flag);
2363  }
2364 
2365  sum_edges += nedge;
2366  (*start)[vertex] = sum_edges;
2367  }
2368 
2369  /* Make sure there's nothing else in file. */
2370  flag = 0;
2371  while (!flag && end_flag != -1) {
2372  chaco_read_int(fin, &end_flag);
2373  if (!end_flag)
2374  flag = 1;
2375  }
2376 
2377  (*start)[*nvtxs] = sum_edges;
2378 
2379  if (vertex != 0) { /* Normal file was read. */
2380  if (narcs) {
2381  }
2382  else { /* no edges, but did have vertex weights or vertex numbers */
2383  free(*start);
2384  *start = NULL;
2385  if (*adjacency != NULL)
2386  free(*adjacency);
2387  *adjacency = NULL;
2388  if (*eweights != NULL)
2389  free(*eweights);
2390  *eweights = NULL;
2391  }
2392  }
2393 
2394  else {
2395  /* Graph was empty */
2396  free(*start);
2397  if (*adjacency != NULL)
2398  free(*adjacency);
2399  if (*vweights != NULL)
2400  free(*vweights);
2401  if (*eweights != NULL)
2402  free(*eweights);
2403  *start = NULL;
2404  *adjacency = NULL;
2405  }
2406 
2407  fclose(fin);
2408 
2409  return (error_flag);
2410 }
2411 
2412 
2413 int UserInputForTests::chaco_input_geom(
2414  FILE *fingeom, /* geometry input file */
2415  const char *geomname, /* name of geometry file */
2416  int nvtxs, /* number of coordinates to read */
2417  int *igeom, /* dimensionality of geometry */
2418  double **x, /* coordinates of vertices */
2419  double **y,
2420  double **z
2421 )
2422 {
2423  double xc, yc, zc =0; /* first x, y, z coordinate */
2424  int nread; /* number of lines of coordinates read */
2425  int flag; /* any bad data at end of file? */
2426  int line_num; /* counts input lines in file */
2427  int end_flag; /* return conditional */
2428  int ndims; /* number of values in an input line */
2429  int i=0; /* loop counter */
2430 
2431  *x = *y = *z = NULL;
2432  line_num = 0;
2433  end_flag = 1;
2434  while (end_flag == 1) {
2435  xc = chaco_read_val(fingeom, &end_flag);
2436  ++line_num;
2437  }
2438 
2439  if (end_flag == -1) {
2440  printf("No values found in geometry file `%s'\n", geomname);
2441  fclose(fingeom);
2442  return (1);
2443  }
2444 
2445  ndims = 1;
2446  yc = chaco_read_val(fingeom, &end_flag);
2447  if (end_flag == 0) {
2448  ndims = 2;
2449  zc = chaco_read_val(fingeom, &end_flag);
2450  if (end_flag == 0) {
2451  ndims = 3;
2452  chaco_read_val(fingeom, &end_flag);
2453  if (!end_flag) {
2454  printf("Too many values on input line of geometry file `%s'\n",
2455  geomname);
2456 
2457  printf(" Maximum dimensionality is 3\n");
2458  fclose(fingeom);
2459  return (1);
2460  }
2461  }
2462  }
2463 
2464  *igeom = ndims;
2465 
2466  *x = (double *) malloc((unsigned) nvtxs * sizeof(double));
2467  (*x)[0] = xc;
2468  if (ndims > 1) {
2469  *y = (double *) malloc((unsigned) nvtxs * sizeof(double));
2470  (*y)[0] = yc;
2471  }
2472  if (ndims > 2) {
2473  *z = (double *) malloc((unsigned) nvtxs * sizeof(double));
2474  (*z)[0] = zc;
2475  }
2476 
2477  for (nread = 1; nread < nvtxs; nread++) {
2478  ++line_num;
2479  if (ndims == 1) {
2480  i = fscanf(fingeom, "%lf", &((*x)[nread]));
2481  }
2482  else if (ndims == 2) {
2483  i = fscanf(fingeom, "%lf%lf", &((*x)[nread]), &((*y)[nread]));
2484  }
2485  else if (ndims == 3) {
2486  i = fscanf(fingeom, "%lf%lf%lf", &((*x)[nread]), &((*y)[nread]),
2487  &((*z)[nread]));
2488  }
2489 
2490  if (i == EOF) {
2491  printf("Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
2492  nvtxs, nread);
2493  fclose(fingeom);
2494  return (1);
2495  }
2496  else if (i != ndims) {
2497  printf("Wrong number of values in line %d of geometry file `%s'\n",
2498  line_num, geomname);
2499  fclose(fingeom);
2500  return (1);
2501  }
2502  }
2503 
2504  /* Check for spurious extra stuff in file. */
2505  flag = 0;
2506  end_flag = 0;
2507  while (!flag && end_flag != -1) {
2508  chaco_read_val(fingeom, &end_flag);
2509  if (!end_flag)
2510  flag = 1;
2511  }
2512 
2513  fclose(fingeom);
2514 
2515  return (0);
2516 }
2517 
2518 // Chaco input assignments from filename.assign; copied from Zoltan
2519 
2520 int UserInputForTests::chaco_input_assign(
2521  FILE *finassign, /* input assignment file */
2522  const char *inassignname, /* name of input assignment file */
2523  int nvtxs, /* number of vertices to output */
2524  short *assignment) /* values to be printed */
2525 {
2526  int flag; /* logical conditional */
2527  int end_flag; /* return flag from read_int() */
2528  int i, j; /* loop counter */
2529 
2530  /* Get the assignment vector one line at a time, checking as you go. */
2531  /* First read past any comments at top. */
2532  end_flag = 1;
2533  while (end_flag == 1) {
2534  assignment[0] = chaco_read_int(finassign, &end_flag);
2535  }
2536 
2537  if (assignment[0] < 0) {
2538  printf("ERROR: Entry %d in assignment file `%s' less than zero (%d)\n",
2539  1, inassignname, assignment[0]);
2540  fclose(finassign);
2541  return (1);
2542  }
2543 
2544  if (end_flag == -1) {
2545  printf("ERROR: No values found in assignment file `%s'\n", inassignname);
2546  fclose(finassign);
2547  return (1);
2548  }
2549 
2550  flag = 0;
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]));
2556  if (j != 1) {
2557  printf("ERROR: Too few values in assignment file `%s'.\n", inassignname);
2558  fclose(finassign);
2559  return (1);
2560  }
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]);
2564  fclose(finassign);
2565  return (1);
2566  }
2567  if (assignment[i] >= np) { // warn since perhaps an error -- initial part
2568  // assignment is greater than number of processors
2569  if (assignment[i] > flag)
2570  flag = assignment[i];
2571  assignment[i] = rand() % np; // randomly assign vtx to a proc in this case
2572  }
2573  }
2574  srand(rand());
2575 
2576  if (flag) {
2577  printf("WARNING: Possible error in assignment file `%s'\n",
2578  inassignname);
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");
2582  }
2583 
2584  /* Check for spurious extra stuff in file. */
2585  flag = 0;
2586  end_flag = 0;
2587  while (!flag && end_flag != -1) {
2588  chaco_read_int(finassign, &end_flag);
2589  if (!end_flag)
2590  flag = 1;
2591  }
2592  if (flag) {
2593  printf("WARNING: Possible error in assignment file `%s'\n", inassignname);
2594  printf(" Numerical data found after expected end of file\n");
2595  }
2596 
2597  fclose(finassign);
2598  return (0);
2599 }
2600 
2601 // Pamgen Reader
2602 void UserInputForTests::readPamgenMeshFile(string path, string testData)
2603 {
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;
2608 
2609  size_t len;
2610  std::fstream file;
2611  int dimension;
2612  if (rank == 0){
2613  // set file name
2614  std::ostringstream meshFileName;
2615  meshFileName << path << "/" << testData << ".pmgen";
2616  // open file
2617 
2618  file.open(meshFileName.str(), std::ios::in);
2619 
2620  if(!file.is_open()) // may be a problem with path or filename
2621  {
2622  if(verbose_ && tcomm_->getRank() == 0)
2623  {
2624  std::cout << "Unable to open pamgen mesh: ";
2625  std::cout << meshFileName.str();
2626  std::cout <<"\nPlease check file path and name." << std::endl;
2627  }
2628  len = 0; // broadcaset 0 length ->will cause exit
2629  }else{
2630  // write to character array
2631  // get size of file
2632  file.seekg (0,file.end);
2633  len = file.tellg();
2634  file.seekg (0);
2635 
2636  // get dimension
2637  dimension = 2;
2638  std::string line;
2639  while(std::getline(file,line))
2640  {
2641  if( line.find("nz") != std::string::npos ||
2642  line.find("nphi") != std::string::npos)
2643  {
2644  dimension = 3;
2645  break;
2646  }
2647  }
2648 
2649  file.clear();
2650  file.seekg(0, std::ios::beg);
2651  }
2652  }
2653 
2654  // broadcast the file size
2655  this->tcomm_->broadcast(0,sizeof(int), (char *)&dimension);
2656  this->tcomm_->broadcast(0,sizeof(size_t),(char *)&len);
2657  this->tcomm_->barrier();
2658 
2659  if(len == 0){
2660  if(verbose_ && tcomm_->getRank() == 0)
2661  std::cout << "Pamgen Mesh file size == 0, exiting UserInputForTests early." << std::endl;
2662  return;
2663  }
2664 
2665  char * file_data = new char[len+1];
2666  file_data[len] = '\0'; // critical to null terminate buffer
2667  if(rank == 0){
2668  file.read(file_data,len); // if proc 0 then read file
2669  }
2670 
2671  // broadcast the file to the world
2672  this->tcomm_->broadcast(0,(int)len+1,file_data);
2673  this->tcomm_->barrier();
2674 
2675  // Create the PamgenMesh
2676 
2677  this->pamgen_mesh = rcp(new PamgenMesh);
2678  this->havePamgenMesh = true;
2679  pamgen_mesh->createMesh(file_data,dimension,this->tcomm_);
2680 
2681  // save mesh info
2682  pamgen_mesh->storeMesh();
2683  this->tcomm_->barrier();
2684 
2685  // set coordinates
2686  this->setPamgenCoordinateMV();
2687 
2688  // set adjacency graph
2689  this->setPamgenAdjacencyGraph();
2690 
2691  this->tcomm_->barrier();
2692  if(rank == 0) file.close();
2693  delete [] file_data;
2694 #else
2695  throw std::runtime_error("Pamgen requested but Trilinos "
2696  "not built with Pamgen");
2697 #endif
2698 }
2699 
2700 #ifdef HAVE_ZOLTAN2_PAMGEN
2701 void UserInputForTests::setPamgenCoordinateMV()
2702 {
2703  int dimension = pamgen_mesh->num_dim;
2704  // get coordinate and point info;
2705 // zlno_t numLocalPoints = pamgen_mesh->num_nodes;
2706 // zgno_t numGlobalPoints = pamgen_mesh->num_nodes_global;
2707  zgno_t numelements = pamgen_mesh->num_elem;
2708  zgno_t numGlobalElements = pamgen_mesh->num_elems_global;
2709  // allocate and set an array of coordinate arrays
2710  zscalar_t **elem_coords = new zscalar_t * [dimension];
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];
2715  }
2716 
2717  // make a Tpetra map
2718  RCP<Tpetra::Map<zlno_t, zgno_t, znode_t> > mp;
2719  // mp = rcp(new map_t(numGlobalElements, numelements, 0, this->tcomm_)); // constructo 1
2720 
2721 // Array<zgno_t>::size_type numEltsPerProc = numelements;
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];
2725  }
2726 
2727  mp = rcp (new map_t (numGlobalElements, elementList, 0, this->tcomm_)); // constructor 2
2728 
2729 
2730  // make an array of array views containing the coordinate data
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);
2735  coordView[i] = a;
2736  }
2737  else {
2738  Teuchos::ArrayView<const zscalar_t> a;
2739  coordView[i] = a;
2740  }
2741  }
2742 
2743  // set the xyz_ multivector
2744  xyz_ = RCP<tMVector_t>(new
2745  tMVector_t(mp, coordView.view(0, dimension),
2746  dimension));
2747 }
2748 
2749 void UserInputForTests::setPamgenAdjacencyGraph()
2750 {
2751 // int rank = this->tcomm_->getRank();
2752 // if(rank == 0) std::cout << "Making a graph from our pamgen mesh...." << std::endl;
2753 
2754  // Define Types
2755 // typedef zlno_t lno_t;
2756 // typedef zgno_t gno_t;
2757 
2758  // get info for setting up map
2759  size_t local_nodes = (size_t)this->pamgen_mesh->num_nodes;
2760  size_t local_els = (size_t)this->pamgen_mesh->num_elem;
2761 
2762  size_t global_els = (size_t)this->pamgen_mesh->num_elems_global; // global rows
2763  size_t global_nodes = (size_t)this->pamgen_mesh->num_nodes_global; //global columns
2764  // make map with global elements assigned to this mesh
2765  // make range map
2766 // if(rank == 0) std::cout << "Building Rowmap: " << std::endl;
2767  RCP<const map_t> rowMap = rcp(new map_t(global_els,0,this->tcomm_));
2768  RCP<const map_t> rangeMap = rowMap;
2769 
2770  // make domain map
2771  RCP<const map_t> domainMap = rcp(new map_t(global_nodes,0,this->tcomm_));
2772 
2773  // Get max number of nodes per element
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];
2779 
2780  // make the element-node adjacency matrix
2781  Teuchos::RCP<tcrsMatrix_t> C = rcp(new tcrsMatrix_t(rowMap,max_nodes_per_el));
2782 
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;
2786  }
2787 
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;
2791  }
2792 
2793  zlno_t el_no = 0;
2794  zscalar_t one = static_cast<zscalar_t>(1);
2795 
2796 // if(rank == 0) std::cout << "Writing C... " << std::endl;
2797  for(int i = 0; i < blks; i++)
2798  {
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];
2802 
2803  for(int j = 0; j < el_per_block; j++)
2804  {
2805  const zgno_t gid = static_cast<zgno_t>(g_el_ids[el_no]);
2806  for(int k = 0; k < nodes_per_el; k++)
2807  {
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));
2812  }
2813  el_no++;
2814  }
2815  }
2816 
2817  C->fillComplete(domainMap, rangeMap);
2818 
2819 
2820  // Compute product C*C'
2821 // if(rank == 0) std::cout << "Compute Multiplication C... " << std::endl;
2822  RCP<tcrsMatrix_t> A = rcp(new tcrsMatrix_t(rowMap,0));
2823  Tpetra::MatrixMatrix::Multiply(*C, false, *C, true, *A);
2824 
2825  // remove entries not adjacent
2826  // make graph
2827 // if(rank == 0) std::cout << "Writing M_... " << std::endl;
2828  this->M_ = rcp(new tcrsMatrix_t(rowMap, A->getGlobalMaxNumRowEntries()));
2829 
2830 // if(rank == 0) std::cout << "\nSetting graph of connectivity..." << std::endl;
2831  Teuchos::ArrayView<const zgno_t> rowMapElementList =
2832  rowMap->getLocalElementList();
2833  for (Teuchos_Ordinal ii = 0; ii < rowMapElementList.size(); ii++)
2834  {
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);
2839 
2840  // modified
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++) {
2845 // if (rowvals[i] == 2*(this->pamgen_mesh->num_dim-1))
2846 // {
2847  if (rowvals[i] >= 1)
2848  {
2849  mod_rowvals.push_back(one);
2850  mod_rowinds.push_back(rowinds[i]);
2851  }
2852  }
2853  this->M_->insertGlobalValues(gid, mod_rowinds, mod_rowvals);
2854  }
2855 
2856  this->M_->fillComplete();
2858  // if(rank == 0) std::cout << "Completed M... " << std::endl;
2859 
2860 }
2861 
2862 #endif
2863 
2864 #endif
keep typedefs that commonly appear in many places localized
#define TEST_FAIL_AND_THROW(comm, ok, s)
RCP< tMVector_t > getUITpetraMultiVector(int nvec)
map_t::node_type default_znode_t
Tpetra::Import< zlno_t, zgno_t, znode_t > import_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Tpetra::Map< zlno_t, zgno_t, znode_t > map_t
RCP< tcrsMatrix_t > getUITpetraCrsMatrix()
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)
RCP< tVector_t > getUITpetraVector()
USERINPUT_FILE_FORMATS
A class that generates typical user input for testing.
static void getUIRandomData(unsigned int seed, zlno_t length, zscalar_t min, zscalar_t max, ArrayView< ArrayRCP< zscalar_t > > data)
Generate lists of random scalars.
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
RCP< tMVector_t > getUIWeights()
static RCP< User > convertToXpetra(const RCP< User > &a)
Convert the object to its Xpetra wrapped version.
UserInputForTests(string path, string testData, const RCP< const Comm< int > > &c, bool debugInfo=false, bool distributeInput=true)
Constructor that reads in a matrix/graph from disk.
Tpetra::Export< zlno_t, zgno_t, znode_t > export_t
string trim_copy(const string &s, const string &delimiters=" \f\n\r\t\v")
RCP< xcrsMatrix_t > getUIXpetraCrsMatrix()
const char param_comment
list fname
Begin.
Definition: validXML.py:19
RCP< xVector_t > getUIXpetraVector()
bool hasInputDataType(const string &input_type)
GeometricGen::GeometricGenerator< zscalar_t, zlno_t, zgno_t, znode_t > geometricgen_t
RCP< tMVector_t > getUIEdgeWeights()
Xpetra::CrsGraph< zlno_t, zgno_t, znode_t > xcrsGraph_t
static RCP< tMVector_t > coordinates
Tpetra::Map map_t
Definition: mapRemotes.cpp:25
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")
RCP< xMVector_t > getUIXpetraMultiVector(int nvec)
RCP< tMVector_t > getUICoordinates()
Tpetra::global_size_t global_size_t
RCP< xcrsGraph_t > getUIXpetraCrsGraph()
float zscalar_t
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Tpetra::Map::global_ordinal_type zgno_t
RCP< tcrsGraph_t > getUITpetraCrsGraph()