COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Operations.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.2 -------------------------------------------------*/
4 /* date: 10/06/2011 --------------------------------------------*/
5 /* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2011, Aydin Buluc
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
33 #ifndef _OPERATIONS_H_
34 #define _OPERATIONS_H_
35 
36 #include <iostream>
37 #include <functional>
38 #include <cmath>
39 #include <mpi.h>
40 
41 using namespace std;
42 
43 
44 template<typename T>
45 struct myset: public std::unary_function<T, T>
46 {
47  myset(T myvalue): value(myvalue) {};
49  const T& operator()(const T& x) const
50  {
51  return value;
52  }
53  T value;
54 };
55 
56 template<typename T>
57 struct identity : public std::unary_function<T, T>
58 {
60  const T operator()(const T& x) const
61  {
62  return x;
63  }
64 };
65 
66 
67 // Because identify reports ambiguity in PGI compilers
68 template<typename T>
69 struct myidentity : public std::unary_function<T, T>
70 {
72  const T operator()(const T& x) const
73  {
74  return x;
75  }
76 };
77 
78 
79 template<typename T>
80 struct totality : public std::unary_function<T, bool>
81 {
83  bool operator()(const T& x) const
84  {
85  return true;
86  }
87 };
88 
89 
90 template<typename T>
91 struct safemultinv : public std::unary_function<T, T>
92 {
93  const T operator()(const T& x) const
94  {
95  T inf = std::numeric_limits<T>::max();
96  return (x == 0) ? inf:(1/x);
97  }
98 };
99 
100 
101 template<typename T1, typename T2>
102 struct bintotality : public std::binary_function<T1, T2, bool>
103 {
105  bool operator()(const T1& x, const T2 & y) const
106  {
107  return true;
108  }
109 };
110 
111 
112 
119 struct exponentiate : public std::binary_function<double, double, double>
120 {
121  double operator()(double x, double y) const { return std::pow(x, y); }
122 };
123 
124 
132 template<typename T>
133 struct maximum : public std::binary_function<T, T, T>
134 {
136  const T operator()(const T& x, const T& y) const
137  {
138  return x < y? y : x;
139  }
140 };
141 
142 
150 template<typename T>
151 struct minimum : public std::binary_function<T, T, T>
152 {
154  const T operator()(const T& x, const T& y) const
155  {
156  return x < y? x : y;
157  }
158 };
159 
167 template<typename T>
168 struct bitwise_and : public std::binary_function<T, T, T>
169 {
171  T operator()(const T& x, const T& y) const
172  {
173  return x & y;
174  }
175 };
176 
177 
185 template<typename T>
186 struct bitwise_or : public std::binary_function<T, T, T>
187 {
189  T operator()(const T& x, const T& y) const
190  {
191  return x | y;
192  }
193 };
194 
202 template<typename T>
203 struct logical_xor : public std::binary_function<T, T, T>
204 {
206  T operator()(const T& x, const T& y) const
207  {
208  return (x || y) && !(x && y);
209  }
210 };
211 
220 template<typename T>
221 struct bitwise_xor : public std::binary_function<T, T, T>
222 {
224  T operator()(const T& x, const T& y) const
225  {
226  return x ^ y;
227  }
228 };
229 
230 
231 // MPIOp: A class that has a static op() function that takes no arguments and returns the corresponding MPI_Op
232 // if and only if the given Op has a mapping to a valid MPI_Op
233 // No concepts checking for the applicability of Op on the datatype T at the moment
234 // In the future, this can be implemented via metafunction forwarding using mpl::or_ and mpl::bool_
235 
236 template <typename Op, typename T>
237 struct MPIOp
238 {
239 };
240 
241 template<typename T> struct MPIOp< maximum<T>, T > { static MPI_Op op() { return MPI_MAX; } };
242 template<typename T> struct MPIOp< minimum<T>, T > { static MPI_Op op() { return MPI_MIN; } };
243 template<typename T> struct MPIOp< std::plus<T>, T > { static MPI_Op op() { return MPI_SUM; } };
244 template<typename T> struct MPIOp< std::multiplies<T>, T > { static MPI_Op op() { return MPI_PROD; } };
245 template<typename T> struct MPIOp< std::logical_and<T>, T > { static MPI_Op op() { return MPI_LAND; } };
246 template<typename T> struct MPIOp< std::logical_or<T>, T > { static MPI_Op op() { return MPI_LOR; } };
247 template<typename T> struct MPIOp< logical_xor<T>, T > { static MPI_Op op() { return MPI_LXOR; } };
248 template<typename T> struct MPIOp< bitwise_and<T>, T > { static MPI_Op op() { return MPI_BAND; } };
249 template<typename T> struct MPIOp< bitwise_or<T>, T > { static MPI_Op op() { return MPI_BOR; } };
250 template<typename T> struct MPIOp< bitwise_xor<T>, T > { static MPI_Op op() { return MPI_BXOR; } };
251 
252 
253 #endif
254 
255