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 //
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 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 // Build maps for 1D or 2D matrix distribution
43 // Assumes square matrix
44 // Karen Devine, SNL
45 //
46 
47 #ifndef __TPETRA_DISTRIBUTIONMM_HPP
48 #define __TPETRA_DISTRIBUTIONMM_HPP
49 
50 namespace Tpetra
51 {
52 
53 template <typename gno_t, typename scalar_t>
54 class DistributionMMFile : public Distribution<gno_t,scalar_t> {
55 // Distribution of nonzeros is determined by nonzero's value
56 // as read from Matrix-Market file.
57 // Vector entry v_i is assigned to the same processor as matrix diagonal a_{ii}.
58 // For now, we derive the vector entry assignment by accruing information
59 // about the diagonal entries' assigments during calls to Mine(I,J,V).
60 // A better scheme might read vector entries' assignments from a file as well,
61 // or have a separate discovery of vector entries' assignments in the
62 // constructor.
63 // Assumptions include:
64 // - If diagonal entries are needed (e.g., for a Laplacian), they are
65 // included in the MMFile
66 // - Part assignments are one-based; that is (I,J,V) = (1,1,4) assigns
67 // (I,J) to process 3.
68 // - Mine(I,J) is undefined; value V must be provided.
69 
70 public:
71  using Distribution<gno_t,scalar_t>::me;
72  using Distribution<gno_t,scalar_t>::np;
73  using Distribution<gno_t,scalar_t>::nrows;
74 
75  DistributionMMFile(size_t nrows_,
76  const Teuchos::RCP<const Teuchos::Comm<int> > &comm_,
77  const Teuchos::ParameterList &params) :
78  Distribution<gno_t,scalar_t>(nrows_, comm_, params)
79  {
80  if (me == 0) std::cout << "\n MMFile Distribution: "
81  << "\n np = " << np << std::endl;
82  }
83 
84  inline enum DistributionType DistType() { return MMFile; }
85 
86  bool Mine(gno_t i, gno_t j) {
87  std::cout << "Invalid call to Mine(i,j); "
88  << "MMFile-distribution requires use of Mine(i,j,p) providing "
89  << "process assignment p." << std::endl;
90  exit(-1);
91  }
92 
93  bool Mine(gno_t i, gno_t j, int oneBasedRank) {
94  // Nonzero (i,j) is Mine if oneBasedRank-1 == me.
95 
96  if (oneBasedRank < 1 || oneBasedRank > np) {
97  std::cout << "Invalid rank " << oneBasedRank
98  << " provided in user distribution; "
99  << "rank must be in range 1 to " << np << std::endl;
100  exit(-1);
101  }
102 
103  // Keep track of diagonal entries that I own for use in vector map
104  if (oneBasedRank-1 == me && i == j) myVecEntries.insert(i);
105 
106  return (oneBasedRank-1 == me);
107  }
108 
109  // myVecEntries keeps track of which diagonal matrix entries are Mine().
110  // myVecEntries is not complete until the entire matrix has been viewed
111  // by Mine(), so use of VecMine before that point may produce misleading
112  // results.
113  inline bool VecMine(gno_t i) {
114  return (myVecEntries.find(i) != myVecEntries.end());
115  }
116 
117 private:
118  std::set<gno_t> myVecEntries; // vector entries that are assigned to me
119 };
120 
121 }
122 #endif