COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
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
43namespace combblas {
44
45template<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
55template<typename T>
56struct myset: public std::unary_function<T, T>
57{
60 const T& operator()(const T& x) const
61 {
62 return value;
63 }
65};
66
67
68template<typename T>
69struct 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
80template<typename T>
81struct myidentity : public std::unary_function<T, T>
82{
84 const T operator()(const T& x) const
85 {
86 return x;
87 }
88};
89
90
91template<typename T>
92struct totality : public std::unary_function<T, bool>
93{
95 bool operator()(const T& x) const
96 {
97 return true;
98 }
99};
100
101
102template<typename T>
103struct 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
113template<typename T>
114struct 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
122template<typename T1, typename T2>
123struct 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
140struct 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
153template<typename T>
154struct 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
171template<typename T>
172struct 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
184template<typename T>
185struct 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
206template<typename T>
207struct 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) { };
222};
223
224
232template<typename T>
233struct 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
250template<typename T>
251struct 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
267template<typename T>
268struct 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
285template<typename T>
286struct 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
double rand()
With 50/50 chances, return a one of the operants.
Definition Operations.h:186
const T operator()(const T &x, const T &y)
Definition Operations.h:188
Returns a special value (passed to the constructor of the functor) when both operants disagree.
Definition Operations.h:208
const T operator()(const T &x, const T &y)
Definition Operations.h:209
bool operator()(const T1 &x, const T2 &y) const
Definition Operations.h:126
Compute the bitwise AND of two integral values.
Definition Operations.h:234
T operator()(const T &x, const T &y) const
Definition Operations.h:236
Compute the bitwise OR of two integral values.
Definition Operations.h:252
T operator()(const T &x, const T &y) const
Definition Operations.h:254
Compute the bitwise exclusive OR of two integral values.
Definition Operations.h:287
T operator()(const T &x, const T &y) const
Definition Operations.h:289
bool operator()(std::pair< T1, T2 > &lhs, std::pair< T1, T2 > &rhs)
Definition Operations.h:48
double operator()(double x, double y) const
Definition Operations.h:142
const T operator()(const T &x) const
Definition Operations.h:72
Compute the logical exclusive OR of two integral values.
Definition Operations.h:269
T operator()(const T &x, const T &y) const
Definition Operations.h:271
Compute the maximum of two values.
Definition Operations.h:155
const T operator()(const T &x, const T &y) const
Definition Operations.h:157
Compute the minimum of two values.
Definition Operations.h:173
const T operator()(const T &x, const T &y) const
Definition Operations.h:175
const T operator()(const T &x) const
Definition Operations.h:84
myset(T myvalue)
Definition Operations.h:58
const T & operator()(const T &x) const
Definition Operations.h:60
const T operator()(const T &x) const
Definition Operations.h:105
const T & operator()(const T &x, const T &y) const
Definition Operations.h:116
bool operator()(const T &x) const
Definition Operations.h:95