Debian Codegraph @

packages/shylu/shylu_dd/frosch/src/Tools/FROSch_ExtractSubmatrices_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_EXTRACTSUBMATRICES_DEF_HPP
11 #define _FROSCH_EXTRACTSUBMATRICES_DEF_HPP
12
13 #include <FROSch_ExtractSubmatrices_decl.hpp>
14
15 namespace FROSch {
16
17 using namespace std;
18 using namespace Teuchos;
19 using namespace Xpetra;
20
21 template <class SC,class LO,class GO,class NO>
22 RCP<const Matrix<SC,LO,GO,NO> > ExtractLocalSubdomainMatrix(RCP<const Matrix<SC,LO,GO,NO> > globalMatrix,
23 RCP<const Map<LO,GO,NO> > map)
24 {
25 FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime,"ExtractLocalSubdomainMatrix");
26 RCP<Matrix<SC,LO,GO,NO> > subdomainMatrix = MatrixFactory<SC,LO,GO,NO>::Build(map,globalMatrix->getGlobalMaxNumRowEntries());
27 RCP<Import<LO,GO,NO> > scatter = ImportFactory<LO,GO,NO>::Build(globalMatrix->getRowMap(),map);
28 subdomainMatrix->doImport(*globalMatrix,*scatter,ADD);
29 //cout << *subdomainMatrix << endl;
30 RCP<const Comm<LO> > SerialComm = rcp(new MpiComm<LO>(MPI_COMM_SELF));
31 RCP<Map<LO,GO,NO> > localSubdomainMap = MapFactory<LO,GO,NO>::Build(map->lib(),map->getLocalNumElements(),0,SerialComm);
32 RCP<Matrix<SC,LO,GO,NO> > localSubdomainMatrix = MatrixFactory<SC,LO,GO,NO>::Build(localSubdomainMap,globalMatrix->getGlobalMaxNumRowEntries());
33
34 for (unsigned i=0; i<localSubdomainMap->getLocalNumElements(); i++) {
35 ArrayView<const GO> indices;
36 ArrayView<const SC> values;
37 subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values);
38
39 LO size = indices.size();
40 if (size>0) {
41 Array<GO> indicesLocal;
42 Array<SC> valuesLocal;
43 for (LO j=0; j<size; j++) {
44 GO localIndex = map->getLocalElement(indices[j]);
45 if (localIndex>=0) {
46 indicesLocal.push_back(localIndex);
47 valuesLocal.push_back(values[j]);
48 }
49 }
50 localSubdomainMatrix->insertGlobalValues(i,indicesLocal(),valuesLocal());
51 }
52 }
53 localSubdomainMatrix->fillComplete();
54 return localSubdomainMatrix.getConst();
55 }
56
57 // this version just read indices without building submatrices, which is done in extractLocalSubdomainMatrix_Symbolic
58 template <class SC,class LO,class GO,class NO>
59 void ExtractLocalSubdomainMatrix_Symbolic(RCP<Matrix<SC,LO,GO,NO> > subdomainMatrix, // input : globalMatrix, re-distributed with map
60 RCP<Matrix<SC,LO,GO,NO> > localSubdomainMatrix) // output : local submatrix
61 {
62 FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime_symbolic, "ExtractLocalSubdomainMatrix_Symbolic");
63 auto subdomainMap = subdomainMatrix->getRowMap();
64
65 const SC zero = ScalarTraits<SC>::zero();
66 for (unsigned i=0; i<subdomainMap->getLocalNumElements(); i++) {
67 ArrayView<const GO> indices;
68 ArrayView<const SC> values;
69 subdomainMatrix->getGlobalRowView(subdomainMap->getGlobalElement(i),indices,values);
70
71 LO size = indices.size();
72 if (size>0) {
73 Array<GO> indicesLocal;
74 Array<SC> valuesLocal;
75 for (LO j=0; j<size; j++) {
76 LO localIndex = subdomainMap->getLocalElement(indices[j]);
77 if (localIndex>=0) {
78 indicesLocal.push_back(localIndex);
79 valuesLocal.push_back(zero);
80 }
81 }
82 localSubdomainMatrix->insertGlobalValues(i,indicesLocal(),valuesLocal());
83 }
84 }
85 localSubdomainMatrix->fillComplete();
86 return;
87 }
88
89 template <class SC,class LO,class GO,class NO>
90 void ExtractLocalSubdomainMatrix_Compute(RCP<const Matrix<SC,LO,GO,NO> > globalMatrix,
91 RCP< Matrix<SC,LO,GO,NO> > subdomainMatrix,
92 RCP< Matrix<SC,LO,GO,NO> > localSubdomainMatrix)
93 {
94 RCP<Import<LO,GO,NO> > scatter = ImportFactory<LO,GO,NO>::Build(globalMatrix->getRowMap(), subdomainMatrix->getRowMap());
95 ExtractLocalSubdomainMatrix_Compute(scatter, globalMatrix, subdomainMatrix, localSubdomainMatrix);
96 }
97
98 template <class SC,class LO,class GO,class NO>
99 void ExtractLocalSubdomainMatrix_Compute(RCP< Import<LO,GO,NO> > scatter,
100 RCP<const Matrix<SC,LO,GO,NO> > globalMatrix,
101 RCP< Matrix<SC,LO,GO,NO> > subdomainMatrix,
102 RCP< Matrix<SC,LO,GO,NO> > localSubdomainMatrix)
103 {
104 FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime_compute, "ExtractLocalSubdomainMatrix_Compute");
105 const SC zero = ScalarTraits<SC>::zero();
106 auto subdomainRowMap = subdomainMatrix->getRowMap();
107
108 subdomainMatrix->setAllToScalar(zero);
109 subdomainMatrix->resumeFill();
110 subdomainMatrix->doImport(*globalMatrix, *scatter, ADD);
111
112 // "fillComplete" is quite expensive, and it seem to be cheaper to replace values each row at a time
113 #if 0 //defined(HAVE_XPETRA_TPETRA)
114 if (globalMatrix->getRowMap()->lib() == UseTpetra)
115 {
116 // NOTE: this fillComplete is expensive on GPUs
117 subdomainMatrix->fillComplete();
118 auto devSubdomainMap = subdomainRowMap->getLocalMap();
119 auto devSubdomainMatrix = subdomainMatrix->getLocalMatrixDevice();
120 auto devLocalSubdomainMatrix = localSubdomainMatrix->getLocalMatrixDevice();
121
122 using UN = unsigned;
123 using execution_space = typename Map<LO,GO,NO>::local_map_type::execution_space;
124 UN numRows = subdomainRowMap->getLocalNumElements();
125 Kokkos::RangePolicy<execution_space> policy_row (0, numRows);
126 Kokkos::parallel_for(
127 "FROSch :: ExtractLocalSubdomainMatrix_Compute", policy_row,
128 KOKKOS_LAMBDA(const UN i) {
129 for (UN j=devSubdomainMatrix.graph.row_map(i); j<devSubdomainMatrix.graph.row_map(i+1); j++) {
130 GO globalIndex_j = devSubdomainMap.getGlobalElement(devSubdomainMatrix.graph.entries(j));
131 GO localIndex_j = devSubdomainMap.getLocalElement(globalIndex_j);
132 if (localIndex_j>=0) {
133 // look for the same column index in localSubdomainMatrix
134 for (UN k=devLocalSubdomainMatrix.graph.row_map(i); k<devLocalSubdomainMatrix.graph.row_map(i+1); k++) {
135 if (devLocalSubdomainMatrix.graph.entries(k) == localIndex_j) {
136 devLocalSubdomainMatrix.values[k] = devSubdomainMatrix.values[j];
137 break;
138 }
139 }
140 }
141 }
142 }
143 );
144 Kokkos::fence();
145 } else
146 #endif
147 {
148 localSubdomainMatrix->resumeFill();
149
150 size_t max_nnz = localSubdomainMatrix->getLocalMaxNumRowEntries();
151 std::vector<LO> local_cols_vector (max_nnz);
152 std::vector<SC> local_vals_vector (max_nnz);
153 for (unsigned i=0; i<subdomainRowMap->getLocalNumElements(); i++) {
154 ArrayView<const GO> global_indices;
155 ArrayView<const SC> global_values;
156 subdomainMatrix->getGlobalRowView(subdomainRowMap->getGlobalElement(i),global_indices,global_values);
157
158 LO size = global_indices.size();
159 if (size>0) {
160 // using "workspace" not to overwrite what's in localSubdomainMatrix
161 size_t new_nnz = 0;
162 ArrayView<LO> local_cols (local_cols_vector);
163 ArrayView<SC> local_vals (local_vals_vector);
164 for (LO j=0; j<size; j++) {
165 GO localIndex = subdomainRowMap->getLocalElement(global_indices[j]);
166 if (localIndex>=0) {
167 local_cols[new_nnz] = localIndex;
168 local_vals[new_nnz] = global_values[j];
169 new_nnz ++;
170 }
171 }
172 localSubdomainMatrix->replaceLocalValues(i, local_cols(0, new_nnz), local_vals(0, new_nnz));
173 }
174 }
175 RCP<ParameterList> fillCompleteParams(new ParameterList);
176 fillCompleteParams->set("No Nonlocal Changes", true);
177 localSubdomainMatrix->fillComplete(fillCompleteParams);
178 }
179
180 return;
181 }
182
183 template <class SC,class LO,class GO,class NO>
184 RCP<const Matrix<SC,LO,GO,NO> > ExtractLocalSubdomainMatrix(RCP<const Matrix<SC,LO,GO,NO> > globalMatrix,
185 RCP<const Map<LO,GO,NO> > map,
186 SC value)
187 {
188 FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime,"ExtractLocalSubdomainMatrix");
189 RCP<Matrix<SC,LO,GO,NO> > subdomainMatrix = MatrixFactory<SC,LO,GO,NO>::Build(map,2*globalMatrix->getGlobalMaxNumRowEntries());
190 RCP<Import<LO,GO,NO> > scatter = ImportFactory<LO,GO,NO>::Build(globalMatrix->getRowMap(),map);
191 subdomainMatrix->doImport(*globalMatrix,*scatter,ADD);
192 //cout << *subdomainMatrix << endl;
193 RCP<const Comm<LO> > SerialComm = rcp(new MpiComm<LO>(MPI_COMM_SELF));
194 RCP<Map<LO,GO,NO> > localSubdomainMap = MapFactory<LO,GO,NO>::Build(map->lib(),map->getLocalNumElements(),0,SerialComm);
195 RCP<Matrix<SC,LO,GO,NO> > localSubdomainMatrix = MatrixFactory<SC,LO,GO,NO>::Build(localSubdomainMap,globalMatrix->getGlobalMaxNumRowEntries());
196
197 for (unsigned i=0; i<localSubdomainMap->getLocalNumElements(); i++) {
198 ArrayView<const GO> indices;
199 ArrayView<const SC> values;
200 subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values);
201
202 LO size = indices.size();
203 if (size>0) {
204 Array<GO> indicesGlobal;
205 Array<SC> valuesLocal;
206 for (LO j=0; j<size; j++) {
207 GO localIndex = map->getLocalElement(indices[j]);
208 if (localIndex>=0) {
209 indicesGlobal.push_back(localIndex);
210 valuesLocal.push_back(value);
211 }
212 }
213 localSubdomainMatrix->insertGlobalValues(i,indicesGlobal(),valuesLocal());
214 }
215 }
216 localSubdomainMatrix->fillComplete();
217 return localSubdomainMatrix.getConst();
218 }
219
220 template <class SC,class LO,class GO,class NO>
221 int UpdateLocalSubdomainMatrix(RCP<Matrix<SC,LO,GO,NO> > globalMatrix,
222 RCP<Map<LO,GO,NO> > &map,
223 RCP<Matrix<SC,LO,GO,NO> > &localSubdomainMatrix)
224 {
225 FROSCH_DETAILTIMER_START(updateLocalSubdomainMatrixTime,"UpdateLocalSubdomainMatrix");
226 RCP<Matrix<SC,LO,GO,NO> > subdomainMatrix = MatrixFactory<SC,LO,GO,NO>::Build(map,2*globalMatrix->getGlobalMaxNumRowEntries());
227 RCP<Import<LO,GO,NO> > scatter = ImportFactory<LO,GO,NO>::Build(globalMatrix->getRowMap(),map);
228 subdomainMatrix->doImport(*globalMatrix,*scatter,ADD);
229
230 localSubdomainMatrix->resumeFill();
231 for (unsigned i=0; i<map->getLocalNumElements(); i++) {
232 ArrayView<const GO> indices;
233 ArrayView<const SC> values;
234 subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values);
235
236 LO size = indices.size();
237 if (size>0) {
238 Array<LO> indicesLocal;
239 Array<SC> valuesLocal;
240 for (LO j=0; j<size; j++) {
241 GO localIndex = map->getLocalElement(indices[j]);
242 if (localIndex>=0) {
243 indicesLocal.push_back(localIndex);
244 valuesLocal.push_back(values[j]);
245 }
246 }
247 localSubdomainMatrix->replaceLocalValues(i,indicesLocal(),valuesLocal());
248 }
249 }
250 localSubdomainMatrix->fillComplete();
251
252 return 0;
253 }
254
255 template <class SC,class LO,class GO,class NO>
256 int BuildSubmatrices(RCP<const Matrix<SC,LO,GO,NO> > k,
257 ArrayView<GO> indI,
258 RCP<Matrix<SC,LO,GO,NO> > &kII,
259 RCP<Matrix<SC,LO,GO,NO> > &kIJ,
260 RCP<Matrix<SC,LO,GO,NO> > &kJI,
261 RCP<Matrix<SC,LO,GO,NO> > &kJJ)
262 {
263 FROSCH_DETAILTIMER_START(buildSubmatricesTime,"BuildSubmatrices");
264 // We need four Maps
265 const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid();
266 RCP<Map<LO,GO,NO> > mapI = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm());
267 RCP<Map<LO,GO,NO> > mapILocal = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indI.size(),0,k->getRowMap()->getComm());
268
269 Array<GO> indJ;
270 for (unsigned i=0; i<k->getLocalNumRows(); i++) {
271 if (mapI->getLocalElement(i)<0) {
272 indJ.push_back(i);
273 }
274 }
275
276 RCP<Map<LO,GO,NO> > mapJ = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indJ(),0,k->getRowMap()->getComm());
277 RCP<Map<LO,GO,NO> > mapJLocal = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indJ.size(),0,k->getRowMap()->getComm());
278 RCP<const Map<LO,GO,NO> > colMap = k->getColMap();
279 #if defined(HAVE_XPETRA_TPETRA)
280 if (k->getRowMap()->lib() == UseTpetra)
281 {
282 using crsmat_type = typename Matrix<SC,LO,GO,NO>::local_matrix_type;
283 using graph_type = typename crsmat_type::StaticCrsGraphType;
284 using rowptr_type = typename graph_type::row_map_type::non_const_type;
285 using indices_type = typename graph_type::entries_type::non_const_type;
286 using values_type = typename crsmat_type::values_type::non_const_type;
287 using execution_space = typename Map<LO,GO,NO>::local_map_type::execution_space;
288
289 using UN = unsigned;
290 UN numRows = k->getLocalNumRows();
291 UN numRowsI = indI.size();
292 UN numRowsJ = indJ.size();
293
294 rowptr_type RowptrII (Kokkos::ViewAllocateWithoutInitializing("RowptrII"), numRowsI+1);
295 rowptr_type RowptrIJ (Kokkos::ViewAllocateWithoutInitializing("RowptrIJ"), numRowsI+1);
296 rowptr_type RowptrJI (Kokkos::ViewAllocateWithoutInitializing("RowptrJI"), numRowsJ+1);
297 rowptr_type RowptrJJ (Kokkos::ViewAllocateWithoutInitializing("RowptrJJ"), numRowsJ+1);
298
299 // count nnz on each blocks
300 Kokkos::deep_copy(RowptrII, 0);
301 Kokkos::deep_copy(RowptrIJ, 0);
302 Kokkos::deep_copy(RowptrJI, 0);
303 Kokkos::deep_copy(RowptrJJ, 0);
304
305 auto localMapI = mapI->getLocalMap();
306 auto localMapJ = mapJ->getLocalMap();
307 auto localColMap = colMap->getLocalMap();
308 auto localK = k->getLocalMatrixDevice();
309 Kokkos::RangePolicy<execution_space> policy_row (0, numRows);
310 Kokkos::parallel_for(
311 "FROSch_BuildSubmatrices::countNnz", policy_row,
312 KOKKOS_LAMBDA(const UN i) {
313 LO tmp1=localMapI.getLocalElement(i);
314 LO tmp2=0;
315 if (tmp1>=0) {
316 for (UN j=localK.graph.row_map(i); j<localK.graph.row_map(i+1); j++) {
317 tmp2 = localMapI.getLocalElement(localColMap.getGlobalElement(localK.graph.entries(j)));
318 if (tmp2>=0) {
319 RowptrII(tmp1+1) ++;
320 } else {
321 RowptrIJ(tmp1+1) ++;
322 }
323 }
324 } else {
325 tmp1=localMapJ.getLocalElement((GO) i);
326 for (UN j=localK.graph.row_map(i); j<localK.graph.row_map(i+1); j++) {
327 tmp2 = localMapI.getLocalElement(localColMap.getGlobalElement(localK.graph.entries(j)));
328 if (tmp2>=0) {
329 RowptrJI(tmp1+1) ++;
330 } else {
331 RowptrJJ(tmp1+1) ++;
332 }
333 }
334 }
335 }
336 );
337 Kokkos::fence();
338
339 // cout nnz
340 UN nnzII = 0;
341 UN nnzIJ = 0;
342 UN nnzJI = 0;
343 UN nnzJJ = 0;
344 Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzII", 1+numRowsI,
345 KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrII[i]; },
346 nnzII);
347 Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzIJ", 1+numRowsI,
348 KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrIJ[i]; },
349 nnzIJ);
350 Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzJI", 1+numRowsJ,
351 KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrJI[i]; },
352 nnzJI);
353 Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzJJ", 1+numRowsJ,
354 KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrJJ[i]; },
355 nnzJJ);
356
357 // make it into offsets
358 #if KOKKOSKERNELS_VERSION >= 40199
359 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<execution_space>
360 (1+numRowsI, RowptrII);
361 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<execution_space>
362 (1+numRowsI, RowptrIJ);
363 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<execution_space>
364 (1+numRowsJ, RowptrJI);
365 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<execution_space>
366 (1+numRowsJ, RowptrJJ);
367 #else
368 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<rowptr_type, execution_space>
369 (1+numRowsI, RowptrII);
370 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<rowptr_type, execution_space>
371 (1+numRowsI, RowptrIJ);
372 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<rowptr_type, execution_space>
373 (1+numRowsJ, RowptrJI);
374 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<rowptr_type, execution_space>
375 (1+numRowsJ, RowptrJJ);
376 #endif
377
378 // allocate kII block
379 indices_type IndicesII (Kokkos::ViewAllocateWithoutInitializing("IndicesII"), nnzII);
380 values_type ValuesII (Kokkos::ViewAllocateWithoutInitializing("ValuesII"), nnzII);
381 // allocate kIJ block
382 indices_type IndicesIJ (Kokkos::ViewAllocateWithoutInitializing("IndicesIJ"), nnzIJ);
383 values_type ValuesIJ (Kokkos::ViewAllocateWithoutInitializing("ValuesIJ"), nnzIJ);
384 // allocate kJI block
385 indices_type IndicesJI (Kokkos::ViewAllocateWithoutInitializing("IndicesJI"), nnzJI);
386 values_type ValuesJI (Kokkos::ViewAllocateWithoutInitializing("ValuesJI"), nnzJI);
387 // allocate kJJ block
388 indices_type IndicesJJ (Kokkos::ViewAllocateWithoutInitializing("IndicesJJ"), nnzJJ);
389 values_type ValuesJJ (Kokkos::ViewAllocateWithoutInitializing("ValuesJJ"), nnzJJ);
390
391 // fill in all the blocks
392 Kokkos::parallel_for(
393 "BuildSubmatrices::countNnz", policy_row,
394 KOKKOS_LAMBDA(const UN i) {
395 LO tmp1=localMapI.getLocalElement((GO) i);
396 LO tmp2=0;
397 if (tmp1>=0) {
398 UN nnz_i = RowptrII[tmp1];
399 UN nnz_j = RowptrIJ[tmp1];
400 for (UN j=localK.graph.row_map(i); j<localK.graph.row_map(i+1); j++) {
401 LO colid = localColMap.getGlobalElement(localK.graph.entries(j));
402 tmp2 = localMapI.getLocalElement(colid);
403 if (tmp2>=0) {
404 IndicesII(nnz_i) = tmp2;
405 ValuesII(nnz_i) = localK.values[j];
406 nnz_i++;
407 } else {
408 tmp2 = localMapJ.getLocalElement(colid);
409 IndicesIJ(nnz_j) = tmp2;
410 ValuesIJ(nnz_j) = localK.values[j];
411 nnz_j++;
412 }
413 }
414 } else {
415 tmp1=localMapJ.getLocalElement((GO) i);
416 UN nnz_i = RowptrJI[tmp1];
417 UN nnz_j = RowptrJJ[tmp1];
418 for (UN j=localK.graph.row_map(i); j<localK.graph.row_map(i+1); j++) {
419 LO colid = localColMap.getGlobalElement(localK.graph.entries(j));
420 tmp2 = localMapI.getLocalElement(colid);
421 if (tmp2>=0) {
422 IndicesJI(nnz_i) = tmp2;
423 ValuesJI(nnz_i) = localK.values[j];
424 nnz_i++;
425 } else {
426 tmp2 = localMapJ.getLocalElement(colid);
427 IndicesJJ(nnz_j) = tmp2;
428 ValuesJJ(nnz_j) = localK.values[j];
429 nnz_j++;
430 }
431 }
432 }
433 }
434 );
435 Kokkos::fence();
436
437 Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::rcp (new Teuchos::ParameterList());
438 params->set("sorted", false);
439
440 // create kII
441 graph_type crsgraphII (IndicesII, RowptrII);
442 crsmat_type LocalII = crsmat_type ("CrsMatrix", numRowsI, ValuesII, crsgraphII);
443 kII = MatrixFactory<SC,LO,GO,NO>::Build(LocalII, mapILocal, mapILocal, mapILocal, mapILocal,
444 params);
445 // create kIJ
446 graph_type crsgraphIJ (IndicesIJ, RowptrIJ);
447 crsmat_type LocalIJ = crsmat_type ("CrsMatrix", numRowsI, ValuesIJ, crsgraphIJ);
448 kIJ = MatrixFactory<SC,LO,GO,NO>::Build(LocalIJ, mapILocal, mapJLocal, mapJLocal, mapILocal,
449 params);
450 // create kJI
451 graph_type crsgraphJI (IndicesJI, RowptrJI);
452 crsmat_type LocalJI = crsmat_type ("CrsMatrix", numRowsJ, ValuesJI, crsgraphJI);
453 kJI = MatrixFactory<SC,LO,GO,NO>::Build(LocalJI, mapJLocal, mapILocal, mapILocal, mapJLocal,
454 params);
455 // create kJJ
456 graph_type crsgraphJJ (IndicesJJ, RowptrJJ);
457 crsmat_type LocalJJ = crsmat_type ("CrsMatrix", numRowsJ, ValuesJJ, crsgraphJJ);
458 kJJ = MatrixFactory<SC,LO,GO,NO>::Build(LocalJJ, mapJLocal, mapJLocal, mapJLocal, mapJLocal,
459 params);
460 } else
461 #endif
462 {
463 kII = MatrixFactory<SC,LO,GO,NO>::Build(mapILocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size()));
464 kIJ = MatrixFactory<SC,LO,GO,NO>::Build(mapILocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indJ.size()));
465 kJI = MatrixFactory<SC,LO,GO,NO>::Build(mapJLocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size()));
466 kJJ = MatrixFactory<SC,LO,GO,NO>::Build(mapJLocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indJ.size()));
467
468 for (unsigned i=0; i<k->getLocalNumRows(); i++) {
469 ArrayView<const LO> indices;
470 ArrayView<const SC> values;
471
472 k->getLocalRowView(i,indices,values);
473 //cout << numEntries << endl;
474 Array<GO> indicesI;
475 Array<SC> valuesI;
476 Array<GO> indicesJ;
477 Array<SC> valuesJ;
478 LO tmp1=mapI->getLocalElement(i);
479 LO tmp2=0;
480 if (tmp1>=0) {
481 for (LO j=0; j<indices.size(); j++) {
482 tmp2 = mapI->getLocalElement(colMap->getGlobalElement(indices[j]));
483 if (tmp2>=0) {
484 indicesI.push_back(tmp2);
485 valuesI.push_back(values[j]);
486 } else {
487 indicesJ.push_back(mapJ->getLocalElement(colMap->getGlobalElement(indices[j])));
488 valuesJ.push_back(values[j]);
489 }
490 }
491 kII->insertGlobalValues(tmp1,indicesI(),valuesI());
492 kIJ->insertGlobalValues(tmp1,indicesJ(),valuesJ());
493 } else {
494 tmp1=mapJ->getLocalElement((GO) i);
495 for (LO j=0; j<indices.size(); j++) {
496 tmp2 = mapI->getLocalElement(colMap->getGlobalElement(indices[j]));
497 if (tmp2>=0) {
498 indicesI.push_back(tmp2);
499 valuesI.push_back(values[j]);
500 } else {
501 indicesJ.push_back(mapJ->getLocalElement(colMap->getGlobalElement(indices[j])));
502 valuesJ.push_back(values[j]);
503 }
504 }
505 kJI->insertGlobalValues(tmp1,indicesI(),valuesI());
506 kJJ->insertGlobalValues(tmp1,indicesJ(),valuesJ());
507 }
508 }
509
510 kII->fillComplete(mapILocal,mapILocal);
511 kIJ->fillComplete(mapJLocal,mapILocal);
512 kJI->fillComplete(mapILocal,mapJLocal);
513 kJJ->fillComplete(mapJLocal,mapJLocal);
514 }
515
516 return 0;
517 }
518
519
520 template <class SC,class LO,class GO,class NO>
521 int BuildSubmatrix(RCP<Matrix<SC,LO,GO,NO> > k,
522 ArrayView<GO> indI,
523 RCP<Matrix<SC,LO,GO,NO> > &kII)
524 {
525 FROSCH_DETAILTIMER_START(buildSubmatrixTime,"BuildSubmatrix");
526 const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid();
527 //RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout));
528 RCP<Map<LO,GO,NO> > mapI = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm());
529
530 kII = MatrixFactory<SC,LO,GO,NO>::Build(mapI,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size()));
531 GO maxGID = mapI->getMaxAllGlobalIndex();
532 GO minGID = mapI->getMinAllGlobalIndex();
533 for (unsigned i=0; i<k->getLocalNumRows(); i++) {
534 ArrayView<const LO> indices;
535 ArrayView<const SC> values;
536
537 k->getLocalRowView(i,indices,values);
538
539 Array<GO> indicesI;
540 Array<SC> valuesI;
541
542 LO tmp1=mapI->getLocalElement(k->getRowMap()->getGlobalElement(i));
543 GO tmp2=0;
544 if (tmp1>=0) {
545 for (LO j=0; j<indices.size(); j++) {
546 tmp2 = k->getColMap()->getGlobalElement(indices[j]);
547 if (minGID<=tmp2 && tmp2<=maxGID) {
548 indicesI.push_back(tmp2);
549 valuesI.push_back(values[j]);
550 }
551 }
552 kII->insertGlobalValues(mapI->getGlobalElement(tmp1),indicesI(),valuesI());
553 }
554 }
555 kII->fillComplete(mapI,mapI);
556
557 return 0;
558 }
559
560 template <class LO,class GO,class NO>
561 int BuildSubgraph(RCP<CrsGraph<LO,GO,NO> > k,
562 ArrayView<GO> indI,
563 RCP<CrsGraph<LO,GO,NO> > &kII)
564 {
565 FROSCH_DETAILTIMER_START(buildSubgraphTime,"BuildSubgraph");
566 const GO INVALID = Teuchos::OrdinalTraits<GO>::invalid();
567 //RCP<FancyOStream> fancy = fancyOStream(rcpFromRef(cout));
568 RCP<Map<LO,GO,NO> > mapI = MapFactory<LO,GO,NO>::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm());
569
570 kII = CrsGraphFactory<LO,GO,NO>::Build(mapI,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size()));
571 GO maxGID = mapI->getMaxAllGlobalIndex();
572 GO minGID = mapI->getMinAllGlobalIndex();
573 for (unsigned i=0; i<k->getLocalNumRows(); i++) {
574 ArrayView<const LO> indices;
575
576 k->getLocalRowView(i,indices);
577
578 Array<GO> indicesI;
579
580 LO tmp1=mapI->getLocalElement(k->getRowMap()->getGlobalElement(i));
581 GO tmp2=0;
582 if (tmp1>=0) {
583 for (LO j=0; j<indices.size(); j++) {
584 tmp2 = k->getColMap()->getGlobalElement(indices[j]);
585 if (minGID<=tmp2 && tmp2<=maxGID) {
586 indicesI.push_back(tmp2);
587 }
588 }
589 kII->insertGlobalValues(mapI->getGlobalElement(tmp1),indicesI());
590 }
591 }
592 kII->fillComplete(mapI,mapI);
593
594 return 0;
595 }
596 }
597
598 #endif