Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_BlockCrsMatrix_Helpers_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_BLOCKCRSMATRIX_HELPERS_DEF_HPP
11 #define TPETRA_BLOCKCRSMATRIX_HELPERS_DEF_HPP
12 
14 
15 #include "Tpetra_BlockCrsMatrix.hpp"
16 #include "Tpetra_CrsMatrix.hpp"
17 #include "Tpetra_HashTable.hpp"
18 #include "Tpetra_Import.hpp"
19 #include "Tpetra_Map.hpp"
20 #include "Tpetra_MultiVector.hpp"
21 #include "Teuchos_ParameterList.hpp"
22 #include "Teuchos_ScalarTraits.hpp"
23 #include <ctime>
24 #include <fstream>
25 
26 namespace Tpetra {
27 
28  template<class Scalar, class LO, class GO, class Node>
29  void blockCrsMatrixWriter(BlockCrsMatrix<Scalar,LO,GO,Node> const &A, std::string const &fileName) {
30  Teuchos::ParameterList pl;
31  std::ofstream out;
32  out.open(fileName.c_str());
33  blockCrsMatrixWriter(A, out, pl);
34  }
35 
36  template<class Scalar, class LO, class GO, class Node>
37  void blockCrsMatrixWriter(BlockCrsMatrix<Scalar,LO,GO,Node> const &A, std::string const &fileName, Teuchos::ParameterList const &params) {
38  std::ofstream out;
39  out.open(fileName.c_str());
40  blockCrsMatrixWriter(A, out, params);
41  }
42 
43  template<class Scalar, class LO, class GO, class Node>
44  void blockCrsMatrixWriter(BlockCrsMatrix<Scalar,LO,GO,Node> const &A, std::ostream &os) {
45  Teuchos::ParameterList pl;
46  blockCrsMatrixWriter(A, os, pl);
47  }
48 
49  template<class Scalar, class LO, class GO, class Node>
50  void blockCrsMatrixWriter(BlockCrsMatrix<Scalar,LO,GO,Node> const &A, std::ostream &os, Teuchos::ParameterList const &params) {
51 
52  using Teuchos::RCP;
53  using Teuchos::rcp;
54 
55  typedef Teuchos::OrdinalTraits<GO> TOT;
56  typedef BlockCrsMatrix<Scalar, LO, GO, Node> block_crs_matrix_type;
57  typedef Tpetra::Import<LO, GO, Node> import_type;
58  typedef Tpetra::Map<LO, GO, Node> map_type;
60  typedef Tpetra::CrsGraph<LO, GO, Node> crs_graph_type;
61 
62  RCP<const map_type> const rowMap = A.getRowMap(); //"mesh" map
63  RCP<const Teuchos::Comm<int> > comm = rowMap->getComm();
64  const int myRank = comm->getRank();
65  const size_t numProcs = comm->getSize();
66 
67  // If true, force use of the import strip-mining infrastructure. This is useful for debugging on one process.
68  bool alwaysUseParallelAlgorithm = false;
69  if (params.isParameter("always use parallel algorithm"))
70  alwaysUseParallelAlgorithm = params.get<bool>("always use parallel algorithm");
71  bool printMatrixMarketHeader = true;
72  if (params.isParameter("print MatrixMarket header"))
73  printMatrixMarketHeader = params.get<bool>("print MatrixMarket header");
74 
75  if (printMatrixMarketHeader && myRank==0) {
76  std::time_t now = std::time(NULL);
77 
78  const std::string dataTypeStr =
79  Teuchos::ScalarTraits<Scalar>::isComplex ? "complex" : "real";
80 
81  // Explanation of first line of file:
82  // - "%%MatrixMarket" is the tag for Matrix Market format.
83  // - "matrix" is what we're printing.
84  // - "coordinate" means sparse (triplet format), rather than dense.
85  // - "real" / "complex" is the type (in an output format sense,
86  // not in a C++ sense) of each value of the matrix. (The
87  // other two possibilities are "integer" (not really necessary
88  // here) and "pattern" (no values, just graph).)
89  os << "%%MatrixMarket matrix coordinate " << dataTypeStr << " general" << std::endl;
90  os << "% time stamp: " << ctime(&now);
91  os << "% written from " << numProcs << " processes" << std::endl;
92  os << "% point representation of Tpetra::BlockCrsMatrix" << std::endl;
93  size_t numRows = A.getGlobalNumRows();
94  size_t numCols = A.getGlobalNumCols();
95  os << "% " << numRows << " block rows, " << numCols << " block columns" << std::endl;
96  const LO blockSize = A.getBlockSize();
97  os << "% block size " << blockSize << std::endl;
98  os << numRows*blockSize << " " << numCols*blockSize << " " << A.getGlobalNumEntries()*blockSize*blockSize << std::endl;
99  }
100 
101  if (numProcs==1 && !alwaysUseParallelAlgorithm) {
102  writeMatrixStrip(A,os,params);
103  } else {
104  size_t numRows = rowMap->getLocalNumElements();
105 
106  //Create source map
107  RCP<const map_type> allMeshGidsMap = rcp(new map_type(TOT::invalid(), numRows, A.getIndexBase(), comm));
108  //Create and populate vector of mesh GIDs corresponding to this pid's rows.
109  //This vector will be imported one pid's worth of information at a time to pid 0.
110  mv_type allMeshGids(allMeshGidsMap,1);
111  Teuchos::ArrayRCP<GO> allMeshGidsData = allMeshGids.getDataNonConst(0);
112 
113  for (size_t i=0; i<numRows; i++)
114  allMeshGidsData[i] = rowMap->getGlobalElement(i);
115  allMeshGidsData = Teuchos::null;
116 
117  // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A.
118  size_t stripSize = allMeshGids.getGlobalLength() / numProcs;
119  size_t remainder = allMeshGids.getGlobalLength() % numProcs;
120  size_t curStart = 0;
121  size_t curStripSize = 0;
122  Teuchos::Array<GO> importMeshGidList;
123  for (size_t i=0; i<numProcs; i++) {
124  if (myRank==0) { // Only PE 0 does this part
125  curStripSize = stripSize;
126  if (i<remainder) curStripSize++; // handle leftovers
127  importMeshGidList.resize(curStripSize); // Set size of vector to max needed
128  for (size_t j=0; j<curStripSize; j++) importMeshGidList[j] = j + curStart + A.getIndexBase();
129  curStart += curStripSize;
130  }
131  // The following import map should be non-trivial only on PE 0.
132  TEUCHOS_TEST_FOR_EXCEPTION(myRank>0 && curStripSize!=0,
133  std::runtime_error, "Tpetra::blockCrsMatrixWriter: (pid "
134  << myRank << ") map size should be zero, but is " << curStripSize);
135  RCP<map_type> importMeshGidMap = rcp(new map_type(TOT::invalid(), importMeshGidList(), A.getIndexBase(), comm));
136  import_type gidImporter(allMeshGidsMap, importMeshGidMap);
137  mv_type importMeshGids(importMeshGidMap, 1);
138  importMeshGids.doImport(allMeshGids, gidImporter, INSERT);
139 
140  // importMeshGids now has a list of GIDs for the current strip of matrix rows.
141  // Use these values to build another importer that will get rows of the matrix.
142 
143  // The following import map will be non-trivial only on PE 0.
144  Teuchos::ArrayRCP<const GO> importMeshGidsData = importMeshGids.getData(0);
145  Teuchos::Array<GO> importMeshGidsGO;
146  importMeshGidsGO.reserve(importMeshGidsData.size());
147  for (typename Teuchos::ArrayRCP<const GO>::size_type j=0; j<importMeshGidsData.size(); ++j)
148  importMeshGidsGO.push_back(importMeshGidsData[j]);
149  RCP<const map_type> importMap = rcp(new map_type(TOT::invalid(), importMeshGidsGO(), rowMap->getIndexBase(), comm) );
150 
151  import_type importer(rowMap,importMap );
152  size_t numEntriesPerRow = A.getCrsGraph().getGlobalMaxNumRowEntries();
153  RCP<crs_graph_type> graph = createCrsGraph(importMap,numEntriesPerRow);
154  RCP<const map_type> domainMap = A.getCrsGraph().getDomainMap();
155  graph->doImport(A.getCrsGraph(), importer, INSERT);
156  graph->fillComplete(domainMap, importMap);
157 
158  block_crs_matrix_type importA(*graph, A.getBlockSize());
159  importA.doImport(A, importer, INSERT);
160 
161  // Finally we are ready to write this strip of the matrix
162  writeMatrixStrip(importA, os, params);
163  }
164  }
165  }
166 
167  template<class Scalar, class LO, class GO, class Node>
168  void writeMatrixStrip(BlockCrsMatrix<Scalar,LO,GO,Node> const &A, std::ostream &os, Teuchos::ParameterList const &params) {
169  using Teuchos::RCP;
170  using map_type = Tpetra::Map<LO, GO, Node>;
171  using bcrs_type = BlockCrsMatrix<Scalar,LO,GO,Node>;
172  using bcrs_local_inds_host_view_type = typename bcrs_type::local_inds_host_view_type;
173  using bcrs_values_host_view_type = typename bcrs_type::values_host_view_type;
174  using impl_scalar_type = typename bcrs_type::impl_scalar_type;
175 
176  size_t numRows = A.getGlobalNumRows();
177  RCP<const map_type> rowMap = A.getRowMap();
178  RCP<const map_type> colMap = A.getColMap();
179  RCP<const Teuchos::Comm<int> > comm = rowMap->getComm();
180  const int myRank = comm->getRank();
181 
182  const size_t meshRowOffset = rowMap->getIndexBase();
183  const size_t meshColOffset = colMap->getIndexBase();
184  TEUCHOS_TEST_FOR_EXCEPTION(meshRowOffset != meshColOffset,
185  std::runtime_error, "Tpetra::writeMatrixStrip: "
186  "mesh row index base != mesh column index base");
187 
188  if (myRank !=0) {
189 
190  TEUCHOS_TEST_FOR_EXCEPTION(A.getLocalNumRows() != 0,
191  std::runtime_error, "Tpetra::writeMatrixStrip: pid "
192  << myRank << " should have 0 rows but has " << A.getLocalNumRows());
193  TEUCHOS_TEST_FOR_EXCEPTION(A.getLocalNumCols() != 0,
194  std::runtime_error, "Tpetra::writeMatrixStrip: pid "
195  << myRank << " should have 0 columns but has " << A.getLocalNumCols());
196 
197  } else {
198 
199  TEUCHOS_TEST_FOR_EXCEPTION(numRows != A.getLocalNumRows(),
200  std::runtime_error, "Tpetra::writeMatrixStrip: "
201  "number of rows on pid 0 does not match global number of rows");
202 
203 
204  int err = 0;
205  const LO blockSize = A.getBlockSize();
206  const size_t numLocalRows = A.getLocalNumRows();
207  bool precisionChanged=false;
208  int oldPrecision = 0; // avoid "unused variable" warning
209  if (params.isParameter("precision")) {
210  oldPrecision = os.precision(params.get<int>("precision"));
211  precisionChanged=true;
212  }
213  int pointOffset = 1;
214  if (params.isParameter("zero-based indexing")) {
215  if (params.get<bool>("zero-based indexing") == true)
216  pointOffset = 0;
217  }
218 
219  size_t localRowInd;
220  for (localRowInd = 0; localRowInd < numLocalRows; ++localRowInd) {
221 
222  // Get a view of the current row.
223  bcrs_local_inds_host_view_type localColInds;
224  bcrs_values_host_view_type vals;
225  LO numEntries;
226  A.getLocalRowView (localRowInd, localColInds, vals); numEntries = localColInds.extent(0);
227  GO globalMeshRowID = rowMap->getGlobalElement(localRowInd) - meshRowOffset;
228 
229  for (LO k = 0; k < numEntries; ++k) {
230  GO globalMeshColID = colMap->getGlobalElement(localColInds[k]) - meshColOffset;
231  const impl_scalar_type* curBlock = vals.data() + blockSize * blockSize * k;
232  // Blocks are stored in row-major format.
233  for (LO j = 0; j < blockSize; ++j) {
234  GO globalPointRowID = globalMeshRowID * blockSize + j + pointOffset;
235  for (LO i = 0; i < blockSize; ++i) {
236  GO globalPointColID = globalMeshColID * blockSize + i + pointOffset;
237  const impl_scalar_type curVal = curBlock[i + j * blockSize];
238 
239  os << globalPointRowID << " " << globalPointColID << " ";
240  if (Teuchos::ScalarTraits<impl_scalar_type>::isComplex) {
241  // Matrix Market format wants complex values to be
242  // written as space-delimited pairs. See Bug 6469.
243  os << Teuchos::ScalarTraits<impl_scalar_type>::real (curVal) << " "
244  << Teuchos::ScalarTraits<impl_scalar_type>::imag (curVal);
245  }
246  else {
247  os << curVal;
248  }
249  os << std::endl;
250  }
251  }
252  }
253  }
254  if (precisionChanged)
255  os.precision(oldPrecision);
256  TEUCHOS_TEST_FOR_EXCEPTION(err != 0,
257  std::runtime_error, "Tpetra::writeMatrixStrip: "
258  "error getting view of local row " << localRowInd);
259 
260  }
261 
262  }
263 
264  template<class Scalar, class LO, class GO, class Node>
265  Teuchos::RCP<Tpetra::CrsGraph<LO, GO, Node> >
266  getBlockCrsGraph(const Tpetra::CrsMatrix<Scalar, LO, GO, Node>& pointMatrix, const LO &blockSize, bool use_LID)
267  {
268  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::getBlockCrsGraph", getBlockCrsGraph0);
269  /*
270  ASSUMPTIONS:
271 
272  1) In point matrix, all entries associated with a little block are present (even if they are zero).
273  2) For given mesh DOF, point DOFs appear consecutively and in ascending order in row & column maps.
274  3) Point column map and block column map are ordered consistently.
275  */
276 
277  using Teuchos::Array;
278  using Teuchos::ArrayView;
279  using Teuchos::RCP;
280 
281  typedef Tpetra::Map<LO,GO,Node> map_type;
282  typedef Tpetra::CrsGraph<LO,GO,Node> crs_graph_type;
283  typedef Tpetra::CrsMatrix<Scalar, LO,GO,Node> crs_matrix_type;
284 
285  using local_graph_device_type = typename crs_matrix_type::local_graph_device_type;
286  using row_map_type = typename local_graph_device_type::row_map_type::non_const_type;
287  using entries_type = typename local_graph_device_type::entries_type::non_const_type;
288 
289  using offset_type = typename row_map_type::non_const_value_type;
290 
291  using execution_space = typename Node::execution_space;
292  using range_type = Kokkos::RangePolicy<execution_space, LO>;
293 
294  const map_type &pointRowMap = *(pointMatrix.getRowMap());
295  RCP<const map_type> meshRowMap, meshColMap, meshDomainMap, meshRangeMap;
296 
297  const map_type &pointColMap = *(pointMatrix.getColMap());
298  const map_type &pointDomainMap = *(pointMatrix.getDomainMap());
299  const map_type &pointRangeMap = *(pointMatrix.getRangeMap());
300 
301  {
302  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::getBlockCrsGraph::createMeshMaps", getBlockCrsGraph1);
303  meshRowMap = createMeshMap<LO,GO,Node>(blockSize, pointRowMap, use_LID);
304  meshColMap = createMeshMap<LO,GO,Node>(blockSize, pointColMap, use_LID);
305  meshDomainMap = createMeshMap<LO,GO,Node>(blockSize, pointDomainMap, use_LID);
306  meshRangeMap = createMeshMap<LO,GO,Node>(blockSize, pointRangeMap, use_LID);
307  Kokkos::DefaultExecutionSpace().fence();
308  }
309 
310  if(meshColMap.is_null()) throw std::runtime_error("ERROR: Cannot create mesh colmap");
311 
312  auto localMeshColMap = meshColMap->getLocalMap();
313  auto localPointColMap = pointColMap.getLocalMap();
314  auto localPointRowMap = pointRowMap.getLocalMap();
315 
316  RCP<crs_graph_type> meshCrsGraph;
317 
318  const offset_type bs2 = blockSize * blockSize;
319 
320  if (use_LID) {
321  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::getBlockCrsGraph::LID", getBlockCrsGraph2);
322  auto pointLocalGraph = pointMatrix.getCrsGraph()->getLocalGraphDevice();
323  auto pointRowptr = pointLocalGraph.row_map;
324  auto pointColind = pointLocalGraph.entries;
325 
326  TEUCHOS_TEST_FOR_EXCEPTION(pointColind.extent(0) % bs2 != 0,
327  std::runtime_error, "Tpetra::getBlockCrsGraph: "
328  "local number of non zero entries is not a multiple of blockSize^2 ");
329 
330  LO block_rows = pointRowptr.extent(0) == 0 ? 0 : (pointRowptr.extent(0)-1)/blockSize;
331  row_map_type blockRowptr("blockRowptr", block_rows+1);
332  entries_type blockColind("blockColind", pointColind.extent(0)/(bs2));
333 
334  Kokkos::parallel_for("fillMesh",range_type(0,block_rows), KOKKOS_LAMBDA(const LO i) {
335 
336  const LO offset_b = pointRowptr(i*blockSize)/bs2;
337  const LO offset_b_max = pointRowptr((i+1)*blockSize)/bs2;
338 
339  if (i==block_rows-1)
340  blockRowptr(i+1) = offset_b_max;
341  blockRowptr(i) = offset_b;
342 
343  const LO offset_p = pointRowptr(i*blockSize);
344 
345  for (LO k=0; k<offset_b_max-offset_b; ++k) {
346  blockColind(offset_b + k) = pointColind(offset_p + k * blockSize)/blockSize;
347  }
348  });
349 
350  meshCrsGraph = rcp(new crs_graph_type(meshRowMap, meshColMap, blockRowptr, blockColind));
351  meshCrsGraph->fillComplete(meshDomainMap,meshRangeMap);
352  Kokkos::DefaultExecutionSpace().fence();
353  }
354  else {
355  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::getBlockCrsGraph::GID", getBlockCrsGraph3);
356  auto pointLocalGraph = pointMatrix.getCrsGraph()->getLocalGraphDevice();
357  auto pointRowptr = pointLocalGraph.row_map;
358  auto pointColind = pointLocalGraph.entries;
359 
360  TEUCHOS_TEST_FOR_EXCEPTION(pointColind.extent(0) % bs2 != 0,
361  std::runtime_error, "Tpetra::getBlockCrsGraph: "
362  "local number of non zero entries is not a multiple of blockSize^2 ");
363 
364  LO block_rows = pointRowptr.extent(0) == 0 ? 0 : (pointRowptr.extent(0)-1)/blockSize;
365  row_map_type blockRowptr("blockRowptr", block_rows+1);
366  entries_type blockColind("blockColind", pointColind.extent(0)/(bs2));
367 
368  Kokkos::parallel_for("fillMesh",range_type(0,block_rows), KOKKOS_LAMBDA(const LO i) {
369 
370  const LO offset_b = pointRowptr(i*blockSize)/bs2;
371  const LO offset_b_max = pointRowptr((i+1)*blockSize)/bs2;
372 
373  if (i==block_rows-1)
374  blockRowptr(i+1) = offset_b_max;
375  blockRowptr(i) = offset_b;
376 
377  const LO offset_p = pointRowptr(i*blockSize);
378  const LO offset_p_max = pointRowptr((i+1)*blockSize);
379 
380  LO filled_block = 0;
381  for (LO p_i=0; p_i<offset_p_max-offset_p; ++p_i) {
382  auto bcol_GID = localPointColMap.getGlobalElement(pointColind(offset_p + p_i))/blockSize;
383  auto bcol_LID = localMeshColMap.getLocalElement(bcol_GID);
384 
385  bool visited = false;
386  for (LO k=0; k<filled_block; ++k) {
387  if (blockColind(offset_b + k) == bcol_LID)
388  visited = true;
389  }
390  if (!visited) {
391  blockColind(offset_b + filled_block) = bcol_LID;
392  ++filled_block;
393  }
394  }
395  });
396 
397  meshCrsGraph = rcp(new crs_graph_type(meshRowMap, meshColMap, blockRowptr, blockColind));
398  meshCrsGraph->fillComplete(meshDomainMap,meshRangeMap);
399  Kokkos::DefaultExecutionSpace().fence();
400  }
401 
402  return meshCrsGraph;
403  }
404 
405  template<class Scalar, class LO, class GO, class Node>
406  Teuchos::RCP<BlockCrsMatrix<Scalar, LO, GO, Node> >
407  convertToBlockCrsMatrix(const Tpetra::CrsMatrix<Scalar, LO, GO, Node>& pointMatrix, const LO &blockSize, bool use_LID)
408  {
409  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::convertToBlockCrsMatrix", convertToBlockCrsMatrix0);
410  /*
411  ASSUMPTIONS:
412 
413  1) In point matrix, all entries associated with a little block are present (even if they are zero).
414  2) For given mesh DOF, point DOFs appear consecutively and in ascending order in row & column maps.
415  3) Point column map and block column map are ordered consistently.
416  */
417 
418  using Teuchos::Array;
419  using Teuchos::ArrayView;
420  using Teuchos::RCP;
421 
422  typedef Tpetra::BlockCrsMatrix<Scalar,LO,GO,Node> block_crs_matrix_type;
423  typedef Tpetra::CrsMatrix<Scalar, LO,GO,Node> crs_matrix_type;
424 
425  using local_graph_device_type = typename crs_matrix_type::local_graph_device_type;
426  using local_matrix_device_type = typename crs_matrix_type::local_matrix_device_type;
427  using row_map_type = typename local_graph_device_type::row_map_type::non_const_type;
428  using values_type = typename local_matrix_device_type::values_type::non_const_type;
429 
430  using offset_type = typename row_map_type::non_const_value_type;
431 
432  using execution_space = typename Node::execution_space;
433  using range_type = Kokkos::RangePolicy<execution_space, LO>;
434 
435  RCP<block_crs_matrix_type> blockMatrix;
436 
437  const offset_type bs2 = blockSize * blockSize;
438 
439  auto meshCrsGraph = getBlockCrsGraph(pointMatrix, blockSize, use_LID);
440 
441  if (use_LID) {
442  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::convertToBlockCrsMatrix::LID", convertToBlockCrsMatrix1);
443  auto pointLocalGraph = pointMatrix.getCrsGraph()->getLocalGraphDevice();
444  auto pointRowptr = pointLocalGraph.row_map;
445  auto pointColind = pointLocalGraph.entries;
446 
447  offset_type block_rows = pointRowptr.extent(0) == 0 ? 0 : (pointRowptr.extent(0)-1)/blockSize;
448  values_type blockValues("values", meshCrsGraph->getLocalNumEntries()*bs2);
449  auto pointValues = pointMatrix.getLocalValuesDevice (Access::ReadOnly);
450  auto blockRowptr = meshCrsGraph->getLocalGraphDevice().row_map;
451 
452  Kokkos::parallel_for("copyblockValues",range_type(0,block_rows),KOKKOS_LAMBDA(const LO i) {
453  const offset_type blkBeg = blockRowptr[i];
454  const offset_type numBlocks = blockRowptr[i+1] - blkBeg;
455 
456  // For each block in the row...
457  for (offset_type block=0; block < numBlocks; block++) {
458 
459  // For each entry in the block...
460  for(LO little_row=0; little_row<blockSize; little_row++) {
461  offset_type point_row_offset = pointRowptr[i*blockSize + little_row];
462  for(LO little_col=0; little_col<blockSize; little_col++) {
463  blockValues((blkBeg+block) * bs2 + little_row * blockSize + little_col) =
464  pointValues[point_row_offset + block*blockSize + little_col];
465  }
466  }
467 
468  }
469  });
470  blockMatrix = rcp(new block_crs_matrix_type(*meshCrsGraph, blockValues, blockSize));
471  Kokkos::DefaultExecutionSpace().fence();
472  }
473  else {
474  TEUCHOS_FUNC_TIME_MONITOR_DIFF("Tpetra::convertToBlockCrsMatrix::GID", convertToBlockCrsMatrix2);
475  auto localMeshColMap = meshCrsGraph->getColMap()->getLocalMap();
476  auto localPointColMap = pointMatrix.getColMap()->getLocalMap();
477 
478  values_type blockValues("values", meshCrsGraph->getLocalNumEntries()*bs2);
479  {
480  auto pointLocalGraph = pointMatrix.getCrsGraph()->getLocalGraphDevice();
481  auto pointRowptr = pointLocalGraph.row_map;
482  auto pointColind = pointLocalGraph.entries;
483 
484  offset_type block_rows = pointRowptr.extent(0) == 0 ? 0 : (pointRowptr.extent(0)-1)/blockSize;
485  auto pointValues = pointMatrix.getLocalValuesDevice (Access::ReadOnly);
486  auto blockRowptr = meshCrsGraph->getLocalGraphDevice().row_map;
487  auto blockColind = meshCrsGraph->getLocalGraphDevice().entries;
488 
489  row_map_type pointGColind("pointGColind", pointColind.extent(0));
490 
491  Kokkos::parallel_for("computePointGColind",range_type(0,pointColind.extent(0)),KOKKOS_LAMBDA(const LO i) {
492  pointGColind(i) = localPointColMap.getGlobalElement(pointColind(i));
493  });
494 
495  row_map_type blockGColind("blockGColind", blockColind.extent(0));
496 
497  Kokkos::parallel_for("computeBlockGColind",range_type(0,blockGColind.extent(0)),KOKKOS_LAMBDA(const LO i) {
498  blockGColind(i) = localMeshColMap.getGlobalElement(blockColind(i));
499  });
500 
501  Kokkos::parallel_for("copyblockValues",range_type(0,block_rows),KOKKOS_LAMBDA(const LO i) {
502  const offset_type blkBeg = blockRowptr[i];
503  const offset_type numBlocks = blockRowptr[i+1] - blkBeg;
504 
505  for (offset_type point_i=0; point_i < pointRowptr[i*blockSize + 1] - pointRowptr[i*blockSize]; point_i++) {
506 
507  offset_type block_inv=static_cast<offset_type>(-1);
508  offset_type little_col_inv=static_cast<offset_type>(-1);
509  for (offset_type block_2=0; block_2 < numBlocks; block_2++) {
510  for (LO little_col_2=0; little_col_2 < blockSize; little_col_2++) {
511  if (blockGColind(blkBeg+block_2)*blockSize + little_col_2 == pointGColind(pointRowptr[i*blockSize] + point_i)) {
512  block_inv = block_2;
513  little_col_inv = little_col_2;
514  break;
515  }
516  }
517  if (block_inv!=static_cast<offset_type>(-1))
518  break;
519  }
520 
521  for(LO little_row=0; little_row<blockSize; little_row++) {
522  blockValues((blkBeg+block_inv) * bs2 + little_row * blockSize + little_col_inv) = pointValues[pointRowptr[i*blockSize+little_row] + point_i];
523  }
524  }
525  });
526  }
527  blockMatrix = rcp(new block_crs_matrix_type(*meshCrsGraph, blockValues, blockSize));
528  Kokkos::DefaultExecutionSpace().fence();
529  }
530 
531  return blockMatrix;
532 
533  }
534 
535  template<class Scalar, class LO, class GO, class Node>
536  Teuchos::RCP<Tpetra::CrsMatrix<Scalar, LO, GO, Node>>
537  fillLogicalBlocks(const Tpetra::CrsMatrix<Scalar, LO, GO, Node>& pointMatrix, const LO &blockSize)
538  {
540  using execution_space = typename Node::execution_space;
541  using dev_row_view_t = typename crs_t::local_graph_device_type::row_map_type::non_const_type;
542  using dev_col_view_t = typename crs_t::local_graph_device_type::entries_type::non_const_type;
543  using dev_val_view_t = typename crs_t::local_matrix_device_type::values_type::non_const_type;
544  using range_type = Kokkos::RangePolicy<execution_space, size_t>;
545  using team_policy = Kokkos::TeamPolicy<execution_space>;
546  using member_type = typename team_policy::member_type;
547  using scratch_view = Kokkos::View<bool*, typename execution_space::scratch_memory_space>;
548  using Ordinal = typename dev_row_view_t::non_const_value_type;
549 
550  // Get structure / values
551  auto local = pointMatrix.getLocalMatrixDevice();
552  auto row_ptrs = local.graph.row_map;
553  auto col_inds = local.graph.entries;
554  auto values = local.values;
555  const auto nrows = pointMatrix.getLocalNumRows();
556 
557  //
558  // Populate all active blocks, they must be fully populated with entries so fill with zeroes
559  //
560 
561  // Make row workspace views
562  dev_row_view_t new_rowmap("new_rowmap", nrows+1);
563  const auto blocks_per_row = nrows / blockSize; // assumes square matrix
564  dev_row_view_t active_block_row_map("active_block_row_map", blocks_per_row + 1);
565  const int max_threads = execution_space().concurrency();
566  assert(blockSize > 1);
567  assert(nrows % blockSize == 0);
568  const int mem_level = 1;
569  const int bytes = scratch_view::shmem_size(blocks_per_row);
570 
571  if (max_threads >= blockSize) {
572  // Prefer 1 team per block since this will require a lot less scratch memory
573  team_policy tp(blocks_per_row, blockSize);
574 
575  // Count active blocks
576  Kokkos::parallel_for("countActiveBlocks", tp.set_scratch_size(mem_level, Kokkos::PerTeam(bytes)), KOKKOS_LAMBDA(const member_type& team) {
577  Ordinal block_row = team.league_rank();
578 
579  scratch_view row_block_active(team.team_scratch(mem_level), blocks_per_row);
580  Kokkos::single(
581  Kokkos::PerTeam(team), [&] () {
582  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
583  row_block_active(row_block_idx) = false;
584  }
585  });
586  team.team_barrier();
587 
588  // All threads in a team scan a blocks-worth of rows to see which
589  // blocks are active
590  Kokkos::parallel_for(
591  Kokkos::TeamThreadRange(team, blockSize), [&] (Ordinal block_offset) {
592 
593  Ordinal row = block_row*blockSize + block_offset;
594  Ordinal row_itr = row_ptrs(row);
595  Ordinal row_end = row_ptrs(row+1);
596 
597  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
598  const Ordinal first_possible_col_in_block = row_block_idx * blockSize;
599  const Ordinal first_possible_col_in_next_block = (row_block_idx+1) * blockSize;
600  Ordinal curr_nnz_col = col_inds(row_itr);
601  while (curr_nnz_col >= first_possible_col_in_block && curr_nnz_col < first_possible_col_in_next_block && row_itr < row_end) {
602  // This block has at least one nnz in this row
603  row_block_active(row_block_idx) = true;
604  ++row_itr;
605  if (row_itr == row_end) break;
606  curr_nnz_col = col_inds(row_itr);
607  }
608  }
609  });
610 
611  team.team_barrier();
612 
613  Kokkos::single(
614  Kokkos::PerTeam(team), [&] () {
615  Ordinal count = 0;
616  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
617  if (row_block_active(row_block_idx)) {
618  ++count;
619  }
620  }
621  active_block_row_map(block_row) = count;
622  });
623  });
624  }
625  else {
626  // We don't have enough parallelism to make a thread team handle a block, so just
627  // have 1 thread per block
628  team_policy tp(blocks_per_row, 1);
629 
630  // Count active blocks
631  Kokkos::parallel_for("countActiveBlocks", tp.set_scratch_size(mem_level, Kokkos::PerTeam(bytes)), KOKKOS_LAMBDA(const member_type& team) {
632  Ordinal block_row = team.league_rank();
633 
634  scratch_view row_block_active(team.team_scratch(mem_level), blocks_per_row);
635  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
636  row_block_active(row_block_idx) = false;
637  }
638 
639  // One thread scans a blocks-worth of rows to see which blocks are active
640  for (int block_offset=0; block_offset < blockSize; ++block_offset) {
641  Ordinal row = block_row*blockSize + block_offset;
642  Ordinal row_itr = row_ptrs(row);
643  Ordinal row_end = row_ptrs(row+1);
644 
645  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
646  const Ordinal first_possible_col_in_block = row_block_idx * blockSize;
647  const Ordinal first_possible_col_in_next_block = (row_block_idx+1) * blockSize;
648  Ordinal curr_nnz_col = col_inds(row_itr);
649  while (curr_nnz_col >= first_possible_col_in_block && curr_nnz_col < first_possible_col_in_next_block && row_itr < row_end) {
650  // This block has at least one nnz in this row
651  row_block_active(row_block_idx) = true;
652  ++row_itr;
653  if (row_itr == row_end) break;
654  curr_nnz_col = col_inds(row_itr);
655  }
656  }
657  }
658 
659  Ordinal count = 0;
660  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
661  if (row_block_active(row_block_idx)) {
662  ++count;
663  }
664  }
665  active_block_row_map(block_row) = count;
666  });
667  }
668 
669  Ordinal nnz_block_count = 0;
670  KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<
671  execution_space>(active_block_row_map.extent(0), active_block_row_map, nnz_block_count);
672  dev_col_view_t block_col_ids("block_col_ids", nnz_block_count);
673 
674  // Find active blocks
675  if (max_threads >= blockSize) {
676  // Prefer 1 team per block since this will require a lot less scratch memory
677  team_policy tp(blocks_per_row, blockSize);
678 
679  Kokkos::parallel_for("findActiveBlocks", tp.set_scratch_size(mem_level, Kokkos::PerTeam(bytes)), KOKKOS_LAMBDA(const member_type& team) {
680  Ordinal block_row = team.league_rank();
681 
682  scratch_view row_block_active(team.team_scratch(mem_level), blocks_per_row);
683  Kokkos::single(
684  Kokkos::PerTeam(team), [&] () {
685  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
686  row_block_active(row_block_idx) = false;
687  }
688  });
689  team.team_barrier();
690 
691  // All threads in a team scan a blocks-worth of rows to see which
692  // blocks are active
693  Kokkos::parallel_for(
694  Kokkos::TeamThreadRange(team, blockSize), [&] (Ordinal block_offset) {
695 
696  Ordinal row = block_row*blockSize + block_offset;
697  Ordinal row_itr = row_ptrs(row);
698  Ordinal row_end = row_ptrs(row+1);
699 
700  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
701  const Ordinal first_possible_col_in_block = row_block_idx * blockSize;
702  const Ordinal first_possible_col_in_next_block = (row_block_idx+1) * blockSize;
703  Ordinal curr_nnz_col = col_inds(row_itr);
704  while (curr_nnz_col >= first_possible_col_in_block && curr_nnz_col < first_possible_col_in_next_block && row_itr < row_end) {
705  // This block has at least one nnz in this row
706  row_block_active(row_block_idx) = true;
707  ++row_itr;
708  if (row_itr == row_end) break;
709  curr_nnz_col = col_inds(row_itr);
710  }
711  }
712  });
713 
714  team.team_barrier();
715 
716  Kokkos::single(
717  Kokkos::PerTeam(team), [&] () {
718  Ordinal offset = active_block_row_map[block_row];
719  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
720  if (row_block_active(row_block_idx)) {
721  block_col_ids(offset) = row_block_idx;
722  ++offset;
723  }
724  }
725  });
726  });
727  }
728  else {
729  team_policy tp(blocks_per_row, 1);
730 
731  Kokkos::parallel_for("findActiveBlocks", tp.set_scratch_size(mem_level, Kokkos::PerTeam(bytes)), KOKKOS_LAMBDA(const member_type& team) {
732  Ordinal block_row = team.league_rank();
733 
734  scratch_view row_block_active(team.team_scratch(mem_level), blocks_per_row);
735  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
736  row_block_active(row_block_idx) = false;
737  }
738 
739  // One thread scans a blocks-worth of rows to see which blocks are active
740  for (int block_offset=0; block_offset < blockSize; ++block_offset) {
741  Ordinal row = block_row*blockSize + block_offset;
742  Ordinal row_itr = row_ptrs(row);
743  Ordinal row_end = row_ptrs(row+1);
744 
745  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
746  const Ordinal first_possible_col_in_block = row_block_idx * blockSize;
747  const Ordinal first_possible_col_in_next_block = (row_block_idx+1) * blockSize;
748  Ordinal curr_nnz_col = col_inds(row_itr);
749  while (curr_nnz_col >= first_possible_col_in_block && curr_nnz_col < first_possible_col_in_next_block && row_itr < row_end) {
750  // This block has at least one nnz in this row
751  row_block_active(row_block_idx) = true;
752  ++row_itr;
753  if (row_itr == row_end) break;
754  curr_nnz_col = col_inds(row_itr);
755  }
756  }
757  }
758 
759  Ordinal offset = active_block_row_map[block_row];
760  for (size_t row_block_idx = 0; row_block_idx < blocks_per_row; ++row_block_idx) {
761  if (row_block_active(row_block_idx)) {
762  block_col_ids(offset) = row_block_idx;
763  ++offset;
764  }
765  }
766  });
767  }
768 
769  // Sizing
770  Kokkos::parallel_for("sizing", range_type(0, nrows), KOKKOS_LAMBDA(const size_t row) {
771  const auto block_row = row / blockSize;
772  const Ordinal block_row_begin = active_block_row_map(block_row);
773  const Ordinal block_row_end = active_block_row_map(block_row+1);
774  const Ordinal row_nnz = (block_row_end - block_row_begin) * blockSize;
775  new_rowmap(row) = row_nnz;
776  });
777 
778  Ordinal new_nnz_count = 0;
779  KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<
780  execution_space>(new_rowmap.extent(0), new_rowmap, new_nnz_count);
781  // Now populate cols and vals
782  dev_col_view_t new_col_ids("new_col_ids", new_nnz_count);
783  dev_val_view_t new_vals("new_vals", new_nnz_count);
784  Kokkos::parallel_for("entries", range_type(0, nrows), KOKKOS_LAMBDA(const size_t row) {
785  Ordinal row_itr = row_ptrs(row);
786  Ordinal row_end = row_ptrs(row+1);
787  Ordinal row_itr_new = new_rowmap(row);
788 
789  Ordinal block_row = row / blockSize;
790  Ordinal block_row_begin = active_block_row_map(block_row);
791  Ordinal block_row_end = active_block_row_map(block_row+1);
792 
793  for (Ordinal row_block_idx = block_row_begin; row_block_idx < block_row_end; ++row_block_idx) {
794  const Ordinal block_col = block_col_ids(row_block_idx);
795  const Ordinal first_possible_col_in_block = block_col * blockSize;
796  const Ordinal first_possible_col_in_next_block = (block_col+1) * blockSize;
797  for (Ordinal possible_col = first_possible_col_in_block; possible_col < first_possible_col_in_next_block; ++possible_col, ++row_itr_new) {
798  new_col_ids(row_itr_new) = possible_col;
799  Ordinal curr_nnz_col = col_inds(row_itr);
800  if (possible_col == curr_nnz_col && row_itr < row_end) {
801  // Already a non-zero entry
802  new_vals(row_itr_new) = values(row_itr);
803  ++row_itr;
804  }
805  }
806  }
807  });
808 
809  // Create new, filled CRS
810  auto crs_row_map = pointMatrix.getRowMap();
811  auto crs_col_map = pointMatrix.getColMap();
812  Teuchos::RCP<crs_t> result = Teuchos::rcp(new crs_t(crs_row_map, crs_col_map, new_rowmap, new_col_ids, new_vals));
813  result->fillComplete();
814  return result;
815  }
816 
817  template<class Scalar, class LO, class GO, class Node>
818  Teuchos::RCP<Tpetra::CrsMatrix<Scalar, LO, GO, Node>>
820  {
822  using dev_row_view_t = typename crs_t::local_graph_device_type::row_map_type::non_const_type;
823  using dev_col_view_t = typename crs_t::local_graph_device_type::entries_type::non_const_type;
824  using dev_val_view_t = typename crs_t::local_matrix_device_type::values_type::non_const_type;
825  using impl_scalar_t = typename Kokkos::ArithTraits<Scalar>::val_type;
826  using STS = Kokkos::ArithTraits<impl_scalar_t>;
827  using Ordinal = typename dev_row_view_t::non_const_value_type;
828  using execution_space = typename Node::execution_space;
829  using range_type = Kokkos::RangePolicy<execution_space, size_t>;
830 
831  // Get structure / values
832  auto local = pointMatrix.getLocalMatrixHost();
833  auto row_ptrs = local.graph.row_map;
834  auto col_inds = local.graph.entries;
835  auto values = local.values;
836  const auto nrows = pointMatrix.getLocalNumRows();
837 
838  // Sizing and rows
839  dev_row_view_t new_rowmap("new_rowmap", nrows+1);
840  const impl_scalar_t zero = STS::zero();
841  Kokkos::parallel_for("sizing", range_type(0, nrows), KOKKOS_LAMBDA(const size_t row) {
842  const Ordinal row_nnz_begin = row_ptrs(row);
843  Ordinal row_nnzs = 0;
844  for (Ordinal row_nnz = row_nnz_begin; row_nnz < row_ptrs(row+1); ++row_nnz) {
845  const impl_scalar_t value = values(row_nnz);
846  if (value != zero) {
847  ++row_nnzs;
848  }
849  }
850  new_rowmap(row) = row_nnzs;
851  });
852 
853  Ordinal real_nnzs = 0;
854  KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<
855  execution_space>(new_rowmap.extent(0), new_rowmap, real_nnzs);
856  // Now populate cols and vals
857  dev_col_view_t new_col_ids("new_col_ids", real_nnzs);
858  dev_val_view_t new_vals("new_vals", real_nnzs);
859  Kokkos::parallel_for("entries", range_type(0, nrows), KOKKOS_LAMBDA(const size_t row) {
860  Ordinal row_nnzs = 0;
861  const Ordinal old_row_nnz_begin = row_ptrs(row);
862  const Ordinal new_row_nnz_begin = new_rowmap(row);
863  for (Ordinal old_row_nnz = old_row_nnz_begin; old_row_nnz < row_ptrs(row+1); ++old_row_nnz) {
864  const impl_scalar_t value = values(old_row_nnz);
865  if (value != zero) {
866  new_col_ids(new_row_nnz_begin + row_nnzs) = col_inds(old_row_nnz);
867  new_vals(new_row_nnz_begin + row_nnzs) = value;
868  ++row_nnzs;
869  }
870  }
871  });
872 
873  // Create new, unfilled CRS
874  auto crs_row_map = pointMatrix.getRowMap();
875  auto crs_col_map = pointMatrix.getColMap();
876  Teuchos::RCP<crs_t> result = Teuchos::rcp(new crs_t(crs_row_map, crs_col_map, new_rowmap, new_col_ids, new_vals));
877  result->fillComplete();
878  return result;
879  }
880 
881  template<class LO, class GO, class Node>
882  Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
883  createMeshMap (const LO& blockSize, const Tpetra::Map<LO,GO,Node>& pointMap, bool use_LID)
884  {
885  typedef Teuchos::OrdinalTraits<Tpetra::global_size_t> TOT;
886  typedef Tpetra::Map<LO,GO,Node> map_type;
887 
888  if(use_LID) {
889 
890  using execution_space = typename Node::execution_space;
891  using range_type = Kokkos::RangePolicy<execution_space, size_t>;
892 
893  auto pointGlobalID = pointMap.getMyGlobalIndicesDevice();
894  LO block_rows = pointGlobalID.extent(0)/blockSize;
895  Kokkos::View<GO*, typename map_type::device_type> meshGlobalID("meshGlobalID", block_rows);
896 
897  Kokkos::parallel_for("fillMeshMap",range_type(0,block_rows), KOKKOS_LAMBDA(const LO i) {
898  meshGlobalID(i) = pointGlobalID(i*blockSize)/blockSize;
899  });
900 
901  Teuchos::RCP<const map_type> meshMap = Teuchos::rcp( new map_type(TOT::invalid(), meshGlobalID, 0, pointMap.getComm()) );
902  return meshMap;
903  }
904  else {
905  //calculate mesh GIDs
906  Teuchos::ArrayView<const GO> pointGids = pointMap.getLocalElementList();
907  Teuchos::Array<GO> meshGids;
908  GO indexBase = pointMap.getIndexBase();
909 
910  // Use hash table to track whether we've encountered this GID previously. This will happen
911  // when striding through the point DOFs in a block. It should not happen otherwise.
912  // I don't use sort/make unique because I don't want to change the ordering.
913  meshGids.reserve(pointGids.size());
914  Tpetra::Details::HashTable<GO,int> hashTable(pointGids.size());
915  for (int i=0; i<pointGids.size(); ++i) {
916  GO meshGid = (pointGids[i]-indexBase) / blockSize + indexBase;
917  if (hashTable.get(meshGid) == -1) {
918  hashTable.add(meshGid,1); //(key,value)
919  meshGids.push_back(meshGid);
920  }
921  }
922 
923  Teuchos::RCP<const map_type> meshMap = Teuchos::rcp( new map_type(TOT::invalid(), meshGids(), 0, pointMap.getComm()) );
924  return meshMap;
925  }
926  }
927 
928 
929  template<class LO, class GO, class Node>
930  Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
931  createPointMap (const LO& blockSize, const Tpetra::Map<LO,GO,Node>& blockMap)
932  {
933  typedef Teuchos::OrdinalTraits<Tpetra::global_size_t> TOT;
934  typedef Tpetra::Map<LO,GO,Node> map_type;
935 
936  //calculate mesh GIDs
937  Teuchos::ArrayView<const GO> blockGids = blockMap.getLocalElementList();
938  Teuchos::Array<GO> pointGids(blockGids.size() * blockSize);
939  GO indexBase = blockMap.getIndexBase();
940 
941  for(LO i=0, ct=0; i<(LO)blockGids.size(); i++) {
942  GO base = (blockGids[i] - indexBase)* blockSize + indexBase;
943  for(LO j=0; j<blockSize; j++, ct++)
944  pointGids[i*blockSize+j] = base+j;
945  }
946 
947  Teuchos::RCP<const map_type> pointMap = Teuchos::rcp( new map_type(TOT::invalid(), pointGids(), 0, blockMap.getComm()) );
948  return pointMap;
949 
950  }
951 
952 
953  template<class Scalar, class LO, class GO, class Node>
954  Teuchos::RCP<CrsMatrix<Scalar, LO, GO, Node> >
956 
957  using Teuchos::Array;
958  using Teuchos::ArrayView;
959  using Teuchos::RCP;
960 
961  typedef Tpetra::BlockCrsMatrix<Scalar,LO,GO,Node> block_crs_matrix_type;
962  typedef Tpetra::Map<LO,GO,Node> map_type;
963  typedef Tpetra::CrsMatrix<Scalar, LO,GO,Node> crs_matrix_type;
964 
965  using crs_graph_type = typename block_crs_matrix_type::crs_graph_type;
966  using local_graph_device_type = typename crs_matrix_type::local_graph_device_type;
967  using local_matrix_device_type = typename crs_matrix_type::local_matrix_device_type;
968  using row_map_type = typename local_graph_device_type::row_map_type::non_const_type;
969  using entries_type = typename local_graph_device_type::entries_type::non_const_type;
970  using values_type = typename local_matrix_device_type::values_type::non_const_type;
971 
972  using row_map_type_const = typename local_graph_device_type::row_map_type;
973  using entries_type_const = typename local_graph_device_type::entries_type;
974 
975  using little_block_type = typename block_crs_matrix_type::const_little_block_type;
976  using offset_type = typename row_map_type::non_const_value_type;
977  using column_type = typename entries_type::non_const_value_type;
978 
979  using execution_space = typename Node::execution_space;
980  using range_type = Kokkos::RangePolicy<execution_space, LO>;
981 
982 
983  LO blocksize = blockMatrix.getBlockSize();
984  const offset_type bs2 = blocksize * blocksize;
985  size_t block_nnz = blockMatrix.getLocalNumEntries();
986  size_t point_nnz = block_nnz * bs2;
987 
988  // We can get these from the blockMatrix directly
989  RCP<const map_type> pointDomainMap = blockMatrix.getDomainMap();
990  RCP<const map_type> pointRangeMap = blockMatrix.getRangeMap();
991 
992  // We need to generate the row/col Map ourselves.
993  RCP<const map_type> blockRowMap = blockMatrix.getRowMap();
994  RCP<const map_type> pointRowMap = createPointMap<LO,GO,Node>(blocksize, *blockRowMap);
995 
996  RCP<const map_type> blockColMap = blockMatrix.getColMap();
997  RCP<const map_type> pointColMap = createPointMap<LO,GO,Node>(blocksize, *blockColMap);
998 
999  // Get the last few things
1000 
1001  const crs_graph_type & blockGraph = blockMatrix.getCrsGraph();
1002  LO point_rows = (LO) pointRowMap->getLocalNumElements();
1003  LO block_rows = (LO) blockRowMap->getLocalNumElements();
1004  auto blockValues = blockMatrix.getValuesDevice();
1005  auto blockLocalGraph = blockGraph.getLocalGraphDevice();
1006  row_map_type_const blockRowptr = blockLocalGraph.row_map;
1007  entries_type_const blockColind = blockLocalGraph.entries;
1008 
1009  // Generate the point matrix rowptr / colind / values
1010  row_map_type rowptr("row_map", point_rows+1);
1011  entries_type colind("entries", point_nnz);
1012  values_type values("values", point_nnz);
1013 
1014  // Pre-fill the rowptr
1015  Kokkos::parallel_for("fillRowPtr",range_type(0,block_rows),KOKKOS_LAMBDA(const LO i) {
1016  offset_type base = blockRowptr[i];
1017  offset_type diff = blockRowptr[i+1] - base;
1018  for(LO j=0; j<blocksize; j++) {
1019  rowptr[i*blocksize +j] = base*bs2 + j*diff*blocksize;
1020  }
1021 
1022  // Fill the last guy, if we're on the final entry
1023  if(i==block_rows-1) {
1024  rowptr[block_rows*blocksize] = blockRowptr[i+1] * bs2;
1025  }
1026  });
1027 
1028 
1029  Kokkos::parallel_for("copyEntriesAndValues",range_type(0,block_rows),KOKKOS_LAMBDA(const LO i) {
1030  const offset_type blkBeg = blockRowptr[i];
1031  const offset_type numBlocks = blockRowptr[i+1] - blkBeg;
1032 
1033  // For each block in the row...
1034  for (offset_type block=0; block < numBlocks; block++) {
1035  column_type point_col_base = blockColind[blkBeg + block] * blocksize;
1036  little_block_type my_block(blockValues.data () + (blkBeg+block) * bs2, blocksize, blocksize);
1037 
1038  // For each entry in the block...
1039  for(LO little_row=0; little_row<blocksize; little_row++) {
1040  offset_type point_row_offset = rowptr[i*blocksize + little_row];
1041  for(LO little_col=0; little_col<blocksize; little_col++) {
1042  colind[point_row_offset + block*blocksize + little_col] = point_col_base + little_col;
1043  values[point_row_offset + block*blocksize + little_col] = my_block(little_row,little_col);
1044  }
1045  }
1046 
1047  }
1048  });
1049 
1050  // Generate the matrix
1051  RCP<crs_matrix_type> pointCrsMatrix = rcp(new crs_matrix_type(pointRowMap, pointColMap, 0));
1052  pointCrsMatrix->setAllValues(rowptr,colind,values);
1053 
1054  // FUTURE OPTIMIZATION: Directly compute import/export, rather than letting ESFC do it
1055  pointCrsMatrix->expertStaticFillComplete(pointDomainMap,pointRangeMap);
1056 
1057  return pointCrsMatrix;
1058  }
1059 
1060 
1061 } // namespace Tpetra
1062 
1063 //
1064 // Explicit instantiation macro for blockCrsMatrixWriter (various
1065 // overloads), writeMatrixStrip, and convertToBlockCrsMatrix.
1066 //
1067 // Must be expanded from within the Tpetra namespace!
1068 //
1069 #define TPETRA_BLOCKCRSMATRIX_HELPERS_INSTANT(S,LO,GO,NODE) \
1070  template void blockCrsMatrixWriter(BlockCrsMatrix<S,LO,GO,NODE> const &A, std::string const &fileName); \
1071  template void blockCrsMatrixWriter(BlockCrsMatrix<S,LO,GO,NODE> const &A, std::string const &fileName, Teuchos::ParameterList const &params); \
1072  template void blockCrsMatrixWriter(BlockCrsMatrix<S,LO,GO,NODE> const &A, std::ostream &os); \
1073  template void blockCrsMatrixWriter(BlockCrsMatrix<S,LO,GO,NODE> const &A, std::ostream &os, Teuchos::ParameterList const &params); \
1074  template void writeMatrixStrip(BlockCrsMatrix<S,LO,GO,NODE> const &A, std::ostream &os, Teuchos::ParameterList const &params); \
1075  template Teuchos::RCP<BlockCrsMatrix<S, LO, GO, NODE> > convertToBlockCrsMatrix(const CrsMatrix<S, LO, GO, NODE>& pointMatrix, const LO &blockSize, bool use_LID); \
1076  template Teuchos::RCP<CrsMatrix<S, LO, GO, NODE> > fillLogicalBlocks(const CrsMatrix<S, LO, GO, NODE>& pointMatrix, const LO &blockSize); \
1077  template Teuchos::RCP<CrsMatrix<S, LO, GO, NODE> > unfillFormerBlockCrs(const CrsMatrix<S, LO, GO, NODE>& pointMatrix); \
1078  template Teuchos::RCP<CrsMatrix<S, LO, GO, NODE> > convertToCrsMatrix(const BlockCrsMatrix<S, LO, GO, NODE>& blockMatrix); \
1079  template Teuchos::RCP<CrsGraph<LO, GO, NODE>> getBlockCrsGraph(const Tpetra::CrsMatrix<S, LO, GO, NODE>& pointMatrix, const LO &blockSize, bool use_local_ID);
1080 
1081 //
1082 // Explicit instantiation macro for createMeshMap / createPointMap
1083 //
1084 #define TPETRA_CREATEMESHMAP_INSTANT(LO,GO,NODE) \
1085  template Teuchos::RCP<const Map<LO,GO,NODE> > createMeshMap (const LO& blockSize, const Map<LO,GO,NODE>& pointMap, bool use_local_ID); \
1086  template Teuchos::RCP<const Map<LO,GO,NODE> > createPointMap (const LO& blockSize, const Map<LO,GO,NODE>& blockMap);
1087 
1088 #endif // TPETRA_BLOCKCRSMATRIX_HELPERS_DEF_HPP
Teuchos::RCP< const Tpetra::Map< LO, GO, Node > > createPointMap(LO const &blockSize, const Tpetra::Map< LO, GO, Node > &blockMap)
Helper function to generate a point map from a block map (with a given block size) GIDs associated wi...
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
virtual size_t getLocalNumEntries() const override
The local number of stored (structurally nonzero) entries.
virtual global_size_t getGlobalNumCols() const override
The global number of columns of this matrix.
void blockCrsMatrixWriter(BlockCrsMatrix< Scalar, LO, GO, Node > const &A, std::string const &fileName)
Helper function to write a BlockCrsMatrix. Calls the 3-argument version.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
virtual LO getBlockSize() const override
The number of degrees of freedom per mesh point.
Teuchos::RCP< Tpetra::CrsMatrix< Scalar, LO, GO, Node > > unfillFormerBlockCrs(const Tpetra::CrsMatrix< Scalar, LO, GO, Node > &pointMatrix)
Unfill all point entries in a logical block of a CrsMatrix with zeroes. This can be called after conv...
Sparse matrix whose entries are small dense square blocks, all of the same dimensions.
Teuchos::RCP< const Tpetra::Map< LO, GO, Node > > createMeshMap(LO const &blockSize, const Tpetra::Map< LO, GO, Node > &pointMap, bool use_local_ID=false)
Helper function to generate a mesh map from a point map. Important! It&#39;s assumed that point GIDs asso...
Teuchos::RCP< const map_type > getRangeMap() const override
The range Map of this matrix.
One or more distributed dense vectors.
Teuchos::RCP< const map_type > getDomainMap() const override
Returns the Map associated with the domain of this graph.
size_t getGlobalMaxNumRowEntries() const override
Maximum number of entries in any row of the graph, over all processes in the graph&#39;s communicator...
global_indices_array_device_type getMyGlobalIndicesDevice() const
Return a view of the global indices owned by this process on the Map&#39;s device.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
Teuchos::RCP< const crs_graph_type > getCrsGraph() const
This matrix&#39;s graph, as a CrsGraph.
size_t getLocalNumRows() const override
get the local number of block rows
void writeMatrixStrip(BlockCrsMatrix< Scalar, LO, GO, Node > const &A, std::ostream &os, Teuchos::ParameterList const &params)
Helper function called by blockCrsMatrixWriter.
void getLocalRowView(LO LocalRow, local_inds_host_view_type &indices, values_host_view_type &values) const override
Get a view of the (mesh, i.e., block) row, using local (mesh, i.e., block) indices.
Teuchos::RCP< const map_type > getColMap() const override
get the (mesh) map for the columns of this block matrix.
Teuchos::ArrayView< const global_ordinal_type > getLocalElementList() const
Return a NONOWNING view of the global indices owned by this process.
Teuchos::RCP< const map_type > getDomainMap() const override
Get the (point) domain Map of this matrix.
Teuchos::RCP< const map_type > getRowMap() const override
get the (mesh) map for the rows of this block matrix.
Insert new values that don&#39;t currently exist.
global_ordinal_type getIndexBase() const
The index base for this Map.
global_size_t getGlobalNumRows() const override
get the global number of block rows
TPETRA_DETAILS_ALWAYS_INLINE local_matrix_device_type getLocalMatrixDevice() const
The local sparse matrix.
virtual GO getIndexBase() const override
The index base for global indices in this matrix.
virtual global_size_t getGlobalNumEntries() const override
The global number of stored (structurally nonzero) entries.
Teuchos::RCP< Tpetra::CrsMatrix< Scalar, LO, GO, Node > > fillLogicalBlocks(const Tpetra::CrsMatrix< Scalar, LO, GO, Node > &pointMatrix, const LO &blockSize)
Fill all point entries in a logical block of a CrsMatrix with zeroes. This should be called before co...
Teuchos::RCP< const map_type > getRangeMap() const override
Get the (point) range Map of this matrix.
Teuchos::RCP< CrsMatrix< Scalar, LO, GO, Node > > convertToCrsMatrix(const Tpetra::BlockCrsMatrix< Scalar, LO, GO, Node > &blockMatrix)
Non-member constructor that creates a point CrsMatrix from an existing BlockCrsMatrix.
Teuchos::RCP< CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > createCrsGraph(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t maxNumEntriesPerRow=0, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Nonmember function to create an empty CrsGraph given a row Map and the max number of entries allowed ...
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
Teuchos::RCP< Tpetra::CrsGraph< LO, GO, Node > > getBlockCrsGraph(const Tpetra::CrsMatrix< Scalar, LO, GO, Node > &pointMatrix, const LO &blockSize, bool use_local_ID=true)
Non-member constructor that creates the CrsGraph of a BlockCrsMatrix from an existing point CrsMatrix...
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this matrix.
void add(const KeyType key, const ValueType value)
Add a key and its value to the hash table.
Teuchos::RCP< BlockCrsMatrix< Scalar, LO, GO, Node > > convertToBlockCrsMatrix(const Tpetra::CrsMatrix< Scalar, LO, GO, Node > &pointMatrix, const LO &blockSize, bool use_local_ID=true)
Non-member constructor that creates a BlockCrsMatrix from an existing point CrsMatrix.
local_matrix_device_type::values_type::const_type getLocalValuesDevice(Access::ReadOnlyStruct s) const
Get the Kokkos local values on device, read only.
Teuchos::RCP< const map_type > getColMap() const override
The Map that describes the column distribution in this matrix.
virtual size_t getLocalNumCols() const override
The number of columns needed to apply the forward operator on this node.
size_t getLocalNumRows() const override
The number of matrix rows owned by the calling process.
Teuchos::RCP< const map_type > getRowMap() const override
The Map that describes the row distribution in this matrix.