Zoltan2
Zoltan2_IdentifierModel.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_IDENTIFIERMODEL_HPP_
51 #define _ZOLTAN2_IDENTIFIERMODEL_HPP_
52 
53 #include <Zoltan2_Model.hpp>
54 #include <Zoltan2_Adapter.hpp>
55 #include <Zoltan2_StridedData.hpp>
56 
57 namespace Zoltan2 {
58 
69 template <typename Adapter>
70 class IdentifierModel : public Model<Adapter>
71 {
72 public:
73 
74 #ifndef DOXYGEN_SHOULD_SKIP_THIS
75  typedef typename Adapter::scalar_t scalar_t;
76  typedef typename Adapter::gno_t gno_t;
77  typedef typename Adapter::lno_t lno_t;
78  typedef StridedData<lno_t, scalar_t> input_t;
79 #endif
80 
88  IdentifierModel(const RCP<const Adapter> &ia,
89  const RCP<const Environment> &env,
90  const RCP<const Comm<int> > &comm, modelFlag_t &modelFlags);
91 
94  inline size_t getLocalNumIdentifiers() const { return gids_.size(); }
95 
99  return numGlobalIdentifiers_;
100  }
101 
110  inline size_t getIdentifierList(ArrayView<const gno_t> &Ids,
111  ArrayView<input_t> &wgts) const
112  {
113  Ids = ArrayView<const gno_t>();
114  wgts = weights_.view(0, nUserWeights_);
115  size_t n = getLocalNumIdentifiers();
116  if (n){
117  Ids = ArrayView<const gno_t>(
118  reinterpret_cast<const gno_t*>(gids_.getRawPtr()), n);
119  }
120  return n;
121  }
122 
124  // The Model interface.
126 
127  inline size_t getLocalNumObjects() const {return getLocalNumIdentifiers();}
128 
129  inline size_t getGlobalNumObjects() const {return getGlobalNumIdentifiers();}
130 
131 private:
132 
133  gno_t numGlobalIdentifiers_;
134  const RCP<const Environment> env_;
135  const RCP<const Comm<int> > comm_;
136  ArrayRCP<const gno_t> gids_;
137  ArrayRCP<const gno_t> gidsConst_;
138  int nUserWeights_;
139  ArrayRCP<input_t> weights_;
140 };
141 
143 template <typename Adapter>
145  const RCP<const Adapter> &ia,
146  const RCP<const Environment> &env,
147  const RCP<const Comm<int> > &comm,
148  modelFlag_t &modelFlags):
149  numGlobalIdentifiers_(), env_(env), comm_(comm),
150  gids_(), gidsConst_(), nUserWeights_(0), weights_()
151 {
152  // Get the local and global problem size
153  size_t nLocalIds = ia->getLocalNumIDs();
154  gno_t lsum = nLocalIds;
155  reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum,
156  &numGlobalIdentifiers_);
157 
158  // Get the number of weights
159  // Use max number of weights over all processes as nUserWeights_
160  int tmp = ia->getNumWeightsPerID();
161  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 1,
162  &tmp, &nUserWeights_);
163 
164  // Prepare to store views from input adapter
165  // TODO: Do we have to store these views, or can we get them on an
166  // TODO: as-needed basis?
167  Array<const scalar_t *> wgts(nUserWeights_, (const scalar_t *)NULL);
168  Array<int> wgtStrides(nUserWeights_, 0);
169 
170  if (nUserWeights_ > 0){
171  input_t *w = new input_t [nUserWeights_];
172  weights_ = arcp<input_t>(w, 0, nUserWeights_);
173  }
174 
175  const gno_t *gids=NULL;
176 
177  // Get the input adapter's views
178  try{
179  ia->getIDsView(gids);
180  for (int idx=0; idx < nUserWeights_; idx++)
181  ia->getWeightsView(wgts[idx], wgtStrides[idx], idx);
182  }
184 
185  if (nLocalIds){
186  gids_ = arcp(gids, 0, nLocalIds, false);
187 
188  if (nUserWeights_ > 0){
189  for (int idx=0; idx < nUserWeights_; idx++){
190  ArrayRCP<const scalar_t> wgtArray(wgts[idx], 0,
191  nLocalIds*wgtStrides[idx], false);
192  weights_[idx] = input_t(wgtArray, wgtStrides[idx]);
193  }
194  }
195  }
196 
197  gidsConst_ = arcp_const_cast<const gno_t>(gids_);
198 
199  env_->memory("After construction of identifier model");
200 }
201 
202 } // namespace Zoltan2
203 
204 #endif
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the Model interface.
size_t global_size_t
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
The StridedData class manages lists of weights or coordinates.
size_t getLocalNumObjects() const
Return the local number of objects.
The base class for all model classes.
IdentifierModel defines the interface for all identifier models.
size_t getGlobalNumObjects() const
Return the global number of objects.
global_size_t getGlobalNumIdentifiers() const
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
IdentifierModel(const RCP< const Adapter > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &modelFlags)
Constructor.
This file defines the StridedData class.