COMBINATORIAL_BLAS  1.6
Operations.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 6/15/2017 ---------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc --------------------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2017, The Regents of the University of California
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 
29 
34 #ifndef _OPERATIONS_H_
35 #define _OPERATIONS_H_
36 
37 #include <iostream>
38 #include <functional>
39 #include <cmath>
40 #include <limits>
41 #include "psort/MersenneTwister.h"
42 
43 namespace combblas {
44 
45 template<typename T1, typename T2>
47 {
48  bool operator()(std::pair<T1,T2> & lhs, std::pair<T1,T2> & rhs){
49  return lhs.first == rhs.first;
50  }
51 };
52 
53 
54 
55 template<typename T>
56 struct myset: public std::unary_function<T, T>
57 {
58  myset(T myvalue): value(myvalue) {};
60  const T& operator()(const T& x) const
61  {
62  return value;
63  }
64  T value;
65 };
66 
67 
68 template<typename T>
69 struct identity : public std::unary_function<T, T>
70 {
72  const T operator()(const T& x) const
73  {
74  return x;
75  }
76 };
77 
78 
79 // Because identify reports ambiguity in PGI compilers
80 template<typename T>
81 struct myidentity : public std::unary_function<T, T>
82 {
84  const T operator()(const T& x) const
85  {
86  return x;
87  }
88 };
89 
90 
91 template<typename T>
92 struct totality : public std::unary_function<T, bool>
93 {
95  bool operator()(const T& x) const
96  {
97  return true;
98  }
99 };
100 
101 
102 template<typename T>
103 struct safemultinv : public std::unary_function<T, T>
104 {
105  const T operator()(const T& x) const
106  {
107  T inf = std::numeric_limits<T>::max();
108  return (x == 0) ? inf:(1/x);
109  }
110 };
111 
112 
113 template<typename T>
114 struct sel2nd: public std::binary_function<T, T, T>
115 {
116  const T& operator()(const T& x, const T & y) const
117  {
118  return y;
119  }
120 };
121 
122 template<typename T1, typename T2>
123 struct bintotality : public std::binary_function<T1, T2, bool>
124 {
126  bool operator()(const T1& x, const T2 & y) const
127  {
128  return true;
129  }
130 };
131 
132 
133 
140 struct exponentiate : public std::binary_function<double, double, double>
141 {
142  double operator()(double x, double y) const { return std::pow(x, y); }
143 };
144 
145 
153 template<typename T>
154 struct maximum : public std::binary_function<T, T, T>
155 {
157  const T operator()(const T& x, const T& y) const
158  {
159  return x < y? y : x;
160  }
161 };
162 
163 
171 template<typename T>
172 struct minimum : public std::binary_function<T, T, T>
173 {
175  const T operator()(const T& x, const T& y) const
176  {
177  return x < y? x : y;
178  }
179 };
180 
184 template<typename T>
185 struct RandReduce : public std::binary_function<T, T, T>
186 {
188  const T operator()(const T& x, const T& y)
189  {
190  return (M.rand() < 0.5)? x : y;
191  }
193  {
194  #ifdef DETERMINISTIC
195  M = MTRand(1);
196  #else
197  M = MTRand(); // generate random numbers with Mersenne Twister
198  #endif
199  }
201 };
202 
206 template<typename T>
207 struct SetIfNotEqual : public std::binary_function<T, T, T>
208 {
209  const T operator()(const T& x, const T& y)
210  {
211  if(x != y)
212  {
213  return valuetoset;
214  }
215  else
216  {
217  return x;
218  }
219  }
220  SetIfNotEqual(T value):valuetoset(value) { };
221  T valuetoset;
222 };
223 
224 
232 template<typename T>
233 struct bitwise_and : public std::binary_function<T, T, T>
234 {
236  T operator()(const T& x, const T& y) const
237  {
238  return x & y;
239  }
240 };
241 
242 
250 template<typename T>
251 struct bitwise_or : public std::binary_function<T, T, T>
252 {
254  T operator()(const T& x, const T& y) const
255  {
256  return x | y;
257  }
258 };
259 
267 template<typename T>
268 struct logical_xor : public std::binary_function<T, T, T>
269 {
271  T operator()(const T& x, const T& y) const
272  {
273  return (x || y) && !(x && y);
274  }
275 };
276 
285 template<typename T>
286 struct bitwise_xor : public std::binary_function<T, T, T>
287 {
289  T operator()(const T& x, const T& y) const
290  {
291  return x ^ y;
292  }
293 };
294 
295 }
296 
297 
298 
299 #endif
300 
301 
Returns a special value (passed to the constructor of the functor) when both operants disagree...
Definition: Operations.h:207
Compute the minimum of two values.
Definition: Operations.h:172
bool operator()(const T1 &x, const T2 &y) const
Definition: Operations.h:126
Compute the bitwise AND of two integral values.
Definition: Operations.h:233
const T operator()(const T &x) const
Definition: Operations.h:105
Compute the maximum of two values.
Definition: Operations.h:154
Compute the bitwise OR of two integral values.
Definition: Operations.h:251
const T operator()(const T &x, const T &y) const
Definition: Operations.h:175
const T operator()(const T &x, const T &y)
Definition: Operations.h:209
T operator()(const T &x, const T &y) const
Definition: Operations.h:289
With 50/50 chances, return a one of the operants.
Definition: Operations.h:185
const T operator()(const T &x) const
Definition: Operations.h:84
double operator()(double x, double y) const
Definition: Operations.h:142
Compute the bitwise exclusive OR of two integral values.
Definition: Operations.h:286
const T & operator()(const T &x) const
Definition: Operations.h:60
Compute the logical exclusive OR of two integral values.
Definition: Operations.h:268
const T operator()(const T &x, const T &y) const
Definition: Operations.h:157
T operator()(const T &x, const T &y) const
Definition: Operations.h:236
const T operator()(const T &x) const
Definition: Operations.h:72
myset(T myvalue)
Definition: Operations.h:58
bool operator()(std::pair< T1, T2 > &lhs, std::pair< T1, T2 > &rhs)
Definition: Operations.h:48
const T & operator()(const T &x, const T &y) const
Definition: Operations.h:116
const T operator()(const T &x, const T &y)
Definition: Operations.h:188
Definition: CCGrid.h:4
bool operator()(const T &x) const
Definition: Operations.h:95
T operator()(const T &x, const T &y) const
Definition: Operations.h:271
T operator()(const T &x, const T &y) const
Definition: Operations.h:254