Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_DistributionMM.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Build maps for 1D or 2D matrix distribution
11 // Assumes square matrix
12 // Karen Devine, SNL
13 //
14 
15 #ifndef __TPETRA_DISTRIBUTIONMM_HPP
16 #define __TPETRA_DISTRIBUTIONMM_HPP
17 
18 namespace Tpetra
19 {
20 
21 template <typename gno_t, typename scalar_t>
22 class DistributionMMFile : public Distribution<gno_t,scalar_t> {
23 // Distribution of nonzeros is determined by nonzero's value
24 // as read from Matrix-Market file.
25 // Vector entry v_i is assigned to the same processor as matrix diagonal a_{ii}.
26 // For now, we derive the vector entry assignment by accruing information
27 // about the diagonal entries' assigments during calls to Mine(I,J,V).
28 // A better scheme might read vector entries' assignments from a file as well,
29 // or have a separate discovery of vector entries' assignments in the
30 // constructor.
31 // Assumptions include:
32 // - If diagonal entries are needed (e.g., for a Laplacian), they are
33 // included in the MMFile
34 // - Part assignments are one-based; that is (I,J,V) = (1,1,4) assigns
35 // (I,J) to process 3.
36 // - Mine(I,J) is undefined; value V must be provided.
37 
38 public:
39  using Distribution<gno_t,scalar_t>::me;
40  using Distribution<gno_t,scalar_t>::np;
41  using Distribution<gno_t,scalar_t>::nrows;
42 
43  DistributionMMFile(size_t nrows_,
44  const Teuchos::RCP<const Teuchos::Comm<int> > &comm_,
45  const Teuchos::ParameterList &params) :
46  Distribution<gno_t,scalar_t>(nrows_, comm_, params)
47  {
48  if (me == 0) std::cout << "\n MMFile Distribution: "
49  << "\n np = " << np << std::endl;
50  }
51 
52  inline enum DistributionType DistType() { return MMFile; }
53 
54  bool Mine(gno_t i, gno_t j) {
55  std::cout << "Invalid call to Mine(i,j); "
56  << "MMFile-distribution requires use of Mine(i,j,p) providing "
57  << "process assignment p." << std::endl;
58  exit(-1);
59  }
60 
61  bool Mine(gno_t i, gno_t j, int oneBasedRank) {
62  // Nonzero (i,j) is Mine if oneBasedRank-1 == me.
63 
64  if (oneBasedRank < 1 || oneBasedRank > np) {
65  std::cout << "Invalid rank " << oneBasedRank
66  << " provided in user distribution; "
67  << "rank must be in range 1 to " << np << std::endl;
68  exit(-1);
69  }
70 
71  // Keep track of diagonal entries that I own for use in vector map
72  if (oneBasedRank-1 == me && i == j) myVecEntries.insert(i);
73 
74  return (oneBasedRank-1 == me);
75  }
76 
77  // myVecEntries keeps track of which diagonal matrix entries are Mine().
78  // myVecEntries is not complete until the entire matrix has been viewed
79  // by Mine(), so use of VecMine before that point may produce misleading
80  // results.
81  inline bool VecMine(gno_t i) {
82  return (myVecEntries.find(i) != myVecEntries.end());
83  }
84 
85 private:
86  std::set<gno_t> myVecEntries; // vector entries that are assigned to me
87 };
88 
89 }
90 #endif