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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
//------------------------------------------------------------------------------
// GB_iso_add: apply a binary op and check for iso result for C=A+B
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Compute c = op(a,b) for two matrices A and B, and return true if C=A+B
// results in an iso matrix C. If true, the output scalar c is the iso value
// for the matrix C.
#include "GB_add.h"
#include "GB_emult.h"
bool GB_iso_add // c = op(a,b), return true if C is iso
(
// output
GB_void *restrict c, // output scalar of iso array
// input
GrB_Type ctype, // type of c
GrB_Matrix A, // input matrix
const GB_void *restrict alpha_scalar, // of type op->xtype
GrB_Matrix B, // input matrix
const GB_void *restrict beta_scalar, // of type op->ytype
GrB_BinaryOp op, // binary operator, if present
const bool is_eWiseUnion
)
{
//--------------------------------------------------------------------------
// get inputs
//--------------------------------------------------------------------------
ASSERT_MATRIX_OK (A, "A for GB_iso_add", GB0) ;
ASSERT_MATRIX_OK (B, "B for GB_iso_add", GB0) ;
ASSERT_TYPE_OK (ctype, "ctype for GB_iso_add", GB0) ;
ASSERT_BINARYOP_OK_OR_NULL (op, "op for GB_iso_add", GB0) ;
ASSERT (c != NULL) ;
//--------------------------------------------------------------------------
// special case if both A and B are full (or as-if-full)
//--------------------------------------------------------------------------
if (GB_as_if_full (A) && GB_as_if_full (B))
{
// A and B are both full (or as-if-full), and eWiseMult, eWiseAdd, and
// eWiseUnion all compute the same thing. GB_emult detects this
// condition and calls GB_add, so that GB_emult doesn't have to handle
// this case. GB_add tests the iso condition as if it were computing
// C=A.*B. The alpha_scalar and beta_scalar values are not used.
return (GB_iso_emult (c, ctype, A, B, op)) ;
}
//--------------------------------------------------------------------------
// quick return if the op is positional
//--------------------------------------------------------------------------
if (GB_OP_IS_POSITIONAL (op))
{
// C is not iso
return (false) ;
}
if (!is_eWiseUnion && (!A->iso || !B->iso))
{
// for eWiseAdd: A and B must be iso for C to be iso
return (false) ;
}
//--------------------------------------------------------------------------
// get the scalar codes and sizes for C, A, and B
//--------------------------------------------------------------------------
const size_t csize = ctype->size ;
const size_t asize = A->type->size ;
const size_t bsize = B->type->size ;
const GB_Type_code ccode = ctype->code ;
const GB_Type_code acode = A->type->code ;
const GB_Type_code bcode = B->type->code ;
// c = zero
memset (c, 0, csize) ;
//--------------------------------------------------------------------------
// handle the iso case for eWiseUnion and eWiseAdd
//--------------------------------------------------------------------------
if (is_eWiseUnion)
{
//======================================================================
// eWiseUnion:
//======================================================================
// for eWiseUnion: the type of C is always identical to op->ztype
ASSERT (op != NULL) ;
ASSERT (ctype == op->ztype) ;
// get the binary operator
const GxB_binary_function fadd = op->binop_function ;
const GB_Opcode opcode = op->opcode ;
const GrB_Type xtype = op->xtype ;
const GrB_Type ytype = op->ytype ;
const GB_Type_code xcode = xtype->code ;
const GB_Type_code ycode = ytype->code ;
const size_t xsize = xtype->size ;
const size_t ysize = ytype->size ;
if (opcode == GB_PAIR_binop_code)
{
//------------------------------------------------------------------
// C is iso, with c = 1
//------------------------------------------------------------------
GB_cast_one (c, ccode) ;
return (true) ;
}
else if (B->iso &&
(opcode == GB_SECOND_binop_code || opcode == GB_ANY_binop_code))
{
//------------------------------------------------------------------
// if b == beta: C is iso, with c = b
//------------------------------------------------------------------
// b = (ytype) Bx [0]
GB_void b [GB_VLA(ysize)] ;
GB_cast_scalar (b, ycode, B->x, bcode, bsize) ;
if (memcmp (b, beta_scalar, ysize) == 0)
{
// c = b
memcpy (c, b, ysize) ;
return (true) ;
}
}
else if (A->iso &&
(opcode == GB_FIRST_binop_code || opcode == GB_ANY_binop_code))
{
//------------------------------------------------------------------
// if a == alpha: C is iso, with c = a
//------------------------------------------------------------------
// a = (xtype) Ax [0]
GB_void a [GB_VLA(xsize)] ;
GB_cast_scalar (a, xcode, A->x, acode, asize) ;
if (memcmp (a, alpha_scalar, xsize) == 0)
{
// c = a
memcpy (c, a, xsize) ;
return (true) ;
}
}
else if (A->iso && B->iso)
{
//------------------------------------------------------------------
// C is iso if op(a,beta) == op(a,b) == op(alpha,b)
//------------------------------------------------------------------
// a = (xtype) Ax [0]
GB_void a [GB_VLA(xsize)] ;
GB_cast_scalar (a, xcode, A->x, acode, asize) ;
// b = (ytype) Bx [0]
GB_void b [GB_VLA(ysize)] ;
GB_cast_scalar (b, ycode, B->x, bcode, bsize) ;
// c = op(a,b)
fadd (c, a, b) ;
// t = op(a,beta)
GB_void t [GB_VLA(csize)] ;
fadd (t, a, beta_scalar) ;
if (memcmp (c, t, csize) != 0)
{
// op(a,b) != op(a,beta) so C is not iso
return (false) ;
}
// t = op(alpha,b)
fadd (t, alpha_scalar, b) ;
if (memcmp (c, t, csize) != 0)
{
// op(a,b) != op(alpha,b) so C is not iso
return (false) ;
}
// C is iso with c = op(a,b)
return (true) ;
}
}
else
{
//======================================================================
// eWiseAdd: both A and B must be iso
//======================================================================
//----------------------------------------------------------------------
// compare the iso values of A and B, typecasted to C
//----------------------------------------------------------------------
// a = (ctype) Ax [0]
GB_void a [GB_VLA(csize)] ;
GB_cast_scalar (a, ccode, A->x, acode, asize) ;
// b = (ctype) Bx [0]
GB_void b [GB_VLA(csize)] ;
GB_cast_scalar (b, ccode, B->x, bcode, bsize) ;
if (memcmp (a, b, csize) != 0)
{
// the iso values of A and B differ, when typecasted to C
return (false) ;
}
//----------------------------------------------------------------------
// compute the C iso value and compare with A and B
//----------------------------------------------------------------------
if (op == NULL)
{
// For GB_wait, the pattern of A and B are known to be disjoint, so
// no operator is used, and op is NULL. No typecasting is done.
ASSERT (ctype == A->type) ;
memcpy (c, a, csize) ;
return (true) ;
}
else
{
// get the binary operator
const GxB_binary_function fadd = op->binop_function ;
const GrB_Type xtype = op->xtype ;
const GrB_Type ytype = op->ytype ;
const GrB_Type ztype = op->ztype ;
const GB_Type_code xcode = xtype->code ;
const GB_Type_code ycode = ytype->code ;
const GB_Type_code zcode = ztype->code ;
const size_t xsize = xtype->size ;
const size_t ysize = ytype->size ;
const size_t zsize = ztype->size ;
// x = (xtype) Ax [0]
GB_void x [GB_VLA(xsize)] ;
GB_cast_scalar (x, xcode, A->x, acode, asize) ;
// y = (ytype) Bx [0]
GB_void y [GB_VLA(ysize)] ;
GB_cast_scalar (y, ycode, B->x, bcode, bsize) ;
// z = op (x,y)
GB_void z [GB_VLA(zsize)] ;
fadd (z, x, y) ;
// c = (ctype) z
GB_cast_scalar (c, ccode, z, zcode, zsize) ;
if (memcmp (c, a, csize) == 0)
{
// the iso values of C and A match
return (true) ;
}
}
}
// if the tests above fall through, C is not iso
return (false) ;
}
|