Zoltan2  Version of the Day
Zoltan2_GidLookupHelper.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 // @HEADER
46 // ***********************************************************************
47 // copyright
48 // ***********************************************************************
49 // @HEADER
50 
55 #ifndef _ZOLTAN2_GIDLOOKUPHELPER
56 #define _ZOLTAN2_GIDLOOKUPHELPER
57 
58 #include <Zoltan2_Standards.hpp>
59 #include <Zoltan2_Environment.hpp>
61 #include <Teuchos_Hashtable.hpp>
62 #include <map>
63 
64 namespace Zoltan2
65 {
75 template <typename T, typename lno_t>
77 
78 private:
79 
80  RCP<const Environment> env_;
81  ArrayRCP<const T> gidList_;
82  bool useHashTable_;
83 
84  map<T, lno_t> indexMap_;
85 
86  RCP<Teuchos::Hashtable<double, lno_t> > indexHash_;
87 
88 public:
91  GidLookupHelper(const RCP<const Environment> &env,
92  const ArrayRCP<const T> &gidList);
93 
97 
104  lno_t lookup(const T gid) const;
105 
108  lno_t size() const {
109  if (useHashTable_)
110  return indexHash_->size();
111  else
112  return indexMap_.size();
113  }
114 
120  void getIndices(ArrayRCP<lno_t> &indices) const;
121 };
122 
123 template<typename T, typename lno_t>
125  env_(rcp(new Environment)), gidList_(), useHashTable_(false),
126  indexMap_(), indexHash_()
127  {}
128 
129 template<typename T, typename lno_t>
131  const RCP<const Environment> &env,
132  const ArrayRCP<const T> &gidList):
133  env_(env),
134  gidList_(gidList), useHashTable_(false), indexMap_(), indexHash_()
135 {
136  lno_t len = gidList_.size();
137 
138  if (len < 1)
139  return;
140 
142  useHashTable_ = true;
143 
144  const T *ids = gidList_.getRawPtr();
145 
146  if (!useHashTable_){
147  try{
148  for (lno_t i=0; i < gidList.size(); i++){
149  typename map<T, lno_t>::iterator rec = indexMap_.find(*ids);
150  if (rec == indexMap_.end())
151  indexMap_[*ids] = i;
152  ids++;
153  }
154  }
155  catch (const std::exception &e){
156  env_->localMemoryAssertion(__FILE__, __LINE__, len, false);
157  }
158  }
159  else{
160  typedef typename Teuchos::Hashtable<double, lno_t> id2index_hash_t;
161  id2index_hash_t *p = NULL;
162 
163  try{
164  p = new id2index_hash_t(len);
165  }
166  catch (const std::exception &e){
167  env_->localMemoryAssertion(__FILE__, __LINE__, len, false);
168  }
169 
170  for (lno_t i=0; i < len; i++){
171  double key = IdentifierTraits<T>::key(*ids++);
172  try{
173  if (!p->containsKey(key))
174  p->put(key, i);
175  }
176  Z2_THROW_OUTSIDE_ERROR(*env_);
177  }
178 
179  indexHash_ = rcp<id2index_hash_t>(p);
180  }
181 }
182 
183 template<typename T, typename lno_t>
185 {
186  lno_t idx=0;
187  bool badId=false;
188  if (useHashTable_){
189  try{
190  double key = IdentifierTraits<T>::key(gid);
191  idx = indexHash_->get(key);
192  }
193  catch (const std::exception &e) {
194  badId = true;
195  }
196  }
197  else{
198  typename map<T, lno_t>::const_iterator next = indexMap_.find(gid);
199  if (next == indexMap_.end())
200  badId = true;
201  else
202  idx = (*next).second;
203  }
204 
205  env_->localInputAssertion(__FILE__, __LINE__, "invalid global id",
206  badId==false, BASIC_ASSERTION);
207 
208  return idx;
209 }
210 
211 template<typename T, typename lno_t>
213  ArrayRCP<lno_t> &indices) const
214 {
215  lno_t numIds = size();
216  lno_t *idx = new lno_t [numIds];
217  ArrayRCP<lno_t> indexList = arcp(idx, 0, numIds, true);
218 
219  if (numIds == gidList_.size()){
220  for (lno_t i=0; i < gidList_.size(); i++){
221  *idx++ = i;
222  }
223  }
224  else{
225  const T *ids = gidList_.getRawPtr();
226  set<T> allGids;
227 
228  for (lno_t i=0; i < gidList_.size(); i++){
229  typename set<T>::iterator rec = allGids.find(ids[i]);
230  if (rec == allGids.end()){
231  allGids.insert(ids[i]);
232  *idx++ = i;
233  }
234  }
235  }
236 
237  indices = indexList;
238 }
239 
240 } // namespace Z2
241 
242 #endif // _ZOLTAN2_GIDLOOKUPHELPER
lno_t size() const
the number of unique gids in the list.
fast typical checks for valid arguments
Defines basic traits for user global identifiers.
The functions to be defined for users&#39; global ID types.
static double key(const T c)
Return a double which is a unique key for the value.
lno_t lookup(const T gid) const
the index of the global id in the gidList passed into the constructor.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void getIndices(ArrayRCP< lno_t > &indices) const
Return a list of the indices in gidList that were included in the lookup table (all if there were no ...
Gathering definitions used in software development.
Defines the Environment class.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
A class to do GID lookups.
GidLookupHelper()
Construct an empty lookup helper.