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