46 #ifndef MUELU_ZOLTANINTERFACE_DEF_HPP 47 #define MUELU_ZOLTANINTERFACE_DEF_HPP 50 #if defined(HAVE_MUELU_ZOLTAN) && defined(HAVE_MPI) 52 #include <Teuchos_Utils.hpp> 53 #include <Teuchos_DefaultMpiComm.hpp> 54 #include <Teuchos_OpaqueWrapper.hpp> 59 #include "MueLu_Utilities.hpp" 63 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
65 RCP<ParameterList> validParamList = rcp(
new ParameterList());
67 validParamList->set< RCP<const FactoryBase> >(
"A", Teuchos::null,
"Factory of the matrix A");
68 validParamList->set< RCP<const FactoryBase> >(
"Coordinates", Teuchos::null,
"Factory of the coordinates");
70 return validParamList;
74 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
76 Input(currentLevel,
"A");
77 Input(currentLevel,
"Coordinates");
80 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
84 RCP<Matrix> A = Get< RCP<Matrix> > (level,
"A");
85 RCP<const Map> rowMap = A->getRowMap();
87 RCP<MultiVector> Coords = Get< RCP<MultiVector> >(level,
"Coordinates");
88 size_t dim = Coords->getNumVectors();
90 GO numParts = level.Get<GO>(
"number of partitions");
94 RCP<Xpetra::Vector<GO, LO, GO, NO> > decomposition = Xpetra::VectorFactory<GO, LO, GO, NO>::Build(rowMap,
true);
95 Set(level,
"Partition", decomposition);
100 Zoltan_Initialize(0, NULL, &zoltanVersion_);
102 RCP<const Teuchos::MpiComm<int> > dupMpiComm = rcp_dynamic_cast<
const Teuchos::MpiComm<int> >(rowMap->getComm()->duplicate());
103 RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > zoltanComm = dupMpiComm->getRawMpiComm();
105 RCP<Zoltan> zoltanObj_ = rcp(
new Zoltan((*zoltanComm)()));
106 if (zoltanObj_ == Teuchos::null)
113 if ((rv = zoltanObj_->Set_Param(
"num_gid_entries",
"1")) != ZOLTAN_OK)
115 if ((rv = zoltanObj_->Set_Param(
"num_lid_entries",
"0") ) != ZOLTAN_OK)
117 if ((rv = zoltanObj_->Set_Param(
"obj_weight_dim",
"1") ) != ZOLTAN_OK)
120 if (GetVerbLevel() &
Statistics1) zoltanObj_->Set_Param(
"debug_level",
"1");
121 else zoltanObj_->Set_Param(
"debug_level",
"0");
123 zoltanObj_->Set_Param(
"num_global_partitions",
toString(numParts));
125 zoltanObj_->Set_Num_Obj_Fn(GetLocalNumberOfRows, (
void *) &*A);
126 zoltanObj_->Set_Obj_List_Fn(GetLocalNumberOfNonzeros, (
void *) &*A);
127 zoltanObj_->Set_Num_Geom_Fn(GetProblemDimension, (
void *) &dim);
128 zoltanObj_->Set_Geom_Multi_Fn(GetProblemGeometry, (
void *) Coords.get());
131 ZOLTAN_ID_PTR import_gids = NULL;
132 ZOLTAN_ID_PTR import_lids = NULL;
133 int *import_procs = NULL;
134 int *import_to_part = NULL;
135 ZOLTAN_ID_PTR export_gids = NULL;
136 ZOLTAN_ID_PTR export_lids = NULL;
137 int *export_procs = NULL;
138 int *export_to_part = NULL;
147 rv = zoltanObj_->LB_Partition(newDecomp, num_gid_entries, num_lid_entries,
148 num_imported, import_gids, import_lids, import_procs, import_to_part,
149 num_exported, export_gids, export_lids, export_procs, export_to_part);
150 if (rv == ZOLTAN_FATAL)
156 RCP<Xpetra::Vector<GO, LO, GO, NO> > decomposition;
158 decomposition = Xpetra::VectorFactory<GO, LO, GO, NO>::Build(rowMap,
false);
159 ArrayRCP<GO> decompEntries = decomposition->getDataNonConst(0);
161 int mypid = rowMap->getComm()->getRank();
162 for (
typename ArrayRCP<GO>::iterator i = decompEntries.begin(); i != decompEntries.end(); ++i)
165 LO blockSize = A->GetFixedBlockSize();
166 for (
int i = 0; i < num_exported; ++i) {
169 LO localEl = rowMap->getLocalElement(export_gids[i]);
170 int partNum = export_to_part[i];
171 for (LO j = 0; j < blockSize; ++j)
172 decompEntries[localEl + j] = partNum;
176 Set(level,
"Partition", decomposition);
178 zoltanObj_->LB_Free_Part(&import_gids, &import_lids, &import_procs, &import_to_part);
179 zoltanObj_->LB_Free_Part(&export_gids, &export_lids, &export_procs, &export_to_part);
187 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
190 *ierr = ZOLTAN_FATAL;
193 Matrix *A = (Matrix*) data;
196 LO blockSize = A->GetFixedBlockSize();
199 return A->getRowMap()->getNodeNumElements() / blockSize;
206 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
209 ZOLTAN_ID_PTR lids,
int wgtDim,
float *weights,
int *ierr) {
210 if (data == NULL || NumGidEntries < 1) {
211 *ierr = ZOLTAN_FATAL;
217 Matrix *A = (Matrix*) data;
218 RCP<const Map> map = A->getRowMap();
220 LO blockSize = A->GetFixedBlockSize();
223 size_t numElements = map->getNodeNumElements();
224 ArrayView<const GO> mapGIDs = map->getNodeElementList();
226 if (blockSize == 1) {
227 for (
size_t i = 0; i < numElements; i++) {
228 gids[i] = as<ZOLTAN_ID_TYPE>(mapGIDs[i]);
229 weights[i] = A->getNumEntriesInLocalRow(i);
233 LO numBlockElements = numElements / blockSize;
235 for (LO i = 0; i < numBlockElements; i++) {
238 gids[i] = as<ZOLTAN_ID_TYPE>(mapGIDs[i*blockSize]);
240 for (LO j = 0; j < blockSize; j++)
241 weights[i] += A->getNumEntriesInLocalRow(i*blockSize+j);
251 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
255 int dim = *((
int*)data);
265 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
268 ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids,
int dim,
double *coordinates,
int *ierr)
271 *ierr = ZOLTAN_FATAL;
275 MultiVector *Coords = (MultiVector*) data;
277 if (dim != Teuchos::as<int>(Coords->getNumVectors())) {
279 *ierr = ZOLTAN_FATAL;
283 TEUCHOS_TEST_FOR_EXCEPTION(numObjectIDs != Teuchos::as<int>(Coords->getLocalLength()),
Exceptions::Incompatible,
"Length of coordinates must be the same as the number of objects");
285 ArrayRCP<ArrayRCP<const SC> > CoordsData(dim);
286 for (
int j = 0; j < dim; ++j)
287 CoordsData[j] = Coords->getData(j);
289 size_t numElements = Coords->getLocalLength();
290 for (
size_t i = 0; i < numElements; ++i)
291 for (
int j = 0; j < dim; ++j)
292 coordinates[i*dim+j] = (
double) CoordsData[j][i];
300 #endif //if defined(HAVE_MUELU_ZOLTAN) && defined(HAVE_MPI) 302 #endif // MUELU_ZOLTANINTERFACE_DEF_HPP
static int GetProblemDimension(void *data, int *ierr)
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
static int GetLocalNumberOfRows(void *data, int *ierr)
static void GetProblemGeometry(void *data, int numGIDEntries, int numLIDEntries, int numObjectIDs, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int dim, double *coordinates, int *ierr)
Timer to be used in factories. Similar to Monitor but with additional timers.
Namespace for MueLu classes and methods.
Exception throws to report incompatible objects (like maps).
static void GetLocalNumberOfNonzeros(void *data, int NumGidEntries, int NumLidEntries, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, int wgtDim, float *weights, int *ierr)
Class that holds all level-specific information.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void Build(Level &level) const
Build an object with this factory.
Exception throws to report errors in the internal logical of the program.
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.