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 
59 
60 namespace Zoltan2 {
61 
63 // General case //
65 
66 template <typename tpl_t, typename zno_t>
67 struct TPL_Traits {
68 
69  static inline bool OK_TO_CAST_TPL_T()
70  {
71  // Return true is pointer to tpl_t can be safely used as pointer to zno_t
72  return ((sizeof(tpl_t) == sizeof(zno_t)) &&
73  (std::numeric_limits<tpl_t>::is_signed ==
74  std::numeric_limits<zno_t>::is_signed));
75  }
76 
77  static inline void ASSIGN_TPL_T(tpl_t &a, zno_t b,
78  const RCP<const Environment> &env)
79  {
80  // Assign a = b; make sure tpl_t is large enough to accept zno_t.
81  try {
82  a = Teuchos::asSafe<tpl_t, zno_t>(b);
83  }
84  catch (std::exception &e) {
85  env->localInputAssertion(__FILE__, __LINE__,
86  "Value too large for TPL index type. "
87  "Rebuild TPL with larger index type or rebuild without the TPL.",
88  false, BASIC_ASSERTION);
89  }
90  }
91 
92  static inline void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView<const zno_t> &b,
93  const RCP<const Environment> &env)
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], env);
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  const RCP<const Environment> &env)
132  { a = b; }
133 
134  static inline void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView<const tpl_t> &b,
135  const RCP<const Environment> &env)
136  {
137  if (b.size() > 0)
138  *a = const_cast<tpl_t *> (b.getRawPtr());
139  else
140  *a = NULL;
141  // Note: the Scotch manual says that if any rank has a non-NULL array,
142  // every process must have a non-NULL array. In practice,
143  // however, this condition is not needed for the arrays we use.
144  // For now, we'll set these arrays to NULL. We could allocate
145  // a dummy value here if needed. KDD 1/23/14
146  }
147  static inline void DELETE_TPL_T_ARRAY(tpl_t **a) { }
148 };
149 
150 } // namespace Zoltan2
151 
152 #endif
static bool OK_TO_CAST_TPL_T()
static void DELETE_TPL_T_ARRAY(tpl_t **a)
fast typical checks for valid arguments
static void ASSIGN_TPL_T(tpl_t &a, zno_t b, const RCP< const Environment > &env)
static void ASSIGN_TPL_T(tpl_t &a, tpl_t b, const RCP< const Environment > &env)
static void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView< const tpl_t > &b, const RCP< const Environment > &env)
static void ASSIGN_TPL_T_ARRAY(tpl_t **a, ArrayView< const zno_t > &b, const RCP< const Environment > &env)
Gathering definitions used in software development.
Defines the Environment class.