26 #include <Teuchos_DefaultComm.hpp>
27 #include <Teuchos_RCP.hpp>
28 #include <Teuchos_Comm.hpp>
29 #include <Teuchos_CommHelpers.hpp>
33 using Teuchos::rcp_const_cast;
36 typedef Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t>
tvector_t;
37 typedef Xpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t>
xvector_t;
40 template <
typename User>
44 RCP<const Comm<int> > comm = vector.getMap()->getComm();
45 int fail = 0, gfail=0;
50 size_t length = vector.getLocalLength();
63 if (nvals != vector.getLocalLength())
68 for (
int v=0; v < nvec; v++){
69 auto vecdata = vector.getData(v);
73 if (!fail && stride != 1)
77 for (
size_t i = 0; i < length; i++)
78 if (vals[i*stride] != vecdata[i]) {
91 template <
typename User>
94 std::vector<const zscalar_t *> &
weights, std::vector<int> &strides)
100 RCP<const Comm<int> > comm = vector.getMap()->getComm();
101 int wdim = weights.size();
108 if (!gfail && wdim) {
112 for (
int w=0; !fail && w < wdim; w++){
115 if (!fail && stride != strides[w])
118 for (
size_t v=0; !fail && v < vector.getLocalLength(); v++){
119 if (wgt[v*stride] != weights[w][v*stride])
140 std::getline(fp, line);
141 while (line[0]==
'#') std::getline(fp, line);
142 std::istringstream issHeader(line);
144 issHeader >> nIDs >> nEdges >> s_code;
145 if (!strcmp(s_code.c_str(),
"010") || !strcmp(s_code.c_str(),
"011")) {
146 if (!(issHeader >> nWgts)) nWgts = 1;
150 template <
typename User>
153 const char *fileprefixInp,
154 const Teuchos::Comm<int> &comm
160 int fail = 0, gfail=0;
162 std::string tmp(fileprefixInp);
163 tmp = tmp +
"_generated";
164 const char *fileprefixGen = tmp.c_str();
169 if (comm.getRank() == 0) {
171 size_t nIDsGen, nIDsInp;
172 size_t nEdgesGen, nEdgesInp;
173 int nWgtsGen = 0, nWgtsInp = 0;
174 std::string lineGen, lineInp;
176 std::ifstream fpGen, fpInp;
177 std::string graphFilenameGen = fileprefixGen;
178 graphFilenameGen = graphFilenameGen +
".graph";
179 std::string graphFilenameInp = fileprefixInp;
180 graphFilenameInp = graphFilenameInp +
".graph";
183 fpGen.open(graphFilenameGen.c_str(), std::ios::in);
187 fpInp.open(graphFilenameInp.c_str(), std::ios::in);
191 if (nIDsGen != nIDsInp) {
192 std::cout <<
"GenerateFiles: nIDsGen " << nIDsGen
193 <<
" != nIDsInp " << nIDsInp << std::endl;
198 if (!fail && nEdgesGen != 0) {
199 std::cout <<
"GenerateFiles: nEdgesGen " << nEdgesGen <<
" != 0"
205 if (!fail && nWgtsGen) {
209 for (cntWgtLines = 0; cntWgtLines < nIDsGen &&
210 std::getline(fpGen, lineGen) &&
211 std::getline(fpInp, lineInp); cntWgtLines++) {
213 std::istringstream issGen(lineGen);
214 std::istringstream issInp(lineInp);
217 double wgtGen, wgtInp;
219 while (issGen >> wgtGen) {
223 if (wgtGen != wgtInp) fail = 2224;
228 if (nw != nWgtsGen) fail = 2225;
230 if (cntWgtLines != nIDsGen) fail = 2226;
235 size_t cntWgtLines = 0;
236 for (cntWgtLines = 0; cntWgtLines < nIDsGen &&
237 std::getline(fpGen, lineGen); cntWgtLines++) {
238 std::istringstream issGen(lineGen);
241 while (issGen) { issGen >> wgtGen; nw++; }
242 if (nw != nWgtsGen) fail = 2227;
244 if (cntWgtLines != nIDsGen) fail = 2228;
253 std::string coordsFilenameGen = fileprefixGen;
254 coordsFilenameGen = coordsFilenameGen +
".coords";
255 std::string coordsFilenameInp = fileprefixInp;
256 coordsFilenameInp = coordsFilenameInp +
".coords";
258 fpGen.open(coordsFilenameGen.c_str(), std::ios::in);
259 fpInp.open(coordsFilenameInp.c_str(), std::ios::in);
262 for (cnt = 0; std::getline(fpGen,lineGen) &&
263 std::getline(fpInp,lineInp); cnt++) {
266 std::istringstream issGen(lineGen);
267 std::istringstream issInp(lineInp);
272 while (issGen && issInp) {
275 std::cout <<
"Coordinates " << xGen <<
" != " << xInp
284 if (issGen || issInp) {
285 std::cout <<
"Dimension of generated file != dimension of input file"
292 if (!fail && cnt != nIDsGen) {
293 std::cout <<
"Number of coordinates read " << cnt
294 <<
" != number of IDs " << nIDsGen << std::endl;
309 int main(
int narg,
char *arg[])
311 Tpetra::ScopeGuard tscope(&narg, &arg);
312 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
314 int rank = comm->getRank();
315 int fail = 0, gfail=0;
319 std::string inputFilePrefix(
"simple");
320 Teuchos::CommandLineProcessor cmdp (
false,
false);
321 cmdp.setOption(
"file", &inputFilePrefix,
322 "chaco file (prefix) to be used in test");
323 cmdp.parse(narg, arg);
328 RCP<UserInputForTests> uinput;
329 Teuchos::ParameterList params;
330 params.set(
"input file", inputFilePrefix);
331 params.set(
"file type",
"Chaco");
336 catch(std::exception &e){
338 std::cout << e.what() << std::endl;
342 RCP<tvector_t> inputMVector;
343 RCP<tvector_t> migratedMVector;
347 inputMVector = uinput->getUICoordinates();
348 size_t vlen = inputMVector->getLocalLength();
351 std::vector<const zscalar_t *> IDWeights;
352 std::vector<int> IDWeightsStrides;
355 if (uinput->hasUIWeights()) {
357 auto uiweights = uinput->getUIWeights();
359 nWeights = uiweights->getNumVectors();
360 IDWeights.resize(nWeights);
361 IDWeightsStrides.resize(nWeights);
363 for (
int w = 0; w < nWeights; w++) {
364 IDWeights[w] = uiweights->getData(w).getRawPtr();
365 IDWeightsStrides[w] = 1;
377 part_t *p =
new part_t [vlen];
378 memset(p, 0,
sizeof(part_t) * vlen);
379 ArrayRCP<part_t> solnParts(p, 0, vlen,
true);
381 soln_t solution(env, comm, nWeights);
382 solution.setParts(solnParts);
389 std::cout <<
"Constructed with Tpetra::MultiVector" << std::endl;
391 RCP<const tvector_t> ctV = rcp_const_cast<
const tvector_t>(inputMVector);
392 RCP<Zoltan2::XpetraMultiVectorAdapter<tvector_t> > tVInput;
397 IDWeights, IDWeightsStrides));
399 catch (std::exception &e){
401 std::cout << e.what() << std::endl;
405 fail = verifyInputAdapter<tvector_t>(*tVInput, *inputMVector, nVec,
406 IDWeights, IDWeightsStrides);
414 tVInput->applyPartitioningSolution(*inputMVector, vMigrate, solution);
415 migratedMVector = rcp(vMigrate);
417 catch (std::exception &e){
424 RCP<const tvector_t> cnewV =
425 rcp_const_cast<
const tvector_t>(migratedMVector);
426 RCP<Zoltan2::XpetraMultiVectorAdapter<tvector_t> > newInput;
431 catch (std::exception &e){
433 std::cout << e.what() << std::endl;
438 std::cout <<
"Constructed with ";
439 std::cout <<
"Tpetra::MultiVector migrated to proc 0" << std::endl;
443 fail = verifyInputAdapter<tvector_t>(*newInput, *migratedMVector, nVec);
444 if (fail) fail += 100;
457 std::cout <<
"Constructed with Xpetra::MultiVector" << std::endl;
461 RCP<const xvector_t> cxV = rcp_const_cast<
const xvector_t>(xV);
462 RCP<Zoltan2::XpetraMultiVectorAdapter<xvector_t> > xVInput;
470 catch (std::exception &e){
472 std::cout << e.what() << std::endl;
476 fail = verifyInputAdapter<xvector_t>(*xVInput, *inputMVector, nVec,
477 IDWeights, IDWeightsStrides);
484 xVInput->applyPartitioningSolution(*xV, vMigrate, solution);
486 catch (std::exception &e){
493 RCP<const xvector_t> cnewV(vMigrate);
494 RCP<Zoltan2::XpetraMultiVectorAdapter<xvector_t> > newInput;
499 catch (std::exception &e){
501 std::cout << e.what() << std::endl;
506 std::cout <<
"Constructed with ";
507 std::cout <<
"Xpetra::MultiVector migrated to proc 0" << std::endl;
509 fail = verifyInputAdapter<xvector_t>(*newInput, *migratedMVector, nVec);
510 if (fail) fail += 100;
519 #ifdef HAVE_EPETRA_DATA_TYPES
522 typedef Epetra_MultiVector evector_t;
525 std::cout <<
"Constructed with Epetra_MultiVector" << std::endl;
528 rcp(
new Epetra_MultiVector(uinput->getUIEpetraCrsGraph()->RowMap(),
530 for (
int v = 0; v < nVec; v++) {
531 auto inV = inputMVector->getData(v);
532 for (
int i = 0; i < eV->MyLength(); i++)
533 eV->ReplaceMyValue(i, v, inV[i]);
536 RCP<const evector_t> ceV = rcp_const_cast<
const evector_t>(eV);
537 RCP<Zoltan2::XpetraMultiVectorAdapter<evector_t> > eVInput;
539 bool goodAdapter =
true;
543 IDWeights, IDWeightsStrides));
545 catch (std::exception &e){
546 if (std::is_same<znode_t, Xpetra::EpetraNode>::value) {
549 std::cout << e.what() << std::endl;
554 std::cout <<
"Node type is not supported by Xpetra's Epetra interface;"
555 <<
" Skipping this test." << std::endl;
556 std::cout <<
"FYI: Here's the exception message: " << std::endl
557 << e.what() << std::endl;
564 fail = verifyInputAdapter<evector_t>(*eVInput, *inputMVector, nVec,
565 IDWeights, IDWeightsStrides);
570 evector_t *vMigrate =NULL;
572 eVInput->applyPartitioningSolution(*eV, vMigrate, solution);
574 catch (std::exception &e){
581 RCP<const evector_t> cnewV(vMigrate,
true);
582 RCP<Zoltan2::XpetraMultiVectorAdapter<evector_t> > newInput;
587 catch (std::exception &e){
589 std::cout << e.what() << std::endl;
594 std::cout <<
"Constructed with ";
595 std::cout <<
"Epetra_MultiVector migrated to proc 0" << std::endl;
597 fail = verifyInputAdapter<evector_t>(*newInput, *migratedMVector,
599 if (fail) fail += 100;
614 std::cout <<
"PASS" << std::endl;
void printFailureCode(const Comm< int > &comm, int fail)
void generateFiles(const char *fileprefix, const Teuchos::Comm< int > &comm) const
Write files that can be used as input to Zoltan or Zoltan2 driver Creates chaco-formatted input files...
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
int main(int narg, char **arg)
common code used by tests
static RCP< User > convertToXpetra(const RCP< User > &a)
Convert the object to its Xpetra wrapped version.
Defines the XpetraMultiVectorAdapter.
int getNumEntriesPerID() const
Return the number of vectors.
SparseMatrixAdapter_t::part_t part_t
A PartitioningSolution is a solution to a partitioning problem.
VectorAdapter defines the interface for vector input.
void getEntriesView(const scalar_t *&elements, int &stride, int idx=0) const
Provide a pointer to the elements of the specified vector.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
An adapter for Xpetra::MultiVector.
static const std::string fail
int globalFail(const Comm< int > &comm, int fail)
void getIDsView(const gno_t *&ids) const
size_t getLocalNumIDs() const
Returns the number of objects on this process.
Tpetra::Map::global_ordinal_type zgno_t
void getWeightsView(const scalar_t *&weights, int &stride, int idx) const
Provide pointer to a weight array with stride.