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