Zoltan2
AdapterForTests.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 
51 #ifndef ADAPTERFORTESTS
52 #define ADAPTERFORTESTS
53 
54 #include <Zoltan2_Parameters.hpp>
55 #include <UserInputForTests.hpp>
56 
59 
65 
66 #ifdef HAVE_ZOLTAN2_PAMGEN
68 #endif
69 
70 #include <Teuchos_DefaultComm.hpp>
71 #include <Teuchos_XMLObject.hpp>
72 #include <Teuchos_FileInputSource.hpp>
73 
74 #include <Tpetra_MultiVector.hpp>
75 #include <Tpetra_CrsMatrix.hpp>
76 
77 #include <string>
78 #include <iostream>
79 #include <vector>
80 
81 using Teuchos::RCP;
82 using Teuchos::ArrayRCP;
83 using Teuchos::ArrayView;
84 using Teuchos::Array;
85 using Teuchos::Comm;
86 using Teuchos::rcp;
87 using Teuchos::arcp;
88 using Teuchos::rcp_const_cast;
89 using Teuchos::ParameterList;
90 using std::string;
91 using namespace Zoltan2_TestingFramework;
92 
93 // helper struct to store both an adapter and the coordinate adapter
95 {
96  Zoltan2::BaseAdapterRoot * adapter = nullptr; // generic base class
97  EAdapterType adapterType; // convert back to proper adapter type
98 };
99 
101 {
102  AdapterWithTemplateName main; // the main adapter - never null
104 };
105 
106 /* \brief A class for constructing Zoltan2 input adapters */
108 public:
119  UserInputForTests *uinput, const ParameterList &pList,
120  const RCP<const Comm<int> > &comm);
121 
122  ~AdapterFactory(); // handles deleting BaseAdapterRoot * data for adapter
123 
125  return adaptersSet.main.adapter;
126  }
127 
129  return adaptersSet.main.adapterType;
130  }
131 
133  return adaptersSet.coordinate.adapter;
134  }
135 
137  return adaptersSet.coordinate.adapterType;
138  }
139 
140 private:
142 
152  getBasicIdentiferAdapterForInput(UserInputForTests *uinput,
153  const ParameterList &pList, const RCP<const Comm<int> > &comm);
154 
164  getXpetraMVAdapterForInput(UserInputForTests *uinput,
165  const ParameterList &pList, const RCP<const Comm<int> > &comm);
166 
176  getXpetraCrsGraphAdapterForInput(UserInputForTests *uinput,
177  const ParameterList &pList, const RCP<const Comm<int> > &comm);
178 
188  getXpetraCrsMatrixAdapterForInput(UserInputForTests *uinput,
189  const ParameterList &pList, const RCP<const Comm<int> > &comm);
190 
200  getBasicVectorAdapterForInput(UserInputForTests *uinput,
201  const ParameterList &pList, const RCP<const Comm<int> > &comm);
202 
212  getPamgenMeshAdapterForInput(UserInputForTests *uinput,
213  const ParameterList &pList, const RCP<const Comm<int> > &comm);
214 
223  template <typename T>
224  void InitializeVectorData(const RCP<T> &data,
225  std::vector<const zscalar_t *> &coords,
226  std::vector<int> & strides,
227  int stride);
228 
229 #ifdef HAVE_EPETRA_DATA_TYPES
238  template <typename T>
239  void InitializeEpetraVectorData(const RCP<T> &data,
240  std::vector<const zscalar_t *> &coords,
241  std::vector<int> & strides,
242  int stride);
243 #endif
244 };
245 
246 
248  UserInputForTests *uinput,
249  const ParameterList &pList,
250  const RCP<const Comm<int> > &comm)
251 {
252  if(!pList.isParameter("input adapter"))
253  {
254  std::cerr << "Input adapter unspecified" << std::endl;
255  return;
256  }
257 
258  // pick method for chosen adapter
259  std::string input_adapter_name = pList.get<string>("input adapter");
260 
261  if(input_adapter_name == "BasicIdentifier")
262  adaptersSet.main = getBasicIdentiferAdapterForInput(uinput, pList, comm);
263  else if(input_adapter_name == "XpetraMultiVector")
264  adaptersSet.main = getXpetraMVAdapterForInput(uinput, pList, comm);
265  else if(input_adapter_name == "XpetraCrsGraph")
266  adaptersSet = getXpetraCrsGraphAdapterForInput(uinput,pList, comm);
267  else if(input_adapter_name == "XpetraCrsMatrix")
268  adaptersSet = getXpetraCrsMatrixAdapterForInput(uinput,pList, comm);
269  else if(input_adapter_name == "BasicVector")
270  adaptersSet.main = getBasicVectorAdapterForInput(uinput,pList, comm);
271  else if(input_adapter_name == "PamgenMesh")
272  adaptersSet.main = getPamgenMeshAdapterForInput(uinput,pList, comm);
273 
274  if(adaptersSet.main.adapter == nullptr) {
275  throw std::logic_error("AdapterFactory failed to create adapter!");
276  }
277 }
278 
280  if( adaptersSet.main.adapter ) {
281  delete adaptersSet.main.adapter;
282  }
283 
284  if( adaptersSet.coordinate.adapter ) {
285  delete adaptersSet.coordinate.adapter;
286  }
287 }
288 
289 
291  AdapterFactory::getBasicIdentiferAdapterForInput(UserInputForTests *uinput,
292  const ParameterList &pList,
293  const RCP<const Comm<int> > &comm)
294 {
296 
297  if(!pList.isParameter("data type"))
298  {
299  std::cerr << "Input data type unspecified" << std::endl;
300  return result;
301  }
302 
303  string input_type = pList.get<string>("data type"); // get the input type
304 
305  if (!uinput->hasInputDataType(input_type))
306  {
307  std::cerr << "Input type: " + input_type + " unavailable or misspelled."
308  << std::endl; // bad type
309  return result;
310  }
311 
312  std::vector<const zscalar_t *> weights;
313  std::vector<int> weightStrides;
314  const zgno_t *globalIds = NULL;
315  size_t localCount = 0;
316 
317  // get weights if any
318  // get weights if any
319  if(uinput->hasUIWeights())
320  {
321  RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
322  // copy to weight
323  size_t cols = vtx_weights->getNumVectors();
324  for (size_t i = 0; i< cols; i++) {
325  weights.push_back(vtx_weights->getData(i).getRawPtr());
326  weightStrides.push_back((int)vtx_weights->getStride());
327  }
328  }
329 
330  if(input_type == "coordinates")
331  {
332  RCP<tMVector_t> data = uinput->getUICoordinates();
333  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
334  localCount = data->getLocalLength();
335  }
336  else if(input_type == "tpetra_vector")
337  {
338  RCP<tVector_t> data = uinput->getUITpetraVector();
339  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
340  localCount = data->getLocalLength();
341  }
342  else if(input_type == "tpetra_multivector")
343  {
344  int nvec = pList.get<int>("vector_dimension");
345  RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
346  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
347  localCount = data->getLocalLength();
348  }
349  else if(input_type == "tpetra_crs_graph")
350  {
351  RCP<tcrsGraph_t> data = uinput->getUITpetraCrsGraph();
352  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
353  localCount = data->getNodeNumCols();
354  }
355  else if(input_type == "tpetra_crs_matrix")
356  {
357  RCP<tcrsMatrix_t> data = uinput->getUITpetraCrsMatrix();
358  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
359  localCount = data->getNodeNumCols();
360  }
361  else if(input_type == "xpetra_vector")
362  {
363  RCP<xVector_t> data = uinput->getUIXpetraVector();
364  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
365  localCount = data->getLocalLength();
366  }
367  else if(input_type == "xpetra_multivector")
368  {
369  int nvec = pList.get<int>("vector_dimension");
370  RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
371  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
372  localCount = data->getLocalLength();
373  }
374  else if(input_type == "xpetra_crs_graph")
375  {
376  RCP<xcrsGraph_t> data = uinput->getUIXpetraCrsGraph();
377  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
378  localCount = data->getNodeNumCols();
379  }
380  else if(input_type == "xpetra_crs_matrix")
381  {
382  RCP<xcrsMatrix_t> data = uinput->getUIXpetraCrsMatrix();
383  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
384  localCount = data->getNodeNumCols();
385  }
386 #ifdef HAVE_EPETRA_DATA_TYPES
387  else if(input_type == "epetra_vector")
388  {
389  RCP<Epetra_Vector> data = uinput->getUIEpetraVector();
390  globalIds = (zgno_t *)data->Map().MyGlobalElements();
391  localCount = data->MyLength();
392  }
393  else if(input_type == "epetra_multivector")
394  {
395  int nvec = pList.get<int>("vector_dimension");
396  RCP<Epetra_MultiVector> data = uinput->getUIEpetraMultiVector(nvec);
397  globalIds = (zgno_t *)data->Map().MyGlobalElements();
398  localCount = data->MyLength();
399  }
400  else if(input_type == "epetra_crs_graph")
401  {
402  RCP<Epetra_CrsGraph> data = uinput->getUIEpetraCrsGraph();
403  globalIds = (zgno_t *)data->Map().MyGlobalElements();
404  localCount = data->NumMyCols();
405  }
406  else if(input_type == "epetra_crs_matrix")
407  {
408  RCP<Epetra_CrsMatrix> data = uinput->getUIEpetraCrsMatrix();
409  globalIds = (zgno_t *)data->Map().MyGlobalElements();
410  localCount = data->NumMyCols();
411  }
412 #endif
413 
414  result.adapterType = AT_basic_id_t;
415  result.adapter = new Zoltan2_TestingFramework::basic_id_t(zlno_t(localCount),
416  globalIds,
417  weights,weightStrides);
418  return result;
419 }
420 
421 
422 AdapterWithTemplateName AdapterFactory::getXpetraMVAdapterForInput(
423  UserInputForTests *uinput,
424  const ParameterList &pList,
425  const RCP<const Comm<int> > &comm)
426 {
428 
429  if(!pList.isParameter("data type"))
430  {
431  std::cerr << "Input data type unspecified" << std::endl;
432  return result;
433  }
434 
435  string input_type = pList.get<string>("data type");
436  if (!uinput->hasInputDataType(input_type))
437  {
438  std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
439  << std::endl; // bad type
440  return result;
441  }
442 
443  std::vector<const zscalar_t *> weights;
444  std::vector<int> weightStrides;
445 
446  // get weights if any
447  if(uinput->hasUIWeights())
448  {
449  RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
450  // copy to weight
451  size_t weightsPerRow = vtx_weights->getNumVectors();
452  for (size_t i = 0; i< weightsPerRow; i++) {
453  weights.push_back(vtx_weights->getData(i).getRawPtr());
454  weightStrides.push_back(1);
455  }
456  }
457 
458  // set adapter
459  if(input_type == "coordinates")
460  {
461  RCP<tMVector_t> data = uinput->getUICoordinates();
462  RCP<const tMVector_t> const_data = rcp_const_cast<const tMVector_t>(data);
463  if(weights.empty())
464  result.adapter = new xMV_tMV_t(const_data);
465  else {
466  result.adapter = new xMV_tMV_t(const_data,weights,weightStrides);
467  }
468  result.adapterType = AT_xMV_tMV_t;
469  }
470  else if(input_type == "tpetra_multivector")
471  {
472  int nvec = pList.get<int>("vector_dimension");
473  RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
474  RCP<const tMVector_t> const_data = rcp_const_cast<const tMVector_t>(data);
475  if(weights.empty())
476  result.adapter = new xMV_tMV_t(const_data);
477  else
478  result.adapter = new xMV_tMV_t(const_data,weights,weightStrides);
479  result.adapterType = AT_xMV_tMV_t;
480  }
481  else if(input_type == "xpetra_multivector")
482  {
483  int nvec = pList.get<int>("vector_dimension");
484  RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
485  RCP<const xMVector_t> const_data = rcp_const_cast<const xMVector_t>(data);
486  if(weights.empty())
487  result.adapter = new xMV_xMV_t(const_data);
488  else{
489  result.adapter = new xMV_xMV_t(const_data,weights,weightStrides);
490  }
491  result.adapterType = AT_xMV_xMV_t;
492  }
493 #ifdef HAVE_EPETRA_DATA_TYPES
494  else if(input_type == "epetra_multivector")
495  {
496  int nvec = pList.get<int>("vector_dimension");
497  RCP<Epetra_MultiVector> data = uinput->getUIEpetraMultiVector(nvec);
498  RCP<const Epetra_MultiVector> const_data = rcp_const_cast<const Epetra_MultiVector>(data);
499 
500  if(weights.empty())
501  result.adapter = new xMV_eMV_t(const_data);
502  else
503  result.adapter = new xMV_eMV_t(const_data,weights,weightStrides);
504  result.adapterType = AT_xMV_eMV_t;
505  }
506 #endif
507 
508  if(result.adapter == nullptr)
509  std::cerr << "Input data chosen not compatible with xpetra multi-vector adapter." << std::endl;
510 
511  return result;
512 }
513 
514 
515 AdapterWithOptionalCoordinateAdapter AdapterFactory::getXpetraCrsGraphAdapterForInput(
516  UserInputForTests *uinput,
517  const ParameterList &pList,
518  const RCP<const Comm<int> > &comm)
519 {
520 
522 
523  if(!pList.isParameter("data type"))
524  {
525  std::cerr << "Input data type unspecified" << std::endl;
526  return adapters;
527  }
528 
529  string input_type = pList.get<string>("data type");
530  if (!uinput->hasInputDataType(input_type))
531  {
532  std::cerr << "Input type: " + input_type + ", unavailable or misspelled."
533  << std::endl; // bad type
534  return adapters;
535  }
536 
537  std::vector<const zscalar_t *> vtx_weights;
538  std::vector<const zscalar_t *> edge_weights;
539  std::vector<int> vtx_weightStride;
540  std::vector<int> edge_weightStride;
541 
542  // get vtx weights if any
543  if(uinput->hasUIWeights())
544  {
545  RCP<tMVector_t> vtx_weights_tmp = uinput->getUIWeights();
546  // copy to weight
547  size_t weightsPerRow = vtx_weights_tmp->getNumVectors();
548  for (size_t i = 0; i< weightsPerRow; i++) {
549  vtx_weights.push_back(vtx_weights_tmp->getData(i).getRawPtr());
550  vtx_weightStride.push_back(1);
551  }
552  }
553 
554  // get edge weights if any
555  if(uinput->hasUIEdgeWeights())
556  {
557  RCP<tMVector_t> edge_weights_tmp = uinput->getUIEdgeWeights();
558  // copy to weight
559  size_t weightsPerRow = edge_weights_tmp->getNumVectors();
560  for (size_t i = 0; i< weightsPerRow; i++) {
561  edge_weights.push_back(edge_weights_tmp->getData(i).getRawPtr());
562  edge_weightStride.push_back(1);
563  }
564  }
565 
566  // make the coordinate adapter
567  // get an adapter for the coordinates
568  // need to make a copy of the plist and change the vector type
569  Teuchos::ParameterList pCopy(pList);
570  pCopy = pCopy.set<std::string>("data type","coordinates");
571 
572  // for coordinate adapter
573  #define SET_COORDS_INPUT_1(adapterClass) \
574  auto * ca = dynamic_cast<adapterClass*>(adapters.coordinate.adapter); \
575  if(!ca) {throw std::logic_error( "Coordinate adapter case failed!" );} \
576  ia->setCoordinateInput(ca);
577 
578  if(input_type == "tpetra_crs_graph")
579  {
580  RCP<tcrsGraph_t> data = uinput->getUITpetraCrsGraph();
581  RCP<const tcrsGraph_t> const_data = rcp_const_cast<const tcrsGraph_t>(data);
582 
583  xCG_tCG_t * ia = new xCG_tCG_t(const_data,(int)vtx_weights.size(),(int)edge_weights.size());
584  adapters.main.adapterType = AT_xCG_tCG_t;
585  adapters.main.adapter = ia;
586 
587  if(!vtx_weights.empty()) {
588  for(int i = 0; i < (int)vtx_weights.size(); i++)
589  ia->setVertexWeights(vtx_weights[i],vtx_weightStride[i],i);
590  }
591 
592  if(!edge_weights.empty()) {
593  for(int i = 0; i < (int)edge_weights.size(); i++)
594  ia->setEdgeWeights(edge_weights[i],edge_weightStride[i],i);
595  }
596 
597  if (uinput->hasUICoordinates()) {
598  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
600  }
601  }
602  else if(input_type == "xpetra_crs_graph")
603  {
604  RCP<xcrsGraph_t> data = uinput->getUIXpetraCrsGraph();
605  RCP<const xcrsGraph_t> const_data = rcp_const_cast<const xcrsGraph_t>(data);
606 
607  xCG_xCG_t * ia = new xCG_xCG_t(const_data, (int)vtx_weights.size(), (int)edge_weights.size());
608  adapters.main.adapterType = AT_xCG_xCG_t;
609  adapters.main.adapter = ia;
610  if(!vtx_weights.empty())
611  {
612  for(int i = 0; i < (int)vtx_weights.size(); i++)
613  ia->setVertexWeights(vtx_weights[i],vtx_weightStride[i],i);
614  }
615 
616  if(!edge_weights.empty())
617  {
618  for(int i = 0; i < (int)edge_weights.size(); i++)
619  ia->setEdgeWeights(edge_weights[i],edge_weightStride[i],i);
620  }
621 
622  if (uinput->hasUICoordinates()) {
623  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
625  }
626  }
627 #ifdef HAVE_EPETRA_DATA_TYPES
628 
629  else if(input_type == "epetra_crs_graph")
630  {
631  RCP<Epetra_CrsGraph> data = uinput->getUIEpetraCrsGraph();
632  RCP<const Epetra_CrsGraph> const_data = rcp_const_cast<const Epetra_CrsGraph>(data);
633  xCG_eCG_t * ia = new xCG_eCG_t(const_data,(int)vtx_weights.size(),(int)edge_weights.size());
634  adapters.main.adapterType = AT_xCG_eCG_t;
635  adapters.main.adapter = ia;
636  if(!vtx_weights.empty())
637  {
638  for(int i = 0; i < (int)vtx_weights.size(); i++)
639  ia->setVertexWeights(vtx_weights[i],vtx_weightStride[i],i);
640  }
641 
642  if(!edge_weights.empty())
643  {
644  for(int i = 0; i < (int)edge_weights.size(); i++)
645  ia->setEdgeWeights(edge_weights[i],edge_weightStride[i],i);
646  }
647 
648  if (uinput->hasUICoordinates()) {
649  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
651  }
652  }
653 #endif
654 
655  if(adapters.main.adapter == nullptr) {
656  std::cerr << "Input data chosen not compatible with "
657  << "XpetraCrsGraph adapter." << std::endl;
658  return adapters;
659  }
660 
661  return adapters;
662 }
663 
664 
665 AdapterWithOptionalCoordinateAdapter AdapterFactory::getXpetraCrsMatrixAdapterForInput(
666  UserInputForTests *uinput,
667  const ParameterList &pList,
668  const RCP<const Comm<int> > &comm)
669 {
671 
672  if(!pList.isParameter("data type"))
673  {
674  std::cerr << "Input data type unspecified" << std::endl;
675  return adapters;
676  }
677 
678  string input_type = pList.get<string>("data type");
679  if (!uinput->hasInputDataType(input_type))
680  {
681  std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
682  << std::endl; // bad type
683  return adapters;
684  }
685 
686  std::vector<const zscalar_t *> weights;
687  std::vector<int> strides;
688 
689  // get weights if any
690  if(uinput->hasUIWeights())
691  {
692  if(comm->getRank() == 0) std::cout << "Have weights...." << std::endl;
693  RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
694 
695  // copy to weight
696  int weightsPerRow = (int)vtx_weights->getNumVectors();
697  for (int i = 0; i< weightsPerRow; i++)
698  {
699  weights.push_back(vtx_weights->getData(i).getRawPtr());
700  strides.push_back(1);
701  }
702 
703  }
704 
705  // make the coordinate adapter
706  // get an adapter for the coordinates
707  // need to make a copy of the plist and change the vector type
708  Teuchos::ParameterList pCopy(pList);
709  pCopy = pCopy.set<std::string>("data type","coordinates");
710 
711  // for coordinate adapter
712  #define SET_COORDS_INPUT_2(adapterClass) \
713  auto * ca = dynamic_cast<adapterClass*>(adapters.coordinate.adapter); \
714  if(!ca) {throw std::logic_error( "Coordinate adapter case failed!" );} \
715  ia->setCoordinateInput(ca);
716 
717  // set adapter
718  if(input_type == "tpetra_crs_matrix")
719  {
720  if(comm->getRank() == 0) std::cout << "Make tpetra crs matrix adapter...." << std::endl;
721 
722  // get pointer to data
723  RCP<tcrsMatrix_t> data = uinput->getUITpetraCrsMatrix();
724  RCP<const tcrsMatrix_t> const_data = rcp_const_cast<const tcrsMatrix_t>(data); // const cast data
725 
726  // new adapter
727  xCM_tCM_t *ia = new xCM_tCM_t(const_data, (int)weights.size());
728  adapters.main.adapterType = AT_xCM_tCM_t;
729  adapters.main.adapter = ia;
730 
731  // if we have weights set them
732  if(!weights.empty())
733  {
734  for(int i = 0; i < (int)weights.size(); i++)
735  ia->setWeights(weights[i],strides[i],i);
736  }
737 
738  if (uinput->hasUICoordinates()) {
739  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
741  }
742  }
743  else if(input_type == "xpetra_crs_matrix")
744  {
745  RCP<xcrsMatrix_t> data = uinput->getUIXpetraCrsMatrix();
746  RCP<const xcrsMatrix_t> const_data = rcp_const_cast<const xcrsMatrix_t>(data);
747 
748  // new adapter
749  xCM_xCM_t *ia = new xCM_xCM_t(const_data, (int)weights.size());
750  adapters.main.adapterType = AT_xCM_xCM_t;
751  adapters.main.adapter = ia;
752 
753  // if we have weights set them
754  if(!weights.empty())
755  {
756  for(int i = 0; i < (int)weights.size(); i++)
757  ia->setWeights(weights[i],strides[i],i);
758  }
759 
760  if (uinput->hasUICoordinates()) {
761  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
763  }
764  }
765 #ifdef HAVE_EPETRA_DATA_TYPES
766  else if(input_type == "epetra_crs_matrix")
767  {
768  RCP<Epetra_CrsMatrix> data = uinput->getUIEpetraCrsMatrix();
769  RCP<const Epetra_CrsMatrix> const_data = rcp_const_cast<const Epetra_CrsMatrix>(data);
770 
771  // new adapter
772  xCM_eCM_t *ia = new xCM_eCM_t(const_data, (int)weights.size());
773  adapters.main.adapterType = AT_xCM_eCM_t;
774  adapters.main.adapter = ia;
775 
776  // if we have weights set them
777  if(!weights.empty())
778  {
779  for(int i = 0; i < (int)weights.size(); i++)
780  ia->setWeights(weights[i],strides[i],i);
781  }
782 
783  if (uinput->hasUICoordinates()) {
784  adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
786  }
787  }
788 #endif
789 
790  if(adapters.main.adapter == nullptr)
791  {
792  std::cerr << "Input data chosen not compatible with "
793  << "XpetraCrsMatrix adapter." << std::endl;
794  return adapters;
795  }
796 
797  return adapters;
798 }
799 
800 AdapterWithTemplateName AdapterFactory::getBasicVectorAdapterForInput(
801  UserInputForTests *uinput,
802  const ParameterList &pList,
803  const RCP<const Comm<int> > &comm)
804 {
805 
807 
808  if(!pList.isParameter("data type"))
809  {
810  std::cerr << "Input data type unspecified" << std::endl;
811  return result;
812  }
813 
814  string input_type = pList.get<string>("data type");
815  if (!uinput->hasInputDataType(input_type))
816  {
817  std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
818  << std::endl; // bad type
819  return result;
820  }
821 
822  std::vector<const zscalar_t *> weights;
823  std::vector<int> weightStrides;
824  const zgno_t * globalIds;
825  zlno_t localCount = 0;
826 
827  // get weights if any
828  // get weights if any
829  if(uinput->hasUIWeights())
830  {
831  RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
832  // copy to weight
833  size_t cols = vtx_weights->getNumVectors();
834  for (size_t i = 0; i< cols; i++) {
835  weights.push_back(vtx_weights->getData(i).getRawPtr());
836  weightStrides.push_back(1);
837  }
838  }
839 
840  // get vector stride
841  int stride = 1;
842  if(pList.isParameter("stride"))
843  stride = pList.get<int>("stride");
844 
846 
847  if(input_type == "coordinates")
848  {
849  RCP<tMVector_t> data = uinput->getUICoordinates();
850  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
851  localCount = static_cast<zlno_t>(data->getLocalLength());
852 
853  // get strided data
854  std::vector<const zscalar_t *> coords;
855  std::vector<int> entry_strides;
856  InitializeVectorData(data,coords,entry_strides,stride);
857 
858 
859 
860  if (weights.empty()) {
861  size_t dim = coords.size(); //BDD add NULL for constructor call
862  size_t push_null = 3-dim;
863  for (size_t i = 0; i < push_null; i ++) coords.push_back(NULL);
865  zlno_t(localCount),
866  globalIds,
867  coords[0],
868  coords[1],coords[2],
869  stride, stride, stride);
870  } else if (weights.size() == 1) {
871  size_t dim = coords.size(); //BDD add NULL for constructor call
872  size_t push_null = 3-dim;
873  for (size_t i = 0; i < push_null; i ++) coords.push_back(NULL);
875  zlno_t(localCount),
876  globalIds,
877  coords[0],
878  coords[1],coords[2],
879  stride, stride, stride,
880  true,
881  weights[0],
882  weightStrides[0]);
883  } else { // More than one weight per ID
885  zlno_t(localCount),
886  globalIds,
887  coords, entry_strides,
888  weights, weightStrides);
889  }
890  }
891  else if(input_type == "tpetra_vector")
892  {
893  RCP<tVector_t> data = uinput->getUITpetraVector();
894  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
895  localCount = static_cast<zlno_t>(data->getLocalLength());
896 
897  // get strided data
898  std::vector<const zscalar_t *> coords;
899  std::vector<int> entry_strides;
900  InitializeVectorData(data,coords,entry_strides,stride);
901 
902  if(weights.empty())
903  {
904  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
905  coords[0], entry_strides[0]);
906  }else{
907  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
908  coords[0], entry_strides[0],
909  true,
910  weights[0],
911  weightStrides[0]);
912 
913  }
914 
915  }
916  else if(input_type == "tpetra_multivector")
917  {
918  int nvec = pList.get<int>("vector_dimension");
919 
920  RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
921  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
922  localCount = static_cast<zlno_t>(data->getLocalLength());
923 
924  // get strided data
925  std::vector<const zscalar_t *> coords;
926  std::vector<int> entry_strides;
927  InitializeVectorData(data,coords,entry_strides,stride);
928 
929  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
930  coords, entry_strides,
931  weights,weightStrides);
932 
933  }
934  else if(input_type == "xpetra_vector")
935  {
936  RCP<xVector_t> data = uinput->getUIXpetraVector();
937  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
938  localCount = static_cast<zlno_t>(data->getLocalLength());
939 
940  // get strided data
941  std::vector<const zscalar_t *> coords;
942  std::vector<int> entry_strides;
943  InitializeVectorData(data,coords,entry_strides,stride);
944 
945  if(weights.empty())
946  {
947  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
948  coords[0], entry_strides[0]);
949  }else{
950  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
951  coords[0], entry_strides[0],
952  true,
953  weights[0],
954  weightStrides[0]);
955 
956  }
957  }
958  else if(input_type == "xpetra_multivector")
959  {
960  int nvec = pList.get<int>("vector_dimension");
961  RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
962  globalIds = (zgno_t *)data->getMap()->getNodeElementList().getRawPtr();
963  localCount = static_cast<zlno_t>(data->getLocalLength());
964 
965  // get strided data
966  std::vector<const zscalar_t *> coords;
967  std::vector<int> entry_strides;
968  InitializeVectorData(data,coords,entry_strides,stride);
969  if(comm->getRank() == 0) std::cout << "size of entry strides: " << entry_strides.size() << std::endl;
970  if(comm->getRank() == 0) std::cout << "size of coords: " << coords.size() << std::endl;
971 
972  // make vector!
973  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
974  coords, entry_strides,
975  weights,weightStrides);
976  }
977 
978 #ifdef HAVE_EPETRA_DATA_TYPES
979  else if(input_type == "epetra_vector")
980  {
981  RCP<Epetra_Vector> data = uinput->getUIEpetraVector();
982  globalIds = (zgno_t *)data->Map().MyGlobalElements();
983  localCount = static_cast<zlno_t>(data->MyLength());
984 
985  // get strided data
986  std::vector<const zscalar_t *> coords;
987  std::vector<int> entry_strides;
988  InitializeEpetraVectorData(data,coords,entry_strides,stride);
989  if(weights.empty())
990  {
991  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
992  coords[0], entry_strides[0]);
993  }else{
994  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
995  coords[0], entry_strides[0],
996  true,
997  weights[0],
998  weightStrides[0]);
999 
1000  }
1001 
1002  // delete [] epetravectors;
1003  }
1004  else if(input_type == "epetra_multivector")
1005  {
1006  int nvec = pList.get<int>("vector_dimension");
1007  RCP<Epetra_MultiVector> data = uinput->getUIEpetraMultiVector(nvec);
1008  globalIds = (zgno_t *)data->Map().MyGlobalElements();
1009  localCount = data->MyLength();
1010 
1011  std::vector<const zscalar_t *> coords;
1012  std::vector<int> entry_strides;
1013  InitializeEpetraVectorData(data,coords,entry_strides,stride);
1014 
1015  // make vector!
1016  result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
1017  coords, entry_strides,
1018  weights,weightStrides);
1019  }
1020 
1021 #endif
1022 
1023  return result;
1024 }
1025 
1026 template <typename T>
1027 void AdapterFactory::InitializeVectorData(const RCP<T> &data,
1028  std::vector<const zscalar_t *> &coords,
1029  std::vector<int> & strides,
1030  int stride)
1031 {
1032  // set up adapter data
1033  size_t localCount = data->getLocalLength();
1034  size_t nvecs = data->getNumVectors();
1035  size_t vecsize = data->getNumVectors() * data->getLocalLength();
1036 // printf("Number of vectors by data: %zu\n", nvecs);
1037  // printf("Size of data: %zu\n", vecsize);
1038 
1039  ArrayRCP<zscalar_t> *petravectors = new ArrayRCP<zscalar_t>[nvecs];
1040 
1041  // printf("Getting t-petra vectors...\n");
1042  for (size_t i = 0; i < nvecs; i++)
1043  petravectors[i] = data->getDataNonConst(i);
1044 
1045  // debugging
1046  // for (size_t i = 0; i < nvecs; i++){
1047  // printf("Tpetra vector %zu: {",i);
1048  //
1049  // for (size_t j = 0; j < localCount; j++)
1050  // {
1051  // printf("%1.2g ",petravectors[i][j]);
1052  // }
1053  // printf("}\n");
1054  // }
1055 
1056  size_t idx = 0;
1057  zscalar_t *coordarr = new zscalar_t[vecsize];
1058 
1059  if(stride == 1 || stride != (int)nvecs)
1060  {
1061  for (size_t i = 0; i < nvecs; i++) {
1062  for (size_t j = 0; j < localCount; j++) {
1063  coordarr[idx++] = petravectors[i][j];
1064  }
1065  }
1066  }else
1067  {
1068  for (size_t j = 0; j < localCount; j++) {
1069  for (size_t i = 0; i < nvecs; i++) {
1070  coordarr[idx++] = petravectors[i][j];
1071  }
1072  }
1073  }
1074 
1075  // debugging
1076  // printf("Made coordarr : {");
1077  // for (zlno_t i = 0; i < vecsize; i++){
1078  // printf("%1.2g ",coordarr[i]);
1079  // }
1080  // printf("}\n");
1081 
1082  // always build for dim 3
1083  coords = std::vector<const zscalar_t *>(nvecs);
1084  strides = std::vector<int>(nvecs);
1085 
1086  for (size_t i = 0; i < nvecs; i++) {
1087  if(stride == 1)
1088  coords[i] = &coordarr[i*localCount];
1089  else
1090  coords[i] = &coordarr[i];
1091 
1092  strides[i] = stride;
1093  }
1094 
1095  // debugging
1096  // printf("Made coords...\n");
1097  // for (size_t i = 0; i < nvecs; i++){
1098  // const zscalar_t * tmp = coords[i];
1099  // printf("coord %zu: {",i);
1100  // for(size_t j = 0; j < localCount; j++)
1101  // {
1102  // printf("%1.2g ", tmp[j]);
1103  // }
1104  // printf("}\n");
1105  // }
1106 
1107  // printf("clean up coordarr and tpetravectors...\n\n\n");
1108  delete [] petravectors;
1109 }
1110 
1111 #ifdef HAVE_EPETRA_DATA_TYPES
1112 
1113 template <typename T>
1114 void AdapterFactory::InitializeEpetraVectorData(const RCP<T> &data,
1115  std::vector<const zscalar_t *> &coords,
1116  std::vector<int> & strides,
1117  int stride){
1118  size_t localCount = data->MyLength();
1119  size_t nvecs = data->NumVectors();
1120  size_t vecsize = nvecs * localCount;
1121 
1122  // printf("Number of vectors by data: %zu\n", nvecs);
1123  // printf("Size of data: %zu\n", vecsize);
1124 
1125  std::vector<zscalar_t *> epetravectors(nvecs);
1126  zscalar_t ** arr;
1127  // printf("get data from epetra vector..\n");
1128  data->ExtractView(&arr);
1129 
1130  for(size_t k = 0; k < nvecs; k++)
1131  {
1132  epetravectors[k] = arr[k];
1133  }
1134 
1135  size_t idx = 0;
1136  basic_vector_adapter::scalar_t *coordarr =
1137  new basic_vector_adapter::scalar_t[vecsize];
1138 
1139  if(stride == 1 || stride != (int)nvecs)
1140  {
1141  for (size_t i = 0; i < nvecs; i++) {
1142  for (size_t j = 0; j < localCount; j++) {
1143  coordarr[idx++] = epetravectors[i][j];
1144  }
1145  }
1146  }else
1147  {
1148  for (size_t j = 0; j < localCount; j++) {
1149  for (size_t i = 0; i < nvecs; i++) {
1150  coordarr[idx++] = epetravectors[i][j];
1151  }
1152  }
1153  }
1154 
1155  // debugging
1156 // printf("Made coordarr : {");
1157 // for (zlno_t i = 0; i < vecsize; i++){
1158 // printf("%1.2g ",coordarr[i]);
1159 // }
1160 // printf("}\n");
1161 
1162  coords = std::vector<const zscalar_t *>(nvecs);
1163  strides = std::vector<int>(nvecs);
1164 
1165  for (size_t i = 0; i < nvecs; i++) {
1166  if(stride == 1)
1167  coords[i] = &coordarr[i*localCount];
1168  else
1169  coords[i] = &coordarr[i];
1170 
1171  strides[i] = stride;
1172  }
1173 
1174 // printf("Made coords...\n");
1175 // for (size_t i = 0; i < nvecs; i++){
1176 // const zscalar_t * tmp = coords[i];
1177 // printf("coord %zu: {",i);
1178 // for(size_t j = 0; j < localCount; j++)
1179 // {
1180 // printf("%1.2g ", tmp[j]);
1181 // }
1182 // printf("}\n");
1183 // }
1184 
1185 }
1186 #endif
1187 
1188 
1189 // pamgen adapter
1191 AdapterFactory::getPamgenMeshAdapterForInput(UserInputForTests *uinput,
1192  const ParameterList &pList,
1193  const RCP<const Comm<int> > &comm)
1194 {
1195  AdapterWithTemplateName result;
1196 
1197 #ifdef HAVE_ZOLTAN2_PAMGEN
1198  if(uinput->hasPamgenMesh())
1199  {
1200  if(uinput->hasPamgenMesh())
1201  {
1202 // if(comm->getRank() == 0) std::cout << "Have pamgen mesh, constructing adapter...." << std::endl;
1203  result.adapter =
1204  new pamgen_adapter_t(*(comm.get()), "region");
1206 // if(comm->getRank() == 0)
1207 // ia->print(0);
1208  }
1209  }else{
1210  std::cerr << "Pamgen mesh is unavailable for PamgenMeshAdapter!"
1211  << std::endl;
1212  }
1213 
1214  return result;
1215 #else
1216  throw std::runtime_error("Pamgen input requested but Trilinos is not "
1217  "built with Pamgen");
1218 #endif
1219 }
1220 #endif
1221 
1222 
#define SET_COORDS_INPUT_1(adapterClass)
#define SET_COORDS_INPUT_2(adapterClass)
Generate input for testing purposes.
Defines the BasicIdentifierAdapter class.
Defines the BasicVectorAdapter class.
Defines the EvaluatePartition class.
Defines the PamgenMeshAdapter class.
Defines Parameter related enumerators, declares functions.
Defines the PartitioningProblem class.
float zscalar_t
Tpetra::Map ::local_ordinal_type zlno_t
Tpetra::Map ::global_ordinal_type zgno_t
#define Z2_TEST_UPCAST_COORDS(adptr, TEMPLATE_ACTION)
Defines XpetraCrsGraphAdapter class.
Defines the XpetraCrsMatrixAdapter class.
Defines the XpetraMultiVectorAdapter.
Zoltan2::BaseAdapterRoot * getCoordinateAdapter() const
Zoltan2::BaseAdapterRoot * getMainAdapter() const
AdapterFactory(UserInputForTests *uinput, const ParameterList &pList, const RCP< const Comm< int > > &comm)
A class method for constructing an input adapter defind in a parameter list.
EAdapterType getCoordinateAdapterType() const
EAdapterType getMainAdapterType() const
RCP< xcrsGraph_t > getUIXpetraCrsGraph()
RCP< tVector_t > getUITpetraVector()
RCP< tMVector_t > getUIEdgeWeights()
RCP< xMVector_t > getUIXpetraMultiVector(int nvec)
bool hasInputDataType(const string &input_type)
RCP< tMVector_t > getUITpetraMultiVector(int nvec)
RCP< tMVector_t > getUICoordinates()
RCP< tcrsGraph_t > getUITpetraCrsGraph()
RCP< xVector_t > getUIXpetraVector()
RCP< xcrsMatrix_t > getUIXpetraCrsMatrix()
RCP< tcrsMatrix_t > getUITpetraCrsMatrix()
RCP< tMVector_t > getUIWeights()
BaseAdapter defines methods required by all Adapters.
InputTraits< User >::scalar_t scalar_t
BasicVectorAdapter represents a vector (plus optional weights) supplied by the user as pointers to st...
Provides access for Zoltan2 to Xpetra::CrsGraph data.
void setVertexWeights(const scalar_t *val, int stride, int idx)
Provide a pointer to vertex weights.
void setEdgeWeights(const scalar_t *val, int stride, int idx)
Provide a pointer to edge weights.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
void setWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each entity of the primaryEntityType.
Zoltan2::BasicVectorAdapter< tMVector_t > xCG_eCG_t
Zoltan2::XpetraCrsMatrixAdapter< tcrsMatrix_t, tMVector_t > xCM_tCM_t
Zoltan2::XpetraMultiVectorAdapter< tMVector_t > xMV_tMV_t
Zoltan2::XpetraCrsMatrixAdapter< xcrsMatrix_t, tMVector_t > xCM_xCM_t
Zoltan2::BasicVectorAdapter< userTypes_t > pamgen_adapter_t
Zoltan2::XpetraMultiVectorAdapter< xMVector_t > xMV_xMV_t
Zoltan2::XpetraCrsGraphAdapter< tcrsGraph_t, tMVector_t > xCG_tCG_t
Zoltan2::BasicVectorAdapter< tMVector_t > xMV_eMV_t
Zoltan2::BasicVectorAdapter< tMVector_t > basic_vector_adapter
Zoltan2::XpetraCrsGraphAdapter< xcrsGraph_t, tMVector_t > xCG_xCG_t
Zoltan2::BasicIdentifierAdapter< userTypes_t > basic_id_t
Zoltan2::BasicVectorAdapter< tMVector_t > xCM_eCM_t
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Zoltan2::BaseAdapterRoot * adapter