Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_TpetraRowMatrixAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_
51 #define _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_
52 
55 #include <Zoltan2_StridedData.hpp>
56 
57 #include <Tpetra_RowMatrix.hpp>
58 
59 #include <vector>
60 
61 namespace Zoltan2 {
62 
64 
77 template <typename User, typename UserCoord = User>
78 class TpetraRowMatrixAdapter : public MatrixAdapter<User, UserCoord> {
79 public:
80 
81 
82 #ifndef DOXYGEN_SHOULD_SKIP_THIS
83  using scalar_t = typename InputTraits<User>::scalar_t;
84  using offset_t = typename InputTraits<User>::offset_t;
85  using lno_t = typename InputTraits<User>::lno_t;
86  using gno_t = typename InputTraits<User>::gno_t;
87  using part_t = typename InputTraits<User>::part_t;
88  using node_t = typename InputTraits<User>::node_t;
89  using device_t = typename node_t::device_type;
90  using host_t = typename Kokkos::HostSpace::memory_space;
91  using user_t = User;
92  using userCoord_t = UserCoord;
93 
94  using Base = MatrixAdapter<User, UserCoord>;
95 #endif
96 
102  TpetraRowMatrixAdapter(const RCP<const User> &inmatrix,
103  int nWeightsPerRow = 0);
104 
117  void setWeights(const scalar_t *weightVal, int stride, int idx = 0);
118 
128  void setWeightsDevice(typename Base::ConstWeightsDeviceView1D val, int idx);
129 
139  void setWeightsHost(typename Base::ConstWeightsHostView1D val, int idx);
140 
156  void setRowWeights(const scalar_t *weightVal, int stride, int idx = 0);
157 
170  void setRowWeightsDevice(typename Base::ConstWeightsDeviceView1D val,
171  int idx);
172 
185  void setRowWeightsHost(typename Base::ConstWeightsHostView1D val,
186  int idx);
187 
188 
194  void setWeightIsDegree(int idx);
195 
201  void setRowWeightIsNumberOfNonZeros(int idx);
202 
204 // The MatrixAdapter Interface
206 
207  size_t getLocalNumRows() const;
208 
209  size_t getLocalNumColumns() const;
210 
211  size_t getLocalNumEntries() const;
212 
213  bool CRSViewAvailable() const;
214 
215  void getRowIDsView(const gno_t *&rowIds) const override;
216 
217  void getRowIDsHostView(
218  typename Base::ConstIdsHostView &rowIds) const override;
219 
220  void getRowIDsDeviceView(
221  typename Base::ConstIdsDeviceView &rowIds) const override;
222 
223  void getCRSView(ArrayRCP<const offset_t> &offsets,
224  ArrayRCP<const gno_t> &colIds) const;
225 
226  void getCRSHostView(
227  typename Base::ConstOffsetsHostView &offsets,
228  typename Base::ConstIdsHostView &colIds) const override;
229 
230  void getCRSDeviceView(
231  typename Base::ConstOffsetsDeviceView &offsets,
232  typename Base::ConstIdsDeviceView &colIds) const override;
233 
234  void getCRSView(ArrayRCP<const offset_t> &offsets,
235  ArrayRCP<const gno_t> &colIds,
236  ArrayRCP<const scalar_t> &values) const;
237 
238  void getCRSHostView(
239  typename Base::ConstOffsetsHostView &offsets,
240  typename Base::ConstIdsHostView &colIds,
241  typename Base::ConstScalarsHostView &values) const override;
242 
243  void getCRSDeviceView(
244  typename Base::ConstOffsetsDeviceView &offsets,
245  typename Base::ConstIdsDeviceView &colIds,
246  typename Base::ConstScalarsDeviceView &values) const override;
247 
248  int getNumWeightsPerRow() const;
249 
250  void getRowWeightsView(const scalar_t *&weights, int &stride,
251  int idx = 0) const;
252 
253  void getRowWeightsDeviceView(typename Base::WeightsDeviceView1D &weights,
254  int idx = 0) const;
255 
257  typename Base::WeightsDeviceView &weights) const override;
258 
259  void getRowWeightsHostView(typename Base::WeightsHostView1D &weights,
260  int idx = 0) const;
261 
263  typename Base::WeightsHostView &weights) const override;
264 
265  bool useNumNonzerosAsRowWeight(int idx) const;
266 
267  template <typename Adapter>
269  const User &in, User *&out,
270  const PartitioningSolution<Adapter> &solution) const;
271 
272  template <typename Adapter>
274  const User &in, RCP<User> &out,
275  const PartitioningSolution<Adapter> &solution) const;
276 
277 protected:
278  // Used by TpetraCrsMatrixAdapter
279  TpetraRowMatrixAdapter(int nWeightsPerRow,
280  const RCP<const User> &inmatrix)
281  : matrix_(inmatrix), nWeightsPerRow_(nWeightsPerRow) {}
282 
283  RCP<const User> matrix_;
284 
285  ArrayRCP<offset_t> offset_;
286  ArrayRCP<gno_t> columnIds_;
287  ArrayRCP<scalar_t> values_;
288 
289  typename Base::ConstOffsetsHostView offsHost_;
290  typename Base::ConstIdsHostView colIdsHost_;
291  typename Base::ScalarsHostView valuesHost_;
292 
293  typename Base::ConstOffsetsDeviceView offsDevice_;
294  typename Base::ConstIdsDeviceView colIdsDevice_;
295  typename Base::ScalarsDeviceView valuesDevice_;
296 
298  ArrayRCP<StridedData<lno_t, scalar_t>> rowWeights_;
299  typename Base::WeightsDeviceView rowWeightsDevice_;
300  Kokkos::View<bool *, host_t> numNzWeight_;
301 
303 
304  virtual RCP<User> doMigration(const User &from, size_t numLocalRows,
305  const gno_t *myNewRows) const;
306 };
307 
309 // Definitions
311 
312 template <typename User, typename UserCoord>
314  const RCP<const User> &inmatrix, int nWeightsPerRow):
315  matrix_(inmatrix), offset_(), columnIds_(),
316  nWeightsPerRow_(nWeightsPerRow), rowWeights_(),
317  mayHaveDiagonalEntries(true) {
318  using strided_t = StridedData<lno_t, scalar_t>;
319  using localInds_t = typename User::nonconst_local_inds_host_view_type;
320  using localVals_t = typename User::nonconst_values_host_view_type;
321 
322  const auto nrows = matrix_->getLocalNumRows();
323  const auto nnz = matrix_->getLocalNumEntries();
324  auto maxNumEntries = matrix_->getLocalMaxNumRowEntries();
325 
326  // Unfortunately we have to copy the offsets, column Ids, and vals
327  // because column Ids are not usually stored in row id order.
328 
329  colIdsHost_ = typename Base::ConstIdsHostView("colIdsHost_", nnz);
330  offsHost_ = typename Base::ConstOffsetsHostView("offsHost_", nrows + 1);
331  valuesHost_ = typename Base::ScalarsHostView("valuesHost_", nnz);
332 
333  localInds_t localColInds("localColInds", maxNumEntries);
334  localVals_t localVals("localVals", maxNumEntries);
335 
336  for (size_t r = 0; r < nrows; r++) {
337  size_t numEntries = 0;
338  matrix_->getLocalRowCopy(r, localColInds, localVals, numEntries); // Diff from CrsGraph
339 
340  offsHost_(r + 1) = offsHost_(r) + numEntries;
341  for (offset_t e = offsHost_(r), i = 0; e < offsHost_(r + 1); e++) {
342  colIdsHost_(e) = matrix_->getColMap()->getGlobalElement(localColInds(i++));
343  }
344  for (size_t j = 0; j < nnz; j++) {
345  valuesHost_(r) = localVals[j];
346  }
347  }
348  offsDevice_ = Kokkos::create_mirror_view_and_copy(
349  typename Base::device_t(), offsHost_);
350  colIdsDevice_ = Kokkos::create_mirror_view_and_copy(
351  typename Base::device_t(), colIdsHost_);
352  valuesDevice_ = Kokkos::create_mirror_view_and_copy(
353  typename Base::device_t(), valuesHost_);
354 
355  if (nWeightsPerRow_ > 0) {
356  rowWeights_ =
357  arcp(new strided_t[nWeightsPerRow_], 0, nWeightsPerRow_, true);
358 
359  rowWeightsDevice_ = typename Base::WeightsDeviceView(
360  "rowWeightsDevice_", nrows, nWeightsPerRow_);
361 
362  numNzWeight_ = Kokkos::View<bool *, host_t>(
363  "numNzWeight_", nWeightsPerRow_);
364 
365  for (int i = 0; i < nWeightsPerRow_; ++i) {
366  numNzWeight_(i) = false;
367  }
368  }
369 }
370 
372 template <typename User, typename UserCoord>
374  const scalar_t *weightVal, int stride, int idx) {
375  if (this->getPrimaryEntityType() == MATRIX_ROW)
376  setRowWeights(weightVal, stride, idx);
377  else {
378  // TODO: Need to allow weights for columns and/or nonzeros
379  std::ostringstream emsg;
380  emsg << __FILE__ << "," << __LINE__
381  << " error: setWeights not yet supported for"
382  << " columns or nonzeros." << std::endl;
383  throw std::runtime_error(emsg.str());
384  }
385 }
386 
388 template <typename User, typename UserCoord>
390  typename Base::ConstWeightsDeviceView1D val, int idx) {
391  if (this->getPrimaryEntityType() == MATRIX_ROW)
392  setRowWeightsDevice(val, idx);
393  else {
394  // TODO: Need to allow weights for columns and/or nonzeros
395  std::ostringstream emsg;
396  emsg << __FILE__ << "," << __LINE__
397  << " error: setWeights not yet supported for"
398  << " columns or nonzeros." << std::endl;
399  throw std::runtime_error(emsg.str());
400  }
401 }
402 
404 template <typename User, typename UserCoord>
406  typename Base::ConstWeightsHostView1D val, int idx) {
407  if (this->getPrimaryEntityType() == MATRIX_ROW)
408  setRowWeightsHost(val, idx);
409  else {
410  // TODO: Need to allow weights for columns and/or nonzeros
411  std::ostringstream emsg;
412  emsg << __FILE__ << "," << __LINE__
413  << " error: setWeights not yet supported for"
414  << " columns or nonzeros." << std::endl;
415  throw std::runtime_error(emsg.str());
416  }
417 }
418 
420 template <typename User, typename UserCoord>
422  const scalar_t *weightVal, int stride, int idx) {
423  typedef StridedData<lno_t, scalar_t> input_t;
424  AssertCondition((idx >= 0) and (idx < nWeightsPerRow_),
425  "Invalid row weight index: " + std::to_string(idx));
426 
427  size_t nrows = getLocalNumRows();
428  ArrayRCP<const scalar_t> weightV(weightVal, 0, nrows * stride, false);
429  rowWeights_[idx] = input_t(weightV, stride);
430 }
431 
433 template <typename User, typename UserCoord>
435  typename Base::ConstWeightsDeviceView1D weights, int idx) {
436 
437  AssertCondition((idx >= 0) and (idx < nWeightsPerRow_),
438  "Invalid row weight index: " + std::to_string(idx));
439 
440  Kokkos::parallel_for(
441  rowWeightsDevice_.extent(0), KOKKOS_CLASS_LAMBDA(const int rowID) {
442  rowWeightsDevice_(rowID, idx) = weights(rowID);
443  });
444 
445  Kokkos::fence();
446 }
447 
449 template <typename User, typename UserCoord>
451  typename Base::ConstWeightsHostView1D weightsHost, int idx) {
452  AssertCondition((idx >= 0) and (idx < nWeightsPerRow_),
453  "Invalid row weight index: " + std::to_string(idx));
454 
455  auto weightsDevice = Kokkos::create_mirror_view_and_copy(
456  typename Base::device_t(), weightsHost);
457 
458  setRowWeightsDevice(weightsDevice, idx);
459 }
460 
462 template <typename User, typename UserCoord>
464  if (this->getPrimaryEntityType() == MATRIX_ROW)
465  setRowWeightIsNumberOfNonZeros(idx);
466  else {
467  // TODO: Need to allow weights for columns and/or nonzeros
468  std::ostringstream emsg;
469  emsg << __FILE__ << "," << __LINE__
470  << " error: setWeightIsNumberOfNonZeros not yet supported for"
471  << " columns" << std::endl;
472  throw std::runtime_error(emsg.str());
473  }
474 }
475 
477 template <typename User, typename UserCoord>
479  int idx) {
480  if (idx < 0 || idx >= nWeightsPerRow_) {
481  std::ostringstream emsg;
482  emsg << __FILE__ << ":" << __LINE__ << " Invalid row weight index " << idx
483  << std::endl;
484  throw std::runtime_error(emsg.str());
485  }
486 
487  numNzWeight_(idx) = true;
488 }
489 
491 template <typename User, typename UserCoord>
493  return matrix_->getLocalNumRows();
494 }
495 
497 template <typename User, typename UserCoord>
499  return matrix_->getLocalNumCols();
500 }
501 
503 template <typename User, typename UserCoord>
505  return matrix_->getLocalNumEntries();
506 }
507 
509 template <typename User, typename UserCoord>
511 
513 template <typename User, typename UserCoord>
515  ArrayView<const gno_t> rowView = matrix_->getRowMap()->getLocalElementList();
516  rowIds = rowView.getRawPtr();
517 }
518 
520 template <typename User, typename UserCoord>
522  typename Base::ConstIdsHostView &rowIds) const {
523  auto idsDevice = matrix_->getRowMap()->getMyGlobalIndices();
524  auto tmpIds = typename Base::IdsHostView("", idsDevice.extent(0));
525 
526  Kokkos::deep_copy(tmpIds, idsDevice);
527 
528  rowIds = tmpIds;
529 }
530 
532 template <typename User, typename UserCoord>
534  typename Base::ConstIdsDeviceView &rowIds) const {
535 
536  auto idsDevice = matrix_->getRowMap()->getMyGlobalIndices();
537  auto tmpIds = typename Base::IdsDeviceView("", idsDevice.extent(0));
538 
539  Kokkos::deep_copy(tmpIds, idsDevice);
540 
541  rowIds = tmpIds;
542 }
543 
545 template <typename User, typename UserCoord>
546 void TpetraRowMatrixAdapter<User, UserCoord>::getCRSView(ArrayRCP<const offset_t> &offsets,
547  ArrayRCP<const gno_t> &colIds) const {
548  offsets = offset_;
549  colIds = columnIds_;
550 }
551 
553 template <typename User, typename UserCoord>
555  typename Base::ConstOffsetsHostView &offsets,
556  typename Base::ConstIdsHostView &colIds) const {
557  auto hostOffsets = Kokkos::create_mirror_view(offsDevice_);
558  Kokkos::deep_copy(hostOffsets, offsDevice_);
559  offsets = hostOffsets;
560 
561  auto hostColIds = Kokkos::create_mirror_view(colIdsDevice_);
562  Kokkos::deep_copy(hostColIds, colIdsDevice_);
563  colIds = hostColIds;
564 }
565 
567 template <typename User, typename UserCoord>
569  typename Base::ConstOffsetsDeviceView &offsets,
570  typename Base::ConstIdsDeviceView &colIds) const {
571  offsets = offsDevice_;
572  colIds = colIdsDevice_;
573 }
574 
576 template <typename User, typename UserCoord>
577 void TpetraRowMatrixAdapter<User, UserCoord>::getCRSView(ArrayRCP<const offset_t> &offsets,
578  ArrayRCP<const gno_t> &colIds,
579  ArrayRCP<const scalar_t> &values) const {
580  offsets = offset_;
581  colIds = columnIds_;
582  values = values_;
583 }
584 
586 template <typename User, typename UserCoord>
588  typename Base::ConstOffsetsHostView &offsets,
589  typename Base::ConstIdsHostView &colIds,
590  typename Base::ConstScalarsHostView &values) const {
591  auto hostOffsets = Kokkos::create_mirror_view(offsDevice_);
592  Kokkos::deep_copy(hostOffsets, offsDevice_);
593  offsets = hostOffsets;
594 
595  auto hostColIds = Kokkos::create_mirror_view(colIdsDevice_);
596  Kokkos::deep_copy(hostColIds, colIdsDevice_);
597  colIds = hostColIds;
598 
599  auto hostValues = Kokkos::create_mirror_view(valuesDevice_);
600  Kokkos::deep_copy(hostValues, valuesDevice_);
601  values = hostValues;
602 }
603 
605 template <typename User, typename UserCoord>
607  typename Base::ConstOffsetsDeviceView &offsets,
608  typename Base::ConstIdsDeviceView &colIds,
609  typename Base::ConstScalarsDeviceView &values) const {
610  offsets = offsDevice_;
611  colIds = colIdsDevice_;
612  values = valuesDevice_;
613 }
614 
616 template <typename User, typename UserCoord>
618 
620 template <typename User, typename UserCoord>
622  int idx) const {
623  if (idx < 0 || idx >= nWeightsPerRow_) {
624  std::ostringstream emsg;
625  emsg << __FILE__ << ":" << __LINE__ << " Invalid row weight index "
626  << idx << std::endl;
627  throw std::runtime_error(emsg.str());
628  }
629 
630  size_t length;
631  rowWeights_[idx].getStridedList(length, weights, stride);
632 }
633 
635 template <typename User, typename UserCoord>
637  typename Base::WeightsDeviceView1D &weights, int idx) const {
638  AssertCondition((idx >= 0) and (idx < nWeightsPerRow_),
639  "Invalid row weight index.");
640 
641  const auto size = rowWeightsDevice_.extent(0);
642  weights = typename Base::WeightsDeviceView1D("weights", size);
643 
644  Kokkos::parallel_for(
645  size, KOKKOS_CLASS_LAMBDA(const int id) {
646  weights(id) = rowWeightsDevice_(id, idx);
647  });
648 
649  Kokkos::fence();
650 }
651 
653 template <typename User, typename UserCoord>
655  typename Base::WeightsDeviceView &weights) const {
656 
657  weights = rowWeightsDevice_;
658 }
659 
661 template <typename User, typename UserCoord>
663  typename Base::WeightsHostView1D &weights, int idx) const {
664  AssertCondition((idx >= 0) and (idx < nWeightsPerRow_),
665  "Invalid row weight index.");
666 
667  auto weightsDevice = typename Base::WeightsDeviceView1D(
668  "weights", rowWeightsDevice_.extent(0));
669  getRowWeightsDeviceView(weightsDevice, idx);
670 
671  weights = Kokkos::create_mirror_view(weightsDevice);
672  Kokkos::deep_copy(weights, weightsDevice);
673 }
674 
676 template <typename User, typename UserCoord>
678  typename Base::WeightsHostView &weights) const {
679 
680  weights = Kokkos::create_mirror_view(rowWeightsDevice_);
681  Kokkos::deep_copy(weights, rowWeightsDevice_);
682 }
683 
685 template <typename User, typename UserCoord>
686 bool TpetraRowMatrixAdapter<User, UserCoord>::useNumNonzerosAsRowWeight(int idx) const { return numNzWeight_[idx]; }
687 
689 template <typename User, typename UserCoord>
690 template <typename Adapter>
692  const User &in, User *&out,
693  const PartitioningSolution<Adapter> &solution) const {
694  // Get an import list (rows to be received)
695  size_t numNewRows;
696  ArrayRCP<gno_t> importList;
697  try {
698  numNewRows =
699  Zoltan2::getImportList<Adapter, TpetraRowMatrixAdapter<User, UserCoord>>(
700  solution, this, importList);
701  }
703 
704  // Move the rows, creating a new matrix.
705  RCP<User> outPtr = doMigration(in, numNewRows, importList.getRawPtr());
706  out = outPtr.get();
707  outPtr.release();
708 }
709 
711 template <typename User, typename UserCoord>
712 template <typename Adapter>
714  const User &in, RCP<User> &out,
715  const PartitioningSolution<Adapter> &solution) const {
716  // Get an import list (rows to be received)
717  size_t numNewRows;
718  ArrayRCP<gno_t> importList;
719  try {
720  numNewRows =
721  Zoltan2::getImportList<Adapter, TpetraRowMatrixAdapter<User, UserCoord>>(
722  solution, this, importList);
723  }
725 
726  // Move the rows, creating a new matrix.
727  out = doMigration(in, numNewRows, importList.getRawPtr());
728 }
729 
731 template <typename User, typename UserCoord>
733  const User &from, size_t numLocalRows, const gno_t *myNewRows) const {
734  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
735  typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> tcrsmatrix_t;
736 
737  // We cannot create a Tpetra::RowMatrix, unless the underlying type is
738  // something we know (like Tpetra::CrsMatrix).
739  // If the underlying type is something different, the user probably doesn't
740  // want a Tpetra::CrsMatrix back, so we throw an error.
741 
742  // Try to cast "from" matrix to a TPetra::CrsMatrix
743  // If that fails we throw an error.
744  // We could cast as a ref which will throw std::bad_cast but with ptr
745  // approach it might be clearer what's going on here
746  const tcrsmatrix_t *pCrsMatrix = dynamic_cast<const tcrsmatrix_t *>(&from);
747 
748  if (!pCrsMatrix) {
749  throw std::logic_error("TpetraRowMatrixAdapter cannot migrate data for "
750  "your RowMatrix; it can migrate data only for "
751  "Tpetra::CrsMatrix. "
752  "You can inherit from TpetraRowMatrixAdapter and "
753  "implement migration for your RowMatrix.");
754  }
755 
756  // source map
757  const RCP<const map_t> &smap = from.getRowMap();
758  gno_t numGlobalRows = smap->getGlobalNumElements();
759  gno_t base = smap->getMinAllGlobalIndex();
760 
761  // target map
762  ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
763  const RCP<const Teuchos::Comm<int>> &comm = from.getComm();
764  RCP<const map_t> tmap = rcp(new map_t(numGlobalRows, rowList, base, comm));
765 
766  // importer
767  Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
768 
769  int oldNumElts = smap->getLocalNumElements();
770  int newNumElts = numLocalRows;
771 
772  // number of non zeros in my new rows
773  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> vector_t;
774  vector_t numOld(smap); // TODO These vectors should have scalar=size_t,
775  vector_t numNew(tmap); // but ETI does not yet support that.
776  for (int lid = 0; lid < oldNumElts; lid++) {
777  numOld.replaceGlobalValue(smap->getGlobalElement(lid),
778  scalar_t(from.getNumEntriesInLocalRow(lid)));
779  }
780  numNew.doImport(numOld, importer, Tpetra::INSERT);
781 
782  // TODO Could skip this copy if could declare vector with scalar=size_t.
783  ArrayRCP<size_t> nnz(newNumElts);
784  if (newNumElts > 0) {
785  ArrayRCP<scalar_t> ptr = numNew.getDataNonConst(0);
786  for (int lid = 0; lid < newNumElts; lid++) {
787  nnz[lid] = static_cast<size_t>(ptr[lid]);
788  }
789  }
790 
791  RCP<tcrsmatrix_t> M = rcp(new tcrsmatrix_t(tmap, nnz()));
792 
793  M->doImport(from, importer, Tpetra::INSERT);
794  M->fillComplete();
795 
796  return Teuchos::rcp_dynamic_cast<User>(M);
797 }
798 
799 } // namespace Zoltan2
800 
801 #endif // _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_
bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
void setWeightsHost(typename Base::ConstWeightsHostView1D val, int idx)
Provide a host view of weights for the primary entity type.
ArrayRCP< StridedData< lno_t, scalar_t > > rowWeights_
void getRowWeightsHostView(typename Base::WeightsHostView1D &weights, int idx=0) const
virtual RCP< User > doMigration(const User &from, size_t numLocalRows, const gno_t *myNewRows) const
void setRowWeightsHost(typename Base::ConstWeightsHostView1D val, int idx)
Provide a host view to row weights.
Helper functions for Partitioning Problems.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
typename InputTraits< User >::scalar_t scalar_t
MatrixAdapter defines the adapter interface for matrices.
static void AssertCondition(bool condition, const std::string &message, const char *file=__FILE__, int line=__LINE__)
static ArrayRCP< ArrayRCP< zscalar_t > > weights
typename Kokkos::HostSpace::memory_space host_t
typename node_t::device_type device_t
default_part_t part_t
The data type to represent part numbers.
default_offset_t offset_t
The data type to represent offsets.
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:18
void setWeightsDevice(typename Base::ConstWeightsDeviceView1D val, int idx)
Provide a device view of weights for the primary entity type.
typename node_t::device_type device_t
typename InputTraits< User >::part_t part_t
bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row...
Provides access for Zoltan2 to Tpetra::RowMatrix data.
typename InputTraits< User >::node_t node_t
void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
Provide a pointer to the row weights, if any.
size_t getLocalNumEntries() const
Returns the number of nonzeros on this process.
A PartitioningSolution is a solution to a partitioning problem.
void getRowIDsView(const gno_t *&rowIds) const override
void setRowWeightsDevice(typename Base::ConstWeightsDeviceView1D val, int idx)
Provide a device view to row weights.
int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
void getCRSDeviceView(typename Base::ConstOffsetsDeviceView &offsets, typename Base::ConstIdsDeviceView &colIds) const override
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
size_t getLocalNumColumns() const
Returns the number of columns on this process.
Tpetra::Map map_t
Definition: mapRemotes.cpp:16
The StridedData class manages lists of weights or coordinates.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:17
void getRowIDsDeviceView(typename Base::ConstIdsDeviceView &rowIds) const override
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
void setRowWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each row.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
typename InputTraits< User >::offset_t offset_t
void getCRSHostView(typename Base::ConstOffsetsHostView &offsets, typename Base::ConstIdsHostView &colIds) const override
void getRowWeightsDeviceView(typename Base::WeightsDeviceView1D &weights, int idx=0) const
void setWeightIsDegree(int idx)
Specify an index for which the weight should be the degree of the entity.
Defines the MatrixAdapter interface.
void setWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each entity of the primaryEntityType.
void getRowIDsHostView(typename Base::ConstIdsHostView &rowIds) const override
size_t getLocalNumRows() const
Returns the number of rows on this process.
TpetraRowMatrixAdapter(int nWeightsPerRow, const RCP< const User > &inmatrix)
void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds) const
void setRowWeightIsNumberOfNonZeros(int idx)
Specify an index for which the row weight should be the global number of nonzeros in the row...
default_scalar_t scalar_t
The data type for weights and coordinates.
TpetraRowMatrixAdapter(const RCP< const User > &inmatrix, int nWeightsPerRow=0)
Constructor.
This file defines the StridedData class.