Debian Codegraph @

packages/shylu/shylu_dd/frosch/src/SchwarzOperators/FROSch_HarmonicCoarseOperator_def.hpp

raw
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_HARMONICCOARSEOPERATOR_DEF_HPP
11 #define _FROSCH_HARMONICCOARSEOPERATOR_DEF_HPP
12
13 #include <FROSch_HarmonicCoarseOperator_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 HarmonicCoarseOperator<SC,LO,GO,NO>::HarmonicCoarseOperator(ConstXMatrixPtr k,
24 ParameterListPtr parameterList) :
25 CoarseOperator<SC,LO,GO,NO> (k,parameterList),
26 AssembledInterfaceCoarseSpace_ (new CoarseSpace<SC,LO,GO,NO>(this->MpiComm_,this->SerialComm_))
27 {
28 FROSCH_DETAILTIMER_START_LEVELID(harmonicCoarseOperatorTime,"HarmonicCoarseOperator::HarmonicCoarseOperator");
29 }
30
31 template <class SC,class LO,class GO,class NO>
32 typename HarmonicCoarseOperator<SC,LO,GO,NO>::ConstXMapPtr HarmonicCoarseOperator<SC,LO,GO,NO>::computeCoarseSpace(CoarseSpacePtr coarseSpace)
33 {
34 FROSCH_DETAILTIMER_START_LEVELID(computeCoarseSpaceTime,"HarmonicCoarseOperator::computeCoarseSpace");
35
36 // Build local saddle point problem
37 ConstXMapPtr repeatedMap;
38 ConstXMatrixPtr repeatedMatrix;
39 if (this->coarseExtractLocalSubdomainMatrix_Symbolic_Done_) {
40 ExtractLocalSubdomainMatrix_Compute(this->coarseScatter_,
41 this->K_.getConst(),
42 this->coarseSubdomainMatrix_,
43 this->coarseLocalSubdomainMatrix_);
44 repeatedMap = this->coarseSubdomainMatrix_->getRowMap();
45 repeatedMatrix = this->coarseLocalSubdomainMatrix_;
46 } else {
47 repeatedMap = AssembleSubdomainMap(NumberOfBlocks_,DofsMaps_,DofsPerNode_);
48 repeatedMatrix = ExtractLocalSubdomainMatrix(this->K_.getConst(),repeatedMap.getConst());
49 }
50
51 // Remove coupling blocks
52 if (this->ParameterList_->get("Extensions: Remove Coupling",false)) {
53 FROSCH_ASSERT(this->ParameterList_->isParameter("Extensions: Coupling IDs to Remove"),"FROSch::HarmonicCoarseOperator: Coupling IDs to remove are not specified (\"Extensions: Coupling IDs to Remove\").");
54 //FROSCH_ASSERT(this->ParameterList_->isType<decltype(couplingIDsToRemove)>("Extensions: Coupling IDs to Remove"),"FROSch::HarmonicCoarseOperator: \"Extensions: Coupling IDs to Remove\" is not of type Teuchos::TwoDArray<int>.");
55
56 TwoDArray<int> couplingIDsToRemove = this->ParameterList_->get("Extensions: Coupling IDs to Remove",TwoDArray<int>(0,0,0));
57 repeatedMatrix = removeCouplingBetweenDofs(repeatedMatrix,repeatedMap,couplingIDsToRemove);
58 }
59
60 // Extract submatrices
61 GOVec indicesGammaDofsAll(0);
62 GOVec indicesIDofsAll(0);
63 LO tmp = 0;
64
65 for (UN i=0; i<NumberOfBlocks_; i++) {
66 for (UN j=0; j<GammaDofs_[i].size(); j++) {
67 indicesGammaDofsAll.push_back(tmp+GammaDofs_[i][j]);
68 }
69 for (UN j=0; j<IDofs_[i].size(); j++) {
70 indicesIDofsAll.push_back(tmp+IDofs_[i][j]);
71 }
72 tmp += GammaDofs_[i].size()+IDofs_[i].size();
73 }
74
75 XMatrixPtr kII;
76 XMatrixPtr kIGamma;
77 XMatrixPtr kGammaI;
78 XMatrixPtr kGammaGamma;
79
80 BuildSubmatrices(repeatedMatrix.getConst(),indicesIDofsAll(),kII,kIGamma,kGammaI,kGammaGamma);
81
82 //Detect linear dependencies
83 if (!this->ParameterList_->get("Skip DetectLinearDependencies",false)) {
84 LOVecPtr linearDependentVectors = detectLinearDependencies(indicesGammaDofsAll(),this->K_->getRowMap(),this->K_->getRangeMap(),repeatedMap,this->ParameterList_->get("Phi: Dropping Threshold",1.e-8),this->ParameterList_->get("Phi: Orthogonalization Threshold",1.e-12));
85 // cout << this->MpiComm_->getRank() << " " << linearDependentVectors.size() << endl;
86 AssembledInterfaceCoarseSpace_->zeroOutBasisVectors(linearDependentVectors());
87 }
88
89 // Build the saddle point harmonic extensions
90 XMultiVectorPtr localCoarseSpaceBasis;
91 if (AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements()) {
92 localCoarseSpaceBasis = computeExtensions(repeatedMatrix->getRowMap(),indicesGammaDofsAll(),indicesIDofsAll(),kII,kIGamma);
93
94 coarseSpace->addSubspace(AssembledInterfaceCoarseSpace_->getBasisMap(),AssembledInterfaceCoarseSpace_->getBasisMapUnique(),localCoarseSpaceBasis);
95 } else {
96 FROSCH_NOTIFICATION("FROSch::HarmonicCoarseOperator",this->Verbose_,"The Coarse Space is empty. No extensions are computed.");
97 }
98
99 return repeatedMap;
100 }
101
102 template <class SC,class LO,class GO,class NO>
103 int HarmonicCoarseOperator<SC,LO,GO,NO>::assembleInterfaceCoarseSpace()
104 {
105 FROSCH_DETAILTIMER_START_LEVELID(assembleInterfaceCoarseSpaceTime,"HarmonicCoarseOperator::assembleInterfaceCoarseSpace");
106 LO ii=0;
107 for (UN i=0; i<NumberOfBlocks_; i++) {
108 if (!InterfaceCoarseSpaces_[i].is_null()) {
109 if (InterfaceCoarseSpaces_[i]->hasBasisMap()) {
110 FROSCH_ASSERT(InterfaceCoarseSpaces_[i]->hasBasisMapUnique(),"FROSch::HarmonicCoarseOperator: !InterfaceCoarseSpaces_[i]->hasAssembledBasis()");
111 this->AssembledInterfaceCoarseSpace_->addSubspace(InterfaceCoarseSpaces_[i]->getBasisMap(),InterfaceCoarseSpaces_[i]->getBasisMapUnique(),InterfaceCoarseSpaces_[i]->getAssembledBasis(),ii);
112 ii += InterfaceCoarseSpaces_[i]->getAssembledBasis()->getLocalLength();
113 }
114 InterfaceCoarseSpaces_[i].reset();
115 }
116 }
117 return this->AssembledInterfaceCoarseSpace_->assembleCoarseSpace();
118 }
119
120 template <class SC,class LO,class GO,class NO>
121 int HarmonicCoarseOperator<SC,LO,GO,NO>::addZeroCoarseSpaceBlock(ConstXMapPtr dofsMap)
122 {
123 FROSCH_DETAILTIMER_START_LEVELID(addZeroCoarseSpaceBlockTime,"HarmonicCoarseOperator::addZeroCoarseSpaceBlock");
124 /////
125 int blockId = NumberOfBlocks_-1;
126
127 // Process the parameter list
128 stringstream blockIdStringstream;
129 blockIdStringstream << blockId+1;
130 string blockIdString = blockIdStringstream.str();
131 RCP<ParameterList> coarseSpaceList = sublist(sublist(this->ParameterList_,"Blocks"),blockIdString.c_str());
132 bool useForCoarseSpace = coarseSpaceList->get("Use For Coarse Space",true);
133
134 if (useForCoarseSpace) {
135 // Das könnte man noch ändern
136 NumberOfBlocks_++;
137
138 GammaDofs_[blockId] = LOVecPtr(0);
139
140 XMultiVectorPtr mVPhiGamma;
141 XMapPtr blockCoarseMap;
142 if (useForCoarseSpace) {
143 InterfaceCoarseSpaces_[blockId].reset(new CoarseSpace<SC,LO,GO,NO>(this->MpiComm_,this->SerialComm_));
144
145 //Epetra_SerialComm serialComm;
146 XMapPtr serialGammaMap = MapFactory<LO,GO,NO>::Build(dofsMap->lib(),dofsMap->getLocalNumElements(),0,this->SerialComm_);
147 mVPhiGamma = MultiVectorFactory<LO,GO,NO>::Build(serialGammaMap,dofsMap->getLocalNumElements());
148 }
149
150 for (int i=0; i<dofsMap->getLocalNumElements(); i++) {
151 GammaDofs_[blockId]->push_back(i);
152
153 if (useForCoarseSpace) {
154 mVPhiGamma->replaceLocalValue(i,i,ScalarTraits<SC>::one());
155 }
156 }
157
158 GammaDofs_->resize(GammaDofs_.size()+1);
159 IDofs_->resize(IDofs_.size()+1);
160 InterfaceCoarseSpaces_->resize(InterfaceCoarseSpaces_.size()+1);
161 DofsMaps_->resize(DofsMaps_.size()+1);
162 DofsPerNode_->resize(DofsPerNode_.size()+1);
163
164 IDofs_[blockId] = LOVecPtr(0);
165
166 if (useForCoarseSpace) {
167 blockCoarseMap = MapFactory<LO,GO,NO>::Build(dofsMap->lib(),-1,GammaDofs_[blockId](),0,this->MpiComm_);
168
169 InterfaceCoarseSpaces_[blockId]->addSubspace(blockCoarseMap,mVPhiGamma);
170 InterfaceCoarseSpaces_[blockId]->assembleCoarseSpace();
171 }
172
173 DofsMaps_[blockId] = XMapPtrVecPtr(0);
174 DofsMaps_[blockId].push_back(dofsMap);
175
176 DofsPerNode_[blockId] = 1;
177 }
178 return 0;
179 }
180
181 template <class SC,class LO,class GO,class NO>
182 int HarmonicCoarseOperator<SC,LO,GO,NO>::computeVolumeFunctions(UN blockId,
183 UN dimension,
184 ConstXMapPtr nodesMap,
185 ConstXMultiVectorPtr nodeList,
186 EntitySetConstPtr interior)
187 {
188 FROSCH_DETAILTIMER_START_LEVELID(computeVolumeFunctionsTime,"HarmonicCoarseOperator::computeVolumeFunctions");
189 // Process the parameter list
190 stringstream blockIdStringstream;
191 blockIdStringstream << blockId+1;
192 string blockIdString = blockIdStringstream.str();
193 RCP<ParameterList> coarseSpaceList = sublist(sublist(this->ParameterList_,"Blocks"),blockIdString.c_str());
194
195 bool useForCoarseSpace = coarseSpaceList->get("Use For Coarse Space",true);
196 bool useRotations = coarseSpaceList->get("Rotations",true);
197 if (useRotations && nodeList.is_null()) {
198 useRotations = false;
199 FROSCH_WARNING("FROSch::HarmonicCoarseOperator",this->Verbose_,"Rotations cannot be used since nodeList.is_null().");
200 }
201
202 this->GammaDofs_[blockId] = LOVecPtr(this->DofsPerNode_[blockId]*interior->getEntity(0)->getNumNodes());
203 this->IDofs_[blockId] = LOVecPtr(0);
204 for (UN k=0; k<this->DofsPerNode_[blockId]; k++) {
205 for (UN i=0; i<interior->getEntity(0)->getNumNodes(); i++) {
206 this->GammaDofs_[blockId][this->DofsPerNode_[blockId]*i+k] = interior->getEntity(0)->getLocalDofID(i,k);
207 }
208 }
209
210 if (useForCoarseSpace) {
211 InterfaceCoarseSpaces_[blockId].reset(new CoarseSpace<SC,LO,GO,NO>(this->MpiComm_,this->SerialComm_));
212
213 interior->buildEntityMap(nodesMap);
214
215 XMultiVectorPtrVecPtr translations = computeTranslations(blockId,interior);
216 for (UN i=0; i<translations.size(); i++) {
217 this->InterfaceCoarseSpaces_[blockId]->addSubspace(interior->getEntityMap(),null,translations[i]);
218 }
219 if (useRotations) {
220 XMultiVectorPtrVecPtr rotations = computeRotations(blockId,dimension,nodeList,interior);
221 for (UN i=0; i<rotations.size(); i++) {
222 this->InterfaceCoarseSpaces_[blockId]->addSubspace(interior->getEntityMap(),null,rotations[i]);
223 }
224 }
225
226 InterfaceCoarseSpaces_[blockId]->assembleCoarseSpace();
227
228 // Count entities
229 GO numEntitiesGlobal = interior->getEntityMap()->getMaxAllGlobalIndex();
230 if (interior->getEntityMap()->lib()==UseEpetra || interior->getEntityMap()->getGlobalNumElements()>0) {
231 numEntitiesGlobal += 1;
232 }
233
234 if (this->Verbose_) {
235 cout
236 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
237 << setw(89) << "-----------------------------------------------------------------------------------------"
238 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
239 << "| "
240 << left << setw(74) << "> RGDSW coarse space " << right << setw(8) << "(Level " << setw(2) << this->LevelID_ << ")"
241 << " |"
242 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
243 << setw(89) << "========================================================================================="
244 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
245 << "| " << left << setw(19) << "Volumes " << " | " << setw(19) << "Translations " << right
246 << " | " << setw(41) << boolalpha << useForCoarseSpace << noboolalpha
247 << " |"
248 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
249 << "| " << left << setw(19) << "Volumes " << " | " << setw(19) << "Rotations " << right
250 << " | " << setw(41) << boolalpha << useRotations << noboolalpha
251 << " |"
252 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
253 << setw(89) << "-----------------------------------------------------------------------------------------"
254 << endl;
255 }
256 }
257 return 0;
258 }
259
260 template <class SC,class LO,class GO,class NO>
261 typename HarmonicCoarseOperator<SC,LO,GO,NO>::XMultiVectorPtrVecPtr HarmonicCoarseOperator<SC,LO,GO,NO>::computeTranslations(UN blockId,
262 EntitySetConstPtr entitySet)
263 {
264 FROSCH_DETAILTIMER_START_LEVELID(computeTranslationsTime,"HarmonicCoarseOperator::computeTranslations");
265 XMultiVectorPtrVecPtr translations(this->DofsPerNode_[blockId]);
266 XMapPtr serialGammaMap = MapFactory<LO,GO,NO>::Build(this->K_->getRangeMap()->lib(),this->GammaDofs_[blockId].size(),0,this->SerialComm_);
267 for (UN i=0; i<this->DofsPerNode_[blockId]; i++) {
268 if (entitySet->getNumEntities()>0) {
269 translations[i] = MultiVectorFactory<SC,LO,GO,NO>::Build(serialGammaMap,entitySet->getNumEntities());
270 } else {
271 translations[i] = null;
272 }
273 }
274
275 for (UN k=0; k<this->DofsPerNode_[blockId]; k++) {
276 for (UN i=0; i<entitySet->getNumEntities(); i++) {
277 for (UN j=0; j<entitySet->getEntity(i)->getNumNodes(); j++) {
278 translations[k]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,k),i,ScalarTraits<SC>::one());
279 }
280 }
281 }
282 return translations;
283 }
284
285 template <class SC,class LO,class GO,class NO>
286 typename HarmonicCoarseOperator<SC,LO,GO,NO>::XMapPtr HarmonicCoarseOperator<SC,LO,GO,NO>::assembleCoarseMap()
287 {
288 FROSCH_DETAILTIMER_START_LEVELID(assembleCoarseMapTime,"HarmonicCoarseOperator::assembleCoarseMap");
289 GOVec mapVector(0);
290 GO tmp = 0;
291 for (UN i=0; i<NumberOfBlocks_; i++) {
292 if (!InterfaceCoarseSpaces_[i].is_null()) {
293 if (InterfaceCoarseSpaces_[i]->hasBasisMap()) {
294 for (UN j=0; j<InterfaceCoarseSpaces_[i]->getBasisMap()->getLocalNumElements(); j++) {
295 mapVector.push_back(InterfaceCoarseSpaces_[i]->getBasisMap()->getGlobalElement(j)+tmp);
296 }
297 if (InterfaceCoarseSpaces_[i]->getBasisMap()->getMaxAllGlobalIndex()>=0) {
298 tmp += InterfaceCoarseSpaces_[i]->getBasisMap()->getMaxAllGlobalIndex()+1;
299 }
300 }
301 }
302 }
303 return MapFactory<LO,GO,NO>::Build(DofsMaps_[0][0]->lib(),-1,mapVector(),0,this->MpiComm_);
304 }
305
306 template <class SC,class LO,class GO,class NO>
307 typename HarmonicCoarseOperator<SC,LO,GO,NO>::XMultiVectorPtrVecPtr HarmonicCoarseOperator<SC,LO,GO,NO>::computeRotations(UN blockId,
308 UN dimension,
309 ConstXMultiVectorPtr nodeList,
310 EntitySetConstPtr entitySet,
311 UN discardRotations)
312 {
313 FROSCH_DETAILTIMER_START_LEVELID(computeRotationsTime,"HarmonicCoarseOperator::computeRotations");
314 FROSCH_ASSERT(nodeList->getNumVectors()==dimension,"FROSch::HarmonicCoarseOperator: Dimension of the nodeList is wrong.");
315 FROSCH_ASSERT(dimension==this->DofsPerNode_[blockId],"FROSch::HarmonicCoarseOperator: Dimension!=this->DofsPerNode_[blockId]");
316
317 UN rotationsPerEntity = 0;
318 switch (dimension) {
319 case 1:
320 return null;
321 case 2:
322 rotationsPerEntity = 1;
323 break;
324 case 3:
325 rotationsPerEntity = 3;
326 break;
327 default:
328 FROSCH_ASSERT(false,"FROSch::HarmonicCoarseOperator: The dimension is neither 2 nor 3!");
329 }
330
331 XMultiVectorPtrVecPtr rotations(rotationsPerEntity);
332 XMapPtr serialGammaMap = MapFactory<LO,GO,NO>::Build(this->K_->getRangeMap()->lib(),this->GammaDofs_[blockId].size(),0,this->SerialComm_);
333 for (UN i=0; i<rotationsPerEntity; i++) {
334 if (entitySet->getNumEntities()>0) {
335 rotations[i] = MultiVectorFactory<SC,LO,GO,NO>::Build(serialGammaMap,entitySet->getNumEntities());
336 } else {
337 rotations[i] = null;
338 }
339 }
340
341 SC x,y,z,rx,ry,rz;
342 SCVec rx0(3,0.0),ry0(3,0.0),rz0(3,0.0),errx(3,0.0),erry(3,0.0),errz(3,0.0);
343 for (UN i=0; i<entitySet->getNumEntities(); i++) {
344 // Compute values for the first node to check if rotation is constant
345 x = nodeList->getData(0)[entitySet->getEntity(i)->getLocalNodeID(0)];
346 y = nodeList->getData(1)[entitySet->getEntity(i)->getLocalNodeID(0)];
347
348 rx0[0] = y;
349 ry0[0] = -x;
350 if (dimension == 3) {
351 z = nodeList->getData(2)[entitySet->getEntity(i)->getLocalNodeID(0)];
352
353 rz0[0] = 0;
354
355 rx0[1] = -z;
356 ry0[1] = 0;
357 rz0[1] = x;
358
359 rx0[2] = 0;
360 ry0[2] = z;
361 rz0[2] = -y;
362 }
363 for (UN j=0; j<entitySet->getEntity(i)->getNumNodes(); j++) {
364 x = nodeList->getData(0)[entitySet->getEntity(i)->getLocalNodeID(j)];
365 y = nodeList->getData(1)[entitySet->getEntity(i)->getLocalNodeID(j)];
366
367 ////////////////
368 // Rotation 1 //
369 ////////////////
370 rx = y;
371 ry = -x;
372 rotations[0]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,0),i,rx);
373 rotations[0]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,1),i,ry);
374
375 // Compute difference (Euclidean norm of error to constant function)
376 errx[0] += (rx0[0]-rx)*(rx0[0]-rx);
377 erry[0] += (ry0[0]-ry)*(ry0[0]-ry);
378
379 if (dimension == 3) {
380 z = nodeList->getData(2)[entitySet->getEntity(i)->getLocalNodeID(j)];
381
382 rz = 0;
383 rotations[0]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,2),i,rz);
384
385 // Compute difference (Euclidean norm of error to constant function)
386 errz[0] += (rz0[0]-rz)*(rz0[0]-rz);
387
388 ////////////////
389 // Rotation 2 //
390 ////////////////
391 rx = -z;
392 ry = 0;
393 rz = x;
394 rotations[1]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,0),i,rx);
395 rotations[1]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,1),i,ry);
396 rotations[1]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,2),i,rz);
397
398 // Compute difference (Euclidean norm of error to constant function)
399 errx[1] += (rx0[1]-rx)*(rx0[1]-rx);
400 erry[1] += (ry0[1]-ry)*(ry0[1]-ry);
401 errz[1] += (rz0[1]-rz)*(rz0[1]-rz);
402
403 ////////////////
404 // Rotation 3 //
405 ////////////////
406 rx = 0;
407 ry = z;
408 rz = -y;
409 rotations[2]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,0),i,rx);
410 rotations[2]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,1),i,ry);
411 rotations[2]->replaceLocalValue(entitySet->getEntity(i)->getGammaDofID(j,2),i,rz);
412
413 // Compute difference (Euclidean norm of error to constant function)
414 errx[2] += (rx0[2]-rx)*(rx0[2]-rx);
415 erry[2] += (ry0[2]-ry)*(ry0[2]-ry);
416 errz[2] += (rz0[2]-rz)*(rz0[2]-rz);
417 }
418 }
419
420 // If error to constant function is almost zero => scale rotation with zero
421 SC err;
422 UN numZeroRotations = 0;
423 switch (dimension) {
424 case 2:
425 err = errx[0]+erry[0];
426 err = ScalarTraits<SC>::squareroot(err);
427 if (fabs(err)<1.0e-12) {
428 FROSCH_ASSERT(false,"FROSch::HarmonicCoarseOperator: In 2D, no rotation can be constant!");
429 rotations[0]->getVectorNonConst(i)->scale(ScalarTraits<SC>::zero());
430 numZeroRotations++;
431 }
432 break;
433 case 3:
434 for (UN j=0; j<3; j++) {
435 err = errx[j]+erry[j]+errz[j];
436 err = ScalarTraits<SC>::squareroot(err);
437 if (fabs(err)<1.0e-12) {
438 rotations[j]->getVectorNonConst(i)->scale(ScalarTraits<SC>::zero());
439 numZeroRotations++;
440 }
441 }
442 break;
443 default:
444 FROSCH_ASSERT(false,"FROSch::HarmonicCoarseOperator: The dimension is neither 1 nor 2 nor 3!");
445 }
446 // If necessary, discard additional rotations
447 UN rotationsToDiscard = discardRotations - numZeroRotations;
448 if (rotationsToDiscard<0) {
449 FROSCH_WARNING("FROSch::HarmonicCoarseOperator",this->Verbose_,"More rotations have been discarded than expected.");
450 } else if (rotationsToDiscard>0) {
451 UN it=0;
452 UN rotationsDiscarded=0;
453 while (rotationsDiscarded<rotationsToDiscard) {
454 if (rotations[it]->getVector(i)->norm2()>1.0e-12) {
455 rotations[it]->getVectorNonConst(i)->scale(ScalarTraits<SC>::zero());
456 rotationsDiscarded++;
457 }
458 it++;
459 }
460 }
461 }
462 return rotations;
463 }
464
465 template <class SC,class LO,class GO,class NO>
466 typename HarmonicCoarseOperator<SC,LO,GO,NO>::LOVecPtr HarmonicCoarseOperator<SC,LO,GO,NO>::detectLinearDependencies(GOVecView indicesGammaDofsAll,
467 ConstXMapPtr rowMap,
468 ConstXMapPtr rangeMap,
469 ConstXMapPtr repeatedMap,
470 SC tresholdDropping,
471 SC tresholdOrthogonalization)
472 {
473 FROSCH_DETAILTIMER_START_LEVELID(detectLinearDependenciesTime,"HarmonicCoarseOperator::detectLinearDependencies");
474 LOVecPtr linearDependentVectors(AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements()); //if (this->Verbose_) cout << AssembledInterfaceCoarseSpace_->getAssembledBasis()->getNumVectors() << " " << AssembledInterfaceCoarseSpace_->getAssembledBasis()->getLocalLength() << " " << indicesGammaDofsAll.size() << endl;
475
476 auto asembledBasis = AssembledInterfaceCoarseSpace_->getAssembledBasis();
477 auto basisMap = AssembledInterfaceCoarseSpace_->getBasisMap();
478 auto basisMapUnique = AssembledInterfaceCoarseSpace_->getBasisMapUnique();
479 UN numMVRows = asembledBasis->getLocalLength();
480 UN numCols = asembledBasis->getNumVectors();
481 if (numMVRows > 0 && numCols > 0) {
482
483 XMatrixPtr phiGamma;
484 #if defined(HAVE_XPETRA_KOKKOS_REFACTOR) && defined(HAVE_XPETRA_TPETRA)
485 if (rowMap->lib() == UseTpetra)
486 {
487 using crsmat_type = typename Matrix<SC,LO,GO,NO>::local_matrix_type;
488 using graph_type = typename crsmat_type::StaticCrsGraphType;
489 using rowptr_type = typename graph_type::row_map_type::non_const_type;
490 using indices_type = typename graph_type::entries_type::non_const_type;
491 using values_type = typename crsmat_type::values_type::non_const_type;
492 using local_mv_type = typename MultiVector<SC,LO,GO,NO>::dual_view_type::t_dev_const_um;
493 using local_map_type = typename Map<LO,GO,NO>::local_map_type;
494
495 UN numRows = rowMap->getLocalNumElements();
496
497 auto localRowMap = rowMap->getLocalMap();
498 auto localRepeatedMap = repeatedMap->getLocalMap();
499 auto localBasisMap = basisMap->getLocalMap();
500
501 auto localMVBasis = asembledBasis->getDeviceLocalView(Xpetra::Access::ReadOnly);
502
503 // Array for scaling the columns of PhiGamma (1/norm(PhiGamma(:,i)))
504 using execution_space = typename Map<LO,GO,NO>::local_map_type::execution_space;
505 using SCView = Kokkos::View<SC*, execution_space>;
506 SCView scale(Kokkos::ViewAllocateWithoutInitializing("FROSch::HarmonicCoarseOperator::detectLinearDependencies::scale"), numCols);
507 detectLinearDependenciesFunctor<GOVecView, SCView, local_map_type, local_mv_type, rowptr_type, indices_type, values_type>
508 scale_functor(numMVRows, numCols, scale, localMVBasis);
509 Kokkos::RangePolicy<ScaleTag, execution_space> policy_scale (0, numCols);
510 Kokkos::parallel_for(
511 "FROSch_HarmonicCoarseOperator::detectLinearDependencies::scale", policy_scale, scale_functor);
512
513 // count nnz
514 rowptr_type Rowptr (Kokkos::ViewAllocateWithoutInitializing("Rowptr"), numRows+1);
515 Kokkos::deep_copy(Rowptr, 0);
516 detectLinearDependenciesFunctor<GOVecView, SCView, local_map_type, local_mv_type, rowptr_type, indices_type, values_type>
517 count_functor(numMVRows, numCols, localMVBasis, tresholdDropping, indicesGammaDofsAll,
518 localRowMap, localRepeatedMap, Rowptr);
519 Kokkos::RangePolicy<CountNnzTag, execution_space> policy_count (0, numMVRows);
520 Kokkos::parallel_for(
521 "FROSch_HarmonicCoarseOperator::detectLinearDependencies::countNnz", policy_count, count_functor);
522 Kokkos::fence();
523
524 // get Nnz
525 UN nnz = 0;
526 Kokkos::RangePolicy<TotalNnzTag, execution_space> policy_nnz (0, 1+numRows);
527 Kokkos::parallel_reduce(
528 "FROSch_HarmonicCoarseOperator::detectLinearDependencies::reduceNnz", policy_nnz, count_functor, nnz);
529 Kokkos::fence();
530
531 // make it into offsets
532 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<rowptr_type, execution_space>
533 (1+numRows, Rowptr);
534 Kokkos::fence();
535
536 // allocate local matrix
537 indices_type Indices (Kokkos::ViewAllocateWithoutInitializing("Indices"), nnz);
538 values_type Values (Kokkos::ViewAllocateWithoutInitializing("Values"), nnz);
539
540 // fill in all the nz entries
541 detectLinearDependenciesFunctor<GOVecView, SCView, local_map_type, local_mv_type, rowptr_type, indices_type, values_type>
542 fill_functor(numMVRows, numCols, scale, localMVBasis, tresholdDropping, indicesGammaDofsAll,
543 localRowMap, localRepeatedMap, Rowptr, Indices, Values);
544 Kokkos::RangePolicy<FillNzEntriesTag, execution_space> policy_fill (0, numMVRows);
545 Kokkos::parallel_for(
546 "FROSch_HarmonicCoarseOperator::detectLinearDependencies::fillNz", policy_fill, fill_functor);
547 Kokkos::fence();
548
549 Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::rcp (new Teuchos::ParameterList());
550 params->set("sorted", false);
551 params->set("No Nonlocal Changes", true);
552
553 graph_type crsgraph (Indices, Rowptr);
554 crsmat_type crsmat = crsmat_type ("CrsMatrix", numRows, Values, crsgraph);
555
556 phiGamma = MatrixFactory<SC,LO,GO,NO>::Build(crsmat, rowMap, basisMap, basisMapUnique, rangeMap,
557 params);
558 } else
559 #endif
560 {
561 // Array for scaling the columns of PhiGamma (1/norm(PhiGamma(:,i)))
562 SCVec scale(numCols, 0.0);
563 for (UN j = 0; j < numCols; j++) {
564 ConstSCVecPtr assembledInterfaceCoarseSpaceData = asembledBasis->getData(j);
565 for (UN i = 0; i < numMVRows; i++) {
566 scale[j] += assembledInterfaceCoarseSpaceData[i]*assembledInterfaceCoarseSpaceData[i];
567 }
568 scale[j] = 1.0/sqrt(scale[j]);
569 }
570
571 //Construct matrix phiGamma
572 phiGamma = MatrixFactory<SC,LO,GO,NO>::Build(rowMap,basisMap->getLocalNumElements());
573 LO iD;
574 SC valueTmp;
575 for (UN i=0; i<asembledBasis->getLocalLength(); i++) {
576 iD = repeatedMap->getGlobalElement(indicesGammaDofsAll[i]);
577 if (rowMap->getLocalElement(iD)!=-1) { // This should prevent duplicate entries on the interface
578 GOVec indices;
579 SCVec values;
580 for (UN j=0; j<asembledBasis->getNumVectors(); j++) {
581 valueTmp=asembledBasis->getData(j)[i];
582 if (fabs(valueTmp)>tresholdDropping) {
583 indices.push_back(basisMap->getGlobalElement(j));
584 values.push_back(valueTmp*scale[j]);
585 }
586 }
587 phiGamma->insertGlobalValues(iD,indices(),values());
588 }
589 }
590 RCP<ParameterList> fillCompleteParams(new ParameterList);
591 fillCompleteParams->set("No Nonlocal Changes", true);
592 phiGamma->fillComplete(basisMapUnique,rangeMap,fillCompleteParams);
593 }
594
595 //Compute Phi^T * Phi
596 RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout));
597 XMatrixPtr phiTPhi = MatrixMatrix<SC,LO,GO,NO>::Multiply(*phiGamma,true,*phiGamma,false,*fancy); //phiGamma->describe(*fancy,VERB_EXTREME); phiTPhi->describe(*fancy,VERB_EXTREME); //AssembledInterfaceCoarseSpace_->getBasisMap()->describe(*fancy,VERB_EXTREME);
598
599 // Extract local part of the matrix
600 ConstXMatrixPtr repeatedPhiTPhi = ExtractLocalSubdomainMatrix(phiTPhi.getConst(),AssembledInterfaceCoarseSpace_->getBasisMap());
601 //Communicate matrix to the repeated map
602 // repeatedPhiTPhi = MatrixFactory<SC,LO,GO,NO>::Build(AssembledInterfaceCoarseSpace_->getBasisMap());
603 // XExportPtr exporter = ExportFactory<LO,GO,NO>::Build(rowMap,repeatedMap);
604 // repeatedPhiTPhi->doExport(*phiTPhi,*exporter,INSERT);
605
606 UN numRows = repeatedPhiTPhi->getRowMap()->getLocalNumElements();
607 TSerialDenseMatrixPtr denseRepeatedPhiTPhi(new SerialDenseMatrix<LO,SC>(numRows,repeatedPhiTPhi->getColMap()->getLocalNumElements()));
608 for (UN i=0; i<numRows; i++) {
609 ConstLOVecView indices;
610 ConstSCVecView values;
611 repeatedPhiTPhi->getLocalRowView(i,indices,values);
612 for (UN j=0; j<indices.size(); j++) {
613 (*denseRepeatedPhiTPhi)(i,indices[j]) = values[j];
614 }
615 }
616 // if (this->MpiComm_->getRank()==3) {
617 // for (LO i=0; i<denseRepeatedPhiTPhi->numRows(); i++) {
618 // for (LO j=0; j<denseRepeatedPhiTPhi->numCols(); j++) {
619 // cout << (*denseRepeatedPhiTPhi)(i,j) << " ";
620 // }
621 // cout << endl;
622 // }
623 // }
624
625 //Compute local QR factorization
626 TSerialQRDenseSolverPtr qRSolver(new SerialQRDenseSolver<LO,SC>());
627 qRSolver->setMatrix(denseRepeatedPhiTPhi);
628 qRSolver->factor();
629 qRSolver->formQ();
630 qRSolver->formR();
631
632 //Find rows of R approx. zero
633 TSerialDenseMatrixPtr r = qRSolver->getR();
634 LO tmp = 0;
635 for (LO i=0; i<r->numRows(); i++) {
636 // SC normRow = 0.0;
637 // for (LO j=0; j<r->numCols(); j++) {
638 // normRow += (*r)(i,j)*(*r)(i,j);
639 // }
640 // if (sqrt(normRow)<treshold) {
641 // //cout << this->MpiComm_->getRank() << " " << i << " " << AssembledInterfaceCoarseSpace_->getBasisMap()->getGlobalElement(i) << " " << sqrt(normRow) << std::endl;
642 // linearDependentVectors[tmp] = i;
643 // tmp++;
644 // }
645 if (fabs((*r)(i,i))<tresholdOrthogonalization) {
646 //cout << this->MpiComm_->getRank() << " " << i << " " << AssembledInterfaceCoarseSpace_->getBasisMap()->getGlobalElement(i) << " " << sqrt(normRow) << std::endl;
647 linearDependentVectors[tmp] = i;
648 tmp++;
649 }
650 }
651 linearDependentVectors.resize(tmp);
652 }
653
654 FROSCH_DETAILTIMER_START_LEVELID(printStatisticsTime,"print statistics");
655 // Statistics on linear dependencies
656 GO global = AssembledInterfaceCoarseSpace_->getBasisMap()->getMaxAllGlobalIndex();
657 if (AssembledInterfaceCoarseSpace_->getBasisMap()->lib()==UseEpetra || AssembledInterfaceCoarseSpace_->getBasisMap()->getGlobalNumElements()>0) {
658 global += 1;
659 }
660 LOVec localVec(3);
661 LOVec sumVec(3);
662 SCVec avgVec(3);
663 LOVec minVec(3);
664 LOVec maxVec(3);
665
666 LO numLocalBasisFunctions = AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements();
667 LO numLocalLinearDependencies = linearDependentVectors.size();
668 LO numLocalLinearIndependencies = numLocalBasisFunctions-numLocalLinearDependencies;
669
670 sumVec[0] = AssembledInterfaceCoarseSpace_->getBasisMap()->getGlobalNumElements();
671 avgVec[0] = max(sumVec[0]/double(this->MpiComm_->getSize()),0.0);
672 reduceAll(*this->MpiComm_,REDUCE_MIN,numLocalBasisFunctions,ptr(&minVec[0]));
673 reduceAll(*this->MpiComm_,REDUCE_MAX,numLocalBasisFunctions,ptr(&maxVec[0]));
674
675 reduceAll(*this->MpiComm_,REDUCE_SUM,numLocalLinearDependencies,ptr(&sumVec[1]));
676 avgVec[1] = max(sumVec[1]/double(this->MpiComm_->getSize()),0.0);
677 reduceAll(*this->MpiComm_,REDUCE_MIN,numLocalLinearDependencies,ptr(&minVec[1]));
678 reduceAll(*this->MpiComm_,REDUCE_MAX,numLocalLinearDependencies,ptr(&maxVec[1]));
679
680 reduceAll(*this->MpiComm_,REDUCE_SUM,numLocalLinearIndependencies,ptr(&sumVec[2]));
681 avgVec[2] = max(sumVec[2]/double(this->MpiComm_->getSize()),0.0);
682 reduceAll(*this->MpiComm_,REDUCE_MIN,numLocalLinearIndependencies,ptr(&minVec[2]));
683 reduceAll(*this->MpiComm_,REDUCE_MAX,numLocalLinearIndependencies,ptr(&maxVec[2]));
684
685 if (this->Verbose_) {
686 cout
687 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
688 << setw(89) << "-----------------------------------------------------------------------------------------"
689 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
690 << "| "
691 << left << setw(74) << "> Local Linear Dependencies of Coarse Basis Functions Statistics " << right << setw(8) << "(Level " << setw(2) << this->LevelID_ << ")"
692 << " |"
693 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
694 << setw(89) << "========================================================================================="
695 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
696 << "| " << left << setw(20) << " " << right
697 << " | " << setw(10) << "total"
698 << " | " << setw(10) << "avg"
699 << " | " << setw(10) << "min"
700 << " | " << setw(10) << "max"
701 << " | " << setw(10) << "global sum"
702 << " |"
703 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
704 << setw(89) << "-----------------------------------------------------------------------------------------"
705 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
706 << "| " << left << setw(20) << "Basis functions" << right
707 << " | " << setw(10) << global
708 << " | " << setw(10) << setprecision(5) << avgVec[0]
709 << " | " << setw(10) << minVec[0]
710 << " | " << setw(10) << maxVec[0]
711 << " | " << setw(10) << sumVec[0]
712 << " |"
713 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
714 << "| " << left << setw(20) << "Dependent" << right
715 << " | " << setw(10) << " "
716 << " | " << setw(10) << setprecision(5) << avgVec[1]
717 << " | " << setw(10) << minVec[1]
718 << " | " << setw(10) << maxVec[1]
719 << " | " << setw(10) << sumVec[1]
720 << " |"
721 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
722 << "| " << left << setw(20) << "Independent" << right
723 << " | " << setw(10) << " "
724 << " | " << setw(10) << setprecision(5) << avgVec[2]
725 << " | " << setw(10) << minVec[2]
726 << " | " << setw(10) << maxVec[2]
727 << " | " << setw(10) << sumVec[2]
728 << " |"
729 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
730 << setw(89) << "-----------------------------------------------------------------------------------------"
731 << endl;
732 }
733 FROSCH_DETAILTIMER_STOP(printStatisticsTime);
734
735 return linearDependentVectors;
736 }
737
738 template <class SC,class LO,class GO,class NO>
739 typename HarmonicCoarseOperator<SC,LO,GO,NO>::XMultiVectorPtr HarmonicCoarseOperator<SC,LO,GO,NO>::computeExtensions(ConstXMapPtr localMap,
740 GOVecView indicesGammaDofsAll,
741 GOVecView indicesIDofsAll,
742 XMatrixPtr kII,
743 XMatrixPtr kIGamma)
744 {
745 FROSCH_DETAILTIMER_START_LEVELID(computeExtensionsTime,"HarmonicCoarseOperator::computeExtensions");
746
747 if (this->Verbose_) {
748 cout
749 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
750 << setw(89) << "-----------------------------------------------------------------------------------------"
751 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
752 << "| "
753 << left << setw(74) << "> Extensions solver" << right << setw(8) << "(Level " << setw(2) << this->LevelID_ << ")"
754 << " |"
755 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
756 << setw(89) << "========================================================================================="
757 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
758 << "| " << left << setw(41) << "Solver type" << right
759 << " | " << setw(41) << this->ParameterList_->sublist("ExtensionSolver").get("SolverType","Amesos2")
760 << " |"
761 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
762 << "| " << left << setw(41) << "Solver" << right
763 << " | " << setw(41) << this->ParameterList_->sublist("ExtensionSolver").get("Solver","Klu")
764 << " |"
765 << "\n" << setw(FROSCH_OUTPUT_INDENT) << " "
766 << setw(89) << "-----------------------------------------------------------------------------------------"
767 << endl;
768 }
769
770 //this->Phi_ = MatrixFactory<SC,LO,GO,NO>::Build(this->K_->getRangeMap(),AssembledInterfaceCoarseSpace_->getBasisMap(),AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements()); // Nonzeroes abhängig von dim/dofs!!!
771 XMultiVectorPtr mVPhi = MultiVectorFactory<SC,LO,GO,NO>::Build(localMap,AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements());
772 XMultiVectorPtr mVtmp = MultiVectorFactory<SC,LO,GO,NO>::Build(kII->getRowMap(),AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements());
773 XMultiVectorPtr mVPhiI = MultiVectorFactory<SC,LO,GO,NO>::Build(kII->getRowMap(),AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements());
774
775 //Build mVPhiGamma
776 XMultiVectorPtr mVPhiGamma = MultiVectorFactory<SC,LO,GO,NO>::Build(kIGamma->getDomainMap(),AssembledInterfaceCoarseSpace_->getBasisMap()->getLocalNumElements());
777 if (AssembledInterfaceCoarseSpace_->hasAssembledBasis()) {
778 for (UN i=0; i<AssembledInterfaceCoarseSpace_->getAssembledBasis()->getNumVectors(); i++) {
779 ConstSCVecPtr assembledInterfaceCoarseSpaceData = AssembledInterfaceCoarseSpace_->getAssembledBasis()->getData(i);
780 for (UN j=0; j<AssembledInterfaceCoarseSpace_->getAssembledBasis()->getLocalLength(); j++) {
781 mVPhiGamma->replaceLocalValue(j,i,assembledInterfaceCoarseSpaceData[j]);
782 mVPhi->replaceLocalValue(indicesGammaDofsAll[j],i,assembledInterfaceCoarseSpaceData[j]);
783 }
784 }
785 }
786 // LO jj=0;
787 // LO kk=0;
788 // UNVec numLocalBlockRows(NumberOfBlocks_);
789 // for (UN i=0; i<NumberOfBlocks_; i++) {
790 // UN j = 0;
791 // UN k = 0;
792 // if (InterfaceCoarseSpaces_[i]->hasAssembledBasis()) {
793 // numLocalBlockRows[i] = InterfaceCoarseSpaces_[i]->getAssembledBasis()->getNumVectors();
794 // for (j=0; j<numLocalBlockRows[i]; j++) {
795 // for (k=0; k<InterfaceCoarseSpaces_[i]->getAssembledBasis()->getLocalLength(); k++) {
796 // mVPhiGamma->replaceLocalValue(k+kk,j+jj,InterfaceCoarseSpaces_[i]->getAssembledBasis()->getData(j)[k]);
797 // mVPhi->replaceLocalValue(indicesGammaDofsAll[k+kk],j+jj,InterfaceCoarseSpaces_[i]->getAssembledBasis()->getData(j)[k]);
798 // }
799 // }
800 // } else { // Das ist für den Fall, dass keine Basisfunktionen für einen Block gebaut werden sollen
801 // k=GammaDofs_[i].size();
802 // }
803 // jj += j;
804 // kk += k;
805 // }
806 // RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); this->Phi_->describe(*fancy,VERB_EXTREME);
807 // Hier Multiplikation kIGamma*PhiGamma
808 kIGamma->apply(*mVPhiGamma,*mVtmp);
809
810 mVtmp->scale(-ScalarTraits<SC>::one());
811
812 // Jetzt der solver für kII
813 if (indicesIDofsAll.size()>0) {
814 if (this->coarseExtractLocalSubdomainMatrix_Symbolic_Done_) {
815 ExtensionSolver_->updateMatrix(kII, true);
816 } else {
817 ExtensionSolver_ = SolverFactory<SC,LO,GO,NO>::Build(kII,
818 sublist(this->ParameterList_,"ExtensionSolver"),
819 string("ExtensionSolver (Level ") + to_string(this->LevelID_) + string(")"));
820 ExtensionSolver_->initialize();
821 }
822 ExtensionSolver_->compute();
823 ExtensionSolver_->apply(*mVtmp,*mVPhiI);
824 }
825
826 GOVec2D excludeCols(NumberOfBlocks_);
827
828 LOVec bound(NumberOfBlocks_+1,(LO)0);
829 for (UN i=0; i<NumberOfBlocks_; i++) {
830 bound[i+1] = bound[i] + this->IDofs_[i].size();
831 }
832
833 LO itmp = 0;
834 ConstUNVecView numLocalBlockColumns = AssembledInterfaceCoarseSpace_->getLocalSubspaceSizes();
835 FROSCH_ASSERT(numLocalBlockColumns.size()==NumberOfBlocks_,"FROSch::HarmonicCoarseOperator: numLocalBlockColumns.size()!=NumberOfBlocks_("+to_string(numLocalBlockColumns.size())+", "+to_string(NumberOfBlocks_)+") ");
836 for (UN i=0; i<NumberOfBlocks_; i++) {
837 stringstream blockIdStringstream;
838 blockIdStringstream << i+1;
839 string blockIdString = blockIdStringstream.str();
840 RCP<ParameterList> coarseSpaceList = sublist(sublist(this->ParameterList_,"Blocks"),blockIdString.c_str());
841 string excludeBlocksString = coarseSpaceList->get("Exclude","0");
842 UNVec excludeBlocks = FROSch::GetIndicesFromString(excludeBlocksString,(UN)0);
843 sortunique(excludeBlocks);
844 for (UN j=0; j<excludeBlocks.size(); j++) {
845 excludeBlocks[j] -= 1;
846 }
847 UNVec extensionBlocks(0);
848 for (UN j=0; j<NumberOfBlocks_; j++) {
849 typename UNVec::iterator it = find(excludeBlocks.begin(),excludeBlocks.end(),j);
850 if (it == excludeBlocks.end()) {
851 extensionBlocks.push_back(j);
852 }
853 }
854
855 #if defined(HAVE_XPETRA_TPETRA)
856 if (mVPhi->getMap()->lib() == UseTpetra) {
857 using XMap = typename SchwarzOperator<SC,LO,GO,NO>::XMap;
858 using execution_space = typename XMap::local_map_type::execution_space;
859
860 // explicitly copying indicesIDofsAll in Teuchos::Array to Kokkos::View on "device"
861 using GOIndViewHost = Kokkos::View<GO*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryUnmanaged>;
862 using GOIndView = Kokkos::View<GO*, execution_space>;
863 UN numIndices = indicesIDofsAll.size();
864 GOIndViewHost indicesIDofsAllHostData(indicesIDofsAll.getRawPtr(), numIndices);
865 GOIndView indicesIDofsAllData (Kokkos::ViewAllocateWithoutInitializing("indicesIDofsAllData"), numIndices);
866 Kokkos::deep_copy(indicesIDofsAllData, indicesIDofsAllHostData);
867
868 using xTMVector = Xpetra::TpetraMultiVector<SC,LO,GO,NO>;
869 using TMVector = Tpetra::MultiVector<SC,LO,GO,NO>;
870 using SCView = typename TMVector::dual_view_type::t_dev;
871 using ConstSCView = typename TMVector::dual_view_type::t_dev::const_type;
872 // Xpetra wrapper for Tpetra MV
873 auto mVPhiIXTpetraMVector = rcp_dynamic_cast<const xTMVector>(mVPhiI, true);
874 auto mVPhiXTpetraMVector = rcp_dynamic_cast< xTMVector>(mVPhi, true);
875 // Tpetra MV
876 auto mVPhiITpetraMVector = mVPhiIXTpetraMVector->getTpetra_MultiVector();
877 auto mVPhiTpetraMVector = mVPhiXTpetraMVector->getTpetra_MultiVector();
878 // Kokkos-Kernels Views
879 auto mvPhiIView = mVPhiITpetraMVector->getLocalViewDevice(Tpetra::Access::ReadOnly);
880 auto mvPhiView = mVPhiTpetraMVector->getLocalViewDevice(Tpetra::Access::ReadWrite);
881 auto mvPhiICols = Tpetra::getMultiVectorWhichVectors(*mVPhiITpetraMVector);
882 auto mvPhiCols = Tpetra::getMultiVectorWhichVectors(*mVPhiTpetraMVector);
883 for (UN j=0; j<numLocalBlockColumns[i]; j++) {
884 int col_in = mVPhiITpetraMVector->isConstantStride() ? itmp : mvPhiICols[itmp];
885 int col_out = mVPhiTpetraMVector->isConstantStride() ? itmp : mvPhiCols[itmp];
886 CopyPhiViewFunctor<GOIndView, ConstSCView, SCView> functor(col_in, indicesIDofsAllData, mvPhiIView, col_out, mvPhiView);
887 for (UN ii=0; ii<extensionBlocks.size(); ii++) {
888 Kokkos::RangePolicy<execution_space> policy (bound[extensionBlocks[ii]], bound[extensionBlocks[ii]+1]);
889 Kokkos::parallel_for(
890 "FROSch_HarmonicCoarseOperator::fillPhiData", policy, functor);
891 }
892 itmp++;
893 }
894 Kokkos::fence();
895 } else
896 #endif
897 {
898 for (UN j=0; j<numLocalBlockColumns[i]; j++) {
899 ConstSCVecPtr mVPhiIData = mVPhiI->getData(itmp);
900 for (UN ii=0; ii<extensionBlocks.size(); ii++) {
901 for (LO k=bound[extensionBlocks[ii]]; k<bound[extensionBlocks[ii]+1]; k++) {
902 mVPhi->replaceLocalValue(indicesIDofsAll[k],itmp,mVPhiIData[k]);
903 }
904 }
905 itmp++;
906 }
907 }
908 }
909 return mVPhi;
910 }
911
912 // Should we move this to Tools?
913 template <class SC,class LO,class GO,class NO>
914 typename HarmonicCoarseOperator<SC,LO,GO,NO>::ConstXMatrixPtr HarmonicCoarseOperator<SC,LO,GO,NO>::removeCouplingBetweenDofs(ConstXMatrixPtr matrix,
915 ConstXMapPtr map,
916 TwoDArray<int> &couplingIDsToRemove)
917 {
918 FROSCH_DETAILTIMER_START_LEVELID(removeCouplingBetweenDofsTime,"HarmonicCoarseOperator::removeCouplingBetweenDofs");
919
920 ConstUNVecView numLocalBlockRows = AssembledInterfaceCoarseSpace_->getLocalSubspaceSizes();
921 FROSCH_ASSERT(numLocalBlockRows.size()==NumberOfBlocks_,"FROSch::HarmonicCoarseOperator: numLocalBlockRows.size()!=NumberOfBlocks_");
922
923 // Array<RCP<MultiVector<bool,LO,GO,NO> > > mask(NumberOfBlocks_);
924 // for (UN i=0; i<NumberOfBlocks_; i++) {
925 // mask[i] = MultiVectorFactory<bool,LO,GO,NO>::Build(matrix->getRowMap(),DofsPerNode_[i]);
926 // mask[i]->putScalar(false);
927 // }
928 Array<Array<bool> > mask(NumberOfBlocks_);
929 for (UN i=0; i<NumberOfBlocks_; i++) {
930 mask[i] = Array<bool>(DofsPerNode_[i]*map->getLocalNumElements(),false);
931 }
932
933 FROSCH_ASSERT(couplingIDsToRemove.getNumCols()==4,"FROSch::HarmonicCoarseOperator: couplingIDsToRemove.getNumCols()!=4");
934 for (UN i=0; i<couplingIDsToRemove.getNumRows() ; i++) {
935 FROSCH_ASSERT(couplingIDsToRemove[i][0]>=0,"FROSch::HarmonicCoarseOperator: couplingIDsToRemove[i][0]<0");
936 FROSCH_ASSERT(couplingIDsToRemove[i][1]>=0,"FROSch::HarmonicCoarseOperator: couplingIDsToRemove[i][1]<0");
937 FROSCH_ASSERT(couplingIDsToRemove[i][2]>=0,"FROSch::HarmonicCoarseOperator: couplingIDsToRemove[i][2]<0");
938 FROSCH_ASSERT(couplingIDsToRemove[i][3]>=0,"FROSch::HarmonicCoarseOperator: couplingIDsToRemove[i][3]<0");
939
940 int blockID1 = couplingIDsToRemove[i][0];
941 int dofID1 = couplingIDsToRemove[i][1];
942
943 int blockID2 = couplingIDsToRemove[i][2];
944 int dofID2 = couplingIDsToRemove[i][3];
945
946 Array<bool>& tmpMask = mask[blockID1];
947 for (UN j=0; j<DofsMaps_[blockID2][dofID2]->getLocalNumElements(); j++) {
948 GO globalIndex = DofsMaps_[blockID2][dofID2]->getGlobalElement(j);
949 FROSCH_ASSERT(globalIndex>=0,"FROSch::HarmonicCoarseOperator: globalIndex<0");
950 LO localIndex = map->getLocalElement(globalIndex);
951 FROSCH_ASSERT(localIndex>=0,"FROSch::HarmonicCoarseOperator: localIndex<0");
952 //mask[blockID1]->replaceLocalValue(localIndex,dofID1,true);
953 tmpMask[dofID1*map->getLocalNumElements()+localIndex] = true;
954 }
955 }
956
957 XMatrixPtr reducedMatrix = MatrixFactory<SC,LO,GO,NO>::Build(matrix->getRowMap(),matrix->getLocalMaxNumRowEntries());
958 for (UN i=0; i<NumberOfBlocks_; i++) {
959 Array<bool>& tmpMask = mask[i];
960 for (UN j=0; j<DofsPerNode_[i]; j++) {
961 //ConstBoolVecPtr maskData = mask[i]->getData(j);
962 for (UN k=0; k<DofsMaps_[i][j]->getLocalNumElements(); k++) {
963 GO globalIndex = DofsMaps_[i][j]->getGlobalElement(k);
964 FROSCH_ASSERT(globalIndex>=0,"FROSch::HarmonicCoarseOperator: globalIndex<0");
965 LO localIndex = map->getLocalElement(globalIndex);
966 FROSCH_ASSERT(localIndex>=0,"FROSch::HarmonicCoarseOperator: localIndex<0");
967
968 ArrayView<const LO> indices;
969 ArrayView<const SC> values;
970 matrix->getLocalRowView(localIndex,indices,values);
971
972 LO size = indices.size();
973 if (size>0) {
974 Array<GO> indicesGlobal;
975 Array<SC> valuesGlobal;
976 for (LO l=0; l<size; l++) {
977 //if (!maskData[indices[l]]) {
978 if (!tmpMask[j*map->getLocalNumElements()+indices[l]]) {
979 indicesGlobal.push_back(matrix->getRowMap()->getGlobalElement(indices[l]));
980 valuesGlobal.push_back(values[l]);
981 }
982 }
983 reducedMatrix->insertGlobalValues(matrix->getRowMap()->getGlobalElement(localIndex),indicesGlobal(),valuesGlobal());
984 }
985 }
986 }
987 }
988 reducedMatrix->fillComplete();
989
990 // for (UN i=0; i<NumberOfBlocks_; i++) {
991 // if (this->Verbose_) {RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); matrix->describe(*fancy,VERB_EXTREME);}
992 // if (this->Verbose_) {RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout)); reducedMatrix->describe(*fancy,VERB_EXTREME);}
993 // }
994
995 return reducedMatrix.getConst();
996 }
997
998 template <class SC,class LO,class GO, class NO>
999 int HarmonicCoarseOperator<SC,LO,GO,NO>::buildElementNodeList()
1000 {
1001 //get elements belonging to one subdomain
1002 FROSCH_DETAILTIMER_START_LEVELID(buildElementNodeListTime,"CoarseOperator::buildElementNodeList");
1003
1004 Teuchos::ArrayView<const GO> elements = KRowMap_->getLocalElementList();
1005 UN maxNumElements = -1;
1006 UN numElementsLocal = elements.size();
1007
1008 reduceAll(*this->MpiComm_,Teuchos::REDUCE_MAX,numElementsLocal,Teuchos::ptr(&maxNumElements));
1009
1010
1011 GraphPtr elemGraph = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLGatheringMaps_[0],maxNumElements);
1012 Teuchos::ArrayView<const GO> myGlobals = this->SubdomainConnectGraph_->getRowMap()->getLocalElementList();
1013
1014 for (size_t i = 0; i < this->SubdomainConnectGraph_->getRowMap()->getLocalNumElements(); i++) {
1015 elemGraph->insertGlobalIndices(myGlobals[i],elements);
1016 }
1017 elemGraph->fillComplete();
1018
1019 GraphPtr tmpElemGraph = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLGatheringMaps_[1],maxNumElements);
1020 GraphPtr elemSGraph;
1021 //communicate ElemGrapg to CoarseComm_
1022 tmpElemGraph->doExport(*elemGraph,*this->MLCoarseSolveExporters_[1],Xpetra::INSERT);
1023 UN gathered = 0;
1024 for (int i = 2;i<this->MLGatheringMaps_.size();i++) {
1025 gathered = 1;
1026 tmpElemGraph->fillComplete();
1027 elemSGraph = tmpElemGraph;
1028 tmpElemGraph = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLGatheringMaps_[i],maxNumElements);
1029 tmpElemGraph->doExport(*elemSGraph,*this->MLCoarseSolveExporters_[i],Xpetra::INSERT);
1030 }
1031 //tmpElemGraph->fillComplete();
1032 elemSGraph = tmpElemGraph;
1033
1034 if (gathered == 0) {
1035 elemSGraph = tmpElemGraph;
1036 }
1037 this->ElementNodeList_ =Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLCoarseMap_,maxNumElements);
1038
1039 if (this->OnCoarseSolveComm_) {
1040 const size_t numMyElementS = elemSGraph->getMap()->getLocalNumElements();
1041 Teuchos::ArrayView<const GO> va;
1042 for (UN i = 0; i < numMyElementS; i++) {
1043 GO kg = this->MLGatheringMaps_[this->MLGatheringMaps_.size()-1]->getGlobalElement(i);
1044 tmpElemGraph->getGlobalRowView(kg,va);
1045 Teuchos::Array<GO> vva(va);
1046 this->ElementNodeList_->insertGlobalIndices(kg,vva());//mal va nehmen
1047 }
1048 this->ElementNodeList_->fillComplete();
1049
1050 }
1051 return 0;
1052 }
1053
1054 template<class SC,class LO, class GO, class NO>
1055 int HarmonicCoarseOperator<SC,LO,GO,NO>::buildGlobalGraph(Teuchos::RCP<DDInterface<SC,LO,GO,NO> > theDDInterface)
1056 {
1057 FROSCH_DETAILTIMER_START_LEVELID(buildGlobalGraphTime,"HarmonicCoarseOperator::buildGlobalGraph");
1058 std::map<GO,int> rep;
1059 Teuchos::Array<GO> entries;
1060 IntVec2D conn;
1061 InterfaceEntityPtrVec connVec;
1062 int connrank;
1063 //get connected subdomains
1064 theDDInterface->identifyConnectivityEntities();
1065 EntitySetConstPtr connect= theDDInterface->getConnectivityEntities();
1066 connect->buildEntityMap(theDDInterface->getNodesMap());
1067 connVec = connect->getEntityVector();
1068 connrank = connect->getEntityMap()->getComm()->getRank();
1069
1070 GO connVecSize = connVec.size();
1071 conn.resize(connVecSize);
1072 {
1073 if (connVecSize>0) {
1074 for (GO i = 0;i<connVecSize;i++) {
1075 conn[i] = connVec[i]->getSubdomainsVector();
1076 for (int j = 0; j<conn[i].size(); j++) rep.insert(std::pair<GO,int>(conn.at(i).at(j),connrank));
1077 }
1078 for (auto& x: rep) {
1079 entries.push_back(x.first);
1080 }
1081 }
1082 }
1083
1084 const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid();
1085 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > graphMap = Xpetra::MapFactory<LO,GO,NO>::Build(this->K_->getMap()->lib(),INVALID,1,0,this->K_->getMap()->getComm());
1086
1087 //UN maxNumElements = -1;
1088 //get the maximum number of neighbors for a subdomain
1089 MaxNumNeigh_ = -1;
1090 UN numElementsLocal = entries.size();
1091 reduceAll(*this->MpiComm_,Teuchos::REDUCE_MAX,numElementsLocal,Teuchos::ptr(&MaxNumNeigh_));
1092 this->SubdomainConnectGraph_ = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(graphMap,MaxNumNeigh_);
1093 this->SubdomainConnectGraph_->insertGlobalIndices(graphMap->getComm()->getRank(),entries());
1094
1095 return 0;
1096 }
1097
1098 template <class SC,class LO, class GO,class NO>
1099 int HarmonicCoarseOperator<SC,LO,GO,NO>::buildCoarseGraph()
1100 {
1101 //bring graph to CoarseSolveComm_
1102 FROSCH_DETAILTIMER_START_LEVELID(buildCoarseGraphTime,"CoarseOperator::buildCoarseGraph");
1103
1104 //create temporary graphs to perform communication (possibly with gathering steps)
1105 GraphPtr tmpGraph = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLGatheringMaps_[1],MaxNumNeigh_);;
1106 GraphPtr tmpGraphGathering;
1107
1108 tmpGraph->doExport(*this->SubdomainConnectGraph_,*this->MLCoarseSolveExporters_[1],Xpetra::INSERT);
1109
1110 for (int i = 2;i<this->MLGatheringMaps_.size();i++) {
1111 tmpGraph->fillComplete();
1112 tmpGraphGathering = tmpGraph;
1113 tmpGraph = Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLGatheringMaps_[i],MaxNumNeigh_);
1114 tmpGraph->doExport(*tmpGraphGathering,*this->MLCoarseSolveExporters_[i],Xpetra::INSERT);
1115 }
1116 const size_t numMyElementS = this->MLGatheringMaps_[this->MLGatheringMaps_.size()-1]->getLocalNumElements();
1117 this->SubdomainConnectGraph_= Xpetra::CrsGraphFactory<LO,GO,NO>::Build(this->MLCoarseMap_,MaxNumNeigh_);
1118
1119 if (this->OnCoarseSolveComm_) {
1120 for (size_t k = 0; k<numMyElementS; k++) {
1121 Teuchos::ArrayView<const LO> in;
1122 Teuchos::ArrayView<const GO> vals_graph;
1123 GO kg = this->MLGatheringMaps_[this->MLGatheringMaps_.size()-1]->getGlobalElement(k);
1124 tmpGraph->getGlobalRowView(kg,vals_graph);
1125 Teuchos::Array<GO> vals(vals_graph);
1126 this->SubdomainConnectGraph_->insertGlobalIndices(kg,vals());
1127 }
1128 this->SubdomainConnectGraph_->fillComplete();
1129 }
1130 return 0;
1131 }
1132
1133 template <class SC,class LO,class GO,class NO>
1134 void HarmonicCoarseOperator<SC,LO,GO,NO>::extractLocalSubdomainMatrix_Symbolic()
1135 {
1136 if (this->K_->getRowMap()->lib() == UseTpetra) {
1137 FROSCH_DETAILTIMER_START_LEVELID(extractLocalSubdomainMatrix_SymbolicTime,"HarmonicCoarseOperatorCoarseOperator::extractLocalSubdomainMatrix_Symbolic");
1138 XMapPtr repeatedMap = AssembleSubdomainMap(this->NumberOfBlocks_, this->DofsMaps_, this->DofsPerNode_);
1139
1140 // buid sudomain matrix
1141 this->coarseSubdomainMatrix_ = MatrixFactory<SC,LO,GO,NO>::Build(repeatedMap, repeatedMap, this->K_->getGlobalMaxNumRowEntries());
1142 this->coarseScatter_ = ImportFactory<LO,GO,NO>::Build(this->K_->getRowMap(), repeatedMap);
1143 this->coarseSubdomainMatrix_->doImport(*(this->K_.getConst()), *(this->coarseScatter_),ADD);
1144
1145 // build local subdomain matrix
1146 RCP<const Comm<LO> > SerialComm = rcp(new MpiComm<LO>(MPI_COMM_SELF));
1147 RCP<Map<LO,GO,NO> > localSubdomainMap = MapFactory<LO,GO,NO>::Build(repeatedMap->lib(), repeatedMap->getLocalNumElements(), 0, SerialComm);
1148 this->coarseLocalSubdomainMatrix_ = MatrixFactory<SC,LO,GO,NO>::Build(localSubdomainMap, localSubdomainMap, this->K_->getGlobalMaxNumRowEntries());
1149
1150 // fill in column indexes
1151 ExtractLocalSubdomainMatrix_Symbolic(this->coarseSubdomainMatrix_, // input
1152 this->coarseLocalSubdomainMatrix_); // output
1153
1154 // turn flag on
1155 this->coarseExtractLocalSubdomainMatrix_Symbolic_Done_ = true;
1156
1157
1158 // -------------------------------------------------------
1159 // symbolic for computeExtensions to factor kII
1160 GOVec indicesGammaDofsAll(0);
1161 GOVec indicesIDofsAll(0);
1162 LO tmp = 0;
1163
1164 for (UN i=0; i<NumberOfBlocks_; i++) {
1165 for (UN j=0; j<GammaDofs_[i].size(); j++) {
1166 indicesGammaDofsAll.push_back(tmp+GammaDofs_[i][j]);
1167 }
1168 for (UN j=0; j<IDofs_[i].size(); j++) {
1169 indicesIDofsAll.push_back(tmp+IDofs_[i][j]);
1170 }
1171 tmp += GammaDofs_[i].size()+IDofs_[i].size();
1172 }
1173
1174 // extract local submatrices
1175 XMatrixPtr kII;
1176 XMatrixPtr kIGamma;
1177 XMatrixPtr kGammaI;
1178 XMatrixPtr kGammaGamma;
1179 auto repeatedMatrix = this->coarseLocalSubdomainMatrix_;
1180 BuildSubmatrices(repeatedMatrix.getConst(),indicesIDofsAll(),kII,kIGamma,kGammaI,kGammaGamma);
1181
1182 // perform symbolic on kII
1183 ExtensionSolver_ = SolverFactory<SC,LO,GO,NO>::Build(kII,
1184 sublist(this->ParameterList_,"ExtensionSolver"),
1185 string("ExtensionSolver (Level ") + to_string(this->LevelID_) + string(")"));
1186 ExtensionSolver_->initialize();
1187 }
1188 }
1189
1190 }
1191
1192 #endif