Zoltan2
Loading...
Searching...
No Matches
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
13
14
15#ifndef ADAPTERFORTESTS
16#define ADAPTERFORTESTS
17
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
45using Teuchos::RCP;
46using Teuchos::ArrayRCP;
47using Teuchos::ArrayView;
48using Teuchos::Array;
49using Teuchos::Comm;
50using Teuchos::rcp;
51using Teuchos::arcp;
52using Teuchos::rcp_const_cast;
53using Teuchos::ParameterList;
54using std::string;
55using 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 */
72public:
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
104private:
106 std::vector<void*> ownedCoordArrays;
107
117 getBasicIdentiferAdapterForInput(UserInputForTests *uinput,
118 const ParameterList &pList, const RCP<const Comm<int> > &comm);
119
129 getXpetraMVAdapterForInput(UserInputForTests *uinput,
130 const ParameterList &pList, const RCP<const Comm<int> > &comm);
131
141 getXpetraCrsGraphAdapterForInput(UserInputForTests *uinput,
142 const ParameterList &pList, const RCP<const Comm<int> > &comm);
143
153 getXpetraCrsMatrixAdapterForInput(UserInputForTests *uinput,
154 const ParameterList &pList, const RCP<const Comm<int> > &comm);
155
165 getBasicVectorAdapterForInput(UserInputForTests *uinput,
166 const ParameterList &pList, const RCP<const Comm<int> > &comm);
167
177 getPamgenMeshAdapterForInput(UserInputForTests *uinput,
178 const ParameterList &pList, const RCP<const Comm<int> > &comm);
179
188 template <typename T>
189 void InitializeVectorData(const RCP<T> &data,
190 std::vector<const zscalar_t *> &coords,
191 std::vector<int> & strides,
192 int stride);
193
194};
195
196
198 UserInputForTests *uinput,
199 const ParameterList &pList,
200 const RCP<const Comm<int> > &comm)
201{
202 if(!pList.isParameter("input adapter"))
203 {
204 std::cerr << "Input adapter unspecified" << std::endl;
205 return;
206 }
207
208 // pick method for chosen adapter
209 std::string input_adapter_name = pList.get<string>("input adapter");
210
211 if(input_adapter_name == "BasicIdentifier")
212 adaptersSet.main = getBasicIdentiferAdapterForInput(uinput, pList, comm);
213 else if(input_adapter_name == "XpetraMultiVector")
214 adaptersSet.main = getXpetraMVAdapterForInput(uinput, pList, comm);
215 else if(input_adapter_name == "XpetraCrsGraph")
216 adaptersSet = getXpetraCrsGraphAdapterForInput(uinput,pList, comm);
217 else if(input_adapter_name == "XpetraCrsMatrix")
218 adaptersSet = getXpetraCrsMatrixAdapterForInput(uinput,pList, comm);
219 else if(input_adapter_name == "BasicVector")
220 adaptersSet.main = getBasicVectorAdapterForInput(uinput,pList, comm);
221 else if(input_adapter_name == "PamgenMesh")
222 adaptersSet.main = getPamgenMeshAdapterForInput(uinput,pList, comm);
223
224 if(adaptersSet.main.adapter == nullptr) {
225 throw std::logic_error("AdapterFactory failed to create adapter!");
226 }
227}
228
230 if( adaptersSet.main.adapter ) {
231 delete adaptersSet.main.adapter;
232 }
233
234 if( adaptersSet.coordinate.adapter ) {
235 delete adaptersSet.coordinate.adapter;
236 }
237 for (void* ptr : ownedCoordArrays) {
238 delete[] static_cast<zscalar_t*>(ptr);
239 }
240}
241
242
244 AdapterFactory::getBasicIdentiferAdapterForInput(UserInputForTests *uinput,
245 const ParameterList &pList,
246 const RCP<const Comm<int> > &comm)
247{
249
250 if(!pList.isParameter("data type"))
251 {
252 std::cerr << "Input data type unspecified" << std::endl;
253 return result;
254 }
255
256 string input_type = pList.get<string>("data type"); // get the input type
257
258 if (!uinput->hasInputDataType(input_type))
259 {
260 std::cerr << "Input type: " + input_type + " unavailable or misspelled."
261 << std::endl; // bad type
262 return result;
263 }
264
265 std::vector<const zscalar_t *> weights;
266 std::vector<int> weightStrides;
267 const zgno_t *globalIds = NULL;
268 size_t localCount = 0;
269
270 // get weights if any
271 // get weights if any
272 if(uinput->hasUIWeights())
273 {
274 RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
275 // copy to weight
276 size_t cols = vtx_weights->getNumVectors();
277 for (size_t i = 0; i< cols; i++) {
278 weights.push_back(vtx_weights->getData(i).getRawPtr());
279 weightStrides.push_back((int)vtx_weights->getStride());
280 }
281 }
282
283 if(input_type == "coordinates")
284 {
285 RCP<tMVector_t> data = uinput->getUICoordinates();
286 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
287 localCount = data->getLocalLength();
288 }
289 else if(input_type == "tpetra_vector")
290 {
291 RCP<tVector_t> data = uinput->getUITpetraVector();
292 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
293 localCount = data->getLocalLength();
294 }
295 else if(input_type == "tpetra_multivector")
296 {
297 int nvec = pList.get<int>("vector_dimension");
298 RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
299 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
300 localCount = data->getLocalLength();
301 }
302 else if(input_type == "tpetra_crs_graph")
303 {
304 RCP<tcrsGraph_t> data = uinput->getUITpetraCrsGraph();
305 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
306 localCount = data->getLocalNumCols();
307 }
308 else if(input_type == "tpetra_crs_matrix")
309 {
310 RCP<tcrsMatrix_t> data = uinput->getUITpetraCrsMatrix();
311 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
312 localCount = data->getLocalNumCols();
313 }
314 else if(input_type == "xpetra_vector")
315 {
316 RCP<xVector_t> data = uinput->getUIXpetraVector();
317 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
318 localCount = data->getLocalLength();
319 }
320 else if(input_type == "xpetra_multivector")
321 {
322 int nvec = pList.get<int>("vector_dimension");
323 RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
324 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
325 localCount = data->getLocalLength();
326 }
327 else if(input_type == "xpetra_crs_graph")
328 {
329 RCP<xcrsGraph_t> data = uinput->getUIXpetraCrsGraph();
330 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
331 localCount = data->getLocalNumCols();
332 }
333 else if(input_type == "xpetra_crs_matrix")
334 {
335 RCP<xcrsMatrix_t> data = uinput->getUIXpetraCrsMatrix();
336 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
337 localCount = data->getLocalNumCols();
338 }
339
340 result.adapterType = AT_basic_id_t;
341 result.adapter = new Zoltan2_TestingFramework::basic_id_t(zlno_t(localCount),
342 globalIds,
343 weights,weightStrides);
344 return result;
345}
346
347
348AdapterWithTemplateName AdapterFactory::getXpetraMVAdapterForInput(
349 UserInputForTests *uinput,
350 const ParameterList &pList,
351 const RCP<const Comm<int> > &comm)
352{
353 AdapterWithTemplateName result;
354
355 if(!pList.isParameter("data type"))
356 {
357 std::cerr << "Input data type unspecified" << std::endl;
358 return result;
359 }
360
361 string input_type = pList.get<string>("data type");
362 if (!uinput->hasInputDataType(input_type))
363 {
364 std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
365 << std::endl; // bad type
366 return result;
367 }
368
369 std::vector<const zscalar_t *> weights;
370 std::vector<int> weightStrides;
371
372 // get weights if any
373 if(uinput->hasUIWeights())
374 {
375 RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
376 // copy to weight
377 size_t weightsPerRow = vtx_weights->getNumVectors();
378 for (size_t i = 0; i< weightsPerRow; i++) {
379 weights.push_back(vtx_weights->getData(i).getRawPtr());
380 weightStrides.push_back(1);
381 }
382 }
383
384 // set adapter
385 if(input_type == "coordinates")
386 {
387 RCP<tMVector_t> data = uinput->getUICoordinates();
388 RCP<const tMVector_t> const_data = rcp_const_cast<const tMVector_t>(data);
389 if(weights.empty())
390 result.adapter = new xMV_tMV_t(const_data);
391 else {
392 result.adapter = new xMV_tMV_t(const_data,weights,weightStrides);
393 }
394 result.adapterType = AT_xMV_tMV_t;
395 }
396 else if(input_type == "tpetra_multivector")
397 {
398 int nvec = pList.get<int>("vector_dimension");
399 RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
400 RCP<const tMVector_t> const_data = rcp_const_cast<const tMVector_t>(data);
401 if(weights.empty())
402 result.adapter = new xMV_tMV_t(const_data);
403 else
404 result.adapter = new xMV_tMV_t(const_data,weights,weightStrides);
405 result.adapterType = AT_xMV_tMV_t;
406 }
407 else if(input_type == "xpetra_multivector")
408 {
409 int nvec = pList.get<int>("vector_dimension");
410 RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
411 RCP<const xMVector_t> const_data = rcp_const_cast<const xMVector_t>(data);
412 if(weights.empty())
413 result.adapter = new xMV_xMV_t(const_data);
414 else{
415 result.adapter = new xMV_xMV_t(const_data,weights,weightStrides);
416 }
417 result.adapterType = AT_xMV_xMV_t;
418 }
419
420 if(result.adapter == nullptr)
421 std::cerr << "Input data chosen not compatible with xpetra multi-vector adapter." << std::endl;
422
423 return result;
424}
425
426
427AdapterWithOptionalCoordinateAdapter AdapterFactory::getXpetraCrsGraphAdapterForInput(
428 UserInputForTests *uinput,
429 const ParameterList &pList,
430 const RCP<const Comm<int> > &comm)
431{
432
433 AdapterWithOptionalCoordinateAdapter adapters;
434
435 if(!pList.isParameter("data type"))
436 {
437 std::cerr << "Input data type unspecified" << std::endl;
438 return adapters;
439 }
440
441 string input_type = pList.get<string>("data type");
442 if (!uinput->hasInputDataType(input_type))
443 {
444 std::cerr << "Input type: " + input_type + ", unavailable or misspelled."
445 << std::endl; // bad type
446 return adapters;
447 }
448
449 std::vector<const zscalar_t *> vtx_weights;
450 std::vector<const zscalar_t *> edge_weights;
451 std::vector<int> vtx_weightStride;
452 std::vector<int> edge_weightStride;
453
454 // get vtx weights if any
455 if(uinput->hasUIWeights())
456 {
457 RCP<tMVector_t> vtx_weights_tmp = uinput->getUIWeights();
458 // copy to weight
459 size_t weightsPerRow = vtx_weights_tmp->getNumVectors();
460 for (size_t i = 0; i< weightsPerRow; i++) {
461 vtx_weights.push_back(vtx_weights_tmp->getData(i).getRawPtr());
462 vtx_weightStride.push_back(1);
463 }
464 }
465
466 // get edge weights if any
467 if(uinput->hasUIEdgeWeights())
468 {
469 RCP<tMVector_t> edge_weights_tmp = uinput->getUIEdgeWeights();
470 // copy to weight
471 size_t weightsPerRow = edge_weights_tmp->getNumVectors();
472 for (size_t i = 0; i< weightsPerRow; i++) {
473 edge_weights.push_back(edge_weights_tmp->getData(i).getRawPtr());
474 edge_weightStride.push_back(1);
475 }
476 }
477
478 // make the coordinate adapter
479 // get an adapter for the coordinates
480 // need to make a copy of the plist and change the vector type
481 Teuchos::ParameterList pCopy(pList);
482 pCopy = pCopy.set<std::string>("data type","coordinates");
483
484 // for coordinate adapter
485 #define SET_COORDS_INPUT_1(adapterClass) \
486 auto * ca = dynamic_cast<adapterClass*>(adapters.coordinate.adapter); \
487 if(!ca) {throw std::logic_error( "Coordinate adapter case failed!" );} \
488 ia->setCoordinateInput(ca);
489
490 if(input_type == "tpetra_crs_graph")
491 {
492 RCP<tcrsGraph_t> data = uinput->getUITpetraCrsGraph();
493 RCP<const tcrsGraph_t> const_data = rcp_const_cast<const tcrsGraph_t>(data);
494
495 xCG_tCG_t * ia = new xCG_tCG_t(const_data,(int)vtx_weights.size(),(int)edge_weights.size());
496 adapters.main.adapterType = AT_xCG_tCG_t;
497 adapters.main.adapter = ia;
498
499 if(!vtx_weights.empty()) {
500 for(int i = 0; i < (int)vtx_weights.size(); i++)
501 ia->setVertexWeights(vtx_weights[i],vtx_weightStride[i],i);
502 }
503
504 if(!edge_weights.empty()) {
505 for(int i = 0; i < (int)edge_weights.size(); i++)
506 ia->setEdgeWeights(edge_weights[i],edge_weightStride[i],i);
507 }
508
509 if (uinput->hasUICoordinates()) {
510 adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
512 }
513 }
514 else if(input_type == "xpetra_crs_graph")
515 {
516 RCP<xcrsGraph_t> data = uinput->getUIXpetraCrsGraph();
517 RCP<const xcrsGraph_t> const_data = rcp_const_cast<const xcrsGraph_t>(data);
518
519 xCG_xCG_t * ia = new xCG_xCG_t(const_data, (int)vtx_weights.size(), (int)edge_weights.size());
520 adapters.main.adapterType = AT_xCG_xCG_t;
521 adapters.main.adapter = ia;
522 if(!vtx_weights.empty())
523 {
524 for(int i = 0; i < (int)vtx_weights.size(); i++)
525 ia->setVertexWeights(vtx_weights[i],vtx_weightStride[i],i);
526 }
527
528 if(!edge_weights.empty())
529 {
530 for(int i = 0; i < (int)edge_weights.size(); i++)
531 ia->setEdgeWeights(edge_weights[i],edge_weightStride[i],i);
532 }
533
534 if (uinput->hasUICoordinates()) {
535 adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
537 }
538 }
539
540 if(adapters.main.adapter == nullptr) {
541 std::cerr << "Input data chosen not compatible with "
542 << "XpetraCrsGraph adapter." << std::endl;
543 return adapters;
544 }
545
546 return adapters;
547}
548
549
550AdapterWithOptionalCoordinateAdapter AdapterFactory::getXpetraCrsMatrixAdapterForInput(
551 UserInputForTests *uinput,
552 const ParameterList &pList,
553 const RCP<const Comm<int> > &comm)
554{
555 AdapterWithOptionalCoordinateAdapter adapters;
556
557 if(!pList.isParameter("data type"))
558 {
559 std::cerr << "Input data type unspecified" << std::endl;
560 return adapters;
561 }
562
563 string input_type = pList.get<string>("data type");
564 if (!uinput->hasInputDataType(input_type))
565 {
566 std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
567 << std::endl; // bad type
568 return adapters;
569 }
570
571 std::vector<const zscalar_t *> weights;
572 std::vector<int> strides;
573
574 // get weights if any
575 if(uinput->hasUIWeights())
576 {
577 if(comm->getRank() == 0) std::cout << "Have weights...." << std::endl;
578 RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
579
580 // copy to weight
581 int weightsPerRow = (int)vtx_weights->getNumVectors();
582 for (int i = 0; i< weightsPerRow; i++)
583 {
584 weights.push_back(vtx_weights->getData(i).getRawPtr());
585 strides.push_back(1);
586 }
587
588 }
589
590 // make the coordinate adapter
591 // get an adapter for the coordinates
592 // need to make a copy of the plist and change the vector type
593 Teuchos::ParameterList pCopy(pList);
594 pCopy = pCopy.set<std::string>("data type","coordinates");
595
596 // for coordinate adapter
597 #define SET_COORDS_INPUT_2(adapterClass) \
598 auto * ca = dynamic_cast<adapterClass*>(adapters.coordinate.adapter); \
599 if(!ca) {throw std::logic_error( "Coordinate adapter case failed!" );} \
600 ia->setCoordinateInput(ca);
601
602 // set adapter
603 if(input_type == "tpetra_crs_matrix")
604 {
605 if(comm->getRank() == 0) std::cout << "Make tpetra crs matrix adapter...." << std::endl;
606
607 // get pointer to data
608 RCP<tcrsMatrix_t> data = uinput->getUITpetraCrsMatrix();
609 RCP<const tcrsMatrix_t> const_data = rcp_const_cast<const tcrsMatrix_t>(data); // const cast data
610
611 // new adapter
612 xCM_tCM_t *ia = new xCM_tCM_t(const_data, (int)weights.size());
613 adapters.main.adapterType = AT_xCM_tCM_t;
614 adapters.main.adapter = ia;
615
616 // if we have weights set them
617 if(!weights.empty())
618 {
619 for(int i = 0; i < (int)weights.size(); i++)
620 ia->setWeights(weights[i],strides[i],i);
621 }
622
623 if (uinput->hasUICoordinates()) {
624 adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
626 }
627 }
628 else if(input_type == "xpetra_crs_matrix")
629 {
630 RCP<xcrsMatrix_t> data = uinput->getUIXpetraCrsMatrix();
631 RCP<const xcrsMatrix_t> const_data = rcp_const_cast<const xcrsMatrix_t>(data);
632
633 // new adapter
634 xCM_xCM_t *ia = new xCM_xCM_t(const_data, (int)weights.size());
635 adapters.main.adapterType = AT_xCM_xCM_t;
636 adapters.main.adapter = ia;
637
638 // if we have weights set them
639 if(!weights.empty())
640 {
641 for(int i = 0; i < (int)weights.size(); i++)
642 ia->setWeights(weights[i],strides[i],i);
643 }
644
645 if (uinput->hasUICoordinates()) {
646 adapters.coordinate = getXpetraMVAdapterForInput(uinput, pCopy, comm);
648 }
649 }
650
651 if(adapters.main.adapter == nullptr)
652 {
653 std::cerr << "Input data chosen not compatible with "
654 << "XpetraCrsMatrix adapter." << std::endl;
655 return adapters;
656 }
657
658 return adapters;
659}
660
661AdapterWithTemplateName AdapterFactory::getBasicVectorAdapterForInput(
662 UserInputForTests *uinput,
663 const ParameterList &pList,
664 const RCP<const Comm<int> > &comm)
665{
666
667 AdapterWithTemplateName result;
668
669 if(!pList.isParameter("data type"))
670 {
671 std::cerr << "Input data type unspecified" << std::endl;
672 return result;
673 }
674
675 string input_type = pList.get<string>("data type");
676 if (!uinput->hasInputDataType(input_type))
677 {
678 std::cerr << "Input type:" + input_type + ", unavailable or misspelled."
679 << std::endl; // bad type
680 return result;
681 }
682
683 std::vector<const zscalar_t *> weights;
684 std::vector<int> weightStrides;
685 const zgno_t * globalIds;
686 zlno_t localCount = 0;
687
688 // get weights if any
689 // get weights if any
690 if(uinput->hasUIWeights())
691 {
692 RCP<tMVector_t> vtx_weights = uinput->getUIWeights();
693 // copy to weight
694 size_t cols = vtx_weights->getNumVectors();
695 for (size_t i = 0; i< cols; i++) {
696 weights.push_back(vtx_weights->getData(i).getRawPtr());
697 weightStrides.push_back(1);
698 }
699 }
700
701 // get vector stride
702 int stride = 1;
703 if(pList.isParameter("stride"))
704 stride = pList.get<int>("stride");
705
707
708 if(input_type == "coordinates")
709 {
710 RCP<tMVector_t> data = uinput->getUICoordinates();
711 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
712 localCount = static_cast<zlno_t>(data->getLocalLength());
713
714 // get strided data
715 std::vector<const zscalar_t *> coords;
716 std::vector<int> entry_strides;
717 InitializeVectorData(data,coords,entry_strides,stride);
718
719
720
721 if (weights.empty()) {
722 size_t dim = coords.size(); //BDD add NULL for constructor call
723 size_t push_null = 3-dim;
724 for (size_t i = 0; i < push_null; i ++) coords.push_back(NULL);
726 zlno_t(localCount),
727 globalIds,
728 coords[0],
729 coords[1],coords[2],
730 stride, stride, stride);
731 } else if (weights.size() == 1) {
732 size_t dim = coords.size(); //BDD add NULL for constructor call
733 size_t push_null = 3-dim;
734 for (size_t i = 0; i < push_null; i ++) coords.push_back(NULL);
736 zlno_t(localCount),
737 globalIds,
738 coords[0],
739 coords[1],coords[2],
740 stride, stride, stride,
741 true,
742 weights[0],
743 weightStrides[0]);
744 } else { // More than one weight per ID
746 zlno_t(localCount),
747 globalIds,
748 coords, entry_strides,
749 weights, weightStrides);
750 }
751 }
752 else if(input_type == "tpetra_vector")
753 {
754 RCP<tVector_t> data = uinput->getUITpetraVector();
755 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
756 localCount = static_cast<zlno_t>(data->getLocalLength());
757
758 // get strided data
759 std::vector<const zscalar_t *> coords;
760 std::vector<int> entry_strides;
761 InitializeVectorData(data,coords,entry_strides,stride);
762
763 if(weights.empty())
764 {
765 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
766 coords[0], entry_strides[0]);
767 }else{
768 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
769 coords[0], entry_strides[0],
770 true,
771 weights[0],
772 weightStrides[0]);
773
774 }
775
776 }
777 else if(input_type == "tpetra_multivector")
778 {
779 int nvec = pList.get<int>("vector_dimension");
780
781 RCP<tMVector_t> data = uinput->getUITpetraMultiVector(nvec);
782 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
783 localCount = static_cast<zlno_t>(data->getLocalLength());
784
785 // get strided data
786 std::vector<const zscalar_t *> coords;
787 std::vector<int> entry_strides;
788 InitializeVectorData(data,coords,entry_strides,stride);
789
790 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
791 coords, entry_strides,
792 weights,weightStrides);
793
794 }
795 else if(input_type == "xpetra_vector")
796 {
797 RCP<xVector_t> data = uinput->getUIXpetraVector();
798 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
799 localCount = static_cast<zlno_t>(data->getLocalLength());
800
801 // get strided data
802 std::vector<const zscalar_t *> coords;
803 std::vector<int> entry_strides;
804 InitializeVectorData(data,coords,entry_strides,stride);
805
806 if(weights.empty())
807 {
808 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
809 coords[0], entry_strides[0]);
810 }else{
811 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
812 coords[0], entry_strides[0],
813 true,
814 weights[0],
815 weightStrides[0]);
816
817 }
818 }
819 else if(input_type == "xpetra_multivector")
820 {
821 int nvec = pList.get<int>("vector_dimension");
822 RCP<xMVector_t> data = uinput->getUIXpetraMultiVector(nvec);
823 globalIds = (zgno_t *)data->getMap()->getLocalElementList().getRawPtr();
824 localCount = static_cast<zlno_t>(data->getLocalLength());
825
826 // get strided data
827 std::vector<const zscalar_t *> coords;
828 std::vector<int> entry_strides;
829 InitializeVectorData(data,coords,entry_strides,stride);
830 if(comm->getRank() == 0) std::cout << "size of entry strides: " << entry_strides.size() << std::endl;
831 if(comm->getRank() == 0) std::cout << "size of coords: " << coords.size() << std::endl;
832
833 // make vector!
834 result.adapter = new Zoltan2_TestingFramework::basic_vector_adapter(localCount, globalIds,
835 coords, entry_strides,
836 weights,weightStrides);
837 }
838
839 return result;
840}
841
842template <typename T>
843void AdapterFactory::InitializeVectorData(const RCP<T> &data,
844 std::vector<const zscalar_t *> &coords,
845 std::vector<int> & strides,
846 int stride)
847{
848 // set up adapter data
849 size_t localCount = data->getLocalLength();
850 size_t nvecs = data->getNumVectors();
851 size_t vecsize = data->getNumVectors() * data->getLocalLength();
852// printf("Number of vectors by data: %zu\n", nvecs);
853 // printf("Size of data: %zu\n", vecsize);
854
855 ArrayRCP<zscalar_t> *petravectors = new ArrayRCP<zscalar_t>[nvecs];
856
857 // printf("Getting t-petra vectors...\n");
858 for (size_t i = 0; i < nvecs; i++)
859 petravectors[i] = data->getDataNonConst(i);
860
861 // debugging
862 // for (size_t i = 0; i < nvecs; i++){
863 // printf("Tpetra vector %zu: {",i);
864 //
865 // for (size_t j = 0; j < localCount; j++)
866 // {
867 // printf("%1.2g ",petravectors[i][j]);
868 // }
869 // printf("}\n");
870 // }
871
872 size_t idx = 0;
873 zscalar_t *coordarr = new zscalar_t[vecsize];
874 ownedCoordArrays.push_back(coordarr);
875
876 if(stride == 1 || stride != (int)nvecs)
877 {
878 for (size_t i = 0; i < nvecs; i++) {
879 for (size_t j = 0; j < localCount; j++) {
880 coordarr[idx++] = petravectors[i][j];
881 }
882 }
883 }else
884 {
885 for (size_t j = 0; j < localCount; j++) {
886 for (size_t i = 0; i < nvecs; i++) {
887 coordarr[idx++] = petravectors[i][j];
888 }
889 }
890 }
891
892 // debugging
893 // printf("Made coordarr : {");
894 // for (zlno_t i = 0; i < vecsize; i++){
895 // printf("%1.2g ",coordarr[i]);
896 // }
897 // printf("}\n");
898
899 // always build for dim 3
900 coords = std::vector<const zscalar_t *>(nvecs);
901 strides = std::vector<int>(nvecs);
902
903 for (size_t i = 0; i < nvecs; i++) {
904 if(stride == 1)
905 coords[i] = &coordarr[i*localCount];
906 else
907 coords[i] = &coordarr[i];
908
909 strides[i] = stride;
910 }
911
912 // debugging
913 // printf("Made coords...\n");
914 // for (size_t i = 0; i < nvecs; i++){
915 // const zscalar_t * tmp = coords[i];
916 // printf("coord %zu: {",i);
917 // for(size_t j = 0; j < localCount; j++)
918 // {
919 // printf("%1.2g ", tmp[j]);
920 // }
921 // printf("}\n");
922 // }
923
924 // printf("clean up coordarr and tpetravectors...\n\n\n");
925 // delete [] coordarr; // coordarr memory must stay alive as coords point into it
926 delete [] petravectors;
927}
928
929// pamgen adapter
931AdapterFactory::getPamgenMeshAdapterForInput(UserInputForTests *uinput,
932 const ParameterList &pList,
933 const RCP<const Comm<int> > &comm)
934{
935 AdapterWithTemplateName result;
936
937#ifdef HAVE_ZOLTAN2_PAMGEN
938 if(uinput->hasPamgenMesh())
939 {
940 if(uinput->hasPamgenMesh())
941 {
942// if(comm->getRank() == 0) std::cout << "Have pamgen mesh, constructing adapter...." << std::endl;
943 result.adapter =
944 new pamgen_adapter_t(*(comm.get()), "region");
946// if(comm->getRank() == 0)
947// ia->print(0);
948 }
949 }else{
950 std::cerr << "Pamgen mesh is unavailable for PamgenMeshAdapter!"
951 << std::endl;
952 }
953
954 return result;
955#else
956 throw std::runtime_error("Pamgen input requested but Trilinos is not "
957 "built with Pamgen");
958#endif
959}
960#endif
961
962
#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.
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.
void setWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each entity of the primaryEntityType.
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 > basic_vector_adapter
Zoltan2::XpetraCrsGraphAdapter< xcrsGraph_t, tMVector_t > xCG_xCG_t
Zoltan2::BasicIdentifierAdapter< userTypes_t > basic_id_t
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Zoltan2::BaseAdapterRoot * adapter