Zoltan2  Version of the Day
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_XpetraTraits.hpp>
54 #include <Zoltan2_ChacoReader.hpp>
55 
56 #include <Tpetra_MultiVector.hpp>
57 #include <Xpetra_Vector.hpp>
58 #include <Xpetra_CrsMatrix.hpp>
59 #include <Xpetra_CrsGraph.hpp>
60 
61 #include <MatrixMarket_Tpetra.hpp>
62 #include <Galeri_XpetraProblemFactory.hpp>
63 #include <Galeri_XpetraParameters.hpp>
64 
65 #include <Kokkos_DefaultNode.hpp>
66 
67 //#include <Xpetra_EpetraUtils.hpp>
68 #ifdef HAVE_ZOLTAN2_MPI
69 #include <Epetra_MpiComm.h>
70 #else
71 #include <Epetra_SerialComm.h>
72 #endif
73 
74 using Teuchos::RCP;
75 using Teuchos::ArrayRCP;
76 using Teuchos::ArrayView;
77 using Teuchos::Array;
78 using Teuchos::Comm;
79 using Teuchos::rcp;
80 using Teuchos::arcp;
81 using Teuchos::rcp_const_cast;
82 using std::string;
83 
115 {
116 public:
117 
118  typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> tcrsMatrix_t;
119  typedef Tpetra::CrsGraph<lno_t, gno_t, node_t> tcrsGraph_t;
120  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> tVector_t;
121  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> tMVector_t;
122 
123  typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> xcrsMatrix_t;
124  typedef Xpetra::CrsGraph<lno_t, gno_t, node_t> xcrsGraph_t;
125  typedef Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> xVector_t;
126  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> xMVector_t;
127 
128  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
129  typedef Tpetra::Export<lno_t, gno_t, node_t> export_t;
130  typedef Tpetra::Import<lno_t, gno_t, node_t> import_t;
131  typedef Kokkos::DefaultNode::DefaultNodeType default_node_t;
132 
149  UserInputForTests(string path, string testData,
150  const RCP<const Comm<int> > &c, bool debugInfo=false);
151 
168  UserInputForTests(int x, int y, int z, string matrixType,
169  const RCP<const Comm<int> > &c, bool debugInfo=false);
170 
173  static void getRandomData(unsigned int seed, lno_t length,
174  scalar_t min, scalar_t max, ArrayView<ArrayRCP<scalar_t > > data);
175 
176  RCP<tMVector_t> getCoordinates();
177 
178  RCP<tMVector_t> getWeights();
179 
180  RCP<tMVector_t> getEdgeWeights();
181 
182  RCP<tcrsMatrix_t> getTpetraCrsMatrix();
183 
184  RCP<tcrsGraph_t> getTpetraCrsGraph();
185 
186  RCP<tVector_t> getTpetraVector();
187 
188  RCP<tMVector_t> getTpetraMultiVector(int nvec);
189 
190  RCP<xcrsMatrix_t> getXpetraCrsMatrix();
191 
192  RCP<xcrsGraph_t> getXpetraCrsGraph();
193 
194  RCP<xVector_t> getXpetraVector();
195 
196  RCP<xMVector_t> getXpetraMultiVector(int nvec);
197 
198 #ifdef HAVE_EPETRA_DATA_TYPES
199  RCP<Epetra_CrsGraph> getEpetraCrsGraph();
200 
201  RCP<Epetra_CrsMatrix> getEpetraCrsMatrix();
202 
203  RCP<Epetra_Vector> getEpetraVector();
204 
205  RCP<Epetra_MultiVector> getEpetraMultiVector(int nvec);
206 #endif
207 
208 private:
209 
210  bool verbose_;
211 
212  const RCP<const Comm<int> > tcomm_;
213 
214  RCP<tcrsMatrix_t> M_;
215  RCP<xcrsMatrix_t> xM_;
216 
217  RCP<tMVector_t> xyz_;
218  RCP<tMVector_t> vtxWeights_;
219  RCP<tMVector_t> edgWeights_;
220 
221 #ifdef HAVE_EPETRA_DATA_TYPES
222  RCP<const Epetra_Comm> ecomm_;
223  RCP<Epetra_CrsMatrix> eM_;
224  RCP<Epetra_CrsGraph> eG_;
225 #endif
226 
227  // Read a Matrix Market file into M_
228  // using Tpetra::MatrixMarket::Reader.
229  // If there are "Tim Davis" style coordinates
230  // that go with the file, read those into xyz_.
231 
232  void readMatrixMarketFile(string path, string testData);
233 
234  // Build matrix M_ from a mesh and a problem type
235  // with Galeri::Xpetra.
236 
237  void buildCrsMatrix(int xdim, int ydim, int zdim, string type);
238 
239  // Read a Zoltan1 Chaco or Matrix Market file
240  // into M_. If it has geometric coordinates,
241  // read them into xyz_. If it has weights,
242  // read those into vtxWeights_ and edgWeights_.
243 
244  void readZoltanTestData(string path, string testData);
245 
246  // Read Zoltan data that is in a .graph file.
247  void getChacoGraph(FILE *fptr, string name);
248 
249  // Read Zoltan data that is in a .coords file.
250  void getChacoCoords(FILE *fptr, string name);
251 };
252 
253 UserInputForTests::UserInputForTests(string path, string testData,
254  const RCP<const Comm<int> > &c, bool debugInfo):
255  verbose_(debugInfo),
256  tcomm_(c), M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_()
258  ,ecomm_(), eM_(), eG_()
259 #endif
260 {
261  bool zoltan1 = false;
262  string::size_type loc = path.find("/zoltan/test/"); // Zoltan1 data
263  if (loc != string::npos)
264  zoltan1 = true;
265 
266  if (zoltan1)
267  readZoltanTestData(path, testData);
268  else
269  readMatrixMarketFile(path, testData);
270 
271 #ifdef HAVE_EPETRA_DATA_TYPES
272  ecomm_ = Xpetra::toEpetra(c);
273 #endif
274 }
275 
277  string matrixType, const RCP<const Comm<int> > &c, bool debugInfo):
278  verbose_(debugInfo),
279  tcomm_(c), M_(), xM_(), xyz_(), vtxWeights_(), edgWeights_()
281  ,ecomm_(), eM_(), eG_()
282 #endif
283 {
284  if (matrixType.size() == 0){
285  int dim = 0;
286  if (x > 0) dim++;
287  if (y > 0) dim++;
288  if (z > 0) dim++;
289  if (dim == 1)
290  matrixType = string("Laplace1D");
291  else if (dim == 2)
292  matrixType = string("Laplace2D");
293  else if (dim == 3)
294  matrixType = string("Laplace3D");
295  else
296  throw std::runtime_error("input");
297 
298  if (verbose_ && tcomm_->getRank() == 0)
299  std::cout << "UserInputForTests, Matrix type : " << matrixType << std::endl;
300  }
301 
302  buildCrsMatrix(x, y, z, matrixType);
303 
304 #ifdef HAVE_EPETRA_DATA_TYPES
305  ecomm_ = Xpetra::toEpetra(c);
306 #endif
307 }
308 
309 RCP<UserInputForTests::tMVector_t> UserInputForTests::getCoordinates()
310 {
311  if (xyz_.is_null())
312  throw std::runtime_error("could not read coord file");
313  return xyz_;
314 }
315 
316 RCP<UserInputForTests::tMVector_t> UserInputForTests::getWeights()
317 {
318  return vtxWeights_;
319 }
320 
321 RCP<UserInputForTests::tMVector_t> UserInputForTests::getEdgeWeights()
322 {
323  return edgWeights_;
324 }
325 
326 RCP<UserInputForTests::tcrsMatrix_t> UserInputForTests::getTpetraCrsMatrix()
327 {
328  if (M_.is_null())
329  throw std::runtime_error("could not read mtx file");
330  return M_;
331 }
332 
333 RCP<UserInputForTests::tcrsGraph_t> UserInputForTests::getTpetraCrsGraph()
334 {
335  if (M_.is_null())
336  throw std::runtime_error("could not read mtx file");
337  return rcp_const_cast<tcrsGraph_t>(M_->getCrsGraph());
338 }
339 
340 RCP<UserInputForTests::tVector_t> UserInputForTests::getTpetraVector()
341 {
342  RCP<tVector_t> V = rcp(new tVector_t(M_->getRowMap(), 1));
343  V->randomize();
344 
345  return V;
346 }
347 
348 RCP<UserInputForTests::tMVector_t> UserInputForTests::getTpetraMultiVector(int nvec)
349 {
350  RCP<tMVector_t> mV = rcp(new tMVector_t(M_->getRowMap(), nvec));
351  mV->randomize();
352 
353  return mV;
354 }
355 
356 RCP<UserInputForTests::xcrsMatrix_t> UserInputForTests::getXpetraCrsMatrix()
357 {
358  if (M_.is_null())
359  throw std::runtime_error("could not read mtx file");
360  return xM_;
361 }
362 
363 RCP<UserInputForTests::xcrsGraph_t> UserInputForTests::getXpetraCrsGraph()
364 {
365  if (M_.is_null())
366  throw std::runtime_error("could not read mtx file");
367  return rcp_const_cast<xcrsGraph_t>(xM_->getCrsGraph());
368 }
369 
370 RCP<UserInputForTests::xVector_t> UserInputForTests::getXpetraVector()
371 {
372  RCP<const tVector_t> tV = getTpetraVector();
373  RCP<const xVector_t> xV =
375  return rcp_const_cast<xVector_t>(xV);
376 }
377 
378 RCP<UserInputForTests::xMVector_t> UserInputForTests::getXpetraMultiVector(int nvec)
379 {
380  RCP<const tMVector_t> tMV = getTpetraMultiVector(nvec);
381  RCP<const xMVector_t> xMV =
383  return rcp_const_cast<xMVector_t>(xMV);
384 }
385 
386 #ifdef HAVE_EPETRA_DATA_TYPES
387 RCP<Epetra_CrsGraph> UserInputForTests::getEpetraCrsGraph()
388 {
389  if (M_.is_null())
390  throw std::runtime_error("could not read mtx file");
391  RCP<const tcrsGraph_t> tgraph = M_->getCrsGraph();
392  RCP<const Tpetra::Map<lno_t, gno_t> > trowMap = tgraph->getRowMap();
393  RCP<const Tpetra::Map<lno_t, gno_t> > tcolMap = tgraph->getColMap();
394 
395  int nElts = static_cast<int>(trowMap->getGlobalNumElements());
396  int nMyElts = static_cast<int>(trowMap->getNodeNumElements());
397  int base = trowMap->getIndexBase();
398  ArrayView<const int> gids = trowMap->getNodeElementList();
399 
400  Epetra_BlockMap erowMap(nElts, nMyElts,
401  gids.getRawPtr(), 1, base, *ecomm_);
402 
403  Array<int> rowSize(nMyElts);
404  for (int i=0; i < nMyElts; i++){
405  rowSize[i] = static_cast<int>(M_->getNumEntriesInLocalRow(i+base));
406  }
407 
408  size_t maxRow = M_->getNodeMaxNumRowEntries();
409  Array<int> colGids(maxRow);
410  ArrayView<const int> colLid;
411 
412  eG_ = rcp(new Epetra_CrsGraph(Copy, erowMap,
413  rowSize.getRawPtr(), true));
414 
415  for (int i=0; i < nMyElts; i++){
416  tgraph->getLocalRowView(i+base, colLid);
417  for (int j=0; j < colLid.size(); j++)
418  colGids[j] = tcolMap->getGlobalElement(colLid[j]);
419  eG_->InsertGlobalIndices(gids[i], rowSize[i], colGids.getRawPtr());
420  }
421  eG_->FillComplete();
422  return eG_;
423 }
424 
425 RCP<Epetra_CrsMatrix> UserInputForTests::getEpetraCrsMatrix()
426 {
427  if (M_.is_null())
428  throw std::runtime_error("could not read mtx file");
429  RCP<Epetra_CrsGraph> egraph = getEpetraCrsGraph();
430  eM_ = rcp(new Epetra_CrsMatrix(Copy, *egraph));
431 
432  size_t maxRow = M_->getNodeMaxNumRowEntries();
433  int nrows = egraph->NumMyRows();
434  int base = egraph->IndexBase();
435  const Epetra_BlockMap &rowMap = egraph->RowMap();
436  const Epetra_BlockMap &colMap = egraph->ColMap();
437  Array<int> colGid(maxRow);
438 
439  for (int i=0; i < nrows; i++){
440  ArrayView<const int> colLid;
441  ArrayView<const scalar_t> nz;
442  M_->getLocalRowView(i+base, colLid, nz);
443  size_t rowSize = colLid.size();
444  int rowGid = rowMap.GID(i+base);
445  for (size_t j=0; j < rowSize; j++){
446  colGid[j] = colMap.GID(colLid[j]);
447  }
448  eM_->InsertGlobalValues(
449  rowGid, rowSize, nz.getRawPtr(), colGid.getRawPtr());
450  }
451  eM_->FillComplete();
452  return eM_;
453 }
454 
455 RCP<Epetra_Vector> UserInputForTests::getEpetraVector()
456 {
457  RCP<Epetra_CrsGraph> egraph = getEpetraCrsGraph();
458  RCP<Epetra_Vector> V = rcp(new Epetra_Vector(egraph->RowMap()));
459  V->Random();
460  return V;
461 }
462 
463 RCP<Epetra_MultiVector> UserInputForTests::getEpetraMultiVector(int nvec)
464 {
465  RCP<Epetra_CrsGraph> egraph = getEpetraCrsGraph();
466  RCP<Epetra_MultiVector> mV =
467  rcp(new Epetra_MultiVector(egraph->RowMap(), nvec));
468  mV->Random();
469  return mV;
470 }
471 #endif
472 
473 void UserInputForTests::getRandomData(unsigned int seed, lno_t length,
474  scalar_t min, scalar_t max,
475  ArrayView<ArrayRCP<scalar_t > > data)
476 {
477  if (length < 1)
478  return;
479 
480  size_t dim = data.size();
481  for (size_t i=0; i < dim; i++){
482  scalar_t *tmp = new scalar_t [length];
483  if (!tmp)
484  throw (std::bad_alloc());
485  data[i] = Teuchos::arcp(tmp, 0, length, true);
486  }
487 
488  scalar_t scalingFactor = (max-min) / RAND_MAX;
489  srand(seed);
490  for (size_t i=0; i < dim; i++){
491  scalar_t *x = data[i].getRawPtr();
492  for (lno_t j=0; j < length; j++)
493  *x++ = min + (scalar_t(rand()) * scalingFactor);
494  }
495 }
496 
497 void UserInputForTests::readMatrixMarketFile(string path, string testData)
498 {
499  std::ostringstream fname;
500  fname << path << "/" << testData << ".mtx";
501  RCP<Kokkos::DefaultNode::DefaultNodeType> dnode
502  = Kokkos::DefaultNode::getDefaultNode();
503 
504  if (verbose_ && tcomm_->getRank() == 0)
505  std::cout << "UserInputForTests, Read: " << fname.str() << std::endl;
506 
507  // This reader has some problems. "Pattern" matrices
508  // cannot be read. Until the
509  // reader is fixed, we'll have to get inputs that are consistent with
510  // the reader. (Tpetra bug 5611 and 5624)
511 
512  bool aok = true;
513  try{
514  M_ = Tpetra::MatrixMarket::Reader<tcrsMatrix_t>::readSparseFile(
515  fname.str(), tcomm_, dnode, true, true, false);
516  }
517  catch (std::exception &e) {
518  //TEST_FAIL_AND_THROW(*tcomm_, 1, e.what());
519  aok = false;
520  }
521 
522  if (aok){
523  RCP<const xcrsMatrix_t> xm =
525  xM_ = rcp_const_cast<xcrsMatrix_t>(xm);
526  }
527  else{
528  if (tcomm_->getRank() == 0)
529  std::cout << "UserInputForTests unable to read matrix." << std::endl;
530  }
531 
532  // Open the coordinate file.
533 
534  fname.str("");
535  fname << path << "/" << testData << "_coord.mtx";
536 
537  size_t coordDim = 0, numGlobalCoords = 0;
538  size_t msg[2]={0,0};
539  ArrayRCP<ArrayRCP<scalar_t> > xyz;
540  std::ifstream coordFile;
541 
542  if (tcomm_->getRank() == 0){
543 
544  if (verbose_)
545  std::cout << "UserInputForTests, Read: " <<
546  fname.str() << std::endl;
547 
548  int fail = 0;
549  try{
550  coordFile.open(fname.str().c_str());
551  }
552  catch (std::exception &e){ // there is no coordinate file
553  fail = 1;
554  }
555 
556  if (!fail){
557 
558  // Read past banner to number and dimension of coordinates.
559 
560  char c[256];
561  bool done=false;
562 
563  while (!done && !fail && coordFile.good()){
564  coordFile.getline(c, 256);
565  if (!c[0])
566  fail = 1;
567  else if (c[0] == '%')
568  continue;
569  else {
570  done=true;
571  std::istringstream s(c);
572  s >> numGlobalCoords >> coordDim;
573  if (!s.eof() || numGlobalCoords < 1 || coordDim < 1)
574  fail=1;
575  }
576  }
577 
578  if (done){
579 
580  // Read in the coordinates.
581 
582  xyz = Teuchos::arcp(new ArrayRCP<scalar_t> [coordDim], 0, coordDim);
583 
584  for (size_t dim=0; !fail && dim < coordDim; dim++){
585  size_t idx;
586  scalar_t *tmp = new scalar_t [numGlobalCoords];
587  if (!tmp)
588  fail = 1;
589  else{
590  xyz[dim] = Teuchos::arcp(tmp, 0, numGlobalCoords);
591 
592  for (idx=0; !coordFile.eof() && idx < numGlobalCoords; idx++){
593  coordFile.getline(c, 256);
594  std::istringstream s(c);
595  s >> tmp[idx];
596  }
597 
598  if (idx < numGlobalCoords)
599  fail = 1;
600  }
601  }
602 
603  if (fail){
604  ArrayRCP<scalar_t> emptyArray;
605  for (size_t dim=0; dim < coordDim; dim++)
606  xyz[dim] = emptyArray; // free the memory
607 
608  coordDim = 0;
609  }
610  }
611  else{
612  fail = 1;
613  }
614 
615  coordFile.close();
616  }
617 
618  msg[0] = coordDim;
619  msg[1] = numGlobalCoords;
620  }
621 
622  // Broadcast coordinate dimension
623  Teuchos::broadcast<int, size_t>(*tcomm_, 0, 2, msg);
624 
625  coordDim = msg[0];
626  numGlobalCoords= msg[1];
627 
628  if (coordDim == 0)
629  return;
630 
631  gno_t base;
632  RCP<const map_t> toMap;
633 
634  if (!M_.is_null()){
635  base = M_->getIndexBase();
636  const RCP<const map_t> &mapM = M_->getRowMap();
637  toMap = mapM;
638  }
639  else{
640  base = 0;
641  toMap = rcp(new map_t(numGlobalCoords, base, tcomm_));
642  }
643 
644  // Export coordinates to their owners
645 
646  xyz_ = rcp(new tMVector_t(toMap, coordDim));
647 
648  ArrayRCP<ArrayView<const scalar_t> > coordLists(coordDim);
649 
650  if (tcomm_->getRank() == 0){
651 
652  for (size_t dim=0; dim < coordDim; dim++)
653  coordLists[dim] = xyz[dim].view(0, numGlobalCoords);
654 
655  gno_t *tmp = new gno_t [numGlobalCoords];
656  if (!tmp)
657  throw std::bad_alloc();
658 
659  ArrayRCP<const gno_t> rowIds = Teuchos::arcp(tmp, 0, numGlobalCoords);
660 
661  gno_t basePlusNumGlobalCoords = base+numGlobalCoords;
662  for (gno_t id=base; id < basePlusNumGlobalCoords; id++)
663  *tmp++ = id;
664 
665  RCP<const map_t> fromMap = rcp(new map_t(numGlobalCoords,
666  rowIds.view(0, numGlobalCoords), base, tcomm_));
667 
668  tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
669 
670  export_t exporter(fromMap, toMap);
671 
672  xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
673  }
674  else{
675 
676  RCP<const map_t> fromMap = rcp(new map_t(numGlobalCoords,
677  ArrayView<gno_t>(), base, tcomm_));
678 
679  tMVector_t allCoords(fromMap, coordLists.view(0, coordDim), coordDim);
680 
681  export_t exporter(fromMap, toMap);
682 
683  xyz_->doExport(allCoords, exporter, Tpetra::INSERT);
684  }
685 }
686 
687 void UserInputForTests::buildCrsMatrix(int xdim, int ydim, int zdim,
688  string problemType)
689 {
690  Teuchos::CommandLineProcessor tclp;
691  Galeri::Xpetra::Parameters<gno_t> params(tclp,
692  xdim, ydim, zdim, problemType);
693 
694  RCP<const Tpetra::Map<lno_t, gno_t> > map =
695  rcp(new Tpetra::Map<lno_t, gno_t>(
696  params.GetNumGlobalElements(), 0, tcomm_));
697 
698  if (verbose_ && tcomm_->getRank() == 0){
699  std::cout << "UserInputForTests, Create matrix with " << problemType;
700  std::cout << " (and" << xdim;
701  if (zdim > 0)
702  std::cout << " x " << ydim << " x " << zdim << std::endl;
703  else if (ydim > 0)
704  std::cout << " x" << ydim << " x 1" << std::endl;
705  else
706  std::cout << "x 1 x 1" << std::endl;
707 
708  std::cout << " mesh)" << std::endl;
709  }
710 
711  try{
712  RCP<Galeri::Xpetra::Problem<Tpetra::Map<lno_t, gno_t>, Tpetra::CrsMatrix<scalar_t, lno_t, gno_t>, Tpetra::MultiVector<scalar_t, lno_t, gno_t> > > Pr =
713  Galeri::Xpetra::BuildProblem<scalar_t, lno_t, gno_t, Tpetra::Map<lno_t, gno_t>, Tpetra::CrsMatrix<scalar_t, lno_t, gno_t>, Tpetra::MultiVector<scalar_t, lno_t, gno_t> >
714  (params.GetMatrixType(), map, params.GetParameterList());
715  M_ = Pr->BuildMatrix();
716  }
717  catch (std::exception &e) { // Probably not enough memory
718  TEST_FAIL_AND_THROW(*tcomm_, 1, e.what());
719  }
720 
721  RCP<const xcrsMatrix_t> xm =
723  xM_ = rcp_const_cast<xcrsMatrix_t>(xm);
724 
725  // Compute the coordinates for the matrix rows.
726 
727  if (verbose_ && tcomm_->getRank() == 0)
728  std::cout <<
729  "UserInputForTests, Implied matrix row coordinates computed" <<
730  std::endl;
731 
732  ArrayView<const gno_t> gids = map->getNodeElementList();
733  lno_t count = gids.size();
734  int dim = 3;
735  size_t pos = problemType.find("2D");
736  if (pos != string::npos)
737  dim = 2;
738  else if (problemType == string("Laplace1D") ||
739  problemType == string("Identity"))
740  dim = 1;
741 
742  Array<ArrayRCP<scalar_t> > coordinates(dim);
743 
744  if (count > 0){
745  for (int i=0; i < dim; i++){
746  scalar_t *c = new scalar_t [count];
747  if (!c)
748  throw(std::bad_alloc());
749  coordinates[i] = Teuchos::arcp(c, 0, count, true);
750  }
751 
752  if (dim==3){
753  scalar_t *x = coordinates[0].getRawPtr();
754  scalar_t *y = coordinates[1].getRawPtr();
755  scalar_t *z = coordinates[2].getRawPtr();
756  gno_t xySize = xdim * ydim;
757  for (lno_t i=0; i < count; i++){
758  gno_t iz = gids[i] / xySize;
759  gno_t xy = gids[i] - iz*xySize;
760  z[i] = scalar_t(iz);
761  y[i] = scalar_t(xy / xdim);
762  x[i] = scalar_t(xy % xdim);
763  }
764  }
765  else if (dim==2){
766  scalar_t *x = coordinates[0].getRawPtr();
767  scalar_t *y = coordinates[1].getRawPtr();
768  for (lno_t i=0; i < count; i++){
769  y[i] = scalar_t(gids[i] / xdim);
770  x[i] = scalar_t(gids[i] % xdim);
771  }
772  }
773  else{
774  scalar_t *x = coordinates[0].getRawPtr();
775  for (lno_t i=0; i < count; i++)
776  x[i] = scalar_t(gids[i]);
777  }
778  }
779 
780  Array<ArrayView<const scalar_t> > coordView(dim);
781  if (count > 0)
782  for (int i=0; i < dim; i++)
783  coordView[i] = coordinates[i].view(0,count);
784 
785  xyz_ = rcp(new tMVector_t(map, coordView.view(0, dim), dim));
786 }
787 
788 void UserInputForTests::readZoltanTestData(string path, string testData)
789 {
790  int rank = tcomm_->getRank();
791  FILE *graphFile = NULL;
792  FILE *coordFile = NULL;
793  int fileInfo[2];
794 
795  if (rank == 0){
796  std::ostringstream chGraphFileName;
797  chGraphFileName << path << "/ch_" << testData << "/" << testData << ".graph";
798  std::ostringstream chCoordFileName;
799  chCoordFileName << path << "/ch_" << testData << "/" << testData << ".coords";
800  memset(fileInfo, 0, sizeof(int) * 2);
801 
802  graphFile = fopen(chGraphFileName.str().c_str(), "r");
803  if (graphFile){
804  fileInfo[0] = 1;
805  if (verbose_ && tcomm_->getRank() == 0)
806  std::cout << "UserInputForTests, open " <<
807  chGraphFileName << std::endl;
808 
809  coordFile = fopen(chCoordFileName.str().c_str(), "r");
810  if (coordFile){
811  fileInfo[1] = 1;
812  if (verbose_ && tcomm_->getRank() == 0)
813  std::cout << "UserInputForTests, open " <<
814  chCoordFileName << std::endl;
815  }
816  }
817  }
818 
819  Teuchos::broadcast<int, int>(*tcomm_, 0, 2, fileInfo);
820 
821  bool haveGraph = (fileInfo[0] == 1);
822  bool haveCoords = (fileInfo[1] == 1);
823 
824  if (haveGraph){
825  // Writes M_, vtxWeights_, and edgWeights_ and closes file.
826  try{
827  getChacoGraph(graphFile, testData);
828  }
830 
831  if (haveCoords){
832  // Writes xyz_ and closes the file.
833  try{
834  getChacoCoords(coordFile, testData);
835  }
837  }
838  }
839 
840  RCP<const xcrsMatrix_t> xm =
842  xM_ = rcp_const_cast<xcrsMatrix_t>(xm);
843 }
844 
845 void UserInputForTests::getChacoGraph(FILE *fptr, string fname)
846 {
847  int rank = tcomm_->getRank();
848  int graphCounts[5];
849  int nvtxs=0, nedges=0;
850  int vwgt_dim=0, ewgt_dim=0;
851  int *start = NULL, *adj = NULL;
852  float *ewgts = NULL, *vwgts = NULL;
853  size_t *nzPerRow = NULL;
854  size_t maxRowLen = 0;
855  gno_t base = 0;
856  ArrayRCP<const size_t> rowSizes;
857  int fail = 0;
858  bool haveEdges = true;
859 
860  if (rank == 0){
861 
862  memset(graphCounts, 0, 5*sizeof(int));
863 
864  // This function is in the Zoltan C-library.
865 
866  // Reads in the file and closes it when done.
867  char *nonConstName = new char [fname.size() + 1];
868  strcpy(nonConstName, fname.c_str());
869 
870  fail = Zoltan2::chaco_input_graph(fptr, nonConstName,
871  &start, &adj, &nvtxs, &vwgt_dim, &vwgts, &ewgt_dim, &ewgts);
872  delete [] nonConstName;
873 
874  // There are Zoltan2 test graphs that have no edges.
875 
876  if (start == NULL)
877  haveEdges = false;
878 
879  if (verbose_){
880  std::cout << "UserInputForTests, " << nvtxs << " vertices,";
881  if (haveEdges)
882  std::cout << start[nvtxs] << " edges,";
883  else
884  std::cout << "no edges,";
885  std::cout << vwgt_dim << " vertex weights, ";
886  std::cout << ewgt_dim << " edge weights" << std::endl;
887  }
888 
889  if (nvtxs==0)
890  fail = true;
891 
892  if (fail){
893  Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
894  throw std::runtime_error("Unable to read chaco file");
895  }
896 
897  if (haveEdges)
898  nedges = start[nvtxs];
899 
900  nzPerRow = new size_t [nvtxs];
901  if (!nzPerRow)
902  throw std::bad_alloc();
903  rowSizes = arcp(nzPerRow, 0, nvtxs, true);
904 
905  if (haveEdges){
906  for (int i=0; i < nvtxs; i++){
907  nzPerRow[i] = start[i+1] - start[i];
908  if (nzPerRow[i] > maxRowLen)
909  maxRowLen = nzPerRow[i];
910  }
911  }
912  else{
913  memset(nzPerRow, 0, sizeof(size_t) * nvtxs);
914  }
915 
916  if (haveEdges){
917  free(start);
918  start = NULL;
919  }
920 
921  // Make sure base gid is zero.
922 
923  if (nedges){
924  int chbase = adj[0];
925  for (int i=1; i < nedges; i++)
926  if (adj[i] < chbase)
927  chbase = adj[i];
928 
929  if (chbase > 0){
930  for (int i=0; i < nedges; i++)
931  adj[i] -= chbase;
932  }
933  }
934 
935  graphCounts[0] = nvtxs;
936  graphCounts[1] = nedges;
937  graphCounts[2] = vwgt_dim;
938  graphCounts[3] = ewgt_dim;
939  graphCounts[4] = maxRowLen; // size_t maxRowLen will fit; it is <= (int-int)
940  }
941 
942  Teuchos::broadcast<int, int>(*tcomm_, 0, 5, graphCounts);
943 
944  if (graphCounts[0] == 0)
945  throw std::runtime_error("Unable to read chaco file");
946 
947  haveEdges = (graphCounts[1] > 0);
948 
949  RCP<tcrsMatrix_t> fromMatrix;
950  RCP<const map_t> fromMap;
951 
952  // Create a Tpetra::CrsMatrix where rank 0 has entire matrix.
953 
954  if (rank == 0){
955  fromMap = rcp(new map_t(nvtxs, nvtxs, base, tcomm_));
956 
957  fromMatrix =
958  rcp(new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
959 
960  if (haveEdges){
961 
962  gno_t *edgeIds = new gno_t [nedges];
963  if (nedges && !edgeIds)
964  throw std::bad_alloc();
965  for (int i=0; i < nedges; i++)
966  edgeIds[i] = adj[i];
967 
968  free(adj);
969  adj = NULL;
970 
971  gno_t *nextId = edgeIds;
972  Array<scalar_t> values(maxRowLen, 1.0);
973 
974  for (gno_t i=0; i < nvtxs; i++){
975  if (nzPerRow[i] > 0){
976  ArrayView<const gno_t> rowNz(nextId, nzPerRow[i]);
977  fromMatrix->insertGlobalValues(i, rowNz, values.view(0,nzPerRow[i]));
978  nextId += nzPerRow[i];
979  }
980  }
981 
982  delete [] edgeIds;
983  edgeIds = NULL;
984  }
985 
986  fromMatrix->fillComplete();
987  }
988  else{
989  nvtxs = graphCounts[0];
990  nedges = graphCounts[1];
991  vwgt_dim = graphCounts[2];
992  ewgt_dim = graphCounts[3];
993  maxRowLen = graphCounts[4];
994 
995  // Create a Tpetra::CrsMatrix where rank 0 has entire matrix.
996 
997  fromMap = rcp(new map_t(nvtxs, 0, base, tcomm_));
998 
999  fromMatrix =
1000  rcp(new tcrsMatrix_t(fromMap, rowSizes, Tpetra::StaticProfile));
1001 
1002  fromMatrix->fillComplete();
1003  }
1004 
1005  // Create a Tpetra::CrsMatrix with default row distribution.
1006 
1007  RCP<const map_t> toMap = rcp(new map_t(nvtxs, base, tcomm_));
1008  RCP<tcrsMatrix_t> toMatrix = rcp(new tcrsMatrix_t(toMap, maxRowLen));
1009 
1010  // Import the data.
1011 
1012  RCP<import_t> importer = rcp(new import_t(fromMap, toMap));
1013  toMatrix->doImport(*fromMatrix, *importer, Tpetra::INSERT);
1014  toMatrix->fillComplete();
1015 
1016  M_ = toMatrix;
1017 
1018  // Vertex weights, if any
1019 
1020  typedef ArrayRCP<const ArrayView<const scalar_t> > arrayArray_t;
1021 
1022  if (vwgt_dim > 0){
1023 
1024  ArrayRCP<scalar_t> weightBuf;
1025  ArrayView<const scalar_t> *wgts = new ArrayView<const scalar_t> [vwgt_dim];
1026 
1027  if (rank == 0){
1028  size_t len = vwgt_dim * nvtxs;
1029  scalar_t *buf = new scalar_t [len];
1030  if (!buf) throw std::bad_alloc();
1031  weightBuf = arcp(buf, 0, len, true);
1032 
1033  for (int wdim=0; wdim < vwgt_dim; wdim++){
1034  wgts[wdim] = ArrayView<const scalar_t>(buf, nvtxs);
1035  float *vw = vwgts + wdim;
1036  for (int i=0; i < nvtxs; i++, vw += vwgt_dim)
1037  buf[i] = *vw;
1038  buf += nvtxs;
1039  }
1040 
1041  free(vwgts);
1042  vwgts = NULL;
1043  }
1044 
1045  arrayArray_t vweights = arcp(wgts, 0, vwgt_dim, true);
1046 
1047  RCP<tMVector_t> fromVertexWeights =
1048  rcp(new tMVector_t(fromMap, vweights.view(0, vwgt_dim), vwgt_dim));
1049 
1050  RCP<tMVector_t> toVertexWeights = rcp(new tMVector_t(toMap, vwgt_dim));
1051 
1052  toVertexWeights->doImport(*fromVertexWeights, *importer, Tpetra::INSERT);
1053 
1054  vtxWeights_ = toVertexWeights;
1055  }
1056 
1057  // Edge weights, if any
1058 
1059  if (haveEdges && ewgt_dim > 0){
1060 
1061  ArrayRCP<scalar_t> weightBuf;
1062  ArrayView<const scalar_t> *wgts = new ArrayView<const scalar_t> [ewgt_dim];
1063 
1064  toMap = rcp(new map_t(nedges, M_->getNodeNumEntries(), base, tcomm_));
1065 
1066  if (rank == 0){
1067  size_t len = ewgt_dim * nedges;
1068  scalar_t *buf = new scalar_t [len];
1069  if (!buf) throw std::bad_alloc();
1070  weightBuf = arcp(buf, 0, len, true);
1071 
1072  for (int wdim=0; wdim < ewgt_dim; wdim++){
1073  wgts[wdim] = ArrayView<const scalar_t>(buf, nedges);
1074  float *ew = ewgts + wdim;
1075  for (int i=0; i < nedges; i++, ew += ewgt_dim)
1076  buf[i] = *ew;
1077  buf += nedges;
1078  }
1079 
1080  free(ewgts);
1081  ewgts = NULL;
1082  fromMap = rcp(new map_t(nedges, nedges, base, tcomm_));
1083  }
1084  else{
1085  fromMap = rcp(new map_t(nedges, 0, base, tcomm_));
1086  }
1087 
1088  arrayArray_t eweights = arcp(wgts, 0, ewgt_dim, true);
1089 
1090  RCP<tMVector_t> fromEdgeWeights =
1091  rcp(new tMVector_t(fromMap, eweights.view(0, ewgt_dim), ewgt_dim));
1092 
1093  RCP<tMVector_t> toEdgeWeights = rcp(new tMVector_t(toMap, ewgt_dim));
1094 
1095  RCP<import_t> edgeImporter = rcp(new import_t(fromMap, toMap));
1096 
1097  toEdgeWeights->doImport(*fromEdgeWeights, *edgeImporter, Tpetra::INSERT);
1098 
1099  edgWeights_ = toEdgeWeights;
1100  }
1101 }
1102 
1103 void UserInputForTests::getChacoCoords(FILE *fptr, string fname)
1104 {
1105  int rank = tcomm_->getRank();
1106  int ndim=0;
1107  float *x=NULL, *y=NULL, *z=NULL;
1108  int fail = 0;
1109 
1110  size_t localNumVtx = M_->getNodeNumRows();
1111  size_t globalNumVtx = M_->getGlobalNumRows();
1112 
1113  if (rank == 0){
1114 
1115  // This function is in the Zoltan C-library.
1116 
1117  // Reads in the file and closes it when done.
1118  char *nonConstName = new char [fname.size() + 1];
1119  strcpy(nonConstName, fname.c_str());
1120  fail = Zoltan2::chaco_input_geom(fptr, nonConstName, globalNumVtx,
1121  &ndim, &x, &y, &z);
1122  delete [] nonConstName;
1123 
1124  if (fail)
1125  ndim = 0;
1126 
1127  if (verbose_){
1128  std::cout << "UserInputForTests, read " << globalNumVtx;
1129  std::cout << " " << ndim << "-dimensional coordinates." << std::endl;
1130  }
1131  }
1132 
1133  Teuchos::broadcast<int, int>(*tcomm_, 0, 1, &ndim);
1134 
1135  if (ndim == 0)
1136  throw std::runtime_error("Can't read coordinate file");
1137 
1138  ArrayRCP<ArrayRCP<const scalar_t> > coords(ndim);
1139  lno_t len = 0;
1140 
1141  if (rank == 0){
1142 
1143  for (int dim=0; dim < ndim; dim++){
1144  scalar_t *v = new scalar_t [globalNumVtx];
1145  if (!v)
1146  throw std::bad_alloc();
1147  coords[dim] = arcp<const scalar_t>(v, 0, globalNumVtx, true);
1148  float *val = (dim==0 ? x : (dim==1 ? y : z));
1149  for (size_t i=0; i < globalNumVtx; i++)
1150  v[i] = scalar_t(val[i]);
1151 
1152  free(val);
1153  }
1154 
1155  len = globalNumVtx;;
1156  }
1157 
1158  RCP<const map_t> fromMap = rcp(new map_t(globalNumVtx, len, 0, tcomm_));
1159  RCP<const map_t> toMap = rcp(new map_t(globalNumVtx, localNumVtx, 0, tcomm_));
1160  RCP<import_t> importer = rcp(new import_t(fromMap, toMap));
1161 
1162  Array<ArrayView<const scalar_t> > coordData;
1163  for (int dim=0; dim < ndim; dim++)
1164  coordData.push_back(coords[dim].view(0, len));
1165 
1166  RCP<tMVector_t> fromCoords =
1167  rcp(new tMVector_t(fromMap, coordData.view(0, ndim), ndim));
1168 
1169  RCP<tMVector_t> toCoords = rcp(new tMVector_t(toMap, ndim));
1170 
1171  toCoords->doImport(*fromCoords, *importer, Tpetra::INSERT);
1172 
1173  xyz_ = toCoords;
1174 
1175 }
1176 
1177 #endif
#define TEST_FAIL_AND_THROW(comm, ok, s)
Xpetra::CrsGraph< lno_t, gno_t, node_t > xcrsGraph_t
RCP< xMVector_t > getXpetraMultiVector(int nvec)
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
static void getRandomData(unsigned int seed, lno_t length, scalar_t min, scalar_t max, ArrayView< ArrayRCP< scalar_t > > data)
Generate lists of random scalars.
Tpetra::Map< lno_t, gno_t, node_t > map_t
Tpetra::Vector< scalar_t, lno_t, gno_t, node_t > tVector_t
RCP< tMVector_t > getEdgeWeights()
A class that generates typical user input for testing.
RCP< tMVector_t > getTpetraMultiVector(int nvec)
Traits of Xpetra classes, including migration method.
RCP< xcrsGraph_t > getXpetraCrsGraph()
#define HAVE_EPETRA_DATA_TYPES
Tpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > tMVector_t
Tpetra::Import< lno_t, gno_t, node_t > import_t
RCP< xcrsMatrix_t > getXpetraCrsMatrix()
Xpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > xMVector_t
RCP< tcrsMatrix_t > getTpetraCrsMatrix()
Tpetra::CrsGraph< lno_t, gno_t, node_t > tcrsGraph_t
static RCP< const User > convertToXpetra(const RCP< const User > &a)
Convert the object to its Xpetra wrapped version.
double scalar_t
RCP< tMVector_t > getWeights()
RCP< tMVector_t > getCoordinates()
Tpetra::Export< lno_t, gno_t, node_t > export_t
RCP< tcrsGraph_t > getTpetraCrsGraph()
RCP< tVector_t > getTpetraVector()
int chaco_input_graph(FILE *fin, char *inname, int **start, int **adjacency, int *nvtxs, int *vwgt_dim, float **vweights, int *ewgt_dim, float **eweights)
read a Chaco graph file Reader code was copied from zoltan1 test driver code. File is closed when rea...
Xpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > xcrsMatrix_t
RCP< xVector_t > getXpetraVector()
Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > tcrsMatrix_t
Xpetra::Vector< scalar_t, lno_t, gno_t, node_t > xVector_t
Kokkos::DefaultNode::DefaultNodeType default_node_t
UserInputForTests(string path, string testData, const RCP< const Comm< int > > &c, bool debugInfo=false)
Constructor that reads in a matrix/graph from disk.
int chaco_input_geom(FILE *fingeom, char *geomname, int nvtxs, int *igeom, float **x, float **y, float **z)
read a Chaco coordinates file Reader code was copied from zoltan1 test driver code. File is closed when read is completed.