// @HEADER // ***************************************************************************** // Zoltan2: A package of combinatorial algorithms for scientific computing // // Copyright 2012 NTESS and the Zoltan2 contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file Zoltan2_XpetraTraits.hpp \brief Traits of Xpetra classes, including migration method. */ #ifndef _ZOLTAN2_XPETRATRAITS_HPP_ #define _ZOLTAN2_XPETRATRAITS_HPP_ #include #include #include #include #include #include #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) #include #include #include #endif namespace Zoltan2 { ////////////////////////////////////////////////////////////////////////////// // Extra traits needed only for Xpetra matrices and graphs /*! \brief Defines the traits required for Tpetra, Eptra and Xpetra objects. * * Definitions are provided for: * * \li Tpetra::CrsMatrix * \li Epetra_CrsMatrix * \li Xpetra::CrsMatrix * \li Xpetra::CrsMatrix * \li Tpetra::CrsGraph * \li Epetra_CrsGraph * \li Xpetra::CrsGraph * \li Xpetra::CrsGraph * \li Tpetra::Vector * \li Epetra_Vector * \li Xpetra::Vector * \li Xpetra::Vector * \li Tpetra::MultiVector * \li Epetra_MultiVector * \li Xpetra::MultiVector * \li Xpetra::MultiVector */ template struct XpetraTraits { /*! \brief Convert the object to its Xpetra wrapped version. */ static inline RCP convertToXpetra(const RCP &a) { return a; } /*! \brief The objects global ordinal data type. */ typedef default_gno_t gno_t; /*! \brief The objects local ordinal data type. */ typedef default_lno_t lno_t; /*! \brief Migrate the object * Given a user object and a new row distribution, create and * return a new user object with the new distribution. */ static RCP doMigration(const User &from, size_t numLocalRows, const gno_t *myNewRows) { return Teuchos::null; } }; #ifndef DOXYGEN_SHOULD_SKIP_THIS ////////////////////////////////////////////////////////////////////////////// // Tpetra::CrsMatrix template struct XpetraTraits > { typedef typename Xpetra::CrsMatrix xmatrix_t; typedef typename Xpetra::TpetraCrsMatrix xtmatrix_t; typedef typename Tpetra::CrsMatrix tmatrix_t; static inline RCP convertToXpetra(const RCP &a) { return rcp(new xtmatrix_t(a)); } static RCP doMigration(const tmatrix_t &from, size_t numLocalRows, const gno_t *myNewRows) { typedef Tpetra::Map map_t; // source map const RCP &smap = from.getRowMap(); gno_t numGlobalRows = smap->getGlobalNumElements(); gno_t base = smap->getMinAllGlobalIndex(); // target map ArrayView rowList(myNewRows, numLocalRows); const RCP > &comm = from.getComm(); RCP tmap = rcp(new map_t(numGlobalRows, rowList, base, comm)); // importer Tpetra::Import importer(smap, tmap); // target matrix // Chris Siefert proposed using the following to make migration // more efficient. // By default, the Domain and Range maps are the same as in "from". // As in the original code, we instead set them both to tmap. // The assumption is a square matrix. // TODO: what about rectangular matrices? // TODO: Should choice of domain/range maps be an option to this function? // KDD 3/7/16: disabling Chris' new code to avoid dashboard failures; // KDD 3/7/16: can re-enable when issue #114 is fixed. // KDD 3/7/16: when re-enable CSIEFERT code, can comment out // KDD 3/7/16: "Original way" code. // KDD 1/27/17: Re-enabling Chris' code, as this issue is resolved. RCP M; from.importAndFillComplete(M, importer, tmap, tmap); //// Original way we did it: //// //int oldNumElts = smap->getLocalNumElements(); //int newNumElts = numLocalRows; //// number of non zeros in my new rows //typedef Tpetra::Vector vector_t; //vector_t numOld(smap); // TODO These vectors should have scalar=size_t, //vector_t numNew(tmap); // but ETI does not yet support that. //for (int lid=0; lid < oldNumElts; lid++){ //numOld.replaceGlobalValue(smap->getGlobalElement(lid), //scalar_t(from.getNumEntriesInLocalRow(lid))); //} //numNew.doImport(numOld, importer, Tpetra::INSERT); // TODO Could skip this copy if could declare vector with scalar=size_t. //ArrayRCP nnz(newNumElts); //if (newNumElts > 0){ //ArrayRCP ptr = numNew.getDataNonConst(0); //for (int lid=0; lid < newNumElts; lid++){ //nnz[lid] = static_cast(ptr[lid]); //} //} //RCP M = rcp(new tmatrix_t(tmap, nnz, Tpetra::StaticProfile)); //M->doImport(from, importer, Tpetra::INSERT); //M->fillComplete(); // End of original way we did it. return M; } }; ////////////////////////////////////////////////////////////////////////////// #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) // Epetra_CrsMatrix template <> struct XpetraTraits { typedef InputTraits::scalar_t scalar_t; typedef InputTraits::lno_t lno_t; typedef InputTraits::gno_t gno_t; typedef InputTraits::node_t node_t; static inline RCP > convertToXpetra(const RCP &a) { RCP > xa; try { xa = rcp(new Xpetra::EpetraCrsMatrixT(a)); } catch (std::exception &e) { if (std::is_same::value) throw std::runtime_error(std::string("Cannot convert from " "Epetra_CrsMatrix to " "Xpetra::EpetraCrsMatrixT\n") + e.what()); else throw std::runtime_error(std::string("Cannot convert from " "Epetra_CrsMatrix to " "Xpetra::EpetraCrsMatrixT\n" "Use node_t that is supported by " "Xpetra with Epetra classes\n") + e.what()); } return xa; } static RCP doMigration(const Epetra_CrsMatrix &from, size_t numLocalRows, const gno_t *myNewRows) { // source map const Epetra_Map &smap = from.RowMap(); gno_t numGlobalRows = smap.NumGlobalElements(); int base = smap.MinAllGID(); // target map const Epetra_Comm &comm = from.Comm(); Epetra_Map tmap(numGlobalRows, numLocalRows, myNewRows, base, comm); // importer Epetra_Import importer(tmap, smap); // target matrix // Chris Siefert proposed using the following to make migration // more efficient. // By default, the Domain and Range maps are the same as in "from". // As in the original code, we instead set them both to tmap. // The assumption is a square matrix. // TODO: what about rectangular matrices? // TODO: Should choice of domain/range maps be an option to this function? RCP M = rcp(new Epetra_CrsMatrix(from, importer, &tmap, &tmap)); // Original way we did it: // // int oldNumElts = smap.NumMyElements(); // // // number of non zeros in my new rows // Epetra_Vector numOld(smap); // Epetra_Vector numNew(tmap); // // for (int lid=0; lid < oldNumElts; lid++){ // numOld[lid] = from.NumMyEntries(lid); // } // numNew.Import(numOld, importer, Insert); // // int newNumElts = numLocalRows; // Array nnz(newNumElts); // for (int lid=0; lid < newNumElts; lid++){ // nnz[lid] = static_cast(numNew[lid]); // } // // RCP M = // rcp(new Epetra_CrsMatrix(::Copy, tmap, nnz.getRawPtr(), true)); // M->Import(from, importer, Insert); // M->FillComplete(); return M; } }; #endif ////////////////////////////////////////////////////////////////////////////// // Xpetra::CrsMatrix // KDDKDD: Do we need specializations for Xpetra::EpetraCrsMatrix and // KDDKDD: Xpetra::TpetraCrsMatrix template struct XpetraTraits > { typedef Xpetra::CrsMatrix x_matrix_t; typedef Xpetra::TpetraCrsMatrix xt_matrix_t; typedef Tpetra::CrsMatrix t_matrix_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_matrix_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getRowMap()->lib(); if (lib == Xpetra::UseEpetra){ throw std::logic_error("compiler should have used specialization"); } else{ // Do the import with the Tpetra::CrsMatrix traits object const xt_matrix_t *xtm = dynamic_cast(&from); RCP tm = xtm->getTpetra_CrsMatrix(); RCP tmnew = XpetraTraits::doMigration( *tm, numLocalRows, myNewRows); RCP xmnew = XpetraTraits::convertToXpetra(tmnew); return xmnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Xpetra::CrsMatrix specialization template struct XpetraTraits > { typedef double scalar_t; typedef int lno_t; typedef int gno_t; typedef Xpetra::CrsMatrix x_matrix_t; typedef Xpetra::TpetraCrsMatrix xt_matrix_t; typedef Tpetra::CrsMatrix t_matrix_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_matrix_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getRowMap()->lib(); if (lib == Xpetra::UseEpetra){ #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) typedef Epetra_CrsMatrix e_matrix_t; typedef Xpetra::EpetraCrsMatrixT xe_matrix_t; // Do the import with the Epetra_CrsMatrix traits object const xe_matrix_t *xem = dynamic_cast(&from); RCP em = xem->getEpetra_CrsMatrix(); RCP emnew = XpetraTraits::doMigration( *em, numLocalRows, myNewRows); RCP xmnew = XpetraTraits::convertToXpetra(emnew); return xmnew; #else throw std::runtime_error("Xpetra with Epetra requested, but " "Trilinos is not built with Epetra"); #endif } else{ // Do the import with the Tpetra::CrsMatrix traits object const xt_matrix_t *xtm = dynamic_cast(&from); RCP tm = xtm->getTpetra_CrsMatrix(); RCP tmnew = XpetraTraits::doMigration( *tm, numLocalRows, myNewRows); RCP xmnew = XpetraTraits::convertToXpetra(tmnew); return xmnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Tpetra::CrsGraph template struct XpetraTraits > { typedef typename Xpetra::CrsGraph xgraph_t; typedef typename Xpetra::TpetraCrsGraph xtgraph_t; typedef typename Tpetra::CrsGraph tgraph_t; static inline RCP convertToXpetra(const RCP &a) { return rcp(new xtgraph_t(a)); } static RCP doMigration(const tgraph_t &from, size_t numLocalRows, const gno_t *myNewRows) { typedef Tpetra::Map map_t; // source map const RCP &smap = from.getRowMap(); int oldNumElts = smap->getLocalNumElements(); gno_t numGlobalRows = smap->getGlobalNumElements(); gno_t base = smap->getMinAllGlobalIndex(); // target map ArrayView rowList(myNewRows, numLocalRows); const RCP > &comm = from.getComm(); RCP tmap = rcp(new map_t(numGlobalRows, rowList, base, comm)); // importer Tpetra::Import importer(smap, tmap); // number of entries in my new rows typedef Tpetra::Vector vector_t; vector_t numOld(smap); vector_t numNew(tmap); for (int lid=0; lid < oldNumElts; lid++){ numOld.replaceGlobalValue(smap->getGlobalElement(lid), from.getNumEntriesInLocalRow(lid)); } numNew.doImport(numOld, importer, Tpetra::INSERT); size_t numElts = tmap->getLocalNumElements(); ArrayRCP nnz; if (numElts > 0) nnz = numNew.getData(0); // hangs if vector len == 0 ArrayRCP nnz_size_t; if (numElts && sizeof(gno_t) != sizeof(size_t)){ size_t *vals = new size_t [numElts]; nnz_size_t = arcp(vals, 0, numElts, true); for (size_t i=0; i < numElts; i++){ vals[i] = static_cast(nnz[i]); } } else{ nnz_size_t = arcp_reinterpret_cast(nnz); } // target graph RCP G = rcp(new tgraph_t(tmap, nnz_size_t())); G->doImport(from, importer, Tpetra::INSERT); G->fillComplete(); return G; } }; ////////////////////////////////////////////////////////////////////////////// #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) // Epetra_CrsGraph template < > struct XpetraTraits { typedef InputTraits::lno_t lno_t; typedef InputTraits::gno_t gno_t; typedef InputTraits::node_t node_t; static inline RCP > convertToXpetra(const RCP &a) { RCP > xa; try { xa = rcp(new Xpetra::EpetraCrsGraphT(a)); } catch (std::exception &e) { if (std::is_same::value) throw std::runtime_error(std::string("Cannot convert from " "Epetra_CrsGraph to " "Xpetra::EpetraCrsGraphT\n") + e.what()); else throw std::runtime_error(std::string("Cannot convert from " "Epetra_CrsGraph to " "Xpetra::EpetraCrsGraphT\n" "Use node_t that is supported by " "Xpetra with Epetra classes\n") + e.what()); } return xa; } static RCP doMigration(const Epetra_CrsGraph &from, size_t numLocalRows, const gno_t *myNewRows) { // source map const Epetra_BlockMap &smap = from.RowMap(); gno_t numGlobalRows = smap.NumGlobalElements(); lno_t oldNumElts = smap.NumMyElements(); int base = smap.MinAllGID(); // target map const Epetra_Comm &comm = from.Comm(); Epetra_BlockMap tmap(numGlobalRows, numLocalRows, myNewRows, 1, base, comm); lno_t newNumElts = tmap.NumMyElements(); // importer Epetra_Import importer(tmap, smap); // number of non zeros in my new rows Epetra_Vector numOld(smap); Epetra_Vector numNew(tmap); for (int lid=0; lid < oldNumElts; lid++){ numOld[lid] = from.NumMyIndices(lid); } numNew.Import(numOld, importer, Insert); Array nnz(newNumElts); for (int lid=0; lid < newNumElts; lid++){ nnz[lid] = static_cast(numNew[lid]); } // target graph RCP G = rcp(new Epetra_CrsGraph(::Copy, tmap, nnz.getRawPtr(), true)); G->Import(from, importer, Insert); G->FillComplete(); return G; } }; #endif ////////////////////////////////////////////////////////////////////////////// // Xpetra::CrsGraph // KDDKDD Do we need specializations for Xpetra::TpetraCrsGraph and // KDDKDD Xpetra::EpetraCrsGraph? template struct XpetraTraits > { typedef Xpetra::CrsGraph x_graph_t; typedef Xpetra::TpetraCrsGraph xt_graph_t; typedef Tpetra::CrsGraph t_graph_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_graph_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getRowMap()->lib(); if (lib == Xpetra::UseEpetra){ throw std::logic_error("compiler should have used specialization"); } else{ // Do the import with the Tpetra::CrsGraph traits object const xt_graph_t *xtg = dynamic_cast(&from); RCP tg = xtg->getTpetra_CrsGraph(); RCP tgnew = XpetraTraits::doMigration( *tg, numLocalRows, myNewRows); RCP xgnew = XpetraTraits::convertToXpetra(tgnew); return xgnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Xpetra::CrsGraph specialization template < typename node_t> struct XpetraTraits > { typedef int lno_t; typedef int gno_t; typedef Xpetra::CrsGraph x_graph_t; typedef Xpetra::TpetraCrsGraph xt_graph_t; typedef Tpetra::CrsGraph t_graph_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_graph_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getRowMap()->lib(); if (lib == Xpetra::UseEpetra){ #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) typedef Xpetra::EpetraCrsGraphT xe_graph_t; typedef Epetra_CrsGraph e_graph_t; // Do the import with the Epetra_CrsGraph traits object const xe_graph_t *xeg = dynamic_cast(&from); RCP eg = xeg->getEpetra_CrsGraph(); RCP egnew = XpetraTraits::doMigration( *eg, numLocalRows, myNewRows); RCP xgnew = XpetraTraits::convertToXpetra(egnew); return xgnew; #else throw std::runtime_error("Xpetra with Epetra requested, but " "Trilinos is not built with Epetra"); #endif } else{ // Do the import with the Tpetra::CrsGraph traits object const xt_graph_t *xtg = dynamic_cast(&from); RCP tg = xtg->getTpetra_CrsGraph(); RCP tgnew = XpetraTraits::doMigration( *tg, numLocalRows, myNewRows); RCP xgnew = XpetraTraits::convertToXpetra(tgnew); return xgnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Tpetra::Vector template struct XpetraTraits > { typedef Tpetra::Vector t_vector_t; typedef Xpetra::TpetraVector xt_vector_t; typedef Xpetra::Vector x_vector_t; static inline RCP convertToXpetra(const RCP &a) { return rcp(new xt_vector_t(a)); } static RCP doMigration(const t_vector_t &from, size_t numLocalElts, const gno_t *myNewElts) { typedef Tpetra::Map map_t; // source map const RCP &smap = from.getMap(); gno_t numGlobalElts = smap->getGlobalNumElements(); gno_t base = smap->getMinAllGlobalIndex(); // target map ArrayView eltList(myNewElts, numLocalElts); const RCP > comm = from.getMap()->getComm(); RCP tmap = rcp(new map_t(numGlobalElts, eltList, base, comm)); // importer Tpetra::Import importer(smap, tmap); // target vector RCP V = Tpetra::createVector(tmap); V->doImport(from, importer, Tpetra::INSERT); return V; } }; ////////////////////////////////////////////////////////////////////////////// #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) // Epetra_Vector template < > struct XpetraTraits { typedef InputTraits::lno_t lno_t; typedef InputTraits::gno_t gno_t; typedef InputTraits::node_t node_t; typedef InputTraits::scalar_t scalar_t; typedef Xpetra::Vector x_vector_t; static inline RCP convertToXpetra(const RCP &a) { RCP > xev; try { xev = rcp(new Xpetra::EpetraVectorT(a)); } catch (std::exception &e) { if (std::is_same::value) throw std::runtime_error(std::string("Cannot convert from " "Epetra_Vector to " "Xpetra::EpetraVectorT\n") + e.what()); else throw std::runtime_error(std::string("Cannot convert from " "Epetra_Vector to " "Xpetra::EpetraVectorT\n" "Use node_t that is supported by " "Xpetra with Epetra classes\n") + e.what()); } return rcp_implicit_cast(xev); } static RCP doMigration(const Epetra_Vector &from, size_t numLocalElts, const gno_t *myNewElts) { // source map const Epetra_BlockMap &smap = from.Map(); gno_t numGlobalElts = smap.NumGlobalElements(); int base = smap.MinAllGID(); // target map const Epetra_Comm &comm = from.Comm(); const Epetra_BlockMap tmap(numGlobalElts, numLocalElts, myNewElts, 1, base, comm); // importer Epetra_Import importer(tmap, smap); // target vector RCP V = rcp(new Epetra_Vector(tmap, true)); Epetra_CombineMode c = Insert; V->Import(from, importer, c); return V; } }; #endif ////////////////////////////////////////////////////////////////////////////// // Xpetra::Vector template struct XpetraTraits > { typedef Xpetra::Vector x_vector_t; typedef Xpetra::TpetraVector xt_vector_t; typedef Tpetra::Vector t_vector_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_vector_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getMap()->lib(); if (lib == Xpetra::UseEpetra){ throw std::logic_error("compiler should have used specialization"); } else{ // Do the import with the Tpetra::Vector traits object const xt_vector_t *xtv = dynamic_cast(&from); RCP tv = xtv->getTpetra_Vector(); RCP tvnew = XpetraTraits::doMigration( *tv, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(tvnew); return xvnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Xpetra::Vector specialization template struct XpetraTraits > { typedef double scalar_t; typedef int lno_t; typedef int gno_t; typedef Xpetra::Vector x_vector_t; typedef Xpetra::TpetraVector xt_vector_t; typedef Tpetra::Vector t_vector_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_vector_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getMap()->lib(); if (lib == Xpetra::UseEpetra){ #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) typedef Epetra_Vector e_vector_t; typedef Xpetra::EpetraVectorT xe_vector_t; // Do the import with the Epetra_Vector traits object const xe_vector_t *xev = dynamic_cast(&from); RCP ev = rcp(xev->getEpetra_Vector()); RCP evnew = XpetraTraits::doMigration( *ev, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(evnew); return xvnew; #else throw std::runtime_error("Xpetra with Epetra requested, but " "Trilinos is not built with Epetra"); #endif } else{ // Do the import with the Tpetra::Vector traits object const xt_vector_t *xtv = dynamic_cast(&from); RCP tv = xtv->getTpetra_Vector(); RCP tvnew = XpetraTraits::doMigration( *tv, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(tvnew); return xvnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Tpetra::MultiVector template struct XpetraTraits > { typedef Tpetra::MultiVector t_vector_t; typedef Xpetra::TpetraMultiVector xt_vector_t; typedef Xpetra::MultiVector x_vector_t; static inline RCP convertToXpetra(const RCP &a) { return rcp(new xt_vector_t(a)); } static RCP doMigration(const t_vector_t &from, size_t numLocalElts, const gno_t *myNewElts) { typedef Tpetra::Map map_t; // source map const RCP &smap = from.getMap(); gno_t numGlobalElts = smap->getGlobalNumElements(); gno_t base = smap->getMinAllGlobalIndex(); // target map ArrayView eltList(myNewElts, numLocalElts); const RCP > comm = from.getMap()->getComm(); RCP tmap = rcp(new map_t(numGlobalElts, eltList, base, comm)); // importer Tpetra::Import importer(smap, tmap); // target vector RCP MV = rcp( new t_vector_t(tmap, from.getNumVectors(), true)); MV->doImport(from, importer, Tpetra::INSERT); return MV; } }; ////////////////////////////////////////////////////////////////////////////// #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) // Epetra_MultiVector template < > struct XpetraTraits { typedef InputTraits::lno_t lno_t; typedef InputTraits::gno_t gno_t; typedef InputTraits::node_t node_t; typedef InputTraits::scalar_t scalar_t; typedef Xpetra::MultiVector x_mvector_t; static inline RCP convertToXpetra( const RCP &a) { RCP > xemv; try { xemv = rcp(new Xpetra::EpetraMultiVectorT(a)); } catch (std::exception &e) { if (std::is_same::value) throw std::runtime_error(std::string("Cannot convert from " "Epetra_MultiVector to " "Xpetra::EpetraMultiVectorT\n") + e.what()); else throw std::runtime_error(std::string("Cannot convert from " "Epetra_MultiVector to " "Xpetra::EpetraMultiVectorT\n" "Use node_t that is supported by " "Xpetra with Epetra classes\n") + e.what()); } return rcp_implicit_cast(xemv); } static RCP doMigration(const Epetra_MultiVector &from, size_t numLocalElts, const gno_t *myNewElts) { // source map const Epetra_BlockMap &smap = from.Map(); gno_t numGlobalElts = smap.NumGlobalElements(); int base = smap.MinAllGID(); // target map const Epetra_Comm &comm = from.Comm(); const Epetra_BlockMap tmap(numGlobalElts, numLocalElts, myNewElts, 1, base, comm); // importer Epetra_Import importer(tmap, smap); // target vector RCP MV = rcp( new Epetra_MultiVector(tmap, from.NumVectors(), true)); Epetra_CombineMode c = Insert; MV->Import(from, importer, c); return MV; } }; #endif ////////////////////////////////////////////////////////////////////////////// // Xpetra::MultiVector template struct XpetraTraits > { typedef Xpetra::MultiVector x_mvector_t; typedef Xpetra::TpetraMultiVector xt_mvector_t; typedef Tpetra::MultiVector t_mvector_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_mvector_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getMap()->lib(); if (lib == Xpetra::UseEpetra){ throw std::logic_error("compiler should have used specialization"); } else{ // Do the import with the Tpetra::MultiVector traits object const xt_mvector_t *xtv = dynamic_cast(&from); RCP tv = xtv->getTpetra_MultiVector(); RCP tvnew = XpetraTraits::doMigration( *tv, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(tvnew); return xvnew; } } }; ////////////////////////////////////////////////////////////////////////////// // Xpetra::MultiVector specialization template struct XpetraTraits > { typedef double scalar_t; typedef int lno_t; typedef int gno_t; typedef Xpetra::MultiVector x_mvector_t; typedef Xpetra::TpetraMultiVector xt_mvector_t; typedef Tpetra::MultiVector t_mvector_t; static inline RCP convertToXpetra(const RCP &a) { return a; } static RCP doMigration(const x_mvector_t &from, size_t numLocalRows, const gno_t *myNewRows) { Xpetra::UnderlyingLib lib = from.getMap()->lib(); if (lib == Xpetra::UseEpetra){ #if defined(HAVE_ZOLTAN2_EPETRA) && defined(HAVE_XPETRA_EPETRA) typedef Epetra_MultiVector e_mvector_t; typedef Xpetra::EpetraMultiVectorT xe_mvector_t; // Do the import with the Epetra_MultiVector traits object const xe_mvector_t *xev = dynamic_cast(&from); RCP ev = xev->getEpetra_MultiVector(); RCP evnew = XpetraTraits::doMigration( *ev, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(evnew); return xvnew; #else throw std::runtime_error("Xpetra with Epetra requested, but " "Trilinos is not built with Epetra"); #endif } else{ // Do the import with the Tpetra::MultiVector traits object const xt_mvector_t *xtv = dynamic_cast(&from); RCP tv = xtv->getTpetra_MultiVector(); RCP tvnew = XpetraTraits::doMigration( *tv, numLocalRows, myNewRows); RCP xvnew = XpetraTraits::convertToXpetra(tvnew); return xvnew; } } }; #endif // DOXYGEN_SHOULD_SKIP_THIS } //namespace Zoltan2 #endif // _ZOLTAN2_XPETRATRAITS_HPP_