Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_CrsMatrix_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // ************************************************************************
38 // @HEADER
39 
40 #ifndef TPETRA_CRSMATRIX_DEF_HPP
41 #define TPETRA_CRSMATRIX_DEF_HPP
42 
50 
51 #include "Tpetra_Import_Util.hpp"
52 #include "Tpetra_Import_Util2.hpp"
53 #include "Tpetra_RowMatrix.hpp"
54 #include "Tpetra_LocalCrsMatrixOperator.hpp"
55 
62 #include "Tpetra_Details_getDiagCopyWithoutOffsets.hpp"
70 #include "Tpetra_Details_packCrsMatrix.hpp"
71 #include "Tpetra_Details_unpackCrsMatrixAndCombine.hpp"
73 #include "Teuchos_FancyOStream.hpp"
74 #include "Teuchos_RCP.hpp"
75 #include "Teuchos_DataAccess.hpp"
76 #include "Teuchos_SerialDenseMatrix.hpp" // unused here, could delete
77 #include "KokkosBlas1_scal.hpp"
78 #include "KokkosSparse_getDiagCopy.hpp"
79 #include "KokkosSparse_spmv.hpp"
80 
81 #include <memory>
82 #include <sstream>
83 #include <typeinfo>
84 #include <utility>
85 #include <vector>
86 
87 namespace Tpetra {
88 
89 namespace { // (anonymous)
90 
91  template<class T, class BinaryFunction>
92  T atomic_binary_function_update (volatile T* const dest,
93  const T& inputVal,
94  BinaryFunction f)
95  {
96  T oldVal = *dest;
97  T assume;
98 
99  // NOTE (mfh 30 Nov 2015) I do NOT need a fence here for IBM
100  // POWER architectures, because 'newval' depends on 'assume',
101  // which depends on 'oldVal', which depends on '*dest'. This
102  // sets up a chain of read dependencies that should ensure
103  // correct behavior given a sane memory model.
104  do {
105  assume = oldVal;
106  T newVal = f (assume, inputVal);
107  oldVal = Kokkos::atomic_compare_exchange (dest, assume, newVal);
108  } while (assume != oldVal);
109 
110  return oldVal;
111  }
112 } // namespace (anonymous)
113 
114 //
115 // Users must never rely on anything in the Details namespace.
116 //
117 namespace Details {
118 
128 template<class Scalar>
129 struct AbsMax {
131  Scalar operator() (const Scalar& x, const Scalar& y) {
132  typedef Teuchos::ScalarTraits<Scalar> STS;
133  return std::max (STS::magnitude (x), STS::magnitude (y));
134  }
135 };
136 
137 } // namespace Details
138 } // namespace Tpetra
139 
140 namespace Tpetra {
141 
142  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
143  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
144  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
145  size_t maxNumEntriesPerRow,
146  const Teuchos::RCP<Teuchos::ParameterList>& params) :
147  dist_object_type (rowMap)
148  {
149  const char tfecfFuncName[] = "CrsMatrix(RCP<const Map>, size_t "
150  "[, RCP<ParameterList>]): ";
151  Teuchos::RCP<crs_graph_type> graph;
152  try {
153  graph = Teuchos::rcp (new crs_graph_type (rowMap, maxNumEntriesPerRow,
154  params));
155  }
156  catch (std::exception& e) {
157  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
158  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
159  "size_t [, RCP<ParameterList>]) threw an exception: "
160  << e.what ());
161  }
162  // myGraph_ not null means that the matrix owns the graph. That's
163  // different than the const CrsGraph constructor, where the matrix
164  // does _not_ own the graph.
165  myGraph_ = graph;
166  staticGraph_ = myGraph_;
167  resumeFill (params);
169  }
170 
171  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
173  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
174  const Teuchos::ArrayView<const size_t>& numEntPerRowToAlloc,
175  const Teuchos::RCP<Teuchos::ParameterList>& params) :
176  dist_object_type (rowMap)
177  {
178  const char tfecfFuncName[] = "CrsMatrix(RCP<const Map>, "
179  "ArrayView<const size_t>[, RCP<ParameterList>]): ";
180  Teuchos::RCP<crs_graph_type> graph;
181  try {
182  using Teuchos::rcp;
183  graph = rcp(new crs_graph_type(rowMap, numEntPerRowToAlloc,
184  params));
185  }
186  catch (std::exception& e) {
187  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
188  (true, std::runtime_error, "CrsGraph constructor "
189  "(RCP<const Map>, ArrayView<const size_t>"
190  "[, RCP<ParameterList>]) threw an exception: "
191  << e.what ());
192  }
193  // myGraph_ not null means that the matrix owns the graph. That's
194  // different than the const CrsGraph constructor, where the matrix
195  // does _not_ own the graph.
196  myGraph_ = graph;
197  staticGraph_ = graph;
198  resumeFill (params);
200  }
201 
202  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
204  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
205  const Teuchos::RCP<const map_type>& colMap,
206  const size_t maxNumEntPerRow,
207  const Teuchos::RCP<Teuchos::ParameterList>& params) :
208  dist_object_type (rowMap)
209  {
210  const char tfecfFuncName[] = "CrsMatrix(RCP<const Map>, "
211  "RCP<const Map>, size_t[, RCP<ParameterList>]): ";
212  const char suffix[] =
213  " Please report this bug to the Tpetra developers.";
214 
215  // An artifact of debugging something a while back.
216  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
217  (! staticGraph_.is_null (), std::logic_error,
218  "staticGraph_ is not null at the beginning of the constructor."
219  << suffix);
220  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
221  (! myGraph_.is_null (), std::logic_error,
222  "myGraph_ is not null at the beginning of the constructor."
223  << suffix);
224  Teuchos::RCP<crs_graph_type> graph;
225  try {
226  graph = Teuchos::rcp (new crs_graph_type (rowMap, colMap,
227  maxNumEntPerRow,
228  params));
229  }
230  catch (std::exception& e) {
231  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
232  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
233  "RCP<const Map>, size_t[, RCP<ParameterList>]) threw an "
234  "exception: " << e.what ());
235  }
236  // myGraph_ not null means that the matrix owns the graph. That's
237  // different than the const CrsGraph constructor, where the matrix
238  // does _not_ own the graph.
239  myGraph_ = graph;
240  staticGraph_ = myGraph_;
241  resumeFill (params);
243  }
244 
245  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
247  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
248  const Teuchos::RCP<const map_type>& colMap,
249  const Teuchos::ArrayView<const size_t>& numEntPerRowToAlloc,
250  const Teuchos::RCP<Teuchos::ParameterList>& params) :
251  dist_object_type (rowMap)
252  {
253  const char tfecfFuncName[] =
254  "CrsMatrix(RCP<const Map>, RCP<const Map>, "
255  "ArrayView<const size_t>[, RCP<ParameterList>]): ";
256  Teuchos::RCP<crs_graph_type> graph;
257  try {
258  graph = Teuchos::rcp (new crs_graph_type (rowMap, colMap,
259  numEntPerRowToAlloc,
260  params));
261  }
262  catch (std::exception& e) {
263  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
264  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
265  "RCP<const Map>, ArrayView<const size_t>[, "
266  "RCP<ParameterList>]) threw an exception: " << e.what ());
267  }
268  // myGraph_ not null means that the matrix owns the graph. That's
269  // different than the const CrsGraph constructor, where the matrix
270  // does _not_ own the graph.
271  myGraph_ = graph;
272  staticGraph_ = graph;
273  resumeFill (params);
275  }
276 
277 
278  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
280  CrsMatrix (const Teuchos::RCP<const crs_graph_type>& graph,
281  const Teuchos::RCP<Teuchos::ParameterList>& /* params */) :
282  dist_object_type (graph->getRowMap ()),
283  staticGraph_ (graph),
284  storageStatus_ (Details::STORAGE_1D_PACKED)
285  {
286  using std::endl;
287  typedef typename local_matrix_device_type::values_type values_type;
288  const char tfecfFuncName[] = "CrsMatrix(RCP<const CrsGraph>[, "
289  "RCP<ParameterList>]): ";
290  const bool verbose = Details::Behavior::verbose("CrsMatrix");
291 
292  std::unique_ptr<std::string> prefix;
293  if (verbose) {
294  prefix = this->createPrefix("CrsMatrix", "CrsMatrix(graph,params)");
295  std::ostringstream os;
296  os << *prefix << "Start" << endl;
297  std::cerr << os.str ();
298  }
299 
300  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
301  (graph.is_null (), std::runtime_error, "Input graph is null.");
302  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
303  (! graph->isFillComplete (), std::runtime_error, "Input graph "
304  "is not fill complete. You must call fillComplete on the "
305  "graph before using it to construct a CrsMatrix. Note that "
306  "calling resumeFill on the graph makes it not fill complete, "
307  "even if you had previously called fillComplete. In that "
308  "case, you must call fillComplete on the graph again.");
309 
310  // The graph is fill complete, so it is locally indexed and has a
311  // fixed structure. This means we can allocate the (1-D) array of
312  // values and build the local matrix right now. Note that the
313  // local matrix's number of columns comes from the column Map, not
314  // the domain Map.
315 
316  const size_t numEnt = graph->lclIndsPacked_wdv.extent (0);
317  if (verbose) {
318  std::ostringstream os;
319  os << *prefix << "Allocate values: " << numEnt << endl;
320  std::cerr << os.str ();
321  }
322 
323  values_type val ("Tpetra::CrsMatrix::values", numEnt);
324  valuesPacked_wdv = values_wdv_type(val);
325  valuesUnpacked_wdv = valuesPacked_wdv;
326 
328 
329  if (verbose) {
330  std::ostringstream os;
331  os << *prefix << "Done" << endl;
332  std::cerr << os.str ();
333  }
334  }
335 
336  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
339  const Teuchos::RCP<const crs_graph_type>& graph,
340  const Teuchos::RCP<Teuchos::ParameterList>& params) :
341  dist_object_type (graph->getRowMap ()),
342  staticGraph_ (graph),
343  storageStatus_ (matrix.storageStatus_)
344  {
345  const char tfecfFuncName[] = "CrsMatrix(RCP<const CrsGraph>, "
346  "local_matrix_device_type::values_type, "
347  "[,RCP<ParameterList>]): ";
348  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
349  (graph.is_null (), std::runtime_error, "Input graph is null.");
350  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
351  (! graph->isFillComplete (), std::runtime_error, "Input graph "
352  "is not fill complete. You must call fillComplete on the "
353  "graph before using it to construct a CrsMatrix. Note that "
354  "calling resumeFill on the graph makes it not fill complete, "
355  "even if you had previously called fillComplete. In that "
356  "case, you must call fillComplete on the graph again.");
357 
358  size_t numValuesPacked = graph->lclIndsPacked_wdv.extent(0);
359  valuesPacked_wdv = values_wdv_type(matrix.valuesPacked_wdv, 0, numValuesPacked);
360 
361  size_t numValuesUnpacked = graph->lclIndsUnpacked_wdv.extent(0);
362  valuesUnpacked_wdv = values_wdv_type(matrix.valuesUnpacked_wdv, 0, numValuesUnpacked);
363 
365  }
366 
367 
368  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
370  CrsMatrix (const Teuchos::RCP<const crs_graph_type>& graph,
371  const typename local_matrix_device_type::values_type& values,
372  const Teuchos::RCP<Teuchos::ParameterList>& /* params */) :
373  dist_object_type (graph->getRowMap ()),
374  staticGraph_ (graph),
375  storageStatus_ (Details::STORAGE_1D_PACKED)
376  {
377  const char tfecfFuncName[] = "CrsMatrix(RCP<const CrsGraph>, "
378  "local_matrix_device_type::values_type, "
379  "[,RCP<ParameterList>]): ";
380  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
381  (graph.is_null (), std::runtime_error, "Input graph is null.");
382  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
383  (! graph->isFillComplete (), std::runtime_error, "Input graph "
384  "is not fill complete. You must call fillComplete on the "
385  "graph before using it to construct a CrsMatrix. Note that "
386  "calling resumeFill on the graph makes it not fill complete, "
387  "even if you had previously called fillComplete. In that "
388  "case, you must call fillComplete on the graph again.");
389 
390  // The graph is fill complete, so it is locally indexed and has a
391  // fixed structure. This means we can allocate the (1-D) array of
392  // values and build the local matrix right now. Note that the
393  // local matrix's number of columns comes from the column Map, not
394  // the domain Map.
395 
396  valuesPacked_wdv = values_wdv_type(values);
397  valuesUnpacked_wdv = valuesPacked_wdv;
398 
399  // FIXME (22 Jun 2016) I would very much like to get rid of
400  // k_values1D_ at some point. I find it confusing to have all
401  // these extra references lying around.
402  // KDDKDD ALMOST THERE, MARK!
403 // k_values1D_ = valuesUnpacked_wdv.getDeviceView(Access::ReadWrite);
404 
406  }
407 
408  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
410  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
411  const Teuchos::RCP<const map_type>& colMap,
412  const typename local_graph_device_type::row_map_type& rowPointers,
413  const typename local_graph_device_type::entries_type::non_const_type& columnIndices,
414  const typename local_matrix_device_type::values_type& values,
415  const Teuchos::RCP<Teuchos::ParameterList>& params) :
416  dist_object_type (rowMap),
417  storageStatus_ (Details::STORAGE_1D_PACKED)
418  {
419  using Details::getEntryOnHost;
420  using Teuchos::RCP;
421  using std::endl;
422  const char tfecfFuncName[] = "Tpetra::CrsMatrix(RCP<const Map>, "
423  "RCP<const Map>, ptr, ind, val[, params]): ";
424  const char suffix[] =
425  ". Please report this bug to the Tpetra developers.";
426  const bool debug = Details::Behavior::debug("CrsMatrix");
427  const bool verbose = Details::Behavior::verbose("CrsMatrix");
428 
429  std::unique_ptr<std::string> prefix;
430  if (verbose) {
431  prefix = this->createPrefix(
432  "CrsMatrix", "CrsMatrix(rowMap,colMap,ptr,ind,val[,params])");
433  std::ostringstream os;
434  os << *prefix << "Start" << endl;
435  std::cerr << os.str ();
436  }
437 
438  // Check the user's input. Note that this might throw only on
439  // some processes but not others, causing deadlock. We prefer
440  // deadlock due to exceptions to segfaults, because users can
441  // catch exceptions.
442  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
443  (values.extent(0) != columnIndices.extent(0),
444  std::invalid_argument, "values.extent(0)=" << values.extent(0)
445  << " != columnIndices.extent(0) = " << columnIndices.extent(0)
446  << ".");
447  if (debug && rowPointers.extent(0) != 0) {
448  const size_t numEnt =
449  getEntryOnHost(rowPointers, rowPointers.extent(0) - 1);
450  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
451  (numEnt != size_t(columnIndices.extent(0)) ||
452  numEnt != size_t(values.extent(0)),
453  std::invalid_argument, "Last entry of rowPointers says that "
454  "the matrix has " << numEnt << " entr"
455  << (numEnt != 1 ? "ies" : "y") << ", but the dimensions of "
456  "columnIndices and values don't match this. "
457  "columnIndices.extent(0)=" << columnIndices.extent (0)
458  << " and values.extent(0)=" << values.extent (0) << ".");
459  }
460 
461  RCP<crs_graph_type> graph;
462  try {
463  graph = Teuchos::rcp (new crs_graph_type (rowMap, colMap, rowPointers,
464  columnIndices, params));
465  }
466  catch (std::exception& e) {
467  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
468  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
469  "RCP<const Map>, ptr, ind[, params]) threw an exception: "
470  << e.what ());
471  }
472  // The newly created CrsGraph _must_ have a local graph at this
473  // point. We don't really care whether CrsGraph's constructor
474  // deep-copies or shallow-copies the input, but the dimensions
475  // have to be right. That's how we tell whether the CrsGraph has
476  // a local graph.
477  auto lclGraph = graph->getLocalGraphDevice ();
478  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
479  (lclGraph.row_map.extent (0) != rowPointers.extent (0) ||
480  lclGraph.entries.extent (0) != columnIndices.extent (0),
481  std::logic_error, "CrsGraph's constructor (rowMap, colMap, ptr, "
482  "ind[, params]) did not set the local graph correctly." << suffix);
483  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
484  (lclGraph.entries.extent (0) != values.extent (0),
485  std::logic_error, "CrsGraph's constructor (rowMap, colMap, ptr, ind[, "
486  "params]) did not set the local graph correctly. "
487  "lclGraph.entries.extent(0) = " << lclGraph.entries.extent (0)
488  << " != values.extent(0) = " << values.extent (0) << suffix);
489 
490  // myGraph_ not null means that the matrix owns the graph. This
491  // is true because the column indices come in as nonconst,
492  // implying shared ownership.
493  myGraph_ = graph;
494  staticGraph_ = graph;
495 
496  // The graph may not be fill complete yet. However, it is locally
497  // indexed (since we have a column Map) and has a fixed structure
498  // (due to the input arrays). This means we can allocate the
499  // (1-D) array of values and build the local matrix right now.
500  // Note that the local matrix's number of columns comes from the
501  // column Map, not the domain Map.
502 
503  valuesPacked_wdv = values_wdv_type(values);
504  valuesUnpacked_wdv = valuesPacked_wdv;
505 
506  // FIXME (22 Jun 2016) I would very much like to get rid of
507  // k_values1D_ at some point. I find it confusing to have all
508  // these extra references lying around.
509 // this->k_values1D_ = valuesPacked_wdv.getDeviceView(Access::ReadWrite);
510 
512  if (verbose) {
513  std::ostringstream os;
514  os << *prefix << "Done" << endl;
515  std::cerr << os.str();
516  }
517  }
518 
519  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
521  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
522  const Teuchos::RCP<const map_type>& colMap,
523  const Teuchos::ArrayRCP<size_t>& ptr,
524  const Teuchos::ArrayRCP<LocalOrdinal>& ind,
525  const Teuchos::ArrayRCP<Scalar>& val,
526  const Teuchos::RCP<Teuchos::ParameterList>& params) :
527  dist_object_type (rowMap),
528  storageStatus_ (Details::STORAGE_1D_PACKED)
529  {
530  using Kokkos::Compat::getKokkosViewDeepCopy;
531  using Teuchos::av_reinterpret_cast;
532  using Teuchos::RCP;
533  using values_type = typename local_matrix_device_type::values_type;
534  using IST = impl_scalar_type;
535  const char tfecfFuncName[] = "Tpetra::CrsMatrix(RCP<const Map>, "
536  "RCP<const Map>, ptr, ind, val[, params]): ";
537 
538  RCP<crs_graph_type> graph;
539  try {
540  graph = Teuchos::rcp (new crs_graph_type (rowMap, colMap, ptr,
541  ind, params));
542  }
543  catch (std::exception& e) {
544  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
545  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
546  "RCP<const Map>, ArrayRCP<size_t>, ArrayRCP<LocalOrdinal>[, "
547  "RCP<ParameterList>]) threw an exception: " << e.what ());
548  }
549  // myGraph_ not null means that the matrix owns the graph. This
550  // is true because the column indices come in as nonconst,
551  // implying shared ownership.
552  myGraph_ = graph;
553  staticGraph_ = graph;
554 
555  // The graph may not be fill complete yet. However, it is locally
556  // indexed (since we have a column Map) and has a fixed structure
557  // (due to the input arrays). This means we can allocate the
558  // (1-D) array of values and build the local matrix right now.
559  // Note that the local matrix's number of columns comes from the
560  // column Map, not the domain Map.
561 
562  // The graph _must_ have a local graph at this point. We don't
563  // really care whether CrsGraph's constructor deep-copies or
564  // shallow-copies the input, but the dimensions have to be right.
565  // That's how we tell whether the CrsGraph has a local graph.
566  auto lclGraph = staticGraph_->getLocalGraphDevice ();
567  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
568  (size_t (lclGraph.row_map.extent (0)) != size_t (ptr.size ()) ||
569  size_t (lclGraph.entries.extent (0)) != size_t (ind.size ()),
570  std::logic_error, "CrsGraph's constructor (rowMap, colMap, "
571  "ptr, ind[, params]) did not set the local graph correctly. "
572  "Please report this bug to the Tpetra developers.");
573 
574  values_type valIn =
575  getKokkosViewDeepCopy<device_type> (av_reinterpret_cast<IST> (val ()));
576  valuesPacked_wdv = values_wdv_type(valIn);
577  valuesUnpacked_wdv = valuesPacked_wdv;
578 
579  // FIXME (22 Jun 2016) I would very much like to get rid of
580  // k_values1D_ at some point. I find it confusing to have all
581  // these extra references lying around.
582 // this->k_values1D_ = valuesPacked_wdv.getDeviceView(Access::ReadWrite);
583 
585  }
586 
587  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
589  CrsMatrix (const Teuchos::RCP<const map_type>& rowMap,
590  const Teuchos::RCP<const map_type>& colMap,
591  const local_matrix_device_type& lclMatrix,
592  const Teuchos::RCP<Teuchos::ParameterList>& params) :
593  dist_object_type (rowMap),
594  storageStatus_ (Details::STORAGE_1D_PACKED),
595  fillComplete_ (true)
596  {
597  const char tfecfFuncName[] = "Tpetra::CrsMatrix(RCP<const Map>, "
598  "RCP<const Map>, local_matrix_device_type[, RCP<ParameterList>]): ";
599  const char suffix[] =
600  " Please report this bug to the Tpetra developers.";
601 
602  Teuchos::RCP<crs_graph_type> graph;
603  try {
604  graph = Teuchos::rcp (new crs_graph_type (rowMap, colMap,
605  lclMatrix.graph, params));
606  }
607  catch (std::exception& e) {
608  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
609  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
610  "RCP<const Map>, local_graph_device_type[, RCP<ParameterList>]) threw an "
611  "exception: " << e.what ());
612  }
613  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
614  (!graph->isFillComplete (), std::logic_error, "CrsGraph constructor (RCP"
615  "<const Map>, RCP<const Map>, local_graph_device_type[, RCP<ParameterList>]) "
616  "did not produce a fill-complete graph. Please report this bug to the "
617  "Tpetra developers.");
618  // myGraph_ not null means that the matrix owns the graph. This
619  // is true because the column indices come in as nonconst through
620  // the matrix, implying shared ownership.
621  myGraph_ = graph;
622  staticGraph_ = graph;
623 
624  valuesPacked_wdv = values_wdv_type(lclMatrix.values);
625  valuesUnpacked_wdv = valuesPacked_wdv;
626 
627  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
628  (isFillActive (), std::logic_error,
629  "At the end of a CrsMatrix constructor that should produce "
630  "a fillComplete matrix, isFillActive() is true." << suffix);
631  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
632  (! isFillComplete (), std::logic_error, "At the end of a "
633  "CrsMatrix constructor that should produce a fillComplete "
634  "matrix, isFillComplete() is false." << suffix);
636  }
637 
638  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
641  const Teuchos::RCP<const map_type>& rowMap,
642  const Teuchos::RCP<const map_type>& colMap,
643  const Teuchos::RCP<const map_type>& domainMap,
644  const Teuchos::RCP<const map_type>& rangeMap,
645  const Teuchos::RCP<Teuchos::ParameterList>& params) :
646  dist_object_type (rowMap),
647  storageStatus_ (Details::STORAGE_1D_PACKED),
648  fillComplete_ (true)
649  {
650  const char tfecfFuncName[] = "Tpetra::CrsMatrix(RCP<const Map>, "
651  "RCP<const Map>, RCP<const Map>, RCP<const Map>, "
652  "local_matrix_device_type[, RCP<ParameterList>]): ";
653  const char suffix[] =
654  " Please report this bug to the Tpetra developers.";
655 
656  Teuchos::RCP<crs_graph_type> graph;
657  try {
658  graph = Teuchos::rcp (new crs_graph_type (lclMatrix.graph, rowMap, colMap,
659  domainMap, rangeMap, params));
660  }
661  catch (std::exception& e) {
662  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
663  (true, std::runtime_error, "CrsGraph constructor (RCP<const Map>, "
664  "RCP<const Map>, RCP<const Map>, RCP<const Map>, local_graph_device_type[, "
665  "RCP<ParameterList>]) threw an exception: " << e.what ());
666  }
667  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
668  (! graph->isFillComplete (), std::logic_error, "CrsGraph "
669  "constructor (RCP<const Map>, RCP<const Map>, RCP<const Map>, "
670  "RCP<const Map>, local_graph_device_type[, RCP<ParameterList>]) did "
671  "not produce a fillComplete graph." << suffix);
672  // myGraph_ not null means that the matrix owns the graph. This
673  // is true because the column indices come in as nonconst through
674  // the matrix, implying shared ownership.
675  myGraph_ = graph;
676  staticGraph_ = graph;
677 
678  valuesPacked_wdv = values_wdv_type(lclMatrix.values);
679  valuesUnpacked_wdv = valuesPacked_wdv;
680 
681  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
682  (isFillActive (), std::logic_error,
683  "At the end of a CrsMatrix constructor that should produce "
684  "a fillComplete matrix, isFillActive() is true." << suffix);
685  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
686  (! isFillComplete (), std::logic_error, "At the end of a "
687  "CrsMatrix constructor that should produce a fillComplete "
688  "matrix, isFillComplete() is false." << suffix);
690  }
691 
692  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
695  const Teuchos::RCP<const map_type>& rowMap,
696  const Teuchos::RCP<const map_type>& colMap,
697  const Teuchos::RCP<const map_type>& domainMap,
698  const Teuchos::RCP<const map_type>& rangeMap,
699  const Teuchos::RCP<const import_type>& importer,
700  const Teuchos::RCP<const export_type>& exporter,
701  const Teuchos::RCP<Teuchos::ParameterList>& params) :
702  dist_object_type (rowMap),
703  storageStatus_ (Details::STORAGE_1D_PACKED),
704  fillComplete_ (true)
705  {
706  using Teuchos::rcp;
707  const char tfecfFuncName[] = "Tpetra::CrsMatrix"
708  "(lclMat,Map,Map,Map,Map,Import,Export,params): ";
709  const char suffix[] =
710  " Please report this bug to the Tpetra developers.";
711 
712  Teuchos::RCP<crs_graph_type> graph;
713  try {
714  graph = rcp (new crs_graph_type (lclMatrix.graph, rowMap, colMap,
715  domainMap, rangeMap, importer,
716  exporter, params));
717  }
718  catch (std::exception& e) {
719  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
720  (true, std::runtime_error, "CrsGraph constructor "
721  "(local_graph_device_type, Map, Map, Map, Map, Import, Export, "
722  "params) threw: " << e.what ());
723  }
724  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
725  (!graph->isFillComplete (), std::logic_error, "CrsGraph "
726  "constructor (local_graph_device_type, Map, Map, Map, Map, Import, "
727  "Export, params) did not produce a fill-complete graph. "
728  "Please report this bug to the Tpetra developers.");
729  // myGraph_ not null means that the matrix owns the graph. This
730  // is true because the column indices come in as nonconst through
731  // the matrix, implying shared ownership.
732  myGraph_ = graph;
733  staticGraph_ = graph;
734 
735  valuesPacked_wdv = values_wdv_type(lclMatrix.values);
736  valuesUnpacked_wdv = valuesPacked_wdv;
737 
738  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
739  (isFillActive (), std::logic_error,
740  "At the end of a CrsMatrix constructor that should produce "
741  "a fillComplete matrix, isFillActive() is true." << suffix);
742  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
743  (! isFillComplete (), std::logic_error, "At the end of a "
744  "CrsMatrix constructor that should produce a fillComplete "
745  "matrix, isFillComplete() is false." << suffix);
747  }
748 
749  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
752  const Teuchos::DataAccess copyOrView):
753  dist_object_type (source.getCrsGraph()->getRowMap ()),
754  staticGraph_ (source.getCrsGraph()),
755  storageStatus_ (source.storageStatus_)
756  {
757  const char tfecfFuncName[] = "Tpetra::CrsMatrix("
758  "const CrsMatrix&, const Teuchos::DataAccess): ";
759  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
760  (! source.isFillComplete (), std::invalid_argument,
761  "Source graph must be fillComplete().");
762 
763  if (copyOrView == Teuchos::Copy) {
764  using values_type = typename local_matrix_device_type::values_type;
765  auto vals = source.getLocalValuesDevice (Access::ReadOnly);
766  using Kokkos::view_alloc;
767  using Kokkos::WithoutInitializing;
768  values_type newvals (view_alloc ("val", WithoutInitializing),
769  vals.extent (0));
770  // DEEP_COPY REVIEW - DEVICE-TO_DEVICE
771  Kokkos::deep_copy (newvals, vals);
772  valuesPacked_wdv = values_wdv_type(newvals);
773  valuesUnpacked_wdv = valuesPacked_wdv;
774  fillComplete (source.getDomainMap (), source.getRangeMap ());
775  }
776  else if (copyOrView == Teuchos::View) {
777  valuesPacked_wdv = values_wdv_type(source.valuesPacked_wdv);
778  valuesUnpacked_wdv = values_wdv_type(source.valuesUnpacked_wdv);
779  fillComplete (source.getDomainMap (), source.getRangeMap ());
780  }
781  else {
782  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
783  (true, std::invalid_argument, "Second argument 'copyOrView' "
784  "has an invalid value " << copyOrView << ". Valid values "
785  "include Teuchos::Copy = " << Teuchos::Copy << " and "
786  "Teuchos::View = " << Teuchos::View << ".");
787  }
789  }
790 
791  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
792  void
795  {
796  std::swap(crs_matrix.importMV_, this->importMV_);
797  std::swap(crs_matrix.exportMV_, this->exportMV_);
798  std::swap(crs_matrix.staticGraph_, this->staticGraph_);
799  std::swap(crs_matrix.myGraph_, this->myGraph_);
800  std::swap(crs_matrix.valuesPacked_wdv, this->valuesPacked_wdv);
801  std::swap(crs_matrix.valuesUnpacked_wdv, this->valuesUnpacked_wdv);
802  std::swap(crs_matrix.storageStatus_, this->storageStatus_);
803  std::swap(crs_matrix.fillComplete_, this->fillComplete_);
804  std::swap(crs_matrix.nonlocals_, this->nonlocals_);
805  }
806 
807  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
808  Teuchos::RCP<const Teuchos::Comm<int> >
810  getComm () const {
811  return getCrsGraphRef ().getComm ();
812  }
813 
814  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
815  bool
817  isFillComplete () const {
818  return fillComplete_;
819  }
820 
821  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
822  bool
824  isFillActive () const {
825  return ! fillComplete_;
826  }
827 
828  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
829  bool
832  return this->getCrsGraphRef ().isStorageOptimized ();
833  }
834 
835  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
836  bool
839  return getCrsGraphRef ().isLocallyIndexed ();
840  }
841 
842  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
843  bool
846  return getCrsGraphRef ().isGloballyIndexed ();
847  }
848 
849  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
850  bool
852  hasColMap () const {
853  return getCrsGraphRef ().hasColMap ();
854  }
855 
856  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
860  return getCrsGraphRef ().getGlobalNumEntries ();
861  }
862 
863  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
864  size_t
867  return getCrsGraphRef ().getLocalNumEntries ();
868  }
869 
870  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
874  return getCrsGraphRef ().getGlobalNumRows ();
875  }
876 
877  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
881  return getCrsGraphRef ().getGlobalNumCols ();
882  }
883 
884  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
885  size_t
888  return getCrsGraphRef ().getLocalNumRows ();
889  }
890 
891 
892  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
893  size_t
896  return getCrsGraphRef ().getLocalNumCols ();
897  }
898 
899 
900  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
901  size_t
903  getNumEntriesInGlobalRow (GlobalOrdinal globalRow) const {
904  return getCrsGraphRef ().getNumEntriesInGlobalRow (globalRow);
905  }
906 
907  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
908  size_t
910  getNumEntriesInLocalRow (LocalOrdinal localRow) const {
911  return getCrsGraphRef ().getNumEntriesInLocalRow (localRow);
912  }
913 
914  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
915  size_t
918  return getCrsGraphRef ().getGlobalMaxNumRowEntries ();
919  }
920 
921  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
922  size_t
925  return getCrsGraphRef ().getLocalMaxNumRowEntries ();
926  }
927 
928  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
929  GlobalOrdinal
931  getIndexBase () const {
932  return getRowMap ()->getIndexBase ();
933  }
934 
935  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
936  Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
938  getRowMap () const {
939  return getCrsGraphRef ().getRowMap ();
940  }
941 
942  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
943  Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
945  getColMap () const {
946  return getCrsGraphRef ().getColMap ();
947  }
948 
949  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
950  Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
952  getDomainMap () const {
953  return getCrsGraphRef ().getDomainMap ();
954  }
955 
956  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
957  Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
959  getRangeMap () const {
960  return getCrsGraphRef ().getRangeMap ();
961  }
962 
963  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
964  Teuchos::RCP<const RowGraph<LocalOrdinal, GlobalOrdinal, Node> >
966  getGraph () const {
967  if (staticGraph_ != Teuchos::null) {
968  return staticGraph_;
969  }
970  return myGraph_;
971  }
972 
973  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
974  Teuchos::RCP<const CrsGraph<LocalOrdinal, GlobalOrdinal, Node> >
976  getCrsGraph () const {
977  if (staticGraph_ != Teuchos::null) {
978  return staticGraph_;
979  }
980  return myGraph_;
981  }
982 
983  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
986  getCrsGraphRef () const
987  {
988 #ifdef HAVE_TPETRA_DEBUG
989  constexpr bool debug = true;
990 #else
991  constexpr bool debug = false;
992 #endif // HAVE_TPETRA_DEBUG
993 
994  if (! this->staticGraph_.is_null ()) {
995  return * (this->staticGraph_);
996  }
997  else {
998  if (debug) {
999  const char tfecfFuncName[] = "getCrsGraphRef: ";
1000  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1001  (this->myGraph_.is_null (), std::logic_error,
1002  "Both staticGraph_ and myGraph_ are null. "
1003  "Please report this bug to the Tpetra developers.");
1004  }
1005  return * (this->myGraph_);
1006  }
1007  }
1008 
1009  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1010  typename CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_matrix_device_type
1013  {
1014  auto numCols = staticGraph_->getColMap()->getLocalNumElements();
1015  return local_matrix_device_type("Tpetra::CrsMatrix::lclMatrixDevice",
1016  numCols,
1017  valuesPacked_wdv.getDeviceView(Access::ReadWrite),
1018  staticGraph_->getLocalGraphDevice());
1019  }
1020 
1021  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1022  typename CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_matrix_host_type
1024  getLocalMatrixHost () const
1025  {
1026  auto numCols = staticGraph_->getColMap()->getLocalNumElements();
1027  return local_matrix_host_type("Tpetra::CrsMatrix::lclMatrixHost", numCols,
1028  valuesPacked_wdv.getHostView(Access::ReadWrite),
1029  staticGraph_->getLocalGraphHost());
1030  }
1031 
1032 #if KOKKOSKERNELS_VERSION < 40299
1033 // KDDKDD NOT SURE WHY THIS MUST RETURN A SHARED_PTR
1034  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1035  std::shared_ptr<typename CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_multiply_op_type>
1038  {
1039  auto localMatrix = getLocalMatrixDevice();
1040 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) || defined(KOKKOSKERNELS_ENABLE_TPL_ROCSPARSE) || defined(KOKKOSKERNELS_ENABLE_TPL_MKL)
1041  if(this->getLocalNumEntries() <= size_t(Teuchos::OrdinalTraits<LocalOrdinal>::max()))
1042  {
1043  if(this->ordinalRowptrs.data() == nullptr)
1044  {
1045  auto originalRowptrs = localMatrix.graph.row_map;
1046  //create LocalOrdinal-typed copy of the local graph's rowptrs.
1047  //This enables the LocalCrsMatrixOperator to use cuSPARSE SpMV.
1048  this->ordinalRowptrs = ordinal_rowptrs_type(
1049  Kokkos::ViewAllocateWithoutInitializing("CrsMatrix::ordinalRowptrs"), originalRowptrs.extent(0));
1050  auto ordinalRowptrs_ = this->ordinalRowptrs; //don't want to capture 'this'
1051  Kokkos::parallel_for("CrsMatrix::getLocalMultiplyOperator::convertRowptrs",
1052  Kokkos::RangePolicy<execution_space>(0, originalRowptrs.extent(0)),
1053  KOKKOS_LAMBDA(LocalOrdinal i)
1054  {
1055  ordinalRowptrs_(i) = originalRowptrs(i);
1056  });
1057  }
1058  //return local operator using ordinalRowptrs
1059  return std::make_shared<local_multiply_op_type>(
1060  std::make_shared<local_matrix_device_type>(localMatrix), this->ordinalRowptrs);
1061  }
1062 #endif
1063 // KDDKDD NOT SURE WHY THIS MUST RETURN A SHARED_PTR
1064  return std::make_shared<local_multiply_op_type>(
1065  std::make_shared<local_matrix_device_type>(localMatrix));
1066  }
1067 #endif
1068 
1069  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1070  bool
1072  isStaticGraph () const {
1073  return myGraph_.is_null ();
1074  }
1075 
1076  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1077  bool
1080  return true;
1081  }
1082 
1083  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1084  bool
1087  return true;
1088  }
1089 
1090  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1091  void
1093  allocateValues (ELocalGlobal lg, GraphAllocationStatus gas,
1094  const bool verbose)
1095  {
1096  using Details::Behavior;
1098  using std::endl;
1099  const char tfecfFuncName[] = "allocateValues: ";
1100  const char suffix[] =
1101  " Please report this bug to the Tpetra developers.";
1102  ProfilingRegion region("Tpetra::CrsMatrix::allocateValues");
1103 
1104  std::unique_ptr<std::string> prefix;
1105  if (verbose) {
1106  prefix = this->createPrefix("CrsMatrix", "allocateValues");
1107  std::ostringstream os;
1108  os << *prefix << "lg: "
1109  << (lg == LocalIndices ? "Local" : "Global") << "Indices"
1110  << ", gas: Graph"
1111  << (gas == GraphAlreadyAllocated ? "Already" : "NotYet")
1112  << "Allocated" << endl;
1113  std::cerr << os.str();
1114  }
1115 
1116  const bool debug = Behavior::debug("CrsMatrix");
1117  if (debug) {
1118  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1119  (this->staticGraph_.is_null (), std::logic_error,
1120  "staticGraph_ is null." << suffix);
1121 
1122  // If the graph indices are already allocated, then gas should be
1123  // GraphAlreadyAllocated. Otherwise, gas should be
1124  // GraphNotYetAllocated.
1125  if ((gas == GraphAlreadyAllocated) !=
1126  staticGraph_->indicesAreAllocated ()) {
1127  const char err1[] = "The caller has asserted that the graph "
1128  "is ";
1129  const char err2[] = "already allocated, but the static graph "
1130  "says that its indices are ";
1131  const char err3[] = "already allocated. ";
1132  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1133  (gas == GraphAlreadyAllocated &&
1134  ! staticGraph_->indicesAreAllocated (), std::logic_error,
1135  err1 << err2 << "not " << err3 << suffix);
1136  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1137  (gas != GraphAlreadyAllocated &&
1138  staticGraph_->indicesAreAllocated (), std::logic_error,
1139  err1 << "not " << err2 << err3 << suffix);
1140  }
1141 
1142  // If the graph is unallocated, then it had better be a
1143  // matrix-owned graph. ("Matrix-owned graph" means that the
1144  // matrix gets to define the graph structure. If the CrsMatrix
1145  // constructor that takes an RCP<const CrsGraph> was used, then
1146  // the matrix does _not_ own the graph.)
1147  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1148  (! this->staticGraph_->indicesAreAllocated () &&
1149  this->myGraph_.is_null (), std::logic_error,
1150  "The static graph says that its indices are not allocated, "
1151  "but the graph is not owned by the matrix." << suffix);
1152  }
1153 
1154  if (gas == GraphNotYetAllocated) {
1155  if (debug) {
1156  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1157  (this->myGraph_.is_null (), std::logic_error,
1158  "gas = GraphNotYetAllocated, but myGraph_ is null." << suffix);
1159  }
1160  try {
1161  this->myGraph_->allocateIndices (lg, verbose);
1162  }
1163  catch (std::exception& e) {
1164  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1165  (true, std::runtime_error, "CrsGraph::allocateIndices "
1166  "threw an exception: " << e.what ());
1167  }
1168  catch (...) {
1169  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1170  (true, std::runtime_error, "CrsGraph::allocateIndices "
1171  "threw an exception not a subclass of std::exception.");
1172  }
1173  }
1174 
1175  // Allocate matrix values.
1176  const size_t lclTotalNumEntries = this->staticGraph_->getLocalAllocationSize();
1177  if (debug) {
1178  const size_t lclNumRows = this->staticGraph_->getLocalNumRows ();
1179  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1180  (this->staticGraph_->getRowPtrsUnpackedHost()(lclNumRows) != lclTotalNumEntries, std::logic_error,
1181  "length of staticGraph's lclIndsUnpacked does not match final entry of rowPtrsUnapcked_host." << suffix);
1182  }
1183 
1184  // Allocate array of (packed???) matrix values.
1185  using values_type = typename local_matrix_device_type::values_type;
1186  if (verbose) {
1187  std::ostringstream os;
1188  os << *prefix << "Allocate values_wdv: Pre "
1189  << valuesUnpacked_wdv.extent(0) << ", post "
1190  << lclTotalNumEntries << endl;
1191  std::cerr << os.str();
1192  }
1193 // this->k_values1D_ =
1194  valuesUnpacked_wdv = values_wdv_type(
1195  values_type("Tpetra::CrsMatrix::values",
1196  lclTotalNumEntries));
1197  }
1198 
1199 
1200  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1201  void
1203  fillLocalGraphAndMatrix (const Teuchos::RCP<Teuchos::ParameterList>& params)
1204  {
1206  using ::Tpetra::Details::getEntryOnHost;
1207  using Teuchos::arcp_const_cast;
1208  using Teuchos::Array;
1209  using Teuchos::ArrayRCP;
1210  using Teuchos::null;
1211  using Teuchos::RCP;
1212  using Teuchos::rcp;
1213  using std::endl;
1214  using row_map_type = typename local_graph_device_type::row_map_type;
1215  using lclinds_1d_type = typename Graph::local_graph_device_type::entries_type::non_const_type;
1216  using values_type = typename local_matrix_device_type::values_type;
1217  Details::ProfilingRegion regionFLGAM
1218  ("Tpetra::CrsMatrix::fillLocalGraphAndMatrix");
1219 
1220  const char tfecfFuncName[] = "fillLocalGraphAndMatrix (called from "
1221  "fillComplete or expertStaticFillComplete): ";
1222  const char suffix[] =
1223  " Please report this bug to the Tpetra developers.";
1224  const bool debug = Details::Behavior::debug("CrsMatrix");
1225  const bool verbose = Details::Behavior::verbose("CrsMatrix");
1226 
1227  std::unique_ptr<std::string> prefix;
1228  if (verbose) {
1229  prefix = this->createPrefix("CrsMatrix", "fillLocalGraphAndMatrix");
1230  std::ostringstream os;
1231  os << *prefix << endl;
1232  std::cerr << os.str ();
1233  }
1234 
1235  if (debug) {
1236  // fillComplete() only calls fillLocalGraphAndMatrix() if the
1237  // matrix owns the graph, which means myGraph_ is not null.
1238  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1239  (myGraph_.is_null (), std::logic_error, "The nonconst graph "
1240  "(myGraph_) is null. This means that the matrix has a "
1241  "const (a.k.a. \"static\") graph. fillComplete or "
1242  "expertStaticFillComplete should never call "
1243  "fillLocalGraphAndMatrix in that case." << suffix);
1244  }
1245 
1246  const size_t lclNumRows = this->getLocalNumRows ();
1247 
1248  // This method's goal is to fill in the three arrays (compressed
1249  // sparse row format) that define the sparse graph's and matrix's
1250  // structure, and the sparse matrix's values.
1251  //
1252  // Get references to the data in myGraph_, so we can modify them
1253  // as well. Note that we only call fillLocalGraphAndMatrix() if
1254  // the matrix owns the graph, which means myGraph_ is not null.
1255 
1256  // NOTE: This does not work correctly w/ GCC 12.3 + CUDA due to a compiler bug.
1257  // See: https://github.com/trilinos/Trilinos/issues/12237
1258  //using row_entries_type = decltype (myGraph_->k_numRowEntries_);
1259  using row_entries_type = typename crs_graph_type::num_row_entries_type;
1260 
1261  typename Graph::local_graph_device_type::row_map_type curRowOffsets =
1262  myGraph_->rowPtrsUnpacked_dev_;
1263 
1264  if (debug) {
1265  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1266  (curRowOffsets.extent (0) == 0, std::logic_error,
1267  "curRowOffsets.extent(0) == 0.");
1268  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1269  (curRowOffsets.extent (0) != lclNumRows + 1, std::logic_error,
1270  "curRowOffsets.extent(0) = "
1271  << curRowOffsets.extent (0) << " != lclNumRows + 1 = "
1272  << (lclNumRows + 1) << ".");
1273  const size_t numOffsets = curRowOffsets.extent (0);
1274  const auto valToCheck = myGraph_->getRowPtrsUnpackedHost()(numOffsets - 1);
1275  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1276  (numOffsets != 0 &&
1277  myGraph_->lclIndsUnpacked_wdv.extent (0) != valToCheck,
1278  std::logic_error, "numOffsets = " <<
1279  numOffsets << " != 0 and myGraph_->lclIndsUnpacked_wdv.extent(0) = "
1280  << myGraph_->lclIndsUnpacked_wdv.extent (0) << " != curRowOffsets("
1281  << numOffsets << ") = " << valToCheck << ".");
1282  }
1283 
1284  if (myGraph_->getLocalNumEntries() !=
1285  myGraph_->getLocalAllocationSize()) {
1286 
1287  // Use the nonconst version of row_map_type for k_ptrs,
1288  // because row_map_type is const and we need to modify k_ptrs here.
1289  typename row_map_type::non_const_type k_ptrs;
1290  row_map_type k_ptrs_const;
1291  lclinds_1d_type k_inds;
1292  values_type k_vals;
1293 
1294  if (verbose) {
1295  std::ostringstream os;
1296  const auto numEnt = myGraph_->getLocalNumEntries();
1297  const auto allocSize = myGraph_->getLocalAllocationSize();
1298  os << *prefix << "Unpacked 1-D storage: numEnt=" << numEnt
1299  << ", allocSize=" << allocSize << endl;
1300  std::cerr << os.str ();
1301  }
1302  // The matrix's current 1-D storage is "unpacked." This means
1303  // the row offsets may differ from what the final row offsets
1304  // should be. This could happen, for example, if the user
1305  // set an upper
1306  // bound on the number of entries per row, but didn't fill all
1307  // those entries.
1308  if (debug && curRowOffsets.extent (0) != 0) {
1309  const size_t numOffsets =
1310  static_cast<size_t> (curRowOffsets.extent (0));
1311  const auto valToCheck = myGraph_->getRowPtrsUnpackedHost()(numOffsets - 1);
1312  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1313  (static_cast<size_t> (valToCheck) !=
1314  static_cast<size_t> (valuesUnpacked_wdv.extent (0)),
1315  std::logic_error, "(unpacked branch) Before "
1316  "allocating or packing, curRowOffsets(" << (numOffsets-1)
1317  << ") = " << valToCheck << " != valuesUnpacked_wdv.extent(0)"
1318  " = " << valuesUnpacked_wdv.extent (0) << ".");
1319  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1320  (static_cast<size_t> (valToCheck) !=
1321  static_cast<size_t> (myGraph_->lclIndsUnpacked_wdv.extent (0)),
1322  std::logic_error, "(unpacked branch) Before "
1323  "allocating or packing, curRowOffsets(" << (numOffsets-1)
1324  << ") = " << valToCheck
1325  << " != myGraph_->lclIndsUnpacked_wdv.extent(0) = "
1326  << myGraph_->lclIndsUnpacked_wdv.extent (0) << ".");
1327  }
1328  // Pack the row offsets into k_ptrs, by doing a sum-scan of
1329  // the array of valid entry counts per row.
1330 
1331  // Total number of entries in the matrix on the calling
1332  // process. We will compute this in the loop below. It's
1333  // cheap to compute and useful as a sanity check.
1334  size_t lclTotalNumEntries = 0;
1335  {
1336  // Allocate the packed row offsets array. We use a nonconst
1337  // temporary (packedRowOffsets) here, because k_ptrs is
1338  // const. We will assign packedRowOffsets to k_ptrs below.
1339  if (verbose) {
1340  std::ostringstream os;
1341  os << *prefix << "Allocate packed row offsets: "
1342  << (lclNumRows+1) << endl;
1343  std::cerr << os.str ();
1344  }
1345  typename row_map_type::non_const_type
1346  packedRowOffsets ("Tpetra::CrsGraph::ptr", lclNumRows + 1);
1347  typename row_entries_type::const_type numRowEnt_h =
1348  myGraph_->k_numRowEntries_;
1349  // We're computing offsets on device. This function can
1350  // handle numRowEnt_h being a host View.
1351  lclTotalNumEntries =
1352  computeOffsetsFromCounts (packedRowOffsets, numRowEnt_h);
1353  // packedRowOffsets is modifiable; k_ptrs isn't, so we have
1354  // to use packedRowOffsets in the loop above and assign here.
1355  k_ptrs = packedRowOffsets;
1356  k_ptrs_const = k_ptrs;
1357  }
1358 
1359  if (debug) {
1360  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1361  (static_cast<size_t> (k_ptrs.extent (0)) != lclNumRows + 1,
1362  std::logic_error,
1363  "(unpacked branch) After packing k_ptrs, "
1364  "k_ptrs.extent(0) = " << k_ptrs.extent (0) << " != "
1365  "lclNumRows+1 = " << (lclNumRows+1) << ".");
1366  const auto valToCheck = getEntryOnHost (k_ptrs, lclNumRows);
1367  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1368  (valToCheck != lclTotalNumEntries, std::logic_error,
1369  "(unpacked branch) After filling k_ptrs, "
1370  "k_ptrs(lclNumRows=" << lclNumRows << ") = " << valToCheck
1371  << " != total number of entries on the calling process = "
1372  << lclTotalNumEntries << ".");
1373  }
1374 
1375  // Allocate the arrays of packed column indices and values.
1376  if (verbose) {
1377  std::ostringstream os;
1378  os << *prefix << "Allocate packed local column indices: "
1379  << lclTotalNumEntries << endl;
1380  std::cerr << os.str ();
1381  }
1382  k_inds = lclinds_1d_type ("Tpetra::CrsGraph::lclInds", lclTotalNumEntries);
1383  if (verbose) {
1384  std::ostringstream os;
1385  os << *prefix << "Allocate packed values: "
1386  << lclTotalNumEntries << endl;
1387  std::cerr << os.str ();
1388  }
1389  k_vals = values_type ("Tpetra::CrsMatrix::values", lclTotalNumEntries);
1390 
1391  // curRowOffsets (myGraph_->rowPtrsUnpacked_) (???), lclIndsUnpacked_wdv,
1392  // and valuesUnpacked_wdv are currently unpacked. Pack them, using
1393  // the packed row offsets array k_ptrs that we created above.
1394  //
1395  // FIXME (mfh 06 Aug 2014) If "Optimize Storage" is false, we
1396  // need to keep around the unpacked row offsets, column
1397  // indices, and values arrays.
1398 
1399  // Pack the column indices from unpacked lclIndsUnpacked_wdv into
1400  // packed k_inds. We will replace lclIndsUnpacked_wdv below.
1401  using inds_packer_type = pack_functor<
1402  typename Graph::local_graph_device_type::entries_type::non_const_type,
1403  typename Graph::local_inds_dualv_type::t_dev::const_type,
1404  typename Graph::local_graph_device_type::row_map_type::non_const_type,
1405  typename Graph::local_graph_device_type::row_map_type>;
1406  inds_packer_type indsPacker (
1407  k_inds,
1408  myGraph_->lclIndsUnpacked_wdv.getDeviceView(Access::ReadOnly),
1409  k_ptrs, curRowOffsets);
1410  using exec_space = typename decltype (k_inds)::execution_space;
1411  using range_type = Kokkos::RangePolicy<exec_space, LocalOrdinal>;
1412  Kokkos::parallel_for
1413  ("Tpetra::CrsMatrix pack column indices",
1414  range_type (0, lclNumRows), indsPacker);
1415 
1416  // Pack the values from unpacked valuesUnpacked_wdv into packed
1417  // k_vals. We will replace valuesPacked_wdv below.
1418  using vals_packer_type = pack_functor<
1419  typename values_type::non_const_type,
1420  typename values_type::const_type,
1421  typename row_map_type::non_const_type,
1422  typename row_map_type::const_type>;
1423  vals_packer_type valsPacker (
1424  k_vals,
1425  this->valuesUnpacked_wdv.getDeviceView(Access::ReadOnly),
1426  k_ptrs, curRowOffsets);
1427  Kokkos::parallel_for ("Tpetra::CrsMatrix pack values",
1428  range_type (0, lclNumRows), valsPacker);
1429 
1430  if (debug) {
1431  const char myPrefix[] = "(\"Optimize Storage\""
1432  "=true branch) After packing, ";
1433  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1434  (k_ptrs.extent (0) == 0, std::logic_error, myPrefix
1435  << "k_ptrs.extent(0) = 0. This probably means that "
1436  "rowPtrsUnpacked_ was never allocated.");
1437  if (k_ptrs.extent (0) != 0) {
1438  const size_t numOffsets (k_ptrs.extent (0));
1439  const auto valToCheck =
1440  getEntryOnHost (k_ptrs, numOffsets - 1);
1441  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1442  (size_t (valToCheck) != k_vals.extent (0),
1443  std::logic_error, myPrefix <<
1444  "k_ptrs(" << (numOffsets-1) << ") = " << valToCheck <<
1445  " != k_vals.extent(0) = " << k_vals.extent (0) << ".");
1446  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1447  (size_t (valToCheck) != k_inds.extent (0),
1448  std::logic_error, myPrefix <<
1449  "k_ptrs(" << (numOffsets-1) << ") = " << valToCheck <<
1450  " != k_inds.extent(0) = " << k_inds.extent (0) << ".");
1451  }
1452  }
1453  // Build the local graph.
1454  myGraph_->setRowPtrsPacked(k_ptrs_const);
1455  myGraph_->lclIndsPacked_wdv =
1456  typename crs_graph_type::local_inds_wdv_type(k_inds);
1457  valuesPacked_wdv = values_wdv_type(k_vals);
1458  }
1459  else { // We don't have to pack, so just set the pointers.
1460  // FIXME KDDKDD https://github.com/trilinos/Trilinos/issues/9657
1461  // FIXME? This is already done in the graph fill call - need to avoid the memcpy to host
1462  myGraph_->rowPtrsPacked_dev_ = myGraph_->rowPtrsUnpacked_dev_;
1463  myGraph_->rowPtrsPacked_host_ = myGraph_->rowPtrsUnpacked_host_;
1464  myGraph_->packedUnpackedRowPtrsMatch_ = true;
1465  myGraph_->lclIndsPacked_wdv = myGraph_->lclIndsUnpacked_wdv;
1466  valuesPacked_wdv = valuesUnpacked_wdv;
1467 
1468  if (verbose) {
1469  std::ostringstream os;
1470  os << *prefix << "Storage already packed: rowPtrsUnpacked_: "
1471  << myGraph_->getRowPtrsUnpackedHost().extent(0) << ", lclIndsUnpacked_wdv: "
1472  << myGraph_->lclIndsUnpacked_wdv.extent(0) << ", valuesUnpacked_wdv: "
1473  << valuesUnpacked_wdv.extent(0) << endl;
1474  std::cerr << os.str();
1475  }
1476 
1477  if (debug) {
1478  const char myPrefix[] =
1479  "(\"Optimize Storage\"=false branch) ";
1480  auto rowPtrsUnpackedHost = myGraph_->getRowPtrsUnpackedHost();
1481  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1482  (myGraph_->rowPtrsUnpacked_dev_.extent (0) == 0, std::logic_error, myPrefix
1483  << "myGraph->rowPtrsUnpacked_dev_.extent(0) = 0. This probably means "
1484  "that rowPtrsUnpacked_ was never allocated.");
1485  if (myGraph_->rowPtrsUnpacked_dev_.extent (0) != 0) {
1486  const size_t numOffsets = rowPtrsUnpackedHost.extent (0);
1487  const auto valToCheck = rowPtrsUnpackedHost(numOffsets - 1);
1488  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1489  (size_t (valToCheck) != valuesPacked_wdv.extent (0),
1490  std::logic_error, myPrefix <<
1491  "k_ptrs_const(" << (numOffsets-1) << ") = " << valToCheck
1492  << " != valuesPacked_wdv.extent(0) = "
1493  << valuesPacked_wdv.extent (0) << ".");
1494  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1495  (size_t (valToCheck) != myGraph_->lclIndsPacked_wdv.extent (0),
1496  std::logic_error, myPrefix <<
1497  "k_ptrs_const(" << (numOffsets-1) << ") = " << valToCheck
1498  << " != myGraph_->lclIndsPacked.extent(0) = "
1499  << myGraph_->lclIndsPacked_wdv.extent (0) << ".");
1500  }
1501  }
1502  }
1503 
1504  if (debug) {
1505  const char myPrefix[] = "After packing, ";
1506  auto rowPtrsPackedHost = myGraph_->getRowPtrsPackedHost();
1507  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1508  (size_t (rowPtrsPackedHost.extent (0)) != size_t (lclNumRows + 1),
1509  std::logic_error, myPrefix << "myGraph_->rowPtrsPacked_host_.extent(0) = "
1510  << rowPtrsPackedHost.extent (0) << " != lclNumRows+1 = " <<
1511  (lclNumRows+1) << ".");
1512  if (rowPtrsPackedHost.extent (0) != 0) {
1513  const size_t numOffsets (rowPtrsPackedHost.extent (0));
1514  const size_t valToCheck = rowPtrsPackedHost(numOffsets-1);
1515  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1516  (valToCheck != size_t (valuesPacked_wdv.extent (0)),
1517  std::logic_error, myPrefix << "k_ptrs_const(" <<
1518  (numOffsets-1) << ") = " << valToCheck
1519  << " != valuesPacked_wdv.extent(0) = "
1520  << valuesPacked_wdv.extent (0) << ".");
1521  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1522  (valToCheck != size_t (myGraph_->lclIndsPacked_wdv.extent (0)),
1523  std::logic_error, myPrefix << "k_ptrs_const(" <<
1524  (numOffsets-1) << ") = " << valToCheck
1525  << " != myGraph_->lclIndsPacked_wdvk_inds.extent(0) = "
1526  << myGraph_->lclIndsPacked_wdv.extent (0) << ".");
1527  }
1528  }
1529 
1530  // May we ditch the old allocations for the packed (and otherwise
1531  // "optimized") allocations, later in this routine? Optimize
1532  // storage if the graph is not static, or if the graph already has
1533  // optimized storage.
1534  const bool defaultOptStorage =
1535  ! isStaticGraph () || staticGraph_->isStorageOptimized ();
1536  const bool requestOptimizedStorage =
1537  (! params.is_null () &&
1538  params->get ("Optimize Storage", defaultOptStorage)) ||
1539  (params.is_null () && defaultOptStorage);
1540 
1541  // The graph has optimized storage when indices are allocated,
1542  // myGraph_->k_numRowEntries_ is empty, and there are more than
1543  // zero rows on this process.
1544  if (requestOptimizedStorage) {
1545  // Free the old, unpacked, unoptimized allocations.
1546  // Free graph data structures that are only needed for
1547  // unpacked 1-D storage.
1548  if (verbose) {
1549  std::ostringstream os;
1550  os << *prefix << "Optimizing storage: free k_numRowEntries_: "
1551  << myGraph_->k_numRowEntries_.extent(0) << endl;
1552  std::cerr << os.str();
1553  }
1554 
1555  myGraph_->k_numRowEntries_ = row_entries_type ();
1556 
1557  // Keep the new 1-D packed allocations.
1558  // FIXME KDDKDD https://github.com/trilinos/Trilinos/issues/9657
1559  // We directly set the memory spaces to avoid a memcpy from device to host
1560  myGraph_->rowPtrsUnpacked_dev_ = myGraph_->rowPtrsPacked_dev_;
1561  myGraph_->rowPtrsUnpacked_host_ = myGraph_->rowPtrsPacked_host_;
1562  myGraph_->packedUnpackedRowPtrsMatch_ = true;
1563  myGraph_->lclIndsUnpacked_wdv = myGraph_->lclIndsPacked_wdv;
1564  valuesUnpacked_wdv = valuesPacked_wdv;
1565 
1566  myGraph_->storageStatus_ = Details::STORAGE_1D_PACKED;
1567  this->storageStatus_ = Details::STORAGE_1D_PACKED;
1568  }
1569  else {
1570  if (verbose) {
1571  std::ostringstream os;
1572  os << *prefix << "User requested NOT to optimize storage"
1573  << endl;
1574  std::cerr << os.str();
1575  }
1576  }
1577  }
1578 
1579  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1580  void
1582  fillLocalMatrix (const Teuchos::RCP<Teuchos::ParameterList>& params)
1583  {
1584  using ::Tpetra::Details::ProfilingRegion;
1585  using Teuchos::ArrayRCP;
1586  using Teuchos::Array;
1587  using Teuchos::null;
1588  using Teuchos::RCP;
1589  using Teuchos::rcp;
1590  using std::endl;
1591  using row_map_type = typename Graph::local_graph_device_type::row_map_type;
1592  using non_const_row_map_type = typename row_map_type::non_const_type;
1593  using values_type = typename local_matrix_device_type::values_type;
1594  ProfilingRegion regionFLM("Tpetra::CrsMatrix::fillLocalMatrix");
1595  const size_t lclNumRows = getLocalNumRows();
1596 
1597  const bool verbose = Details::Behavior::verbose("CrsMatrix");
1598  std::unique_ptr<std::string> prefix;
1599  if (verbose) {
1600  prefix = this->createPrefix("CrsMatrix", "fillLocalMatrix");
1601  std::ostringstream os;
1602  os << *prefix << "lclNumRows: " << lclNumRows << endl;
1603  std::cerr << os.str ();
1604  }
1605 
1606  // The goals of this routine are first, to allocate and fill
1607  // packed 1-D storage (see below for an explanation) in the vals
1608  // array, and second, to give vals to the local matrix and
1609  // finalize the local matrix. We only need k_ptrs, the packed 1-D
1610  // row offsets, within the scope of this routine, since we're only
1611  // filling the local matrix here (use fillLocalGraphAndMatrix() to
1612  // fill both the graph and the matrix at the same time).
1613 
1614  // get data from staticGraph_
1615  size_t nodeNumEntries = staticGraph_->getLocalNumEntries ();
1616  size_t nodeNumAllocated = staticGraph_->getLocalAllocationSize ();
1617  row_map_type k_rowPtrs = staticGraph_->rowPtrsPacked_dev_;
1618 
1619  row_map_type k_ptrs; // "packed" row offsets array
1620  values_type k_vals; // "packed" values array
1621 
1622  // May we ditch the old allocations for the packed (and otherwise
1623  // "optimized") allocations, later in this routine? Request
1624  // optimized storage by default.
1625  bool requestOptimizedStorage = true;
1626  const bool default_OptimizeStorage =
1627  ! isStaticGraph() || staticGraph_->isStorageOptimized();
1628  if (! params.is_null() &&
1629  ! params->get("Optimize Storage", default_OptimizeStorage)) {
1630  requestOptimizedStorage = false;
1631  }
1632  // If we're not allowed to change a static graph, then we can't
1633  // change the storage of the matrix, either. This means that if
1634  // the graph's storage isn't already optimized, we can't optimize
1635  // the matrix's storage either. Check and give warning, as
1636  // appropriate.
1637  if (! staticGraph_->isStorageOptimized () &&
1638  requestOptimizedStorage) {
1640  (true, std::runtime_error, "You requested optimized storage "
1641  "by setting the \"Optimize Storage\" flag to \"true\" in "
1642  "the ParameterList, or by virtue of default behavior. "
1643  "However, the associated CrsGraph was filled separately and "
1644  "requested not to optimize storage. Therefore, the "
1645  "CrsMatrix cannot optimize storage.");
1646  requestOptimizedStorage = false;
1647  }
1648 
1649  // NOTE: This does not work correctly w/ GCC 12.3 + CUDA due to a compiler bug.
1650  // See: https://github.com/trilinos/Trilinos/issues/12237
1651  //using row_entries_type = decltype (staticGraph_->k_numRowEntries_);
1652  using row_entries_type = typename crs_graph_type::num_row_entries_type;
1653 
1654  // The matrix's values are currently
1655  // stored in a 1-D format. However, this format is "unpacked";
1656  // it doesn't necessarily have the same row offsets as indicated
1657  // by the ptrs array returned by allocRowPtrs. This could
1658  // happen, for example, if the user
1659  // fixed the number of matrix entries in
1660  // each row, but didn't fill all those entries.
1661  //
1662  // As above, we don't need to keep the "packed" row offsets
1663  // array ptrs here, but we do need it here temporarily, so we
1664  // have to allocate it. We'll free ptrs later in this method.
1665  //
1666  // Note that this routine checks whether storage has already
1667  // been packed. This is a common case for solution of nonlinear
1668  // PDEs using the finite element method, as long as the
1669  // structure of the sparse matrix does not change between linear
1670  // solves.
1671  if (nodeNumEntries != nodeNumAllocated) {
1672  if (verbose) {
1673  std::ostringstream os;
1674  os << *prefix << "Unpacked 1-D storage: numEnt="
1675  << nodeNumEntries << ", allocSize=" << nodeNumAllocated
1676  << endl;
1677  std::cerr << os.str();
1678  }
1679  // We have to pack the 1-D storage, since the user didn't fill
1680  // up all requested storage.
1681  if (verbose) {
1682  std::ostringstream os;
1683  os << *prefix << "Allocate packed row offsets: "
1684  << (lclNumRows+1) << endl;
1685  std::cerr << os.str();
1686  }
1687  non_const_row_map_type tmpk_ptrs ("Tpetra::CrsGraph::ptr",
1688  lclNumRows+1);
1689  // Total number of entries in the matrix on the calling
1690  // process. We will compute this in the loop below. It's
1691  // cheap to compute and useful as a sanity check.
1692  size_t lclTotalNumEntries = 0;
1693  k_ptrs = tmpk_ptrs;
1694  {
1695  typename row_entries_type::const_type numRowEnt_h =
1696  staticGraph_->k_numRowEntries_;
1697  // This function can handle the counts being a host View.
1698  lclTotalNumEntries =
1699  Details::computeOffsetsFromCounts (tmpk_ptrs, numRowEnt_h);
1700  }
1701 
1702  // Allocate the "packed" values array.
1703  // It has exactly the right number of entries.
1704  if (verbose) {
1705  std::ostringstream os;
1706  os << *prefix << "Allocate packed values: "
1707  << lclTotalNumEntries << endl;
1708  std::cerr << os.str ();
1709  }
1710  k_vals = values_type ("Tpetra::CrsMatrix::val", lclTotalNumEntries);
1711 
1712  // Pack values_wdv into k_vals. We will replace values_wdv below.
1713  pack_functor<
1714  typename values_type::non_const_type,
1715  typename values_type::const_type,
1716  typename row_map_type::non_const_type,
1717  typename row_map_type::const_type> valsPacker
1718  (k_vals, valuesUnpacked_wdv.getDeviceView(Access::ReadOnly),
1719  tmpk_ptrs, k_rowPtrs);
1720 
1721  using exec_space = typename decltype (k_vals)::execution_space;
1722  using range_type = Kokkos::RangePolicy<exec_space, LocalOrdinal>;
1723  Kokkos::parallel_for ("Tpetra::CrsMatrix pack values",
1724  range_type (0, lclNumRows), valsPacker);
1725  valuesPacked_wdv = values_wdv_type(k_vals);
1726  }
1727  else { // We don't have to pack, so just set the pointer.
1728  valuesPacked_wdv = valuesUnpacked_wdv;
1729  if (verbose) {
1730  std::ostringstream os;
1731  os << *prefix << "Storage already packed: "
1732  << "valuesUnpacked_wdv: " << valuesUnpacked_wdv.extent(0) << endl;
1733  std::cerr << os.str();
1734  }
1735  }
1736 
1737  // May we ditch the old allocations for the packed one?
1738  if (requestOptimizedStorage) {
1739  // The user requested optimized storage, so we can dump the
1740  // unpacked 1-D storage, and keep the packed storage.
1741  valuesUnpacked_wdv = valuesPacked_wdv;
1742 // k_values1D_ = valuesPacked_wdv.getDeviceView(Access::ReadWrite);
1743  this->storageStatus_ = Details::STORAGE_1D_PACKED;
1744  }
1745  }
1746 
1747  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1748  void
1750  insertIndicesAndValues (crs_graph_type& graph,
1751  RowInfo& rowInfo,
1752  const typename crs_graph_type::SLocalGlobalViews& newInds,
1753  const Teuchos::ArrayView<impl_scalar_type>& oldRowVals,
1754  const Teuchos::ArrayView<const impl_scalar_type>& newRowVals,
1755  const ELocalGlobal lg,
1756  const ELocalGlobal I)
1757  {
1758  const size_t oldNumEnt = rowInfo.numEntries;
1759  const size_t numInserted = graph.insertIndices (rowInfo, newInds, lg, I);
1760 
1761  // Use of memcpy here works around an issue with GCC >= 4.9.0,
1762  // that probably relates to scalar_type vs. impl_scalar_type
1763  // aliasing. See history of Tpetra_CrsGraph_def.hpp for
1764  // details; look for GCC_WORKAROUND macro definition.
1765  if (numInserted > 0) {
1766  const size_t startOffset = oldNumEnt;
1767  memcpy (&oldRowVals[startOffset], &newRowVals[0],
1768  numInserted * sizeof (impl_scalar_type));
1769  }
1770  }
1771 
1772  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1773  void
1775  insertLocalValues (const LocalOrdinal lclRow,
1776  const Teuchos::ArrayView<const LocalOrdinal>& indices,
1777  const Teuchos::ArrayView<const Scalar>& values,
1778  const CombineMode CM)
1779  {
1780  using std::endl;
1781  const char tfecfFuncName[] = "insertLocalValues: ";
1782 
1783  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1784  (! this->isFillActive (), std::runtime_error,
1785  "Fill is not active. After calling fillComplete, you must call "
1786  "resumeFill before you may insert entries into the matrix again.");
1787  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1788  (this->isStaticGraph (), std::runtime_error,
1789  "Cannot insert indices with static graph; use replaceLocalValues() "
1790  "instead.");
1791  // At this point, we know that myGraph_ is nonnull.
1792  crs_graph_type& graph = * (this->myGraph_);
1793  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1794  (graph.colMap_.is_null (), std::runtime_error,
1795  "Cannot insert local indices without a column map.");
1796  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1797  (graph.isGloballyIndexed (),
1798  std::runtime_error, "Graph indices are global; use "
1799  "insertGlobalValues().");
1800  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1801  (values.size () != indices.size (), std::runtime_error,
1802  "values.size() = " << values.size ()
1803  << " != indices.size() = " << indices.size () << ".");
1804  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
1805  ! graph.rowMap_->isNodeLocalElement (lclRow), std::runtime_error,
1806  "Local row index " << lclRow << " does not belong to this process.");
1807 
1808  if (! graph.indicesAreAllocated ()) {
1809  // We only allocate values at most once per process, so it's OK
1810  // to check TPETRA_VERBOSE here.
1811  const bool verbose = Details::Behavior::verbose("CrsMatrix");
1812  this->allocateValues (LocalIndices, GraphNotYetAllocated, verbose);
1813  }
1814 
1815 #ifdef HAVE_TPETRA_DEBUG
1816  const size_t numEntriesToAdd = static_cast<size_t> (indices.size ());
1817  // In a debug build, test whether any of the given column indices
1818  // are not in the column Map. Keep track of the invalid column
1819  // indices so we can tell the user about them.
1820  {
1821  using Teuchos::toString;
1822 
1823  const map_type& colMap = * (graph.colMap_);
1824  Teuchos::Array<LocalOrdinal> badColInds;
1825  bool allInColMap = true;
1826  for (size_t k = 0; k < numEntriesToAdd; ++k) {
1827  if (! colMap.isNodeLocalElement (indices[k])) {
1828  allInColMap = false;
1829  badColInds.push_back (indices[k]);
1830  }
1831  }
1832  if (! allInColMap) {
1833  std::ostringstream os;
1834  os << "You attempted to insert entries in owned row " << lclRow
1835  << ", at the following column indices: " << toString (indices)
1836  << "." << endl;
1837  os << "Of those, the following indices are not in the column Map on "
1838  "this process: " << toString (badColInds) << "." << endl << "Since "
1839  "the matrix has a column Map already, it is invalid to insert "
1840  "entries at those locations.";
1841  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1842  (true, std::invalid_argument, os.str ());
1843  }
1844  }
1845 #endif // HAVE_TPETRA_DEBUG
1846 
1847  RowInfo rowInfo = graph.getRowInfo (lclRow);
1848 
1849  auto valsView = this->getValuesViewHostNonConst(rowInfo);
1850  if (CM == ADD) {
1851  auto fun = [&](size_t const k, size_t const /*start*/, size_t const offset) {
1852  valsView[offset] += values[k]; };
1853  std::function<void(size_t const, size_t const, size_t const)> cb(std::ref(fun));
1854  graph.insertLocalIndicesImpl(lclRow, indices, cb);
1855  } else if (CM == INSERT) {
1856  auto fun = [&](size_t const k, size_t const /*start*/, size_t const offset) {
1857  valsView[offset] = values[k]; };
1858  std::function<void(size_t const, size_t const, size_t const)> cb(std::ref(fun));
1859  graph.insertLocalIndicesImpl(lclRow, indices, cb);
1860  } else {
1861  std::ostringstream os;
1862  os << "You attempted to use insertLocalValues with CombineMode " << combineModeToString(CM)
1863  << "but this has not been implemented." << endl;
1864  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1865  (true, std::invalid_argument, os.str ());
1866  }
1867  }
1868 
1869  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1870  void
1872  insertLocalValues (const LocalOrdinal localRow,
1873  const LocalOrdinal numEnt,
1874  const Scalar vals[],
1875  const LocalOrdinal cols[],
1876  const CombineMode CM)
1877  {
1878  Teuchos::ArrayView<const LocalOrdinal> colsT (cols, numEnt);
1879  Teuchos::ArrayView<const Scalar> valsT (vals, numEnt);
1880  this->insertLocalValues (localRow, colsT, valsT, CM);
1881  }
1882 
1883  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1884  void
1887  RowInfo& rowInfo,
1888  const GlobalOrdinal gblColInds[],
1889  const impl_scalar_type vals[],
1890  const size_t numInputEnt)
1891  {
1892 #ifdef HAVE_TPETRA_DEBUG
1893  const char tfecfFuncName[] = "insertGlobalValuesImpl: ";
1894  const size_t origNumEnt = graph.getNumEntriesInLocalRow (rowInfo.localRow);
1895  const size_t curNumEnt = rowInfo.numEntries;
1896 #endif // HAVE_TPETRA_DEBUG
1897 
1898  if (! graph.indicesAreAllocated ()) {
1899  // We only allocate values at most once per process, so it's OK
1900  // to check TPETRA_VERBOSE here.
1901  using ::Tpetra::Details::Behavior;
1902  const bool verbose = Behavior::verbose("CrsMatrix");
1903  this->allocateValues (GlobalIndices, GraphNotYetAllocated, verbose);
1904  // mfh 23 Jul 2017: allocateValues invalidates existing
1905  // getRowInfo results. Once we get rid of lazy graph
1906  // allocation, we'll be able to move the getRowInfo call outside
1907  // of this method.
1908  rowInfo = graph.getRowInfo (rowInfo.localRow);
1909  }
1910 
1911  auto valsView = this->getValuesViewHostNonConst(rowInfo);
1912  auto fun = [&](size_t const k, size_t const /*start*/, size_t const offset){
1913  valsView[offset] += vals[k];
1914  };
1915  std::function<void(size_t const, size_t const, size_t const)> cb(std::ref(fun));
1916 #ifdef HAVE_TPETRA_DEBUG
1917  //numInserted is only used inside the debug code below.
1918  auto numInserted =
1919 #endif
1920  graph.insertGlobalIndicesImpl(rowInfo, gblColInds, numInputEnt, cb);
1921 
1922 #ifdef HAVE_TPETRA_DEBUG
1923  size_t newNumEnt = curNumEnt + numInserted;
1924  const size_t chkNewNumEnt =
1925  graph.getNumEntriesInLocalRow (rowInfo.localRow);
1926  if (chkNewNumEnt != newNumEnt) {
1927  std::ostringstream os;
1928  os << std::endl << "newNumEnt = " << newNumEnt
1929  << " != graph.getNumEntriesInLocalRow(" << rowInfo.localRow
1930  << ") = " << chkNewNumEnt << "." << std::endl
1931  << "\torigNumEnt: " << origNumEnt << std::endl
1932  << "\tnumInputEnt: " << numInputEnt << std::endl
1933  << "\tgblColInds: [";
1934  for (size_t k = 0; k < numInputEnt; ++k) {
1935  os << gblColInds[k];
1936  if (k + size_t (1) < numInputEnt) {
1937  os << ",";
1938  }
1939  }
1940  os << "]" << std::endl
1941  << "\tvals: [";
1942  for (size_t k = 0; k < numInputEnt; ++k) {
1943  os << vals[k];
1944  if (k + size_t (1) < numInputEnt) {
1945  os << ",";
1946  }
1947  }
1948  os << "]" << std::endl;
1949 
1950  if (this->supportsRowViews ()) {
1951  values_host_view_type vals2;
1952  if (this->isGloballyIndexed ()) {
1953  global_inds_host_view_type gblColInds2;
1954  const GlobalOrdinal gblRow =
1955  graph.rowMap_->getGlobalElement (rowInfo.localRow);
1956  if (gblRow ==
1957  Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()) {
1958  os << "Local row index " << rowInfo.localRow << " is invalid!"
1959  << std::endl;
1960  }
1961  else {
1962  bool getViewThrew = false;
1963  try {
1964  this->getGlobalRowView (gblRow, gblColInds2, vals2);
1965  }
1966  catch (std::exception& e) {
1967  getViewThrew = true;
1968  os << "getGlobalRowView threw exception:" << std::endl
1969  << e.what () << std::endl;
1970  }
1971  if (! getViewThrew) {
1972  os << "\tNew global column indices: ";
1973  for (size_t jjj = 0; jjj < gblColInds2.extent(0); jjj++)
1974  os << gblColInds2[jjj] << " ";
1975  os << std::endl;
1976  os << "\tNew values: ";
1977  for (size_t jjj = 0; jjj < vals2.extent(0); jjj++)
1978  os << vals2[jjj] << " ";
1979  os << std::endl;
1980  }
1981  }
1982  }
1983  else if (this->isLocallyIndexed ()) {
1984  local_inds_host_view_type lclColInds2;
1985  this->getLocalRowView (rowInfo.localRow, lclColInds2, vals2);
1986  os << "\tNew local column indices: ";
1987  for (size_t jjj = 0; jjj < lclColInds2.extent(0); jjj++)
1988  os << lclColInds2[jjj] << " ";
1989  os << std::endl;
1990  os << "\tNew values: ";
1991  for (size_t jjj = 0; jjj < vals2.extent(0); jjj++)
1992  os << vals2[jjj] << " ";
1993  os << std::endl;
1994  }
1995  }
1996 
1997  os << "Please report this bug to the Tpetra developers.";
1998  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
1999  (true, std::logic_error, os.str ());
2000  }
2001 #endif // HAVE_TPETRA_DEBUG
2002  }
2003 
2004  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2005  void
2007  insertGlobalValues (const GlobalOrdinal gblRow,
2008  const Teuchos::ArrayView<const GlobalOrdinal>& indices,
2009  const Teuchos::ArrayView<const Scalar>& values)
2010  {
2011  using Teuchos::toString;
2012  using std::endl;
2013  typedef impl_scalar_type IST;
2014  typedef LocalOrdinal LO;
2015  typedef GlobalOrdinal GO;
2016  typedef Tpetra::Details::OrdinalTraits<LO> OTLO;
2017  typedef typename Teuchos::ArrayView<const GO>::size_type size_type;
2018  const char tfecfFuncName[] = "insertGlobalValues: ";
2019 
2020 #ifdef HAVE_TPETRA_DEBUG
2021  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2022  (values.size () != indices.size (), std::runtime_error,
2023  "values.size() = " << values.size () << " != indices.size() = "
2024  << indices.size () << ".");
2025 #endif // HAVE_TPETRA_DEBUG
2026 
2027  // getRowMap() is not thread safe, because it increments RCP's
2028  // reference count. getCrsGraphRef() is thread safe.
2029  const map_type& rowMap = * (this->getCrsGraphRef ().rowMap_);
2030  const LO lclRow = rowMap.getLocalElement (gblRow);
2031 
2032  if (lclRow == OTLO::invalid ()) {
2033  // Input row is _not_ owned by the calling process.
2034  //
2035  // See a note (now deleted) from mfh 14 Dec 2012: If input row
2036  // is not in the row Map, it doesn't matter whether or not the
2037  // graph is static; the data just get stashed for later use by
2038  // globalAssemble().
2039  this->insertNonownedGlobalValues (gblRow, indices, values);
2040  }
2041  else { // Input row _is_ owned by the calling process
2042  if (this->isStaticGraph ()) {
2043  // Uh oh! Not allowed to insert into owned rows in that case.
2044  const int myRank = rowMap.getComm ()->getRank ();
2045  const int numProcs = rowMap.getComm ()->getSize ();
2046  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2047  (true, std::runtime_error,
2048  "The matrix was constructed with a constant (\"static\") graph, "
2049  "yet the given global row index " << gblRow << " is in the row "
2050  "Map on the calling process (with rank " << myRank << ", of " <<
2051  numProcs << " process(es)). In this case, you may not insert "
2052  "new entries into rows owned by the calling process.");
2053  }
2054 
2055  crs_graph_type& graph = * (this->myGraph_);
2056  const IST* const inputVals =
2057  reinterpret_cast<const IST*> (values.getRawPtr ());
2058  const GO* const inputGblColInds = indices.getRawPtr ();
2059  const size_t numInputEnt = indices.size ();
2060  RowInfo rowInfo = graph.getRowInfo (lclRow);
2061 
2062  // If the matrix has a column Map, check at this point whether
2063  // the column indices belong to the column Map.
2064  //
2065  // FIXME (mfh 16 May 2013) We may want to consider deferring the
2066  // test to the CrsGraph method, since it may have to do this
2067  // anyway.
2068  if (! graph.colMap_.is_null ()) {
2069  const map_type& colMap = * (graph.colMap_);
2070  // In a debug build, keep track of the nonowned ("bad") column
2071  // indices, so that we can display them in the exception
2072  // message. In a release build, just ditch the loop early if
2073  // we encounter a nonowned column index.
2074 #ifdef HAVE_TPETRA_DEBUG
2075  Teuchos::Array<GO> badColInds;
2076 #endif // HAVE_TPETRA_DEBUG
2077  const size_type numEntriesToInsert = indices.size ();
2078  bool allInColMap = true;
2079  for (size_type k = 0; k < numEntriesToInsert; ++k) {
2080  if (! colMap.isNodeGlobalElement (indices[k])) {
2081  allInColMap = false;
2082 #ifdef HAVE_TPETRA_DEBUG
2083  badColInds.push_back (indices[k]);
2084 #else
2085  break;
2086 #endif // HAVE_TPETRA_DEBUG
2087  }
2088  }
2089  if (! allInColMap) {
2090  std::ostringstream os;
2091  os << "You attempted to insert entries in owned row " << gblRow
2092  << ", at the following column indices: " << toString (indices)
2093  << "." << endl;
2094 #ifdef HAVE_TPETRA_DEBUG
2095  os << "Of those, the following indices are not in the column Map "
2096  "on this process: " << toString (badColInds) << "." << endl
2097  << "Since the matrix has a column Map already, it is invalid "
2098  "to insert entries at those locations.";
2099 #else
2100  os << "At least one of those indices is not in the column Map "
2101  "on this process." << endl << "It is invalid to insert into "
2102  "columns not in the column Map on the process that owns the "
2103  "row.";
2104 #endif // HAVE_TPETRA_DEBUG
2105  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2106  (true, std::invalid_argument, os.str ());
2107  }
2108  }
2109 
2110  this->insertGlobalValuesImpl (graph, rowInfo, inputGblColInds,
2111  inputVals, numInputEnt);
2112  }
2113  }
2114 
2115 
2116  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2117  void
2119  insertGlobalValues (const GlobalOrdinal globalRow,
2120  const LocalOrdinal numEnt,
2121  const Scalar vals[],
2122  const GlobalOrdinal inds[])
2123  {
2124  Teuchos::ArrayView<const GlobalOrdinal> indsT (inds, numEnt);
2125  Teuchos::ArrayView<const Scalar> valsT (vals, numEnt);
2126  this->insertGlobalValues (globalRow, indsT, valsT);
2127  }
2128 
2129 
2130  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2131  void
2134  const GlobalOrdinal gblRow,
2135  const Teuchos::ArrayView<const GlobalOrdinal>& indices,
2136  const Teuchos::ArrayView<const Scalar>& values,
2137  const bool debug)
2138  {
2139  typedef impl_scalar_type IST;
2140  typedef LocalOrdinal LO;
2141  typedef GlobalOrdinal GO;
2142  typedef Tpetra::Details::OrdinalTraits<LO> OTLO;
2143  const char tfecfFuncName[] = "insertGlobalValuesFiltered: ";
2144 
2145  if (debug) {
2146  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2147  (values.size () != indices.size (), std::runtime_error,
2148  "values.size() = " << values.size () << " != indices.size() = "
2149  << indices.size () << ".");
2150  }
2151 
2152  // getRowMap() is not thread safe, because it increments RCP's
2153  // reference count. getCrsGraphRef() is thread safe.
2154  const map_type& rowMap = * (this->getCrsGraphRef ().rowMap_);
2155  const LO lclRow = rowMap.getLocalElement (gblRow);
2156  if (lclRow == OTLO::invalid ()) {
2157  // Input row is _not_ owned by the calling process.
2158  //
2159  // See a note (now deleted) from mfh 14 Dec 2012: If input row
2160  // is not in the row Map, it doesn't matter whether or not the
2161  // graph is static; the data just get stashed for later use by
2162  // globalAssemble().
2163  this->insertNonownedGlobalValues (gblRow, indices, values);
2164  }
2165  else { // Input row _is_ owned by the calling process
2166  if (this->isStaticGraph ()) {
2167  // Uh oh! Not allowed to insert into owned rows in that case.
2168  const int myRank = rowMap.getComm ()->getRank ();
2169  const int numProcs = rowMap.getComm ()->getSize ();
2170  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2171  (true, std::runtime_error,
2172  "The matrix was constructed with a constant (\"static\") graph, "
2173  "yet the given global row index " << gblRow << " is in the row "
2174  "Map on the calling process (with rank " << myRank << ", of " <<
2175  numProcs << " process(es)). In this case, you may not insert "
2176  "new entries into rows owned by the calling process.");
2177  }
2178 
2179  crs_graph_type& graph = * (this->myGraph_);
2180  const IST* const inputVals =
2181  reinterpret_cast<const IST*> (values.getRawPtr ());
2182  const GO* const inputGblColInds = indices.getRawPtr ();
2183  const size_t numInputEnt = indices.size ();
2184  RowInfo rowInfo = graph.getRowInfo (lclRow);
2185 
2186  if (!graph.colMap_.is_null() && graph.isLocallyIndexed()) {
2187  // This branch is similar in function to the following branch, but for
2188  // the special case that the target graph is locally indexed.
2189  // In this case, we cannot simply filter
2190  // out global indices that don't exist on the receiving process and
2191  // insert the remaining (global) indices, but we must convert them (the
2192  // remaining global indices) to local and call `insertLocalValues`.
2193  const map_type& colMap = * (graph.colMap_);
2194  size_t curOffset = 0;
2195  while (curOffset < numInputEnt) {
2196  // Find a sequence of input indices that are in the column Map on the
2197  // calling process. Doing a sequence at a time, instead of one at a
2198  // time, amortizes some overhead.
2199  Teuchos::Array<LO> lclIndices;
2200  size_t endOffset = curOffset;
2201  for ( ; endOffset < numInputEnt; ++endOffset) {
2202  auto lclIndex = colMap.getLocalElement(inputGblColInds[endOffset]);
2203  if (lclIndex != OTLO::invalid())
2204  lclIndices.push_back(lclIndex);
2205  else
2206  break;
2207  }
2208  // curOffset, endOffset: half-exclusive range of indices in the column
2209  // Map on the calling process. If endOffset == curOffset, the range is
2210  // empty.
2211  const LO numIndInSeq = (endOffset - curOffset);
2212  if (numIndInSeq != 0) {
2213  this->insertLocalValues(lclRow, lclIndices(), values(curOffset, numIndInSeq));
2214  }
2215  // Invariant before the increment line: Either endOffset ==
2216  // numInputEnt, or inputGblColInds[endOffset] is not in the column Map
2217  // on the calling process.
2218  if (debug) {
2219  const bool invariant = endOffset == numInputEnt ||
2220  colMap.getLocalElement (inputGblColInds[endOffset]) == OTLO::invalid ();
2221  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2222  (! invariant, std::logic_error, std::endl << "Invariant failed!");
2223  }
2224  curOffset = endOffset + 1;
2225  }
2226  }
2227  else if (! graph.colMap_.is_null ()) { // We have a column Map.
2228  const map_type& colMap = * (graph.colMap_);
2229  size_t curOffset = 0;
2230  while (curOffset < numInputEnt) {
2231  // Find a sequence of input indices that are in the column
2232  // Map on the calling process. Doing a sequence at a time,
2233  // instead of one at a time, amortizes some overhead.
2234  size_t endOffset = curOffset;
2235  for ( ; endOffset < numInputEnt &&
2236  colMap.getLocalElement (inputGblColInds[endOffset]) != OTLO::invalid ();
2237  ++endOffset)
2238  {}
2239  // curOffset, endOffset: half-exclusive range of indices in
2240  // the column Map on the calling process. If endOffset ==
2241  // curOffset, the range is empty.
2242  const LO numIndInSeq = (endOffset - curOffset);
2243  if (numIndInSeq != 0) {
2244  rowInfo = graph.getRowInfo(lclRow); // KDD 5/19 Need fresh RowInfo in each loop iteration
2245  this->insertGlobalValuesImpl (graph, rowInfo,
2246  inputGblColInds + curOffset,
2247  inputVals + curOffset,
2248  numIndInSeq);
2249  }
2250  // Invariant before the increment line: Either endOffset ==
2251  // numInputEnt, or inputGblColInds[endOffset] is not in the
2252  // column Map on the calling process.
2253  if (debug) {
2254  const bool invariant = endOffset == numInputEnt ||
2255  colMap.getLocalElement (inputGblColInds[endOffset]) == OTLO::invalid ();
2256  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
2257  (! invariant, std::logic_error, std::endl << "Invariant failed!");
2258  }
2259  curOffset = endOffset + 1;
2260  }
2261  }
2262  else { // we don't have a column Map.
2263  this->insertGlobalValuesImpl (graph, rowInfo, inputGblColInds,
2264  inputVals, numInputEnt);
2265  }
2266  }
2267  }
2268 
2269  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2270  void
2271  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
2272  insertGlobalValuesFilteredChecked(
2273  const GlobalOrdinal gblRow,
2274  const Teuchos::ArrayView<const GlobalOrdinal>& indices,
2275  const Teuchos::ArrayView<const Scalar>& values,
2276  const char* const prefix,
2277  const bool debug,
2278  const bool verbose)
2279  {
2281  using std::endl;
2282 
2283  try {
2284  insertGlobalValuesFiltered(gblRow, indices, values, debug);
2285  }
2286  catch(std::exception& e) {
2287  std::ostringstream os;
2288  if (verbose) {
2289  const size_t maxNumToPrint =
2291  os << *prefix << ": insertGlobalValuesFiltered threw an "
2292  "exception: " << e.what() << endl
2293  << "Global row index: " << gblRow << endl;
2294  verbosePrintArray(os, indices, "Global column indices",
2295  maxNumToPrint);
2296  os << endl;
2297  verbosePrintArray(os, values, "Values", maxNumToPrint);
2298  os << endl;
2299  }
2300  else {
2301  os << ": insertGlobalValuesFiltered threw an exception: "
2302  << e.what();
2303  }
2304  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, os.str());
2305  }
2306  }
2307 
2308  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2309  LocalOrdinal
2312  const crs_graph_type& graph,
2313  const RowInfo& rowInfo,
2314  const LocalOrdinal inds[],
2315  const impl_scalar_type newVals[],
2316  const LocalOrdinal numElts)
2317  {
2318  typedef LocalOrdinal LO;
2319  typedef GlobalOrdinal GO;
2320  const bool sorted = graph.isSorted ();
2321 
2322  size_t hint = 0; // Guess for the current index k into rowVals
2323  LO numValid = 0; // number of valid local column indices
2324 
2325  if (graph.isLocallyIndexed ()) {
2326  // Get a view of the column indices in the row. This amortizes
2327  // the cost of getting the view over all the entries of inds.
2328  auto colInds = graph.getLocalIndsViewHost (rowInfo);
2329 
2330  for (LO j = 0; j < numElts; ++j) {
2331  const LO lclColInd = inds[j];
2332  const size_t offset =
2333  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2334  lclColInd, hint, sorted);
2335  if (offset != rowInfo.numEntries) {
2336  rowVals[offset] = newVals[j];
2337  hint = offset + 1;
2338  ++numValid;
2339  }
2340  }
2341  }
2342  else if (graph.isGloballyIndexed ()) {
2343  if (graph.colMap_.is_null ()) {
2344  return Teuchos::OrdinalTraits<LO>::invalid ();
2345  }
2346  const map_type colMap = * (graph.colMap_);
2347 
2348  // Get a view of the column indices in the row. This amortizes
2349  // the cost of getting the view over all the entries of inds.
2350  auto colInds = graph.getGlobalIndsViewHost (rowInfo);
2351 
2352  for (LO j = 0; j < numElts; ++j) {
2353  const GO gblColInd = colMap.getGlobalElement (inds[j]);
2354  if (gblColInd != Teuchos::OrdinalTraits<GO>::invalid ()) {
2355  const size_t offset =
2356  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2357  gblColInd, hint, sorted);
2358  if (offset != rowInfo.numEntries) {
2359  rowVals[offset] = newVals[j];
2360  hint = offset + 1;
2361  ++numValid;
2362  }
2363  }
2364  }
2365  }
2366  // NOTE (mfh 26 Jun 2014, 26 Nov 2015) In the current version of
2367  // CrsGraph and CrsMatrix, it's possible for a matrix (or graph)
2368  // to be neither locally nor globally indexed on a process.
2369  // This means that the graph or matrix has no entries on that
2370  // process. Epetra also works like this. It's related to lazy
2371  // allocation (on first insertion, not at graph / matrix
2372  // construction). Lazy allocation will go away because it is
2373  // not thread scalable.
2374 
2375  return numValid;
2376  }
2377 
2378  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2379  LocalOrdinal
2381  replaceLocalValues (const LocalOrdinal localRow,
2382  const Teuchos::ArrayView<const LocalOrdinal>& lclCols,
2383  const Teuchos::ArrayView<const Scalar>& vals)
2384  {
2385  typedef LocalOrdinal LO;
2386 
2387  const LO numInputEnt = static_cast<LO> (lclCols.size ());
2388  if (static_cast<LO> (vals.size ()) != numInputEnt) {
2389  return Teuchos::OrdinalTraits<LO>::invalid ();
2390  }
2391  const LO* const inputInds = lclCols.getRawPtr ();
2392  const Scalar* const inputVals = vals.getRawPtr ();
2393  return this->replaceLocalValues (localRow, numInputEnt,
2394  inputVals, inputInds);
2395  }
2396 
2397  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2399  local_ordinal_type
2402  const local_ordinal_type localRow,
2403  const Kokkos::View<const local_ordinal_type*, Kokkos::AnonymousSpace>& inputInds,
2404  const Kokkos::View<const impl_scalar_type*, Kokkos::AnonymousSpace>& inputVals)
2405  {
2406  using LO = local_ordinal_type;
2407  const LO numInputEnt = inputInds.extent(0);
2408  if (numInputEnt != static_cast<LO>(inputVals.extent(0))) {
2409  return Teuchos::OrdinalTraits<LO>::invalid();
2410  }
2411  const Scalar* const inVals =
2412  reinterpret_cast<const Scalar*>(inputVals.data());
2413  return this->replaceLocalValues(localRow, numInputEnt,
2414  inVals, inputInds.data());
2415  }
2416 
2417  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2418  LocalOrdinal
2420  replaceLocalValues (const LocalOrdinal localRow,
2421  const LocalOrdinal numEnt,
2422  const Scalar inputVals[],
2423  const LocalOrdinal inputCols[])
2424  {
2425  typedef impl_scalar_type IST;
2426  typedef LocalOrdinal LO;
2427 
2428  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
2429  // Fill must be active and the "nonconst" graph must exist.
2430  return Teuchos::OrdinalTraits<LO>::invalid ();
2431  }
2432  const crs_graph_type& graph = * (this->staticGraph_);
2433  const RowInfo rowInfo = graph.getRowInfo (localRow);
2434 
2435  if (rowInfo.localRow == Teuchos::OrdinalTraits<size_t>::invalid ()) {
2436  // The calling process does not own this row, so it is not
2437  // allowed to modify its values.
2438  return static_cast<LO> (0);
2439  }
2440  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
2441  const IST* const inVals = reinterpret_cast<const IST*> (inputVals);
2442  return this->replaceLocalValuesImpl (curRowVals.data (), graph, rowInfo,
2443  inputCols, inVals, numEnt);
2444  }
2445 
2446  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2447  LocalOrdinal
2450  const crs_graph_type& graph,
2451  const RowInfo& rowInfo,
2452  const GlobalOrdinal inds[],
2453  const impl_scalar_type newVals[],
2454  const LocalOrdinal numElts)
2455  {
2456  Teuchos::ArrayView<const GlobalOrdinal> indsT(inds, numElts);
2457  auto fun =
2458  [&](size_t const k, size_t const /*start*/, size_t const offset) {
2459  rowVals[offset] = newVals[k];
2460  };
2461  std::function<void(size_t const, size_t const, size_t const)> cb(std::ref(fun));
2462  return graph.findGlobalIndices(rowInfo, indsT, cb);
2463  }
2464 
2465  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2466  LocalOrdinal
2468  replaceGlobalValues (const GlobalOrdinal globalRow,
2469  const Teuchos::ArrayView<const GlobalOrdinal>& inputGblColInds,
2470  const Teuchos::ArrayView<const Scalar>& inputVals)
2471  {
2472  typedef LocalOrdinal LO;
2473 
2474  const LO numInputEnt = static_cast<LO> (inputGblColInds.size ());
2475  if (static_cast<LO> (inputVals.size ()) != numInputEnt) {
2476  return Teuchos::OrdinalTraits<LO>::invalid ();
2477  }
2478  return this->replaceGlobalValues (globalRow, numInputEnt,
2479  inputVals.getRawPtr (),
2480  inputGblColInds.getRawPtr ());
2481  }
2482 
2483  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2484  LocalOrdinal
2486  replaceGlobalValues (const GlobalOrdinal globalRow,
2487  const LocalOrdinal numEnt,
2488  const Scalar inputVals[],
2489  const GlobalOrdinal inputGblColInds[])
2490  {
2491  typedef impl_scalar_type IST;
2492  typedef LocalOrdinal LO;
2493 
2494  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
2495  // Fill must be active and the "nonconst" graph must exist.
2496  return Teuchos::OrdinalTraits<LO>::invalid ();
2497  }
2498  const crs_graph_type& graph = * (this->staticGraph_);
2499 
2500  const RowInfo rowInfo = graph.getRowInfoFromGlobalRowIndex (globalRow);
2501  if (rowInfo.localRow == Teuchos::OrdinalTraits<size_t>::invalid ()) {
2502  // The input local row is invalid on the calling process,
2503  // which means that the calling process summed 0 entries.
2504  return static_cast<LO> (0);
2505  }
2506 
2507  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
2508  const IST* const inVals = reinterpret_cast<const IST*> (inputVals);
2509  return this->replaceGlobalValuesImpl (curRowVals.data (), graph, rowInfo,
2510  inputGblColInds, inVals, numEnt);
2511  }
2512 
2513  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2515  local_ordinal_type
2518  const global_ordinal_type globalRow,
2519  const Kokkos::View<const global_ordinal_type*, Kokkos::AnonymousSpace>& inputInds,
2520  const Kokkos::View<const impl_scalar_type*, Kokkos::AnonymousSpace>& inputVals)
2521  {
2522  // We use static_assert here to check the template parameters,
2523  // rather than std::enable_if (e.g., on the return value, to
2524  // enable compilation only if the template parameters match the
2525  // desired attributes). This turns obscure link errors into
2526  // clear compilation errors. It also makes the return value a
2527  // lot easier to see.
2528  using LO = local_ordinal_type;
2529  const LO numInputEnt = static_cast<LO>(inputInds.extent(0));
2530  if (static_cast<LO>(inputVals.extent(0)) != numInputEnt) {
2531  return Teuchos::OrdinalTraits<LO>::invalid();
2532  }
2533  const Scalar* const inVals =
2534  reinterpret_cast<const Scalar*>(inputVals.data());
2535  return this->replaceGlobalValues(globalRow, numInputEnt, inVals,
2536  inputInds.data());
2537  }
2538 
2539  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2540  LocalOrdinal
2543  const crs_graph_type& graph,
2544  const RowInfo& rowInfo,
2545  const GlobalOrdinal inds[],
2546  const impl_scalar_type newVals[],
2547  const LocalOrdinal numElts,
2548  const bool atomic)
2549  {
2550  typedef LocalOrdinal LO;
2551  typedef GlobalOrdinal GO;
2552 
2553  const bool sorted = graph.isSorted ();
2554 
2555  size_t hint = 0; // guess at the index's relative offset in the row
2556  LO numValid = 0; // number of valid input column indices
2557 
2558  if (graph.isLocallyIndexed ()) {
2559  // NOTE (mfh 04 Nov 2015) Dereferencing an RCP or reading its
2560  // pointer does NOT change its reference count. Thus, this
2561  // code is still thread safe.
2562  if (graph.colMap_.is_null ()) {
2563  // NO input column indices are valid in this case, since if
2564  // the column Map is null on the calling process, then the
2565  // calling process owns no graph entries.
2566  return numValid;
2567  }
2568  const map_type& colMap = * (graph.colMap_);
2569 
2570  // Get a view of the column indices in the row. This amortizes
2571  // the cost of getting the view over all the entries of inds.
2572  auto colInds = graph.getLocalIndsViewHost (rowInfo);
2573  const LO LINV = Teuchos::OrdinalTraits<LO>::invalid ();
2574 
2575  for (LO j = 0; j < numElts; ++j) {
2576  const LO lclColInd = colMap.getLocalElement (inds[j]);
2577  if (lclColInd != LINV) {
2578  const size_t offset =
2579  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2580  lclColInd, hint, sorted);
2581  if (offset != rowInfo.numEntries) {
2582  if (atomic) {
2583  Kokkos::atomic_add (&rowVals[offset], newVals[j]);
2584  }
2585  else {
2586  rowVals[offset] += newVals[j];
2587  }
2588  hint = offset + 1;
2589  numValid++;
2590  }
2591  }
2592  }
2593  }
2594  else if (graph.isGloballyIndexed ()) {
2595  // Get a view of the column indices in the row. This amortizes
2596  // the cost of getting the view over all the entries of inds.
2597  auto colInds = graph.getGlobalIndsViewHost (rowInfo);
2598 
2599  for (LO j = 0; j < numElts; ++j) {
2600  const GO gblColInd = inds[j];
2601  const size_t offset =
2602  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2603  gblColInd, hint, sorted);
2604  if (offset != rowInfo.numEntries) {
2605  if (atomic) {
2606  Kokkos::atomic_add (&rowVals[offset], newVals[j]);
2607  }
2608  else {
2609  rowVals[offset] += newVals[j];
2610  }
2611  hint = offset + 1;
2612  numValid++;
2613  }
2614  }
2615  }
2616  // If the graph is neither locally nor globally indexed on the
2617  // calling process, that means the calling process has no graph
2618  // entries. Thus, none of the input column indices are valid.
2619 
2620  return numValid;
2621  }
2622 
2623  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2624  LocalOrdinal
2626  sumIntoGlobalValues (const GlobalOrdinal gblRow,
2627  const Teuchos::ArrayView<const GlobalOrdinal>& inputGblColInds,
2628  const Teuchos::ArrayView<const Scalar>& inputVals,
2629  const bool atomic)
2630  {
2631  typedef LocalOrdinal LO;
2632 
2633  const LO numInputEnt = static_cast<LO> (inputGblColInds.size ());
2634  if (static_cast<LO> (inputVals.size ()) != numInputEnt) {
2635  return Teuchos::OrdinalTraits<LO>::invalid ();
2636  }
2637  return this->sumIntoGlobalValues (gblRow, numInputEnt,
2638  inputVals.getRawPtr (),
2639  inputGblColInds.getRawPtr (),
2640  atomic);
2641  }
2642 
2643  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2644  LocalOrdinal
2646  sumIntoGlobalValues (const GlobalOrdinal gblRow,
2647  const LocalOrdinal numInputEnt,
2648  const Scalar inputVals[],
2649  const GlobalOrdinal inputGblColInds[],
2650  const bool atomic)
2651  {
2652  typedef impl_scalar_type IST;
2653  typedef LocalOrdinal LO;
2654  typedef GlobalOrdinal GO;
2655 
2656  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
2657  // Fill must be active and the "nonconst" graph must exist.
2658  return Teuchos::OrdinalTraits<LO>::invalid ();
2659  }
2660  const crs_graph_type& graph = * (this->staticGraph_);
2661 
2662  const RowInfo rowInfo = graph.getRowInfoFromGlobalRowIndex (gblRow);
2663  if (rowInfo.localRow == Teuchos::OrdinalTraits<size_t>::invalid ()) {
2664  // mfh 23 Mar 2017, 26 Jul 2017: This branch may not be not
2665  // thread safe in a debug build, in part because it uses
2666  // Teuchos::ArrayView, and in part because of the data structure
2667  // used to stash outgoing entries.
2668  using Teuchos::ArrayView;
2669  ArrayView<const GO> inputGblColInds_av(
2670  numInputEnt == 0 ? nullptr : inputGblColInds,
2671  numInputEnt);
2672  ArrayView<const Scalar> inputVals_av(
2673  numInputEnt == 0 ? nullptr :
2674  inputVals, numInputEnt);
2675  // gblRow is not in the row Map on the calling process, so stash
2676  // the given entries away in a separate data structure.
2677  // globalAssemble() (called during fillComplete()) will exchange
2678  // that data and sum it in using sumIntoGlobalValues().
2679  this->insertNonownedGlobalValues (gblRow, inputGblColInds_av,
2680  inputVals_av);
2681  // FIXME (mfh 08 Jul 2014) It's not clear what to return here,
2682  // since we won't know whether the given indices were valid
2683  // until globalAssemble (called in fillComplete) is called.
2684  // That's why insertNonownedGlobalValues doesn't return
2685  // anything. Just for consistency, I'll return the number of
2686  // entries that the user gave us.
2687  return numInputEnt;
2688  }
2689  else { // input row is in the row Map on the calling process
2690  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
2691  const IST* const inVals = reinterpret_cast<const IST*> (inputVals);
2692  return this->sumIntoGlobalValuesImpl (curRowVals.data (), graph, rowInfo,
2693  inputGblColInds, inVals,
2694  numInputEnt, atomic);
2695  }
2696  }
2697 
2698  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2699  LocalOrdinal
2701  transformLocalValues (const LocalOrdinal lclRow,
2702  const LocalOrdinal numInputEnt,
2703  const impl_scalar_type inputVals[],
2704  const LocalOrdinal inputCols[],
2705  std::function<impl_scalar_type (const impl_scalar_type&, const impl_scalar_type&) > f,
2706  const bool atomic)
2707  {
2708  using Tpetra::Details::OrdinalTraits;
2709  typedef LocalOrdinal LO;
2710 
2711  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
2712  // Fill must be active and the "nonconst" graph must exist.
2713  return Teuchos::OrdinalTraits<LO>::invalid ();
2714  }
2715  const crs_graph_type& graph = * (this->staticGraph_);
2716  const RowInfo rowInfo = graph.getRowInfo (lclRow);
2717 
2718  if (rowInfo.localRow == OrdinalTraits<size_t>::invalid ()) {
2719  // The calling process does not own this row, so it is not
2720  // allowed to modify its values.
2721  return static_cast<LO> (0);
2722  }
2723  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
2724  return this->transformLocalValues (curRowVals.data (), graph,
2725  rowInfo, inputCols, inputVals,
2726  numInputEnt, f, atomic);
2727  }
2728 
2729  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2730  LocalOrdinal
2731  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
2732  transformGlobalValues (const GlobalOrdinal gblRow,
2733  const LocalOrdinal numInputEnt,
2734  const impl_scalar_type inputVals[],
2735  const GlobalOrdinal inputCols[],
2736  std::function<impl_scalar_type (const impl_scalar_type&, const impl_scalar_type&) > f,
2737  const bool atomic)
2738  {
2739  using Tpetra::Details::OrdinalTraits;
2740  typedef LocalOrdinal LO;
2741 
2742  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
2743  // Fill must be active and the "nonconst" graph must exist.
2744  return OrdinalTraits<LO>::invalid ();
2745  }
2746  const crs_graph_type& graph = * (this->staticGraph_);
2747  const RowInfo rowInfo = graph.getRowInfoFromGlobalRowIndex (gblRow);
2748 
2749  if (rowInfo.localRow == OrdinalTraits<size_t>::invalid ()) {
2750  // The calling process does not own this row, so it is not
2751  // allowed to modify its values.
2752  return static_cast<LO> (0);
2753  }
2754  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
2755  return this->transformGlobalValues (curRowVals.data (), graph,
2756  rowInfo, inputCols, inputVals,
2757  numInputEnt, f, atomic);
2758  }
2759 
2760  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2761  LocalOrdinal
2762  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
2763  transformLocalValues (impl_scalar_type rowVals[],
2764  const crs_graph_type& graph,
2765  const RowInfo& rowInfo,
2766  const LocalOrdinal inds[],
2767  const impl_scalar_type newVals[],
2768  const LocalOrdinal numElts,
2769  std::function<impl_scalar_type (const impl_scalar_type&, const impl_scalar_type&) > f,
2770  const bool atomic)
2771  {
2772  typedef impl_scalar_type ST;
2773  typedef LocalOrdinal LO;
2774  typedef GlobalOrdinal GO;
2775 
2776  //if (newVals.extent (0) != inds.extent (0)) {
2777  // The sizes of the input arrays must match.
2778  //return Tpetra::Details::OrdinalTraits<LO>::invalid ();
2779  //}
2780  //const LO numElts = static_cast<LO> (inds.extent (0));
2781  const bool sorted = graph.isSorted ();
2782 
2783  LO numValid = 0; // number of valid input column indices
2784  size_t hint = 0; // Guess for the current index k into rowVals
2785 
2786  if (graph.isLocallyIndexed ()) {
2787  // Get a view of the column indices in the row. This amortizes
2788  // the cost of getting the view over all the entries of inds.
2789  auto colInds = graph.getLocalIndsViewHost (rowInfo);
2790 
2791  for (LO j = 0; j < numElts; ++j) {
2792  const LO lclColInd = inds[j];
2793  const size_t offset =
2794  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2795  lclColInd, hint, sorted);
2796  if (offset != rowInfo.numEntries) {
2797  if (atomic) {
2798  // NOTE (mfh 30 Nov 2015) The commented-out code is
2799  // wrong because another thread may have changed
2800  // rowVals[offset] between those two lines of code.
2801  //
2802  //const ST newVal = f (rowVals[offset], newVals[j]);
2803  //Kokkos::atomic_assign (&rowVals[offset], newVal);
2804 
2805  volatile ST* const dest = &rowVals[offset];
2806  (void) atomic_binary_function_update (dest, newVals[j], f);
2807  }
2808  else {
2809  // use binary function f
2810  rowVals[offset] = f (rowVals[offset], newVals[j]);
2811  }
2812  hint = offset + 1;
2813  ++numValid;
2814  }
2815  }
2816  }
2817  else if (graph.isGloballyIndexed ()) {
2818  // NOTE (mfh 26 Nov 2015) Dereferencing an RCP or reading its
2819  // pointer does NOT change its reference count. Thus, this
2820  // code is still thread safe.
2821  if (graph.colMap_.is_null ()) {
2822  // NO input column indices are valid in this case. Either
2823  // the column Map hasn't been set yet (so local indices
2824  // don't exist yet), or the calling process owns no graph
2825  // entries.
2826  return numValid;
2827  }
2828  const map_type& colMap = * (graph.colMap_);
2829  // Get a view of the column indices in the row. This amortizes
2830  // the cost of getting the view over all the entries of inds.
2831  auto colInds = graph.getGlobalIndsViewHost (rowInfo);
2832 
2833  const GO GINV = Teuchos::OrdinalTraits<GO>::invalid ();
2834  for (LO j = 0; j < numElts; ++j) {
2835  const GO gblColInd = colMap.getGlobalElement (inds[j]);
2836  if (gblColInd != GINV) {
2837  const size_t offset =
2838  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2839  gblColInd, hint, sorted);
2840  if (offset != rowInfo.numEntries) {
2841  if (atomic) {
2842  // NOTE (mfh 30 Nov 2015) The commented-out code is
2843  // wrong because another thread may have changed
2844  // rowVals[offset] between those two lines of code.
2845  //
2846  //const ST newVal = f (rowVals[offset], newVals[j]);
2847  //Kokkos::atomic_assign (&rowVals[offset], newVal);
2848 
2849  volatile ST* const dest = &rowVals[offset];
2850  (void) atomic_binary_function_update (dest, newVals[j], f);
2851  }
2852  else {
2853  // use binary function f
2854  rowVals[offset] = f (rowVals[offset], newVals[j]);
2855  }
2856  hint = offset + 1;
2857  numValid++;
2858  }
2859  }
2860  }
2861  }
2862  // If the graph is neither locally nor globally indexed on the
2863  // calling process, that means the calling process has no graph
2864  // entries. Thus, none of the input column indices are valid.
2865 
2866  return numValid;
2867  }
2868 
2869  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2870  LocalOrdinal
2871  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
2872  transformGlobalValues (impl_scalar_type rowVals[],
2873  const crs_graph_type& graph,
2874  const RowInfo& rowInfo,
2875  const GlobalOrdinal inds[],
2876  const impl_scalar_type newVals[],
2877  const LocalOrdinal numElts,
2878  std::function<impl_scalar_type (const impl_scalar_type&, const impl_scalar_type&) > f,
2879  const bool atomic)
2880  {
2881  typedef impl_scalar_type ST;
2882  typedef LocalOrdinal LO;
2883  typedef GlobalOrdinal GO;
2884 
2885  //if (newVals.extent (0) != inds.extent (0)) {
2886  // The sizes of the input arrays must match.
2887  //return Tpetra::Details::OrdinalTraits<LO>::invalid ();
2888  //}
2889  //const LO numElts = static_cast<LO> (inds.extent (0));
2890  const bool sorted = graph.isSorted ();
2891 
2892  LO numValid = 0; // number of valid input column indices
2893  size_t hint = 0; // Guess for the current index k into rowVals
2894 
2895  if (graph.isGloballyIndexed ()) {
2896  // Get a view of the column indices in the row. This amortizes
2897  // the cost of getting the view over all the entries of inds.
2898  auto colInds = graph.getGlobalIndsViewHost (rowInfo);
2899 
2900  for (LO j = 0; j < numElts; ++j) {
2901  const GO gblColInd = inds[j];
2902  const size_t offset =
2903  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2904  gblColInd, hint, sorted);
2905  if (offset != rowInfo.numEntries) {
2906  if (atomic) {
2907  // NOTE (mfh 30 Nov 2015) The commented-out code is
2908  // wrong because another thread may have changed
2909  // rowVals[offset] between those two lines of code.
2910  //
2911  //const ST newVal = f (rowVals[offset], newVals[j]);
2912  //Kokkos::atomic_assign (&rowVals[offset], newVal);
2913 
2914  volatile ST* const dest = &rowVals[offset];
2915  (void) atomic_binary_function_update (dest, newVals[j], f);
2916  }
2917  else {
2918  // use binary function f
2919  rowVals[offset] = f (rowVals[offset], newVals[j]);
2920  }
2921  hint = offset + 1;
2922  ++numValid;
2923  }
2924  }
2925  }
2926  else if (graph.isLocallyIndexed ()) {
2927  // NOTE (mfh 26 Nov 2015) Dereferencing an RCP or reading its
2928  // pointer does NOT change its reference count. Thus, this
2929  // code is still thread safe.
2930  if (graph.colMap_.is_null ()) {
2931  // NO input column indices are valid in this case. Either the
2932  // column Map hasn't been set yet (so local indices don't
2933  // exist yet), or the calling process owns no graph entries.
2934  return numValid;
2935  }
2936  const map_type& colMap = * (graph.colMap_);
2937  // Get a view of the column indices in the row. This amortizes
2938  // the cost of getting the view over all the entries of inds.
2939  auto colInds = graph.getLocalIndsViewHost (rowInfo);
2940 
2941  const LO LINV = Teuchos::OrdinalTraits<LO>::invalid ();
2942  for (LO j = 0; j < numElts; ++j) {
2943  const LO lclColInd = colMap.getLocalElement (inds[j]);
2944  if (lclColInd != LINV) {
2945  const size_t offset =
2946  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
2947  lclColInd, hint, sorted);
2948  if (offset != rowInfo.numEntries) {
2949  if (atomic) {
2950  // NOTE (mfh 30 Nov 2015) The commented-out code is
2951  // wrong because another thread may have changed
2952  // rowVals[offset] between those two lines of code.
2953  //
2954  //const ST newVal = f (rowVals[offset], newVals[j]);
2955  //Kokkos::atomic_assign (&rowVals[offset], newVal);
2956 
2957  volatile ST* const dest = &rowVals[offset];
2958  (void) atomic_binary_function_update (dest, newVals[j], f);
2959  }
2960  else {
2961  // use binary function f
2962  rowVals[offset] = f (rowVals[offset], newVals[j]);
2963  }
2964  hint = offset + 1;
2965  numValid++;
2966  }
2967  }
2968  }
2969  }
2970  // If the graph is neither locally nor globally indexed on the
2971  // calling process, that means the calling process has no graph
2972  // entries. Thus, none of the input column indices are valid.
2973 
2974  return numValid;
2975  }
2976 
2977  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2978  LocalOrdinal
2981  const crs_graph_type& graph,
2982  const RowInfo& rowInfo,
2983  const LocalOrdinal inds[],
2984  const impl_scalar_type newVals[],
2985  const LocalOrdinal numElts,
2986  const bool atomic)
2987  {
2988  typedef LocalOrdinal LO;
2989  typedef GlobalOrdinal GO;
2990 
2991  const bool sorted = graph.isSorted ();
2992 
2993  size_t hint = 0; // Guess for the current index k into rowVals
2994  LO numValid = 0; // number of valid local column indices
2995 
2996  if (graph.isLocallyIndexed ()) {
2997  // Get a view of the column indices in the row. This amortizes
2998  // the cost of getting the view over all the entries of inds.
2999  auto colInds = graph.getLocalIndsViewHost (rowInfo);
3000 
3001  for (LO j = 0; j < numElts; ++j) {
3002  const LO lclColInd = inds[j];
3003  const size_t offset =
3004  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
3005  lclColInd, hint, sorted);
3006  if (offset != rowInfo.numEntries) {
3007  if (atomic) {
3008  Kokkos::atomic_add (&rowVals[offset], newVals[j]);
3009  }
3010  else {
3011  rowVals[offset] += newVals[j];
3012  }
3013  hint = offset + 1;
3014  ++numValid;
3015  }
3016  }
3017  }
3018  else if (graph.isGloballyIndexed ()) {
3019  if (graph.colMap_.is_null ()) {
3020  return Teuchos::OrdinalTraits<LO>::invalid ();
3021  }
3022  const map_type colMap = * (graph.colMap_);
3023 
3024  // Get a view of the column indices in the row. This amortizes
3025  // the cost of getting the view over all the entries of inds.
3026  auto colInds = graph.getGlobalIndsViewHost (rowInfo);
3027 
3028  for (LO j = 0; j < numElts; ++j) {
3029  const GO gblColInd = colMap.getGlobalElement (inds[j]);
3030  if (gblColInd != Teuchos::OrdinalTraits<GO>::invalid ()) {
3031  const size_t offset =
3032  KokkosSparse::findRelOffset (colInds, rowInfo.numEntries,
3033  gblColInd, hint, sorted);
3034  if (offset != rowInfo.numEntries) {
3035  if (atomic) {
3036  Kokkos::atomic_add (&rowVals[offset], newVals[j]);
3037  }
3038  else {
3039  rowVals[offset] += newVals[j];
3040  }
3041  hint = offset + 1;
3042  ++numValid;
3043  }
3044  }
3045  }
3046  }
3047  // NOTE (mfh 26 Jun 2014, 26 Nov 2015) In the current version of
3048  // CrsGraph and CrsMatrix, it's possible for a matrix (or graph)
3049  // to be neither locally nor globally indexed on a process.
3050  // This means that the graph or matrix has no entries on that
3051  // process. Epetra also works like this. It's related to lazy
3052  // allocation (on first insertion, not at graph / matrix
3053  // construction). Lazy allocation will go away because it is
3054  // not thread scalable.
3055 
3056  return numValid;
3057  }
3058 
3059  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3060  LocalOrdinal
3062  sumIntoLocalValues (const LocalOrdinal localRow,
3063  const Teuchos::ArrayView<const LocalOrdinal>& indices,
3064  const Teuchos::ArrayView<const Scalar>& values,
3065  const bool atomic)
3066  {
3067  using LO = local_ordinal_type;
3068  const LO numInputEnt = static_cast<LO>(indices.size());
3069  if (static_cast<LO>(values.size()) != numInputEnt) {
3070  return Teuchos::OrdinalTraits<LO>::invalid();
3071  }
3072  const LO* const inputInds = indices.getRawPtr();
3073  const scalar_type* const inputVals = values.getRawPtr();
3074  return this->sumIntoLocalValues(localRow, numInputEnt,
3075  inputVals, inputInds, atomic);
3076  }
3077 
3078  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3080  local_ordinal_type
3083  const local_ordinal_type localRow,
3084  const Kokkos::View<const local_ordinal_type*, Kokkos::AnonymousSpace>& inputInds,
3085  const Kokkos::View<const impl_scalar_type*, Kokkos::AnonymousSpace>& inputVals,
3086  const bool atomic)
3087  {
3088  using LO = local_ordinal_type;
3089  const LO numInputEnt = static_cast<LO>(inputInds.extent(0));
3090  if (static_cast<LO>(inputVals.extent(0)) != numInputEnt) {
3091  return Teuchos::OrdinalTraits<LO>::invalid();
3092  }
3093  const scalar_type* inVals =
3094  reinterpret_cast<const scalar_type*>(inputVals.data());
3095  return this->sumIntoLocalValues(localRow, numInputEnt, inVals,
3096  inputInds.data(), atomic);
3097  }
3098 
3099  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3100  LocalOrdinal
3102  sumIntoLocalValues (const LocalOrdinal localRow,
3103  const LocalOrdinal numEnt,
3104  const Scalar vals[],
3105  const LocalOrdinal cols[],
3106  const bool atomic)
3107  {
3108  typedef impl_scalar_type IST;
3109  typedef LocalOrdinal LO;
3110 
3111  if (! this->isFillActive () || this->staticGraph_.is_null ()) {
3112  // Fill must be active and the "nonconst" graph must exist.
3113  return Teuchos::OrdinalTraits<LO>::invalid ();
3114  }
3115  const crs_graph_type& graph = * (this->staticGraph_);
3116  const RowInfo rowInfo = graph.getRowInfo (localRow);
3117 
3118  if (rowInfo.localRow == Teuchos::OrdinalTraits<size_t>::invalid ()) {
3119  // The calling process does not own this row, so it is not
3120  // allowed to modify its values.
3121  return static_cast<LO> (0);
3122  }
3123  auto curRowVals = this->getValuesViewHostNonConst (rowInfo);
3124  const IST* const inputVals = reinterpret_cast<const IST*> (vals);
3125  return this->sumIntoLocalValuesImpl (curRowVals.data (), graph, rowInfo,
3126  cols, inputVals, numEnt, atomic);
3127  }
3128 
3129  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3131  values_dualv_type::t_host::const_type
3133  getValuesViewHost (const RowInfo& rowinfo) const
3134  {
3135  if (rowinfo.allocSize == 0 || valuesUnpacked_wdv.extent(0) == 0)
3136  return typename values_dualv_type::t_host::const_type ();
3137  else
3138  return valuesUnpacked_wdv.getHostSubview(rowinfo.offset1D,
3139  rowinfo.allocSize,
3140  Access::ReadOnly);
3141  }
3142 
3143  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3145  values_dualv_type::t_host
3148  {
3149  if (rowinfo.allocSize == 0 || valuesUnpacked_wdv.extent(0) == 0)
3150  return typename values_dualv_type::t_host ();
3151  else
3152  return valuesUnpacked_wdv.getHostSubview(rowinfo.offset1D,
3153  rowinfo.allocSize,
3154  Access::ReadWrite);
3155  }
3156 
3157  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3159  values_dualv_type::t_dev::const_type
3161  getValuesViewDevice (const RowInfo& rowinfo) const
3162  {
3163  if (rowinfo.allocSize == 0 || valuesUnpacked_wdv.extent(0) == 0)
3164  return typename values_dualv_type::t_dev::const_type ();
3165  else
3166  return valuesUnpacked_wdv.getDeviceSubview(rowinfo.offset1D,
3167  rowinfo.allocSize,
3168  Access::ReadOnly);
3169  }
3170 
3171  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3173  values_dualv_type::t_dev
3176  {
3177  if (rowinfo.allocSize == 0 || valuesUnpacked_wdv.extent(0) == 0)
3178  return typename values_dualv_type::t_dev ();
3179  else
3180  return valuesUnpacked_wdv.getDeviceSubview(rowinfo.offset1D,
3181  rowinfo.allocSize,
3182  Access::ReadWrite);
3183  }
3184 
3185 
3186  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3187  void
3190  nonconst_local_inds_host_view_type &indices,
3191  nonconst_values_host_view_type &values,
3192  size_t& numEntries) const
3193  {
3194  using Teuchos::ArrayView;
3195  using Teuchos::av_reinterpret_cast;
3196  const char tfecfFuncName[] = "getLocalRowCopy: ";
3197 
3198  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3199  (! this->hasColMap (), std::runtime_error,
3200  "The matrix does not have a column Map yet. This means we don't have "
3201  "local indices for columns yet, so it doesn't make sense to call this "
3202  "method. If the matrix doesn't have a column Map yet, you should call "
3203  "fillComplete on it first.");
3204 
3205  const RowInfo rowinfo = staticGraph_->getRowInfo (localRow);
3206  const size_t theNumEntries = rowinfo.numEntries;
3207  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3208  (static_cast<size_t> (indices.size ()) < theNumEntries ||
3209  static_cast<size_t> (values.size ()) < theNumEntries,
3210  std::runtime_error, "Row with local index " << localRow << " has " <<
3211  theNumEntries << " entry/ies, but indices.size() = " <<
3212  indices.size () << " and values.size() = " << values.size () << ".");
3213  numEntries = theNumEntries; // first side effect
3214 
3215  if (rowinfo.localRow != Teuchos::OrdinalTraits<size_t>::invalid ()) {
3216  if (staticGraph_->isLocallyIndexed ()) {
3217  auto curLclInds = staticGraph_->getLocalIndsViewHost(rowinfo);
3218  auto curVals = getValuesViewHost(rowinfo);
3219 
3220  for (size_t j = 0; j < theNumEntries; ++j) {
3221  values[j] = curVals[j];
3222  indices[j] = curLclInds(j);
3223  }
3224  }
3225  else if (staticGraph_->isGloballyIndexed ()) {
3226  // Don't call getColMap(), because it touches RCP's reference count.
3227  const map_type& colMap = * (staticGraph_->colMap_);
3228  auto curGblInds = staticGraph_->getGlobalIndsViewHost(rowinfo);
3229  auto curVals = getValuesViewHost(rowinfo);
3230 
3231  for (size_t j = 0; j < theNumEntries; ++j) {
3232  values[j] = curVals[j];
3233  indices[j] = colMap.getLocalElement (curGblInds(j));
3234  }
3235  }
3236  }
3237  }
3238 
3239 
3240 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3241 void
3244  nonconst_global_inds_host_view_type &indices,
3245  nonconst_values_host_view_type &values,
3246  size_t& numEntries) const
3247  {
3248  using Teuchos::ArrayView;
3249  using Teuchos::av_reinterpret_cast;
3250  const char tfecfFuncName[] = "getGlobalRowCopy: ";
3251 
3252  const RowInfo rowinfo =
3253  staticGraph_->getRowInfoFromGlobalRowIndex (globalRow);
3254  const size_t theNumEntries = rowinfo.numEntries;
3255  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3256  static_cast<size_t> (indices.size ()) < theNumEntries ||
3257  static_cast<size_t> (values.size ()) < theNumEntries,
3258  std::runtime_error, "Row with global index " << globalRow << " has "
3259  << theNumEntries << " entry/ies, but indices.size() = " <<
3260  indices.size () << " and values.size() = " << values.size () << ".");
3261  numEntries = theNumEntries; // first side effect
3262 
3263  if (rowinfo.localRow != Teuchos::OrdinalTraits<size_t>::invalid ()) {
3264  if (staticGraph_->isLocallyIndexed ()) {
3265  const map_type& colMap = * (staticGraph_->colMap_);
3266  auto curLclInds = staticGraph_->getLocalIndsViewHost(rowinfo);
3267  auto curVals = getValuesViewHost(rowinfo);
3268 
3269  for (size_t j = 0; j < theNumEntries; ++j) {
3270  values[j] = curVals[j];
3271  indices[j] = colMap.getGlobalElement (curLclInds(j));
3272  }
3273  }
3274  else if (staticGraph_->isGloballyIndexed ()) {
3275  auto curGblInds = staticGraph_->getGlobalIndsViewHost(rowinfo);
3276  auto curVals = getValuesViewHost(rowinfo);
3277 
3278  for (size_t j = 0; j < theNumEntries; ++j) {
3279  values[j] = curVals[j];
3280  indices[j] = curGblInds(j);
3281  }
3282  }
3283  }
3284  }
3285 
3286 
3287  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3288  void
3290  getLocalRowView(LocalOrdinal localRow,
3291  local_inds_host_view_type &indices,
3292  values_host_view_type &values) const
3293  {
3294  const char tfecfFuncName[] = "getLocalRowView: ";
3295 
3296  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3297  isGloballyIndexed (), std::runtime_error, "The matrix currently stores "
3298  "its indices as global indices, so you cannot get a view with local "
3299  "column indices. If the matrix has a column Map, you may call "
3300  "getLocalRowCopy() to get local column indices; otherwise, you may get "
3301  "a view with global column indices by calling getGlobalRowCopy().");
3302 
3303  const RowInfo rowInfo = staticGraph_->getRowInfo (localRow);
3304  if (rowInfo.localRow != Teuchos::OrdinalTraits<size_t>::invalid () &&
3305  rowInfo.numEntries > 0) {
3306  indices = staticGraph_->lclIndsUnpacked_wdv.getHostSubview(
3307  rowInfo.offset1D,
3308  rowInfo.numEntries,
3309  Access::ReadOnly);
3310  values = valuesUnpacked_wdv.getHostSubview(rowInfo.offset1D,
3311  rowInfo.numEntries,
3312  Access::ReadOnly);
3313  }
3314  else {
3315  // This does the right thing (reports an empty row) if the input
3316  // row is invalid.
3317  indices = local_inds_host_view_type();
3318  values = values_host_view_type();
3319  }
3320 
3321 #ifdef HAVE_TPETRA_DEBUG
3322  const char suffix[] = ". This should never happen. Please report this "
3323  "bug to the Tpetra developers.";
3324  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3325  (static_cast<size_t> (indices.size ()) !=
3326  static_cast<size_t> (values.size ()), std::logic_error,
3327  "At the end of this method, for local row " << localRow << ", "
3328  "indices.size() = " << indices.size () << " != values.size () = "
3329  << values.size () << suffix);
3330  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3331  (static_cast<size_t> (indices.size ()) !=
3332  static_cast<size_t> (rowInfo.numEntries), std::logic_error,
3333  "At the end of this method, for local row " << localRow << ", "
3334  "indices.size() = " << indices.size () << " != rowInfo.numEntries = "
3335  << rowInfo.numEntries << suffix);
3336  const size_t expectedNumEntries = getNumEntriesInLocalRow (localRow);
3337  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3338  (rowInfo.numEntries != expectedNumEntries, std::logic_error, "At the end "
3339  "of this method, for local row " << localRow << ", rowInfo.numEntries = "
3340  << rowInfo.numEntries << " != getNumEntriesInLocalRow(localRow) = " <<
3341  expectedNumEntries << suffix);
3342 #endif // HAVE_TPETRA_DEBUG
3343  }
3344 
3345 
3346  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3347  void
3349  getGlobalRowView (GlobalOrdinal globalRow,
3350  global_inds_host_view_type &indices,
3351  values_host_view_type &values) const
3352  {
3353  const char tfecfFuncName[] = "getGlobalRowView: ";
3354 
3355  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3356  isLocallyIndexed (), std::runtime_error,
3357  "The matrix is locally indexed, so we cannot return a view of the row "
3358  "with global column indices. Use getGlobalRowCopy() instead.");
3359 
3360  // This does the right thing (reports an empty row) if the input
3361  // row is invalid.
3362  const RowInfo rowInfo =
3363  staticGraph_->getRowInfoFromGlobalRowIndex (globalRow);
3364  if (rowInfo.localRow != Teuchos::OrdinalTraits<size_t>::invalid () &&
3365  rowInfo.numEntries > 0) {
3366  indices = staticGraph_->gblInds_wdv.getHostSubview(rowInfo.offset1D,
3367  rowInfo.numEntries,
3368  Access::ReadOnly);
3369  values = valuesUnpacked_wdv.getHostSubview(rowInfo.offset1D,
3370  rowInfo.numEntries,
3371  Access::ReadOnly);
3372  }
3373  else {
3374  indices = global_inds_host_view_type();
3375  values = values_host_view_type();
3376  }
3377 
3378 #ifdef HAVE_TPETRA_DEBUG
3379  const char suffix[] = ". This should never happen. Please report this "
3380  "bug to the Tpetra developers.";
3381  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3382  (static_cast<size_t> (indices.size ()) !=
3383  static_cast<size_t> (values.size ()), std::logic_error,
3384  "At the end of this method, for global row " << globalRow << ", "
3385  "indices.size() = " << indices.size () << " != values.size () = "
3386  << values.size () << suffix);
3387  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3388  (static_cast<size_t> (indices.size ()) !=
3389  static_cast<size_t> (rowInfo.numEntries), std::logic_error,
3390  "At the end of this method, for global row " << globalRow << ", "
3391  "indices.size() = " << indices.size () << " != rowInfo.numEntries = "
3392  << rowInfo.numEntries << suffix);
3393  const size_t expectedNumEntries = getNumEntriesInGlobalRow (globalRow);
3394  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3395  (rowInfo.numEntries != expectedNumEntries, std::logic_error, "At the end "
3396  "of this method, for global row " << globalRow << ", rowInfo.numEntries "
3397  "= " << rowInfo.numEntries << " != getNumEntriesInGlobalRow(globalRow) ="
3398  " " << expectedNumEntries << suffix);
3399 #endif // HAVE_TPETRA_DEBUG
3400  }
3401 
3402 
3403  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3404  void
3406  scale (const Scalar& alpha)
3407  {
3408  const impl_scalar_type theAlpha = static_cast<impl_scalar_type> (alpha);
3409 
3410  const size_t nlrs = staticGraph_->getLocalNumRows ();
3411  const size_t numEntries = staticGraph_->getLocalNumEntries ();
3412  if (! staticGraph_->indicesAreAllocated () ||
3413  nlrs == 0 || numEntries == 0) {
3414  // do nothing
3415  }
3416  else {
3417 
3418  auto vals = valuesPacked_wdv.getDeviceView(Access::ReadWrite);
3419  KokkosBlas::scal(vals, theAlpha, vals);
3420 
3421  }
3422  }
3423 
3424  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3425  void
3427  setAllToScalar (const Scalar& alpha)
3428  {
3429  const impl_scalar_type theAlpha = static_cast<impl_scalar_type> (alpha);
3430 
3431  // replace all values in the matrix
3432  // it is easiest to replace all allocated values, instead of replacing only the ones with valid entries
3433  // however, if there are no valid entries, we can short-circuit
3434  // furthermore, if the values aren't allocated, we can short-circuit (no entry have been inserted so far)
3435  const size_t numEntries = staticGraph_->getLocalNumEntries();
3436  if (! staticGraph_->indicesAreAllocated () || numEntries == 0) {
3437  // do nothing
3438  }
3439  else {
3440  // DEEP_COPY REVIEW - VALUE-TO-DEVICE
3441  Kokkos::deep_copy (execution_space(), valuesUnpacked_wdv.getDeviceView(Access::OverwriteAll),
3442  theAlpha);
3443  // CAG: This fence was found to be required on Cuda with UVM=on.
3444  Kokkos::fence("CrsMatrix::setAllToScalar");
3445  }
3446  }
3447 
3448  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3449  void
3451  setAllValues (const typename local_graph_device_type::row_map_type& rowPointers,
3452  const typename local_graph_device_type::entries_type::non_const_type& columnIndices,
3453  const typename local_matrix_device_type::values_type& values)
3454  {
3455  using ProfilingRegion=Details::ProfilingRegion;
3456  ProfilingRegion region ("Tpetra::CrsMatrix::setAllValues");
3457  const char tfecfFuncName[] = "setAllValues: ";
3458  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3459  (columnIndices.size () != values.size (), std::invalid_argument,
3460  "columnIndices.size() = " << columnIndices.size () << " != values.size()"
3461  " = " << values.size () << ".");
3462  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3463  (myGraph_.is_null (), std::runtime_error, "myGraph_ must not be null.");
3464 
3465  try {
3466  myGraph_->setAllIndices (rowPointers, columnIndices);
3467  }
3468  catch (std::exception &e) {
3469  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3470  (true, std::runtime_error, "myGraph_->setAllIndices() threw an "
3471  "exception: " << e.what ());
3472  }
3473 
3474  // Make sure that myGraph_ now has a local graph. It may not be
3475  // fillComplete yet, so it's important to check. We don't care
3476  // whether setAllIndices() did a shallow copy or a deep copy, so a
3477  // good way to check is to compare dimensions.
3478  auto lclGraph = myGraph_->getLocalGraphDevice ();
3479  const size_t numEnt = lclGraph.entries.extent (0);
3480  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3481  (lclGraph.row_map.extent (0) != rowPointers.extent (0) ||
3482  numEnt != static_cast<size_t> (columnIndices.extent (0)),
3483  std::logic_error, "myGraph_->setAllIndices() did not correctly create "
3484  "local graph. Please report this bug to the Tpetra developers.");
3485 
3486  valuesPacked_wdv = values_wdv_type(values);
3487  valuesUnpacked_wdv = valuesPacked_wdv;
3488 
3489  // Storage MUST be packed, since the interface doesn't give any
3490  // way to indicate any extra space at the end of each row.
3491  this->storageStatus_ = Details::STORAGE_1D_PACKED;
3492 
3493  checkInternalState ();
3494  }
3495 
3496  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3497  void
3499  setAllValues ( const local_matrix_device_type& localDeviceMatrix)
3500  {
3501  using ProfilingRegion=Details::ProfilingRegion;
3502  ProfilingRegion region ("Tpetra::CrsMatrix::setAllValues from KokkosSparse::CrsMatrix");
3503 
3504  auto graph = localDeviceMatrix.graph;
3505  //FIXME how to check whether graph is allocated
3506 
3507  auto rows = graph.row_map;
3508  auto columns = graph.entries;
3509  auto values = localDeviceMatrix.values;
3510 
3511  setAllValues(rows,columns,values);
3512  }
3513 
3514  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3515  void
3517  setAllValues (const Teuchos::ArrayRCP<size_t>& ptr,
3518  const Teuchos::ArrayRCP<LocalOrdinal>& ind,
3519  const Teuchos::ArrayRCP<Scalar>& val)
3520  {
3521  using Kokkos::Compat::getKokkosViewDeepCopy;
3522  using Teuchos::ArrayRCP;
3523  using Teuchos::av_reinterpret_cast;
3524  typedef device_type DT;
3525  typedef impl_scalar_type IST;
3526  typedef typename local_graph_device_type::row_map_type row_map_type;
3527  //typedef typename row_map_type::non_const_value_type row_offset_type;
3528  const char tfecfFuncName[] = "setAllValues(ArrayRCP<size_t>, ArrayRCP<LO>, ArrayRCP<Scalar>): ";
3529 
3530  // The row offset type may depend on the execution space. It may
3531  // not necessarily be size_t. If it's not, we need to make a deep
3532  // copy. We need to make a deep copy anyway so that Kokkos can
3533  // own the memory. Regardless, ptrIn gets the copy.
3534  typename row_map_type::non_const_type ptrNative ("ptr", ptr.size ());
3535  Kokkos::View<const size_t*,
3536  typename row_map_type::array_layout,
3537  Kokkos::HostSpace,
3538  Kokkos::MemoryUnmanaged> ptrSizeT (ptr.getRawPtr (), ptr.size ());
3539  ::Tpetra::Details::copyOffsets (ptrNative, ptrSizeT);
3540 
3541  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3542  (ptrNative.extent (0) != ptrSizeT.extent (0),
3543  std::logic_error, "ptrNative.extent(0) = " <<
3544  ptrNative.extent (0) << " != ptrSizeT.extent(0) = "
3545  << ptrSizeT.extent (0) << ". Please report this bug to the "
3546  "Tpetra developers.");
3547 
3548  auto indIn = getKokkosViewDeepCopy<DT> (ind ());
3549  auto valIn = getKokkosViewDeepCopy<DT> (av_reinterpret_cast<IST> (val ()));
3550  this->setAllValues (ptrNative, indIn, valIn);
3551  }
3552 
3553  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3554  void
3556  getLocalDiagOffsets (Teuchos::ArrayRCP<size_t>& offsets) const
3557  {
3558  const char tfecfFuncName[] = "getLocalDiagOffsets: ";
3559  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3560  (staticGraph_.is_null (), std::runtime_error, "The matrix has no graph.");
3561 
3562  // mfh 11 May 2016: We plan to deprecate the ArrayRCP version of
3563  // this method in CrsGraph too, so don't call it (otherwise build
3564  // warnings will show up and annoy users). Instead, copy results
3565  // in and out, if the memory space requires it.
3566 
3567  const size_t lclNumRows = staticGraph_->getLocalNumRows ();
3568  if (static_cast<size_t> (offsets.size ()) < lclNumRows) {
3569  offsets.resize (lclNumRows);
3570  }
3571 
3572  // The input ArrayRCP must always be a host pointer. Thus, if
3573  // device_type::memory_space is Kokkos::HostSpace, it's OK for us
3574  // to write to that allocation directly as a Kokkos::View.
3575  if (std::is_same<memory_space, Kokkos::HostSpace>::value) {
3576  // It is always syntactically correct to assign a raw host
3577  // pointer to a device View, so this code will compile correctly
3578  // even if this branch never runs.
3579  typedef Kokkos::View<size_t*, device_type,
3580  Kokkos::MemoryUnmanaged> output_type;
3581  output_type offsetsOut (offsets.getRawPtr (), lclNumRows);
3582  staticGraph_->getLocalDiagOffsets (offsetsOut);
3583  }
3584  else {
3585  Kokkos::View<size_t*, device_type> offsetsTmp ("diagOffsets", lclNumRows);
3586  staticGraph_->getLocalDiagOffsets (offsetsTmp);
3587  typedef Kokkos::View<size_t*, Kokkos::HostSpace,
3588  Kokkos::MemoryUnmanaged> output_type;
3589  output_type offsetsOut (offsets.getRawPtr (), lclNumRows);
3590  // DEEP_COPY REVIEW - DEVICE-TO-HOST
3591  Kokkos::deep_copy (execution_space(), offsetsOut, offsetsTmp);
3592  }
3593  }
3594 
3595  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3596  void
3599  {
3600  using Teuchos::ArrayRCP;
3601  using Teuchos::ArrayView;
3602  using Teuchos::av_reinterpret_cast;
3603  const char tfecfFuncName[] = "getLocalDiagCopy (1-arg): ";
3604  typedef local_ordinal_type LO;
3605 
3606 
3607  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3608  staticGraph_.is_null (), std::runtime_error,
3609  "This method requires that the matrix have a graph.");
3610  auto rowMapPtr = this->getRowMap ();
3611  if (rowMapPtr.is_null () || rowMapPtr->getComm ().is_null ()) {
3612  // Processes on which the row Map or its communicator is null
3613  // don't participate. Users shouldn't even call this method on
3614  // those processes.
3615  return;
3616  }
3617  auto colMapPtr = this->getColMap ();
3618  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3619  (! this->hasColMap () || colMapPtr.is_null (), std::runtime_error,
3620  "This method requires that the matrix have a column Map.");
3621  const map_type& rowMap = * rowMapPtr;
3622  const map_type& colMap = * colMapPtr;
3623  const LO myNumRows = static_cast<LO> (this->getLocalNumRows ());
3624 
3625 #ifdef HAVE_TPETRA_DEBUG
3626  // isCompatible() requires an all-reduce, and thus this check
3627  // should only be done in debug mode.
3628  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3629  ! diag.getMap ()->isCompatible (rowMap), std::runtime_error,
3630  "The input Vector's Map must be compatible with the CrsMatrix's row "
3631  "Map. You may check this by using Map's isCompatible method: "
3632  "diag.getMap ()->isCompatible (A.getRowMap ());");
3633 #endif // HAVE_TPETRA_DEBUG
3634 
3635  if (this->isFillComplete ()) {
3636  const auto D_lcl = diag.getLocalViewDevice(Access::OverwriteAll);
3637  // 1-D subview of the first (and only) column of D_lcl.
3638  const auto D_lcl_1d =
3639  Kokkos::subview (D_lcl, Kokkos::make_pair (LO (0), myNumRows), 0);
3640 
3641  const auto lclRowMap = rowMap.getLocalMap ();
3642  const auto lclColMap = colMap.getLocalMap ();
3644  (void) getDiagCopyWithoutOffsets (D_lcl_1d, lclRowMap,
3645  lclColMap,
3646  getLocalMatrixDevice ());
3647  }
3648  else {
3650  (void) getLocalDiagCopyWithoutOffsetsNotFillComplete (diag, *this);
3651  }
3652  }
3653 
3654  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3655  void
3658  const Kokkos::View<const size_t*, device_type,
3659  Kokkos::MemoryUnmanaged>& offsets) const
3660  {
3661  typedef LocalOrdinal LO;
3662 
3663 #ifdef HAVE_TPETRA_DEBUG
3664  const char tfecfFuncName[] = "getLocalDiagCopy: ";
3665  const map_type& rowMap = * (this->getRowMap ());
3666  // isCompatible() requires an all-reduce, and thus this check
3667  // should only be done in debug mode.
3668  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3669  ! diag.getMap ()->isCompatible (rowMap), std::runtime_error,
3670  "The input Vector's Map must be compatible with (in the sense of Map::"
3671  "isCompatible) the CrsMatrix's row Map.");
3672 #endif // HAVE_TPETRA_DEBUG
3673 
3674  // For now, we fill the Vector on the host and sync to device.
3675  // Later, we may write a parallel kernel that works entirely on
3676  // device.
3677  //
3678  // NOTE (mfh 21 Jan 2016): The host kernel here assumes UVM. Once
3679  // we write a device kernel, it will not need to assume UVM.
3680 
3681  auto D_lcl = diag.getLocalViewDevice (Access::OverwriteAll);
3682  const LO myNumRows = static_cast<LO> (this->getLocalNumRows ());
3683  // Get 1-D subview of the first (and only) column of D_lcl.
3684  auto D_lcl_1d =
3685  Kokkos::subview (D_lcl, Kokkos::make_pair (LO (0), myNumRows), 0);
3686 
3687  KokkosSparse::getDiagCopy (D_lcl_1d, offsets,
3688  getLocalMatrixDevice ());
3689  }
3690 
3691  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3692  void
3695  const Teuchos::ArrayView<const size_t>& offsets) const
3696  {
3697  using LO = LocalOrdinal;
3698  using host_execution_space = Kokkos::DefaultHostExecutionSpace;
3699  using IST = impl_scalar_type;
3700 
3701 #ifdef HAVE_TPETRA_DEBUG
3702  const char tfecfFuncName[] = "getLocalDiagCopy: ";
3703  const map_type& rowMap = * (this->getRowMap ());
3704  // isCompatible() requires an all-reduce, and thus this check
3705  // should only be done in debug mode.
3706  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3707  ! diag.getMap ()->isCompatible (rowMap), std::runtime_error,
3708  "The input Vector's Map must be compatible with (in the sense of Map::"
3709  "isCompatible) the CrsMatrix's row Map.");
3710 #endif // HAVE_TPETRA_DEBUG
3711 
3712  // See #1510. In case diag has already been marked modified on
3713  // device, we need to clear that flag, since the code below works
3714  // on host.
3715  //diag.clear_sync_state ();
3716 
3717  // For now, we fill the Vector on the host and sync to device.
3718  // Later, we may write a parallel kernel that works entirely on
3719  // device.
3720  auto lclVecHost = diag.getLocalViewHost(Access::OverwriteAll);
3721  // 1-D subview of the first (and only) column of lclVecHost.
3722  auto lclVecHost1d = Kokkos::subview (lclVecHost, Kokkos::ALL (), 0);
3723 
3724  using host_offsets_view_type =
3725  Kokkos::View<const size_t*, Kokkos::HostSpace,
3726  Kokkos::MemoryTraits<Kokkos::Unmanaged> >;
3727  host_offsets_view_type h_offsets (offsets.getRawPtr (), offsets.size ());
3728  // Find the diagonal entries and put them in lclVecHost1d.
3729  using range_type = Kokkos::RangePolicy<host_execution_space, LO>;
3730  const LO myNumRows = static_cast<LO> (this->getLocalNumRows ());
3731  const size_t INV = Tpetra::Details::OrdinalTraits<size_t>::invalid ();
3732 
3733  auto rowPtrsPackedHost = staticGraph_->getRowPtrsPackedHost();
3734  auto valuesPackedHost = valuesPacked_wdv.getHostView(Access::ReadOnly);
3735  Kokkos::parallel_for
3736  ("Tpetra::CrsMatrix::getLocalDiagCopy",
3737  range_type (0, myNumRows),
3738  [&, INV, h_offsets] (const LO lclRow) { // Value capture is a workaround for cuda + gcc-7.2 compiler bug w/c++14
3739  lclVecHost1d(lclRow) = STS::zero (); // default value if no diag entry
3740  if (h_offsets[lclRow] != INV) {
3741  auto curRowOffset = rowPtrsPackedHost (lclRow);
3742  lclVecHost1d(lclRow) =
3743  static_cast<IST> (valuesPackedHost(curRowOffset+h_offsets[lclRow]));
3744  }
3745  });
3746  //diag.sync_device ();
3747  }
3748 
3749 
3750  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3751  void
3754  {
3755  using ::Tpetra::Details::ProfilingRegion;
3756  using Teuchos::ArrayRCP;
3757  using Teuchos::ArrayView;
3758  using Teuchos::null;
3759  using Teuchos::RCP;
3760  using Teuchos::rcp;
3761  using Teuchos::rcpFromRef;
3763  const char tfecfFuncName[] = "leftScale: ";
3764 
3765  ProfilingRegion region ("Tpetra::CrsMatrix::leftScale");
3766 
3767  RCP<const vec_type> xp;
3768  if (this->getRangeMap ()->isSameAs (* (x.getMap ()))) {
3769  // Take from Epetra: If we have a non-trivial exporter, we must
3770  // import elements that are permuted or are on other processors.
3771  auto exporter = this->getCrsGraphRef ().getExporter ();
3772  if (exporter.get () != nullptr) {
3773  RCP<vec_type> tempVec (new vec_type (this->getRowMap ()));
3774  tempVec->doImport (x, *exporter, REPLACE); // reverse mode
3775  xp = tempVec;
3776  }
3777  else {
3778  xp = rcpFromRef (x);
3779  }
3780  }
3781  else if (this->getRowMap ()->isSameAs (* (x.getMap ()))) {
3782  xp = rcpFromRef (x);
3783  }
3784  else {
3785  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3786  (true, std::invalid_argument, "x's Map must be the same as "
3787  "either the row Map or the range Map of the CrsMatrix.");
3788  }
3789 
3790  if (this->isFillComplete()) {
3791  auto x_lcl = xp->getLocalViewDevice (Access::ReadOnly);
3792  auto x_lcl_1d = Kokkos::subview (x_lcl, Kokkos::ALL (), 0);
3794  leftScaleLocalCrsMatrix (getLocalMatrixDevice (),
3795  x_lcl_1d, false, false);
3796  }
3797  else {
3798  // 6/2020 Disallow leftScale of non-fillComplete matrices #7446
3799  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3800  (true, std::runtime_error, "CrsMatrix::leftScale requires matrix to be"
3801  " fillComplete");
3802  }
3803  }
3804 
3805  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3806  void
3809  {
3810  using ::Tpetra::Details::ProfilingRegion;
3811  using Teuchos::ArrayRCP;
3812  using Teuchos::ArrayView;
3813  using Teuchos::null;
3814  using Teuchos::RCP;
3815  using Teuchos::rcp;
3816  using Teuchos::rcpFromRef;
3818  const char tfecfFuncName[] = "rightScale: ";
3819 
3820  ProfilingRegion region ("Tpetra::CrsMatrix::rightScale");
3821 
3822  RCP<const vec_type> xp;
3823  if (this->getDomainMap ()->isSameAs (* (x.getMap ()))) {
3824  // Take from Epetra: If we have a non-trivial exporter, we must
3825  // import elements that are permuted or are on other processors.
3826  auto importer = this->getCrsGraphRef ().getImporter ();
3827  if (importer.get () != nullptr) {
3828  RCP<vec_type> tempVec (new vec_type (this->getColMap ()));
3829  tempVec->doImport (x, *importer, REPLACE);
3830  xp = tempVec;
3831  }
3832  else {
3833  xp = rcpFromRef (x);
3834  }
3835  }
3836  else if (this->getColMap ()->isSameAs (* (x.getMap ()))) {
3837  xp = rcpFromRef (x);
3838  } else {
3839  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3840  (true, std::runtime_error, "x's Map must be the same as "
3841  "either the domain Map or the column Map of the CrsMatrix.");
3842  }
3843 
3844  if (this->isFillComplete()) {
3845  auto x_lcl = xp->getLocalViewDevice (Access::ReadOnly);
3846  auto x_lcl_1d = Kokkos::subview (x_lcl, Kokkos::ALL (), 0);
3848  rightScaleLocalCrsMatrix (getLocalMatrixDevice (),
3849  x_lcl_1d, false, false);
3850  }
3851  else {
3852  // 6/2020 Disallow rightScale of non-fillComplete matrices #7446
3853  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
3854  (true, std::runtime_error, "CrsMatrix::rightScale requires matrix to be"
3855  " fillComplete");
3856  }
3857  }
3858 
3859  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3863  {
3864  using Teuchos::ArrayView;
3865  using Teuchos::outArg;
3866  using Teuchos::REDUCE_SUM;
3867  using Teuchos::reduceAll;
3868 
3869  // FIXME (mfh 05 Aug 2014) Write a thread-parallel kernel for the
3870  // local part of this computation. It could make sense to put
3871  // this operation in the Kokkos::CrsMatrix.
3872 
3873  // check the cache first
3874  mag_type mySum = STM::zero ();
3875  if (getLocalNumEntries() > 0) {
3876  if (isStorageOptimized ()) {
3877  // "Optimized" storage is packed storage. That means we can
3878  // iterate in one pass through the 1-D values array.
3879  const size_t numEntries = getLocalNumEntries ();
3880  auto values = valuesPacked_wdv.getHostView(Access::ReadOnly);
3881  for (size_t k = 0; k < numEntries; ++k) {
3882  auto val = values[k];
3883  // Note (etp 06 Jan 2015) We need abs() here for composite types
3884  // (in general, if mag_type is on the left-hand-side, we need
3885  // abs() on the right-hand-side)
3886  const mag_type val_abs = STS::abs (val);
3887  mySum += val_abs * val_abs;
3888  }
3889  }
3890  else {
3891  const LocalOrdinal numRows =
3892  static_cast<LocalOrdinal> (this->getLocalNumRows ());
3893  for (LocalOrdinal r = 0; r < numRows; ++r) {
3894  const RowInfo rowInfo = myGraph_->getRowInfo (r);
3895  const size_t numEntries = rowInfo.numEntries;
3896  auto A_r = this->getValuesViewHost(rowInfo);
3897  for (size_t k = 0; k < numEntries; ++k) {
3898  const impl_scalar_type val = A_r[k];
3899  const mag_type val_abs = STS::abs (val);
3900  mySum += val_abs * val_abs;
3901  }
3902  }
3903  }
3904  }
3905  mag_type totalSum = STM::zero ();
3906  reduceAll<int, mag_type> (* (getComm ()), REDUCE_SUM,
3907  mySum, outArg (totalSum));
3908  return STM::sqrt (totalSum);
3909  }
3910 
3911  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3912  void
3914  replaceColMap (const Teuchos::RCP<const map_type>& newColMap)
3915  {
3916  const char tfecfFuncName[] = "replaceColMap: ";
3917  // FIXME (mfh 06 Aug 2014) What if the graph is locally indexed?
3918  // Then replacing the column Map might mean that we need to
3919  // reindex the column indices.
3920  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3921  myGraph_.is_null (), std::runtime_error,
3922  "This method does not work if the matrix has a const graph. The whole "
3923  "idea of a const graph is that you are not allowed to change it, but "
3924  "this method necessarily must modify the graph, since the graph owns "
3925  "the matrix's column Map.");
3926  myGraph_->replaceColMap (newColMap);
3927  }
3928 
3929  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3930  void
3933  const Teuchos::RCP<const map_type>& newColMap,
3934  const Teuchos::RCP<const import_type>& newImport,
3935  const bool sortEachRow)
3936  {
3937  const char tfecfFuncName[] = "reindexColumns: ";
3938  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3939  graph == nullptr && myGraph_.is_null (), std::invalid_argument,
3940  "The input graph is null, but the matrix does not own its graph.");
3941 
3942  crs_graph_type& theGraph = (graph == nullptr) ? *myGraph_ : *graph;
3943  const bool sortGraph = false; // we'll sort graph & matrix together below
3944 
3945  theGraph.reindexColumns (newColMap, newImport, sortGraph);
3946 
3947  if (sortEachRow && theGraph.isLocallyIndexed () && ! theGraph.isSorted ()) {
3948  const LocalOrdinal lclNumRows =
3949  static_cast<LocalOrdinal> (theGraph.getLocalNumRows ());
3950 
3951  for (LocalOrdinal row = 0; row < lclNumRows; ++row) {
3952 
3953  const RowInfo rowInfo = theGraph.getRowInfo (row);
3954  auto lclColInds = theGraph.getLocalIndsViewHostNonConst (rowInfo);
3955  auto vals = this->getValuesViewHostNonConst (rowInfo);
3956 
3957  sort2 (lclColInds.data (),
3958  lclColInds.data () + rowInfo.numEntries,
3959  vals.data ());
3960  }
3961  theGraph.indicesAreSorted_ = true;
3962  }
3963  }
3964 
3965  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3966  void
3968  replaceDomainMap (const Teuchos::RCP<const map_type>& newDomainMap)
3969  {
3970  const char tfecfFuncName[] = "replaceDomainMap: ";
3971  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3972  myGraph_.is_null (), std::runtime_error,
3973  "This method does not work if the matrix has a const graph. The whole "
3974  "idea of a const graph is that you are not allowed to change it, but this"
3975  " method necessarily must modify the graph, since the graph owns the "
3976  "matrix's domain Map and Import objects.");
3977  myGraph_->replaceDomainMap (newDomainMap);
3978  }
3979 
3980  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3981  void
3983  replaceDomainMapAndImporter (const Teuchos::RCP<const map_type>& newDomainMap,
3984  Teuchos::RCP<const import_type>& newImporter)
3985  {
3986  const char tfecfFuncName[] = "replaceDomainMapAndImporter: ";
3987  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
3988  myGraph_.is_null (), std::runtime_error,
3989  "This method does not work if the matrix has a const graph. The whole "
3990  "idea of a const graph is that you are not allowed to change it, but this"
3991  " method necessarily must modify the graph, since the graph owns the "
3992  "matrix's domain Map and Import objects.");
3993  myGraph_->replaceDomainMapAndImporter (newDomainMap, newImporter);
3994  }
3995 
3996  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
3997  void
3999  replaceRangeMap (const Teuchos::RCP<const map_type>& newRangeMap)
4000  {
4001  const char tfecfFuncName[] = "replaceRangeMap: ";
4002  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
4003  myGraph_.is_null (), std::runtime_error,
4004  "This method does not work if the matrix has a const graph. The whole "
4005  "idea of a const graph is that you are not allowed to change it, but this"
4006  " method necessarily must modify the graph, since the graph owns the "
4007  "matrix's domain Map and Import objects.");
4008  myGraph_->replaceRangeMap (newRangeMap);
4009  }
4010 
4011  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4012  void
4014  replaceRangeMapAndExporter (const Teuchos::RCP<const map_type>& newRangeMap,
4015  Teuchos::RCP<const export_type>& newExporter)
4016  {
4017  const char tfecfFuncName[] = "replaceRangeMapAndExporter: ";
4018  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
4019  myGraph_.is_null (), std::runtime_error,
4020  "This method does not work if the matrix has a const graph. The whole "
4021  "idea of a const graph is that you are not allowed to change it, but this"
4022  " method necessarily must modify the graph, since the graph owns the "
4023  "matrix's domain Map and Import objects.");
4024  myGraph_->replaceRangeMapAndExporter (newRangeMap, newExporter);
4025  }
4026 
4027  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4028  void
4030  insertNonownedGlobalValues (const GlobalOrdinal globalRow,
4031  const Teuchos::ArrayView<const GlobalOrdinal>& indices,
4032  const Teuchos::ArrayView<const Scalar>& values)
4033  {
4034  using Teuchos::Array;
4035  typedef GlobalOrdinal GO;
4036  typedef typename Array<GO>::size_type size_type;
4037 
4038  const size_type numToInsert = indices.size ();
4039  // Add the new data to the list of nonlocals.
4040  // This creates the arrays if they don't exist yet.
4041  std::pair<Array<GO>, Array<Scalar> >& curRow = nonlocals_[globalRow];
4042  Array<GO>& curRowInds = curRow.first;
4043  Array<Scalar>& curRowVals = curRow.second;
4044  const size_type newCapacity = curRowInds.size () + numToInsert;
4045  curRowInds.reserve (newCapacity);
4046  curRowVals.reserve (newCapacity);
4047  for (size_type k = 0; k < numToInsert; ++k) {
4048  curRowInds.push_back (indices[k]);
4049  curRowVals.push_back (values[k]);
4050  }
4051  }
4052 
4053  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4054  void
4057  {
4058  using Details::Behavior;
4060  using Teuchos::Comm;
4061  using Teuchos::outArg;
4062  using Teuchos::RCP;
4063  using Teuchos::rcp;
4064  using Teuchos::REDUCE_MAX;
4065  using Teuchos::REDUCE_MIN;
4066  using Teuchos::reduceAll;
4067  using std::endl;
4069  //typedef LocalOrdinal LO;
4070  typedef GlobalOrdinal GO;
4071  typedef typename Teuchos::Array<GO>::size_type size_type;
4072  const char tfecfFuncName[] = "globalAssemble: "; // for exception macro
4073  ProfilingRegion regionGlobalAssemble ("Tpetra::CrsMatrix::globalAssemble");
4074 
4075  const bool verbose = Behavior::verbose("CrsMatrix");
4076  std::unique_ptr<std::string> prefix;
4077  if (verbose) {
4078  prefix = this->createPrefix("CrsMatrix", "globalAssemble");
4079  std::ostringstream os;
4080  os << *prefix << "nonlocals_.size()=" << nonlocals_.size()
4081  << endl;
4082  std::cerr << os.str();
4083  }
4084  RCP<const Comm<int> > comm = getComm ();
4085 
4086  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4087  (! isFillActive (), std::runtime_error, "Fill must be active before "
4088  "you may call this method.");
4089 
4090  const size_t myNumNonlocalRows = nonlocals_.size ();
4091 
4092  // If no processes have nonlocal rows, then we don't have to do
4093  // anything. Checking this is probably cheaper than constructing
4094  // the Map of nonlocal rows (see below) and noticing that it has
4095  // zero global entries.
4096  {
4097  const int iHaveNonlocalRows = (myNumNonlocalRows == 0) ? 0 : 1;
4098  int someoneHasNonlocalRows = 0;
4099  reduceAll<int, int> (*comm, REDUCE_MAX, iHaveNonlocalRows,
4100  outArg (someoneHasNonlocalRows));
4101  if (someoneHasNonlocalRows == 0) {
4102  return; // no process has nonlocal rows, so nothing to do
4103  }
4104  }
4105 
4106  // 1. Create a list of the "nonlocal" rows on each process. this
4107  // requires iterating over nonlocals_, so while we do this,
4108  // deduplicate the entries and get a count for each nonlocal
4109  // row on this process.
4110  // 2. Construct a new row Map corresponding to those rows. This
4111  // Map is likely overlapping. We know that the Map is not
4112  // empty on all processes, because the above all-reduce and
4113  // return exclude that case.
4114 
4115  RCP<const map_type> nonlocalRowMap;
4116  Teuchos::Array<size_t> numEntPerNonlocalRow (myNumNonlocalRows);
4117  {
4118  Teuchos::Array<GO> myNonlocalGblRows (myNumNonlocalRows);
4119  size_type curPos = 0;
4120  for (auto mapIter = nonlocals_.begin (); mapIter != nonlocals_.end ();
4121  ++mapIter, ++curPos) {
4122  myNonlocalGblRows[curPos] = mapIter->first;
4123  // Get the values and column indices by reference, since we
4124  // intend to change them in place (that's what "erase" does).
4125  Teuchos::Array<GO>& gblCols = (mapIter->second).first;
4126  Teuchos::Array<Scalar>& vals = (mapIter->second).second;
4127 
4128  // Sort both arrays jointly, using the column indices as keys,
4129  // then merge them jointly. "Merge" here adds values
4130  // corresponding to the same column indices. The first 2 args
4131  // of merge2 are output arguments that work just like the
4132  // return value of std::unique.
4133  sort2 (gblCols.begin (), gblCols.end (), vals.begin ());
4134  typename Teuchos::Array<GO>::iterator gblCols_newEnd;
4135  typename Teuchos::Array<Scalar>::iterator vals_newEnd;
4136  merge2 (gblCols_newEnd, vals_newEnd,
4137  gblCols.begin (), gblCols.end (),
4138  vals.begin (), vals.end ());
4139  gblCols.erase (gblCols_newEnd, gblCols.end ());
4140  vals.erase (vals_newEnd, vals.end ());
4141  numEntPerNonlocalRow[curPos] = gblCols.size ();
4142  }
4143 
4144  // Currently, Map requires that its indexBase be the global min
4145  // of all its global indices. Map won't compute this for us, so
4146  // we must do it. If our process has no nonlocal rows, set the
4147  // "min" to the max possible GO value. This ensures that if
4148  // some process has at least one nonlocal row, then it will pick
4149  // that up as the min. We know that at least one process has a
4150  // nonlocal row, since the all-reduce and return at the top of
4151  // this method excluded that case.
4152  GO myMinNonlocalGblRow = std::numeric_limits<GO>::max ();
4153  {
4154  auto iter = std::min_element (myNonlocalGblRows.begin (),
4155  myNonlocalGblRows.end ());
4156  if (iter != myNonlocalGblRows.end ()) {
4157  myMinNonlocalGblRow = *iter;
4158  }
4159  }
4160  GO gblMinNonlocalGblRow = 0;
4161  reduceAll<int, GO> (*comm, REDUCE_MIN, myMinNonlocalGblRow,
4162  outArg (gblMinNonlocalGblRow));
4163  const GO indexBase = gblMinNonlocalGblRow;
4164  const global_size_t INV = Teuchos::OrdinalTraits<global_size_t>::invalid ();
4165  nonlocalRowMap = rcp (new map_type (INV, myNonlocalGblRows (), indexBase, comm));
4166  }
4167 
4168  // 3. Use the values and column indices for each nonlocal row, as
4169  // stored in nonlocals_, to construct a CrsMatrix corresponding
4170  // to nonlocal rows. We have
4171  // exact counts of the number of entries in each nonlocal row.
4172 
4173  if (verbose) {
4174  std::ostringstream os;
4175  os << *prefix << "Create nonlocal matrix" << endl;
4176  std::cerr << os.str();
4177  }
4178  RCP<crs_matrix_type> nonlocalMatrix =
4179  rcp (new crs_matrix_type (nonlocalRowMap, numEntPerNonlocalRow ()));
4180  {
4181  size_type curPos = 0;
4182  for (auto mapIter = nonlocals_.begin (); mapIter != nonlocals_.end ();
4183  ++mapIter, ++curPos) {
4184  const GO gblRow = mapIter->first;
4185  // Get values & column indices by ref, just to avoid copy.
4186  Teuchos::Array<GO>& gblCols = (mapIter->second).first;
4187  Teuchos::Array<Scalar>& vals = (mapIter->second).second;
4188  //const LO numEnt = static_cast<LO> (numEntPerNonlocalRow[curPos]);
4189  nonlocalMatrix->insertGlobalValues (gblRow, gblCols (), vals ());
4190  }
4191  }
4192  // There's no need to fill-complete the nonlocals matrix.
4193  // We just use it as a temporary container for the Export.
4194 
4195  // 4. If the original row Map is one to one, then we can Export
4196  // directly from nonlocalMatrix into this. Otherwise, we have
4197  // to create a temporary matrix with a one-to-one row Map,
4198  // Export into that, then Import from the temporary matrix into
4199  // *this.
4200 
4201  auto origRowMap = this->getRowMap ();
4202  const bool origRowMapIsOneToOne = origRowMap->isOneToOne ();
4203 
4204  int isLocallyComplete = 1; // true by default
4205 
4206  if (origRowMapIsOneToOne) {
4207  if (verbose) {
4208  std::ostringstream os;
4209  os << *prefix << "Original row Map is 1-to-1" << endl;
4210  std::cerr << os.str();
4211  }
4212  export_type exportToOrig (nonlocalRowMap, origRowMap);
4213  if (! exportToOrig.isLocallyComplete ()) {
4214  isLocallyComplete = 0;
4215  }
4216  if (verbose) {
4217  std::ostringstream os;
4218  os << *prefix << "doExport from nonlocalMatrix" << endl;
4219  std::cerr << os.str();
4220  }
4221  this->doExport (*nonlocalMatrix, exportToOrig, Tpetra::ADD);
4222  // We're done at this point!
4223  }
4224  else {
4225  if (verbose) {
4226  std::ostringstream os;
4227  os << *prefix << "Original row Map is NOT 1-to-1" << endl;
4228  std::cerr << os.str();
4229  }
4230  // If you ask a Map whether it is one to one, it does some
4231  // communication and stashes intermediate results for later use
4232  // by createOneToOne. Thus, calling createOneToOne doesn't cost
4233  // much more then the original cost of calling isOneToOne.
4234  auto oneToOneRowMap = Tpetra::createOneToOne (origRowMap);
4235  export_type exportToOneToOne (nonlocalRowMap, oneToOneRowMap);
4236  if (! exportToOneToOne.isLocallyComplete ()) {
4237  isLocallyComplete = 0;
4238  }
4239 
4240  // Create a temporary matrix with the one-to-one row Map.
4241  //
4242  // TODO (mfh 09 Sep 2016, 12 Sep 2016) Estimate # entries in
4243  // each row, to avoid reallocation during the Export operation.
4244  if (verbose) {
4245  std::ostringstream os;
4246  os << *prefix << "Create & doExport into 1-to-1 matrix"
4247  << endl;
4248  std::cerr << os.str();
4249  }
4250  crs_matrix_type oneToOneMatrix (oneToOneRowMap, 0);
4251  // Export from matrix of nonlocals into the temp one-to-one matrix.
4252  oneToOneMatrix.doExport(*nonlocalMatrix, exportToOneToOne,
4253  Tpetra::ADD);
4254 
4255  // We don't need the matrix of nonlocals anymore, so get rid of
4256  // it, to keep the memory high-water mark down.
4257  if (verbose) {
4258  std::ostringstream os;
4259  os << *prefix << "Free nonlocalMatrix" << endl;
4260  std::cerr << os.str();
4261  }
4262  nonlocalMatrix = Teuchos::null;
4263 
4264  // Import from the one-to-one matrix to the original matrix.
4265  if (verbose) {
4266  std::ostringstream os;
4267  os << *prefix << "doImport from 1-to-1 matrix" << endl;
4268  std::cerr << os.str();
4269  }
4270  import_type importToOrig (oneToOneRowMap, origRowMap);
4271  this->doImport (oneToOneMatrix, importToOrig, Tpetra::ADD);
4272  }
4273 
4274  // It's safe now to clear out nonlocals_, since we've already
4275  // committed side effects to *this. The standard idiom for
4276  // clearing a Container like std::map, is to swap it with an empty
4277  // Container and let the swapped Container fall out of scope.
4278  if (verbose) {
4279  std::ostringstream os;
4280  os << *prefix << "Free nonlocals_ (std::map)" << endl;
4281  std::cerr << os.str();
4282  }
4283  decltype (nonlocals_) newNonlocals;
4284  std::swap (nonlocals_, newNonlocals);
4285 
4286  // FIXME (mfh 12 Sep 2016) I don't like this all-reduce, and I
4287  // don't like throwing an exception here. A local return value
4288  // would likely be more useful to users. However, if users find
4289  // themselves exercising nonlocal inserts often, then they are
4290  // probably novice users who need the help. See Gibhub Issues
4291  // #603 and #601 (esp. the latter) for discussion.
4292 
4293  int isGloballyComplete = 0; // output argument of reduceAll
4294  reduceAll<int, int> (*comm, REDUCE_MIN, isLocallyComplete,
4295  outArg (isGloballyComplete));
4296  TEUCHOS_TEST_FOR_EXCEPTION
4297  (isGloballyComplete != 1, std::runtime_error, "On at least one process, "
4298  "you called insertGlobalValues with a global row index which is not in "
4299  "the matrix's row Map on any process in its communicator.");
4300  }
4301 
4302  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4303  void
4305  resumeFill (const Teuchos::RCP<Teuchos::ParameterList>& params)
4306  {
4307  if (! isStaticGraph ()) { // Don't resume fill of a nonowned graph.
4308  myGraph_->resumeFill (params);
4309  }
4310 #if KOKKOSKERNELS_VERSION >= 40299
4311  // Delete the apply helper (if it exists)
4312  applyHelper.reset();
4313 #endif
4314  fillComplete_ = false;
4315  }
4316 
4317  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4318  bool
4321  return getCrsGraphRef ().haveGlobalConstants ();
4322  }
4323 
4324  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4325  void
4327  fillComplete (const Teuchos::RCP<Teuchos::ParameterList>& params)
4328  {
4329  const char tfecfFuncName[] = "fillComplete(params): ";
4330 
4331  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4332  (this->getCrsGraph ().is_null (), std::logic_error,
4333  "getCrsGraph() returns null. This should not happen at this point. "
4334  "Please report this bug to the Tpetra developers.");
4335 
4336  const crs_graph_type& graph = this->getCrsGraphRef ();
4337  if (this->isStaticGraph () && graph.isFillComplete ()) {
4338  // If this matrix's graph is fill complete and the user did not
4339  // supply a domain or range Map, use the graph's domain and
4340  // range Maps.
4341  this->fillComplete (graph.getDomainMap (), graph.getRangeMap (), params);
4342  }
4343  else { // assume that user's row Map is the domain and range Map
4344  Teuchos::RCP<const map_type> rangeMap = graph.getRowMap ();
4345  Teuchos::RCP<const map_type> domainMap = rangeMap;
4346  this->fillComplete (domainMap, rangeMap, params);
4347  }
4348  }
4349 
4350  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4351  void
4353  fillComplete (const Teuchos::RCP<const map_type>& domainMap,
4354  const Teuchos::RCP<const map_type>& rangeMap,
4355  const Teuchos::RCP<Teuchos::ParameterList>& params)
4356  {
4357  using Details::Behavior;
4359  using Teuchos::ArrayRCP;
4360  using Teuchos::RCP;
4361  using Teuchos::rcp;
4362  using std::endl;
4363  const char tfecfFuncName[] = "fillComplete: ";
4364  ProfilingRegion regionFillComplete
4365  ("Tpetra::CrsMatrix::fillComplete");
4366  const bool verbose = Behavior::verbose("CrsMatrix");
4367  std::unique_ptr<std::string> prefix;
4368  if (verbose) {
4369  prefix = this->createPrefix("CrsMatrix", "fillComplete(dom,ran,p)");
4370  std::ostringstream os;
4371  os << *prefix << endl;
4372  std::cerr << os.str ();
4373  }
4374  Details::ProfilingRegion region(
4375  "Tpetra::CrsMatrix::fillCompete",
4376  "fillCompete");
4377 
4378  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4379  (! this->isFillActive () || this->isFillComplete (), std::runtime_error,
4380  "Matrix fill state must be active (isFillActive() "
4381  "must be true) before you may call fillComplete().");
4382  const int numProcs = this->getComm ()->getSize ();
4383 
4384  //
4385  // Read parameters from the input ParameterList.
4386  //
4387  {
4388  Details::ProfilingRegion region_fc("Tpetra::CrsMatrix::fillCompete", "ParameterList");
4389 
4390  // If true, the caller promises that no process did nonlocal
4391  // changes since the last call to fillComplete.
4392  bool assertNoNonlocalInserts = false;
4393  // If true, makeColMap sorts remote GIDs (within each remote
4394  // process' group).
4395  bool sortGhosts = true;
4396 
4397  if (! params.is_null ()) {
4398  assertNoNonlocalInserts = params->get ("No Nonlocal Changes",
4399  assertNoNonlocalInserts);
4400  if (params->isParameter ("sort column map ghost gids")) {
4401  sortGhosts = params->get ("sort column map ghost gids", sortGhosts);
4402  }
4403  else if (params->isParameter ("Sort column Map ghost GIDs")) {
4404  sortGhosts = params->get ("Sort column Map ghost GIDs", sortGhosts);
4405  }
4406  }
4407  // We also don't need to do global assembly if there is only one
4408  // process in the communicator.
4409  const bool needGlobalAssemble = ! assertNoNonlocalInserts && numProcs > 1;
4410  // This parameter only matters if this matrix owns its graph.
4411  if (! this->myGraph_.is_null ()) {
4412  this->myGraph_->sortGhostsAssociatedWithEachProcessor_ = sortGhosts;
4413  }
4414 
4415  if (! this->getCrsGraphRef ().indicesAreAllocated ()) {
4416  if (this->hasColMap ()) { // use local indices
4417  allocateValues(LocalIndices, GraphNotYetAllocated, verbose);
4418  }
4419  else { // no column Map, so use global indices
4420  allocateValues(GlobalIndices, GraphNotYetAllocated, verbose);
4421  }
4422  }
4423  // Global assemble, if we need to. This call only costs a single
4424  // all-reduce if we didn't need global assembly after all.
4425  if (needGlobalAssemble) {
4426  this->globalAssemble ();
4427  }
4428  else {
4429  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4430  (numProcs == 1 && nonlocals_.size() > 0,
4431  std::runtime_error, "Cannot have nonlocal entries on a serial run. "
4432  "An invalid entry (i.e., with row index not in the row Map) must have "
4433  "been submitted to the CrsMatrix.");
4434  }
4435  }
4436  if (this->isStaticGraph ()) {
4437  Details::ProfilingRegion region_isg("Tpetra::CrsMatrix::fillCompete", "isStaticGraph");
4438  // FIXME (mfh 14 Nov 2016) In order to fix #843, I enable the
4439  // checks below only in debug mode. It would be nicer to do a
4440  // local check, then propagate the error state in a deferred
4441  // way, whenever communication happens. That would reduce the
4442  // cost of checking, to the point where it may make sense to
4443  // enable it even in release mode.
4444 #ifdef HAVE_TPETRA_DEBUG
4445  // FIXME (mfh 18 Jun 2014) This check for correctness of the
4446  // input Maps incurs a penalty of two all-reduces for the
4447  // otherwise optimal const graph case.
4448  //
4449  // We could turn these (max) 2 all-reduces into (max) 1, by
4450  // fusing them. We could do this by adding a "locallySameAs"
4451  // method to Map, which would return one of four states:
4452  //
4453  // a. Certainly globally the same
4454  // b. Certainly globally not the same
4455  // c. Locally the same
4456  // d. Locally not the same
4457  //
4458  // The first two states don't require further communication.
4459  // The latter two states require an all-reduce to communicate
4460  // globally, but we only need one all-reduce, since we only need
4461  // to check whether at least one of the Maps is wrong.
4462  const bool domainMapsMatch =
4463  this->staticGraph_->getDomainMap ()->isSameAs (*domainMap);
4464  const bool rangeMapsMatch =
4465  this->staticGraph_->getRangeMap ()->isSameAs (*rangeMap);
4466 
4467  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4468  (! domainMapsMatch, std::runtime_error,
4469  "The CrsMatrix's domain Map does not match the graph's domain Map. "
4470  "The graph cannot be changed because it was given to the CrsMatrix "
4471  "constructor as const. You can fix this by passing in the graph's "
4472  "domain Map and range Map to the matrix's fillComplete call.");
4473 
4474  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4475  (! rangeMapsMatch, std::runtime_error,
4476  "The CrsMatrix's range Map does not match the graph's range Map. "
4477  "The graph cannot be changed because it was given to the CrsMatrix "
4478  "constructor as const. You can fix this by passing in the graph's "
4479  "domain Map and range Map to the matrix's fillComplete call.");
4480 #endif // HAVE_TPETRA_DEBUG
4481 
4482  // The matrix does _not_ own the graph, and the graph's
4483  // structure is already fixed, so just fill the local matrix.
4484  this->fillLocalMatrix (params);
4485  }
4486  else {
4487  Details::ProfilingRegion region_insg("Tpetra::CrsMatrix::fillCompete", "isNotStaticGraph");
4488  // Set the graph's domain and range Maps. This will clear the
4489  // Import if the domain Map has changed (is a different
4490  // pointer), and the Export if the range Map has changed (is a
4491  // different pointer).
4492  this->myGraph_->setDomainRangeMaps (domainMap, rangeMap);
4493 
4494  // Make the graph's column Map, if necessary.
4495  Teuchos::Array<int> remotePIDs (0);
4496  const bool mustBuildColMap = ! this->hasColMap ();
4497  if (mustBuildColMap) {
4498  this->myGraph_->makeColMap (remotePIDs);
4499  }
4500 
4501  // Make indices local, if necessary. The method won't do
4502  // anything if the graph is already locally indexed.
4503  const std::pair<size_t, std::string> makeIndicesLocalResult =
4504  this->myGraph_->makeIndicesLocal(verbose);
4505  // TODO (mfh 20 Jul 2017) Instead of throwing here, pass along
4506  // the error state to makeImportExport
4507  // which may do all-reduces and thus may
4508  // have the opportunity to communicate that error state.
4509  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4510  (makeIndicesLocalResult.first != 0, std::runtime_error,
4511  makeIndicesLocalResult.second);
4512 
4513  const bool sorted = this->myGraph_->isSorted ();
4514  const bool merged = this->myGraph_->isMerged ();
4515  this->sortAndMergeIndicesAndValues (sorted, merged);
4516 
4517  // Make Import and Export objects, if they haven't been made
4518  // already. If we made a column Map above, reuse information
4519  // from that process to avoid communiation in the Import setup.
4520  this->myGraph_->makeImportExport (remotePIDs, mustBuildColMap);
4521 
4522  // The matrix _does_ own the graph, so fill the local graph at
4523  // the same time as the local matrix.
4524  this->fillLocalGraphAndMatrix (params);
4525 
4526  const bool callGraphComputeGlobalConstants = params.get () == nullptr ||
4527  params->get ("compute global constants", true);
4528  if (callGraphComputeGlobalConstants) {
4529  this->myGraph_->computeGlobalConstants ();
4530  }
4531  else {
4532  this->myGraph_->computeLocalConstants ();
4533  }
4534  this->myGraph_->fillComplete_ = true;
4535  this->myGraph_->checkInternalState ();
4536  }
4537 
4538  // FIXME (mfh 28 Aug 2014) "Preserve Local Graph" bool parameter no longer used.
4539 
4540  this->fillComplete_ = true; // Now we're fill complete!
4541  {
4542  Details::ProfilingRegion region_cis(
4543  "Tpetra::CrsMatrix::fillCompete", "checkInternalState"
4544  );
4545  this->checkInternalState ();
4546  }
4547  } //fillComplete(domainMap, rangeMap, params)
4548 
4549  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4550  void
4552  expertStaticFillComplete (const Teuchos::RCP<const map_type> & domainMap,
4553  const Teuchos::RCP<const map_type> & rangeMap,
4554  const Teuchos::RCP<const import_type>& importer,
4555  const Teuchos::RCP<const export_type>& exporter,
4556  const Teuchos::RCP<Teuchos::ParameterList> &params)
4557  {
4558 #ifdef HAVE_TPETRA_MMM_TIMINGS
4559  std::string label;
4560  if(!params.is_null())
4561  label = params->get("Timer Label",label);
4562  std::string prefix = std::string("Tpetra ")+ label + std::string(": ");
4563  using Teuchos::TimeMonitor;
4564 
4565  Teuchos::TimeMonitor all(*TimeMonitor::getNewTimer(prefix + std::string("ESFC-all")));
4566 #endif
4567 
4568  const char tfecfFuncName[] = "expertStaticFillComplete: ";
4569  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( ! isFillActive() || isFillComplete(),
4570  std::runtime_error, "Matrix fill state must be active (isFillActive() "
4571  "must be true) before calling fillComplete().");
4572  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
4573  myGraph_.is_null (), std::logic_error, "myGraph_ is null. This is not allowed.");
4574 
4575  {
4576 #ifdef HAVE_TPETRA_MMM_TIMINGS
4577  Teuchos::TimeMonitor graph(*TimeMonitor::getNewTimer(prefix + std::string("eSFC-M-Graph")));
4578 #endif
4579  // We will presume globalAssemble is not needed, so we do the ESFC on the graph
4580  myGraph_->expertStaticFillComplete (domainMap, rangeMap, importer, exporter,params);
4581  }
4582 
4583  {
4584 #ifdef HAVE_TPETRA_MMM_TIMINGS
4585  TimeMonitor fLGAM(*TimeMonitor::getNewTimer(prefix + std::string("eSFC-M-fLGAM")));
4586 #endif
4587  // Fill the local graph and matrix
4588  fillLocalGraphAndMatrix (params);
4589  }
4590  // FIXME (mfh 28 Aug 2014) "Preserve Local Graph" bool parameter no longer used.
4591 
4592  // Now we're fill complete!
4593  fillComplete_ = true;
4594 
4595  // Sanity checks at the end.
4596 #ifdef HAVE_TPETRA_DEBUG
4597  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(isFillActive(), std::logic_error,
4598  ": We're at the end of fillComplete(), but isFillActive() is true. "
4599  "Please report this bug to the Tpetra developers.");
4600  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(! isFillComplete(), std::logic_error,
4601  ": We're at the end of fillComplete(), but isFillActive() is true. "
4602  "Please report this bug to the Tpetra developers.");
4603 #endif // HAVE_TPETRA_DEBUG
4604  {
4605 #ifdef HAVE_TPETRA_MMM_TIMINGS
4606  Teuchos::TimeMonitor cIS(*TimeMonitor::getNewTimer(prefix + std::string("ESFC-M-cIS")));
4607 #endif
4608 
4609  checkInternalState();
4610  }
4611  }
4612 
4613  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4615  mergeRowIndicesAndValues (size_t rowLen, LocalOrdinal* cols, impl_scalar_type* vals)
4616  {
4617  impl_scalar_type* rowValueIter = vals;
4618  // beg,end define a half-exclusive interval over which to iterate.
4619  LocalOrdinal* beg = cols;
4620  LocalOrdinal* end = cols + rowLen;
4621  LocalOrdinal* newend = beg;
4622  if (beg != end) {
4623  LocalOrdinal* cur = beg + 1;
4624  impl_scalar_type* vcur = rowValueIter + 1;
4625  impl_scalar_type* vend = rowValueIter;
4626  cur = beg+1;
4627  while (cur != end) {
4628  if (*cur != *newend) {
4629  // new entry; save it
4630  ++newend;
4631  ++vend;
4632  (*newend) = (*cur);
4633  (*vend) = (*vcur);
4634  }
4635  else {
4636  // old entry; merge it
4637  //(*vend) = f (*vend, *vcur);
4638  (*vend) += *vcur;
4639  }
4640  ++cur;
4641  ++vcur;
4642  }
4643  ++newend; // one past the last entry, per typical [beg,end) semantics
4644  }
4645  return newend - beg;
4646  }
4647 
4648  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4649  void
4651  sortAndMergeIndicesAndValues (const bool sorted, const bool merged)
4652  {
4653  using ::Tpetra::Details::ProfilingRegion;
4654  typedef LocalOrdinal LO;
4655  typedef typename Kokkos::View<LO*, device_type>::HostMirror::execution_space
4656  host_execution_space;
4657  typedef Kokkos::RangePolicy<host_execution_space, LO> range_type;
4658  const char tfecfFuncName[] = "sortAndMergeIndicesAndValues: ";
4659  ProfilingRegion regionSAM ("Tpetra::CrsMatrix::sortAndMergeIndicesAndValues");
4660 
4661  if (! sorted || ! merged) {
4662  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4663  (this->isStaticGraph (), std::runtime_error, "Cannot sort or merge with "
4664  "\"static\" (const) graph, since the matrix does not own the graph.");
4665  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4666  (this->myGraph_.is_null (), std::logic_error, "myGraph_ is null, but "
4667  "this matrix claims ! isStaticGraph(). "
4668  "Please report this bug to the Tpetra developers.");
4669  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
4670  (this->isStorageOptimized (), std::logic_error, "It is invalid to call "
4671  "this method if the graph's storage has already been optimized. "
4672  "Please report this bug to the Tpetra developers.");
4673 
4674  crs_graph_type& graph = * (this->myGraph_);
4675  const LO lclNumRows = static_cast<LO> (this->getLocalNumRows ());
4676  size_t totalNumDups = 0;
4677  {
4678  //Accessing host unpacked (4-array CRS) local matrix.
4679  auto rowBegins_ = graph.getRowPtrsUnpackedHost();
4680  auto rowLengths_ = graph.k_numRowEntries_;
4681  auto vals_ = this->valuesUnpacked_wdv.getHostView(Access::ReadWrite);
4682  auto cols_ = graph.lclIndsUnpacked_wdv.getHostView(Access::ReadWrite);
4683  Kokkos::parallel_reduce ("sortAndMergeIndicesAndValues", range_type (0, lclNumRows),
4684  [=] (const LO lclRow, size_t& numDups) {
4685  size_t rowBegin = rowBegins_(lclRow);
4686  size_t rowLen = rowLengths_(lclRow);
4687  LO* cols = cols_.data() + rowBegin;
4688  impl_scalar_type* vals = vals_.data() + rowBegin;
4689  if (! sorted) {
4690  sort2 (cols, cols + rowLen, vals);
4691  }
4692  if (! merged) {
4693  size_t newRowLength = mergeRowIndicesAndValues (rowLen, cols, vals);
4694  rowLengths_(lclRow) = newRowLength;
4695  numDups += rowLen - newRowLength;
4696  }
4697  }, totalNumDups);
4698  }
4699  if (! sorted) {
4700  graph.indicesAreSorted_ = true; // we just sorted every row
4701  }
4702  if (! merged) {
4703  graph.noRedundancies_ = true; // we just merged every row
4704  }
4705  }
4706  }
4707 
4708  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4709  void
4713  Scalar alpha,
4714  Scalar beta) const
4715  {
4717  using Teuchos::RCP;
4718  using Teuchos::rcp;
4719  using Teuchos::rcp_const_cast;
4720  using Teuchos::rcpFromRef;
4721  const Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero ();
4722  const Scalar ONE = Teuchos::ScalarTraits<Scalar>::one ();
4723 
4724  // mfh 05 Jun 2014: Special case for alpha == 0. I added this to
4725  // fix an Ifpack2 test (RILUKSingleProcessUnitTests), which was
4726  // failing only for the Kokkos refactor version of Tpetra. It's a
4727  // good idea regardless to have the bypass.
4728  if (alpha == ZERO) {
4729  if (beta == ZERO) {
4730  Y_in.putScalar (ZERO);
4731  } else if (beta != ONE) {
4732  Y_in.scale (beta);
4733  }
4734  return;
4735  }
4736 
4737  // It's possible that X is a view of Y or vice versa. We don't
4738  // allow this (apply() requires that X and Y not alias one
4739  // another), but it's helpful to detect and work around this case.
4740  // We don't try to to detect the more subtle cases (e.g., one is a
4741  // subview of the other, but their initial pointers differ). We
4742  // only need to do this if this matrix's Import is trivial;
4743  // otherwise, we don't actually apply the operator from X into Y.
4744 
4745  RCP<const import_type> importer = this->getGraph ()->getImporter ();
4746  RCP<const export_type> exporter = this->getGraph ()->getExporter ();
4747 
4748  // If beta == 0, then the output MV will be overwritten; none of
4749  // its entries should be read. (Sparse BLAS semantics say that we
4750  // must ignore any Inf or NaN entries in Y_in, if beta is zero.)
4751  // This matters if we need to do an Export operation; see below.
4752  const bool Y_is_overwritten = (beta == ZERO);
4753 
4754  // We treat the case of a replicated MV output specially.
4755  const bool Y_is_replicated =
4756  (! Y_in.isDistributed () && this->getComm ()->getSize () != 1);
4757 
4758  // This is part of the special case for replicated MV output.
4759  // We'll let each process do its thing, but do an all-reduce at
4760  // the end to sum up the results. Setting beta=0 on all processes
4761  // but Proc 0 makes the math work out for the all-reduce. (This
4762  // assumes that the replicated data is correctly replicated, so
4763  // that the data are the same on all processes.)
4764  if (Y_is_replicated && this->getComm ()->getRank () > 0) {
4765  beta = ZERO;
4766  }
4767 
4768  // Temporary MV for Import operation. After the block of code
4769  // below, this will be an (Imported if necessary) column Map MV
4770  // ready to give to localApply(...).
4771  RCP<const MV> X_colMap;
4772  if (importer.is_null ()) {
4773  if (! X_in.isConstantStride ()) {
4774  // Not all sparse mat-vec kernels can handle an input MV with
4775  // nonconstant stride correctly, so we have to copy it in that
4776  // case into a constant stride MV. To make a constant stride
4777  // copy of X_in, we force creation of the column (== domain)
4778  // Map MV (if it hasn't already been created, else fetch the
4779  // cached copy). This avoids creating a new MV each time.
4780  RCP<MV> X_colMapNonConst = getColumnMapMultiVector (X_in, true);
4781  Tpetra::deep_copy (*X_colMapNonConst, X_in);
4782  X_colMap = rcp_const_cast<const MV> (X_colMapNonConst);
4783  }
4784  else {
4785  // The domain and column Maps are the same, so do the local
4786  // multiply using the domain Map input MV X_in.
4787  X_colMap = rcpFromRef (X_in);
4788  }
4789  }
4790  else { // need to Import source (multi)vector
4791  ProfilingRegion regionImport ("Tpetra::CrsMatrix::apply: Import");
4792 
4793  // We're doing an Import anyway, which will copy the relevant
4794  // elements of the domain Map MV X_in into a separate column Map
4795  // MV. Thus, we don't have to worry whether X_in is constant
4796  // stride.
4797  RCP<MV> X_colMapNonConst = getColumnMapMultiVector (X_in);
4798 
4799  // Import from the domain Map MV to the column Map MV.
4800  X_colMapNonConst->doImport (X_in, *importer, INSERT);
4801  X_colMap = rcp_const_cast<const MV> (X_colMapNonConst);
4802  }
4803 
4804  // Temporary MV for doExport (if needed), or for copying a
4805  // nonconstant stride output MV into a constant stride MV. This
4806  // is null if we don't need the temporary MV, that is, if the
4807  // Export is trivial (null).
4808  RCP<MV> Y_rowMap = getRowMapMultiVector (Y_in);
4809 
4810  // If we have a nontrivial Export object, we must perform an
4811  // Export. In that case, the local multiply result will go into
4812  // the row Map multivector. We don't have to make a
4813  // constant-stride version of Y_in in this case, because we had to
4814  // make a constant stride Y_rowMap MV and do an Export anyway.
4815  if (! exporter.is_null ()) {
4816  this->localApply (*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, ZERO);
4817  {
4818  ProfilingRegion regionExport ("Tpetra::CrsMatrix::apply: Export");
4819 
4820  // If we're overwriting the output MV Y_in completely (beta ==
4821  // 0), then make sure that it is filled with zeros before we
4822  // do the Export. Otherwise, the ADD combine mode will use
4823  // data in Y_in, which is supposed to be zero.
4824  if (Y_is_overwritten) {
4825  Y_in.putScalar (ZERO);
4826  }
4827  else {
4828  // Scale output MV by beta, so that doExport sums in the
4829  // mat-vec contribution: Y_in = beta*Y_in + alpha*A*X_in.
4830  Y_in.scale (beta);
4831  }
4832  // Do the Export operation.
4833  Y_in.doExport (*Y_rowMap, *exporter, ADD_ASSIGN);
4834  }
4835  }
4836  else { // Don't do an Export: row Map and range Map are the same.
4837  //
4838  // If Y_in does not have constant stride, or if the column Map
4839  // MV aliases Y_in, then we can't let the kernel write directly
4840  // to Y_in. Instead, we have to use the cached row (== range)
4841  // Map MV as temporary storage.
4842  //
4843  // FIXME (mfh 05 Jun 2014) This test for aliasing only tests if
4844  // the user passed in the same MultiVector for both X and Y. It
4845  // won't detect whether one MultiVector views the other. We
4846  // should also check the MultiVectors' raw data pointers.
4847  if (! Y_in.isConstantStride () || X_colMap.getRawPtr () == &Y_in) {
4848  // Force creating the MV if it hasn't been created already.
4849  // This will reuse a previously created cached MV.
4850  Y_rowMap = getRowMapMultiVector (Y_in, true);
4851 
4852  // If beta == 0, we don't need to copy Y_in into Y_rowMap,
4853  // since we're overwriting it anyway.
4854  if (beta != ZERO) {
4855  Tpetra::deep_copy (*Y_rowMap, Y_in);
4856  }
4857  this->localApply (*X_colMap, *Y_rowMap, Teuchos::NO_TRANS, alpha, beta);
4858  Tpetra::deep_copy (Y_in, *Y_rowMap);
4859  }
4860  else {
4861  this->localApply (*X_colMap, Y_in, Teuchos::NO_TRANS, alpha, beta);
4862  }
4863  }
4864 
4865  // If the range Map is a locally replicated Map, sum up
4866  // contributions from each process. We set beta = 0 on all
4867  // processes but Proc 0 initially, so this will handle the scaling
4868  // factor beta correctly.
4869  if (Y_is_replicated) {
4870  ProfilingRegion regionReduce ("Tpetra::CrsMatrix::apply: Reduce Y");
4871  Y_in.reduce ();
4872  }
4873  }
4874 
4875  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
4876  void
4880  const Teuchos::ETransp mode,
4881  Scalar alpha,
4882  Scalar beta) const
4883  {
4885  using Teuchos::null;
4886  using Teuchos::RCP;
4887  using Teuchos::rcp;
4888  using Teuchos::rcp_const_cast;
4889  using Teuchos::rcpFromRef;
4890  const Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero ();
4891 
4892  // Take shortcuts for alpha == 0.
4893  if (alpha == ZERO) {
4894  // Follow the Sparse BLAS convention by ignoring both the matrix
4895  // and X_in, in this case.
4896  if (beta == ZERO) {
4897  // Follow the Sparse BLAS convention by overwriting any Inf or
4898  // NaN values in Y_in, in this case.
4899  Y_in.putScalar (ZERO);
4900  }
4901  else {
4902  Y_in.scale (beta);
4903  }
4904  return;
4905  }
4906  else if (beta == ZERO) {
4907  //Thyra was implicitly assuming that Y gets set to zero / or is overwritten
4908  //when bets==0. This was not the case with transpose in a multithreaded
4909  //environment where a multiplication with subsequent atomic_adds is used
4910  //since 0 is effectively not special cased. Doing the explicit set to zero here
4911  //This catches cases where Y is nan or inf.
4912  Y_in.putScalar (ZERO);
4913  }
4914 
4915  const size_t numVectors = X_in.getNumVectors ();
4916 
4917  // We don't allow X_in and Y_in to alias one another. It's hard
4918  // to check this, because advanced users could create views from
4919  // raw pointers. However, if X_in and Y_in reference the same
4920  // object, we will do the user a favor by copying X into new
4921  // storage (with a warning). We only need to do this if we have
4922  // trivial importers; otherwise, we don't actually apply the
4923  // operator from X into Y.
4924  RCP<const import_type> importer = this->getGraph ()->getImporter ();
4925  RCP<const export_type> exporter = this->getGraph ()->getExporter ();
4926  // access X indirectly, in case we need to create temporary storage
4927  RCP<const MV> X;
4928 
4929  // some parameters for below
4930  const bool Y_is_replicated = (! Y_in.isDistributed () && this->getComm ()->getSize () != 1);
4931  const bool Y_is_overwritten = (beta == ZERO);
4932  if (Y_is_replicated && this->getComm ()->getRank () > 0) {
4933  beta = ZERO;
4934  }
4935 
4936  // The kernels do not allow input or output with nonconstant stride.
4937  if (! X_in.isConstantStride () && importer.is_null ()) {
4938  X = rcp (new MV (X_in, Teuchos::Copy)); // Constant-stride copy of X_in
4939  } else {
4940  X = rcpFromRef (X_in); // Reference to X_in
4941  }
4942 
4943  // Set up temporary multivectors for Import and/or Export.
4944  if (importer != Teuchos::null) {
4945  if (importMV_ != Teuchos::null && importMV_->getNumVectors() != numVectors) {
4946  importMV_ = null;
4947  }
4948  if (importMV_ == null) {
4949  importMV_ = rcp (new MV (this->getColMap (), numVectors));
4950  }
4951  }
4952  if (exporter != Teuchos::null) {
4953  if (exportMV_ != Teuchos::null && exportMV_->getNumVectors() != numVectors) {
4954  exportMV_ = null;
4955  }
4956  if (exportMV_ == null) {
4957  exportMV_ = rcp (new MV (this->getRowMap (), numVectors));
4958  }
4959  }
4960 
4961  // If we have a non-trivial exporter, we must import elements that
4962  // are permuted or are on other processors.
4963  if (! exporter.is_null ()) {
4964  ProfilingRegion regionImport ("Tpetra::CrsMatrix::apply (transpose): Import");
4965  exportMV_->doImport (X_in, *exporter, INSERT);
4966  X = exportMV_; // multiply out of exportMV_
4967  }
4968 
4969  // If we have a non-trivial importer, we must export elements that
4970  // are permuted or belong to other processors. We will compute
4971  // solution into the to-be-exported MV; get a view.
4972  if (importer != Teuchos::null) {
4973  ProfilingRegion regionExport ("Tpetra::CrsMatrix::apply (transpose): Export");
4974 
4975  // FIXME (mfh 18 Apr 2015) Temporary fix suggested by Clark
4976  // Dohrmann on Fri 17 Apr 2015. At some point, we need to go
4977  // back and figure out why this helps. importMV_ SHOULD be
4978  // completely overwritten in the localApply(...) call
4979  // below, because beta == ZERO there.
4980  importMV_->putScalar (ZERO);
4981  // Do the local computation.
4982  this->localApply (*X, *importMV_, mode, alpha, ZERO);
4983 
4984  if (Y_is_overwritten) {
4985  Y_in.putScalar (ZERO);
4986  } else {
4987  Y_in.scale (beta);
4988  }
4989  Y_in.doExport (*importMV_, *importer, ADD_ASSIGN);
4990  }
4991  // otherwise, multiply into Y
4992  else {
4993  // can't multiply in-situ; can't multiply into non-strided multivector
4994  //
4995  // FIXME (mfh 05 Jun 2014) This test for aliasing only tests if
4996  // the user passed in the same MultiVector for both X and Y. It
4997  // won't detect whether one MultiVector views the other. We
4998  // should also check the MultiVectors' raw data pointers.
4999  if (! Y_in.isConstantStride () || X.getRawPtr () == &Y_in) {
5000  // Make a deep copy of Y_in, into which to write the multiply result.
5001  MV Y (Y_in, Teuchos::Copy);
5002  this->localApply (*X, Y, mode, alpha, beta);
5003  Tpetra::deep_copy (Y_in, Y);
5004  } else {
5005  this->localApply (*X, Y_in, mode, alpha, beta);
5006  }
5007  }
5008 
5009  // If the range Map is a locally replicated map, sum the
5010  // contributions from each process. (That's why we set beta=0
5011  // above for all processes but Proc 0.)
5012  if (Y_is_replicated) {
5013  ProfilingRegion regionReduce ("Tpetra::CrsMatrix::apply (transpose): Reduce Y");
5014  Y_in.reduce ();
5015  }
5016  }
5017 
5018  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5019  void
5023  const Teuchos::ETransp mode,
5024  const Scalar& alpha,
5025  const Scalar& beta) const
5026  {
5028  using Teuchos::NO_TRANS;
5029  ProfilingRegion regionLocalApply ("Tpetra::CrsMatrix::localApply");
5030 
5031  auto X_lcl = X.getLocalViewDevice(Access::ReadOnly);
5032  auto Y_lcl = Y.getLocalViewDevice(Access::ReadWrite);
5033 
5034  const bool debug = ::Tpetra::Details::Behavior::debug ();
5035  if (debug) {
5036  const char tfecfFuncName[] = "localApply: ";
5037  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5038  (X.getNumVectors () != Y.getNumVectors (), std::runtime_error,
5039  "X.getNumVectors() = " << X.getNumVectors () << " != "
5040  "Y.getNumVectors() = " << Y.getNumVectors () << ".");
5041  const bool transpose = (mode != Teuchos::NO_TRANS);
5042  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5043  (! transpose && X.getLocalLength () !=
5044  getColMap ()->getLocalNumElements (), std::runtime_error,
5045  "NO_TRANS case: X has the wrong number of local rows. "
5046  "X.getLocalLength() = " << X.getLocalLength () << " != "
5047  "getColMap()->getLocalNumElements() = " <<
5048  getColMap ()->getLocalNumElements () << ".");
5049  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5050  (! transpose && Y.getLocalLength () !=
5051  getRowMap ()->getLocalNumElements (), std::runtime_error,
5052  "NO_TRANS case: Y has the wrong number of local rows. "
5053  "Y.getLocalLength() = " << Y.getLocalLength () << " != "
5054  "getRowMap()->getLocalNumElements() = " <<
5055  getRowMap ()->getLocalNumElements () << ".");
5056  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5057  (transpose && X.getLocalLength () !=
5058  getRowMap ()->getLocalNumElements (), std::runtime_error,
5059  "TRANS or CONJ_TRANS case: X has the wrong number of local "
5060  "rows. X.getLocalLength() = " << X.getLocalLength ()
5061  << " != getRowMap()->getLocalNumElements() = "
5062  << getRowMap ()->getLocalNumElements () << ".");
5063  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5064  (transpose && Y.getLocalLength () !=
5065  getColMap ()->getLocalNumElements (), std::runtime_error,
5066  "TRANS or CONJ_TRANS case: X has the wrong number of local "
5067  "rows. Y.getLocalLength() = " << Y.getLocalLength ()
5068  << " != getColMap()->getLocalNumElements() = "
5069  << getColMap ()->getLocalNumElements () << ".");
5070  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5071  (! isFillComplete (), std::runtime_error, "The matrix is not "
5072  "fill complete. You must call fillComplete() (possibly with "
5073  "domain and range Map arguments) without an intervening "
5074  "resumeFill() call before you may call this method.");
5075  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5076  (! X.isConstantStride () || ! Y.isConstantStride (),
5077  std::runtime_error, "X and Y must be constant stride.");
5078  // If the two pointers are null, then they don't alias one
5079  // another, even though they are equal.
5080  // Kokkos does not guarantee that zero row-extent vectors
5081  // point to different places, so we have to check that too.
5082  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5083  (X_lcl.data () == Y_lcl.data () && X_lcl.data () != nullptr
5084  && X_lcl.extent(0) != 0,
5085  std::runtime_error, "X and Y may not alias one another.");
5086  }
5087 
5088 #if KOKKOSKERNELS_VERSION >= 40299
5089  auto A_lcl = getLocalMatrixDevice();
5090  if(!applyHelper.get()) {
5091  // The apply helper does not exist, so create it.
5092  applyHelper = std::make_shared<ApplyHelper>(A_lcl.nnz(), A_lcl.graph.row_map);
5093  }
5094 
5095  // Translate mode (Teuchos enum) to KokkosKernels (1-character string)
5096  const char* modeKK = nullptr;
5097  switch(mode)
5098  {
5099  case Teuchos::NO_TRANS:
5100  modeKK = KokkosSparse::NoTranspose; break;
5101  case Teuchos::TRANS:
5102  modeKK = KokkosSparse::Transpose; break;
5103  case Teuchos::CONJ_TRANS:
5104  modeKK = KokkosSparse::ConjugateTranspose; break;
5105  default:
5106  throw std::invalid_argument("Tpetra::CrsMatrix::localApply: invalid mode");
5107  }
5108 
5109  if(applyHelper->shouldUseIntRowptrs())
5110  {
5111  auto A_lcl_int_rowptrs = applyHelper->getIntRowptrMatrix(A_lcl);
5112  KokkosSparse::spmv(
5113  &applyHelper->handle_int, modeKK,
5114  impl_scalar_type(alpha), A_lcl_int_rowptrs, X_lcl, impl_scalar_type(beta), Y_lcl);
5115  }
5116  else
5117  {
5118  KokkosSparse::spmv(
5119  &applyHelper->handle, modeKK,
5120  impl_scalar_type(alpha), A_lcl, X_lcl, impl_scalar_type(beta), Y_lcl);
5121  }
5122 #else
5123  LocalOrdinal nrows = getLocalNumRows();
5124  LocalOrdinal maxRowImbalance = 0;
5125  if(nrows != 0)
5126  maxRowImbalance = getLocalMaxNumRowEntries() - (getLocalNumEntries() / nrows);
5127 
5128  auto matrix_lcl = getLocalMultiplyOperator();
5129  if(size_t(maxRowImbalance) >= Tpetra::Details::Behavior::rowImbalanceThreshold())
5130  matrix_lcl->applyImbalancedRows (X_lcl, Y_lcl, mode, alpha, beta);
5131  else
5132  matrix_lcl->apply (X_lcl, Y_lcl, mode, alpha, beta);
5133 #endif
5134  }
5135 
5136  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5137  void
5141  Teuchos::ETransp mode,
5142  Scalar alpha,
5143  Scalar beta) const
5144  {
5146  const char fnName[] = "Tpetra::CrsMatrix::apply";
5147 
5148  TEUCHOS_TEST_FOR_EXCEPTION
5149  (! isFillComplete (), std::runtime_error,
5150  fnName << ": Cannot call apply() until fillComplete() "
5151  "has been called.");
5152 
5153  if (mode == Teuchos::NO_TRANS) {
5154  ProfilingRegion regionNonTranspose (fnName);
5155  this->applyNonTranspose (X, Y, alpha, beta);
5156  }
5157  else {
5158  ProfilingRegion regionTranspose ("Tpetra::CrsMatrix::apply (transpose)");
5159  this->applyTranspose (X, Y, mode, alpha, beta);
5160  }
5161  }
5162 
5163 
5164  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5165  template<class T>
5166  Teuchos::RCP<CrsMatrix<T, LocalOrdinal, GlobalOrdinal, Node> >
5168  convert () const
5169  {
5170  using Teuchos::RCP;
5171  typedef CrsMatrix<T, LocalOrdinal, GlobalOrdinal, Node> output_matrix_type;
5172  const char tfecfFuncName[] = "convert: ";
5173 
5174  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5175  (! this->isFillComplete (), std::runtime_error, "This matrix (the source "
5176  "of the conversion) is not fill complete. You must first call "
5177  "fillComplete() (possibly with the domain and range Map) without an "
5178  "intervening call to resumeFill(), before you may call this method.");
5179 
5180  RCP<output_matrix_type> newMatrix
5181  (new output_matrix_type (this->getCrsGraph ()));
5182  // Copy old values into new values. impl_scalar_type and T may
5183  // differ, so we can't use Kokkos::deep_copy.
5185  copyConvert (newMatrix->getLocalMatrixDevice ().values,
5186  this->getLocalMatrixDevice ().values);
5187  // Since newmat has a static (const) graph, the graph already has
5188  // a column Map, and Import and Export objects already exist (if
5189  // applicable). Thus, calling fillComplete is cheap.
5190  newMatrix->fillComplete (this->getDomainMap (), this->getRangeMap ());
5191 
5192  return newMatrix;
5193  }
5194 
5195 
5196  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5197  void
5200  {
5201  const bool debug = ::Tpetra::Details::Behavior::debug ("CrsGraph");
5202  if (debug) {
5203  const char tfecfFuncName[] = "checkInternalState: ";
5204  const char err[] = "Internal state is not consistent. "
5205  "Please report this bug to the Tpetra developers.";
5206 
5207  // This version of the graph (RCP<const crs_graph_type>) must
5208  // always be nonnull.
5209  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5210  (staticGraph_.is_null (), std::logic_error, err);
5211  // myGraph == null means that the matrix has a const ("static")
5212  // graph. Otherwise, the matrix has a dynamic graph (it owns its
5213  // graph).
5214  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5215  (! myGraph_.is_null () && myGraph_ != staticGraph_,
5216  std::logic_error, err);
5217  // if matrix is fill complete, then graph must be fill complete
5218  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5219  (isFillComplete () && ! staticGraph_->isFillComplete (),
5220  std::logic_error, err << " Specifically, the matrix is fill complete, "
5221  "but its graph is NOT fill complete.");
5222  // if values are allocated and they are non-zero in number, then
5223  // one of the allocations should be present
5224  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5225  (staticGraph_->indicesAreAllocated () &&
5226  staticGraph_->getLocalAllocationSize() > 0 &&
5227  staticGraph_->getLocalNumRows() > 0 &&
5228  valuesUnpacked_wdv.extent (0) == 0,
5229  std::logic_error, err);
5230  }
5231  }
5232 
5233  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5234  std::string
5237  {
5238  std::ostringstream os;
5239 
5240  os << "Tpetra::CrsMatrix (Kokkos refactor): {";
5241  if (this->getObjectLabel () != "") {
5242  os << "Label: \"" << this->getObjectLabel () << "\", ";
5243  }
5244  if (isFillComplete ()) {
5245  os << "isFillComplete: true"
5246  << ", global dimensions: [" << getGlobalNumRows () << ", "
5247  << getGlobalNumCols () << "]"
5248  << ", global number of entries: " << getGlobalNumEntries ()
5249  << "}";
5250  }
5251  else {
5252  os << "isFillComplete: false"
5253  << ", global dimensions: [" << getGlobalNumRows () << ", "
5254  << getGlobalNumCols () << "]}";
5255  }
5256  return os.str ();
5257  }
5258 
5259  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5260  void
5262  describe (Teuchos::FancyOStream &out,
5263  const Teuchos::EVerbosityLevel verbLevel) const
5264  {
5265  using std::endl;
5266  using std::setw;
5267  using Teuchos::ArrayView;
5268  using Teuchos::Comm;
5269  using Teuchos::RCP;
5270  using Teuchos::TypeNameTraits;
5271  using Teuchos::VERB_DEFAULT;
5272  using Teuchos::VERB_NONE;
5273  using Teuchos::VERB_LOW;
5274  using Teuchos::VERB_MEDIUM;
5275  using Teuchos::VERB_HIGH;
5276  using Teuchos::VERB_EXTREME;
5277 
5278  const Teuchos::EVerbosityLevel vl = (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
5279 
5280  if (vl == VERB_NONE) {
5281  return; // Don't print anything at all
5282  }
5283 
5284  // By convention, describe() always begins with a tab.
5285  Teuchos::OSTab tab0 (out);
5286 
5287  RCP<const Comm<int> > comm = this->getComm();
5288  const int myRank = comm->getRank();
5289  const int numProcs = comm->getSize();
5290  size_t width = 1;
5291  for (size_t dec=10; dec<getGlobalNumRows(); dec *= 10) {
5292  ++width;
5293  }
5294  width = std::max<size_t> (width, static_cast<size_t> (11)) + 2;
5295 
5296  // none: print nothing
5297  // low: print O(1) info from node 0
5298  // medium: print O(P) info, num entries per process
5299  // high: print O(N) info, num entries per row
5300  // extreme: print O(NNZ) info: print indices and values
5301  //
5302  // for medium and higher, print constituent objects at specified verbLevel
5303  if (myRank == 0) {
5304  out << "Tpetra::CrsMatrix (Kokkos refactor):" << endl;
5305  }
5306  Teuchos::OSTab tab1 (out);
5307 
5308  if (myRank == 0) {
5309  if (this->getObjectLabel () != "") {
5310  out << "Label: \"" << this->getObjectLabel () << "\", ";
5311  }
5312  {
5313  out << "Template parameters:" << endl;
5314  Teuchos::OSTab tab2 (out);
5315  out << "Scalar: " << TypeNameTraits<Scalar>::name () << endl
5316  << "LocalOrdinal: " << TypeNameTraits<LocalOrdinal>::name () << endl
5317  << "GlobalOrdinal: " << TypeNameTraits<GlobalOrdinal>::name () << endl
5318  << "Node: " << TypeNameTraits<Node>::name () << endl;
5319  }
5320  if (isFillComplete()) {
5321  out << "isFillComplete: true" << endl
5322  << "Global dimensions: [" << getGlobalNumRows () << ", "
5323  << getGlobalNumCols () << "]" << endl
5324  << "Global number of entries: " << getGlobalNumEntries () << endl
5325  << endl << "Global max number of entries in a row: "
5326  << getGlobalMaxNumRowEntries () << endl;
5327  }
5328  else {
5329  out << "isFillComplete: false" << endl
5330  << "Global dimensions: [" << getGlobalNumRows () << ", "
5331  << getGlobalNumCols () << "]" << endl;
5332  }
5333  }
5334 
5335  if (vl < VERB_MEDIUM) {
5336  return; // all done!
5337  }
5338 
5339  // Describe the row Map.
5340  if (myRank == 0) {
5341  out << endl << "Row Map:" << endl;
5342  }
5343  if (getRowMap ().is_null ()) {
5344  if (myRank == 0) {
5345  out << "null" << endl;
5346  }
5347  }
5348  else {
5349  if (myRank == 0) {
5350  out << endl;
5351  }
5352  getRowMap ()->describe (out, vl);
5353  }
5354 
5355  // Describe the column Map.
5356  if (myRank == 0) {
5357  out << "Column Map: ";
5358  }
5359  if (getColMap ().is_null ()) {
5360  if (myRank == 0) {
5361  out << "null" << endl;
5362  }
5363  } else if (getColMap () == getRowMap ()) {
5364  if (myRank == 0) {
5365  out << "same as row Map" << endl;
5366  }
5367  } else {
5368  if (myRank == 0) {
5369  out << endl;
5370  }
5371  getColMap ()->describe (out, vl);
5372  }
5373 
5374  // Describe the domain Map.
5375  if (myRank == 0) {
5376  out << "Domain Map: ";
5377  }
5378  if (getDomainMap ().is_null ()) {
5379  if (myRank == 0) {
5380  out << "null" << endl;
5381  }
5382  } else if (getDomainMap () == getRowMap ()) {
5383  if (myRank == 0) {
5384  out << "same as row Map" << endl;
5385  }
5386  } else if (getDomainMap () == getColMap ()) {
5387  if (myRank == 0) {
5388  out << "same as column Map" << endl;
5389  }
5390  } else {
5391  if (myRank == 0) {
5392  out << endl;
5393  }
5394  getDomainMap ()->describe (out, vl);
5395  }
5396 
5397  // Describe the range Map.
5398  if (myRank == 0) {
5399  out << "Range Map: ";
5400  }
5401  if (getRangeMap ().is_null ()) {
5402  if (myRank == 0) {
5403  out << "null" << endl;
5404  }
5405  } else if (getRangeMap () == getDomainMap ()) {
5406  if (myRank == 0) {
5407  out << "same as domain Map" << endl;
5408  }
5409  } else if (getRangeMap () == getRowMap ()) {
5410  if (myRank == 0) {
5411  out << "same as row Map" << endl;
5412  }
5413  } else {
5414  if (myRank == 0) {
5415  out << endl;
5416  }
5417  getRangeMap ()->describe (out, vl);
5418  }
5419 
5420  // O(P) data
5421  for (int curRank = 0; curRank < numProcs; ++curRank) {
5422  if (myRank == curRank) {
5423  out << "Process rank: " << curRank << endl;
5424  Teuchos::OSTab tab2 (out);
5425  if (! staticGraph_->indicesAreAllocated ()) {
5426  out << "Graph indices not allocated" << endl;
5427  }
5428  else {
5429  out << "Number of allocated entries: "
5430  << staticGraph_->getLocalAllocationSize () << endl;
5431  }
5432  out << "Number of entries: " << getLocalNumEntries () << endl
5433  << "Max number of entries per row: " << getLocalMaxNumRowEntries ()
5434  << endl;
5435  }
5436  // Give output time to complete by executing some barriers.
5437  comm->barrier ();
5438  comm->barrier ();
5439  comm->barrier ();
5440  }
5441 
5442  if (vl < VERB_HIGH) {
5443  return; // all done!
5444  }
5445 
5446  // O(N) and O(NNZ) data
5447  for (int curRank = 0; curRank < numProcs; ++curRank) {
5448  if (myRank == curRank) {
5449  out << std::setw(width) << "Proc Rank"
5450  << std::setw(width) << "Global Row"
5451  << std::setw(width) << "Num Entries";
5452  if (vl == VERB_EXTREME) {
5453  out << std::setw(width) << "(Index,Value)";
5454  }
5455  out << endl;
5456  for (size_t r = 0; r < getLocalNumRows (); ++r) {
5457  const size_t nE = getNumEntriesInLocalRow(r);
5458  GlobalOrdinal gid = getRowMap()->getGlobalElement(r);
5459  out << std::setw(width) << myRank
5460  << std::setw(width) << gid
5461  << std::setw(width) << nE;
5462  if (vl == VERB_EXTREME) {
5463  if (isGloballyIndexed()) {
5464  global_inds_host_view_type rowinds;
5465  values_host_view_type rowvals;
5466  getGlobalRowView (gid, rowinds, rowvals);
5467  for (size_t j = 0; j < nE; ++j) {
5468  out << " (" << rowinds[j]
5469  << ", " << rowvals[j]
5470  << ") ";
5471  }
5472  }
5473  else if (isLocallyIndexed()) {
5474  local_inds_host_view_type rowinds;
5475  values_host_view_type rowvals;
5476  getLocalRowView (r, rowinds, rowvals);
5477  for (size_t j=0; j < nE; ++j) {
5478  out << " (" << getColMap()->getGlobalElement(rowinds[j])
5479  << ", " << rowvals[j]
5480  << ") ";
5481  }
5482  } // globally or locally indexed
5483  } // vl == VERB_EXTREME
5484  out << endl;
5485  } // for each row r on this process
5486  } // if (myRank == curRank)
5487 
5488  // Give output time to complete
5489  comm->barrier ();
5490  comm->barrier ();
5491  comm->barrier ();
5492  } // for each process p
5493  }
5494 
5495  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5496  bool
5499  {
5500  // It's not clear what kind of compatibility checks on sizes can
5501  // be performed here. Epetra_CrsGraph doesn't check any sizes for
5502  // compatibility.
5503 
5504  // Currently, the source object must be a RowMatrix with the same
5505  // four template parameters as the target CrsMatrix. We might
5506  // relax this requirement later.
5507  const row_matrix_type* srcRowMat =
5508  dynamic_cast<const row_matrix_type*> (&source);
5509  return (srcRowMat != nullptr);
5510  }
5511 
5512  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5513  void
5516  const typename crs_graph_type::padding_type& padding,
5517  const bool verbose)
5518  {
5520  using Details::padCrsArrays;
5521  using std::endl;
5522  using LO = local_ordinal_type;
5523  using row_ptrs_type =
5524  typename local_graph_device_type::row_map_type::non_const_type;
5525  using range_policy =
5526  Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
5527  const char tfecfFuncName[] = "applyCrsPadding";
5528  const char suffix[] =
5529  ". Please report this bug to the Tpetra developers.";
5530  ProfilingRegion regionCAP("Tpetra::CrsMatrix::applyCrsPadding");
5531 
5532  std::unique_ptr<std::string> prefix;
5533  if (verbose) {
5534  prefix = this->createPrefix("CrsMatrix", tfecfFuncName);
5535  std::ostringstream os;
5536  os << *prefix << "padding: ";
5537  padding.print(os);
5538  os << endl;
5539  std::cerr << os.str();
5540  }
5541  const int myRank = ! verbose ? -1 : [&] () {
5542  auto map = this->getMap();
5543  if (map.is_null()) {
5544  return -1;
5545  }
5546  auto comm = map->getComm();
5547  if (comm.is_null()) {
5548  return -1;
5549  }
5550  return comm->getRank();
5551  } ();
5552 
5553  // NOTE (mfh 29 Jan 2020) This allocates the values array.
5554  if (! myGraph_->indicesAreAllocated()) {
5555  if (verbose) {
5556  std::ostringstream os;
5557  os << *prefix << "Call allocateIndices" << endl;
5558  std::cerr << os.str();
5559  }
5560  allocateValues(GlobalIndices, GraphNotYetAllocated, verbose);
5561  }
5562 
5563  // FIXME (mfh 10 Feb 2020) We shouldn't actually reallocate
5564  // row_ptrs_beg or allocate row_ptrs_end unless the allocation
5565  // size needs to increase. That should be the job of
5566  // padCrsArrays.
5567 
5568  // Making copies here because rowPtrsUnpacked_ has a const type. Otherwise, we
5569  // would use it directly.
5570 
5571  if (verbose) {
5572  std::ostringstream os;
5573  os << *prefix << "Allocate row_ptrs_beg: "
5574  << myGraph_->getRowPtrsUnpackedHost().extent(0) << endl;
5575  std::cerr << os.str();
5576  }
5577  using Kokkos::view_alloc;
5578  using Kokkos::WithoutInitializing;
5579  row_ptrs_type row_ptr_beg(view_alloc("row_ptr_beg", WithoutInitializing),
5580  myGraph_->rowPtrsUnpacked_dev_.extent(0));
5581  // DEEP_COPY REVIEW - DEVICE-TO-DEVICE
5582  Kokkos::deep_copy(execution_space(),row_ptr_beg, myGraph_->rowPtrsUnpacked_dev_);
5583 
5584  const size_t N = row_ptr_beg.extent(0) == 0 ? size_t(0) :
5585  size_t(row_ptr_beg.extent(0) - 1);
5586  if (verbose) {
5587  std::ostringstream os;
5588  os << *prefix << "Allocate row_ptrs_end: " << N << endl;
5589  std::cerr << os.str();
5590  }
5591  row_ptrs_type row_ptr_end(
5592  view_alloc("row_ptr_end", WithoutInitializing), N);
5593 
5594  row_ptrs_type num_row_entries_d;
5595 
5596  const bool refill_num_row_entries =
5597  myGraph_->k_numRowEntries_.extent(0) != 0;
5598 
5599  if (refill_num_row_entries) { // unpacked storage
5600  // We can't assume correct *this capture until C++17, and it's
5601  // likely more efficient just to capture what we need anyway.
5602  num_row_entries_d = create_mirror_view_and_copy(memory_space(),
5603  myGraph_->k_numRowEntries_);
5604  Kokkos::parallel_for
5605  ("Fill end row pointers", range_policy(0, N),
5606  KOKKOS_LAMBDA (const size_t i) {
5607  row_ptr_end(i) = row_ptr_beg(i) + num_row_entries_d(i);
5608  });
5609  }
5610  else {
5611  // FIXME (mfh 04 Feb 2020) Fix padCrsArrays so that if packed
5612  // storage, we don't need row_ptr_end to be separate allocation;
5613  // could just have it alias row_ptr_beg+1.
5614  Kokkos::parallel_for
5615  ("Fill end row pointers", range_policy(0, N),
5616  KOKKOS_LAMBDA (const size_t i) {
5617  row_ptr_end(i) = row_ptr_beg(i+1);
5618  });
5619  }
5620 
5621  if (myGraph_->isGloballyIndexed()) {
5622  padCrsArrays(row_ptr_beg, row_ptr_end,
5623  myGraph_->gblInds_wdv,
5624  valuesUnpacked_wdv, padding, myRank, verbose);
5625  const auto newValuesLen = valuesUnpacked_wdv.extent(0);
5626  const auto newColIndsLen = myGraph_->gblInds_wdv.extent(0);
5627  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5628  (newValuesLen != newColIndsLen, std::logic_error,
5629  ": After padding, valuesUnpacked_wdv.extent(0)=" << newValuesLen
5630  << " != myGraph_->gblInds_wdv.extent(0)=" << newColIndsLen
5631  << suffix);
5632  }
5633  else {
5634  padCrsArrays(row_ptr_beg, row_ptr_end,
5635  myGraph_->lclIndsUnpacked_wdv,
5636  valuesUnpacked_wdv, padding, myRank, verbose);
5637  const auto newValuesLen = valuesUnpacked_wdv.extent(0);
5638  const auto newColIndsLen = myGraph_->lclIndsUnpacked_wdv.extent(0);
5639  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5640  (newValuesLen != newColIndsLen, std::logic_error,
5641  ": After padding, valuesUnpacked_wdv.extent(0)=" << newValuesLen
5642  << " != myGraph_->lclIndsUnpacked_wdv.extent(0)=" << newColIndsLen
5643  << suffix);
5644  }
5645 
5646  if (refill_num_row_entries) {
5647  Kokkos::parallel_for
5648  ("Fill num entries", range_policy(0, N),
5649  KOKKOS_LAMBDA (const size_t i) {
5650  num_row_entries_d(i) = row_ptr_end(i) - row_ptr_beg(i);
5651  });
5652  Kokkos::deep_copy(myGraph_->k_numRowEntries_, num_row_entries_d);
5653  }
5654 
5655  if (verbose) {
5656  std::ostringstream os;
5657  os << *prefix << "Assign myGraph_->rowPtrsUnpacked_; "
5658  << "old size: " << myGraph_->rowPtrsUnpacked_host_.extent(0)
5659  << ", new size: " << row_ptr_beg.extent(0) << endl;
5660  std::cerr << os.str();
5661  TEUCHOS_ASSERT( myGraph_->getRowPtrsUnpackedHost().extent(0) ==
5662  row_ptr_beg.extent(0) );
5663  }
5664  myGraph_->setRowPtrsUnpacked(row_ptr_beg);
5665  }
5666 
5667  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5668  void
5669  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
5670  copyAndPermuteStaticGraph(
5671  const RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& srcMat,
5672  const size_t numSameIDs,
5673  const LocalOrdinal permuteToLIDs[],
5674  const LocalOrdinal permuteFromLIDs[],
5675  const size_t numPermutes)
5676  {
5677  using Details::ProfilingRegion;
5678  using Teuchos::Array;
5679  using Teuchos::ArrayView;
5680  using std::endl;
5681  using LO = LocalOrdinal;
5682  using GO = GlobalOrdinal;
5683  const char tfecfFuncName[] = "copyAndPermuteStaticGraph";
5684  const char suffix[] =
5685  " Please report this bug to the Tpetra developers.";
5686  ProfilingRegion regionCAP
5687  ("Tpetra::CrsMatrix::copyAndPermuteStaticGraph");
5688 
5689  const bool debug = Details::Behavior::debug("CrsGraph");
5690  const bool verbose = Details::Behavior::verbose("CrsGraph");
5691  std::unique_ptr<std::string> prefix;
5692  if (verbose) {
5693  prefix = this->createPrefix("CrsGraph", tfecfFuncName);
5694  std::ostringstream os;
5695  os << *prefix << "Start" << endl;
5696  }
5697  const char* const prefix_raw =
5698  verbose ? prefix.get()->c_str() : nullptr;
5699 
5700  const bool sourceIsLocallyIndexed = srcMat.isLocallyIndexed ();
5701  //
5702  // Copy the first numSame row from source to target (this matrix).
5703  // This involves copying rows corresponding to LIDs [0, numSame-1].
5704  //
5705  const map_type& srcRowMap = * (srcMat.getRowMap ());
5706  nonconst_global_inds_host_view_type rowInds;
5707  nonconst_values_host_view_type rowVals;
5708  const LO numSameIDs_as_LID = static_cast<LO> (numSameIDs);
5709  for (LO sourceLID = 0; sourceLID < numSameIDs_as_LID; ++sourceLID) {
5710  // Global ID for the current row index in the source matrix.
5711  // The first numSameIDs GIDs in the two input lists are the
5712  // same, so sourceGID == targetGID in this case.
5713  const GO sourceGID = srcRowMap.getGlobalElement (sourceLID);
5714  const GO targetGID = sourceGID;
5715 
5716  ArrayView<const GO>rowIndsConstView;
5717  ArrayView<const Scalar> rowValsConstView;
5718 
5719  if (sourceIsLocallyIndexed) {
5720  const size_t rowLength = srcMat.getNumEntriesInGlobalRow (sourceGID);
5721  if (rowLength > static_cast<size_t> (rowInds.size())) {
5722  Kokkos::resize(rowInds,rowLength);
5723  Kokkos::resize(rowVals,rowLength);
5724  }
5725  // Resizing invalidates an Array's views, so we must make new
5726  // ones, even if rowLength hasn't changed.
5727  nonconst_global_inds_host_view_type rowIndsView = Kokkos::subview(rowInds,std::make_pair((size_t)0, rowLength));
5728  nonconst_values_host_view_type rowValsView = Kokkos::subview(rowVals,std::make_pair((size_t)0, rowLength));
5729 
5730  // The source matrix is locally indexed, so we have to get a
5731  // copy. Really it's the GIDs that have to be copied (because
5732  // they have to be converted from LIDs).
5733  size_t checkRowLength = 0;
5734  srcMat.getGlobalRowCopy (sourceGID, rowIndsView,
5735  rowValsView, checkRowLength);
5736  if (debug) {
5737  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5738  (rowLength != checkRowLength, std::logic_error, "For "
5739  "global row index " << sourceGID << ", the source "
5740  "matrix's getNumEntriesInGlobalRow returns a row length "
5741  "of " << rowLength << ", but getGlobalRowCopy reports "
5742  "a row length of " << checkRowLength << "." << suffix);
5743  }
5744 
5745  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
5746  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
5747  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
5748  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
5749  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
5750  rowIndsView.data(), rowIndsView.extent(0),
5751  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5752  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
5753  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
5754  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5755  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
5756  // KDDKDD UVM TEMPORARY: KokkosView interface
5757  }
5758  else { // source matrix is globally indexed.
5759  global_inds_host_view_type rowIndsView;
5760  values_host_view_type rowValsView;
5761  srcMat.getGlobalRowView(sourceGID, rowIndsView, rowValsView);
5762  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
5763  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
5764  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
5765  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
5766  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
5767  rowIndsView.data(), rowIndsView.extent(0),
5768  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5769  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
5770  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
5771  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5772  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
5773  // KDDKDD UVM TEMPORARY: KokkosView interface
5774 
5775  }
5776 
5777  // Applying a permutation to a matrix with a static graph
5778  // means REPLACE-ing entries.
5779  combineGlobalValues(targetGID, rowIndsConstView,
5780  rowValsConstView, REPLACE,
5781  prefix_raw, debug, verbose);
5782  }
5783 
5784  if (verbose) {
5785  std::ostringstream os;
5786  os << *prefix << "Do permutes" << endl;
5787  }
5788 
5789  const map_type& tgtRowMap = * (this->getRowMap ());
5790  for (size_t p = 0; p < numPermutes; ++p) {
5791  const GO sourceGID = srcRowMap.getGlobalElement (permuteFromLIDs[p]);
5792  const GO targetGID = tgtRowMap.getGlobalElement (permuteToLIDs[p]);
5793 
5794  ArrayView<const GO> rowIndsConstView;
5795  ArrayView<const Scalar> rowValsConstView;
5796 
5797  if (sourceIsLocallyIndexed) {
5798  const size_t rowLength = srcMat.getNumEntriesInGlobalRow (sourceGID);
5799  if (rowLength > static_cast<size_t> (rowInds.size ())) {
5800  Kokkos::resize(rowInds,rowLength);
5801  Kokkos::resize(rowVals,rowLength);
5802  }
5803  // Resizing invalidates an Array's views, so we must make new
5804  // ones, even if rowLength hasn't changed.
5805  nonconst_global_inds_host_view_type rowIndsView = Kokkos::subview(rowInds,std::make_pair((size_t)0, rowLength));
5806  nonconst_values_host_view_type rowValsView = Kokkos::subview(rowVals,std::make_pair((size_t)0, rowLength));
5807 
5808  // The source matrix is locally indexed, so we have to get a
5809  // copy. Really it's the GIDs that have to be copied (because
5810  // they have to be converted from LIDs).
5811  size_t checkRowLength = 0;
5812  srcMat.getGlobalRowCopy(sourceGID, rowIndsView,
5813  rowValsView, checkRowLength);
5814  if (debug) {
5815  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5816  (rowLength != checkRowLength, std::logic_error, "For "
5817  "source matrix global row index " << sourceGID << ", "
5818  "getNumEntriesInGlobalRow returns a row length of " <<
5819  rowLength << ", but getGlobalRowCopy a row length of "
5820  << checkRowLength << "." << suffix);
5821  }
5822 
5823  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
5824  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
5825  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
5826  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
5827  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
5828  rowIndsView.data(), rowIndsView.extent(0),
5829  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5830  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
5831  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
5832  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5833  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
5834  // KDDKDD UVM TEMPORARY: KokkosView interface
5835  }
5836  else {
5837  global_inds_host_view_type rowIndsView;
5838  values_host_view_type rowValsView;
5839  srcMat.getGlobalRowView(sourceGID, rowIndsView, rowValsView);
5840  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
5841  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
5842  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
5843  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
5844  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
5845  rowIndsView.data(), rowIndsView.extent(0),
5846  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5847  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
5848  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
5849  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5850  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
5851  // KDDKDD UVM TEMPORARY: KokkosView interface
5852  }
5853 
5854  combineGlobalValues(targetGID, rowIndsConstView,
5855  rowValsConstView, REPLACE,
5856  prefix_raw, debug, verbose);
5857  }
5858 
5859  if (verbose) {
5860  std::ostringstream os;
5861  os << *prefix << "Done" << endl;
5862  }
5863  }
5864 
5865  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
5866  void
5867  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
5868  copyAndPermuteNonStaticGraph(
5869  const RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& srcMat,
5870  const size_t numSameIDs,
5871  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& permuteToLIDs_dv,
5872  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& permuteFromLIDs_dv,
5873  const size_t numPermutes)
5874  {
5875  using Details::ProfilingRegion;
5876  using Teuchos::Array;
5877  using Teuchos::ArrayView;
5878  using std::endl;
5879  using LO = LocalOrdinal;
5880  using GO = GlobalOrdinal;
5881  const char tfecfFuncName[] = "copyAndPermuteNonStaticGraph";
5882  const char suffix[] =
5883  " Please report this bug to the Tpetra developers.";
5884  ProfilingRegion regionCAP
5885  ("Tpetra::CrsMatrix::copyAndPermuteNonStaticGraph");
5886 
5887  const bool debug = Details::Behavior::debug("CrsGraph");
5888  const bool verbose = Details::Behavior::verbose("CrsGraph");
5889  std::unique_ptr<std::string> prefix;
5890  if (verbose) {
5891  prefix = this->createPrefix("CrsGraph", tfecfFuncName);
5892  std::ostringstream os;
5893  os << *prefix << "Start" << endl;
5894  }
5895  const char* const prefix_raw =
5896  verbose ? prefix.get()->c_str() : nullptr;
5897 
5898  {
5899  using row_graph_type = RowGraph<LO, GO, Node>;
5900  const row_graph_type& srcGraph = *(srcMat.getGraph());
5901  auto padding =
5902  myGraph_->computeCrsPadding(srcGraph, numSameIDs,
5903  permuteToLIDs_dv, permuteFromLIDs_dv, verbose);
5904  applyCrsPadding(*padding, verbose);
5905  }
5906  const bool sourceIsLocallyIndexed = srcMat.isLocallyIndexed ();
5907  //
5908  // Copy the first numSame row from source to target (this matrix).
5909  // This involves copying rows corresponding to LIDs [0, numSame-1].
5910  //
5911  const map_type& srcRowMap = * (srcMat.getRowMap ());
5912  const LO numSameIDs_as_LID = static_cast<LO> (numSameIDs);
5913  using gids_type = nonconst_global_inds_host_view_type;
5914  using vals_type = nonconst_values_host_view_type;
5915  gids_type rowInds;
5916  vals_type rowVals;
5917  for (LO sourceLID = 0; sourceLID < numSameIDs_as_LID; ++sourceLID) {
5918  // Global ID for the current row index in the source matrix.
5919  // The first numSameIDs GIDs in the two input lists are the
5920  // same, so sourceGID == targetGID in this case.
5921  const GO sourceGID = srcRowMap.getGlobalElement (sourceLID);
5922  const GO targetGID = sourceGID;
5923 
5924  ArrayView<const GO> rowIndsConstView;
5925  ArrayView<const Scalar> rowValsConstView;
5926 
5927  if (sourceIsLocallyIndexed) {
5928 
5929  const size_t rowLength = srcMat.getNumEntriesInGlobalRow (sourceGID);
5930  if (rowLength > static_cast<size_t> (rowInds.extent(0))) {
5931  Kokkos::resize(rowInds,rowLength);
5932  Kokkos::resize(rowVals,rowLength);
5933  }
5934  // Resizing invalidates an Array's views, so we must make new
5935  // ones, even if rowLength hasn't changed.
5936  gids_type rowIndsView = Kokkos::subview(rowInds,std::make_pair((size_t)0, rowLength));
5937  vals_type rowValsView = Kokkos::subview(rowVals,std::make_pair((size_t)0, rowLength));
5938 
5939  // The source matrix is locally indexed, so we have to get a
5940  // copy. Really it's the GIDs that have to be copied (because
5941  // they have to be converted from LIDs).
5942  size_t checkRowLength = 0;
5943  srcMat.getGlobalRowCopy (sourceGID, rowIndsView, rowValsView,
5944  checkRowLength);
5945  if (debug) {
5946  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
5947  (rowLength != checkRowLength, std::logic_error, ": For "
5948  "global row index " << sourceGID << ", the source "
5949  "matrix's getNumEntriesInGlobalRow returns a row length "
5950  "of " << rowLength << ", but getGlobalRowCopy reports "
5951  "a row length of " << checkRowLength << "." << suffix);
5952  }
5953  rowIndsConstView = Teuchos::ArrayView<const GO>(rowIndsView.data(), rowLength);
5954  rowValsConstView = Teuchos::ArrayView<const Scalar>(reinterpret_cast<Scalar *>(rowValsView.data()), rowLength);
5955  }
5956  else { // source matrix is globally indexed.
5957  global_inds_host_view_type rowIndsView;
5958  values_host_view_type rowValsView;
5959  srcMat.getGlobalRowView(sourceGID, rowIndsView, rowValsView);
5960 
5961  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
5962  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
5963  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
5964  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
5965  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
5966  rowIndsView.data(), rowIndsView.extent(0),
5967  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5968  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
5969  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
5970  Teuchos::RCP_DISABLE_NODE_LOOKUP);
5971  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
5972  // KDDKDD UVM TEMPORARY: KokkosView interface
5973  }
5974 
5975  // Combine the data into the target matrix.
5976  insertGlobalValuesFilteredChecked(targetGID, rowIndsConstView,
5977  rowValsConstView, prefix_raw, debug, verbose);
5978  }
5979 
5980  if (verbose) {
5981  std::ostringstream os;
5982  os << *prefix << "Do permutes" << endl;
5983  }
5984  const LO* const permuteFromLIDs = permuteFromLIDs_dv.view_host().data();
5985  const LO* const permuteToLIDs = permuteToLIDs_dv.view_host().data();
5986 
5987  const map_type& tgtRowMap = * (this->getRowMap ());
5988  for (size_t p = 0; p < numPermutes; ++p) {
5989  const GO sourceGID = srcRowMap.getGlobalElement (permuteFromLIDs[p]);
5990  const GO targetGID = tgtRowMap.getGlobalElement (permuteToLIDs[p]);
5991 
5992  ArrayView<const GO> rowIndsConstView;
5993  ArrayView<const Scalar> rowValsConstView;
5994 
5995  if (sourceIsLocallyIndexed) {
5996  const size_t rowLength = srcMat.getNumEntriesInGlobalRow (sourceGID);
5997  if (rowLength > static_cast<size_t> (rowInds.extent(0))) {
5998  Kokkos::resize(rowInds,rowLength);
5999  Kokkos::resize(rowVals,rowLength);
6000  }
6001  // Resizing invalidates an Array's views, so we must make new
6002  // ones, even if rowLength hasn't changed.
6003  gids_type rowIndsView = Kokkos::subview(rowInds,std::make_pair((size_t)0, rowLength));
6004  vals_type rowValsView = Kokkos::subview(rowVals,std::make_pair((size_t)0, rowLength));
6005 
6006  // The source matrix is locally indexed, so we have to get a
6007  // copy. Really it's the GIDs that have to be copied (because
6008  // they have to be converted from LIDs).
6009  size_t checkRowLength = 0;
6010  srcMat.getGlobalRowCopy(sourceGID, rowIndsView,
6011  rowValsView, checkRowLength);
6012  if (debug) {
6013  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6014  (rowLength != checkRowLength, std::logic_error, "For "
6015  "source matrix global row index " << sourceGID << ", "
6016  "getNumEntriesInGlobalRow returns a row length of " <<
6017  rowLength << ", but getGlobalRowCopy a row length of "
6018  << checkRowLength << "." << suffix);
6019  }
6020  rowIndsConstView = Teuchos::ArrayView<const GO>(rowIndsView.data(), rowLength);
6021  rowValsConstView = Teuchos::ArrayView<const Scalar>(reinterpret_cast<Scalar *>(rowValsView.data()), rowLength);
6022  }
6023  else {
6024  global_inds_host_view_type rowIndsView;
6025  values_host_view_type rowValsView;
6026  srcMat.getGlobalRowView(sourceGID, rowIndsView, rowValsView);
6027 
6028  // KDDKDD UVM TEMPORARY: refactor combineGlobalValues to take
6029  // KDDKDD UVM TEMPORARY: Kokkos::View instead of ArrayView
6030  // KDDKDD UVM TEMPORARY: For now, wrap the view in ArrayViews
6031  // KDDKDD UVM TEMPORARY: Should be safe because we hold the KokkosViews
6032  rowIndsConstView = Teuchos::ArrayView<const GO> ( // BAD BAD BAD
6033  rowIndsView.data(), rowIndsView.extent(0),
6034  Teuchos::RCP_DISABLE_NODE_LOOKUP);
6035  rowValsConstView = Teuchos::ArrayView<const Scalar> ( // BAD BAD BAD
6036  reinterpret_cast<const Scalar*>(rowValsView.data()), rowValsView.extent(0),
6037  Teuchos::RCP_DISABLE_NODE_LOOKUP);
6038  // KDDKDD UVM TEMPORARY: Add replace, sum, transform methods with
6039  // KDDKDD UVM TEMPORARY: KokkosView interface
6040  }
6041 
6042  // Combine the data into the target matrix.
6043  insertGlobalValuesFilteredChecked(targetGID, rowIndsConstView,
6044  rowValsConstView, prefix_raw, debug, verbose);
6045  }
6046 
6047  if (verbose) {
6048  std::ostringstream os;
6049  os << *prefix << "Done" << endl;
6050  }
6051  }
6052 
6053  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6054  void
6057  const SrcDistObject& srcObj,
6058  const size_t numSameIDs,
6059  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& permuteToLIDs,
6060  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& permuteFromLIDs,
6061  const CombineMode /*CM*/)
6062  {
6063  using Details::Behavior;
6066  using std::endl;
6067 
6068  // Method name string for TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC.
6069  const char tfecfFuncName[] = "copyAndPermute: ";
6070  ProfilingRegion regionCAP("Tpetra::CrsMatrix::copyAndPermute");
6071 
6072  const bool verbose = Behavior::verbose("CrsMatrix");
6073  std::unique_ptr<std::string> prefix;
6074  if (verbose) {
6075  prefix = this->createPrefix("CrsMatrix", "copyAndPermute");
6076  std::ostringstream os;
6077  os << *prefix << endl
6078  << *prefix << " numSameIDs: " << numSameIDs << endl
6079  << *prefix << " numPermute: " << permuteToLIDs.extent(0)
6080  << endl
6081  << *prefix << " "
6082  << dualViewStatusToString (permuteToLIDs, "permuteToLIDs")
6083  << endl
6084  << *prefix << " "
6085  << dualViewStatusToString (permuteFromLIDs, "permuteFromLIDs")
6086  << endl
6087  << *prefix << " "
6088  << "isStaticGraph: " << (isStaticGraph() ? "true" : "false")
6089  << endl;
6090  std::cerr << os.str ();
6091  }
6092 
6093  const auto numPermute = permuteToLIDs.extent (0);
6094  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6095  (numPermute != permuteFromLIDs.extent (0),
6096  std::invalid_argument, "permuteToLIDs.extent(0) = "
6097  << numPermute << "!= permuteFromLIDs.extent(0) = "
6098  << permuteFromLIDs.extent (0) << ".");
6099 
6100  // This dynamic cast should succeed, because we've already tested
6101  // it in checkSizes().
6103  const RMT& srcMat = dynamic_cast<const RMT&> (srcObj);
6104  if (isStaticGraph ()) {
6105  TEUCHOS_ASSERT( ! permuteToLIDs.need_sync_host () );
6106  auto permuteToLIDs_h = permuteToLIDs.view_host ();
6107  TEUCHOS_ASSERT( ! permuteFromLIDs.need_sync_host () );
6108  auto permuteFromLIDs_h = permuteFromLIDs.view_host ();
6109 
6110  copyAndPermuteStaticGraph(srcMat, numSameIDs,
6111  permuteToLIDs_h.data(),
6112  permuteFromLIDs_h.data(),
6113  numPermute);
6114  }
6115  else {
6116  copyAndPermuteNonStaticGraph(srcMat, numSameIDs, permuteToLIDs,
6117  permuteFromLIDs, numPermute);
6118  }
6119 
6120  if (verbose) {
6121  std::ostringstream os;
6122  os << *prefix << "Done" << endl;
6123  std::cerr << os.str();
6124  }
6125  }
6126 
6127  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6128  void
6131  (const SrcDistObject& source,
6132  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& exportLIDs,
6133  Kokkos::DualView<char*, buffer_device_type>& exports,
6134  Kokkos::DualView<size_t*, buffer_device_type> numPacketsPerLID,
6135  size_t& constantNumPackets)
6136  {
6137  using Details::Behavior;
6140  using Teuchos::outArg;
6141  using Teuchos::REDUCE_MAX;
6142  using Teuchos::reduceAll;
6143  using std::endl;
6144  typedef LocalOrdinal LO;
6145  typedef GlobalOrdinal GO;
6146  const char tfecfFuncName[] = "packAndPrepare: ";
6147  ProfilingRegion regionPAP ("Tpetra::CrsMatrix::packAndPrepare");
6148 
6149  const bool debug = Behavior::debug("CrsMatrix");
6150  const bool verbose = Behavior::verbose("CrsMatrix");
6151 
6152  // Processes on which the communicator is null should not participate.
6153  Teuchos::RCP<const Teuchos::Comm<int> > pComm = this->getComm ();
6154  if (pComm.is_null ()) {
6155  return;
6156  }
6157  const Teuchos::Comm<int>& comm = *pComm;
6158  const int myRank = comm.getSize ();
6159 
6160  std::unique_ptr<std::string> prefix;
6161  if (verbose) {
6162  prefix = this->createPrefix("CrsMatrix", "packAndPrepare");
6163  std::ostringstream os;
6164  os << *prefix << "Start" << endl
6165  << *prefix << " "
6166  << dualViewStatusToString (exportLIDs, "exportLIDs")
6167  << endl
6168  << *prefix << " "
6169  << dualViewStatusToString (exports, "exports")
6170  << endl
6171  << *prefix << " "
6172  << dualViewStatusToString (numPacketsPerLID, "numPacketsPerLID")
6173  << endl;
6174  std::cerr << os.str ();
6175  }
6176 
6177  // Attempt to cast the source object to CrsMatrix. If successful,
6178  // use the source object's packNew() method to pack its data for
6179  // communication. Otherwise, attempt to cast to RowMatrix; if
6180  // successful, use the source object's pack() method. Otherwise,
6181  // the source object doesn't have the right type.
6182  //
6183  // FIXME (mfh 30 Jun 2013, 11 Sep 2017) We don't even need the
6184  // RowMatrix to have the same Node type. Unfortunately, we don't
6185  // have a way to ask if the RowMatrix is "a RowMatrix with any
6186  // Node type," since RowMatrix doesn't have a base class. A
6187  // hypothetical RowMatrixBase<Scalar, LO, GO> class, which does
6188  // not currently exist, would satisfy this requirement.
6189  //
6190  // Why RowMatrixBase<Scalar, LO, GO>? The source object's Scalar
6191  // type doesn't technically need to match the target object's
6192  // Scalar type, so we could just have RowMatrixBase<LO, GO>. LO
6193  // and GO need not be the same, as long as there is no overflow of
6194  // the indices. However, checking for index overflow is global
6195  // and therefore undesirable.
6196 
6197  std::ostringstream msg; // for collecting error messages
6198  int lclBad = 0; // to be set below
6199 
6200  using crs_matrix_type = CrsMatrix<Scalar, LO, GO, Node>;
6201  const crs_matrix_type* srcCrsMat =
6202  dynamic_cast<const crs_matrix_type*> (&source);
6203  if (srcCrsMat != nullptr) {
6204  if (verbose) {
6205  std::ostringstream os;
6206  os << *prefix << "Source matrix same (CrsMatrix) type as target; "
6207  "calling packNew" << endl;
6208  std::cerr << os.str ();
6209  }
6210  try {
6211  srcCrsMat->packNew (exportLIDs, exports, numPacketsPerLID,
6212  constantNumPackets);
6213  }
6214  catch (std::exception& e) {
6215  lclBad = 1;
6216  msg << "Proc " << myRank << ": " << e.what () << std::endl;
6217  }
6218  }
6219  else {
6220  using Kokkos::HostSpace;
6221  using Kokkos::subview;
6222  using exports_type = Kokkos::DualView<char*, buffer_device_type>;
6223  using range_type = Kokkos::pair<size_t, size_t>;
6224 
6225  if (verbose) {
6226  std::ostringstream os;
6227  os << *prefix << "Source matrix NOT same (CrsMatrix) type as target"
6228  << endl;
6229  std::cerr << os.str ();
6230  }
6231 
6232  const row_matrix_type* srcRowMat =
6233  dynamic_cast<const row_matrix_type*> (&source);
6234  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6235  (srcRowMat == nullptr, std::invalid_argument,
6236  "The source object of the Import or Export operation is neither a "
6237  "CrsMatrix (with the same template parameters as the target object), "
6238  "nor a RowMatrix (with the same first four template parameters as the "
6239  "target object).");
6240 
6241  // For the RowMatrix case, we need to convert from
6242  // Kokkos::DualView to Teuchos::Array*. This doesn't need to be
6243  // so terribly efficient, since packing a non-CrsMatrix
6244  // RowMatrix for Import/Export into a CrsMatrix is not a
6245  // critical case. Thus, we may allocate Teuchos::Array objects
6246  // here and copy to and from Kokkos::*View.
6247 
6248  // View exportLIDs's host data as a Teuchos::ArrayView.
6249  TEUCHOS_ASSERT( ! exportLIDs.need_sync_host () );
6250  auto exportLIDs_h = exportLIDs.view_host ();
6251  Teuchos::ArrayView<const LO> exportLIDs_av (exportLIDs_h.data (),
6252  exportLIDs_h.size ());
6253 
6254  // pack() will allocate exports_a as needed. We'll copy back
6255  // into exports (after (re)allocating exports if needed) below.
6256  Teuchos::Array<char> exports_a;
6257 
6258  // View exportLIDs' host data as a Teuchos::ArrayView. We don't
6259  // need to sync, since we're doing write-only access, but we do
6260  // need to mark the DualView as modified on host.
6261 
6262  numPacketsPerLID.clear_sync_state (); // write-only access
6263  numPacketsPerLID.modify_host ();
6264  auto numPacketsPerLID_h = numPacketsPerLID.view_host ();
6265  Teuchos::ArrayView<size_t> numPacketsPerLID_av (numPacketsPerLID_h.data (),
6266  numPacketsPerLID_h.size ());
6267 
6268  // Invoke RowMatrix's legacy pack() interface, using above
6269  // Teuchos::Array* objects.
6270  try {
6271  srcRowMat->pack (exportLIDs_av, exports_a, numPacketsPerLID_av,
6272  constantNumPackets);
6273  }
6274  catch (std::exception& e) {
6275  lclBad = 1;
6276  msg << "Proc " << myRank << ": " << e.what () << std::endl;
6277  }
6278 
6279  // Allocate 'exports', and copy exports_a back into it.
6280  const size_t newAllocSize = static_cast<size_t> (exports_a.size ());
6281  if (static_cast<size_t> (exports.extent (0)) < newAllocSize) {
6282  const std::string oldLabel = exports.d_view.label ();
6283  const std::string newLabel = (oldLabel == "") ? "exports" : oldLabel;
6284  exports = exports_type (newLabel, newAllocSize);
6285  }
6286  // It's safe to assume that we're working on host anyway, so
6287  // just keep exports sync'd to host.
6288  // ignore current device contents
6289  exports.modify_host();
6290 
6291  auto exports_h = exports.view_host ();
6292  auto exports_h_sub = subview (exports_h, range_type (0, newAllocSize));
6293 
6294  // Kokkos::deep_copy needs a Kokkos::View input, so turn
6295  // exports_a into a nonowning Kokkos::View first before copying.
6296  typedef typename exports_type::t_host::execution_space HES;
6297  typedef Kokkos::Device<HES, HostSpace> host_device_type;
6298  Kokkos::View<const char*, host_device_type>
6299  exports_a_kv (exports_a.getRawPtr (), newAllocSize);
6300  // DEEP_COPY REVIEW - NOT TESTED
6301  Kokkos::deep_copy (exports_h_sub, exports_a_kv);
6302  }
6303 
6304  if (debug) {
6305  int gblBad = 0; // output argument; to be set below
6306  reduceAll<int, int> (comm, REDUCE_MAX, lclBad, outArg (gblBad));
6307  if (gblBad != 0) {
6308  Tpetra::Details::gathervPrint (std::cerr, msg.str (), comm);
6309  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6310  (true, std::logic_error, "packNew() or pack() threw an exception on "
6311  "one or more participating processes.");
6312  }
6313  }
6314  else {
6315  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6316  (lclBad != 0, std::logic_error, "packNew threw an exception on one "
6317  "or more participating processes. Here is this process' error "
6318  "message: " << msg.str ());
6319  }
6320 
6321  if (verbose) {
6322  std::ostringstream os;
6323  os << *prefix << "packAndPrepare: Done!" << endl
6324  << *prefix << " "
6325  << dualViewStatusToString (exportLIDs, "exportLIDs")
6326  << endl
6327  << *prefix << " "
6328  << dualViewStatusToString (exports, "exports")
6329  << endl
6330  << *prefix << " "
6331  << dualViewStatusToString (numPacketsPerLID, "numPacketsPerLID")
6332  << endl;
6333  std::cerr << os.str ();
6334  }
6335  }
6336 
6337  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6338  size_t
6339  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
6340  packRow (char exports[],
6341  const size_t offset,
6342  const size_t numEnt,
6343  const GlobalOrdinal gidsIn[],
6344  const impl_scalar_type valsIn[],
6345  const size_t numBytesPerValue) const
6346  {
6347  using Kokkos::View;
6348  using Kokkos::subview;
6350  typedef LocalOrdinal LO;
6351  typedef GlobalOrdinal GO;
6352  typedef impl_scalar_type ST;
6353 
6354  if (numEnt == 0) {
6355  // Empty rows always take zero bytes, to ensure sparsity.
6356  return 0;
6357  }
6358 
6359  const GO gid = 0; // packValueCount wants this
6360  const LO numEntLO = static_cast<size_t> (numEnt);
6361 
6362  const size_t numEntBeg = offset;
6363  const size_t numEntLen = PackTraits<LO>::packValueCount (numEntLO);
6364  const size_t gidsBeg = numEntBeg + numEntLen;
6365  const size_t gidsLen = numEnt * PackTraits<GO>::packValueCount (gid);
6366  const size_t valsBeg = gidsBeg + gidsLen;
6367  const size_t valsLen = numEnt * numBytesPerValue;
6368 
6369  char* const numEntOut = exports + numEntBeg;
6370  char* const gidsOut = exports + gidsBeg;
6371  char* const valsOut = exports + valsBeg;
6372 
6373  size_t numBytesOut = 0;
6374  int errorCode = 0;
6375  numBytesOut += PackTraits<LO>::packValue (numEntOut, numEntLO);
6376 
6377  {
6378  Kokkos::pair<int, size_t> p;
6379  p = PackTraits<GO>::packArray (gidsOut, gidsIn, numEnt);
6380  errorCode += p.first;
6381  numBytesOut += p.second;
6382 
6383  p = PackTraits<ST>::packArray (valsOut, valsIn, numEnt);
6384  errorCode += p.first;
6385  numBytesOut += p.second;
6386  }
6387 
6388  const size_t expectedNumBytes = numEntLen + gidsLen + valsLen;
6389  TEUCHOS_TEST_FOR_EXCEPTION
6390  (numBytesOut != expectedNumBytes, std::logic_error, "packRow: "
6391  "numBytesOut = " << numBytesOut << " != expectedNumBytes = "
6392  << expectedNumBytes << ".");
6393  TEUCHOS_TEST_FOR_EXCEPTION
6394  (errorCode != 0, std::runtime_error, "packRow: "
6395  "PackTraits::packArray returned a nonzero error code");
6396 
6397  return numBytesOut;
6398  }
6399 
6400  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6401  size_t
6402  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
6403  unpackRow (GlobalOrdinal gidsOut[],
6404  impl_scalar_type valsOut[],
6405  const char imports[],
6406  const size_t offset,
6407  const size_t numBytes,
6408  const size_t numEnt,
6409  const size_t numBytesPerValue)
6410  {
6411  using Kokkos::View;
6412  using Kokkos::subview;
6414  typedef LocalOrdinal LO;
6415  typedef GlobalOrdinal GO;
6416  typedef impl_scalar_type ST;
6417 
6418  Details::ProfilingRegion region_upack_row(
6419  "Tpetra::CrsMatrix::unpackRow",
6420  "Import/Export"
6421  );
6422 
6423  if (numBytes == 0) {
6424  // Rows with zero bytes should always have zero entries.
6425  if (numEnt != 0) {
6426  const int myRank = this->getMap ()->getComm ()->getRank ();
6427  TEUCHOS_TEST_FOR_EXCEPTION
6428  (true, std::logic_error, "(Proc " << myRank << ") CrsMatrix::"
6429  "unpackRow: The number of bytes to unpack numBytes=0, but the "
6430  "number of entries to unpack (as reported by numPacketsPerLID) "
6431  "for this row numEnt=" << numEnt << " != 0.");
6432  }
6433  return 0;
6434  }
6435 
6436  if (numEnt == 0 && numBytes != 0) {
6437  const int myRank = this->getMap ()->getComm ()->getRank ();
6438  TEUCHOS_TEST_FOR_EXCEPTION
6439  (true, std::logic_error, "(Proc " << myRank << ") CrsMatrix::"
6440  "unpackRow: The number of entries to unpack (as reported by "
6441  "numPacketsPerLID) numEnt=0, but the number of bytes to unpack "
6442  "numBytes=" << numBytes << " != 0.");
6443  }
6444 
6445  const GO gid = 0; // packValueCount wants this
6446  const LO lid = 0; // packValueCount wants this
6447 
6448  const size_t numEntBeg = offset;
6449  const size_t numEntLen = PackTraits<LO>::packValueCount (lid);
6450  const size_t gidsBeg = numEntBeg + numEntLen;
6451  const size_t gidsLen = numEnt * PackTraits<GO>::packValueCount (gid);
6452  const size_t valsBeg = gidsBeg + gidsLen;
6453  const size_t valsLen = numEnt * numBytesPerValue;
6454 
6455  const char* const numEntIn = imports + numEntBeg;
6456  const char* const gidsIn = imports + gidsBeg;
6457  const char* const valsIn = imports + valsBeg;
6458 
6459  size_t numBytesOut = 0;
6460  int errorCode = 0;
6461  LO numEntOut;
6462  numBytesOut += PackTraits<LO>::unpackValue (numEntOut, numEntIn);
6463  if (static_cast<size_t> (numEntOut) != numEnt ||
6464  numEntOut == static_cast<LO> (0)) {
6465  const int myRank = this->getMap ()->getComm ()->getRank ();
6466  std::ostringstream os;
6467  os << "(Proc " << myRank << ") CrsMatrix::unpackRow: ";
6468  bool firstErrorCondition = false;
6469  if (static_cast<size_t> (numEntOut) != numEnt) {
6470  os << "Number of entries from numPacketsPerLID numEnt=" << numEnt
6471  << " does not equal number of entries unpacked from imports "
6472  "buffer numEntOut=" << numEntOut << ".";
6473  firstErrorCondition = true;
6474  }
6475  if (numEntOut == static_cast<LO> (0)) {
6476  if (firstErrorCondition) {
6477  os << " Also, ";
6478  }
6479  os << "Number of entries unpacked from imports buffer numEntOut=0, "
6480  "but number of bytes to unpack for this row numBytes=" << numBytes
6481  << " != 0. This should never happen, since packRow should only "
6482  "ever pack rows with a nonzero number of entries. In this case, "
6483  "the number of entries from numPacketsPerLID is numEnt=" << numEnt
6484  << ".";
6485  }
6486  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, os.str ());
6487  }
6488 
6489  {
6490  Kokkos::pair<int, size_t> p;
6491  p = PackTraits<GO>::unpackArray (gidsOut, gidsIn, numEnt);
6492  errorCode += p.first;
6493  numBytesOut += p.second;
6494 
6495  p = PackTraits<ST>::unpackArray (valsOut, valsIn, numEnt);
6496  errorCode += p.first;
6497  numBytesOut += p.second;
6498  }
6499 
6500  TEUCHOS_TEST_FOR_EXCEPTION
6501  (numBytesOut != numBytes, std::logic_error, "unpackRow: numBytesOut = "
6502  << numBytesOut << " != numBytes = " << numBytes << ".");
6503 
6504  const size_t expectedNumBytes = numEntLen + gidsLen + valsLen;
6505  TEUCHOS_TEST_FOR_EXCEPTION
6506  (numBytesOut != expectedNumBytes, std::logic_error, "unpackRow: "
6507  "numBytesOut = " << numBytesOut << " != expectedNumBytes = "
6508  << expectedNumBytes << ".");
6509 
6510  TEUCHOS_TEST_FOR_EXCEPTION
6511  (errorCode != 0, std::runtime_error, "unpackRow: "
6512  "PackTraits::unpackArray returned a nonzero error code");
6513 
6514  return numBytesOut;
6515  }
6516 
6517  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6518  void
6519  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
6520  allocatePackSpaceNew (Kokkos::DualView<char*, buffer_device_type>& exports,
6521  size_t& totalNumEntries,
6522  const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& exportLIDs) const
6523  {
6524  using Details::Behavior;
6526  using std::endl;
6527  typedef impl_scalar_type IST;
6528  typedef LocalOrdinal LO;
6529  typedef GlobalOrdinal GO;
6530  //const char tfecfFuncName[] = "allocatePackSpaceNew: ";
6531 
6532  // mfh 18 Oct 2017: Set TPETRA_VERBOSE to true for copious debug
6533  // output to std::cerr on every MPI process. This is unwise for
6534  // runs with large numbers of MPI processes.
6535  const bool verbose = Behavior::verbose("CrsMatrix");
6536  std::unique_ptr<std::string> prefix;
6537  if (verbose) {
6538  prefix = this->createPrefix("CrsMatrix", "allocatePackSpaceNew");
6539  std::ostringstream os;
6540  os << *prefix << "Before:"
6541  << endl
6542  << *prefix << " "
6543  << dualViewStatusToString (exports, "exports")
6544  << endl
6545  << *prefix << " "
6546  << dualViewStatusToString (exportLIDs, "exportLIDs")
6547  << endl;
6548  std::cerr << os.str ();
6549  }
6550 
6551  // The number of export LIDs must fit in LocalOrdinal, assuming
6552  // that the LIDs are distinct and valid on the calling process.
6553  const LO numExportLIDs = static_cast<LO> (exportLIDs.extent (0));
6554 
6555  TEUCHOS_ASSERT( ! exportLIDs.need_sync_host () );
6556  auto exportLIDs_h = exportLIDs.view_host ();
6557 
6558  // Count the total number of matrix entries to send.
6559  totalNumEntries = 0;
6560  for (LO i = 0; i < numExportLIDs; ++i) {
6561  const LO lclRow = exportLIDs_h[i];
6562  size_t curNumEntries = this->getNumEntriesInLocalRow (lclRow);
6563  // FIXME (mfh 25 Jan 2015) We should actually report invalid row
6564  // indices as an error. Just consider them nonowned for now.
6565  if (curNumEntries == Teuchos::OrdinalTraits<size_t>::invalid ()) {
6566  curNumEntries = 0;
6567  }
6568  totalNumEntries += curNumEntries;
6569  }
6570 
6571  // FIXME (mfh 24 Feb 2013, 24 Mar 2017) This code is only correct
6572  // if sizeof(IST) is a meaningful representation of the amount of
6573  // data in a Scalar instance. (LO and GO are always built-in
6574  // integer types.)
6575  //
6576  // Allocate the exports array. It does NOT need padding for
6577  // alignment, since we use memcpy to write to / read from send /
6578  // receive buffers.
6579  const size_t allocSize =
6580  static_cast<size_t> (numExportLIDs) * sizeof (LO) +
6581  totalNumEntries * (sizeof (IST) + sizeof (GO));
6582  if (static_cast<size_t> (exports.extent (0)) < allocSize) {
6583  using exports_type = Kokkos::DualView<char*, buffer_device_type>;
6584 
6585  const std::string oldLabel = exports.d_view.label ();
6586  const std::string newLabel = (oldLabel == "") ? "exports" : oldLabel;
6587  exports = exports_type (newLabel, allocSize);
6588  }
6589 
6590  if (verbose) {
6591  std::ostringstream os;
6592  os << *prefix << "After:"
6593  << endl
6594  << *prefix << " "
6595  << dualViewStatusToString (exports, "exports")
6596  << endl
6597  << *prefix << " "
6598  << dualViewStatusToString (exportLIDs, "exportLIDs")
6599  << endl;
6600  std::cerr << os.str ();
6601  }
6602  }
6603 
6604  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6605  void
6607  packNew (const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& exportLIDs,
6608  Kokkos::DualView<char*, buffer_device_type>& exports,
6609  const Kokkos::DualView<size_t*, buffer_device_type>& numPacketsPerLID,
6610  size_t& constantNumPackets) const
6611  {
6612  // The call to packNew in packAndPrepare catches and handles any exceptions.
6613  Details::ProfilingRegion region_pack_new("Tpetra::CrsMatrix::packNew", "Import/Export");
6614  if (this->isStaticGraph ()) {
6616  packCrsMatrixNew (*this, exports, numPacketsPerLID, exportLIDs,
6617  constantNumPackets);
6618  }
6619  else {
6620  this->packNonStaticNew (exportLIDs, exports, numPacketsPerLID,
6621  constantNumPackets);
6622  }
6623  }
6624 
6625  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6626  void
6628  packNonStaticNew (const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& exportLIDs,
6629  Kokkos::DualView<char*, buffer_device_type>& exports,
6630  const Kokkos::DualView<size_t*, buffer_device_type>& numPacketsPerLID,
6631  size_t& constantNumPackets) const
6632  {
6633  using Details::Behavior;
6635  using Details::PackTraits;
6637  using Kokkos::View;
6638  using std::endl;
6639  using LO = LocalOrdinal;
6640  using GO = GlobalOrdinal;
6641  using ST = impl_scalar_type;
6642  const char tfecfFuncName[] = "packNonStaticNew: ";
6643 
6644  const bool verbose = Behavior::verbose("CrsMatrix");
6645  std::unique_ptr<std::string> prefix;
6646  if (verbose) {
6647  prefix = this->createPrefix("CrsMatrix", "packNonStaticNew");
6648  std::ostringstream os;
6649  os << *prefix << "Start" << endl;
6650  std::cerr << os.str ();
6651  }
6652 
6653  const size_t numExportLIDs = static_cast<size_t> (exportLIDs.extent (0));
6654  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6655  (numExportLIDs != static_cast<size_t> (numPacketsPerLID.extent (0)),
6656  std::invalid_argument, "exportLIDs.size() = " << numExportLIDs
6657  << " != numPacketsPerLID.size() = " << numPacketsPerLID.extent (0)
6658  << ".");
6659 
6660  // Setting this to zero tells the caller to expect a possibly
6661  // different ("nonconstant") number of packets per local index
6662  // (i.e., a possibly different number of entries per row).
6663  constantNumPackets = 0;
6664 
6665  // The pack buffer 'exports' enters this method possibly
6666  // unallocated. Do the first two parts of "Count, allocate, fill,
6667  // compute."
6668  size_t totalNumEntries = 0;
6669  this->allocatePackSpaceNew (exports, totalNumEntries, exportLIDs);
6670  const size_t bufSize = static_cast<size_t> (exports.extent (0));
6671 
6672  // Write-only host access
6673  exports.clear_sync_state();
6674  exports.modify_host();
6675  auto exports_h = exports.view_host ();
6676  if (verbose) {
6677  std::ostringstream os;
6678  os << *prefix << "After marking exports as modified on host, "
6679  << dualViewStatusToString (exports, "exports") << endl;
6680  std::cerr << os.str ();
6681  }
6682 
6683  // Read-only host access
6684  auto exportLIDs_h = exportLIDs.view_host ();
6685 
6686  // Write-only host access
6687  const_cast<Kokkos::DualView<size_t*, buffer_device_type>*>(&numPacketsPerLID)->clear_sync_state();
6688  const_cast<Kokkos::DualView<size_t*, buffer_device_type>*>(&numPacketsPerLID)->modify_host();
6689  auto numPacketsPerLID_h = numPacketsPerLID.view_host ();
6690 
6691  // Compute the number of "packets" (in this case, bytes) per
6692  // export LID (in this case, local index of the row to send), and
6693  // actually pack the data.
6694  auto maxRowNumEnt = this->getLocalMaxNumRowEntries();
6695 
6696 
6697  // Temporary buffer for global column indices.
6698  typename global_inds_host_view_type::non_const_type gidsIn_k;
6699  if (this->isLocallyIndexed()) { // Need storage for Global IDs
6700  gidsIn_k =
6701  typename global_inds_host_view_type::non_const_type("packGids",
6702  maxRowNumEnt);
6703  }
6704 
6705  size_t offset = 0; // current index into 'exports' array.
6706  for (size_t i = 0; i < numExportLIDs; ++i) {
6707  const LO lclRow = exportLIDs_h[i];
6708 
6709  size_t numBytes = 0;
6710  size_t numEnt = this->getNumEntriesInLocalRow (lclRow);
6711 
6712  // Only pack this row's data if it has a nonzero number of
6713  // entries. We can do this because receiving processes get the
6714  // number of packets, and will know that zero packets means zero
6715  // entries.
6716  if (numEnt == 0) {
6717  numPacketsPerLID_h[i] = 0;
6718  continue;
6719  }
6720 
6721  if (this->isLocallyIndexed ()) {
6722  typename global_inds_host_view_type::non_const_type gidsIn;
6723  values_host_view_type valsIn;
6724  // If the matrix is locally indexed on the calling process, we
6725  // have to use its column Map (which it _must_ have in this
6726  // case) to convert to global indices.
6727  local_inds_host_view_type lidsIn;
6728  this->getLocalRowView (lclRow, lidsIn, valsIn);
6729  const map_type& colMap = * (this->getColMap ());
6730  for (size_t k = 0; k < numEnt; ++k) {
6731  gidsIn_k[k] = colMap.getGlobalElement (lidsIn[k]);
6732  }
6733  gidsIn = Kokkos::subview(gidsIn_k, Kokkos::make_pair(GO(0),GO(numEnt)));
6734 
6735  const size_t numBytesPerValue =
6736  PackTraits<ST>::packValueCount (valsIn[0]);
6737  numBytes = this->packRow (exports_h.data (), offset, numEnt,
6738  gidsIn.data (), valsIn.data (),
6739  numBytesPerValue);
6740  }
6741  else if (this->isGloballyIndexed ()) {
6742  global_inds_host_view_type gidsIn;
6743  values_host_view_type valsIn;
6744  // If the matrix is globally indexed on the calling process,
6745  // then we can use the column indices directly. However, we
6746  // have to get the global row index. The calling process must
6747  // have a row Map, since otherwise it shouldn't be participating
6748  // in packing operations.
6749  const map_type& rowMap = * (this->getRowMap ());
6750  const GO gblRow = rowMap.getGlobalElement (lclRow);
6751  this->getGlobalRowView (gblRow, gidsIn, valsIn);
6752 
6753  const size_t numBytesPerValue =
6754  PackTraits<ST>::packValueCount (valsIn[0]);
6755  numBytes = this->packRow (exports_h.data (), offset, numEnt,
6756  gidsIn.data (), valsIn.data (),
6757  numBytesPerValue);
6758  }
6759  // mfh 11 Sep 2017: Currently, if the matrix is neither globally
6760  // nor locally indexed, then it has no entries. Therefore,
6761  // there is nothing to pack. No worries!
6762 
6763  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6764  (offset > bufSize || offset + numBytes > bufSize, std::logic_error,
6765  "First invalid offset into 'exports' pack buffer at index i = " << i
6766  << ". exportLIDs_h[i]: " << exportLIDs_h[i] << ", bufSize: " <<
6767  bufSize << ", offset: " << offset << ", numBytes: " << numBytes <<
6768  ".");
6769  // numPacketsPerLID_h[i] is the number of "packets" in the
6770  // current local row i. Packet=char (really "byte") so use the
6771  // number of bytes of the packed data for that row.
6772  numPacketsPerLID_h[i] = numBytes;
6773  offset += numBytes;
6774  }
6775 
6776  if (verbose) {
6777  std::ostringstream os;
6778  os << *prefix << "Tpetra::CrsMatrix::packNonStaticNew: After:" << endl
6779  << *prefix << " "
6780  << dualViewStatusToString (exports, "exports")
6781  << endl
6782  << *prefix << " "
6783  << dualViewStatusToString (exportLIDs, "exportLIDs")
6784  << endl;
6785  std::cerr << os.str ();
6786  }
6787  }
6788 
6789  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6790  LocalOrdinal
6791  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
6792  combineGlobalValuesRaw(const LocalOrdinal lclRow,
6793  const LocalOrdinal numEnt,
6794  const impl_scalar_type vals[],
6795  const GlobalOrdinal cols[],
6796  const Tpetra::CombineMode combMode,
6797  const char* const prefix,
6798  const bool debug,
6799  const bool verbose)
6800  {
6801  using GO = GlobalOrdinal;
6802 
6803  // mfh 23 Mar 2017: This branch is not thread safe in a debug
6804  // build, due to use of Teuchos::ArrayView; see #229.
6805  const GO gblRow = myGraph_->rowMap_->getGlobalElement(lclRow);
6806  Teuchos::ArrayView<const GO> cols_av
6807  (numEnt == 0 ? nullptr : cols, numEnt);
6808  Teuchos::ArrayView<const Scalar> vals_av
6809  (numEnt == 0 ? nullptr : reinterpret_cast<const Scalar*> (vals), numEnt);
6810 
6811  // FIXME (mfh 23 Mar 2017) This is a work-around for less common
6812  // combine modes. combineGlobalValues throws on error; it does
6813  // not return an error code. Thus, if it returns, it succeeded.
6814  combineGlobalValues(gblRow, cols_av, vals_av, combMode,
6815  prefix, debug, verbose);
6816  return numEnt;
6817  }
6818 
6819  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6820  void
6821  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
6822  combineGlobalValues(
6823  const GlobalOrdinal globalRowIndex,
6824  const Teuchos::ArrayView<const GlobalOrdinal>& columnIndices,
6825  const Teuchos::ArrayView<const Scalar>& values,
6826  const Tpetra::CombineMode combineMode,
6827  const char* const prefix,
6828  const bool debug,
6829  const bool verbose)
6830  {
6831  const char tfecfFuncName[] = "combineGlobalValues: ";
6832 
6833  if (isStaticGraph ()) {
6834  // INSERT doesn't make sense for a static graph, since you
6835  // aren't allowed to change the structure of the graph.
6836  // However, all the other combine modes work.
6837  if (combineMode == ADD) {
6838  sumIntoGlobalValues (globalRowIndex, columnIndices, values);
6839  }
6840  else if (combineMode == REPLACE) {
6841  replaceGlobalValues (globalRowIndex, columnIndices, values);
6842  }
6843  else if (combineMode == ABSMAX) {
6844  using ::Tpetra::Details::AbsMax;
6845  AbsMax<Scalar> f;
6846  this->template transformGlobalValues<AbsMax<Scalar> > (globalRowIndex,
6847  columnIndices,
6848  values, f);
6849  }
6850  else if (combineMode == INSERT) {
6851  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6852  (isStaticGraph() && combineMode == INSERT,
6853  std::invalid_argument, "INSERT combine mode is forbidden "
6854  "if the matrix has a static (const) graph (i.e., was "
6855  "constructed with the CrsMatrix constructor that takes a "
6856  "const CrsGraph pointer).");
6857  }
6858  else {
6859  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6860  (true, std::logic_error, "Invalid combine mode; should "
6861  "never get here! "
6862  "Please report this bug to the Tpetra developers.");
6863  }
6864  }
6865  else { // The matrix has a dynamic graph.
6866  if (combineMode == ADD || combineMode == INSERT) {
6867  // For a dynamic graph, all incoming column indices are
6868  // inserted into the target graph. Duplicate indices will
6869  // have their values summed. In this context, ADD and INSERT
6870  // are equivalent. We need to call insertGlobalValues()
6871  // anyway if the column indices don't yet exist in this row,
6872  // so we just call insertGlobalValues() for both cases.
6873  insertGlobalValuesFilteredChecked(globalRowIndex,
6874  columnIndices, values, prefix, debug, verbose);
6875  }
6876  // FIXME (mfh 14 Mar 2012):
6877  //
6878  // Implementing ABSMAX or REPLACE for a dynamic graph would
6879  // require modifying assembly to attach a possibly different
6880  // combine mode to each inserted (i, j, A_ij) entry. For
6881  // example, consider two different Export operations to the same
6882  // target CrsMatrix, the first with ABSMAX combine mode and the
6883  // second with REPLACE. This isn't a common use case, so we
6884  // won't mess with it for now.
6885  else if (combineMode == ABSMAX) {
6886  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
6887  ! isStaticGraph () && combineMode == ABSMAX, std::logic_error,
6888  "ABSMAX combine mode when the matrix has a dynamic graph is not yet "
6889  "implemented.");
6890  }
6891  else if (combineMode == REPLACE) {
6892  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
6893  ! isStaticGraph () && combineMode == REPLACE, std::logic_error,
6894  "REPLACE combine mode when the matrix has a dynamic graph is not yet "
6895  "implemented.");
6896  }
6897  else {
6898  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
6899  true, std::logic_error, "Should never get here! Please report this "
6900  "bug to the Tpetra developers.");
6901  }
6902  }
6903  }
6904 
6905  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
6906  void
6909  (const Kokkos::DualView<const local_ordinal_type*, buffer_device_type>& importLIDs,
6910  Kokkos::DualView<char*, buffer_device_type> imports,
6911  Kokkos::DualView<size_t*, buffer_device_type> numPacketsPerLID,
6912  const size_t constantNumPackets,
6913  const CombineMode combineMode)
6914  {
6915  using Details::Behavior;
6918  using std::endl;
6919  const char tfecfFuncName[] = "unpackAndCombine: ";
6920  ProfilingRegion regionUAC ("Tpetra::CrsMatrix::unpackAndCombine");
6921 
6922  const bool debug = Behavior::debug("CrsMatrix");
6923  const bool verbose = Behavior::verbose("CrsMatrix");
6924  constexpr int numValidModes = 5;
6925  const CombineMode validModes[numValidModes] =
6926  {ADD, REPLACE, ABSMAX, INSERT, ZERO};
6927  const char* validModeNames[numValidModes] =
6928  {"ADD", "REPLACE", "ABSMAX", "INSERT", "ZERO"};
6929 
6930  std::unique_ptr<std::string> prefix;
6931  if (verbose) {
6932  prefix = this->createPrefix("CrsMatrix", "unpackAndCombine");
6933  std::ostringstream os;
6934  os << *prefix << "Start:" << endl
6935  << *prefix << " "
6936  << dualViewStatusToString (importLIDs, "importLIDs")
6937  << endl
6938  << *prefix << " "
6939  << dualViewStatusToString (imports, "imports")
6940  << endl
6941  << *prefix << " "
6942  << dualViewStatusToString (numPacketsPerLID, "numPacketsPerLID")
6943  << endl
6944  << *prefix << " constantNumPackets: " << constantNumPackets
6945  << endl
6946  << *prefix << " combineMode: " << combineModeToString (combineMode)
6947  << endl;
6948  std::cerr << os.str ();
6949  }
6950 
6951  if (debug) {
6952  if (std::find (validModes, validModes+numValidModes, combineMode) ==
6953  validModes+numValidModes) {
6954  std::ostringstream os;
6955  os << "Invalid combine mode. Valid modes are {";
6956  for (int k = 0; k < numValidModes; ++k) {
6957  os << validModeNames[k];
6958  if (k < numValidModes - 1) {
6959  os << ", ";
6960  }
6961  }
6962  os << "}.";
6963  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6964  (true, std::invalid_argument, os.str ());
6965  }
6966  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
6967  (importLIDs.extent(0) != numPacketsPerLID.extent(0),
6968  std::invalid_argument, "importLIDs.extent(0)="
6969  << importLIDs.extent(0)
6970  << " != numPacketsPerLID.extent(0)="
6971  << numPacketsPerLID.extent(0) << ".");
6972  }
6973 
6974  if (combineMode == ZERO) {
6975  return; // nothing to do
6976  }
6977 
6978  if (debug) {
6979  using Teuchos::reduceAll;
6980  std::unique_ptr<std::ostringstream> msg (new std::ostringstream ());
6981  int lclBad = 0;
6982  try {
6983  unpackAndCombineImpl(importLIDs, imports, numPacketsPerLID,
6984  constantNumPackets, combineMode,
6985  verbose);
6986  } catch (std::exception& e) {
6987  lclBad = 1;
6988  *msg << e.what ();
6989  }
6990  int gblBad = 0;
6991  const Teuchos::Comm<int>& comm = * (this->getComm ());
6992  reduceAll<int, int> (comm, Teuchos::REDUCE_MAX,
6993  lclBad, Teuchos::outArg (gblBad));
6994  if (gblBad != 0) {
6995  // mfh 22 Oct 2017: 'prefix' might be null, since it is only
6996  // initialized in a debug build. Thus, we get the process
6997  // rank again here. This is an error message, so the small
6998  // run-time cost doesn't matter. See #1887.
6999  std::ostringstream os;
7000  os << "Proc " << comm.getRank () << ": " << msg->str () << endl;
7001  msg = std::unique_ptr<std::ostringstream> (new std::ostringstream ());
7002  ::Tpetra::Details::gathervPrint (*msg, os.str (), comm);
7003  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7004  (true, std::logic_error, std::endl << "unpackAndCombineImpl "
7005  "threw an exception on one or more participating processes: "
7006  << endl << msg->str ());
7007  }
7008  }
7009  else {
7010  unpackAndCombineImpl(importLIDs, imports, numPacketsPerLID,
7011  constantNumPackets, combineMode,
7012  verbose);
7013  }
7014 
7015  if (verbose) {
7016  std::ostringstream os;
7017  os << *prefix << "Done!" << endl
7018  << *prefix << " "
7019  << dualViewStatusToString (importLIDs, "importLIDs")
7020  << endl
7021  << *prefix << " "
7022  << dualViewStatusToString (imports, "imports")
7023  << endl
7024  << *prefix << " "
7025  << dualViewStatusToString (numPacketsPerLID, "numPacketsPerLID")
7026  << endl;
7027  std::cerr << os.str ();
7028  }
7029  }
7030 
7031  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7032  void
7035  const Kokkos::DualView<const local_ordinal_type*,
7036  buffer_device_type>& importLIDs,
7037  Kokkos::DualView<char*, buffer_device_type> imports,
7038  Kokkos::DualView<size_t*, buffer_device_type> numPacketsPerLID,
7039  const size_t constantNumPackets,
7040  const CombineMode combineMode,
7041  const bool verbose)
7042  {
7043  Details::ProfilingRegion region_unpack_and_combine_impl(
7044  "Tpetra::CrsMatrix::unpackAndCombineImpl",
7045  "Import/Export"
7046  );
7047  using std::endl;
7048  const char tfecfFuncName[] = "unpackAndCombineImpl";
7049  std::unique_ptr<std::string> prefix;
7050  if (verbose) {
7051  prefix = this->createPrefix("CrsMatrix", tfecfFuncName);
7052  std::ostringstream os;
7053  os << *prefix << "isStaticGraph(): "
7054  << (isStaticGraph() ? "true" : "false")
7055  << ", importLIDs.extent(0): "
7056  << importLIDs.extent(0)
7057  << ", imports.extent(0): "
7058  << imports.extent(0)
7059  << ", numPacketsPerLID.extent(0): "
7060  << numPacketsPerLID.extent(0)
7061  << endl;
7062  std::cerr << os.str();
7063  }
7064 
7065  if (isStaticGraph ()) {
7066  using Details::unpackCrsMatrixAndCombineNew;
7067  unpackCrsMatrixAndCombineNew(*this, imports, numPacketsPerLID,
7068  importLIDs, constantNumPackets,
7069  combineMode);
7070  }
7071  else {
7072  {
7073  using padding_type = typename crs_graph_type::padding_type;
7074  std::unique_ptr<padding_type> padding;
7075  try {
7076  padding = myGraph_->computePaddingForCrsMatrixUnpack(
7077  importLIDs, imports, numPacketsPerLID, verbose);
7078  }
7079  catch (std::exception& e) {
7080  const auto rowMap = getRowMap();
7081  const auto comm = rowMap.is_null() ? Teuchos::null :
7082  rowMap->getComm();
7083  const int myRank = comm.is_null() ? -1 : comm->getRank();
7084  TEUCHOS_TEST_FOR_EXCEPTION
7085  (true, std::runtime_error, "Proc " << myRank << ": "
7086  "Tpetra::CrsGraph::computePaddingForCrsMatrixUnpack "
7087  "threw an exception: " << e.what());
7088  }
7089  if (verbose) {
7090  std::ostringstream os;
7091  os << *prefix << "Call applyCrsPadding" << endl;
7092  std::cerr << os.str();
7093  }
7094  applyCrsPadding(*padding, verbose);
7095  }
7096  if (verbose) {
7097  std::ostringstream os;
7098  os << *prefix << "Call unpackAndCombineImplNonStatic" << endl;
7099  std::cerr << os.str();
7100  }
7101  unpackAndCombineImplNonStatic(importLIDs, imports,
7102  numPacketsPerLID,
7103  constantNumPackets,
7104  combineMode);
7105  }
7106 
7107  if (verbose) {
7108  std::ostringstream os;
7109  os << *prefix << "Done" << endl;
7110  std::cerr << os.str();
7111  }
7112  }
7113 
7114  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7115  void
7116  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
7117  unpackAndCombineImplNonStatic(
7118  const Kokkos::DualView<const local_ordinal_type*,
7119  buffer_device_type>& importLIDs,
7120  Kokkos::DualView<char*, buffer_device_type> imports,
7121  Kokkos::DualView<size_t*, buffer_device_type> numPacketsPerLID,
7122  const size_t constantNumPackets,
7123  const CombineMode combineMode)
7124  {
7125  using Kokkos::View;
7126  using Kokkos::subview;
7127  using Kokkos::MemoryUnmanaged;
7128  using Details::Behavior;
7131  using Details::PackTraits;
7132  using Details::ScalarViewTraits;
7133  using std::endl;
7134  using LO = LocalOrdinal;
7135  using GO = GlobalOrdinal;
7136  using ST = impl_scalar_type;
7137  using size_type = typename Teuchos::ArrayView<LO>::size_type;
7138  using HES =
7139  typename View<int*, device_type>::HostMirror::execution_space;
7140  using pair_type = std::pair<typename View<int*, HES>::size_type,
7141  typename View<int*, HES>::size_type>;
7142  using gids_out_type = View<GO*, HES, MemoryUnmanaged>;
7143  using vals_out_type = View<ST*, HES, MemoryUnmanaged>;
7144  const char tfecfFuncName[] = "unpackAndCombineImplNonStatic";
7145 
7146  const bool debug = Behavior::debug("CrsMatrix");
7147  const bool verbose = Behavior::verbose("CrsMatrix");
7148  std::unique_ptr<std::string> prefix;
7149  if (verbose) {
7150  prefix = this->createPrefix("CrsMatrix", tfecfFuncName);
7151  std::ostringstream os;
7152  os << *prefix << endl; // we've already printed DualViews' statuses
7153  std::cerr << os.str ();
7154  }
7155  const char* const prefix_raw =
7156  verbose ? prefix.get()->c_str() : nullptr;
7157 
7158  const size_type numImportLIDs = importLIDs.extent (0);
7159  if (combineMode == ZERO || numImportLIDs == 0) {
7160  return; // nothing to do; no need to combine entries
7161  }
7162 
7163  Details::ProfilingRegion region_unpack_and_combine_impl_non_static(
7164  "Tpetra::CrsMatrix::unpackAndCombineImplNonStatic",
7165  "Import/Export"
7166  );
7167 
7168  // We're unpacking on host. This is read-only host access.
7169  if (imports.need_sync_host()) {
7170  imports.sync_host ();
7171  }
7172  auto imports_h = imports.view_host();
7173 
7174  // Read-only host access.
7175  if (numPacketsPerLID.need_sync_host()) {
7176  numPacketsPerLID.sync_host ();
7177  }
7178  auto numPacketsPerLID_h = numPacketsPerLID.view_host();
7179 
7180  TEUCHOS_ASSERT( ! importLIDs.need_sync_host() );
7181  auto importLIDs_h = importLIDs.view_host();
7182 
7183  size_t numBytesPerValue;
7184  {
7185  // FIXME (mfh 17 Feb 2015, tjf 2 Aug 2017) What do I do about Scalar types
7186  // with run-time size? We already assume that all entries in both the
7187  // source and target matrices have the same size. If the calling process
7188  // owns at least one entry in either matrix, we can use that entry to set
7189  // the size. However, it is possible that the calling process owns no
7190  // entries. In that case, we're in trouble. One way to fix this would be
7191  // for each row's data to contain the run-time size. This is only
7192  // necessary if the size is not a compile-time constant.
7193  Scalar val;
7194  numBytesPerValue = PackTraits<ST>::packValueCount (val);
7195  }
7196 
7197  // Determine the maximum number of entries in any one row
7198  size_t offset = 0;
7199  size_t maxRowNumEnt = 0;
7200  for (size_type i = 0; i < numImportLIDs; ++i) {
7201  const size_t numBytes = numPacketsPerLID_h[i];
7202  if (numBytes == 0) {
7203  continue; // empty buffer for that row means that the row is empty
7204  }
7205  // We need to unpack a nonzero number of entries for this row.
7206  if (debug) {
7207  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7208  (offset + numBytes > size_t(imports_h.extent (0)),
7209  std::logic_error, ": At local row index importLIDs_h[i="
7210  << i << "]=" << importLIDs_h[i] << ", offset (=" << offset
7211  << ") + numBytes (=" << numBytes << ") > "
7212  "imports_h.extent(0)=" << imports_h.extent (0) << ".");
7213  }
7214  LO numEntLO = 0;
7215 
7216  if (debug) {
7217  const size_t theNumBytes =
7218  PackTraits<LO>::packValueCount (numEntLO);
7219  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7220  (theNumBytes > numBytes, std::logic_error, ": theNumBytes="
7221  << theNumBytes << " > numBytes = " << numBytes << ".");
7222  }
7223  const char* const inBuf = imports_h.data () + offset;
7224  const size_t actualNumBytes =
7225  PackTraits<LO>::unpackValue (numEntLO, inBuf);
7226 
7227  if (debug) {
7228  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7229  (actualNumBytes > numBytes, std::logic_error, ": At i=" << i
7230  << ", actualNumBytes=" << actualNumBytes
7231  << " > numBytes=" << numBytes << ".");
7232  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7233  (numEntLO == 0, std::logic_error, ": At local row index "
7234  "importLIDs_h[i=" << i << "]=" << importLIDs_h[i] << ", "
7235  "the number of entries read from the packed data is "
7236  "numEntLO=" << numEntLO << ", but numBytes=" << numBytes
7237  << " != 0.");
7238  }
7239 
7240  maxRowNumEnt = std::max(size_t(numEntLO), maxRowNumEnt);
7241  offset += numBytes;
7242  }
7243 
7244  // Temporary space to cache incoming global column indices and
7245  // values. Column indices come in as global indices, in case the
7246  // source object's column Map differs from the target object's
7247  // (this's) column Map.
7248  View<GO*, HES> gblColInds;
7249  View<LO*, HES> lclColInds;
7250  View<ST*, HES> vals;
7251  {
7252  GO gid = 0;
7253  LO lid = 0;
7254  // FIXME (mfh 17 Feb 2015, tjf 2 Aug 2017) What do I do about Scalar types
7255  // with run-time size? We already assume that all entries in both the
7256  // source and target matrices have the same size. If the calling process
7257  // owns at least one entry in either matrix, we can use that entry to set
7258  // the size. However, it is possible that the calling process owns no
7259  // entries. In that case, we're in trouble. One way to fix this would be
7260  // for each row's data to contain the run-time size. This is only
7261  // necessary if the size is not a compile-time constant.
7262  Scalar val;
7263  gblColInds = ScalarViewTraits<GO, HES>::allocateArray(
7264  gid, maxRowNumEnt, "gids");
7265  lclColInds = ScalarViewTraits<LO, HES>::allocateArray(
7266  lid, maxRowNumEnt, "lids");
7267  vals = ScalarViewTraits<ST, HES>::allocateArray(
7268  val, maxRowNumEnt, "vals");
7269  }
7270 
7271  offset = 0;
7272  for (size_type i = 0; i < numImportLIDs; ++i) {
7273  const size_t numBytes = numPacketsPerLID_h[i];
7274  if (numBytes == 0) {
7275  continue; // empty buffer for that row means that the row is empty
7276  }
7277  LO numEntLO = 0;
7278  const char* const inBuf = imports_h.data () + offset;
7279  (void) PackTraits<LO>::unpackValue (numEntLO, inBuf);
7280 
7281  const size_t numEnt = static_cast<size_t>(numEntLO);;
7282  const LO lclRow = importLIDs_h[i];
7283 
7284  gids_out_type gidsOut = subview (gblColInds, pair_type (0, numEnt));
7285  vals_out_type valsOut = subview (vals, pair_type (0, numEnt));
7286 
7287  const size_t numBytesOut =
7288  unpackRow (gidsOut.data (), valsOut.data (), imports_h.data (),
7289  offset, numBytes, numEnt, numBytesPerValue);
7290  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
7291  (numBytes != numBytesOut, std::logic_error, ": At i=" << i
7292  << ", numBytes=" << numBytes << " != numBytesOut="
7293  << numBytesOut << ".");
7294 
7295  const ST* const valsRaw = const_cast<const ST*> (valsOut.data ());
7296  const GO* const gidsRaw = const_cast<const GO*> (gidsOut.data ());
7297  combineGlobalValuesRaw(lclRow, numEnt, valsRaw, gidsRaw,
7298  combineMode, prefix_raw, debug, verbose);
7299  // Don't update offset until current LID has succeeded.
7300  offset += numBytes;
7301  } // for each import LID i
7302 
7303  if (verbose) {
7304  std::ostringstream os;
7305  os << *prefix << "Done" << endl;
7306  std::cerr << os.str();
7307  }
7308  }
7309 
7310  template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7311  Teuchos::RCP<MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
7313  getColumnMapMultiVector (const MV& X_domainMap,
7314  const bool force) const
7315  {
7316  using Teuchos::null;
7317  using Teuchos::RCP;
7318  using Teuchos::rcp;
7319 
7320  TEUCHOS_TEST_FOR_EXCEPTION(
7321  ! this->hasColMap (), std::runtime_error, "Tpetra::CrsMatrix::getColumn"
7322  "MapMultiVector: You may only call this method if the matrix has a "
7323  "column Map. If the matrix does not yet have a column Map, you should "
7324  "first call fillComplete (with domain and range Map if necessary).");
7325 
7326  // If the graph is not fill complete, then the Import object (if
7327  // one should exist) hasn't been constructed yet.
7328  TEUCHOS_TEST_FOR_EXCEPTION(
7329  ! this->getGraph ()->isFillComplete (), std::runtime_error, "Tpetra::"
7330  "CrsMatrix::getColumnMapMultiVector: You may only call this method if "
7331  "this matrix's graph is fill complete.");
7332 
7333  const size_t numVecs = X_domainMap.getNumVectors ();
7334  RCP<const import_type> importer = this->getGraph ()->getImporter ();
7335  RCP<const map_type> colMap = this->getColMap ();
7336 
7337  RCP<MV> X_colMap; // null by default
7338 
7339  // If the Import object is trivial (null), then we don't need a
7340  // separate column Map multivector. Just return null in that
7341  // case. The caller is responsible for knowing not to use the
7342  // returned null pointer.
7343  //
7344  // If the Import is nontrivial, then we do need a separate
7345  // column Map multivector for the Import operation. Check in
7346  // that case if we have to (re)create the column Map
7347  // multivector.
7348  if (! importer.is_null () || force) {
7349  if (importMV_.is_null () || importMV_->getNumVectors () != numVecs) {
7350  X_colMap = rcp (new MV (colMap, numVecs));
7351 
7352  // Cache the newly created multivector for later reuse.
7353  importMV_ = X_colMap;
7354  }
7355  else { // Yay, we can reuse the cached multivector!
7356  X_colMap = importMV_;
7357  // mfh 09 Jan 2013: We don't have to fill with zeros first,
7358  // because the Import uses INSERT combine mode, which overwrites
7359  // existing entries.
7360  //
7361  //X_colMap->putScalar (ZERO);
7362  }
7363  }
7364  return X_colMap;
7365  }
7366 
7367  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7368  Teuchos::RCP<MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
7371  const bool force) const
7372  {
7373  using Teuchos::null;
7374  using Teuchos::RCP;
7375  using Teuchos::rcp;
7376 
7377  // If the graph is not fill complete, then the Export object (if
7378  // one should exist) hasn't been constructed yet.
7379  TEUCHOS_TEST_FOR_EXCEPTION(
7380  ! this->getGraph ()->isFillComplete (), std::runtime_error, "Tpetra::"
7381  "CrsMatrix::getRowMapMultiVector: You may only call this method if this "
7382  "matrix's graph is fill complete.");
7383 
7384  const size_t numVecs = Y_rangeMap.getNumVectors ();
7385  RCP<const export_type> exporter = this->getGraph ()->getExporter ();
7386  // Every version of the constructor takes either a row Map, or a
7387  // graph (all of whose constructors take a row Map). Thus, the
7388  // matrix always has a row Map.
7389  RCP<const map_type> rowMap = this->getRowMap ();
7390 
7391  RCP<MV> Y_rowMap; // null by default
7392 
7393  // If the Export object is trivial (null), then we don't need a
7394  // separate row Map multivector. Just return null in that case.
7395  // The caller is responsible for knowing not to use the returned
7396  // null pointer.
7397  //
7398  // If the Export is nontrivial, then we do need a separate row
7399  // Map multivector for the Export operation. Check in that case
7400  // if we have to (re)create the row Map multivector.
7401  if (! exporter.is_null () || force) {
7402  if (exportMV_.is_null () || exportMV_->getNumVectors () != numVecs) {
7403  Y_rowMap = rcp (new MV (rowMap, numVecs));
7404  exportMV_ = Y_rowMap; // Cache the newly created MV for later reuse.
7405  }
7406  else { // Yay, we can reuse the cached multivector!
7407  Y_rowMap = exportMV_;
7408  }
7409  }
7410  return Y_rowMap;
7411  }
7412 
7413  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7414  void
7416  removeEmptyProcessesInPlace (const Teuchos::RCP<const map_type>& newMap)
7417  {
7418  TEUCHOS_TEST_FOR_EXCEPTION(
7419  myGraph_.is_null (), std::logic_error, "Tpetra::CrsMatrix::"
7420  "removeEmptyProcessesInPlace: This method does not work when the matrix "
7421  "was created with a constant graph (that is, when it was created using "
7422  "the version of its constructor that takes an RCP<const CrsGraph>). "
7423  "This is because the matrix is not allowed to modify the graph in that "
7424  "case, but removing empty processes requires modifying the graph.");
7425  myGraph_->removeEmptyProcessesInPlace (newMap);
7426  // Even though CrsMatrix's row Map (as returned by getRowMap())
7427  // comes from its CrsGraph, CrsMatrix still implements DistObject,
7428  // so we also have to change the DistObject's Map.
7429  this->map_ = this->getRowMap ();
7430  // In the nonconst graph case, staticGraph_ is just a const
7431  // pointer to myGraph_. This assignment is probably redundant,
7432  // but it doesn't hurt.
7433  staticGraph_ = Teuchos::rcp_const_cast<const Graph> (myGraph_);
7434  }
7435 
7436  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7437  Teuchos::RCP<RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
7439  add (const Scalar& alpha,
7441  const Scalar& beta,
7442  const Teuchos::RCP<const map_type>& domainMap,
7443  const Teuchos::RCP<const map_type>& rangeMap,
7444  const Teuchos::RCP<Teuchos::ParameterList>& params) const
7445  {
7446  using Teuchos::Array;
7447  using Teuchos::ArrayView;
7448  using Teuchos::ParameterList;
7449  using Teuchos::RCP;
7450  using Teuchos::rcp;
7451  using Teuchos::rcp_implicit_cast;
7452  using Teuchos::sublist;
7453  using std::endl;
7454  using LO = local_ordinal_type;
7455  using GO = global_ordinal_type;
7456  using crs_matrix_type =
7458  const char errPfx[] = "Tpetra::CrsMatrix::add: ";
7459 
7460  const bool debug = Details::Behavior::debug("CrsMatrix");
7461  const bool verbose = Details::Behavior::verbose("CrsMatrix");
7462  std::unique_ptr<std::string> prefix;
7463  if (verbose) {
7464  prefix = this->createPrefix("CrsMatrix", "add");
7465  std::ostringstream os;
7466  os << *prefix << "Start" << endl;
7467  std::cerr << os.str ();
7468  }
7469 
7470  const crs_matrix_type& B = *this; // a convenient abbreviation
7471  const Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
7472  const Scalar ONE = Teuchos::ScalarTraits<Scalar>::one();
7473 
7474  // If the user didn't supply a domain or range Map, then try to
7475  // get one from B first (if it has them), then from A (if it has
7476  // them). If we don't have any domain or range Maps, scold the
7477  // user.
7478  RCP<const map_type> A_domainMap = A.getDomainMap ();
7479  RCP<const map_type> A_rangeMap = A.getRangeMap ();
7480  RCP<const map_type> B_domainMap = B.getDomainMap ();
7481  RCP<const map_type> B_rangeMap = B.getRangeMap ();
7482 
7483  RCP<const map_type> theDomainMap = domainMap;
7484  RCP<const map_type> theRangeMap = rangeMap;
7485 
7486  if (domainMap.is_null ()) {
7487  if (B_domainMap.is_null ()) {
7488  TEUCHOS_TEST_FOR_EXCEPTION(
7489  A_domainMap.is_null (), std::invalid_argument,
7490  "Tpetra::CrsMatrix::add: If neither A nor B have a domain Map, "
7491  "then you must supply a nonnull domain Map to this method.");
7492  theDomainMap = A_domainMap;
7493  } else {
7494  theDomainMap = B_domainMap;
7495  }
7496  }
7497  if (rangeMap.is_null ()) {
7498  if (B_rangeMap.is_null ()) {
7499  TEUCHOS_TEST_FOR_EXCEPTION(
7500  A_rangeMap.is_null (), std::invalid_argument,
7501  "Tpetra::CrsMatrix::add: If neither A nor B have a range Map, "
7502  "then you must supply a nonnull range Map to this method.");
7503  theRangeMap = A_rangeMap;
7504  } else {
7505  theRangeMap = B_rangeMap;
7506  }
7507  }
7508 
7509  if (debug) {
7510  // In debug mode, check that A and B have matching domain and
7511  // range Maps, if they have domain and range Maps at all. (If
7512  // they aren't fill complete, then they may not yet have them.)
7513  if (! A_domainMap.is_null() && ! A_rangeMap.is_null()) {
7514  if (! B_domainMap.is_null() && ! B_rangeMap.is_null()) {
7515  TEUCHOS_TEST_FOR_EXCEPTION
7516  (! B_domainMap->isSameAs(*A_domainMap),
7517  std::invalid_argument,
7518  errPfx << "The input RowMatrix A must have a domain Map "
7519  "which is the same as (isSameAs) this RowMatrix's "
7520  "domain Map.");
7521  TEUCHOS_TEST_FOR_EXCEPTION
7522  (! B_rangeMap->isSameAs(*A_rangeMap), std::invalid_argument,
7523  errPfx << "The input RowMatrix A must have a range Map "
7524  "which is the same as (isSameAs) this RowMatrix's range "
7525  "Map.");
7526  TEUCHOS_TEST_FOR_EXCEPTION
7527  (! domainMap.is_null() &&
7528  ! domainMap->isSameAs(*B_domainMap),
7529  std::invalid_argument,
7530  errPfx << "The input domain Map must be the same as "
7531  "(isSameAs) this RowMatrix's domain Map.");
7532  TEUCHOS_TEST_FOR_EXCEPTION
7533  (! rangeMap.is_null() &&
7534  ! rangeMap->isSameAs(*B_rangeMap),
7535  std::invalid_argument,
7536  errPfx << "The input range Map must be the same as "
7537  "(isSameAs) this RowMatrix's range Map.");
7538  }
7539  }
7540  else if (! B_domainMap.is_null() && ! B_rangeMap.is_null()) {
7541  TEUCHOS_TEST_FOR_EXCEPTION
7542  (! domainMap.is_null() &&
7543  ! domainMap->isSameAs(*B_domainMap),
7544  std::invalid_argument,
7545  errPfx << "The input domain Map must be the same as "
7546  "(isSameAs) this RowMatrix's domain Map.");
7547  TEUCHOS_TEST_FOR_EXCEPTION
7548  (! rangeMap.is_null() && ! rangeMap->isSameAs(*B_rangeMap),
7549  std::invalid_argument,
7550  errPfx << "The input range Map must be the same as "
7551  "(isSameAs) this RowMatrix's range Map.");
7552  }
7553  else {
7554  TEUCHOS_TEST_FOR_EXCEPTION
7555  (domainMap.is_null() || rangeMap.is_null(),
7556  std::invalid_argument, errPfx << "If neither A nor B "
7557  "have a domain and range Map, then you must supply a "
7558  "nonnull domain and range Map to this method.");
7559  }
7560  }
7561 
7562  // What parameters do we pass to C's constructor? Do we call
7563  // fillComplete on C after filling it? And if so, what parameters
7564  // do we pass to C's fillComplete call?
7565  bool callFillComplete = true;
7566  RCP<ParameterList> constructorSublist;
7567  RCP<ParameterList> fillCompleteSublist;
7568  if (! params.is_null()) {
7569  callFillComplete =
7570  params->get("Call fillComplete", callFillComplete);
7571  constructorSublist = sublist(params, "Constructor parameters");
7572  fillCompleteSublist = sublist(params, "fillComplete parameters");
7573  }
7574 
7575  RCP<const map_type> A_rowMap = A.getRowMap ();
7576  RCP<const map_type> B_rowMap = B.getRowMap ();
7577  RCP<const map_type> C_rowMap = B_rowMap; // see discussion in documentation
7578  RCP<crs_matrix_type> C; // The result matrix.
7579 
7580  // If A and B's row Maps are the same, we can compute an upper
7581  // bound on the number of entries in each row of C, before
7582  // actually computing the sum. A reasonable upper bound is the
7583  // sum of the two entry counts in each row.
7584  if (A_rowMap->isSameAs (*B_rowMap)) {
7585  const LO localNumRows = static_cast<LO> (A_rowMap->getLocalNumElements ());
7586  Array<size_t> C_maxNumEntriesPerRow (localNumRows, 0);
7587 
7588  // Get the number of entries in each row of A.
7589  if (alpha != ZERO) {
7590  for (LO localRow = 0; localRow < localNumRows; ++localRow) {
7591  const size_t A_numEntries = A.getNumEntriesInLocalRow (localRow);
7592  C_maxNumEntriesPerRow[localRow] += A_numEntries;
7593  }
7594  }
7595  // Get the number of entries in each row of B.
7596  if (beta != ZERO) {
7597  for (LO localRow = 0; localRow < localNumRows; ++localRow) {
7598  const size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
7599  C_maxNumEntriesPerRow[localRow] += B_numEntries;
7600  }
7601  }
7602  // Construct the result matrix C.
7603  if (constructorSublist.is_null ()) {
7604  C = rcp (new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow ()));
7605  } else {
7606  C = rcp (new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow (),
7607  constructorSublist));
7608  }
7609  // Since A and B have the same row Maps, we could add them
7610  // together all at once and merge values before we call
7611  // insertGlobalValues. However, we don't really need to, since
7612  // we've already allocated enough space in each row of C for C
7613  // to do the merge itself.
7614  }
7615  else { // the row Maps of A and B are not the same
7616  // Construct the result matrix C.
7617  // true: !A_rowMap->isSameAs (*B_rowMap)
7618  TEUCHOS_TEST_FOR_EXCEPTION
7619  (true, std::invalid_argument, errPfx << "The row maps must "
7620  "be the same for statically allocated matrices, to ensure "
7621  "that there is sufficient space to do the addition.");
7622  }
7623 
7624  TEUCHOS_TEST_FOR_EXCEPTION
7625  (C.is_null (), std::logic_error,
7626  errPfx << "C should not be null at this point. "
7627  "Please report this bug to the Tpetra developers.");
7628 
7629  if (verbose) {
7630  std::ostringstream os;
7631  os << *prefix << "Compute C = alpha*A + beta*B" << endl;
7632  std::cerr << os.str ();
7633  }
7634  using gids_type = nonconst_global_inds_host_view_type;
7635  using vals_type = nonconst_values_host_view_type;
7636  gids_type ind;
7637  vals_type val;
7638 
7639  if (alpha != ZERO) {
7640  const LO A_localNumRows = static_cast<LO> (A_rowMap->getLocalNumElements ());
7641  for (LO localRow = 0; localRow < A_localNumRows; ++localRow) {
7642  size_t A_numEntries = A.getNumEntriesInLocalRow (localRow);
7643  const GO globalRow = A_rowMap->getGlobalElement (localRow);
7644  if (A_numEntries > static_cast<size_t> (ind.size ())) {
7645  Kokkos::resize(ind,A_numEntries);
7646  Kokkos::resize(val,A_numEntries);
7647  }
7648  gids_type indView = Kokkos::subview(ind,std::make_pair((size_t)0, A_numEntries));
7649  vals_type valView = Kokkos::subview(val,std::make_pair((size_t)0, A_numEntries));
7650  A.getGlobalRowCopy (globalRow, indView, valView, A_numEntries);
7651 
7652  if (alpha != ONE) {
7653  for (size_t k = 0; k < A_numEntries; ++k) {
7654  valView[k] *= alpha;
7655  }
7656  }
7657  C->insertGlobalValues (globalRow, A_numEntries,
7658  reinterpret_cast<Scalar *>(valView.data()),
7659  indView.data());
7660  }
7661  }
7662 
7663  if (beta != ZERO) {
7664  const LO B_localNumRows = static_cast<LO> (B_rowMap->getLocalNumElements ());
7665  for (LO localRow = 0; localRow < B_localNumRows; ++localRow) {
7666  size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
7667  const GO globalRow = B_rowMap->getGlobalElement (localRow);
7668  if (B_numEntries > static_cast<size_t> (ind.size ())) {
7669  Kokkos::resize(ind,B_numEntries);
7670  Kokkos::resize(val,B_numEntries);
7671  }
7672  gids_type indView = Kokkos::subview(ind,std::make_pair((size_t)0, B_numEntries));
7673  vals_type valView = Kokkos::subview(val,std::make_pair((size_t)0, B_numEntries));
7674  B.getGlobalRowCopy (globalRow, indView, valView, B_numEntries);
7675 
7676  if (beta != ONE) {
7677  for (size_t k = 0; k < B_numEntries; ++k) {
7678  valView[k] *= beta;
7679  }
7680  }
7681  C->insertGlobalValues (globalRow, B_numEntries,
7682  reinterpret_cast<Scalar *>(valView.data()),
7683  indView.data());
7684  }
7685  }
7686 
7687  if (callFillComplete) {
7688  if (verbose) {
7689  std::ostringstream os;
7690  os << *prefix << "Call fillComplete on C" << endl;
7691  std::cerr << os.str ();
7692  }
7693  if (fillCompleteSublist.is_null ()) {
7694  C->fillComplete (theDomainMap, theRangeMap);
7695  } else {
7696  C->fillComplete (theDomainMap, theRangeMap, fillCompleteSublist);
7697  }
7698  }
7699  else if (verbose) {
7700  std::ostringstream os;
7701  os << *prefix << "Do NOT call fillComplete on C" << endl;
7702  std::cerr << os.str ();
7703  }
7704 
7705  if (verbose) {
7706  std::ostringstream os;
7707  os << *prefix << "Done" << endl;
7708  std::cerr << os.str ();
7709  }
7710  return rcp_implicit_cast<row_matrix_type> (C);
7711  }
7712 
7713 
7714 
7715  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
7716  void
7719  const ::Tpetra::Details::Transfer<LocalOrdinal, GlobalOrdinal, Node>& rowTransfer,
7720  const Teuchos::RCP<const ::Tpetra::Details::Transfer<LocalOrdinal, GlobalOrdinal, Node> > & domainTransfer,
7721  const Teuchos::RCP<const map_type>& domainMap,
7722  const Teuchos::RCP<const map_type>& rangeMap,
7723  const Teuchos::RCP<Teuchos::ParameterList>& params) const
7724  {
7725  using Details::Behavior;
7730  using Teuchos::ArrayRCP;
7731  using Teuchos::ArrayView;
7732  using Teuchos::Comm;
7733  using Teuchos::ParameterList;
7734  using Teuchos::RCP;
7735  using std::endl;
7736  typedef LocalOrdinal LO;
7737  typedef GlobalOrdinal GO;
7738  typedef node_type NT;
7739  typedef CrsMatrix<Scalar, LO, GO, NT> this_CRS_type;
7740  typedef Vector<int, LO, GO, NT> IntVectorType;
7741  using Teuchos::as;
7742 
7743  const bool debug = Behavior::debug("CrsMatrix");
7744  const bool verbose = Behavior::verbose("CrsMatrix");
7745  int MyPID = getComm ()->getRank ();
7746 
7747  std::unique_ptr<std::string> verbosePrefix;
7748  if (verbose) {
7749  verbosePrefix =
7750  this->createPrefix("CrsMatrix", "transferAndFillComplete");
7751  std::ostringstream os;
7752  os << "Start" << endl;
7753  std::cerr << os.str();
7754  }
7755 
7756  //
7757  // Get the caller's parameters
7758  //
7759  bool isMM = false; // optimize for matrix-matrix ops.
7760  bool reverseMode = false; // Are we in reverse mode?
7761  bool restrictComm = false; // Do we need to restrict the communicator?
7762 
7763  int mm_optimization_core_count =
7764  Behavior::TAFC_OptimizationCoreCount();
7765  RCP<ParameterList> matrixparams; // parameters for the destination matrix
7766  bool overrideAllreduce = false;
7767  bool useKokkosPath = false;
7768  if (! params.is_null ()) {
7769  matrixparams = sublist (params, "CrsMatrix");
7770  reverseMode = params->get ("Reverse Mode", reverseMode);
7771  useKokkosPath = params->get ("TAFC: use kokkos path", useKokkosPath);
7772  restrictComm = params->get ("Restrict Communicator", restrictComm);
7773  auto & slist = params->sublist("matrixmatrix: kernel params",false);
7774  isMM = slist.get("isMatrixMatrix_TransferAndFillComplete",false);
7775  mm_optimization_core_count = slist.get("MM_TAFC_OptimizationCoreCount",mm_optimization_core_count);
7776 
7777  overrideAllreduce = slist.get("MM_TAFC_OverrideAllreduceCheck",false);
7778  if(getComm()->getSize() < mm_optimization_core_count && isMM) isMM = false;
7779  if(reverseMode) isMM = false;
7780  }
7781 
7782  // Only used in the sparse matrix-matrix multiply (isMM) case.
7783  std::shared_ptr< ::Tpetra::Details::CommRequest> iallreduceRequest;
7784  int mismatch = 0;
7785  int reduced_mismatch = 0;
7786  if (isMM && !overrideAllreduce) {
7787 
7788  // Test for pathological matrix transfer
7789  const bool source_vals = ! getGraph ()->getImporter ().is_null();
7790  const bool target_vals = ! (rowTransfer.getExportLIDs ().size() == 0 ||
7791  rowTransfer.getRemoteLIDs ().size() == 0);
7792  mismatch = (source_vals != target_vals) ? 1 : 0;
7793  iallreduceRequest =
7794  ::Tpetra::Details::iallreduce (mismatch, reduced_mismatch,
7795  Teuchos::REDUCE_MAX, * (getComm ()));
7796  }
7797 
7798 #ifdef HAVE_TPETRA_MMM_TIMINGS
7799  using Teuchos::TimeMonitor;
7800  std::string label;
7801  if(!params.is_null())
7802  label = params->get("Timer Label",label);
7803  std::string prefix = std::string("Tpetra ")+ label + std::string(": ");
7804  std::string tlstr;
7805  {
7806  std::ostringstream os;
7807  if(isMM) os<<":MMOpt";
7808  else os<<":MMLegacy";
7809  tlstr = os.str();
7810  }
7811 
7812  Teuchos::TimeMonitor MMall(*TimeMonitor::getNewTimer(prefix + std::string("TAFC All") +tlstr ));
7813 #endif
7814 
7815  // Make sure that the input argument rowTransfer is either an
7816  // Import or an Export. Import and Export are the only two
7817  // subclasses of Transfer that we defined, but users might
7818  // (unwisely, for now at least) decide to implement their own
7819  // subclasses. Exclude this possibility.
7820  const import_type* xferAsImport = dynamic_cast<const import_type*> (&rowTransfer);
7821  const export_type* xferAsExport = dynamic_cast<const export_type*> (&rowTransfer);
7822  TEUCHOS_TEST_FOR_EXCEPTION(
7823  xferAsImport == nullptr && xferAsExport == nullptr, std::invalid_argument,
7824  "Tpetra::CrsMatrix::transferAndFillComplete: The 'rowTransfer' input "
7825  "argument must be either an Import or an Export, and its template "
7826  "parameters must match the corresponding template parameters of the "
7827  "CrsMatrix.");
7828 
7829  // Make sure that the input argument domainTransfer is either an
7830  // Import or an Export. Import and Export are the only two
7831  // subclasses of Transfer that we defined, but users might
7832  // (unwisely, for now at least) decide to implement their own
7833  // subclasses. Exclude this possibility.
7834  Teuchos::RCP<const import_type> xferDomainAsImport = Teuchos::rcp_dynamic_cast<const import_type> (domainTransfer);
7835  Teuchos::RCP<const export_type> xferDomainAsExport = Teuchos::rcp_dynamic_cast<const export_type> (domainTransfer);
7836 
7837  if(! domainTransfer.is_null()) {
7838  TEUCHOS_TEST_FOR_EXCEPTION(
7839  (xferDomainAsImport.is_null() && xferDomainAsExport.is_null()), std::invalid_argument,
7840  "Tpetra::CrsMatrix::transferAndFillComplete: The 'domainTransfer' input "
7841  "argument must be either an Import or an Export, and its template "
7842  "parameters must match the corresponding template parameters of the "
7843  "CrsMatrix.");
7844 
7845  TEUCHOS_TEST_FOR_EXCEPTION(
7846  ( xferAsImport != nullptr || ! xferDomainAsImport.is_null() ) &&
7847  (( xferAsImport != nullptr && xferDomainAsImport.is_null() ) ||
7848  ( xferAsImport == nullptr && ! xferDomainAsImport.is_null() )), std::invalid_argument,
7849  "Tpetra::CrsMatrix::transferAndFillComplete: The 'rowTransfer' and 'domainTransfer' input "
7850  "arguments must be of the same type (either Import or Export).");
7851 
7852  TEUCHOS_TEST_FOR_EXCEPTION(
7853  ( xferAsExport != nullptr || ! xferDomainAsExport.is_null() ) &&
7854  (( xferAsExport != nullptr && xferDomainAsExport.is_null() ) ||
7855  ( xferAsExport == nullptr && ! xferDomainAsExport.is_null() )), std::invalid_argument,
7856  "Tpetra::CrsMatrix::transferAndFillComplete: The 'rowTransfer' and 'domainTransfer' input "
7857  "arguments must be of the same type (either Import or Export).");
7858  } // domainTransfer != null
7859 
7860 
7861  // FIXME (mfh 15 May 2014) Wouldn't communication still be needed,
7862  // if the source Map is not distributed but the target Map is?
7863  const bool communication_needed = rowTransfer.getSourceMap ()->isDistributed ();
7864 
7865  // Get the new domain and range Maps. We need some of them for
7866  // error checking, now that we have the reverseMode parameter.
7867  RCP<const map_type> MyRowMap = reverseMode ?
7868  rowTransfer.getSourceMap () : rowTransfer.getTargetMap ();
7869  RCP<const map_type> MyColMap; // create this below
7870  RCP<const map_type> MyDomainMap = ! domainMap.is_null () ?
7871  domainMap : getDomainMap ();
7872  RCP<const map_type> MyRangeMap = ! rangeMap.is_null () ?
7873  rangeMap : getRangeMap ();
7874  RCP<const map_type> BaseRowMap = MyRowMap;
7875  RCP<const map_type> BaseDomainMap = MyDomainMap;
7876 
7877  // If the user gave us a nonnull destMat, then check whether it's
7878  // "pristine." That means that it has no entries.
7879  //
7880  // FIXME (mfh 15 May 2014) If this is not true on all processes,
7881  // then this exception test may hang. It would be better to
7882  // forward an error flag to the next communication phase.
7883  if (! destMat.is_null ()) {
7884  // FIXME (mfh 15 May 2014): The Epetra idiom for checking
7885  // whether a graph or matrix has no entries on the calling
7886  // process, is that it is neither locally nor globally indexed.
7887  // This may change eventually with the Kokkos refactor version
7888  // of Tpetra, so it would be better just to check the quantity
7889  // of interest directly. Note that with the Kokkos refactor
7890  // version of Tpetra, asking for the total number of entries in
7891  // a graph or matrix that is not fill complete might require
7892  // computation (kernel launch), since it is not thread scalable
7893  // to update a count every time an entry is inserted.
7894  const bool NewFlag = ! destMat->getGraph ()->isLocallyIndexed () &&
7895  ! destMat->getGraph ()->isGloballyIndexed ();
7896  TEUCHOS_TEST_FOR_EXCEPTION(
7897  ! NewFlag, std::invalid_argument, "Tpetra::CrsMatrix::"
7898  "transferAndFillComplete: The input argument 'destMat' is only allowed "
7899  "to be nonnull, if its graph is empty (neither locally nor globally "
7900  "indexed).");
7901  // FIXME (mfh 15 May 2014) At some point, we want to change
7902  // graphs and matrices so that their DistObject Map
7903  // (this->getMap()) may differ from their row Map. This will
7904  // make redistribution for 2-D distributions more efficient. I
7905  // hesitate to change this check, because I'm not sure how much
7906  // the code here depends on getMap() and getRowMap() being the
7907  // same.
7908  TEUCHOS_TEST_FOR_EXCEPTION(
7909  ! destMat->getRowMap ()->isSameAs (*MyRowMap), std::invalid_argument,
7910  "Tpetra::CrsMatrix::transferAndFillComplete: The (row) Map of the "
7911  "input argument 'destMat' is not the same as the (row) Map specified "
7912  "by the input argument 'rowTransfer'.");
7913  TEUCHOS_TEST_FOR_EXCEPTION(
7914  ! destMat->checkSizes (*this), std::invalid_argument,
7915  "Tpetra::CrsMatrix::transferAndFillComplete: You provided a nonnull "
7916  "destination matrix, but checkSizes() indicates that it is not a legal "
7917  "legal target for redistribution from the source matrix (*this). This "
7918  "may mean that they do not have the same dimensions.");
7919  }
7920 
7921  // If forward mode (the default), then *this's (row) Map must be
7922  // the same as the source Map of the Transfer. If reverse mode,
7923  // then *this's (row) Map must be the same as the target Map of
7924  // the Transfer.
7925  //
7926  // FIXME (mfh 15 May 2014) At some point, we want to change graphs
7927  // and matrices so that their DistObject Map (this->getMap()) may
7928  // differ from their row Map. This will make redistribution for
7929  // 2-D distributions more efficient. I hesitate to change this
7930  // check, because I'm not sure how much the code here depends on
7931  // getMap() and getRowMap() being the same.
7932  TEUCHOS_TEST_FOR_EXCEPTION(
7933  ! (reverseMode || getRowMap ()->isSameAs (*rowTransfer.getSourceMap ())),
7934  std::invalid_argument, "Tpetra::CrsMatrix::transferAndFillComplete: "
7935  "rowTransfer->getSourceMap() must match this->getRowMap() in forward mode.");
7936  TEUCHOS_TEST_FOR_EXCEPTION(
7937  ! (! reverseMode || getRowMap ()->isSameAs (*rowTransfer.getTargetMap ())),
7938  std::invalid_argument, "Tpetra::CrsMatrix::transferAndFillComplete: "
7939  "rowTransfer->getTargetMap() must match this->getRowMap() in reverse mode.");
7940 
7941  // checks for domainTransfer
7942  TEUCHOS_TEST_FOR_EXCEPTION(
7943  ! xferDomainAsImport.is_null() && ! xferDomainAsImport->getTargetMap()->isSameAs(*domainMap),
7944  std::invalid_argument,
7945  "Tpetra::CrsMatrix::transferAndFillComplete: The target map of the 'domainTransfer' input "
7946  "argument must be the same as the rebalanced domain map 'domainMap'");
7947 
7948  TEUCHOS_TEST_FOR_EXCEPTION(
7949  ! xferDomainAsExport.is_null() && ! xferDomainAsExport->getSourceMap()->isSameAs(*domainMap),
7950  std::invalid_argument,
7951  "Tpetra::CrsMatrix::transferAndFillComplete: The source map of the 'domainTransfer' input "
7952  "argument must be the same as the rebalanced domain map 'domainMap'");
7953 
7954  // The basic algorithm here is:
7955  //
7956  // 1. Call the moral equivalent of "Distor.do" to handle the import.
7957  // 2. Copy all the Imported and Copy/Permuted data into the raw
7958  // CrsMatrix / CrsGraphData pointers, still using GIDs.
7959  // 3. Call an optimized version of MakeColMap that avoids the
7960  // Directory lookups (since the importer knows who owns all the
7961  // GIDs) AND reindexes to LIDs.
7962  // 4. Call expertStaticFillComplete()
7963 
7964  // Get information from the Importer
7965  const size_t NumSameIDs = rowTransfer.getNumSameIDs();
7966  ArrayView<const LO> ExportLIDs = reverseMode ?
7967  rowTransfer.getRemoteLIDs () : rowTransfer.getExportLIDs ();
7968  auto RemoteLIDs = reverseMode ?
7969  rowTransfer.getExportLIDs_dv() : rowTransfer.getRemoteLIDs_dv();
7970  auto PermuteToLIDs = reverseMode ?
7971  rowTransfer.getPermuteFromLIDs_dv() : rowTransfer.getPermuteToLIDs_dv();
7972  auto PermuteFromLIDs = reverseMode ?
7973  rowTransfer.getPermuteToLIDs_dv() : rowTransfer.getPermuteFromLIDs_dv();
7974  Distributor& Distor = rowTransfer.getDistributor ();
7975 
7976  // Owning PIDs
7977  Teuchos::Array<int> SourcePids;
7978 
7979  // Temp variables for sub-communicators
7980  RCP<const map_type> ReducedRowMap, ReducedColMap,
7981  ReducedDomainMap, ReducedRangeMap;
7982  RCP<const Comm<int> > ReducedComm;
7983 
7984  // If the user gave us a null destMat, then construct the new
7985  // destination matrix. We will replace its column Map later.
7986  if (destMat.is_null ()) {
7987  destMat = rcp (new this_CRS_type (MyRowMap, 0, matrixparams));
7988  }
7989 
7990  /***************************************************/
7991  /***** 1) First communicator restriction phase ****/
7992  /***************************************************/
7993  if (restrictComm) {
7994 #ifdef HAVE_TPETRA_MMM_TIMINGS
7995  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC restrictComm")));
7996 #endif
7997  ReducedRowMap = MyRowMap->removeEmptyProcesses ();
7998  ReducedComm = ReducedRowMap.is_null () ?
7999  Teuchos::null :
8000  ReducedRowMap->getComm ();
8001  destMat->removeEmptyProcessesInPlace (ReducedRowMap);
8002 
8003  ReducedDomainMap = MyRowMap.getRawPtr () == MyDomainMap.getRawPtr () ?
8004  ReducedRowMap :
8005  MyDomainMap->replaceCommWithSubset (ReducedComm);
8006  ReducedRangeMap = MyRowMap.getRawPtr () == MyRangeMap.getRawPtr () ?
8007  ReducedRowMap :
8008  MyRangeMap->replaceCommWithSubset (ReducedComm);
8009 
8010  // Reset the "my" maps
8011  MyRowMap = ReducedRowMap;
8012  MyDomainMap = ReducedDomainMap;
8013  MyRangeMap = ReducedRangeMap;
8014 
8015  // Update my PID, if we've restricted the communicator
8016  if (! ReducedComm.is_null ()) {
8017  MyPID = ReducedComm->getRank ();
8018  }
8019  else {
8020  MyPID = -2; // For debugging
8021  }
8022  }
8023  else {
8024  ReducedComm = MyRowMap->getComm ();
8025  }
8026 
8027 
8028 
8029  /***************************************************/
8030  /***** 2) From Tpetra::DistObject::doTransfer() ****/
8031  /***************************************************/
8032  // Get the owning PIDs
8033  RCP<const import_type> MyImporter = getGraph ()->getImporter ();
8034 
8035  // check whether domain maps of source matrix and base domain map is the same
8036  bool bSameDomainMap = BaseDomainMap->isSameAs (*getDomainMap ());
8037 
8038  if (! restrictComm && ! MyImporter.is_null () && bSameDomainMap ) {
8039 #ifdef HAVE_TPETRA_MMM_TIMINGS
8040  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs same map")));
8041 #endif
8042  // Same domain map as source matrix
8043  //
8044  // NOTE: This won't work for restrictComm (because the Import
8045  // doesn't know the restricted PIDs), though writing an
8046  // optimized version for that case would be easy (Import an
8047  // IntVector of the new PIDs). Might want to add this later.
8048  Import_Util::getPids (*MyImporter, SourcePids, false);
8049  }
8050  else if (restrictComm && ! MyImporter.is_null () && bSameDomainMap) {
8051  // Same domain map as source matrix (restricted communicator)
8052  // We need one import from the domain to the column map
8053 #ifdef HAVE_TPETRA_MMM_TIMINGS
8054  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs restricted comm")));
8055 #endif
8056  IntVectorType SourceDomain_pids(getDomainMap (),true);
8057  IntVectorType SourceCol_pids(getColMap());
8058  // SourceDomain_pids contains the restricted pids
8059  SourceDomain_pids.putScalar(MyPID);
8060 
8061  SourceCol_pids.doImport (SourceDomain_pids, *MyImporter, INSERT);
8062  SourcePids.resize (getColMap ()->getLocalNumElements ());
8063  SourceCol_pids.get1dCopy (SourcePids ());
8064  }
8065  else if (MyImporter.is_null ()) {
8066  // Matrix has no off-process entries
8067 #ifdef HAVE_TPETRA_MMM_TIMINGS
8068  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs all local entries")));
8069 #endif
8070  SourcePids.resize (getColMap ()->getLocalNumElements ());
8071  SourcePids.assign (getColMap ()->getLocalNumElements (), MyPID);
8072  }
8073  else if ( ! MyImporter.is_null () &&
8074  ! domainTransfer.is_null () ) {
8075  // general implementation for rectangular matrices with
8076  // domain map different than SourceMatrix domain map.
8077  // User has to provide a DomainTransfer object. We need
8078  // to communications (import/export)
8079 #ifdef HAVE_TPETRA_MMM_TIMINGS
8080  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs rectangular case")));
8081 #endif
8082 
8083  // TargetDomain_pids lives on the rebalanced new domain map
8084  IntVectorType TargetDomain_pids (domainMap);
8085  TargetDomain_pids.putScalar (MyPID);
8086 
8087  // SourceDomain_pids lives on the non-rebalanced old domain map
8088  IntVectorType SourceDomain_pids (getDomainMap ());
8089 
8090  // SourceCol_pids lives on the non-rebalanced old column map
8091  IntVectorType SourceCol_pids (getColMap ());
8092 
8093  if (! reverseMode && ! xferDomainAsImport.is_null() ) {
8094  SourceDomain_pids.doExport (TargetDomain_pids, *xferDomainAsImport, INSERT);
8095  }
8096  else if (reverseMode && ! xferDomainAsExport.is_null() ) {
8097  SourceDomain_pids.doExport (TargetDomain_pids, *xferDomainAsExport, INSERT);
8098  }
8099  else if (! reverseMode && ! xferDomainAsExport.is_null() ) {
8100  SourceDomain_pids.doImport (TargetDomain_pids, *xferDomainAsExport, INSERT);
8101  }
8102  else if (reverseMode && ! xferDomainAsImport.is_null() ) {
8103  SourceDomain_pids.doImport (TargetDomain_pids, *xferDomainAsImport, INSERT);
8104  }
8105  else {
8106  TEUCHOS_TEST_FOR_EXCEPTION(
8107  true, std::logic_error, "Tpetra::CrsMatrix::"
8108  "transferAndFillComplete: Should never get here! "
8109  "Please report this bug to a Tpetra developer.");
8110  }
8111  SourceCol_pids.doImport (SourceDomain_pids, *MyImporter, INSERT);
8112  SourcePids.resize (getColMap ()->getLocalNumElements ());
8113  SourceCol_pids.get1dCopy (SourcePids ());
8114  }
8115  else if ( ! MyImporter.is_null () &&
8116  BaseDomainMap->isSameAs (*BaseRowMap) &&
8117  getDomainMap ()->isSameAs (*getRowMap ())) {
8118  // We can use the rowTransfer + SourceMatrix's Import to find out who owns what.
8119 #ifdef HAVE_TPETRA_MMM_TIMINGS
8120  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs query import")));
8121 #endif
8122 
8123  IntVectorType TargetRow_pids (domainMap);
8124  IntVectorType SourceRow_pids (getRowMap ());
8125  IntVectorType SourceCol_pids (getColMap ());
8126 
8127  TargetRow_pids.putScalar (MyPID);
8128  if (! reverseMode && xferAsImport != nullptr) {
8129  SourceRow_pids.doExport (TargetRow_pids, *xferAsImport, INSERT);
8130  }
8131  else if (reverseMode && xferAsExport != nullptr) {
8132  SourceRow_pids.doExport (TargetRow_pids, *xferAsExport, INSERT);
8133  }
8134  else if (! reverseMode && xferAsExport != nullptr) {
8135  SourceRow_pids.doImport (TargetRow_pids, *xferAsExport, INSERT);
8136  }
8137  else if (reverseMode && xferAsImport != nullptr) {
8138  SourceRow_pids.doImport (TargetRow_pids, *xferAsImport, INSERT);
8139  }
8140  else {
8141  TEUCHOS_TEST_FOR_EXCEPTION(
8142  true, std::logic_error, "Tpetra::CrsMatrix::"
8143  "transferAndFillComplete: Should never get here! "
8144  "Please report this bug to a Tpetra developer.");
8145  }
8146 
8147  SourceCol_pids.doImport (SourceRow_pids, *MyImporter, INSERT);
8148  SourcePids.resize (getColMap ()->getLocalNumElements ());
8149  SourceCol_pids.get1dCopy (SourcePids ());
8150  }
8151  else {
8152  TEUCHOS_TEST_FOR_EXCEPTION(
8153  true, std::invalid_argument, "Tpetra::CrsMatrix::"
8154  "transferAndFillComplete: This method only allows either domainMap == "
8155  "getDomainMap (), or (domainMap == rowTransfer.getTargetMap () and "
8156  "getDomainMap () == getRowMap ()).");
8157  }
8158 
8159  // Tpetra-specific stuff
8160  size_t constantNumPackets = destMat->constantNumberOfPackets ();
8161  {
8162 #ifdef HAVE_TPETRA_MMM_TIMINGS
8163  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC reallocate buffers")));
8164 #endif
8165  if (constantNumPackets == 0) {
8166  destMat->reallocArraysForNumPacketsPerLid (ExportLIDs.size (),
8167  RemoteLIDs.view_host().size ());
8168  }
8169  else {
8170  // There are a constant number of packets per element. We
8171  // already know (from the number of "remote" (incoming)
8172  // elements) how many incoming elements we expect, so we can
8173  // resize the buffer accordingly.
8174  const size_t rbufLen = RemoteLIDs.view_host().size() * constantNumPackets;
8175  destMat->reallocImportsIfNeeded (rbufLen, false, nullptr);
8176  }
8177  }
8178 
8179  // Pack & Prepare w/ owning PIDs
8180  {
8181 #ifdef HAVE_TPETRA_MMM_TIMINGS
8182  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC pack and prepare")));
8183 #endif
8184  if (debug) {
8185  using Teuchos::outArg;
8186  using Teuchos::REDUCE_MAX;
8187  using Teuchos::reduceAll;
8188  using std::cerr;
8189  using std::endl;
8190  RCP<const Teuchos::Comm<int> > comm = this->getComm ();
8191  const int myRank = comm->getRank ();
8192 
8193  std::ostringstream errStrm;
8194  int lclErr = 0;
8195  int gblErr = 0;
8196 
8197  Teuchos::ArrayView<size_t> numExportPacketsPerLID;
8198  try {
8199  // packAndPrepare* methods modify numExportPacketsPerLID_.
8200  destMat->numExportPacketsPerLID_.modify_host ();
8201  numExportPacketsPerLID =
8202  getArrayViewFromDualView (destMat->numExportPacketsPerLID_);
8203  }
8204  catch (std::exception& e) {
8205  errStrm << "Proc " << myRank << ": getArrayViewFromDualView threw: "
8206  << e.what () << std::endl;
8207  lclErr = 1;
8208  }
8209  catch (...) {
8210  errStrm << "Proc " << myRank << ": getArrayViewFromDualView threw "
8211  "an exception not a subclass of std::exception" << std::endl;
8212  lclErr = 1;
8213  }
8214 
8215  if (! comm.is_null ()) {
8216  reduceAll<int, int> (*comm, REDUCE_MAX, lclErr, outArg (gblErr));
8217  }
8218  if (gblErr != 0) {
8219  ::Tpetra::Details::gathervPrint (cerr, errStrm.str (), *comm);
8220  TEUCHOS_TEST_FOR_EXCEPTION(
8221  true, std::runtime_error, "getArrayViewFromDualView threw an "
8222  "exception on at least one process.");
8223  }
8224 
8225  if (verbose) {
8226  std::ostringstream os;
8227  os << *verbosePrefix << "Calling packCrsMatrixWithOwningPIDs"
8228  << std::endl;
8229  std::cerr << os.str ();
8230  }
8231  try {
8233  destMat->exports_,
8234  numExportPacketsPerLID,
8235  ExportLIDs,
8236  SourcePids,
8237  constantNumPackets);
8238  }
8239  catch (std::exception& e) {
8240  errStrm << "Proc " << myRank << ": packCrsMatrixWithOwningPIDs threw: "
8241  << e.what () << std::endl;
8242  lclErr = 1;
8243  }
8244  catch (...) {
8245  errStrm << "Proc " << myRank << ": packCrsMatrixWithOwningPIDs threw "
8246  "an exception not a subclass of std::exception" << std::endl;
8247  lclErr = 1;
8248  }
8249 
8250  if (verbose) {
8251  std::ostringstream os;
8252  os << *verbosePrefix << "Done with packCrsMatrixWithOwningPIDs"
8253  << std::endl;
8254  std::cerr << os.str ();
8255  }
8256 
8257  if (! comm.is_null ()) {
8258  reduceAll<int, int> (*comm, REDUCE_MAX, lclErr, outArg (gblErr));
8259  }
8260  if (gblErr != 0) {
8261  ::Tpetra::Details::gathervPrint (cerr, errStrm.str (), *comm);
8262  TEUCHOS_TEST_FOR_EXCEPTION(
8263  true, std::runtime_error, "packCrsMatrixWithOwningPIDs threw an "
8264  "exception on at least one process.");
8265  }
8266  }
8267  else {
8268  // packAndPrepare* methods modify numExportPacketsPerLID_.
8269  destMat->numExportPacketsPerLID_.modify_host ();
8270  Teuchos::ArrayView<size_t> numExportPacketsPerLID =
8271  getArrayViewFromDualView (destMat->numExportPacketsPerLID_);
8272  if (verbose) {
8273  std::ostringstream os;
8274  os << *verbosePrefix << "Calling packCrsMatrixWithOwningPIDs"
8275  << std::endl;
8276  std::cerr << os.str ();
8277  }
8279  destMat->exports_,
8280  numExportPacketsPerLID,
8281  ExportLIDs,
8282  SourcePids,
8283  constantNumPackets);
8284  if (verbose) {
8285  std::ostringstream os;
8286  os << *verbosePrefix << "Done with packCrsMatrixWithOwningPIDs"
8287  << std::endl;
8288  std::cerr << os.str ();
8289  }
8290  }
8291  }
8292 
8293  // Do the exchange of remote data.
8294  {
8295 #ifdef HAVE_TPETRA_MMM_TIMINGS
8296  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC getOwningPIDs exchange remote data")));
8297 #endif
8298  if (! communication_needed) {
8299  if (verbose) {
8300  std::ostringstream os;
8301  os << *verbosePrefix << "Communication not needed" << std::endl;
8302  std::cerr << os.str ();
8303  }
8304  }
8305  else {
8306  if (reverseMode) {
8307  if (constantNumPackets == 0) { // variable number of packets per LID
8308  if (verbose) {
8309  std::ostringstream os;
8310  os << *verbosePrefix << "Reverse mode, variable # packets / LID"
8311  << std::endl;
8312  std::cerr << os.str ();
8313  }
8314  // Make sure that host has the latest version, since we're
8315  // using the version on host. If host has the latest
8316  // version, syncing to host does nothing.
8317  destMat->numExportPacketsPerLID_.sync_host ();
8318  Teuchos::ArrayView<const size_t> numExportPacketsPerLID =
8319  getArrayViewFromDualView (destMat->numExportPacketsPerLID_);
8320  destMat->numImportPacketsPerLID_.sync_host ();
8321  Teuchos::ArrayView<size_t> numImportPacketsPerLID =
8322  getArrayViewFromDualView (destMat->numImportPacketsPerLID_);
8323 
8324  if (verbose) {
8325  std::ostringstream os;
8326  os << *verbosePrefix << "Calling 3-arg doReversePostsAndWaits"
8327  << std::endl;
8328  std::cerr << os.str ();
8329  }
8330  Distor.doReversePostsAndWaits(destMat->numExportPacketsPerLID_.view_host(), 1,
8331  destMat->numImportPacketsPerLID_.view_host());
8332  if (verbose) {
8333  std::ostringstream os;
8334  os << *verbosePrefix << "Finished 3-arg doReversePostsAndWaits"
8335  << std::endl;
8336  std::cerr << os.str ();
8337  }
8338 
8339  size_t totalImportPackets = 0;
8340  for (Array_size_type i = 0; i < numImportPacketsPerLID.size (); ++i) {
8341  totalImportPackets += numImportPacketsPerLID[i];
8342  }
8343 
8344  // Reallocation MUST go before setting the modified flag,
8345  // because it may clear out the flags.
8346  destMat->reallocImportsIfNeeded (totalImportPackets, verbose,
8347  verbosePrefix.get ());
8348  destMat->imports_.modify_host ();
8349  auto hostImports = destMat->imports_.view_host();
8350  // This is a legacy host pack/unpack path, so use the host
8351  // version of exports_.
8352  destMat->exports_.sync_host ();
8353  auto hostExports = destMat->exports_.view_host();
8354  if (verbose) {
8355  std::ostringstream os;
8356  os << *verbosePrefix << "Calling 4-arg doReversePostsAndWaits"
8357  << std::endl;
8358  std::cerr << os.str ();
8359  }
8360  Distor.doReversePostsAndWaits (hostExports,
8361  numExportPacketsPerLID,
8362  hostImports,
8363  numImportPacketsPerLID);
8364  if (verbose) {
8365  std::ostringstream os;
8366  os << *verbosePrefix << "Finished 4-arg doReversePostsAndWaits"
8367  << std::endl;
8368  std::cerr << os.str ();
8369  }
8370  }
8371  else { // constant number of packets per LID
8372  if (verbose) {
8373  std::ostringstream os;
8374  os << *verbosePrefix << "Reverse mode, constant # packets / LID"
8375  << std::endl;
8376  std::cerr << os.str ();
8377  }
8378  destMat->imports_.modify_host ();
8379  auto hostImports = destMat->imports_.view_host();
8380  // This is a legacy host pack/unpack path, so use the host
8381  // version of exports_.
8382  destMat->exports_.sync_host ();
8383  auto hostExports = destMat->exports_.view_host();
8384  if (verbose) {
8385  std::ostringstream os;
8386  os << *verbosePrefix << "Calling 3-arg doReversePostsAndWaits"
8387  << std::endl;
8388  std::cerr << os.str ();
8389  }
8390  Distor.doReversePostsAndWaits (hostExports,
8391  constantNumPackets,
8392  hostImports);
8393  if (verbose) {
8394  std::ostringstream os;
8395  os << *verbosePrefix << "Finished 3-arg doReversePostsAndWaits"
8396  << std::endl;
8397  std::cerr << os.str ();
8398  }
8399  }
8400  }
8401  else { // forward mode (the default)
8402  if (constantNumPackets == 0) { // variable number of packets per LID
8403  if (verbose) {
8404  std::ostringstream os;
8405  os << *verbosePrefix << "Forward mode, variable # packets / LID"
8406  << std::endl;
8407  std::cerr << os.str ();
8408  }
8409  // Make sure that host has the latest version, since we're
8410  // using the version on host. If host has the latest
8411  // version, syncing to host does nothing.
8412  destMat->numExportPacketsPerLID_.sync_host ();
8413  Teuchos::ArrayView<const size_t> numExportPacketsPerLID =
8414  getArrayViewFromDualView (destMat->numExportPacketsPerLID_);
8415  destMat->numImportPacketsPerLID_.sync_host ();
8416  Teuchos::ArrayView<size_t> numImportPacketsPerLID =
8417  getArrayViewFromDualView (destMat->numImportPacketsPerLID_);
8418  if (verbose) {
8419  std::ostringstream os;
8420  os << *verbosePrefix << "Calling 3-arg doPostsAndWaits"
8421  << std::endl;
8422  std::cerr << os.str ();
8423  }
8424  Distor.doPostsAndWaits(destMat->numExportPacketsPerLID_.view_host(), 1,
8425  destMat->numImportPacketsPerLID_.view_host());
8426  if (verbose) {
8427  std::ostringstream os;
8428  os << *verbosePrefix << "Finished 3-arg doPostsAndWaits"
8429  << std::endl;
8430  std::cerr << os.str ();
8431  }
8432 
8433  size_t totalImportPackets = 0;
8434  for (Array_size_type i = 0; i < numImportPacketsPerLID.size (); ++i) {
8435  totalImportPackets += numImportPacketsPerLID[i];
8436  }
8437 
8438  // Reallocation MUST go before setting the modified flag,
8439  // because it may clear out the flags.
8440  destMat->reallocImportsIfNeeded (totalImportPackets, verbose,
8441  verbosePrefix.get ());
8442  destMat->imports_.modify_host ();
8443  auto hostImports = destMat->imports_.view_host();
8444  // This is a legacy host pack/unpack path, so use the host
8445  // version of exports_.
8446  destMat->exports_.sync_host ();
8447  auto hostExports = destMat->exports_.view_host();
8448  if (verbose) {
8449  std::ostringstream os;
8450  os << *verbosePrefix << "Calling 4-arg doPostsAndWaits"
8451  << std::endl;
8452  std::cerr << os.str ();
8453  }
8454  Distor.doPostsAndWaits (hostExports,
8455  numExportPacketsPerLID,
8456  hostImports,
8457  numImportPacketsPerLID);
8458  if (verbose) {
8459  std::ostringstream os;
8460  os << *verbosePrefix << "Finished 4-arg doPostsAndWaits"
8461  << std::endl;
8462  std::cerr << os.str ();
8463  }
8464  }
8465  else { // constant number of packets per LID
8466  if (verbose) {
8467  std::ostringstream os;
8468  os << *verbosePrefix << "Forward mode, constant # packets / LID"
8469  << std::endl;
8470  std::cerr << os.str ();
8471  }
8472  destMat->imports_.modify_host ();
8473  auto hostImports = destMat->imports_.view_host();
8474  // This is a legacy host pack/unpack path, so use the host
8475  // version of exports_.
8476  destMat->exports_.sync_host ();
8477  auto hostExports = destMat->exports_.view_host();
8478  if (verbose) {
8479  std::ostringstream os;
8480  os << *verbosePrefix << "Calling 3-arg doPostsAndWaits"
8481  << std::endl;
8482  std::cerr << os.str ();
8483  }
8484  Distor.doPostsAndWaits (hostExports,
8485  constantNumPackets,
8486  hostImports);
8487  if (verbose) {
8488  std::ostringstream os;
8489  os << *verbosePrefix << "Finished 3-arg doPostsAndWaits"
8490  << std::endl;
8491  std::cerr << os.str ();
8492  }
8493  }
8494  }
8495  }
8496  }
8497 
8498  /*********************************************************************/
8499  /**** 3) Copy all of the Same/Permute/Remote data into CSR_arrays ****/
8500  /*********************************************************************/
8501 
8502  bool runOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace> && !useKokkosPath;
8503 
8504  Teuchos::Array<int> RemotePids;
8505  if (runOnHost) {
8506  Teuchos::Array<int> TargetPids;
8507  // Backwards compatibility measure. We'll use this again below.
8508 
8509  // TODO JHU Need to track down why numImportPacketsPerLID_ has not been corrently marked as modified on host (which it has been)
8510  // TODO JHU somewhere above, e.g., call to Distor.doPostsAndWaits().
8511  // TODO JHU This only becomes apparent as we begin to convert TAFC to run on device.
8512  destMat->numImportPacketsPerLID_.modify_host(); //FIXME
8513 
8514 # ifdef HAVE_TPETRA_MMM_TIMINGS
8515  RCP<TimeMonitor> tmCopySPRdata = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(prefix + std::string("TAFC unpack-count-resize + copy same-perm-remote data"))));
8516 # endif
8517  ArrayRCP<size_t> CSR_rowptr;
8518  ArrayRCP<GO> CSR_colind_GID;
8519  ArrayRCP<LO> CSR_colind_LID;
8520  ArrayRCP<Scalar> CSR_vals;
8521 
8522  destMat->imports_.sync_device ();
8523  destMat->numImportPacketsPerLID_.sync_device ();
8524 
8525  size_t N = BaseRowMap->getLocalNumElements ();
8526 
8527  auto RemoteLIDs_d = RemoteLIDs.view_device();
8528  auto PermuteToLIDs_d = PermuteToLIDs.view_device();
8529  auto PermuteFromLIDs_d = PermuteFromLIDs.view_device();
8530 
8532  *this,
8533  RemoteLIDs_d,
8534  destMat->imports_.view_device(), //hostImports
8535  destMat->numImportPacketsPerLID_.view_device(), //numImportPacketsPerLID
8536  NumSameIDs,
8537  PermuteToLIDs_d,
8538  PermuteFromLIDs_d,
8539  N,
8540  MyPID,
8541  CSR_rowptr,
8542  CSR_colind_GID,
8543  CSR_vals,
8544  SourcePids(),
8545  TargetPids);
8546 
8547  // If LO and GO are the same, we can reuse memory when
8548  // converting the column indices from global to local indices.
8549  if (typeid (LO) == typeid (GO)) {
8550  CSR_colind_LID = Teuchos::arcp_reinterpret_cast<LO> (CSR_colind_GID);
8551  }
8552  else {
8553  CSR_colind_LID.resize (CSR_colind_GID.size());
8554  }
8555  CSR_colind_LID.resize (CSR_colind_GID.size());
8556 
8557  // On return from unpackAndCombineIntoCrsArrays TargetPids[i] == -1 for locally
8558  // owned entries. Convert them to the actual PID.
8559  // JHU FIXME This can be done within unpackAndCombineIntoCrsArrays with a parallel_for.
8560  for(size_t i=0; i<static_cast<size_t>(TargetPids.size()); i++)
8561  {
8562  if(TargetPids[i] == -1) TargetPids[i] = MyPID;
8563  }
8564 #ifdef HAVE_TPETRA_MMM_TIMINGS
8565  tmCopySPRdata = Teuchos::null;
8566 #endif
8567  /**************************************************************/
8568  /**** 4) Call Optimized MakeColMap w/ no Directory Lookups ****/
8569  /**************************************************************/
8570  // Call an optimized version of makeColMap that avoids the
8571  // Directory lookups (since the Import object knows who owns all
8572  // the GIDs).
8573  if (verbose) {
8574  std::ostringstream os;
8575  os << *verbosePrefix << "Calling lowCommunicationMakeColMapAndReindex"
8576  << std::endl;
8577  std::cerr << os.str ();
8578  }
8579  {
8580 #ifdef HAVE_TPETRA_MMM_TIMINGS
8581  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC makeColMap")));
8582 #endif
8583  Import_Util::lowCommunicationMakeColMapAndReindexSerial(CSR_rowptr (),
8584  CSR_colind_LID (),
8585  CSR_colind_GID (),
8586  BaseDomainMap,
8587  TargetPids,
8588  RemotePids,
8589  MyColMap);
8590  }
8591 
8592  if (verbose) {
8593  std::ostringstream os;
8594  os << *verbosePrefix << "restrictComm="
8595  << (restrictComm ? "true" : "false") << std::endl;
8596  std::cerr << os.str ();
8597  }
8598 
8599  /*******************************************************/
8600  /**** 4) Second communicator restriction phase ****/
8601  /*******************************************************/
8602  {
8603 #ifdef HAVE_TPETRA_MMM_TIMINGS
8604  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC restrict colmap")));
8605 #endif
8606  if (restrictComm) {
8607  ReducedColMap = (MyRowMap.getRawPtr () == MyColMap.getRawPtr ()) ?
8608  ReducedRowMap :
8609  MyColMap->replaceCommWithSubset (ReducedComm);
8610  MyColMap = ReducedColMap; // Reset the "my" maps
8611  }
8612 
8613  // Replace the col map
8614  if (verbose) {
8615  std::ostringstream os;
8616  os << *verbosePrefix << "Calling replaceColMap" << std::endl;
8617  std::cerr << os.str ();
8618  }
8619  destMat->replaceColMap (MyColMap);
8620 
8621  // Short circuit if the processor is no longer in the communicator
8622  //
8623  // NOTE: Epetra replaces modifies all "removed" processes so they
8624  // have a dummy (serial) Map that doesn't touch the original
8625  // communicator. Duplicating that here might be a good idea.
8626  if (ReducedComm.is_null ()) {
8627  if (verbose) {
8628  std::ostringstream os;
8629  os << *verbosePrefix << "I am no longer in the communicator; "
8630  "returning" << std::endl;
8631  std::cerr << os.str ();
8632  }
8633  return;
8634  }
8635  }
8636 
8637  /***************************************************/
8638  /**** 5) Sort ****/
8639  /***************************************************/
8640  if ((! reverseMode && xferAsImport != nullptr) ||
8641  (reverseMode && xferAsExport != nullptr)) {
8642  if (verbose) {
8643  std::ostringstream os;
8644  os << *verbosePrefix << "Calling sortCrsEntries" << endl;
8645  std::cerr << os.str ();
8646  }
8647 #ifdef HAVE_TPETRA_MMM_TIMINGS
8648  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC sortCrsEntries")));
8649 #endif
8650  Import_Util::sortCrsEntries (CSR_rowptr(),
8651  CSR_colind_LID(),
8652  CSR_vals());
8653  }
8654  else if ((! reverseMode && xferAsExport != nullptr) ||
8655  (reverseMode && xferAsImport != nullptr)) {
8656  if (verbose) {
8657  std::ostringstream os;
8658  os << *verbosePrefix << "Calling sortAndMergeCrsEntries"
8659  << endl;
8660  std::cerr << os.str();
8661  }
8662 #ifdef HAVE_TPETRA_MMM_TIMINGS
8663  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC sortAndMergeCrsEntries")));
8664 #endif
8665  Import_Util::sortAndMergeCrsEntries (CSR_rowptr(),
8666  CSR_colind_LID(),
8667  CSR_vals());
8668  if (CSR_rowptr[N] != static_cast<size_t>(CSR_vals.size())) {
8669  CSR_colind_LID.resize (CSR_rowptr[N]);
8670  CSR_vals.resize (CSR_rowptr[N]);
8671  }
8672  }
8673  else {
8674  TEUCHOS_TEST_FOR_EXCEPTION(
8675  true, std::logic_error, "Tpetra::CrsMatrix::"
8676  "transferAndFillComplete: Should never get here! "
8677  "Please report this bug to a Tpetra developer.");
8678  }
8679  /***************************************************/
8680  /**** 6) Reset the colmap and the arrays ****/
8681  /***************************************************/
8682 
8683  if (verbose) {
8684  std::ostringstream os;
8685  os << *verbosePrefix << "Calling destMat->setAllValues" << endl;
8686  std::cerr << os.str ();
8687  }
8688 
8689  // Call constructor for the new matrix (restricted as needed)
8690  //
8691  // NOTE (mfh 15 May 2014) This should work fine for the Kokkos
8692  // refactor version of CrsMatrix, though it reserves the right to
8693  // make a deep copy of the arrays.
8694  {
8695 #ifdef HAVE_TPETRA_MMM_TIMINGS
8696  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC setAllValues")));
8697 #endif
8698  destMat->setAllValues (CSR_rowptr, CSR_colind_LID, CSR_vals);
8699  }
8700 
8701  } else {
8702  // run on device
8703 
8704 
8705  // Backwards compatibility measure. We'll use this again below.
8706 
8707  // TODO JHU Need to track down why numImportPacketsPerLID_ has not been corrently marked as modified on host (which it has been)
8708  // TODO JHU somewhere above, e.g., call to Distor.doPostsAndWaits().
8709  // TODO JHU This only becomes apparent as we begin to convert TAFC to run on device.
8710  destMat->numImportPacketsPerLID_.modify_host(); //FIXME
8711 
8712 # ifdef HAVE_TPETRA_MMM_TIMINGS
8713  RCP<TimeMonitor> tmCopySPRdata = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(prefix + std::string("TAFC unpack-count-resize + copy same-perm-remote data"))));
8714 # endif
8715  ArrayRCP<size_t> CSR_rowptr;
8716  ArrayRCP<GO> CSR_colind_GID;
8717  ArrayRCP<LO> CSR_colind_LID;
8718  ArrayRCP<Scalar> CSR_vals;
8719 
8720  destMat->imports_.sync_device ();
8721  destMat->numImportPacketsPerLID_.sync_device ();
8722 
8723  size_t N = BaseRowMap->getLocalNumElements ();
8724 
8725  auto RemoteLIDs_d = RemoteLIDs.view_device();
8726  auto PermuteToLIDs_d = PermuteToLIDs.view_device();
8727  auto PermuteFromLIDs_d = PermuteFromLIDs.view_device();
8728 
8729  Kokkos::View<size_t*,device_type> CSR_rowptr_d;
8730  Kokkos::View<GO*,device_type> CSR_colind_GID_d;
8731  Kokkos::View<LO*,device_type> CSR_colind_LID_d;
8732  Kokkos::View<impl_scalar_type*,device_type> CSR_vals_d;
8733  Kokkos::View<int*,device_type> TargetPids_d;
8734 
8736  *this,
8737  RemoteLIDs_d,
8738  destMat->imports_.view_device(), //hostImports
8739  destMat->numImportPacketsPerLID_.view_device(), //numImportPacketsPerLID
8740  NumSameIDs,
8741  PermuteToLIDs_d,
8742  PermuteFromLIDs_d,
8743  N,
8744  MyPID,
8745  CSR_rowptr_d,
8746  CSR_colind_GID_d,
8747  CSR_vals_d,
8748  SourcePids(),
8749  TargetPids_d);
8750 
8751  Kokkos::resize (CSR_colind_LID_d, CSR_colind_GID_d.size());
8752 
8753 #ifdef HAVE_TPETRA_MMM_TIMINGS
8754  tmCopySPRdata = Teuchos::null;
8755 #endif
8756  /**************************************************************/
8757  /**** 4) Call Optimized MakeColMap w/ no Directory Lookups ****/
8758  /**************************************************************/
8759  // Call an optimized version of makeColMap that avoids the
8760  // Directory lookups (since the Import object knows who owns all
8761  // the GIDs).
8762  if (verbose) {
8763  std::ostringstream os;
8764  os << *verbosePrefix << "Calling lowCommunicationMakeColMapAndReindex"
8765  << std::endl;
8766  std::cerr << os.str ();
8767  }
8768  {
8769 #ifdef HAVE_TPETRA_MMM_TIMINGS
8770  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC makeColMap")));
8771 #endif
8772  Import_Util::lowCommunicationMakeColMapAndReindex(CSR_rowptr_d,
8773  CSR_colind_LID_d,
8774  CSR_colind_GID_d,
8775  BaseDomainMap,
8776  TargetPids_d,
8777  RemotePids,
8778  MyColMap);
8779  }
8780 
8781  if (verbose) {
8782  std::ostringstream os;
8783  os << *verbosePrefix << "restrictComm="
8784  << (restrictComm ? "true" : "false") << std::endl;
8785  std::cerr << os.str ();
8786  }
8787 
8788  /*******************************************************/
8789  /**** 4) Second communicator restriction phase ****/
8790  /*******************************************************/
8791  {
8792 #ifdef HAVE_TPETRA_MMM_TIMINGS
8793  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC restrict colmap")));
8794 #endif
8795  if (restrictComm) {
8796  ReducedColMap = (MyRowMap.getRawPtr () == MyColMap.getRawPtr ()) ?
8797  ReducedRowMap :
8798  MyColMap->replaceCommWithSubset (ReducedComm);
8799  MyColMap = ReducedColMap; // Reset the "my" maps
8800  }
8801 
8802  // Replace the col map
8803  if (verbose) {
8804  std::ostringstream os;
8805  os << *verbosePrefix << "Calling replaceColMap" << std::endl;
8806  std::cerr << os.str ();
8807  }
8808  destMat->replaceColMap (MyColMap);
8809 
8810  // Short circuit if the processor is no longer in the communicator
8811  //
8812  // NOTE: Epetra replaces modifies all "removed" processes so they
8813  // have a dummy (serial) Map that doesn't touch the original
8814  // communicator. Duplicating that here might be a good idea.
8815  if (ReducedComm.is_null ()) {
8816  if (verbose) {
8817  std::ostringstream os;
8818  os << *verbosePrefix << "I am no longer in the communicator; "
8819  "returning" << std::endl;
8820  std::cerr << os.str ();
8821  }
8822  return;
8823  }
8824  }
8825 
8826  /***************************************************/
8827  /**** 5) Sort ****/
8828  /***************************************************/
8829 
8830  if ((! reverseMode && xferAsImport != nullptr) ||
8831  (reverseMode && xferAsExport != nullptr)) {
8832  if (verbose) {
8833  std::ostringstream os;
8834  os << *verbosePrefix << "Calling sortCrsEntries" << endl;
8835  std::cerr << os.str ();
8836  }
8837 #ifdef HAVE_TPETRA_MMM_TIMINGS
8838  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC sortCrsEntries")));
8839 #endif
8840  Import_Util::sortCrsEntries (CSR_rowptr_d,
8841  CSR_colind_LID_d,
8842  CSR_vals_d);
8843  }
8844  else if ((! reverseMode && xferAsExport != nullptr) ||
8845  (reverseMode && xferAsImport != nullptr)) {
8846  if (verbose) {
8847  std::ostringstream os;
8848  os << *verbosePrefix << "Calling sortAndMergeCrsEntries"
8849  << endl;
8850  std::cerr << os.str();
8851  }
8852 #ifdef HAVE_TPETRA_MMM_TIMINGS
8853  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC sortAndMergeCrsEntries")));
8854 #endif
8855  Import_Util::sortAndMergeCrsEntries (CSR_rowptr_d,
8856  CSR_colind_LID_d,
8857  CSR_vals_d);
8858  }
8859  else {
8860  TEUCHOS_TEST_FOR_EXCEPTION(
8861  true, std::logic_error, "Tpetra::CrsMatrix::"
8862  "transferAndFillComplete: Should never get here! "
8863  "Please report this bug to a Tpetra developer.");
8864  }
8865 
8866  /***************************************************/
8867  /**** 6) Reset the colmap and the arrays ****/
8868  /***************************************************/
8869 
8870  if (verbose) {
8871  std::ostringstream os;
8872  os << *verbosePrefix << "Calling destMat->setAllValues" << endl;
8873  std::cerr << os.str ();
8874  }
8875 
8876  {
8877 #ifdef HAVE_TPETRA_MMM_TIMINGS
8878  Teuchos::TimeMonitor MMrc(*TimeMonitor::getNewTimer(prefix + std::string("TAFC setAllValues")));
8879 #endif
8880  destMat->setAllValues (CSR_rowptr_d, CSR_colind_LID_d, CSR_vals_d);
8881  }
8882 
8883  } //if (runOnHost) .. else ..
8884 
8885  /***************************************************/
8886  /**** 7) Build Importer & Call ESFC ****/
8887  /***************************************************/
8888 #ifdef HAVE_TPETRA_MMM_TIMINGS
8889  RCP<TimeMonitor> tmIESFC = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(prefix + std::string("TAFC build importer and esfc"))));
8890 #endif
8891  // Pre-build the importer using the existing PIDs
8892  Teuchos::ParameterList esfc_params;
8893 
8894  RCP<import_type> MyImport;
8895 
8896  // Fulfull the non-blocking allreduce on reduced_mismatch.
8897  if (iallreduceRequest.get () != nullptr) {
8898  if (verbose) {
8899  std::ostringstream os;
8900  os << *verbosePrefix << "Calling iallreduceRequest->wait()"
8901  << endl;
8902  std::cerr << os.str ();
8903  }
8904  iallreduceRequest->wait ();
8905  if (reduced_mismatch != 0) {
8906  isMM = false;
8907  }
8908  }
8909 
8910  if( isMM ) {
8911 #ifdef HAVE_TPETRA_MMM_TIMINGS
8912  Teuchos::TimeMonitor MMisMM (*TimeMonitor::getNewTimer(prefix + std::string("isMM Block")));
8913 #endif
8914  // Combine all type1/2/3 lists, [filter them], then call the expert import constructor.
8915 
8916  if (verbose) {
8917  std::ostringstream os;
8918  os << *verbosePrefix << "Getting CRS pointers" << endl;
8919  std::cerr << os.str ();
8920  }
8921 
8922  Teuchos::ArrayRCP<LocalOrdinal> type3LIDs;
8923  Teuchos::ArrayRCP<int> type3PIDs;
8924  auto rowptr = getCrsGraph()->getLocalRowPtrsHost();
8925  auto colind = getCrsGraph()->getLocalIndicesHost();
8926 
8927  if (verbose) {
8928  std::ostringstream os;
8929  os << *verbosePrefix << "Calling reverseNeighborDiscovery" << std::endl;
8930  std::cerr << os.str ();
8931  }
8932 
8933  {
8934 #ifdef HAVE_TPETRA_MMM_TIMINGS
8935  TimeMonitor tm_rnd (*TimeMonitor::getNewTimer(prefix + std::string("isMMrevNeighDis")));
8936 #endif
8937  Import_Util::reverseNeighborDiscovery(*this,
8938  rowptr,
8939  colind,
8940  rowTransfer,
8941  MyImporter,
8942  MyDomainMap,
8943  type3PIDs,
8944  type3LIDs,
8945  ReducedComm);
8946  }
8947 
8948  if (verbose) {
8949  std::ostringstream os;
8950  os << *verbosePrefix << "Done with reverseNeighborDiscovery" << std::endl;
8951  std::cerr << os.str ();
8952  }
8953 
8954  Teuchos::ArrayView<const int> EPID1 = MyImporter.is_null() ? Teuchos::ArrayView<const int>() : MyImporter->getExportPIDs();
8955  Teuchos::ArrayView<const LO> ELID1 = MyImporter.is_null() ? Teuchos::ArrayView<const LO>() : MyImporter->getExportLIDs();
8956 
8957  Teuchos::ArrayView<const int> TEPID2 = rowTransfer.getExportPIDs(); // row matrix
8958  Teuchos::ArrayView<const LO> TELID2 = rowTransfer.getExportLIDs();
8959 
8960  const int numCols = getGraph()->getColMap()->getLocalNumElements(); // may be dup
8961  // from EpetraExt_MMHelpers.cpp: build_type2_exports
8962  std::vector<bool> IsOwned(numCols,true);
8963  std::vector<int> SentTo(numCols,-1);
8964  if (! MyImporter.is_null ()) {
8965  for (auto && rlid : MyImporter->getRemoteLIDs()) { // the remoteLIDs must be from sourcematrix
8966  IsOwned[rlid]=false;
8967  }
8968  }
8969 
8970  std::vector<std::pair<int,GO> > usrtg;
8971  usrtg.reserve(TEPID2.size());
8972 
8973  {
8974  const auto& colMap = * (this->getColMap ()); // *this is sourcematrix
8975  for (Array_size_type i = 0; i < TEPID2.size (); ++i) {
8976  const LO row = TELID2[i];
8977  const int pid = TEPID2[i];
8978  for (auto j = rowptr[row]; j < rowptr[row+1]; ++j) {
8979  const int col = colind[j];
8980  if (IsOwned[col] && SentTo[col] != pid) {
8981  SentTo[col] = pid;
8982  GO gid = colMap.getGlobalElement (col);
8983  usrtg.push_back (std::pair<int,GO> (pid, gid));
8984  }
8985  }
8986  }
8987  }
8988 
8989 // This sort can _not_ be omitted.[
8990  std::sort(usrtg.begin(),usrtg.end()); // default comparator does the right thing, now sorted in gid order
8991  auto eopg = std ::unique(usrtg.begin(),usrtg.end());
8992  // 25 Jul 2018: Could just ignore the entries at and after eopg.
8993  usrtg.erase(eopg,usrtg.end());
8994 
8995  const Array_size_type type2_us_size = usrtg.size();
8996  Teuchos::ArrayRCP<int> EPID2=Teuchos::arcp(new int[type2_us_size],0,type2_us_size,true);
8997  Teuchos::ArrayRCP< LO> ELID2=Teuchos::arcp(new LO[type2_us_size],0,type2_us_size,true);
8998 
8999  int pos=0;
9000  for(auto && p : usrtg) {
9001  EPID2[pos]= p.first;
9002  ELID2[pos]= this->getDomainMap()->getLocalElement(p.second);
9003  pos++;
9004  }
9005 
9006  Teuchos::ArrayView<int> EPID3 = type3PIDs();
9007  Teuchos::ArrayView< LO> ELID3 = type3LIDs();
9008  GO InfGID = std::numeric_limits<GO>::max();
9009  int InfPID = INT_MAX;
9010 #ifdef TPETRA_MIN3
9011 # undef TPETRA_MIN3
9012 #endif // TPETRA_MIN3
9013 #define TPETRA_MIN3(x,y,z) ((x)<(y)?(std::min(x,z)):(std::min(y,z)))
9014  int i1=0, i2=0, i3=0;
9015  int Len1 = EPID1.size();
9016  int Len2 = EPID2.size();
9017  int Len3 = EPID3.size();
9018 
9019  int MyLen=Len1+Len2+Len3;
9020  Teuchos::ArrayRCP<LO> userExportLIDs = Teuchos::arcp(new LO[MyLen],0,MyLen,true);
9021  Teuchos::ArrayRCP<int> userExportPIDs = Teuchos::arcp(new int[MyLen],0,MyLen,true);
9022  int iloc = 0; // will be the size of the userExportLID/PIDs
9023 
9024  while(i1 < Len1 || i2 < Len2 || i3 < Len3){
9025  int PID1 = (i1<Len1)?(EPID1[i1]):InfPID;
9026  int PID2 = (i2<Len2)?(EPID2[i2]):InfPID;
9027  int PID3 = (i3<Len3)?(EPID3[i3]):InfPID;
9028 
9029  GO GID1 = (i1<Len1)?getDomainMap()->getGlobalElement(ELID1[i1]):InfGID;
9030  GO GID2 = (i2<Len2)?getDomainMap()->getGlobalElement(ELID2[i2]):InfGID;
9031  GO GID3 = (i3<Len3)?getDomainMap()->getGlobalElement(ELID3[i3]):InfGID;
9032 
9033  int MIN_PID = TPETRA_MIN3(PID1,PID2,PID3);
9034  GO MIN_GID = TPETRA_MIN3( ((PID1==MIN_PID)?GID1:InfGID), ((PID2==MIN_PID)?GID2:InfGID), ((PID3==MIN_PID)?GID3:InfGID));
9035 #ifdef TPETRA_MIN3
9036 # undef TPETRA_MIN3
9037 #endif // TPETRA_MIN3
9038  bool added_entry=false;
9039 
9040  if(PID1 == MIN_PID && GID1 == MIN_GID){
9041  userExportLIDs[iloc]=ELID1[i1];
9042  userExportPIDs[iloc]=EPID1[i1];
9043  i1++;
9044  added_entry=true;
9045  iloc++;
9046  }
9047  if(PID2 == MIN_PID && GID2 == MIN_GID){
9048  if(!added_entry) {
9049  userExportLIDs[iloc]=ELID2[i2];
9050  userExportPIDs[iloc]=EPID2[i2];
9051  added_entry=true;
9052  iloc++;
9053  }
9054  i2++;
9055  }
9056  if(PID3 == MIN_PID && GID3 == MIN_GID){
9057  if(!added_entry) {
9058  userExportLIDs[iloc]=ELID3[i3];
9059  userExportPIDs[iloc]=EPID3[i3];
9060  iloc++;
9061  }
9062  i3++;
9063  }
9064  }
9065 
9066  if (verbose) {
9067  std::ostringstream os;
9068  os << *verbosePrefix << "Create Import" << std::endl;
9069  std::cerr << os.str ();
9070  }
9071 
9072 #ifdef HAVE_TPETRA_MMM_TIMINGS
9073  auto ismmIctor(*TimeMonitor::getNewTimer(prefix + std::string("isMMIportCtor")));
9074 #endif
9075  Teuchos::RCP<Teuchos::ParameterList> plist = rcp(new Teuchos::ParameterList());
9076  // 25 Jul 2018: Test for equality with the non-isMM path's Import object.
9077  if ((MyDomainMap != MyColMap) && (!MyDomainMap->isSameAs(*MyColMap)))
9078  MyImport = rcp ( new import_type (MyDomainMap,
9079  MyColMap,
9080  RemotePids,
9081  userExportLIDs.view(0,iloc).getConst(),
9082  userExportPIDs.view(0,iloc).getConst(),
9083  plist)
9084  );
9085 
9086  if (verbose) {
9087  std::ostringstream os;
9088  os << *verbosePrefix << "Call expertStaticFillComplete" << std::endl;
9089  std::cerr << os.str ();
9090  }
9091 
9092  {
9093 #ifdef HAVE_TPETRA_MMM_TIMINGS
9094  TimeMonitor esfc (*TimeMonitor::getNewTimer(prefix + std::string("isMM::destMat->eSFC")));
9095  esfc_params.set("Timer Label",label+std::string("isMM eSFC"));
9096 #endif
9097  if(!params.is_null())
9098  esfc_params.set("compute global constants",params->get("compute global constants",true));
9099  destMat->expertStaticFillComplete (MyDomainMap, MyRangeMap, MyImport,Teuchos::null,rcp(new Teuchos::ParameterList(esfc_params)));
9100 
9101  }
9102 
9103  } // if(isMM)
9104  else {
9105 #ifdef HAVE_TPETRA_MMM_TIMINGS
9106  TimeMonitor MMnotMMblock (*TimeMonitor::getNewTimer(prefix + std::string("TAFC notMMblock")));
9107 #endif
9108  if (verbose) {
9109  std::ostringstream os;
9110  os << *verbosePrefix << "Create Import" << std::endl;
9111  std::cerr << os.str ();
9112  }
9113 
9114 #ifdef HAVE_TPETRA_MMM_TIMINGS
9115  TimeMonitor notMMIcTor(*TimeMonitor::getNewTimer(prefix + std::string("TAFC notMMCreateImporter")));
9116 #endif
9117  Teuchos::RCP<Teuchos::ParameterList> mypars = rcp(new Teuchos::ParameterList);
9118  mypars->set("Timer Label","notMMFrom_tAFC");
9119  if ((MyDomainMap != MyColMap) && (!MyDomainMap->isSameAs(*MyColMap)))
9120  MyImport = rcp (new import_type (MyDomainMap, MyColMap, RemotePids, mypars));
9121 
9122  if (verbose) {
9123  std::ostringstream os;
9124  os << *verbosePrefix << "Call expertStaticFillComplete" << endl;
9125  std::cerr << os.str ();
9126  }
9127 
9128 #ifdef HAVE_TPETRA_MMM_TIMINGS
9129  TimeMonitor esfcnotmm(*TimeMonitor::getNewTimer(prefix + std::string("notMMdestMat->expertStaticFillComplete")));
9130  esfc_params.set("Timer Label",prefix+std::string("notMM eSFC"));
9131 #else
9132  esfc_params.set("Timer Label",std::string("notMM eSFC"));
9133 #endif
9134 
9135  if (!params.is_null ()) {
9136  esfc_params.set ("compute global constants",
9137  params->get ("compute global constants", true));
9138  }
9139  destMat->expertStaticFillComplete (MyDomainMap, MyRangeMap,
9140  MyImport, Teuchos::null,
9141  rcp (new Teuchos::ParameterList (esfc_params)));
9142  }
9143 
9144 #ifdef HAVE_TPETRA_MMM_TIMINGS
9145  tmIESFC = Teuchos::null;
9146 #endif
9147 
9148  if (verbose) {
9149  std::ostringstream os;
9150  os << *verbosePrefix << "Done" << endl;
9151  std::cerr << os.str ();
9152  }
9153  } //transferAndFillComplete
9154 
9155 
9156  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
9157  void
9160  const import_type& importer,
9161  const Teuchos::RCP<const map_type>& domainMap,
9162  const Teuchos::RCP<const map_type>& rangeMap,
9163  const Teuchos::RCP<Teuchos::ParameterList>& params) const
9164  {
9165  transferAndFillComplete (destMatrix, importer, Teuchos::null, domainMap, rangeMap, params);
9166  }
9167 
9168  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
9169  void
9172  const import_type& rowImporter,
9173  const import_type& domainImporter,
9174  const Teuchos::RCP<const map_type>& domainMap,
9175  const Teuchos::RCP<const map_type>& rangeMap,
9176  const Teuchos::RCP<Teuchos::ParameterList>& params) const
9177  {
9178  transferAndFillComplete (destMatrix, rowImporter, Teuchos::rcpFromRef(domainImporter), domainMap, rangeMap, params);
9179  }
9180 
9181  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
9182  void
9185  const export_type& exporter,
9186  const Teuchos::RCP<const map_type>& domainMap,
9187  const Teuchos::RCP<const map_type>& rangeMap,
9188  const Teuchos::RCP<Teuchos::ParameterList>& params) const
9189  {
9190  transferAndFillComplete (destMatrix, exporter, Teuchos::null, domainMap, rangeMap, params);
9191  }
9192 
9193  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
9194  void
9197  const export_type& rowExporter,
9198  const export_type& domainExporter,
9199  const Teuchos::RCP<const map_type>& domainMap,
9200  const Teuchos::RCP<const map_type>& rangeMap,
9201  const Teuchos::RCP<Teuchos::ParameterList>& params) const
9202  {
9203  transferAndFillComplete (destMatrix, rowExporter, Teuchos::rcpFromRef(domainExporter), domainMap, rangeMap, params);
9204  }
9205 
9206 } // namespace Tpetra
9207 
9208 //
9209 // Explicit instantiation macro
9210 //
9211 // Must be expanded from within the Tpetra namespace!
9212 //
9213 
9214 #define TPETRA_CRSMATRIX_MATRIX_INSTANT(SCALAR,LO,GO,NODE) \
9215  \
9216  template class CrsMatrix< SCALAR , LO , GO , NODE >;
9217 
9218 #define TPETRA_CRSMATRIX_CONVERT_INSTANT(SO,SI,LO,GO,NODE) \
9219  \
9220  template Teuchos::RCP< CrsMatrix< SO , LO , GO , NODE > > \
9221  CrsMatrix< SI , LO , GO , NODE >::convert< SO > () const;
9222 
9223 #define TPETRA_CRSMATRIX_IMPORT_AND_FILL_COMPLETE_INSTANT(SCALAR, LO, GO, NODE) \
9224  template<> \
9225  Teuchos::RCP<CrsMatrix<SCALAR, LO, GO, NODE> > \
9226  importAndFillCompleteCrsMatrix (const Teuchos::RCP<const CrsMatrix<SCALAR, LO, GO, NODE> >& sourceMatrix, \
9227  const Import<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9228  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9229  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& importer, \
9230  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9231  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9232  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& domainMap, \
9233  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9234  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9235  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& rangeMap, \
9236  const Teuchos::RCP<Teuchos::ParameterList>& params);
9237 
9238 #define TPETRA_CRSMATRIX_IMPORT_AND_FILL_COMPLETE_INSTANT_TWO(SCALAR, LO, GO, NODE) \
9239  template<> \
9240  Teuchos::RCP<CrsMatrix<SCALAR, LO, GO, NODE> > \
9241  importAndFillCompleteCrsMatrix (const Teuchos::RCP<const CrsMatrix<SCALAR, LO, GO, NODE> >& sourceMatrix, \
9242  const Import<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9243  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9244  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& rowImporter, \
9245  const Import<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9246  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9247  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& domainImporter, \
9248  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9249  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9250  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& domainMap, \
9251  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9252  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9253  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& rangeMap, \
9254  const Teuchos::RCP<Teuchos::ParameterList>& params);
9255 
9256 
9257 #define TPETRA_CRSMATRIX_EXPORT_AND_FILL_COMPLETE_INSTANT(SCALAR, LO, GO, NODE) \
9258  template<> \
9259  Teuchos::RCP<CrsMatrix<SCALAR, LO, GO, NODE> > \
9260  exportAndFillCompleteCrsMatrix (const Teuchos::RCP<const CrsMatrix<SCALAR, LO, GO, NODE> >& sourceMatrix, \
9261  const Export<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9262  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9263  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& exporter, \
9264  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9265  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9266  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& domainMap, \
9267  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9268  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9269  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& rangeMap, \
9270  const Teuchos::RCP<Teuchos::ParameterList>& params);
9271 
9272 #define TPETRA_CRSMATRIX_EXPORT_AND_FILL_COMPLETE_INSTANT_TWO(SCALAR, LO, GO, NODE) \
9273  template<> \
9274  Teuchos::RCP<CrsMatrix<SCALAR, LO, GO, NODE> > \
9275  exportAndFillCompleteCrsMatrix (const Teuchos::RCP<const CrsMatrix<SCALAR, LO, GO, NODE> >& sourceMatrix, \
9276  const Export<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9277  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9278  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& rowExporter, \
9279  const Export<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9280  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9281  CrsMatrix<SCALAR, LO, GO, NODE>::node_type>& domainExporter, \
9282  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9283  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9284  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& domainMap, \
9285  const Teuchos::RCP<const Map<CrsMatrix<SCALAR, LO, GO, NODE>::local_ordinal_type, \
9286  CrsMatrix<SCALAR, LO, GO, NODE>::global_ordinal_type, \
9287  CrsMatrix<SCALAR, LO, GO, NODE>::node_type> >& rangeMap, \
9288  const Teuchos::RCP<Teuchos::ParameterList>& params);
9289 
9290 
9291 #define TPETRA_CRSMATRIX_INSTANT(SCALAR, LO, GO ,NODE) \
9292  TPETRA_CRSMATRIX_MATRIX_INSTANT(SCALAR, LO, GO, NODE) \
9293  TPETRA_CRSMATRIX_IMPORT_AND_FILL_COMPLETE_INSTANT(SCALAR, LO, GO, NODE) \
9294  TPETRA_CRSMATRIX_EXPORT_AND_FILL_COMPLETE_INSTANT(SCALAR, LO, GO, NODE) \
9295  TPETRA_CRSMATRIX_IMPORT_AND_FILL_COMPLETE_INSTANT_TWO(SCALAR, LO, GO, NODE) \
9296  TPETRA_CRSMATRIX_EXPORT_AND_FILL_COMPLETE_INSTANT_TWO(SCALAR, LO, GO, NODE)
9297 
9298 #endif // TPETRA_CRSMATRIX_DEF_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Teuchos::RCP< const map_type > getRowMap() const override
Returns the Map that describes the row distribution in this graph.
bool hasColMap() const override
Whether the matrix has a well-defined column Map.
Declaration and generic definition of traits class that tells Tpetra::CrsMatrix how to pack and unpac...
bool indicesAreSorted_
Whether the graph&#39;s indices are sorted in each row, on this process.
global_size_t getGlobalNumCols() const override
The number of global columns in the matrix.
Impl::CreateMirrorViewFromUnmanagedHostArray< ValueType, OutputDeviceType >::output_view_type create_mirror_view_from_raw_host_array(const OutputDeviceType &, ValueType *inPtr, const size_t inSize, const bool copy=true, const char label[]="")
Variant of Kokkos::create_mirror_view that takes a raw host 1-d array as input.
Functor for the the ABSMAX CombineMode of Import and Export operations.
void checkInternalState() const
Check that this object&#39;s state is sane; throw if it&#39;s not.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
void copyOffsets(const OutputViewType &dst, const InputViewType &src)
Copy row offsets (in a sparse graph or matrix) from src to dst. The offsets may have different types...
CrsGraph< LocalOrdinal, GlobalOrdinal, Node > crs_graph_type
The CrsGraph specialization suitable for this CrsMatrix specialization.
void replaceColMap(const Teuchos::RCP< const map_type > &newColMap)
Replace the matrix&#39;s column Map with the given Map.
virtual LocalOrdinal replaceGlobalValuesImpl(impl_scalar_type rowVals[], const crs_graph_type &graph, const RowInfo &rowInfo, const GlobalOrdinal inds[], const impl_scalar_type newVals[], const LocalOrdinal numElts)
Implementation detail of replaceGlobalValues.
virtual bool supportsRowViews() const override
Return true if getLocalRowView() and getGlobalRowView() are valid for this object.
void replaceDomainMapAndImporter(const Teuchos::RCP< const map_type > &newDomainMap, Teuchos::RCP< const import_type > &newImporter)
Replace the current domain Map and Import with the given objects.
local_inds_dualv_type::t_host::const_type getLocalIndsViewHost(const RowInfo &rowinfo) const
Get a const, locally indexed view of the locally owned row myRow, such that rowinfo = getRowInfo(myRo...
void merge2(IT1 &indResultOut, IT2 &valResultOut, IT1 indBeg, IT1 indEnd, IT2 valBeg, IT2)
Merge values in place, additively, with the same index.
static size_t mergeRowIndicesAndValues(size_t rowLen, local_ordinal_type *cols, impl_scalar_type *vals)
Merge duplicate row indices in the given row, along with their corresponding values.
void getGlobalRowCopy(GlobalOrdinal GlobalRow, nonconst_global_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const override
Fill given arrays with a deep copy of the locally owned entries of the matrix in a given row...
Teuchos::RCP< const map_type > getRangeMap() const override
The range Map of this matrix.
global_size_t getGlobalNumRows() const override
Number of global elements in the row map of this matrix.
size_t insertGlobalIndicesImpl(const local_ordinal_type lclRow, const global_ordinal_type inputGblColInds[], const size_t numInputInds)
Insert global indices, using an input local row index.
std::map< GlobalOrdinal, std::pair< Teuchos::Array< GlobalOrdinal >, Teuchos::Array< Scalar > > > nonlocals_
Nonlocal data added using insertGlobalValues().
static KOKKOS_INLINE_FUNCTION size_t unpackValue(LO &outVal, const char inBuf[])
Unpack the given value from the given output buffer.
void sortAndMergeIndicesAndValues(const bool sorted, const bool merged)
Sort and merge duplicate local column indices in all rows on the calling process, along with their co...
typename device_type::execution_space execution_space
The Kokkos execution space.
void packNew(const Kokkos::DualView< const local_ordinal_type *, buffer_device_type > &exportLIDs, Kokkos::DualView< char *, buffer_device_type > &exports, const Kokkos::DualView< size_t *, buffer_device_type > &numPacketsPerLID, size_t &constantNumPackets) const
Pack this object&#39;s data for an Import or Export.
virtual void insertGlobalValuesImpl(crs_graph_type &graph, RowInfo &rowInfo, const GlobalOrdinal gblColInds[], const impl_scalar_type vals[], const size_t numInputEnt)
Common implementation detail of insertGlobalValues and insertGlobalValuesFiltered.
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
size_t getNumEntriesInLocalRow(local_ordinal_type localRow) const override
Get the number of entries in the given row (local index).
typename Kokkos::ArithTraits< impl_scalar_type >::mag_type mag_type
Type of a norm result.
void putScalar(const Scalar &value)
Set all values in the multivector with the given value.
size_t getNumVectors() const
Number of columns in the multivector.
void getGlobalRowView(GlobalOrdinal GlobalRow, global_inds_host_view_type &indices, values_host_view_type &values) const override
Get a constant, nonpersisting view of a row of this matrix, using global row and column indices...
size_t getLocalLength() const
Local number of rows on the calling process.
Declaration of a function that prints strings from each process.
void setAllToScalar(const Scalar &alpha)
Set all matrix entries equal to alpha.
bool isConstantStride() const
Whether this multivector has constant stride between columns.
Traits class for packing / unpacking data of type T.
void replaceRangeMapAndExporter(const Teuchos::RCP< const map_type > &newRangeMap, Teuchos::RCP< const export_type > &newExporter)
Replace the current Range Map and Export with the given objects.
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const =0
The current number of entries on the calling process in the specified local row.
void scale(const Scalar &alpha)
Scale the matrix&#39;s values: this := alpha*this.
Teuchos::RCP< const map_type > getDomainMap() const override
Returns the Map associated with the domain of this graph.
size_t getLocalNumCols() const override
The number of columns connected to the locally owned rows of this matrix.
Declare and define Tpetra::Details::copyOffsets, an implementation detail of Tpetra (in particular...
void fillLocalGraphAndMatrix(const Teuchos::RCP< Teuchos::ParameterList > &params)
Fill data into the local graph and matrix.
void resumeFill(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Resume operations that may change the values or structure of the matrix.
void leftScale(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x) override
Scale the matrix on the left with the given Vector.
bool noRedundancies_
Whether the graph&#39;s indices are non-redundant (merged) in each row, on this process.
GlobalOrdinal global_ordinal_type
The type of each global index in the matrix.
void padCrsArrays(const RowPtr &rowPtrBeg, const RowPtr &rowPtrEnd, Indices &indices_wdv, const Padding &padding, const int my_rank, const bool verbose)
Determine if the row pointers and indices arrays need to be resized to accommodate new entries...
bool isDistributed() const
Whether this is a globally distributed object.
void reindexColumns(crs_graph_type *const graph, const Teuchos::RCP< const map_type > &newColMap, const Teuchos::RCP< const import_type > &newImport=Teuchos::null, const bool sortEachRow=true)
Reindex the column indices in place, and replace the column Map. Optionally, replace the Import objec...
Teuchos::RCP< const crs_graph_type > getCrsGraph() const
This matrix&#39;s graph, as a CrsGraph.
global_inds_dualv_type::t_host::const_type getGlobalIndsViewHost(const RowInfo &rowinfo) const
Get a const, globally indexed view of the locally owned row myRow, such that rowinfo = getRowInfo(myR...
static bool debug()
Whether Tpetra is in debug mode.
size_t getGlobalMaxNumRowEntries() const override
Maximum number of entries in any row of the matrix, over all processes in the matrix&#39;s communicator...
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y...
void applyNonTranspose(const MV &X_in, MV &Y_in, Scalar alpha, Scalar beta) const
Special case of apply() for mode == Teuchos::NO_TRANS.
Teuchos::RCP< CrsMatrix< T, LocalOrdinal, GlobalOrdinal, Node > > convert() const
Return another CrsMatrix with the same entries, but converted to a different Scalar type T...
Scalar scalar_type
The type of each entry in the matrix.
Allocation information for a locally owned row in a CrsGraph or CrsMatrix.
void swap(CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &matrix)
Swaps the data from *this with the data and maps from crsMatrix.
void fillLocalMatrix(const Teuchos::RCP< Teuchos::ParameterList > &params)
Fill data into the local matrix.
local_inds_wdv_type lclIndsUnpacked_wdv
Local ordinals of column indices for all rows Valid when isLocallyIndexed is true If OptimizedStorage...
void verbosePrintArray(std::ostream &out, const ArrayType &x, const char name[], const size_t maxNumToPrint)
Print min(x.size(), maxNumToPrint) entries of x.
bool isGloballyIndexed() const override
Whether the graph&#39;s column indices are stored as global indices.
void leftScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric, const bool divide=true)
Left-scale a KokkosSparse::CrsMatrix.
Teuchos_Ordinal Array_size_type
Size type for Teuchos Array objects.
void globalAssemble()
Communicate nonlocal contributions to other processes.
void getLocalDiagCopy(Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const override
Get a constant, nonpersisting view of a row of this matrix, using local row and column indices...
size_t findGlobalIndices(const RowInfo &rowInfo, const Teuchos::ArrayView< const global_ordinal_type > &indices, std::function< void(const size_t, const size_t, const size_t)> fun) const
Finds indices in the given row.
KokkosSparse::CrsMatrix< impl_scalar_type, local_ordinal_type, device_type, void, typename local_graph_device_type::size_type > local_matrix_device_type
The specialization of Kokkos::CrsMatrix that represents the part of the sparse matrix on each MPI pro...
virtual LocalOrdinal sumIntoLocalValuesImpl(impl_scalar_type rowVals[], const crs_graph_type &graph, const RowInfo &rowInfo, const LocalOrdinal inds[], const impl_scalar_type newVals[], const LocalOrdinal numElts, const bool atomic=useAtomicUpdatesByDefault)
Implementation detail of sumIntoLocalValues.
void sort(View &view, const size_t &size)
Convenience wrapper for std::sort for host-accessible views.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator, in rank order.
bool isFillActive() const
Whether the matrix is not fill complete.
Teuchos::RCP< MV > importMV_
Column Map MultiVector used in apply().
Declare and define Tpetra::Details::copyConvert, an implementation detail of Tpetra (in particular...
bool isStaticGraph() const
Indicates that the graph is static, so that new entries cannot be added to this matrix.
size_t global_size_t
Global size_t object.
bool hasTransposeApply() const override
Whether apply() allows applying the transpose or conjugate transpose.
void reindexColumns(const Teuchos::RCP< const map_type > &newColMap, const Teuchos::RCP< const import_type > &newImport=Teuchos::null, const bool sortIndicesInEachRow=true)
Reindex the column indices in place, and replace the column Map. Optionally, replace the Import objec...
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
dual_view_type::t_host::const_type getLocalViewHost(Access::ReadOnlyStruct) const
Return a read-only, up-to-date view of this MultiVector&#39;s local data on host. This requires that ther...
size_t getLocalMaxNumRowEntries() const override
Maximum number of entries in any row of the matrix, on this process.
void exportAndFillComplete(Teuchos::RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &destMatrix, const export_type &exporter, const Teuchos::RCP< const map_type > &domainMap=Teuchos::null, const Teuchos::RCP< const map_type > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null) const
Export from this to the given destination matrix, and make the result fill complete.
static KOKKOS_INLINE_FUNCTION size_t packValue(char outBuf[], const LO &inVal)
Pack the given value of type value_type into the given output buffer of bytes (char).
Insert new values that don&#39;t currently exist.
values_dualv_type::t_dev::const_type getValuesViewDevice(const RowInfo &rowinfo) const
Get a const Device view of the locally owned values row myRow, such that rowinfo = getRowInfo(myRow)...
bool isFillComplete() const override
Whether the matrix is fill complete.
bool isSorted() const
Whether graph indices in all rows are known to be sorted.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
The communicator over which the matrix is distributed.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createOneToOne(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &M)
Nonmember constructor for a contiguous Map with user-defined weights and a user-specified, possibly nondefault Kokkos Node type.
void importAndFillComplete(Teuchos::RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &destMatrix, const import_type &importer, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null) const
Import from this to the given destination matrix, and make the result fill complete.
void scale(const Scalar &alpha)
Scale in place: this = alpha*this.
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, nonconst_global_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const =0
Get a copy of the given global row&#39;s entries.
Teuchos::RCP< const map_type > rowMap_
The Map describing the distribution of rows of the graph.
local_matrix_device_type getLocalMatrixDevice() const
The local sparse matrix.
num_row_entries_type k_numRowEntries_
The number of local entries in each locally owned row.
global_ordinal_type getGlobalElement(local_ordinal_type localIndex) const
The global index corresponding to the given local index.
bool isNodeLocalElement(local_ordinal_type localIndex) const
Whether the given local index is valid for this Map on the calling process.
Functions for manipulating CRS arrays.
Kokkos::View< size_t *, Kokkos::LayoutLeft, device_type >::HostMirror num_row_entries_type
Row offsets for &quot;1-D&quot; storage.
GlobalOrdinal getIndexBase() const override
The index base for global indices for this matrix.
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
void unpackAndCombineIntoCrsArrays(const CrsGraph< LO, GO, NT > &sourceGraph, const Teuchos::ArrayView< const LO > &importLIDs, const Teuchos::ArrayView< const typename CrsGraph< LO, GO, NT >::packet_type > &imports, const Teuchos::ArrayView< const size_t > &numPacketsPerLID, const size_t constantNumPackets, const CombineMode combineMode, const size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs, size_t TargetNumRows, size_t TargetNumNonzeros, const int MyTargetPID, const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< GO > &CRS_colind, const Teuchos::ArrayView< const int > &SourcePids, Teuchos::Array< int > &TargetPids)
unpackAndCombineIntoCrsArrays
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X...
Teuchos::RCP< MV > getColumnMapMultiVector(const MV &X_domainMap, const bool force=false) const
Create a (or fetch a cached) column Map MultiVector.
#define TPETRA_ABUSE_WARNING(throw_exception_test, Exception, msg)
Handle an abuse warning, according to HAVE_TPETRA_THROW_ABUSE_WARNINGS and HAVE_TPETRA_PRINT_ABUSE_WA...
bool isNodeGlobalElement(global_ordinal_type globalIndex) const
Whether the given global index is owned by this Map on the calling process.
void packCrsMatrixNew(const CrsMatrix< ST, LO, GO, NT > &sourceMatrix, Kokkos::DualView< char *, typename DistObject< char, LO, GO, NT >::buffer_device_type > &exports, const Kokkos::DualView< size_t *, typename DistObject< char, LO, GO, NT >::buffer_device_type > &numPacketsPerLID, const Kokkos::DualView< const LO *, typename DistObject< char, LO, GO, NT >::buffer_device_type > &exportLIDs, size_t &constantNumPackets)
Pack specified entries of the given local sparse matrix for communication, for &quot;new&quot; DistObject inter...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const override
Print this object with the given verbosity level to the given output stream.
Teuchos::RCP< const RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const override
This matrix&#39;s graph, as a RowGraph.
static bool verbose()
Whether Tpetra is in verbose mode.
CombineMode
Rule for combining data in an Import or Export.
Sum new values.
void unpackAndCombine(const Kokkos::DualView< const local_ordinal_type *, buffer_device_type > &importLIDs, Kokkos::DualView< char *, buffer_device_type > imports, Kokkos::DualView< size_t *, buffer_device_type > numPacketsPerLID, const size_t constantNumPackets, const CombineMode CM) override
Unpack the imported column indices and values, and combine into matrix.
bool isFillComplete() const override
Whether fillComplete() has been called and the graph is in compute mode.
bool haveGlobalConstants() const
Returns true if globalConstants have been computed; false otherwise.
bool isGloballyIndexed() const override
Whether the matrix is globally indexed on the calling process.
RowInfo getRowInfoFromGlobalRowIndex(const global_ordinal_type gblRow) const
Get information about the locally owned row with global index gblRow.
LocalOrdinal sumIntoGlobalValues(const GlobalOrdinal globalRow, const Teuchos::ArrayView< const GlobalOrdinal > &cols, const Teuchos::ArrayView< const Scalar > &vals, const bool atomic=useAtomicUpdatesByDefault)
Sum into one or more sparse matrix entries, using global indices.
Utility functions for packing and unpacking sparse matrix entries.
void copyConvert(const OutputViewType &dst, const InputViewType &src)
Copy values from the 1-D Kokkos::View src, to the 1-D Kokkos::View dst, of the same length...
virtual Teuchos::RCP< RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > add(const Scalar &alpha, const RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta, const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > &params) const override
Implementation of RowMatrix::add: return alpha*A + beta*this.
bool fillComplete_
Whether the matrix is fill complete.
local_ordinal_type replaceGlobalValues(const global_ordinal_type globalRow, const Kokkos::View< const global_ordinal_type *, Kokkos::AnonymousSpace > &inputInds, const Kokkos::View< const impl_scalar_type *, Kokkos::AnonymousSpace > &inputVals)
Replace one or more entries&#39; values, using global indices.
Replace old value with maximum of magnitudes of old and new values.
virtual LocalOrdinal sumIntoGlobalValuesImpl(impl_scalar_type rowVals[], const crs_graph_type &graph, const RowInfo &rowInfo, const GlobalOrdinal inds[], const impl_scalar_type newVals[], const LocalOrdinal numElts, const bool atomic=useAtomicUpdatesByDefault)
Implementation detail of sumIntoGlobalValues.
Abstract base class for objects that can be the source of an Import or Export operation.
local_ordinal_type sumIntoLocalValues(const local_ordinal_type localRow, const Kokkos::View< const local_ordinal_type *, Kokkos::AnonymousSpace > &inputInds, const Kokkos::View< const impl_scalar_type *, Kokkos::AnonymousSpace > &inputVals, const bool atomic=useAtomicUpdatesByDefault)
Sum into one or more sparse matrix entries, using local row and column indices.
typename Node::device_type device_type
The Kokkos device type.
size_t getNumEntriesInLocalRow(local_ordinal_type localRow) const override
Number of entries in the sparse matrix in the given local row, on the calling (MPI) process...
static LocalMapType::local_ordinal_type getDiagCopyWithoutOffsets(const DiagType &D, const LocalMapType &rowMap, const LocalMapType &colMap, const CrsMatrixType &A)
Given a locally indexed, local sparse matrix, and corresponding local row and column Maps...
Teuchos::RCP< MV > getRowMapMultiVector(const MV &Y_rangeMap, const bool force=false) const
Create a (or fetch a cached) row Map MultiVector.
std::string description() const override
A one-line description of this object.
void getLocalDiagOffsets(Teuchos::ArrayRCP< size_t > &offsets) const
Get offsets of the diagonal entries in the matrix.
void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const override
Compute a sparse matrix-MultiVector multiply.
size_t getLocalNumEntries() const override
The local number of entries in this matrix.
LO getLocalDiagCopyWithoutOffsetsNotFillComplete(::Tpetra::Vector< SC, LO, GO, NT > &diag, const ::Tpetra::RowMatrix< SC, LO, GO, NT > &A, const bool debug=false)
Given a locally indexed, global sparse matrix, extract the matrix&#39;s diagonal entries into a Tpetra::V...
Replace existing values with new values.
void fillComplete(const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Tell the matrix that you are done changing its structure or values, and that you are ready to do comp...
Teuchos::RCP< const map_type > getRangeMap() const override
Returns the Map associated with the domain of this graph.
dual_view_type::t_dev::const_type getLocalViewDevice(Access::ReadOnlyStruct) const
Return a read-only, up-to-date view of this MultiVector&#39;s local data on device. This requires that th...
Replace old values with zero.
const row_ptrs_host_view_type & getRowPtrsUnpackedHost() const
Get the unpacked row pointers on host. Lazily make a copy from device.
std::string combineModeToString(const CombineMode combineMode)
Human-readable string representation of the given CombineMode.
void insertGlobalValues(const GlobalOrdinal globalRow, const Teuchos::ArrayView< const GlobalOrdinal > &cols, const Teuchos::ArrayView< const Scalar > &vals)
Insert one or more entries into the matrix, using global column indices.
static size_t rowImbalanceThreshold()
Threshold for deciding if a local matrix is &quot;imbalanced&quot; in the number of entries per row...
bool isLocallyComplete() const
Is this Export or Import locally complete?
virtual LocalOrdinal replaceLocalValuesImpl(impl_scalar_type rowVals[], const crs_graph_type &graph, const RowInfo &rowInfo, const LocalOrdinal inds[], const impl_scalar_type newVals[], const LocalOrdinal numElts)
Implementation detail of replaceLocalValues.
Declaration and definition of Tpetra::Details::leftScaleLocalCrsMatrix.
RowInfo getRowInfo(const local_ordinal_type myRow) const
Get information about the locally owned row with local index myRow.
values_dualv_type::t_dev getValuesViewDeviceNonConst(const RowInfo &rowinfo)
Get a non-const Device view of the locally owned values row myRow, such that rowinfo = getRowInfo(myR...
std::string dualViewStatusToString(const DualViewType &dv, const char name[])
Return the status of the given Kokkos::DualView, as a human-readable string.
virtual void removeEmptyProcessesInPlace(const Teuchos::RCP< const map_type > &newMap) override
Remove processes owning zero rows from the Maps and their communicator.
virtual bool checkSizes(const SrcDistObject &source) override
Compare the source and target (this) objects for compatibility.
local_map_type getLocalMap() const
Get the local Map for Kokkos kernels.
void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2)
Sort the first array, and apply the resulting permutation to the second array.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
typename row_matrix_type::impl_scalar_type impl_scalar_type
The type used internally in place of Scalar.
size_t unpackAndCombineWithOwningPIDsCount(const CrsGraph< LO, GO, NT > &sourceGraph, const Teuchos::ArrayView< const LO > &importLIDs, const Teuchos::ArrayView< const typename CrsGraph< LO, GO, NT >::packet_type > &imports, const Teuchos::ArrayView< const size_t > &numPacketsPerLID, size_t constantNumPackets, CombineMode combineMode, size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs)
Special version of Tpetra::Details::unpackCrsGraphAndCombine that also unpacks owning process ranks...
A parallel distribution of indices over processes.
void getLocalRowCopy(LocalOrdinal LocalRow, nonconst_local_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const override
Fill given arrays with a deep copy of the locally owned entries of the matrix in a given row...
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, const CombineMode CM, const bool restrictedMode=false)
Export data into this object using an Export object (&quot;forward mode&quot;).
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this matrix.
Teuchos::ArrayView< typename DualViewType::t_dev::value_type > getArrayViewFromDualView(const DualViewType &x)
Get a Teuchos::ArrayView which views the host Kokkos::View of the input 1-D Kokkos::DualView.
static KOKKOS_INLINE_FUNCTION size_t packValueCount(const LO &)
Number of bytes required to pack or unpack the given value of type value_type.
void insertLocalValues(const LocalOrdinal localRow, const Teuchos::ArrayView< const LocalOrdinal > &cols, const Teuchos::ArrayView< const Scalar > &vals, const CombineMode CM=ADD)
Insert one or more entries into the matrix, using local column indices.
Internal functions and macros designed for use with Tpetra::Import and Tpetra::Export objects...
Details::EStorageStatus storageStatus_
Status of the matrix&#39;s storage, when not in a fill-complete state.
A read-only, row-oriented interface to a sparse matrix.
void rightScale(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x) override
Scale the matrix on the right with the given Vector.
void replaceDomainMap(const Teuchos::RCP< const map_type > &newDomainMap)
Replace the current domain Map with the given objects.
Scalar operator()(const Scalar &x, const Scalar &y)
Return the maximum of the magnitudes (absolute values) of x and y.
local_ordinal_type getLocalElement(global_ordinal_type globalIndex) const
The local index corresponding to the given global index.
void getLocalRowView(LocalOrdinal LocalRow, local_inds_host_view_type &indices, values_host_view_type &values) const override
Get a constant view of a row of this matrix, using local row and column indices.
values_dualv_type::t_host::const_type getValuesViewHost(const RowInfo &rowinfo) const
Get a const Host view of the locally owned values row myRow, such that rowinfo = getRowInfo(myRow).
bool isLocallyIndexed() const override
Whether the graph&#39;s column indices are stored as local indices.
A distributed dense vector.
Declaration of Tpetra::Details::iallreduce.
void reduce()
Sum values of a locally replicated multivector across all processes.
Declaration and definition of Tpetra::Details::castAwayConstDualView, an implementation detail of Tpe...
std::shared_ptr< CommRequest > iallreduce(const InputViewType &sendbuf, const OutputViewType &recvbuf, const ::Teuchos::EReductionType op, const ::Teuchos::Comm< int > &comm)
Nonblocking all-reduce, for either rank-1 or rank-0 Kokkos::View objects.
void applyTranspose(const MV &X_in, MV &Y_in, const Teuchos::ETransp mode, Scalar alpha, Scalar beta) const
Special case of apply() for mode != Teuchos::NO_TRANS.
OffsetsViewType::non_const_value_type computeOffsetsFromCounts(const ExecutionSpace &execSpace, const OffsetsViewType &ptr, const CountsViewType &counts)
Compute offsets from counts.
void allocateValues(ELocalGlobal lg, GraphAllocationStatus gas, const bool verbose)
Allocate values (and optionally indices) using the Node.
Kokkos::DualView< ValueType *, DeviceType > castAwayConstDualView(const Kokkos::DualView< const ValueType *, DeviceType > &input_dv)
Cast away const-ness of a 1-D Kokkos::DualView.
void expertStaticFillComplete(const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< const import_type > &importer=Teuchos::null, const Teuchos::RCP< const export_type > &exporter=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Perform a fillComplete on a matrix that already has data.
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const override
Number of entries in the sparse matrix in the given global row, on the calling (MPI) process...
static size_t verbosePrintCountThreshold()
Number of entries below which arrays, lists, etc. will be printed in debug mode.
local_matrix_device_type::values_type::const_type getLocalValuesDevice(Access::ReadOnlyStruct s) const
Get the Kokkos local values on device, read only.
void setAllValues(const typename local_graph_device_type::row_map_type &ptr, const typename local_graph_device_type::entries_type::non_const_type &ind, const typename local_matrix_device_type::values_type &val)
Set the local matrix using three (compressed sparse row) arrays.
bool isLocallyIndexed() const override
Whether the matrix is locally indexed on the calling process.
Teuchos::RCP< const map_type > getColMap() const override
The Map that describes the column distribution in this matrix.
local_ordinal_type replaceLocalValues(const local_ordinal_type localRow, const Kokkos::View< const local_ordinal_type *, Kokkos::AnonymousSpace > &inputInds, const Kokkos::View< const impl_scalar_type *, Kokkos::AnonymousSpace > &inputVals)
Replace one or more entries&#39; values, using local row and column indices.
Declaration and definition of Tpetra::Details::rightScaleLocalCrsMatrix.
Declaration and definition of Tpetra::Details::getEntryOnHost.
void packCrsMatrixWithOwningPIDs(const CrsMatrix< ST, LO, GO, NT > &sourceMatrix, Kokkos::DualView< char *, typename DistObject< char, LO, GO, NT >::buffer_device_type > &exports_dv, const Teuchos::ArrayView< size_t > &numPacketsPerLID, const Teuchos::ArrayView< const LO > &exportLIDs, const Teuchos::ArrayView< const int > &sourcePIDs, size_t &constantNumPackets)
Pack specified entries of the given local sparse matrix for communication.
void replaceRangeMap(const Teuchos::RCP< const map_type > &newRangeMap)
Replace the current range Map with the given objects.
std::shared_ptr< local_multiply_op_type > getLocalMultiplyOperator() const
The local sparse matrix operator (a wrapper of getLocalMatrixDevice() that supports local matrix-vect...
Teuchos::RCP< const map_type > colMap_
The Map describing the distribution of columns of the graph.
LocalOrdinal local_ordinal_type
The type of each local index in the matrix.
mag_type getFrobeniusNorm() const override
Compute and return the Frobenius norm of the matrix.
std::unique_ptr< std::string > createPrefix(const int myRank, const char prefix[])
Create string prefix for each line of verbose output.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const =0
The Map that describes the distribution of rows over processes.
Accumulate new values into existing values (may not be supported in all classes)
void localApply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, const Teuchos::ETransp mode=Teuchos::NO_TRANS, const Scalar &alpha=Teuchos::ScalarTraits< Scalar >::one(), const Scalar &beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Compute the local part of a sparse matrix-(Multi)Vector multiply.
void rightScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric, const bool divide=true)
Right-scale a KokkosSparse::CrsMatrix.
bool isStorageOptimized() const
Returns true if storage has been optimized.
Description of Tpetra&#39;s behavior.
virtual void copyAndPermute(const SrcDistObject &source, const size_t numSameIDs, const Kokkos::DualView< const local_ordinal_type *, buffer_device_type > &permuteToLIDs, const Kokkos::DualView< const local_ordinal_type *, buffer_device_type > &permuteFromLIDs, const CombineMode CM) override
values_dualv_type::t_host getValuesViewHostNonConst(const RowInfo &rowinfo)
Get a non-const Host view of the locally owned values row myRow, such that rowinfo = getRowInfo(myRow...
Functions that wrap Kokkos::create_mirror_view, in order to avoid deep copies when not necessary...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra&#39;s behavior.
size_t getLocalNumRows() const override
The number of matrix rows owned by the calling process.
Teuchos::RCP< MV > exportMV_
Row Map MultiVector used in apply().
Teuchos::RCP< const map_type > getRowMap() const override
The Map that describes the row distribution in this matrix.
global_size_t getGlobalNumEntries() const override
The global number of entries in this matrix.