Zoltan2
Zoltan2_TPLTraits.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 
46 #ifndef _ZOLTAN2_TPLTRAITS_HPP_
47 #define _ZOLTAN2_TPLTRAITS_HPP_
48 
49 #include <Teuchos_RCP.hpp>
50 #include <Teuchos_ArrayView.hpp>
51 #include <Teuchos_as.hpp>
52 #include <Zoltan2_Standards.hpp>
53 #include <Zoltan2_Environment.hpp>
54 
55 #include <zoltan_types.h>
56 
62 
63 namespace Zoltan2 {
64 
66 // General case //
68 
69 template <typename tpl_t, typename zno_t>
70 struct TPL_Traits {
71 
72  static inline bool OK_TO_CAST_TPL_T()
73  {
74  // Return true if pointer to tpl_t can be safely used as pointer to zno_t
75  return ((sizeof(tpl_t) == sizeof(zno_t)) &&
76  (std::numeric_limits<tpl_t>::is_signed ==
77  std::numeric_limits<zno_t>::is_signed));
78  }
79 
80  static inline void ASSIGN_TPL_T(tpl_t &a, zno_t b)
81  {
82  // Assign a = b; make sure tpl_t is large enough to accept zno_t.
83  try {
84  a = Teuchos::asSafe<tpl_t, zno_t>(b);
85  }
86  catch (std::exception &e) {
87  throw std::runtime_error(
88  "TPL_Traits: Value too large for TPL index type. "
89  "Rebuild TPL with larger index type or rebuild without the TPL.");
90  }
91  }
92 
93  static inline void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView<zno_t> &b)
94  {
95  // Allocate array a; copy b values into a.
96  size_t size = b.size();
97  if (size > 0) {
98  *a = new tpl_t[size];
99  for (size_t i = 0; i < size; i++) ASSIGN_TPL_T((*a)[i], b[i]);
100  }
101  else {
102  *a = NULL;
103  // Note: the Scotch manual says that if any rank has a non-NULL array,
104  // every process must have a non-NULL array. In practice,
105  // however, this condition is not needed for the arrays we use.
106  // For now, we'll set these arrays to NULL. We could allocate
107  // a dummy value here if needed. KDD 1/23/14
108  // Note: ParMETIS would likely prefer a dummy value as well. It does
109  // not like NULL adjcny array. KDD 10/7/14
110  }
111  }
112 
113  static inline void DELETE_TPL_T_ARRAY(tpl_t **a)
114  {
115  // Delete the copy made in ASSIGN_TPL_T_ARRAY.
116  delete [] *a;
117  }
118 };
119 
121 // Special case: zno_t == tpl_t //
122 // No error checking or copies //
124 
125 template <typename tpl_t>
126 struct TPL_Traits<tpl_t, tpl_t> {
127 
128  static inline bool OK_TO_CAST_TPL_T() {return true;}
129 
130  static inline void ASSIGN_TPL_T(tpl_t &a, tpl_t b)
131  { a = b; }
132 
133  static inline void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView<tpl_t> &b)
134  {
135  if (b.size() > 0)
136  *a = const_cast<tpl_t *> (b.getRawPtr());
137  else
138  *a = NULL;
139  // Note: the Scotch manual says that if any rank has a non-NULL array,
140  // every process must have a non-NULL array. In practice,
141  // however, this condition is not needed for the arrays we use.
142  // For now, we'll set these arrays to NULL. We could allocate
143  // a dummy value here if needed. KDD 1/23/14
144  }
145  static inline void DELETE_TPL_T_ARRAY(tpl_t **a) { }
146 };
147 
149 // Special case: Zoltan ZOLTAN_ID_PTR
151 
152 template <typename zno_t>
153 struct TPL_Traits<ZOLTAN_ID_PTR, zno_t> {
154 
155  // Copy the data bitwise INTO the array of ZOLTAN_ID_TYPE
156  // We assume that any memory pointed to by ZOLTAN_ID_PTR is
157  // big enough to hold the bits of zno_t -- that is, that
158  // the ZOLTAN_ID_PTR's memory correctly accomodates Zoltan's
159  // num_gid_entries or num_lid_entries
160 
161  static const int NUM_ID = ((sizeof(zno_t) / sizeof(ZOLTAN_ID_TYPE) > 0)
162  ? (sizeof(zno_t) / sizeof(ZOLTAN_ID_TYPE))
163  : 1);
164 
165  static inline bool OK_TO_CAST_TPL_T()
166  {
167  // There may be cases where if it OK to cast a pointer to a
168  // zno_t to a ZOLTAN_ID_PTR, but the semantics of this
169  // function ask whether a pointer to a zno_t can be cast
170  // to a pointer to a ZOLTAN_ID_PTR. Thus, the answer is "no."
171  return false;
172  }
173 
174  static inline void ASSIGN_TPL_T(ZOLTAN_ID_PTR &a, zno_t b)
175  {
176  switch (NUM_ID) {
177  case 1:
178  a[0] = static_cast<ZOLTAN_ID_TYPE>(b);
179  break;
180  case 2: {
181  ZOLTAN_ID_TYPE *ptr = (ZOLTAN_ID_TYPE *)(&b);
182  a[0] = ptr[0];
183  a[1] = ptr[1];
184  break;
185  }
186  default: {
187  ZOLTAN_ID_TYPE *ptr = (ZOLTAN_ID_TYPE *)(&b);
188  for (int i = 0; i < NUM_ID; i++) a[i] = ptr[i];
189  }
190  }
191  }
192 
193  static inline void ASSIGN_TPL_T_ARRAY(ZOLTAN_ID_PTR *a, ArrayView<zno_t> &b)
194  {
195  // Allocate array a; copy b values into a.
196  size_t size = b.size();
197  if (size > 0) {
198  if (NUM_ID == 1) {
199  *a = b.getRawPtr(); // Don't have to make a new copy
200  }
201  else {
202  *a = new ZOLTAN_ID_TYPE[size*NUM_ID];
203  for (size_t i = 0; i < size; i++) ASSIGN_TPL_T((*a)[i], b[i]);
204  }
205  }
206  else {
207  *a = NULL;
208  }
209  }
210 
211  static inline void DELETE_TPL_T_ARRAY(ZOLTAN_ID_PTR *a)
212  {
213  // Delete the copy made in ASSIGN_TPL_T_ARRAY.
214  if (NUM_ID != 1)
215  delete [] *a;
216  }
217 };
218 
219 template <typename zno_t>
220 struct TPL_Traits<zno_t, ZOLTAN_ID_PTR> {
221 
222  // Copy the data bitwise FROM the array of ZOLTAN_ID_TYPE
223 
224  static const int NUM_ID = TPL_Traits<ZOLTAN_ID_PTR, zno_t>::NUM_ID;
225 
226  static inline bool OK_TO_CAST_TPL_T()
227  {
228  // There may be cases where if it OK to cast a pointer to a
229  // ZOLTAN_ID_PTR to a zno_t, but the semantics of this
230  // function ask whether a pointer to a ZOLTAN_ID_PTR can be cast
231  // to a pointer to a zno_t. Thus, the answer is "no."
232  return false;
233  }
234 
235  static inline void ASSIGN_TPL_T(zno_t &a, ZOLTAN_ID_PTR b)
236  {
237  switch (NUM_ID) {
238  case 1:
239  a = static_cast<zno_t>(b[0]);
240  break;
241  default:
242  zno_t *tmp = (zno_t *) b;
243  a = *tmp;
244  }
245  }
246 };
247 
248 } // namespace Zoltan2
249 
250 #endif
static void ASSIGN_TPL_T(ZOLTAN_ID_PTR &a, zno_t b)
static bool OK_TO_CAST_TPL_T()
static void DELETE_TPL_T_ARRAY(tpl_t **a)
static void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView< tpl_t > &b)
static void ASSIGN_TPL_T_ARRAY(ZOLTAN_ID_PTR *a, ArrayView< zno_t > &b)
static void ASSIGN_TPL_T(zno_t &a, ZOLTAN_ID_PTR b)
static void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView< zno_t > &b)
Gathering definitions used in software development.
Defines the Environment class.
static void ASSIGN_TPL_T(tpl_t &a, zno_t b)
static void DELETE_TPL_T_ARRAY(ZOLTAN_ID_PTR *a)
static void ASSIGN_TPL_T(tpl_t &a, tpl_t b)