| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// ShyLU: Scalable Hybrid LU Preconditioner and Solver |
| 4 |
// |
| 5 |
// Copyright 2011 NTESS and the ShyLU contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef _FROSCH_COARSEOPERATOR_DEF_HPP |
| 11 |
#define _FROSCH_COARSEOPERATOR_DEF_HPP |
| 12 |
|
| 13 |
#include <FROSch_CoarseOperator_decl.hpp> |
| 14 |
|
| 15 |
|
| 16 |
namespace FROSch { |
| 17 |
|
| 18 |
using namespace std; |
| 19 |
using namespace Teuchos; |
| 20 |
using namespace Xpetra; |
| 21 |
|
| 22 |
template<class SC,class LO,class GO,class NO> |
| 23 |
CoarseOperator<SC,LO,GO,NO>::CoarseOperator(ConstXMatrixPtr k, |
| 24 |
ParameterListPtr parameterList) : |
| 25 |
SchwarzOperator<SC,LO,GO,NO> (k,parameterList), |
| 26 |
CoarseSpace_ (new CoarseSpace<SC,LO,GO,NO>(this->MpiComm_,this->SerialComm_)), |
| 27 |
DistributionList_ (sublist(parameterList,"Distribution")) |
| 28 |
{ |
| 29 |
FROSCH_DETAILTIMER_START_LEVELID(coarseOperatorTime,"CoarseOperator::CoarseOperator"); |
| 30 |
} |
| 31 |
|
| 32 |
template<class SC,class LO,class GO,class NO> |
| 33 |
CoarseOperator<SC,LO,GO,NO>::~CoarseOperator() |
| 34 |
{ |
| 35 |
CoarseSolver_.reset(); |
| 36 |
} |
| 37 |
|
| 38 |
template <class SC,class LO,class GO,class NO> |
| 39 |
int CoarseOperator<SC,LO,GO,NO>::compute() |
| 40 |
{ |
| 41 |
FROSCH_TIMER_START_LEVELID(computeTime,"CoarseOperator::compute"); |
| 42 |
FROSCH_ASSERT(this->IsInitialized_,"FROSch::CoarseOperator: CoarseOperator has to be initialized before calling compute()"); |
| 43 |
// This is not optimal yet... Some work could be moved to Initialize |
| 44 |
//if (this->Verbose_) cout << "FROSch::CoarseOperator : WARNING: Some of the operations could probably be moved from initialize() to Compute().\n"; |
| 45 |
|
| 46 |
bool reuseCoarseBasis = this->ParameterList_->get("Reuse: Coarse Basis",true); |
| 47 |
bool reuseCoarseMatrix = this->ParameterList_->get("Reuse: Coarse Matrix",false); |
| 48 |
if (!this->IsComputed_) { |
| 49 |
reuseCoarseBasis = false; |
| 50 |
reuseCoarseMatrix = false; |
| 51 |
} |
| 52 |
|
| 53 |
if (!reuseCoarseBasis) { |
| 54 |
if (this->IsComputed_ && this->Verbose_) cout << "FROSch::CoarseOperator : Recomputing the Coarse Basis" << endl; |
| 55 |
clearCoarseSpace(); // AH 12/11/2018: If we do not clear the coarse space, we will always append just append the coarse space |
| 56 |
ConstXMapPtr subdomainMap = this->computeCoarseSpace(CoarseSpace_); // AH 12/11/2018: This map could be overlapping, repeated, or unique. This depends on the specific coarse operator |
| 57 |
if (CoarseSpace_->hasUnassembledMaps()) { // If there is no unassembled basis, the current Phi_ should already be correct |
| 58 |
CoarseSpace_->assembleCoarseSpace(); |
| 59 |
FROSCH_ASSERT(CoarseSpace_->hasAssembledBasis(),"FROSch::CoarseOperator : !CoarseSpace_->hasAssembledBasis()"); |
| 60 |
CoarseSpace_->buildGlobalBasisMatrix(this->K_->getRowMap(),this->K_->getRangeMap(),subdomainMap,this->ParameterList_->get("Phi: Dropping Threshold",1.e-8)); |
| 61 |
FROSCH_ASSERT(CoarseSpace_->hasGlobalBasisMatrix(),"FROSch::CoarseOperator : !CoarseSpace_->hasGlobalBasisMatrix()"); |
| 62 |
Phi_ = CoarseSpace_->getGlobalBasisMatrix(); |
| 63 |
} |
| 64 |
} |
| 65 |
if (!reuseCoarseMatrix) { |
| 66 |
if (this->IsComputed_ && this->Verbose_) cout << "FROSch::CoarseOperator : Recomputing the Coarse Matrix" << endl; |
| 67 |
this->setUpCoarseOperator(); |
| 68 |
} |
| 69 |
this->IsComputed_ = true; |
| 70 |
|
| 71 |
// Store current Phi in ParameterList_ |
| 72 |
if ( this->ParameterList_->get("Store Phi",false) ) { |
| 73 |
FROSCH_NOTIFICATION("FROSch::CoarseOperator",this->Verbose_,"Storing current Phi in Parameterlist."); |
| 74 |
this->ParameterList_->set("RCP(Phi)", Phi_); |
| 75 |
} |
| 76 |
|
| 77 |
// Store current Coarse Matrix in ParameterList_ |
| 78 |
if ( this->ParameterList_->get("Store Coarse Matrix",false) ) { |
| 79 |
FROSCH_NOTIFICATION("FROSch::CoarseOperator",this->Verbose_,"Storing current Coarse Matrix in Parameterlist."); |
| 80 |
this->ParameterList_->set("RCP(Coarse Matrix)", CoarseMatrix_); |
| 81 |
this->ParameterList_->set("bool(CoarseSolveComm)", OnCoarseSolveComm_); |
| 82 |
} |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
template <class SC,class LO,class GO,class NO> |
| 88 |
int CoarseOperator<SC,LO,GO,NO>::clearCoarseSpace() |
| 89 |
{ |
| 90 |
return CoarseSpace_->clearCoarseSpace(); |
| 91 |
} |
| 92 |
|
| 93 |
template<class SC,class LO,class GO,class NO> |
| 94 |
void CoarseOperator<SC,LO,GO,NO>::apply(const XMultiVector &x, |
| 95 |
XMultiVector &y, |
| 96 |
bool usePreconditionerOnly, |
| 97 |
ETransp mode, |
| 98 |
SC alpha, |
| 99 |
SC beta) const |
| 100 |
{ |
| 101 |
FROSCH_TIMER_START_LEVELID(applyTime,"CoarseOperator::apply"); |
| 102 |
static int i = 0; |
| 103 |
if (!Phi_.is_null() && this->IsComputed_) { |
| 104 |
if (XTmp_.is_null() || XTmp_->getNumVectors() != x.getNumVectors()) { |
| 105 |
XTmp_ = MultiVectorFactory<SC,LO,GO,NO>::Build(x.getMap(),x.getNumVectors()); |
| 106 |
} |
| 107 |
*XTmp_ = x; |
| 108 |
if (!usePreconditionerOnly && mode == NO_TRANS) { |
| 109 |
this->K_->apply(x,*XTmp_,mode,ScalarTraits<SC>::one(),ScalarTraits<SC>::zero()); |
| 110 |
} |
| 111 |
if (XCoarseSolve_.is_null() || XCoarseSolve_->getNumVectors() != x.getNumVectors()) { |
| 112 |
XCoarseSolve_ = MultiVectorFactory<SC,LO,GO,NO>::Build(GatheringMaps_[GatheringMaps_.size()-1],x.getNumVectors()); |
| 113 |
} else { |
| 114 |
XCoarseSolve_->replaceMap(GatheringMaps_[GatheringMaps_.size()-1]); // The map is replaced in applyCoarseSolve(). If we do not build it from scratch, we should at least replace the map here. This may be important since the maps live on different communicators. |
| 115 |
} |
| 116 |
if (YCoarseSolve_.is_null() || YCoarseSolve_->getNumVectors() != y.getNumVectors()) { |
| 117 |
YCoarseSolve_ = MultiVectorFactory<SC,LO,GO,NO>::Build(GatheringMaps_[GatheringMaps_.size()-1],y.getNumVectors()); |
| 118 |
} |
| 119 |
applyPhiT(*XTmp_,*XCoarseSolve_); |
| 120 |
applyCoarseSolve(*XCoarseSolve_,*YCoarseSolve_,mode); |
| 121 |
applyPhi(*YCoarseSolve_,*XTmp_); |
| 122 |
if (!usePreconditionerOnly && mode != NO_TRANS) { |
| 123 |
this->K_->apply(*XTmp_,*XTmp_,mode,ScalarTraits<SC>::one(),ScalarTraits<SC>::zero()); |
| 124 |
} |
| 125 |
y.update(alpha,*XTmp_,beta); |
| 126 |
} else { |
| 127 |
if (i==0) { |
| 128 |
FROSCH_WARNING("FROSch::CoarseOperator",(this->Verbose_ && Phi_.is_null()),"Coarse Basis is empty => The CoarseOperator will just act as the identity..."); |
| 129 |
FROSCH_WARNING("FROSch::CoarseOperator",(this->Verbose_ && !this->IsComputed_),"CoarseOperator has not been computed yet => The CoarseOperator will just act as the identity..."); |
| 130 |
i++; |
| 131 |
} |
| 132 |
y.update(ScalarTraits<SC>::one(),x,ScalarTraits<SC>::zero()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
template<class SC,class LO,class GO,class NO> |
| 137 |
void CoarseOperator<SC,LO,GO,NO>::applyPhiT(const XMultiVector& x, |
| 138 |
XMultiVector& y) const |
| 139 |
{ |
| 140 |
FROSCH_DETAILTIMER_START_LEVELID(applyPhiTTime,"CoarseOperator::applyPhiT"); |
| 141 |
// AH 08/22/2019 TODO: We cannot ger rid of the Build() calls because of "XCoarse_ = XCoarseSolveTmp_;". This is basically caused by the whole Gathering Map strategy. As soon as we have replaced this, we can get rid of the Build() calls |
| 142 |
XCoarse_ = MultiVectorFactory<SC,LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique(),x.getNumVectors()); // AH 08/22/2019 TODO: Can we get rid of this? If possible, we should remove the whole GatheringMaps idea and replace it by some smart all-to-all MPI communication |
| 143 |
{ |
| 144 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 145 |
FROSCH_DETAILTIMER_START_LEVELID(applyTime,"apply"); |
| 146 |
#endif |
| 147 |
Phi_->apply(x,*XCoarse_,Teuchos::TRANS); |
| 148 |
} |
| 149 |
for (UN j=0; j<GatheringMaps_.size(); j++) { |
| 150 |
XCoarseSolveTmp_ = MultiVectorFactory<SC,LO,GO,NO>::Build(GatheringMaps_[j],x.getNumVectors()); // AH 08/22/2019 TODO: Can we get rid of this? If possible, we should remove the whole GatheringMaps idea and replace it by some smart all-to-all MPI communication |
| 151 |
{ |
| 152 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 153 |
FROSCH_DETAILTIMER_START_LEVELID(applyTime,"doExport"); |
| 154 |
#endif |
| 155 |
XCoarseSolveTmp_->doExport(*XCoarse_,*CoarseSolveExporters_[j],ADD); |
| 156 |
} |
| 157 |
XCoarse_ = XCoarseSolveTmp_; |
| 158 |
} |
| 159 |
y = *XCoarseSolveTmp_; |
| 160 |
} |
| 161 |
|
| 162 |
template<class SC,class LO,class GO,class NO> |
| 163 |
void CoarseOperator<SC,LO,GO,NO>::applyCoarseSolve(XMultiVector& x, |
| 164 |
XMultiVector& y, |
| 165 |
ETransp mode) const |
| 166 |
{ |
| 167 |
FROSCH_DETAILTIMER_START_LEVELID(applyCoarseSolveTime,"CoarseOperator::applyCoarseSolve"); |
| 168 |
if (OnCoarseSolveComm_) { |
| 169 |
x.replaceMap(CoarseSolveMap_); |
| 170 |
if (YTmp_.is_null() || YTmp_->getNumVectors() != x.getNumVectors()) { |
| 171 |
YTmp_ = MultiVectorFactory<SC,LO,GO,NO>::Build(CoarseSolveMap_,x.getNumVectors()); |
| 172 |
} else { |
| 173 |
YTmp_->replaceMap(CoarseSolveMap_); // The map is replaced later in this function. If we do not build it from scratch, we should at least replace the map here. This may be important since the maps live on different communicators. |
| 174 |
} |
| 175 |
CoarseSolver_->apply(x,*YTmp_,mode); |
| 176 |
} else { |
| 177 |
if (YTmp_.is_null() || YTmp_->getNumVectors() != x.getNumVectors()) { |
| 178 |
YTmp_ = MultiVectorFactory<SC,LO,GO,NO>::Build(CoarseSolveMap_,x.getNumVectors()); |
| 179 |
} else { |
| 180 |
YTmp_->replaceMap(CoarseSolveMap_); // The map is replaced later in this function. If we do not build it from scratch, we should at least replace the map here. This may be important since the maps live on different communicators. |
| 181 |
} |
| 182 |
} |
| 183 |
YTmp_->replaceMap(GatheringMaps_[GatheringMaps_.size()-1]); |
| 184 |
y = *YTmp_; |
| 185 |
} |
| 186 |
|
| 187 |
template<class SC,class LO,class GO,class NO> |
| 188 |
void CoarseOperator<SC,LO,GO,NO>::applyPhi(const XMultiVector& x, |
| 189 |
XMultiVector& y) const |
| 190 |
{ |
| 191 |
FROSCH_DETAILTIMER_START_LEVELID(applyPhiTime,"CoarseOperator::applyPhi"); |
| 192 |
// AH 08/22/2019 TODO: We have the same issue here as in applyPhiT() |
| 193 |
YCoarseSolveTmp_ = MultiVectorFactory<SC,LO,GO,NO>::Build(x.getMap(),x.getNumVectors()); |
| 194 |
*YCoarseSolveTmp_ = x; |
| 195 |
for (int j=GatheringMaps_.size()-1; j>0; j--) { |
| 196 |
YCoarse_ = MultiVectorFactory<SC,LO,GO,NO>::Build(GatheringMaps_[j-1],x.getNumVectors()); |
| 197 |
{ |
| 198 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 199 |
FROSCH_DETAILTIMER_START_LEVELID(applyTime,"doImport"); |
| 200 |
#endif |
| 201 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 202 |
YCoarse_->doImport(*YCoarseSolveTmp_,*CoarseSolveImporters_[j],INSERT); |
| 203 |
#else |
| 204 |
YCoarse_->doImport(*YCoarseSolveTmp_,*CoarseSolveExporters_[j],INSERT); |
| 205 |
#endif |
| 206 |
} |
| 207 |
YCoarseSolveTmp_ = YCoarse_; |
| 208 |
} |
| 209 |
YCoarse_ = MultiVectorFactory<SC,LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique(),x.getNumVectors()); |
| 210 |
{ |
| 211 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 212 |
FROSCH_DETAILTIMER_START_LEVELID(applyTime,"doImport"); |
| 213 |
#endif |
| 214 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 215 |
YCoarse_->doImport(*YCoarseSolveTmp_,*CoarseSolveImporters_[0],INSERT); |
| 216 |
#else |
| 217 |
YCoarse_->doImport(*YCoarseSolveTmp_,*CoarseSolveExporters_[0],INSERT); |
| 218 |
#endif |
| 219 |
} |
| 220 |
{ |
| 221 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 222 |
FROSCH_DETAILTIMER_START_LEVELID(applyTime,"apply"); |
| 223 |
#endif |
| 224 |
Phi_->apply(*YCoarse_,y,NO_TRANS); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
template<class SC,class LO,class GO,class NO> |
| 229 |
typename CoarseOperator<SC,LO,GO,NO>::CoarseSpacePtr CoarseOperator<SC,LO,GO,NO>::getCoarseSpace() const |
| 230 |
{ |
| 231 |
return CoarseSpace_; |
| 232 |
} |
| 233 |
|
| 234 |
template<class SC,class LO,class GO,class NO> |
| 235 |
int CoarseOperator<SC,LO,GO,NO>::setUpCoarseOperator() |
| 236 |
{ |
| 237 |
FROSCH_DETAILTIMER_START_LEVELID(setUpCoarseOperatorTime,"CoarseOperator::setUpCoarseOperator"); |
| 238 |
if (!Phi_.is_null()) { |
| 239 |
// Build CoarseMatrix_ |
| 240 |
XMatrixPtr k0 = buildCoarseMatrix(); |
| 241 |
|
| 242 |
//------------------------------------------------------------------------------------------------------------------------ |
| 243 |
// Communicate coarse matrix |
| 244 |
FROSCH_DETAILTIMER_START_LEVELID(communicateCoarseMatrixTime,"communicate coarse matrix"); |
| 245 |
if (!DistributionList_->get("Type","linear").compare("linear")) { |
| 246 |
XMatrixPtr tmpCoarseMatrix = MatrixFactory<SC,LO,GO,NO>::Build(GatheringMaps_[0]); |
| 247 |
{ |
| 248 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 249 |
FROSCH_DETAILTIMER_START_LEVELID(coarseMatrixExportTime,"Export Coarse Matrix"); |
| 250 |
#endif |
| 251 |
tmpCoarseMatrix->doExport(*k0,*CoarseSolveExporters_[0],INSERT); |
| 252 |
} |
| 253 |
|
| 254 |
for (UN j=1; j<GatheringMaps_.size(); j++) { |
| 255 |
tmpCoarseMatrix->fillComplete(); |
| 256 |
k0 = tmpCoarseMatrix; |
| 257 |
tmpCoarseMatrix = MatrixFactory<SC,LO,GO,NO>::Build(GatheringMaps_[j]); |
| 258 |
{ |
| 259 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 260 |
FROSCH_DETAILTIMER_START_LEVELID(coarseMatrixExportTime,"Export Coarse Matrix"); |
| 261 |
#endif |
| 262 |
tmpCoarseMatrix->doExport(*k0,*CoarseSolveExporters_[j],INSERT); |
| 263 |
} |
| 264 |
} |
| 265 |
k0 = tmpCoarseMatrix; |
| 266 |
|
| 267 |
} else if (!DistributionList_->get("Type","linear").compare("ZoltanDual")) { |
| 268 |
//ZoltanDual |
| 269 |
CoarseSolveExporters_[0] = Xpetra::ExportFactory<LO,GO,NO>::Build(k0->getMap(),GatheringMaps_[0]); |
| 270 |
XMatrixPtr tmpCoarseMatrix = Xpetra::MatrixFactory<SC,LO,GO,NO>::Build(GatheringMaps_[0],k0->getGlobalMaxNumRowEntries()); |
| 271 |
tmpCoarseMatrix->doExport(*k0,*CoarseSolveExporters_[0],Xpetra::INSERT); |
| 272 |
|
| 273 |
for (UN j=1; j<GatheringMaps_.size(); j++) { |
| 274 |
tmpCoarseMatrix->fillComplete(); |
| 275 |
k0 = tmpCoarseMatrix; |
| 276 |
CoarseSolveExporters_[j] = Xpetra::ExportFactory<LO,GO,NO>::Build(GatheringMaps_[j-1],GatheringMaps_[j]); |
| 277 |
tmpCoarseMatrix = Xpetra::MatrixFactory<SC,LO,GO,NO>::Build(GatheringMaps_[j],k0->getGlobalMaxNumRowEntries()); |
| 278 |
tmpCoarseMatrix->doExport(*k0,*CoarseSolveExporters_[j],Xpetra::INSERT); |
| 279 |
} |
| 280 |
|
| 281 |
tmpCoarseMatrix->fillComplete(); |
| 282 |
k0 = tmpCoarseMatrix; |
| 283 |
|
| 284 |
} else if (!DistributionList_->get("Type","linear").compare("Zoltan2")) { |
| 285 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 286 |
GatheringMaps_[0] = rcp_const_cast<XMap> (BuildUniqueMap(k0->getRowMap())); |
| 287 |
CoarseSolveExporters_[0] = ExportFactory<LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique(),GatheringMaps_[0]); |
| 288 |
|
| 289 |
if (NumProcsCoarseSolve_ < this->MpiComm_->getSize()) { |
| 290 |
XMatrixPtr k0Unique = MatrixFactory<SC,LO,GO,NO>::Build(GatheringMaps_[0]); |
| 291 |
k0Unique->doExport(*k0,*CoarseSolveExporters_[0],INSERT); |
| 292 |
k0Unique->fillComplete(GatheringMaps_[0],GatheringMaps_[0]); |
| 293 |
|
| 294 |
if (NumProcsCoarseSolve_<this->MpiComm_->getSize()) { |
| 295 |
ParameterListPtr tmpList = sublist(DistributionList_,"Zoltan2 Parameter"); |
| 296 |
tmpList->set("num_global_parts",NumProcsCoarseSolve_); |
| 297 |
FROSch::RepartionMatrixZoltan2(k0Unique,tmpList); |
| 298 |
} |
| 299 |
|
| 300 |
k0 = k0Unique; |
| 301 |
GatheringMaps_[0] = k0->getRowMap(); |
| 302 |
CoarseSolveExporters_[0] = ExportFactory<LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique(),GatheringMaps_[0]); |
| 303 |
|
| 304 |
if (GatheringMaps_[0]->getLocalNumElements()>0) { |
| 305 |
OnCoarseSolveComm_=true; |
| 306 |
} |
| 307 |
const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid(); |
| 308 |
CoarseSolveComm_ = this->MpiComm_->split(!OnCoarseSolveComm_,this->MpiComm_->getRank()); |
| 309 |
CoarseSolveMap_ = MapFactory<LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique()->lib(),INVALID,GatheringMaps_[0]->getLocalElementList(),0,CoarseSolveComm_); |
| 310 |
} |
| 311 |
#else |
| 312 |
ThrowErrorMissingPackage("FROSch::CoarseOperator","Zoltan2"); |
| 313 |
#endif |
| 314 |
//------------------------------------------------------------------------------------------------------------------------ |
| 315 |
} else { |
| 316 |
FROSCH_ASSERT(false,"Distribution Type unknown!"); |
| 317 |
} |
| 318 |
FROSCH_DETAILTIMER_STOP(communicateCoarseMatrixTime); |
| 319 |
|
| 320 |
//------------------------------------------------------------------------------------------------------------------------ |
| 321 |
// Matrix to the new communicator |
| 322 |
if (OnCoarseSolveComm_) { |
| 323 |
FROSCH_DETAILTIMER_START_LEVELID(replicateCoarseMatrixOnCoarseCommTime,"replicate coarse matrix on coarse comm"); |
| 324 |
LO numRows = k0->getLocalNumRows(); |
| 325 |
ArrayRCP<size_t> elemsPerRow(numRows); |
| 326 |
LO numDiagonalsAdded = 0; |
| 327 |
if (k0->isFillComplete()) { |
| 328 |
ConstLOVecView indices; |
| 329 |
ConstSCVecView values; |
| 330 |
for (LO i = 0; i < numRows; i++) { |
| 331 |
size_t numEntries; |
| 332 |
numEntries = k0->getNumEntriesInLocalRow(i); |
| 333 |
if (numEntries == 0) { |
| 334 |
//Always add the diagonal for empty rows |
| 335 |
numEntries = 1; |
| 336 |
} |
| 337 |
elemsPerRow[i] = numEntries; |
| 338 |
} |
| 339 |
CoarseMatrix_ = MatrixFactory<SC,LO,GO,NO>::Build(CoarseSolveMap_,elemsPerRow); |
| 340 |
for (LO i = 0; i < numRows; i++) { |
| 341 |
GO globalRow = CoarseSolveMap_->getGlobalElement(i); |
| 342 |
k0->getLocalRowView(i,indices,values); |
| 343 |
if (indices.size()>0) { |
| 344 |
GOVec indicesGlob(indices.size()); |
| 345 |
for (UN j=0; j<indices.size(); j++) { |
| 346 |
indicesGlob[j] = k0->getColMap()->getGlobalElement(indices[j]); |
| 347 |
} |
| 348 |
CoarseMatrix_->insertGlobalValues(globalRow,indicesGlob(),values); |
| 349 |
} else { // Add diagonal unit for zero rows // Todo: Do you we need to sort the coarse matrix "NodeWise"? |
| 350 |
GOVec indicesGlob(1,CoarseSolveMap_->getGlobalElement(i)); |
| 351 |
SCVec values(1,ScalarTraits<SC>::one()); |
| 352 |
CoarseMatrix_->insertGlobalValues(globalRow,indicesGlob(),values()); |
| 353 |
numDiagonalsAdded++; |
| 354 |
} |
| 355 |
} |
| 356 |
CoarseMatrix_->fillComplete(CoarseSolveMap_,CoarseSolveMap_); //RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); CoarseMatrix_->describe(*fancy,VERB_EXTREME); |
| 357 |
} else { |
| 358 |
ConstGOVecView indices; |
| 359 |
ConstSCVecView values; |
| 360 |
for (LO i = 0; i < numRows; i++) { |
| 361 |
GO globalRow = CoarseSolveMap_->getGlobalElement(i); |
| 362 |
size_t numEntries; |
| 363 |
numEntries = k0->getNumEntriesInGlobalRow(globalRow); |
| 364 |
if (numEntries == 0) { |
| 365 |
//Always add the diagonal for empty rows |
| 366 |
numEntries = 1; |
| 367 |
} |
| 368 |
elemsPerRow[i] = numEntries; |
| 369 |
} |
| 370 |
CoarseMatrix_ = MatrixFactory<SC,LO,GO,NO>::Build(CoarseSolveMap_,elemsPerRow); |
| 371 |
for (LO i = 0; i < numRows; i++) { |
| 372 |
GO globalRow = CoarseSolveMap_->getGlobalElement(i); |
| 373 |
k0->getGlobalRowView(globalRow,indices,values); |
| 374 |
if (indices.size()>0) { |
| 375 |
CoarseMatrix_->insertGlobalValues(globalRow,indices,values); |
| 376 |
} else { // Add diagonal unit for zero rows // Todo: Do you we need to sort the coarse matrix "NodeWise"? |
| 377 |
GOVec indices(1,globalRow); |
| 378 |
SCVec values(1,ScalarTraits<SC>::one()); |
| 379 |
CoarseMatrix_->insertGlobalValues(globalRow,indices(),values()); |
| 380 |
numDiagonalsAdded++; |
| 381 |
} |
| 382 |
} |
| 383 |
CoarseMatrix_->fillComplete(CoarseSolveMap_,CoarseSolveMap_); //RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); CoarseMatrix_->describe(*fancy,VERB_EXTREME); |
| 384 |
} |
| 385 |
FROSCH_DETAILTIMER_STOP(replicateCoarseMatrixOnCoarseCommTime); |
| 386 |
|
| 387 |
FROSCH_DETAILTIMER_START_LEVELID(printStatisticsTime,"print statistics"); |
| 388 |
// Statistics on adding diagonal entries |
| 389 |
GOVec globalVec(5); |
| 390 |
LOVec localVec(5); |
| 391 |
LOVec sumVec(5); |
| 392 |
SCVec avgVec(5); |
| 393 |
LOVec minVec(5); |
| 394 |
LOVec maxVec(5); |
| 395 |
|
| 396 |
globalVec[0] = CoarseMatrix_->getGlobalNumRows(); |
| 397 |
localVec[0] = CoarseMatrix_->getLocalNumRows(); |
| 398 |
reduceAll(*CoarseSolveComm_,REDUCE_SUM,localVec[0],ptr(&sumVec[0])); |
| 399 |
avgVec[0] = max(sumVec[0]/double(CoarseSolveComm_->getSize()),0.0); |
| 400 |
reduceAll(*CoarseSolveComm_,REDUCE_MIN,localVec[0],ptr(&minVec[0])); |
| 401 |
reduceAll(*CoarseSolveComm_,REDUCE_MAX,localVec[0],ptr(&maxVec[0])); |
| 402 |
|
| 403 |
globalVec[1] = CoarseMatrix_->getGlobalNumEntries(); |
| 404 |
localVec[1] = CoarseMatrix_->getLocalNumEntries(); |
| 405 |
reduceAll(*CoarseSolveComm_,REDUCE_SUM,localVec[1],ptr(&sumVec[1])); |
| 406 |
avgVec[1] = max(sumVec[1]/double(CoarseSolveComm_->getSize()),0.0); |
| 407 |
reduceAll(*CoarseSolveComm_,REDUCE_MIN,localVec[1],ptr(&minVec[1])); |
| 408 |
reduceAll(*CoarseSolveComm_,REDUCE_MAX,localVec[1],ptr(&maxVec[1])); |
| 409 |
|
| 410 |
globalVec[2] = double(globalVec[1])/double(globalVec[0]); |
| 411 |
localVec[2] = double(localVec[1])/double(localVec[0]); |
| 412 |
reduceAll(*CoarseSolveComm_,REDUCE_SUM,localVec[2],ptr(&sumVec[2])); |
| 413 |
avgVec[2] = max(sumVec[2]/double(CoarseSolveComm_->getSize()),0.0); |
| 414 |
reduceAll(*CoarseSolveComm_,REDUCE_MIN,localVec[2],ptr(&minVec[2])); |
| 415 |
reduceAll(*CoarseSolveComm_,REDUCE_MAX,localVec[2],ptr(&maxVec[2])); |
| 416 |
|
| 417 |
localVec[3] = CoarseMatrix_->getLocalMaxNumRowEntries(); |
| 418 |
reduceAll(*CoarseSolveComm_,REDUCE_SUM,localVec[3],ptr(&sumVec[3])); |
| 419 |
avgVec[3] = max(sumVec[3]/double(CoarseSolveComm_->getSize()),0.0); |
| 420 |
reduceAll(*CoarseSolveComm_,REDUCE_MIN,localVec[3],ptr(&minVec[3])); |
| 421 |
reduceAll(*CoarseSolveComm_,REDUCE_MAX,localVec[3],ptr(&maxVec[3])); |
| 422 |
|
| 423 |
localVec[4] = numDiagonalsAdded; |
| 424 |
reduceAll(*CoarseSolveComm_,REDUCE_SUM,localVec[4],ptr(&sumVec[4])); |
| 425 |
avgVec[4] = max(sumVec[4]/double(CoarseSolveComm_->getSize()),0.0); |
| 426 |
reduceAll(*CoarseSolveComm_,REDUCE_MIN,localVec[4],ptr(&minVec[4])); |
| 427 |
reduceAll(*CoarseSolveComm_,REDUCE_MAX,localVec[4],ptr(&maxVec[4])); |
| 428 |
|
| 429 |
if (CoarseSolveComm_->getRank() == 0) { |
| 430 |
cout |
| 431 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 432 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 433 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 434 |
<< "| " |
| 435 |
<< left << setw(74) << "> Coarse Problem Statistics (Coarse Communicator) " << right << setw(8) << "(Level " << setw(2) << this->LevelID_ << ")" |
| 436 |
<< " |" |
| 437 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 438 |
<< setw(89) << "=========================================================================================" |
| 439 |
// << "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 440 |
// << "| " << left << setw(41) << "Dimension of the coarse problem" << right |
| 441 |
// << " | " << setw(41) << dimCoarseProblem |
| 442 |
// << " |" |
| 443 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 444 |
<< "| " << left << setw(41) << "Number of ranks on the coarse comm" << right |
| 445 |
<< " | " << setw(41) << NumProcsCoarseSolve_ |
| 446 |
<< " |" |
| 447 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 448 |
<< "| " << left << setw(41) << "Solver type" << right |
| 449 |
<< " | " << setw(41) << this->ParameterList_->sublist("CoarseSolver").get("SolverType","Amesos2") |
| 450 |
<< " |" |
| 451 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 452 |
<< "| " << left << setw(41) << "Solver" << right |
| 453 |
<< " | " << setw(41) << this->ParameterList_->sublist("CoarseSolver").get("Solver","Klu") |
| 454 |
<< " |" |
| 455 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 456 |
<< "| " << left << setw(41) << "Reuse symbolic factorization" << right |
| 457 |
<< " | " << setw(41) << this->ParameterList_->get("Reuse: Coarse Matrix Symbolic Factorization",true) |
| 458 |
<< " |" |
| 459 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 460 |
<< "| " << left << setw(41) << "Reuse coarse basis" << right |
| 461 |
<< " | " << setw(41) << this->ParameterList_->get("Reuse: Coarse Basis",true) |
| 462 |
<< " |" |
| 463 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 464 |
<< "| " << left << setw(41) << "Reuse coarse matrix" << right |
| 465 |
<< " | " << setw(41) << this->ParameterList_->get("Reuse: Coarse Matrix",false) |
| 466 |
<< " |" |
| 467 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 468 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 469 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 470 |
<< "| " << left << setw(20) << " " << right |
| 471 |
<< " | " << setw(10) << "total" |
| 472 |
<< " | " << setw(10) << "avg" |
| 473 |
<< " | " << setw(10) << "min" |
| 474 |
<< " | " << setw(10) << "max" |
| 475 |
<< " | " << setw(10) << "global sum" |
| 476 |
<< " |" |
| 477 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 478 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 479 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 480 |
<< "| " << left << setw(20) << "Number of rows" << right |
| 481 |
<< " | " << setw(10) << globalVec[0] |
| 482 |
<< " | " << setw(10) << setprecision(5) << avgVec[0] |
| 483 |
<< " | " << setw(10) << minVec[0] |
| 484 |
<< " | " << setw(10) << maxVec[0] |
| 485 |
<< " | " << setw(10) << sumVec[0] |
| 486 |
<< " |" |
| 487 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 488 |
<< "| " << left << setw(20) << "Entries" << right |
| 489 |
<< " | " << setw(10) << globalVec[1] |
| 490 |
<< " | " << setw(10) << setprecision(5) << avgVec[1] |
| 491 |
<< " | " << setw(10) << minVec[1] |
| 492 |
<< " | " << setw(10) << maxVec[1] |
| 493 |
<< " | " << setw(10) << sumVec[1] |
| 494 |
<< " |" |
| 495 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 496 |
<< "| " << left << setw(20) << "Avg entries per row" << right |
| 497 |
<< " | " << setw(10) << globalVec[2] |
| 498 |
<< " | " << setw(10) << setprecision(5) << avgVec[2] |
| 499 |
<< " | " << setw(10) << minVec[2] |
| 500 |
<< " | " << setw(10) << maxVec[2] |
| 501 |
<< " | " << setw(10) << sumVec[2] |
| 502 |
<< " |" |
| 503 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 504 |
<< "| " << left << setw(20) << "Max entries per row" << right |
| 505 |
<< " | " << setw(10) << " " |
| 506 |
<< " | " << setw(10) << setprecision(5) << avgVec[3] |
| 507 |
<< " | " << setw(10) << minVec[3] |
| 508 |
<< " | " << setw(10) << maxVec[3] |
| 509 |
<< " | " << setw(10) << " " |
| 510 |
<< " |" |
| 511 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 512 |
<< "| " << left << setw(20) << "Unit diagonals added" << right |
| 513 |
<< " | " << setw(10) << sumVec[4] |
| 514 |
<< " | " << setw(10) << setprecision(5) << avgVec[4] |
| 515 |
<< " | " << setw(10) << minVec[4] |
| 516 |
<< " | " << setw(10) << maxVec[4] |
| 517 |
<< " | " << setw(10) << sumVec[4] |
| 518 |
<< " |" |
| 519 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 520 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 521 |
<< endl; |
| 522 |
} |
| 523 |
FROSCH_DETAILTIMER_STOP(printStatisticsTime); |
| 524 |
|
| 525 |
bool reuseCoarseMatrixSymbolicFactorization = this->ParameterList_->get("Reuse: Coarse Matrix Symbolic Factorization",true); |
| 526 |
if (!this->IsComputed_) { |
| 527 |
reuseCoarseMatrixSymbolicFactorization = false; |
| 528 |
} |
| 529 |
if (!reuseCoarseMatrixSymbolicFactorization) { |
| 530 |
if (this->IsComputed_ && this->Verbose_) cout << "FROSch::CoarseOperator : Recomputing the Symbolic Factorization of the coarse matrix" << endl; |
| 531 |
CoarseSolver_ = SolverFactory<SC,LO,GO,NO>::Build(CoarseMatrix_, |
| 532 |
sublist(this->ParameterList_,"CoarseSolver"), |
| 533 |
string("CoarseSolver (Level ") + to_string(this->LevelID_) + string(")")); |
| 534 |
CoarseSolver_->initialize(); |
| 535 |
} else { |
| 536 |
FROSCH_ASSERT(!CoarseSolver_.is_null(),"FROSch::CoarseOperator: CoarseSolver_.is_null()"); |
| 537 |
CoarseSolver_->updateMatrix(CoarseMatrix_.getConst(),true); |
| 538 |
} |
| 539 |
CoarseSolver_->compute(); |
| 540 |
} |
| 541 |
} else { |
| 542 |
FROSCH_WARNING("FROSch::CoarseOperator",this->Verbose_,"No coarse basis has been set up. Neglecting CoarseOperator."); |
| 543 |
} |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
template<class SC,class LO,class GO,class NO> |
| 548 |
typename CoarseOperator<SC,LO,GO,NO>::XMatrixPtr CoarseOperator<SC,LO,GO,NO>::buildCoarseMatrix() |
| 549 |
{ |
| 550 |
FROSCH_DETAILTIMER_START_LEVELID(buildCoarseMatrixTime,"CoarseOperator::buildCoarseMatrix"); |
| 551 |
XMatrixPtr k0; |
| 552 |
if (this->ParameterList_->get("Use Triple MatrixMultiply",false)) { |
| 553 |
k0 = MatrixFactory<SC,LO,GO,NO>::Build(CoarseSpace_->getBasisMapUnique(),as<LO>(0)); |
| 554 |
TripleMatrixMultiply<SC,LO,GO,NO>::MultiplyRAP(*Phi_,true,*this->K_,false,*Phi_,false,*k0); |
| 555 |
} else { |
| 556 |
RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); //Phi_->describe(*fancy,VERB_EXTREME); |
| 557 |
XMatrixPtr tmp = MatrixMatrix<SC,LO,GO,NO>::Multiply(*this->K_,false,*Phi_,false,*fancy); |
| 558 |
k0 = MatrixMatrix<SC,LO,GO,NO>::Multiply(*Phi_,true,*tmp,false,*fancy); //k0->describe(*fancy,VERB_EXTREME); |
| 559 |
} |
| 560 |
return k0; |
| 561 |
} |
| 562 |
|
| 563 |
template<class SC,class LO,class GO,class NO> |
| 564 |
int CoarseOperator<SC,LO,GO,NO>::buildCoarseSolveMap(ConstXMapPtr coarseMapUnique) |
| 565 |
{ |
| 566 |
FROSCH_DETAILTIMER_START_LEVELID(buildCoarseSolveMapTime,"CoarseOperator::buildCoarseSolveMap"); |
| 567 |
NumProcsCoarseSolve_ = DistributionList_->get("NumProcs",1); |
| 568 |
double factor = DistributionList_->get("Factor",0.0); |
| 569 |
|
| 570 |
switch (NumProcsCoarseSolve_) { |
| 571 |
case -1: |
| 572 |
FROSCH_ASSERT(false,"We do not know the size of the matrix yet. Therefore, we cannot use the formula NumProcsCoarseSolve_ = int(0.5*(1+max(k0->getGlobalNumRows()/10000,k0->getGlobalNumEntries()/100000)));"); |
| 573 |
//NumProcsCoarseSolve_ = int(0.5*(1+max(k0->getGlobalNumRows()/10000,k0->getGlobalNumEntries()/100000))); |
| 574 |
break; |
| 575 |
|
| 576 |
case 0: |
| 577 |
NumProcsCoarseSolve_ = this->MpiComm_->getSize(); |
| 578 |
break; |
| 579 |
|
| 580 |
default: |
| 581 |
if (NumProcsCoarseSolve_>this->MpiComm_->getSize()) NumProcsCoarseSolve_ = this->MpiComm_->getSize(); |
| 582 |
if (fabs(factor) > 1.0e-12) NumProcsCoarseSolve_ = int(NumProcsCoarseSolve_/factor); |
| 583 |
if (NumProcsCoarseSolve_<1) NumProcsCoarseSolve_ = 1; |
| 584 |
break; |
| 585 |
} |
| 586 |
|
| 587 |
const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid(); |
| 588 |
if (!DistributionList_->get("Type","linear").compare("linear")) { |
| 589 |
|
| 590 |
int gatheringSteps = DistributionList_->get("GatheringSteps",1); |
| 591 |
GatheringMaps_.resize(gatheringSteps); |
| 592 |
CoarseSolveExporters_.resize(gatheringSteps); |
| 593 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 594 |
CoarseSolveImporters_.resize(gatheringSteps); |
| 595 |
#endif |
| 596 |
|
| 597 |
LO numProcsGatheringStep = this->MpiComm_->getSize(); |
| 598 |
GO numGlobalIndices = coarseMapUnique->getMaxAllGlobalIndex(); |
| 599 |
if (coarseMapUnique->lib()==UseEpetra || coarseMapUnique->getGlobalNumElements()>0) { |
| 600 |
numGlobalIndices += 1; |
| 601 |
} |
| 602 |
LO numMyRows; |
| 603 |
double gatheringFactor = pow(double(this->MpiComm_->getSize())/double(NumProcsCoarseSolve_),1.0/double(gatheringSteps)); |
| 604 |
|
| 605 |
for (int i=0; i<gatheringSteps-1; i++) { |
| 606 |
numMyRows = 0; |
| 607 |
numProcsGatheringStep = LO(numProcsGatheringStep/gatheringFactor); |
| 608 |
//if (this->Verbose_) cout << i << " " << numProcsGatheringStep << " " << numGlobalIndices << endl; |
| 609 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/numProcsGatheringStep) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/numProcsGatheringStep) < numProcsGatheringStep) { |
| 610 |
if (this->MpiComm_->getRank()==0) { |
| 611 |
numMyRows = numGlobalIndices - (numGlobalIndices/numProcsGatheringStep)*(numProcsGatheringStep-1); |
| 612 |
} else { |
| 613 |
numMyRows = numGlobalIndices/numProcsGatheringStep; |
| 614 |
} |
| 615 |
} |
| 616 |
{ |
| 617 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 618 |
FROSCH_DETAILTIMER_START_LEVELID(gatheringMapsTime,"Gathering Maps"); |
| 619 |
#endif |
| 620 |
GatheringMaps_[i] = MapFactory<LO,GO,NO>::Build(coarseMapUnique->lib(),INVALID,numMyRows,0,this->MpiComm_); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
numMyRows = 0; |
| 625 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/NumProcsCoarseSolve_) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/NumProcsCoarseSolve_) < NumProcsCoarseSolve_) { |
| 626 |
if (this->MpiComm_->getRank()==0) { |
| 627 |
numMyRows = numGlobalIndices - (numGlobalIndices/NumProcsCoarseSolve_)*(NumProcsCoarseSolve_-1); |
| 628 |
} else { |
| 629 |
numMyRows = numGlobalIndices/NumProcsCoarseSolve_; |
| 630 |
} |
| 631 |
} |
| 632 |
{ |
| 633 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 634 |
FROSCH_DETAILTIMER_START_LEVELID(gatheringMapsTime,"Gathering Maps"); |
| 635 |
#endif |
| 636 |
GatheringMaps_[gatheringSteps-1] = MapFactory<LO,GO,NO>::Build(coarseMapUnique->lib(),INVALID,numMyRows,0,this->MpiComm_); |
| 637 |
} |
| 638 |
//cout << *GatheringMaps_->at(gatheringSteps-1); |
| 639 |
|
| 640 |
//------------------------------------------------------------------------------------------------------------------------ |
| 641 |
// Use a separate Communicator for the coarse problem |
| 642 |
if (GatheringMaps_[GatheringMaps_.size()-1]->getLocalNumElements()>0) { |
| 643 |
OnCoarseSolveComm_=true; |
| 644 |
} |
| 645 |
{ |
| 646 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 647 |
FROSCH_DETAILTIMER_START_LEVELID(commSplitTime,"Coarse Communicator Split"); |
| 648 |
#endif |
| 649 |
CoarseSolveComm_ = this->MpiComm_->split(!OnCoarseSolveComm_,this->MpiComm_->getRank()); |
| 650 |
} |
| 651 |
{ |
| 652 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 653 |
FROSCH_DETAILTIMER_START_LEVELID(coarseCommMapTime,"Coarse Communicator Map"); |
| 654 |
#endif |
| 655 |
CoarseSolveMap_ = MapFactory<LO,GO,NO>::Build(coarseMapUnique->lib(),INVALID,GatheringMaps_[GatheringMaps_.size()-1]->getLocalElementList(),0,CoarseSolveComm_); |
| 656 |
} |
| 657 |
|
| 658 |
// Possibly change the Send type for this Exporter |
| 659 |
ParameterListPtr gatheringCommunicationList = sublist(DistributionList_,"Gathering Communication"); |
| 660 |
// Set communication type "Alltoall" if not specified differently |
| 661 |
if (!gatheringCommunicationList->isParameter("Send type")) gatheringCommunicationList->set("Send type","Send"); |
| 662 |
|
| 663 |
// Create Import and Export objects |
| 664 |
{ |
| 665 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 666 |
FROSCH_DETAILTIMER_START_LEVELID(coarseSolveExportersTime,"Build Exporters"); |
| 667 |
#endif |
| 668 |
CoarseSolveExporters_[0] = ExportFactory<LO,GO,NO>::Build(coarseMapUnique,GatheringMaps_[0]); |
| 669 |
CoarseSolveExporters_[0]->setDistributorParameters(gatheringCommunicationList); // Set the parameter list for the communication of the exporter |
| 670 |
} |
| 671 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 672 |
{ |
| 673 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 674 |
FROSCH_DETAILTIMER_START_LEVELID(coarseSolveImportersTime,"Build Importers"); |
| 675 |
#endif |
| 676 |
CoarseSolveImporters_[0] = ImportFactory<LO,GO,NO>::Build(GatheringMaps_[0],coarseMapUnique); |
| 677 |
CoarseSolveImporters_[0]->setDistributorParameters(gatheringCommunicationList); // Set the parameter list for the communication of the exporter |
| 678 |
} |
| 679 |
#endif |
| 680 |
|
| 681 |
for (UN j=1; j<GatheringMaps_.size(); j++) { |
| 682 |
{ |
| 683 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 684 |
FROSCH_DETAILTIMER_START_LEVELID(coarseSolveExportersTime,"Build Exporters"); |
| 685 |
#endif |
| 686 |
CoarseSolveExporters_[j] = ExportFactory<LO,GO,NO>::Build(GatheringMaps_[j-1],GatheringMaps_[j]); |
| 687 |
CoarseSolveExporters_[j]->setDistributorParameters(gatheringCommunicationList); // Set the parameter list for the communication of the exporter |
| 688 |
} |
| 689 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 690 |
{ |
| 691 |
#ifdef FROSCH_COARSEOPERATOR_DETAIL_TIMERS |
| 692 |
FROSCH_DETAILTIMER_START_LEVELID(coarseSolveImportersTime,"Build Importers"); |
| 693 |
#endif |
| 694 |
CoarseSolveImporters_[j] = ImportFactory<LO,GO,NO>::Build(GatheringMaps_[j],GatheringMaps_[j-1]); |
| 695 |
CoarseSolveImporters_[j]->setDistributorParameters(gatheringCommunicationList); // Set the parameter list for the communication of the exporter |
| 696 |
} |
| 697 |
#endif |
| 698 |
} |
| 699 |
} else if (!DistributionList_->get("Type","linear").compare("ZoltanDual")) { |
| 700 |
//ZoltanDual provides a partition of the coarse problem with Zoltan2 inlcuding the |
| 701 |
//build of a Repeated map suited for the next level |
| 702 |
//GatheringSteps to communicate Matrix |
| 703 |
int gatheringSteps = DistributionList_->get("GatheringSteps",1); |
| 704 |
GatheringMaps_.resize(gatheringSteps); |
| 705 |
CoarseSolveExporters_.resize(gatheringSteps); |
| 706 |
|
| 707 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/NumProcsCoarseSolve_) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/NumProcsCoarseSolve_) < NumProcsCoarseSolve_) { |
| 708 |
// if (this->MpiComm_->getRank()==0) { |
| 709 |
// numMyRows = numGlobalIndices - (numGlobalIndices/NumProcsCoarseSolve_)*(NumProcsCoarseSolve_-1); |
| 710 |
// } else { |
| 711 |
// numMyRows = numGlobalIndices/NumProcsCoarseSolve_; |
| 712 |
// } |
| 713 |
OnCoarseSolveComm_=true; |
| 714 |
} |
| 715 |
|
| 716 |
// // XMapPtr tmpCoarseMap = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),-1,numMyRows,0,this->MpiComm_); |
| 717 |
// if (tmpCoarseMap->getLocalNumElements()>0) { |
| 718 |
// OnCoarseSolveComm_=true; |
| 719 |
// } |
| 720 |
CoarseSolveComm_ = this->MpiComm_->split(!OnCoarseSolveComm_,this->MpiComm_->getRank()); |
| 721 |
|
| 722 |
//Gathering Steps for RepeatedMap################################################# |
| 723 |
//-> Have to test that |
| 724 |
RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); |
| 725 |
|
| 726 |
int MLgatheringSteps = DistributionList_->get("MLGatheringSteps",2); |
| 727 |
MLGatheringMaps_.resize(MLgatheringSteps); |
| 728 |
MLCoarseSolveExporters_.resize(MLgatheringSteps); |
| 729 |
double MLgatheringFactor = pow(double(this->MpiComm_->getSize())/double(NumProcsCoarseSolve_),1.0/double(MLgatheringSteps)); |
| 730 |
GO MLnumGlobalIndices = SubdomainConnectGraph_->getRowMap()->getMaxAllGlobalIndex()+1; |
| 731 |
GO MLnumMyRows; |
| 732 |
|
| 733 |
LO numProcsGatheringStep = this->MpiComm_->getSize(); |
| 734 |
MLGatheringMaps_[0] = Xpetra::MapFactory<LO,GO,NO>::Build(this->K_->getMap()->lib(),INVALID,1,0,this->K_->getMap()->getComm()); |
| 735 |
for (int i=1; i<MLgatheringSteps-1; i++) { |
| 736 |
MLnumMyRows = 0; |
| 737 |
numProcsGatheringStep = LO(numProcsGatheringStep/MLgatheringFactor); |
| 738 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/numProcsGatheringStep) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/numProcsGatheringStep) < numProcsGatheringStep) { |
| 739 |
if (this->MpiComm_->getRank()==0) { |
| 740 |
MLnumMyRows = MLnumGlobalIndices - (MLnumGlobalIndices/numProcsGatheringStep)*(numProcsGatheringStep-1); |
| 741 |
} else { |
| 742 |
MLnumMyRows = MLnumGlobalIndices/numProcsGatheringStep; |
| 743 |
} |
| 744 |
} |
| 745 |
MLGatheringMaps_[i] = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),INVALID,MLnumMyRows,0,this->MpiComm_); |
| 746 |
} |
| 747 |
|
| 748 |
MLnumMyRows = 0; |
| 749 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/NumProcsCoarseSolve_) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/NumProcsCoarseSolve_) < NumProcsCoarseSolve_) { |
| 750 |
if (this->MpiComm_->getRank()==0) { |
| 751 |
MLnumMyRows = MLnumGlobalIndices - (MLnumGlobalIndices/NumProcsCoarseSolve_)*(NumProcsCoarseSolve_-1); |
| 752 |
} else { |
| 753 |
MLnumMyRows = MLnumGlobalIndices/NumProcsCoarseSolve_; |
| 754 |
} |
| 755 |
} |
| 756 |
MLGatheringMaps_[MLgatheringSteps-1] = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),INVALID,MLnumMyRows,0,this->MpiComm_); |
| 757 |
for (UN j=1; j<MLGatheringMaps_.size(); j++) { |
| 758 |
MLCoarseSolveExporters_[j] = Xpetra::ExportFactory<LO,GO,NO>::Build(MLGatheringMaps_[j-1],MLGatheringMaps_[j]); |
| 759 |
} |
| 760 |
int nSubs = this->MpiComm_->getSize(); |
| 761 |
GOVec RowsCoarseSolve; |
| 762 |
if (OnCoarseSolveComm_) { |
| 763 |
int start = (nSubs*(CoarseSolveComm_->getRank()))/NumProcsCoarseSolve_; |
| 764 |
int end = (nSubs*(CoarseSolveComm_->getRank()+1))/NumProcsCoarseSolve_; |
| 765 |
RowsCoarseSolve.resize(end-start); |
| 766 |
for (int i = 0; i<end-start; i++) { |
| 767 |
RowsCoarseSolve[i] = start+i; |
| 768 |
} |
| 769 |
} |
| 770 |
Teuchos::ArrayView< const GO > CList = MLGatheringMaps_[MLgatheringSteps-1]->getLocalElementList(); |
| 771 |
//MLCoarseMap_ = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),-1,CList,0,CoarseSolveComm_);->Should work but does not WHY?!?! |
| 772 |
MLCoarseMap_ = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),MLGatheringMaps_[MLgatheringSteps-1]->getGlobalNumElements(),0,CoarseSolveComm_); |
| 773 |
|
| 774 |
//##################################################################### |
| 775 |
// Build Repeated Map Zoltan2 |
| 776 |
// build ElementNodeList_ to have adjacent entities to one subdomain |
| 777 |
this->buildElementNodeList(); |
| 778 |
// Connectivity Graph on the CoarseSolveComm_ |
| 779 |
this->buildCoarseGraph(); |
| 780 |
//Build Repeatd Map on CoarseComm------------ |
| 781 |
//Initialize Maps... |
| 782 |
ConstXMapPtr UniqueMap; |
| 783 |
XMapPtr UniqueMapAll; |
| 784 |
XMapPtr tmpRepMap; |
| 785 |
ConstXMapPtr ConstRepMap; |
| 786 |
GOVec uniEle; |
| 787 |
|
| 788 |
if (OnCoarseSolveComm_) { |
| 789 |
//Coarse DofsMaps so far only one Block will work |
| 790 |
ConstXMapPtrVecPtr2D CoarseDofsMaps(1); |
| 791 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 792 |
BuildRepMapZoltan(SubdomainConnectGraph_,ElementNodeList_, DistributionList_,MLCoarseMap_->getComm(),CoarseSolveRepeatedMap_); |
| 793 |
#else |
| 794 |
ThrowErrorMissingPackage("FROSch::CoarseOperator","Zoltan2"); |
| 795 |
#endif |
| 796 |
ConstRepMap = CoarseSolveRepeatedMap_; |
| 797 |
ConstXMapPtrVecPtr NodesMapVector(1); |
| 798 |
//MapVector for next Level |
| 799 |
//So far only one Block is allowed ; needs to be adapetd fpr Block Ops |
| 800 |
ConstXMapPtrVecPtr RepMapVector(1); |
| 801 |
//Create DofMaps according to counting the interface entities |
| 802 |
ConstXMapPtrVecPtr DMap(CoarseDofsPerNode_); |
| 803 |
ConstXMapPtrVecPtr DMapRep(CoarseDofsPerNode_); |
| 804 |
|
| 805 |
tmpRepMap = this->BuildRepeatedMapCoarseLevel(ConstRepMap,CoarseDofsPerNode_,DMapRep,PartitionType_); |
| 806 |
RepMapCoarse_ = tmpRepMap; |
| 807 |
RepMapVector[0] = tmpRepMap; |
| 808 |
|
| 809 |
NodesMapVector[0] = ConstRepMap; |
| 810 |
//Pass Repeated Map Vector on to the next Level |
| 811 |
|
| 812 |
//Create uniqueMap following the repeatedMap |
| 813 |
//Create uniqueNodeMap so that dof belonging to one node are on the same process |
| 814 |
UniqueMap = FROSch::BuildUniqueMap<LO,GO,NO>(CoarseSolveRepeatedMap_); |
| 815 |
UniqueMapAll = this->BuildRepeatedMapCoarseLevel(UniqueMap,CoarseDofsPerNode_,DMap,PartitionType_); |
| 816 |
|
| 817 |
uniEle = UniqueMapAll->getLocalElementList(); |
| 818 |
//Set DofOderingVec and DofsPerNodeVec to ParameterList for the next Level |
| 819 |
//Create Here DofsMaps for the next Level->DofOrdering will become redundant |
| 820 |
Teuchos::ArrayRCP<DofOrdering> dofOrderings(1); |
| 821 |
dofOrderings[0] = Custom; //special Ordering for Coarse Level |
| 822 |
Teuchos::ArrayRCP<UN> dofsPerNodeVector(1); |
| 823 |
dofsPerNodeVector[0] = CoarseDofsPerNode_; |
| 824 |
CoarseDofsMaps[0] = DMapRep; |
| 825 |
|
| 826 |
sublist(sublist(sublist(this->ParameterList_,"CoarseSolver"),"FROSchPreconditioner"),"TwoLevelPreconditioner")->set("Repeated Map Vector",RepMapVector); |
| 827 |
sublist(sublist(sublist(this->ParameterList_,"CoarseSolver"),"FROSchPreconditioner"),"TwoLevelPreconditioner")->set("Dofs Maps Vector",CoarseDofsMaps); |
| 828 |
sublist(sublist(sublist(this->ParameterList_,"CoarseSolver"),"FROSchPreconditioner"),"TwoLevelPreconditioner")->set("DofOrdering Vector",dofOrderings); |
| 829 |
sublist(sublist(sublist(this->ParameterList_,"CoarseSolver"),"FROSchPreconditioner"),"TwoLevelPreconditioner")->set("DofsPerNode Vector",dofsPerNodeVector); |
| 830 |
sublist(sublist(sublist(this->ParameterList_,"CoarseSolver"),"FROSchPreconditioner"),"TwoLevelPreconditioner")->set("Nodes Map Vector",NodesMapVector); |
| 831 |
} |
| 832 |
|
| 833 |
GatheringMaps_[gatheringSteps-1] = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),INVALID,uniEle,0,this->MpiComm_); |
| 834 |
|
| 835 |
GO numGlobalIndices = coarseMapUnique->getMaxAllGlobalIndex(); |
| 836 |
if (coarseMapUnique->lib()==UseEpetra || coarseMapUnique->getGlobalNumElements()>0) { |
| 837 |
numGlobalIndices += 1; |
| 838 |
} |
| 839 |
LO numMyRows; |
| 840 |
double gatheringFactor = pow(double(this->MpiComm_->getSize())/double(NumProcsCoarseSolve_),1.0/double(gatheringSteps)); |
| 841 |
|
| 842 |
// |
| 843 |
// double gatheringFactor = pow(double(this->MpiComm_->getSize())/double(NumProcsCoarseSolve_),1.0/double(gatheringSteps)); |
| 844 |
// LO numProcsGatheringStep = this->MpiComm_->getSize(); |
| 845 |
// GO numGlobalIndices = CoarseMap_->getMaxAllGlobalIndex(); |
| 846 |
// |
| 847 |
// LO numProcsGatheringStep = this->MpiComm_->getSize(); |
| 848 |
// GO numGlobalIndices = coarseMapUnique->getMaxAllGlobalIndex(); |
| 849 |
// if (coarseMapUnique->lib()==UseEpetra || coarseMapUnique->getGlobalNumElements()>0) { |
| 850 |
// numGlobalIndices += 1; |
| 851 |
// } |
| 852 |
// GO numMyRows; |
| 853 |
|
| 854 |
for (int i=0; i<gatheringSteps-1; i++) { |
| 855 |
numMyRows = 0; |
| 856 |
numProcsGatheringStep = LO(numProcsGatheringStep/gatheringFactor); |
| 857 |
if (this->MpiComm_->getRank()%(this->MpiComm_->getSize()/numProcsGatheringStep) == 0 && this->MpiComm_->getRank()/(this->MpiComm_->getSize()/numProcsGatheringStep) < numProcsGatheringStep) { |
| 858 |
if (this->MpiComm_->getRank()==0) { |
| 859 |
numMyRows = numGlobalIndices - (numGlobalIndices/numProcsGatheringStep)*(numProcsGatheringStep-1); |
| 860 |
} else { |
| 861 |
numMyRows = numGlobalIndices/numProcsGatheringStep; |
| 862 |
} |
| 863 |
} |
| 864 |
GatheringMaps_[i] = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),INVALID,numMyRows,0,this->MpiComm_); |
| 865 |
} |
| 866 |
|
| 867 |
CoarseSolveMap_ = Xpetra::MapFactory<LO,GO,NO>::Build(CoarseMap_->lib(),INVALID,GatheringMaps_[gatheringSteps-1]->getLocalElementList(),0,CoarseSolveComm_); |
| 868 |
|
| 869 |
} else if (!DistributionList_->get("Type","linear").compare("Zoltan2")) { |
| 870 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 871 |
GatheringMaps_.resize(1); |
| 872 |
CoarseSolveExporters_.resize(1); |
| 873 |
#ifdef FROSCH_COARSEOPERATOR_EXPORT_AND_IMPORT |
| 874 |
CoarseSolveImporters_.resize(1); |
| 875 |
#endif |
| 876 |
#else |
| 877 |
ThrowErrorMissingPackage("FROSch::CoarseOperator","Zoltan2"); |
| 878 |
#endif |
| 879 |
} else { |
| 880 |
FROSCH_ASSERT(false,"FROSch::CoarseOperator: Distribution type unknown."); |
| 881 |
} |
| 882 |
|
| 883 |
// Output information about the Gatherin Steps |
| 884 |
GO global,sum,numRanks; |
| 885 |
LO local,minVal,maxVal; |
| 886 |
SC avg; |
| 887 |
const SC ZERO = Teuchos::ScalarTraits<SC>::zero(); |
| 888 |
|
| 889 |
global = coarseMapUnique->getMaxAllGlobalIndex(); |
| 890 |
if (coarseMapUnique->lib()==UseEpetra || coarseMapUnique->getGlobalNumElements()>0) { |
| 891 |
global += 1; |
| 892 |
} |
| 893 |
|
| 894 |
local = (LO) max((LO) coarseMapUnique->getLocalNumElements(),(LO) 0); |
| 895 |
reduceAll(*this->MpiComm_,REDUCE_SUM,GO(local),ptr(&sum)); |
| 896 |
avg = max(sum/SC(this->MpiComm_->getSize()),ZERO); |
| 897 |
reduceAll(*this->MpiComm_,REDUCE_MIN,local,ptr(&minVal)); |
| 898 |
reduceAll(*this->MpiComm_,REDUCE_MAX,local,ptr(&maxVal)); |
| 899 |
|
| 900 |
if (this->Verbose_) { |
| 901 |
cout |
| 902 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 903 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 904 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 905 |
<< "| " |
| 906 |
<< left << setw(74) << "> Gathering Steps Statistics " << right << setw(8) << "(Level " << setw(2) << this->LevelID_ << ")" |
| 907 |
<< " |" |
| 908 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 909 |
<< setw(89) << "=========================================================================================" |
| 910 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 911 |
<< "| " << left << setw(7) << " " << right |
| 912 |
<< " | " << setw(10) << "ranks" |
| 913 |
<< " | " << setw(10) << "total" |
| 914 |
<< " | " << setw(10) << "avg" |
| 915 |
<< " | " << setw(10) << "min" |
| 916 |
<< " | " << setw(10) << "max" |
| 917 |
<< " | " << setw(10) << "global sum" |
| 918 |
<< " |" |
| 919 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 920 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 921 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 922 |
<< "| " << left << setw(4) << "Map " << setw(3) << "0" << right |
| 923 |
<< " | " << setw(10) << this->MpiComm_->getSize() |
| 924 |
<< " | " << setw(10) << global |
| 925 |
<< " | " << setw(10) << setprecision(5) << avg |
| 926 |
<< " | " << setw(10) << minVal |
| 927 |
<< " | " << setw(10) << maxVal |
| 928 |
<< " | " << setw(10) << sum |
| 929 |
<< " |"; |
| 930 |
} |
| 931 |
|
| 932 |
for (int i=0; i<GatheringMaps_.size(); i++) { |
| 933 |
global = GatheringMaps_[i]->getMaxAllGlobalIndex(); |
| 934 |
if (GatheringMaps_[i]->lib()==UseEpetra || GatheringMaps_[i]->getGlobalNumElements()>0) { |
| 935 |
global += 1; |
| 936 |
} |
| 937 |
|
| 938 |
local = (LO) max((LO) GatheringMaps_[i]->getLocalNumElements(),(LO) 0); |
| 939 |
reduceAll(*this->MpiComm_,REDUCE_SUM,GO(local),ptr(&sum)); |
| 940 |
reduceAll(*this->MpiComm_,REDUCE_SUM,GO(GatheringMaps_[i]->getLocalNumElements()>0),ptr(&numRanks)); |
| 941 |
avg = max(sum/SC(numRanks),ZERO); |
| 942 |
reduceAll(*this->MpiComm_,REDUCE_MIN,(GatheringMaps_[i]->getLocalNumElements()>0 ? local : numeric_limits<LO>::max()),ptr(&minVal)); |
| 943 |
reduceAll(*this->MpiComm_,REDUCE_MAX,local,ptr(&maxVal)); |
| 944 |
|
| 945 |
if (this->Verbose_) { |
| 946 |
cout |
| 947 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 948 |
<< "| " << setw(4) << left << "Map " << setw(3) << i+1 << right |
| 949 |
<< " | " << setw(10) << numRanks |
| 950 |
<< " | " << setw(10) << global |
| 951 |
<< " | " << setw(10) << setprecision(3) << avg |
| 952 |
<< " | " << setw(10) << minVal |
| 953 |
<< " | " << setw(10) << maxVal |
| 954 |
<< " | " << setw(10) << sum |
| 955 |
<< " |"; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
if (this->Verbose_) { |
| 960 |
cout |
| 961 |
<< "\n" << setw(FROSCH_OUTPUT_INDENT) << " " |
| 962 |
<< setw(89) << "-----------------------------------------------------------------------------------------" |
| 963 |
<< endl; |
| 964 |
} |
| 965 |
|
| 966 |
return 0; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
#endif |