COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
Semirings.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
30#ifndef _SEMIRINGS_H_
31#define _SEMIRINGS_H_
32
33#include <utility>
34#include <climits>
35#include <cmath>
36#include "promote.h"
37
38namespace combblas {
39
40template <typename T>
41const T inf_plus(const T& a, const T& b) {
42 T inf = std::numeric_limits<T>::max();
43 if (a == inf || b == inf){
44 return inf;
45 }
46 return a + b;
47}
48
49// This semiring is used in indexing (SpParMat::operator())
50template <class OUT>
52{
53 static OUT id() { return OUT(); }
54 static bool returnedSAID() { return false; }
55 static OUT add(const OUT & arg1, const OUT & arg2)
56 {
57 std::cout << "Add should not happen (BoolCopy2ndSRing)!" << std::endl;
58 throw std::string("Add should not happen!");
59 std::exit(1);
60 return arg2;
61 }
62 static const OUT& multiply(bool arg1, const OUT & arg2)
63 {
64 return arg2;
65 }
66 static void axpy(bool a, const OUT & x, OUT & y)
67 {
68 y = multiply(a, x);
69 }
70
71 static MPI_Op mpi_op()
72 {
73 static MPI_Op mpiop;
74 static bool exists = false;
75 if (exists)
76 return mpiop;
77 else
78 {
80 exists = true;
81 return mpiop;
82 }
83 }
84
85 static void MPI_func(void * invec, void * inoutvec, int * len, MPI_Datatype *datatype)
86 {
87 if (*len > 0)
88 {
89 std::cout << "MPI Add should not happen (BoolCopy2ndSRing)!" << std::endl;
90 std::exit(1);
91 }
92 }
93};
94
95// This semiring is used in indexing (SpParMat::operator())
96template <class OUT>
98{
99 static OUT id() { return OUT(); }
100 static bool returnedSAID() { return false; }
101 static OUT add(const OUT & arg1, const OUT & arg2)
102 {
103 std::cout << "Add should not happen (BoolCopy1stSRing)!" << std::endl;
104 throw std::string("Add should not happen!");
105 std::exit(1);
106 return arg2;
107 }
108 static const OUT& multiply(const OUT & arg1, bool arg2)
109 {
110 return arg1;
111 }
112 static void axpy(const OUT& a, bool x, OUT & y)
113 {
114 y = multiply(a, x);
115 }
116
117 static MPI_Op mpi_op()
118 {
119 static MPI_Op mpiop;
120 static bool exists = false;
121 if (exists)
122 return mpiop;
123 else
124 {
126 exists = true;
127 return mpiop;
128 }
129 }
130
131 static void MPI_func(void * invec, void * inoutvec, int * len, MPI_Datatype *datatype)
132 {
133 if (*len > 0)
134 {
135 std::cout << "MPI Add should not happen (BoolCopy1stSRing)!" << std::endl;
136 std::exit(1);
137 }
138 }
139};
140
141
142
143template <class T1, class T2, class OUT>
145{
146 static OUT id() { return OUT(); }
147 static bool returnedSAID() { return false; }
148 static MPI_Op mpi_op() { return MPI_MAX; };
149 static OUT add(const OUT & arg1, const OUT & arg2)
150 {
151 return arg2;
152 }
153 static OUT multiply(const T1 & arg1, const T2 & arg2)
154 {
155 // fragile since it wouldn't work with y <- x*A
156 return static_cast<OUT>(arg2);
157 }
158 static void axpy(T1 a, const T2 & x, OUT & y)
159 {
160 //y = add(y, multiply(a, x));
161 y = multiply(a, x);
162 }
163};
164
165template <class T1, class T2>
167{
169 static T_promote id() { return -1; };
170 static bool returnedSAID() { return false; }
171 static MPI_Op mpi_op() { return MPI_MAX; };
172 static T_promote add(const T_promote & arg1, const T_promote & arg2)
173 {
174 return std::max(arg1, arg2);
175 }
176 static T_promote multiply(const T1 & arg1, const T2 & arg2)
177 {
178 // we could have just returned arg2 but it would be
179 // fragile since it wouldn't work with y <- x*A
180 return (static_cast<T_promote>(arg1) *
181 static_cast<T_promote>(arg2) );
182 }
183 static void axpy(T1 a, const T2 & x, T_promote & y)
184 {
185 y = std::max(y, static_cast<T_promote>(a*x));
186 }
187};
188
189
190// This one is used for BFS iteration
191template <class T2>
193{
194 typedef T2 T_promote;
195 static T_promote id(){ return -1; };
196 static bool returnedSAID() { return false; }
197 static MPI_Op mpi_op() { return MPI_MAX; };
198 static T_promote add(const T_promote & arg1, const T_promote & arg2)
199 {
200 return std::max(arg1, arg2);
201 }
202 static T_promote multiply(const bool & arg1, const T2 & arg2)
203 {
204 return arg2;
205 }
206 static void axpy(bool a, const T2 & x, T_promote & y)
207 {
208 y = std::max(y, x);
209 }
210};
211
212template <class T1, class T2>
214{
216 static T_promote id(){ return 0; }
217 static bool returnedSAID() { return false; }
218 static MPI_Op mpi_op() { return MPI_SUM; };
219 static T_promote add(const T_promote & arg1, const T_promote & arg2)
220 {
221 return arg1+arg2;
222 }
223 static T_promote multiply(const T1 & arg1, const T2 & arg2)
224 {
225 return (static_cast<T_promote>(arg1) *
226 static_cast<T_promote>(arg2) );
227 }
228 static void axpy(T1 a, const T2 & x, T_promote & y)
229 {
230 y += a*x;
231 }
232};
233
234
235template <class T1, class T2>
237{
239 static T_promote id() { return std::numeric_limits<T_promote>::max(); };
240 static bool returnedSAID() { return false; }
241 static MPI_Op mpi_op() { return MPI_MIN; };
242 static T_promote add(const T_promote & arg1, const T_promote & arg2)
243 {
244 return std::min(arg1, arg2);
245 }
246 static T_promote multiply(const T1 & arg1, const T2 & arg2)
247 {
249 (static_cast<T_promote>(arg1), static_cast<T_promote>(arg2));
250 }
251 static void axpy(T1 a, const T2 & x, T_promote & y)
252 {
253 y = std::min(y, multiply(a, x));
254 }
255};
256
257}
258
259#endif
static MPI_Op mpi_op()
Definition Semirings.h:117
static const OUT & multiply(const OUT &arg1, bool arg2)
Definition Semirings.h:108
static OUT add(const OUT &arg1, const OUT &arg2)
Definition Semirings.h:101
static void axpy(const OUT &a, bool x, OUT &y)
Definition Semirings.h:112
static bool returnedSAID()
Definition Semirings.h:100
static void MPI_func(void *invec, void *inoutvec, int *len, MPI_Datatype *datatype)
Definition Semirings.h:131
static bool returnedSAID()
Definition Semirings.h:54
static MPI_Op mpi_op()
Definition Semirings.h:71
static const OUT & multiply(bool arg1, const OUT &arg2)
Definition Semirings.h:62
static void MPI_func(void *invec, void *inoutvec, int *len, MPI_Datatype *datatype)
Definition Semirings.h:85
static OUT add(const OUT &arg1, const OUT &arg2)
Definition Semirings.h:55
static void axpy(bool a, const OUT &x, OUT &y)
Definition Semirings.h:66
static T_promote id()
Definition Semirings.h:239
promote_trait< T1, T2 >::T_promote T_promote
Definition Semirings.h:238
static T_promote multiply(const T1 &arg1, const T2 &arg2)
Definition Semirings.h:246
static T_promote add(const T_promote &arg1, const T_promote &arg2)
Definition Semirings.h:242
static bool returnedSAID()
Definition Semirings.h:240
static void axpy(T1 a, const T2 &x, T_promote &y)
Definition Semirings.h:251
static MPI_Op mpi_op()
Definition Semirings.h:241
static T_promote multiply(const T1 &arg1, const T2 &arg2)
Definition Semirings.h:223
static T_promote add(const T_promote &arg1, const T_promote &arg2)
Definition Semirings.h:219
promote_trait< T1, T2 >::T_promote T_promote
Definition Semirings.h:215
static void axpy(T1 a, const T2 &x, T_promote &y)
Definition Semirings.h:228
static T_promote id()
Definition Semirings.h:216
static MPI_Op mpi_op()
Definition Semirings.h:218
static bool returnedSAID()
Definition Semirings.h:217
static void axpy(T1 a, const T2 &x, OUT &y)
Definition Semirings.h:158
static MPI_Op mpi_op()
Definition Semirings.h:148
static bool returnedSAID()
Definition Semirings.h:147
static OUT multiply(const T1 &arg1, const T2 &arg2)
Definition Semirings.h:153
static OUT add(const OUT &arg1, const OUT &arg2)
Definition Semirings.h:149
static T_promote add(const T_promote &arg1, const T_promote &arg2)
Definition Semirings.h:198
static void axpy(bool a, const T2 &x, T_promote &y)
Definition Semirings.h:206
static T_promote multiply(const bool &arg1, const T2 &arg2)
Definition Semirings.h:202
static T_promote multiply(const T1 &arg1, const T2 &arg2)
Definition Semirings.h:176
static T_promote id()
Definition Semirings.h:169
static void axpy(T1 a, const T2 &x, T_promote &y)
Definition Semirings.h:183
static bool returnedSAID()
Definition Semirings.h:170
static MPI_Op mpi_op()
Definition Semirings.h:171
static T_promote add(const T_promote &arg1, const T_promote &arg2)
Definition Semirings.h:172
promote_trait< T1, T2 >::T_promote T_promote
Definition Semirings.h:168