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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
//------------------------------------------------------------------------------
// GB_AxB_rowscale: C = D*B, row scale with diagonal matrix D
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
#include "GB_mxm.h"
#include "GB_binop.h"
#include "GB_apply.h"
#include "GB_stringify.h"
#ifndef GBCUDA_DEV
#include "GB_binop__include.h"
#endif
#define GB_FREE_ALL GB_phybix_free (C) ;
GrB_Info GB_AxB_rowscale // C = D*B, row scale with diagonal D
(
GrB_Matrix C, // output matrix, static header
const GrB_Matrix D, // diagonal input matrix
const GrB_Matrix B, // input matrix
const GrB_Semiring semiring, // semiring that defines C=D*B
const bool flipxy, // if true, do z=fmult(b,a) vs fmult(a,b)
GB_Context Context
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
GrB_Info info ;
ASSERT (C != NULL && (C->static_header || GBNSTATIC)) ;
ASSERT_MATRIX_OK (D, "D for rowscale D*B", GB0) ;
ASSERT_MATRIX_OK (B, "B for rowscale D*B", GB0) ;
ASSERT (!GB_ZOMBIES (D)) ;
ASSERT (!GB_JUMBLED (D)) ;
ASSERT (!GB_PENDING (D)) ;
ASSERT (!GB_ZOMBIES (B)) ;
ASSERT (GB_JUMBLED_OK (B)) ;
ASSERT (!GB_PENDING (B)) ;
ASSERT_SEMIRING_OK (semiring, "semiring for numeric D*B", GB0) ;
ASSERT (D->vdim == B->vlen) ;
ASSERT (GB_is_diagonal (D, Context)) ;
ASSERT (!GB_IS_BITMAP (D)) ; // bitmap or full: not needed
ASSERT (!GB_IS_BITMAP (B)) ;
ASSERT (!GB_IS_FULL (D)) ;
GBURBLE ("(%s=%s*%s) ",
GB_sparsity_char_matrix (B), // C has the sparsity structure of B
GB_sparsity_char_matrix (D),
GB_sparsity_char_matrix (B)) ;
//--------------------------------------------------------------------------
// get the semiring operators
//--------------------------------------------------------------------------
GrB_BinaryOp mult = semiring->multiply ;
GrB_Type ztype = mult->ztype ;
ASSERT (ztype == semiring->add->op->ztype) ;
GB_Opcode opcode = mult->opcode ;
// GB_reduce_to_vector does not use GB_AxB_rowscale:
ASSERT (!(mult->binop_function == NULL &&
(opcode == GB_FIRST_binop_code || opcode == GB_SECOND_binop_code))) ;
//--------------------------------------------------------------------------
// determine if C is iso (ignore the monoid since it isn't used)
//--------------------------------------------------------------------------
size_t zsize = ztype->size ;
GB_void cscalar [GB_VLA(zsize)] ;
bool C_iso = GB_iso_AxB (cscalar, D, B, D->vdim, semiring, flipxy, true) ;
#ifdef GB_DEBUGIFY_DEFN
GB_debugify_mxm (C_iso, GB_sparsity (B), ztype, NULL, false, false,
semiring, flipxy, D, B) ;
#endif
//--------------------------------------------------------------------------
// copy the pattern of B into C
//--------------------------------------------------------------------------
// allocate C->x but do not initialize it
// set C->iso = C_iso OK
GB_OK (GB_dup_worker (&C, C_iso, B, false, ztype, Context)) ;
GB_void *restrict Cx = (GB_void *) C->x ;
//--------------------------------------------------------------------------
// C = D*B, row scale, compute numerical values
//--------------------------------------------------------------------------
if (GB_OPCODE_IS_POSITIONAL (opcode))
{
//----------------------------------------------------------------------
// apply a positional operator: convert C=D*B to C=op(B)
//----------------------------------------------------------------------
// determine unary operator to compute C=D*B
ASSERT (!flipxy) ;
GrB_UnaryOp op = NULL ;
if (ztype == GrB_INT64)
{
switch (opcode)
{
// first_op(D,B) becomes position_i(B)
case GB_FIRSTI_binop_code :
case GB_FIRSTJ_binop_code : op = GxB_POSITIONI_INT64 ; break;
case GB_FIRSTI1_binop_code :
case GB_FIRSTJ1_binop_code : op = GxB_POSITIONI1_INT64; break;
// second_op(D,B) becomes position_op(B)
case GB_SECONDI_binop_code : op = GxB_POSITIONI_INT64 ; break;
case GB_SECONDJ_binop_code : op = GxB_POSITIONJ_INT64 ; break;
case GB_SECONDI1_binop_code : op = GxB_POSITIONI1_INT64; break;
case GB_SECONDJ1_binop_code : op = GxB_POSITIONJ1_INT64; break;
default: ;
}
}
else
{
switch (opcode)
{
// first_op(D,B) becomes position_i(B)
case GB_FIRSTI_binop_code :
case GB_FIRSTJ_binop_code : op = GxB_POSITIONI_INT32 ; break;
case GB_FIRSTI1_binop_code :
case GB_FIRSTJ1_binop_code : op = GxB_POSITIONI1_INT32; break;
// second_op(D,B) becomes position_op(B)
case GB_SECONDI_binop_code : op = GxB_POSITIONI_INT32 ; break;
case GB_SECONDJ_binop_code : op = GxB_POSITIONJ_INT32 ; break;
case GB_SECONDI1_binop_code : op = GxB_POSITIONI1_INT32; break;
case GB_SECONDJ1_binop_code : op = GxB_POSITIONJ1_INT32; break;
default: ;
}
}
GB_OK (GB_apply_op (Cx, C->type, GB_NON_ISO,
(GB_Operator) op, // positional op
NULL, false, false, B, Context)) ;
ASSERT_MATRIX_OK (C, "rowscale positional: C = D*B output", GB0) ;
}
else if (C_iso)
{
//----------------------------------------------------------------------
// C is iso; pattern already computed above
//----------------------------------------------------------------------
GBURBLE ("(iso rowscale) ") ;
memcpy (Cx, cscalar, zsize) ;
}
else
{
//----------------------------------------------------------------------
// C is non iso
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// determine if the values are accessed
//----------------------------------------------------------------------
bool op_is_first = (opcode == GB_FIRST_binop_code) ;
bool op_is_second = (opcode == GB_SECOND_binop_code) ;
bool op_is_pair = (opcode == GB_PAIR_binop_code) ;
bool D_is_pattern = false ;
bool B_is_pattern = false ;
ASSERT (!op_is_pair) ;
if (flipxy)
{
// z = fmult (b,a) will be computed
D_is_pattern = op_is_first || op_is_pair ;
B_is_pattern = op_is_second || op_is_pair ;
ASSERT (GB_IMPLIES (!D_is_pattern,
GB_Type_compatible (D->type, mult->ytype))) ;
ASSERT (GB_IMPLIES (!B_is_pattern,
GB_Type_compatible (B->type, mult->xtype))) ;
}
else
{
// z = fmult (a,b) will be computed
D_is_pattern = op_is_second || op_is_pair ;
B_is_pattern = op_is_first || op_is_pair ;
ASSERT (GB_IMPLIES (!D_is_pattern,
GB_Type_compatible (D->type, mult->xtype))) ;
ASSERT (GB_IMPLIES (!B_is_pattern,
GB_Type_compatible (B->type, mult->ytype))) ;
}
//----------------------------------------------------------------------
// determine the number of threads to use
//----------------------------------------------------------------------
GB_GET_NTHREADS_MAX (nthreads_max, chunk, Context) ;
int nthreads = GB_nthreads (GB_nnz_held (B) + B->nvec, chunk,
nthreads_max) ;
bool done = false ;
#ifndef GBCUDA_DEV
//------------------------------------------------------------------
// define the worker for the switch factory
//------------------------------------------------------------------
#define GB_DxB(mult,xname) GB (_DxB_ ## mult ## xname)
#define GB_BINOP_WORKER(mult,xname) \
{ \
info = GB_DxB(mult,xname) (C, D, B, nthreads) ; \
done = (info != GrB_NO_VALUE) ; \
} \
break ;
//------------------------------------------------------------------
// launch the switch factory
//------------------------------------------------------------------
GB_Type_code xcode, ycode, zcode ;
if (GB_binop_builtin (D->type, D_is_pattern, B->type, B_is_pattern,
mult, flipxy, &opcode, &xcode, &ycode, &zcode))
{
// C=D*B, rowscale with built-in operator
#define GB_BINOP_IS_SEMIRING_MULTIPLIER
#define GB_NO_PAIR
#include "GB_binop_factory.c"
#undef GB_BINOP_IS_SEMIRING_MULTIPLIER
}
#endif
if (!done)
{
//------------------------------------------------------------------
// C = D*B, row scale, with typecasting or user-defined operator
//------------------------------------------------------------------
//------------------------------------------------------------------
// get operators, functions, workspace, contents of D, B, and C
//------------------------------------------------------------------
GB_BURBLE_MATRIX (C, "(generic C=D*B rowscale) ") ;
GxB_binary_function fmult = mult->binop_function ;
size_t csize = C->type->size ;
size_t dsize = D_is_pattern ? 0 : D->type->size ;
size_t bsize = B_is_pattern ? 0 : B->type->size ;
size_t xsize = mult->xtype->size ;
size_t ysize = mult->ytype->size ;
// scalar workspace: because of typecasting, the x/y types need not
// be the same as the size of the D and B types.
// flipxy false: dii = (xtype) D(i,i) and bij = (ytype) B(i,j)
// flipxy true: dii = (ytype) D(i,i) and bij = (xtype) B(i,j)
size_t dii_size = flipxy ? ysize : xsize ;
size_t bij_size = flipxy ? xsize : ysize ;
GB_cast_function cast_D, cast_B ;
if (flipxy)
{
// D is typecasted to y, and B is typecasted to x
cast_D = D_is_pattern ? NULL :
GB_cast_factory (mult->ytype->code, D->type->code) ;
cast_B = B_is_pattern ? NULL :
GB_cast_factory (mult->xtype->code, B->type->code) ;
}
else
{
// D is typecasted to x, and B is typecasted to y
cast_D = D_is_pattern ? NULL :
GB_cast_factory (mult->xtype->code, D->type->code) ;
cast_B = B_is_pattern ? NULL :
GB_cast_factory (mult->ytype->code, B->type->code) ;
}
//------------------------------------------------------------------
// C = D*B via function pointers, and typecasting
//------------------------------------------------------------------
// dii = D(i,i), located in Dx [i]
#define GB_A_IS_PATTERN 0
#define GB_GETA(dii,Dx,i,D_iso) \
GB_void dii [GB_VLA(dii_size)] ; \
if (!D_is_pattern) \
{ \
cast_D (dii, Dx +(D_iso ? 0:(i)*dsize), dsize) ; \
}
// bij = B(i,j), located in Bx [pB]
#define GB_B_IS_PATTERN 0
#define GB_GETB(bij,Bx,pB,B_iso) \
GB_void bij [GB_VLA(bij_size)] ; \
if (!B_is_pattern) \
{ \
cast_B (bij, Bx +(B_iso ? 0:(pB)*bsize), bsize) ; \
}
// address of Cx [p]
#define GB_CX(p) Cx +((p)*csize)
#define GB_ATYPE GB_void
#define GB_BTYPE GB_void
#define GB_CTYPE GB_void
// no vectorization
#define GB_PRAGMA_SIMD_VECTORIZE ;
if (flipxy)
{
#define GB_BINOP(z,x,y,i,j) fmult (z,y,x)
#include "GB_AxB_rowscale_template.c"
#undef GB_BINOP
}
else
{
#define GB_BINOP(z,x,y,i,j) fmult (z,x,y)
#include "GB_AxB_rowscale_template.c"
#undef GB_BINOP
}
}
}
//--------------------------------------------------------------------------
// return result
//--------------------------------------------------------------------------
ASSERT_MATRIX_OK (C, "rowscale: C = D*B output", GB0) ;
return (GrB_SUCCESS) ;
}
|