antioch-0.4.0
vexcl_utils.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_VEXCL_UTILS_H
28 #define ANTIOCH_VEXCL_UTILS_H
29 
30 #ifdef ANTIOCH_METAPROGRAMMING_H
31 # ifndef ANTIOCH_VEXCL_UTILS_DECL_H
32 # error valarray_utils_decl.h must be included before metaprogramming.h
33 # endif
34 #endif
35 
36 // Antioch
39 
40 
41 #ifdef ANTIOCH_HAVE_VEXCL
42 // Though the following implementations are almost all valid without
43 // <vexcl/vexcl.hpp>, successfully using them with
44 // VexCL types requires VexCL to be included first.
45 // Configure-time VexCL support enforces this constraint but
46 // header-only VexCL may be mixed with header-only Antioch
47 // without configure-time flags.
48 #include "vexcl/vexcl.hpp"
49 #else
50 // Forward declaration instead
51 namespace vex {
52 template <typename T> class vector;
53 template <typename T> class is_vector_expression;
54 
55 template<typename real, class RDC>
56 class Reductor;
57 
58 class MAX;
59 class MIN;
60 
61  namespace detail
62  {
63  class get_expression_properties;
64  class extract_terminal;
65  }
66 }
67 namespace boost {
68  namespace proto {
69  template<typename A0, typename A1, typename A2>
70  typename result_of::make_expr<
71  tag::if_else_,
72  deduce_domain,
73  A0 const &,
74  A1 const &,
75  A2 const &
76  >::type const
77  if_else(A0 const & a0, A1 const & a1, A2 const & a2);
78  }
79 }
80 #endif
81 
82 
83 #ifdef VEXCL_OPERATIONS_HPP
84 namespace std {
85 template <typename T>
86 inline
87 ANTIOCH_AUTO(vex::vector<T>)
88 max(const vex::vector<T>& a,
89  const vex::vector<T>& b)
90 ANTIOCH_AUTOFUNC(vex::vector<T>, vex::max(a,b))
91 
92 template <typename T>
93 inline
94 ANTIOCH_AUTO(vex::vector<T>)
95 min(const vex::vector<T>& a,
96  const vex::vector<T>& b)
97 ANTIOCH_AUTOFUNC(vex::vector<T>, vex::min(a,b))
98 }
99 
100 #endif
101 
102 
103 namespace Antioch
104 {
105 
106 template <typename T>
107 inline
108 T
109 max (const vex::vector<T>& in)
110 {
111  vex::Reductor<T, vex::MAX> vex_max(in.queue_list());
112  return vex_max(in);
113 }
114 
115 template <typename T>
116 inline
117 T
118 min (const vex::vector<T>& in)
119 {
120  vex::Reductor<T, vex::MIN> vex_min(in.queue_list());
121  return vex_min(in);
122 }
123 
124 template <typename T>
125 struct return_auto<vex::vector<T> >
126 {
127  static const bool value = true;
128 };
129 
130 template <typename T>
131 struct has_size<vex::vector<T> >
132 {
133  static const bool value = true;
134 };
135 
136 template <typename T>
137 struct size_type<vex::vector<T> >
138 {
139  typedef std::size_t type;
140 };
141 
142 template <typename T>
143 struct value_type<vex::vector<T> >
144 {
145  typedef T type;
146 };
147 
148 template <typename T>
149 struct raw_value_type<vex::vector<T> >
150 {
151  typedef typename raw_value_type<T>::type type;
152 };
153 
154 template <typename T>
155 inline
156 vex::vector<T>
157 zero_clone(const vex::vector<T>& example)
158 {
159  vex::vector<T> returnval(example.queue_list(), example.size());
160  returnval.clear();
161  return returnval;
162 }
163 
164 template <typename T1, typename T2>
165 inline
166 void
167 zero_clone(vex::vector<T1>& output, const vex::vector<T2>& example)
168 {
169  output.resize(example.queue_list(), example.size());
170  output = 0;
171 }
172 
173 template <typename T, typename Scalar>
174 inline
175 vex::vector<T>
176 constant_clone(const vex::vector<T>& example, const Scalar& value)
177 {
178  vex::vector<T> returnval(example.queue_list(), example.size());
179  returnval = value;
180  return returnval;
181 }
182 
183 template <typename T>
184 inline
185 void
186 init_clone(vex::vector<T>& output, const vex::vector<T>& example)
187 {
188  output.resize(example.queue_list(), example.size());
189  output = example;
190 }
191 
192 template <typename BoolInput,
193  typename IfValue, typename ElseValue>
194 inline
195 typename boost::proto::result_of::make_expr<
196  boost::proto::tag::if_else_,
197  boost::proto::deduce_domain,
198  const vex::vector_expression<BoolInput>&,
199  const IfValue&,
200  const ElseValue&
201 >::type const
202 if_else(const vex::vector_expression<BoolInput> &condition,
203  const IfValue &if_true,
204  const ElseValue &if_false)
205 {
206  return boost::proto::if_else(condition, if_true, if_false);
207 }
208 
209 template <typename T>
210 inline
211 bool disjunction_root(const T & vec_input, vexcl_library_tag)
212 {
213 #ifdef ANTIOCH_HAVE_VEXCL
214  vex::detail::get_expression_properties prop;
215  vex::detail::extract_terminals()(boost::proto::as_child(vec_input),prop);
216 
217  vex::any_of any_of(prop.queue);
218  return any_of(vec_input); // at least one true
219 #else
220  antioch_error();
221  return false;
222 #endif
223 }
224 
225 template <typename T>
226 inline
227 bool conjunction_root(const T & vec_input, vexcl_library_tag)
228 {
229 #ifdef ANTIOCH_HAVE_VEXCL
230  vex::detail::get_expression_properties prop;
231  vex::detail::extract_terminals()(boost::proto::as_child(vec_input),prop);
232 
233  vex::all_of all_of(prop.queue);
234  return all_of(vec_input); // at least one false
235 #else
236  antioch_error();
237  return false;
238 #endif
239 }
240 
241 template <typename VectorT, typename IntT>
242 inline
243 typename enable_if_c<
244  vex::is_vector_expression<typename value_type<VectorT>::type>::value &&
245  vex::is_vector_expression<IntT>::value,
246  typename value_type<VectorT>::type
247 >::type
248 eval_index(const VectorT& vec, const IntT& index)
249 {
250  typename value_type<VectorT>::type returnval = zero_clone(vec[0]);
251 
252  // FIXME - this will be painfully slow
253  for (unsigned int i=0; i != index.size(); ++i)
254  returnval[i] = vec[index[i]][i];
255 
256  return returnval;
257 }
258 
259 
260 } // end namespace Antioch
261 
262 
263 #endif //ANTIOCH_VEXCL_UTILS_H
boost::proto::result_of::make_expr< boost::proto::tag::if_else_, boost::proto::deduce_domain, const vex::vector_expression< BoolInput > &, const IfValue &, const ElseValue & >::type const if_else(const vex::vector_expression< BoolInput > &condition, const IfValue &if_true, const ElseValue &if_false)
Definition: vexcl_utils.h:202
bool disjunction_root(const T &vec_input, vexcl_library_tag)
Definition: vexcl_utils.h:211
vex::vector< T > constant_clone(const vex::vector< T > &example, const Scalar &value)
Definition: vexcl_utils.h:176
#define antioch_error()
bool conjunction_root(const T &vec_input, vexcl_library_tag)
Definition: vexcl_utils.h:227
max(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
void zero_clone(vex::vector< T1 > &output, const vex::vector< T2 > &example)
Definition: vexcl_utils.h:167
enable_if_c< vex::is_vector_expression< typename value_type< VectorT >::type >::value &&vex::is_vector_expression< IntT >::value, typename value_type< VectorT >::type >::type eval_index(const VectorT &vec, const IntT &index)
Definition: vexcl_utils.h:248
#define ANTIOCH_AUTOFUNC(Type, Expr)
The parameters are reduced parameters.
typename _Scalar int _Rows int _Cols int _Options int _MaxRows int _MaxCols inline min(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
void init_clone(vex::vector< T > &output, const vex::vector< T > &example)
Definition: vexcl_utils.h:186
#define ANTIOCH_AUTO(Type)

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8