1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/****************************************************************/
/* Parallel Combinatorial BLAS Library (for Graph Computations) */
/* version 1.6 -------------------------------------------------*/
/* date: 6/15/2017 ---------------------------------------------*/
/* authors: Ariful Azad, Aydin Buluc --------------------------*/
/****************************************************************/
/*
Copyright (c) 2010-2017, The Regents of the University of California
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _SEMIRINGS_H_
#define _SEMIRINGS_H_
#include <utility>
#include <climits>
#include <cmath>
#include "promote.h"
namespace combblas {
template <typename T>
const T inf_plus(const T& a, const T& b) {
T inf = std::numeric_limits<T>::max();
if (a == inf || b == inf){
return inf;
}
return a + b;
}
// This semiring is used in indexing (SpParMat::operator())
template <class OUT>
struct BoolCopy2ndSRing
{
static OUT id() { return OUT(); }
static bool returnedSAID() { return false; }
static OUT add(const OUT & arg1, const OUT & arg2)
{
std::cout << "Add should not happen (BoolCopy2ndSRing)!" << std::endl;
throw std::string("Add should not happen!");
std::exit(1);
return arg2;
}
static const OUT& multiply(bool arg1, const OUT & arg2)
{
return arg2;
}
static void axpy(bool a, const OUT & x, OUT & y)
{
y = multiply(a, x);
}
static MPI_Op mpi_op()
{
static MPI_Op mpiop;
static bool exists = false;
if (exists)
return mpiop;
else
{
MPI_Op_create(MPI_func, true, &mpiop);
exists = true;
return mpiop;
}
}
static void MPI_func(void * invec, void * inoutvec, int * len, MPI_Datatype *datatype)
{
if (*len > 0)
{
std::cout << "MPI Add should not happen (BoolCopy2ndSRing)!" << std::endl;
std::exit(1);
}
}
};
// This semiring is used in indexing (SpParMat::operator())
template <class OUT>
struct BoolCopy1stSRing
{
static OUT id() { return OUT(); }
static bool returnedSAID() { return false; }
static OUT add(const OUT & arg1, const OUT & arg2)
{
std::cout << "Add should not happen (BoolCopy1stSRing)!" << std::endl;
throw std::string("Add should not happen!");
std::exit(1);
return arg2;
}
static const OUT& multiply(const OUT & arg1, bool arg2)
{
return arg1;
}
static void axpy(const OUT& a, bool x, OUT & y)
{
y = multiply(a, x);
}
static MPI_Op mpi_op()
{
static MPI_Op mpiop;
static bool exists = false;
if (exists)
return mpiop;
else
{
MPI_Op_create(MPI_func, true, &mpiop);
exists = true;
return mpiop;
}
}
static void MPI_func(void * invec, void * inoutvec, int * len, MPI_Datatype *datatype)
{
if (*len > 0)
{
std::cout << "MPI Add should not happen (BoolCopy1stSRing)!" << std::endl;
std::exit(1);
}
}
};
template <class T1, class T2, class OUT>
struct Select2ndSRing
{
static OUT id() { return OUT(); }
static bool returnedSAID() { return false; }
static MPI_Op mpi_op() { return MPI_MAX; };
static OUT add(const OUT & arg1, const OUT & arg2)
{
return arg2;
}
static OUT multiply(const T1 & arg1, const T2 & arg2)
{
// fragile since it wouldn't work with y <- x*A
return static_cast<OUT>(arg2);
}
static void axpy(T1 a, const T2 & x, OUT & y)
{
//y = add(y, multiply(a, x));
y = multiply(a, x);
}
};
template <class T1, class T2>
struct SelectMaxSRing
{
typedef typename promote_trait<T1,T2>::T_promote T_promote;
static T_promote id() { return -1; };
static bool returnedSAID() { return false; }
static MPI_Op mpi_op() { return MPI_MAX; };
static T_promote add(const T_promote & arg1, const T_promote & arg2)
{
return std::max(arg1, arg2);
}
static T_promote multiply(const T1 & arg1, const T2 & arg2)
{
// we could have just returned arg2 but it would be
// fragile since it wouldn't work with y <- x*A
return (static_cast<T_promote>(arg1) *
static_cast<T_promote>(arg2) );
}
static void axpy(T1 a, const T2 & x, T_promote & y)
{
y = std::max(y, static_cast<T_promote>(a*x));
}
};
// This one is used for BFS iteration
template <class T2>
struct SelectMaxSRing<bool, T2>
{
typedef T2 T_promote;
static T_promote id(){ return -1; };
static bool returnedSAID() { return false; }
static MPI_Op mpi_op() { return MPI_MAX; };
static T_promote add(const T_promote & arg1, const T_promote & arg2)
{
return std::max(arg1, arg2);
}
static T_promote multiply(const bool & arg1, const T2 & arg2)
{
return arg2;
}
static void axpy(bool a, const T2 & x, T_promote & y)
{
y = std::max(y, x);
}
};
template <class T1, class T2>
struct PlusTimesSRing
{
typedef typename promote_trait<T1,T2>::T_promote T_promote;
static T_promote id(){ return 0; }
static bool returnedSAID() { return false; }
static MPI_Op mpi_op() { return MPI_SUM; };
static T_promote add(const T_promote & arg1, const T_promote & arg2)
{
return arg1+arg2;
}
static T_promote multiply(const T1 & arg1, const T2 & arg2)
{
return (static_cast<T_promote>(arg1) *
static_cast<T_promote>(arg2) );
}
static void axpy(T1 a, const T2 & x, T_promote & y)
{
y += a*x;
}
};
template <class T1, class T2>
struct MinPlusSRing
{
typedef typename promote_trait<T1,T2>::T_promote T_promote;
static T_promote id() { return std::numeric_limits<T_promote>::max(); };
static bool returnedSAID() { return false; }
static MPI_Op mpi_op() { return MPI_MIN; };
static T_promote add(const T_promote & arg1, const T_promote & arg2)
{
return std::min(arg1, arg2);
}
static T_promote multiply(const T1 & arg1, const T2 & arg2)
{
return inf_plus< T_promote >
(static_cast<T_promote>(arg1), static_cast<T_promote>(arg2));
}
static void axpy(T1 a, const T2 & x, T_promote & y)
{
y = std::min(y, multiply(a, x));
}
};
}
#endif
|