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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
//------------------------------------------------------------------------------
// GrB_Matrix_apply: apply a unary or binary operator to a matrix
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
#include "GB_apply.h"
#include "GB_scalar.h"
#include "GB_get_mask.h"
//------------------------------------------------------------------------------
// GrB_Matrix_apply: apply a unary operator to a matrix
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply // C<M> = accum (C, op(A)) or op(A')
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M_in, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_UnaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
GB_WHERE (C, "GrB_Matrix_apply (C, M, accum, op, A, desc)") ;
GB_BURBLE_START ("GrB_apply (unary op)") ;
GB_RETURN_IF_NULL_OR_FAULTY (C) ;
GB_RETURN_IF_FAULTY (M_in) ;
GB_RETURN_IF_NULL_OR_FAULTY (A) ;
// get the descriptor
GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
A_transpose, xx1, xx2, xx7) ;
// get the mask
GrB_Matrix M = GB_get_mask (M_in, &Mask_comp, &Mask_struct) ;
//--------------------------------------------------------------------------
// apply the operator and optionally transpose
//--------------------------------------------------------------------------
info = GB_apply (
C, C_replace, // C and its descriptor
M, Mask_comp, Mask_struct, // mask and its descriptor
accum, // optional accum for Z=accum(C,T)
(GB_Operator) op, NULL, false, // operator op(.) to apply to the entries
A, A_transpose, // A and its descriptor
Context) ;
GB_BURBLE_END ;
return (info) ;
}
//------------------------------------------------------------------------------
// GB_1st: apply a binary operator: op(x,A)
//------------------------------------------------------------------------------
static inline GrB_Info GB_1st // C<M>=accum(C,op(x,A))
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M_in, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Scalar x, // first input: scalar x
const GrB_Matrix A, // second input: matrix A
const GrB_Descriptor desc, // descriptor for C, M, and A
GB_Context Context
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
GB_BURBLE_START ("GrB_apply (bind 1st)") ;
GB_RETURN_IF_NULL_OR_FAULTY (C) ;
GB_RETURN_IF_FAULTY (M_in) ;
GB_RETURN_IF_NULL_OR_FAULTY (x) ;
GB_RETURN_IF_NULL_OR_FAULTY (A) ;
// get the descriptor, using GrB_INP1 to transpose the matrix
GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
xx1, A_transpose, xx2, xx7) ;
// get the mask
GrB_Matrix M = GB_get_mask (M_in, &Mask_comp, &Mask_struct) ;
//--------------------------------------------------------------------------
// apply the operator and optionally transpose
//--------------------------------------------------------------------------
info = GB_apply (
C, C_replace, // C and its descriptor
M, Mask_comp, Mask_struct, // mask and its descriptor
accum, // optional accum for Z=accum(C,T)
(GB_Operator) op, x, true, // operator op(x,.) to apply to the entries
A, A_transpose, // A and its descriptor
Context) ;
GB_BURBLE_END ;
return (info) ;
}
//------------------------------------------------------------------------------
// GB_2nd: apply a binary operator or idxunop: op(A,y)
//------------------------------------------------------------------------------
static inline GrB_Info GB_2nd // C<M>=accum(C,op(A,y))
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M_in, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GB_Operator op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const GrB_Scalar y, // second input: scalar y
const GrB_Descriptor desc, // descriptor for C, M, and A
GB_Context Context
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
GB_BURBLE_START ("GrB_apply (bind 2nd) ") ;
GB_RETURN_IF_NULL_OR_FAULTY (C) ;
GB_RETURN_IF_FAULTY (M_in) ;
GB_RETURN_IF_NULL_OR_FAULTY (A) ;
GB_RETURN_IF_NULL_OR_FAULTY (y) ;
// get the descriptor, using GrB_INP0 to transpose the matrix
GB_GET_DESCRIPTOR (info, desc, C_replace, Mask_comp, Mask_struct,
A_transpose, xx1, xx2, xx7) ;
// get the mask
GrB_Matrix M = GB_get_mask (M_in, &Mask_comp, &Mask_struct) ;
//--------------------------------------------------------------------------
// apply the operator and optionally transpose
//--------------------------------------------------------------------------
info = GB_apply (
C, C_replace, // C and its descriptor
M, Mask_comp, Mask_struct, // mask and its descriptor
accum, // optional accum for Z=accum(C,T)
op, y, false, // operator op(.,y) to apply to the entries
A, A_transpose, // A and its descriptor
Context) ;
GB_BURBLE_END ;
return (info) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp1st_Scalar: apply a binary operator: op(x,A)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_BinaryOp1st_Scalar // C<M>=accum(C,op(x,A))
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Scalar x, // first input: scalar x
const GrB_Matrix A, // second input: matrix A
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_BinaryOp1st_Scalar"
" (C, M, accum, op, x, A, desc)") ;
return (GB_1st (C, M, accum, op, x, A, desc, Context)) ;
}
//------------------------------------------------------------------------------
// GxB_Matrix_apply_BinaryOp1st: historical
//------------------------------------------------------------------------------
// identical to GrB_Matrix_apply_BinaryOp1st_Scalar
GrB_Info GxB_Matrix_apply_BinaryOp1st
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Scalar x, // first input: scalar x
const GrB_Matrix A, // second input: matrix A
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
return (GrB_Matrix_apply_BinaryOp1st_Scalar (C, M, accum, op, x, A, desc)) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp2nd_Scalar: apply a binary operator: op(A,y)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_BinaryOp2nd_Scalar // C<M>=accum(C,op(A,y))
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const GrB_Scalar y, // second input: scalar y
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_BinaryOp2nd_Scalar"
" (C, M, accum, op, A, y, desc)") ;
return (GB_2nd (C, M, accum, (GB_Operator) op, A, y, desc, Context)) ;
}
//------------------------------------------------------------------------------
// GxB_Matrix_apply_BinaryOp2nd: historical
//------------------------------------------------------------------------------
// identical to GrB_Matrix_apply_BinaryOp2nd_Scalar
GrB_Info GxB_Matrix_apply_BinaryOp2nd
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const GrB_Scalar y, // second input: scalar y
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
return (GrB_Matrix_apply_BinaryOp2nd_Scalar (C, M, accum, op, A, y, desc)) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp1st_TYPE: apply a binary operator: op(x,A)
//------------------------------------------------------------------------------
#define GB_BIND1ST(prefix,type,T) \
GrB_Info GB_EVAL3 (prefix, _Matrix_apply_BinaryOp1st_, T) \
( \
GrB_Matrix C, /* input/output matrix for results */ \
const GrB_Matrix M, /* optional mask for C*/ \
const GrB_BinaryOp accum, /* optional accum for Z=accum(C,T) */ \
const GrB_BinaryOp op, /* operator to apply to the entries */ \
const type x, /* first input: scalar x */ \
const GrB_Matrix A, /* second input: matrix A */ \
const GrB_Descriptor desc /* descriptor for C, M, and A */ \
) \
{ \
GB_WHERE (C, GB_STR(prefix) "_Matrix_apply_BinaryOp1st_" GB_STR(T) \
" (C, M, accum, op, x, A, desc)") ; \
GB_SCALAR_WRAP (scalar, x, GB_EVAL3 (prefix, _, T)) ; \
return (GB_1st (C, M, accum, op, scalar, A, desc, Context)) ; \
}
GB_BIND1ST (GrB, bool , BOOL )
GB_BIND1ST (GrB, int8_t , INT8 )
GB_BIND1ST (GrB, int16_t , INT16 )
GB_BIND1ST (GrB, int32_t , INT32 )
GB_BIND1ST (GrB, int64_t , INT64 )
GB_BIND1ST (GrB, uint8_t , UINT8 )
GB_BIND1ST (GrB, uint16_t , UINT16)
GB_BIND1ST (GrB, uint32_t , UINT32)
GB_BIND1ST (GrB, uint64_t , UINT64)
GB_BIND1ST (GrB, float , FP32 )
GB_BIND1ST (GrB, double , FP64 )
GB_BIND1ST (GxB, GxB_FC32_t, FC32 )
GB_BIND1ST (GxB, GxB_FC64_t, FC64 )
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp1st_UDT: apply a binary operator: op(x,A)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_BinaryOp1st_UDT
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const void *x, // first input: scalar x
const GrB_Matrix A, // second input: matrix A
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_BinaryOp1st_UDT "
" (C, M, accum, op, x, A, desc)") ;
GB_SCALAR_WRAP_UDT (scalar, x, (op == NULL) ? NULL : op->xtype) ;
return (GB_1st (C, M, accum, op, scalar, A, desc, Context)) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp2nd_TYPE: apply a binary operator: op(A,y)
//------------------------------------------------------------------------------
#define GB_BIND2ND(prefix,type,T) \
GrB_Info GB_EVAL3 (prefix, _Matrix_apply_BinaryOp2nd_, T) \
( \
GrB_Matrix C, /* input/output matrix for results */ \
const GrB_Matrix M, /* optional mask for C*/ \
const GrB_BinaryOp accum, /* optional accum for Z=accum(C,T) */ \
const GrB_BinaryOp op, /* operator to apply to the entries */ \
const GrB_Matrix A, /* first input: matrix A */ \
const type y, /* second input: scalar y */ \
const GrB_Descriptor desc /* descriptor for C, M, and A */ \
) \
{ \
GB_WHERE (C, GB_STR(prefix) "_Matrix_apply_BinaryOp2nd_" GB_STR(T) \
" (C, M, accum, op, A, y, desc)") ; \
GB_SCALAR_WRAP (scalar, y, GB_EVAL3 (prefix, _, T)) ; \
return (GB_2nd (C, M, accum, (GB_Operator) op, A, scalar, desc, Context)) ;\
}
GB_BIND2ND (GrB, bool , BOOL )
GB_BIND2ND (GrB, int8_t , INT8 )
GB_BIND2ND (GrB, int16_t , INT16 )
GB_BIND2ND (GrB, int32_t , INT32 )
GB_BIND2ND (GrB, int64_t , INT64 )
GB_BIND2ND (GrB, uint8_t , UINT8 )
GB_BIND2ND (GrB, uint16_t , UINT16)
GB_BIND2ND (GrB, uint32_t , UINT32)
GB_BIND2ND (GrB, uint64_t , UINT64)
GB_BIND2ND (GrB, float , FP32 )
GB_BIND2ND (GrB, double , FP64 )
GB_BIND2ND (GxB, GxB_FC32_t, FC32 )
GB_BIND2ND (GxB, GxB_FC64_t, FC64 )
//------------------------------------------------------------------------------
// GrB_Matrix_apply_BinaryOp2nd_UDT: apply a binary operator: op(A,y)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_BinaryOp2nd_UDT
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_BinaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const void *y, // second input: scalar y
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_BinaryOp2nd_UDT"
" (C, M, accum, op, A, y, desc)") ;
GB_SCALAR_WRAP_UDT (scalar, y, (op == NULL) ? NULL : op->ytype) ;
return (GB_2nd (C, M, accum, (GB_Operator) op, A, scalar, desc, Context)) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_IndexOp_TYPE: apply a binary operator: op(A,i,j,thunk)
//------------------------------------------------------------------------------
#define GB_IDXUNOP(prefix,type,T) \
GrB_Info GB_EVAL3 (prefix, _Matrix_apply_IndexOp_, T) \
( \
GrB_Matrix C, /* input/output matrix for results */ \
const GrB_Matrix M, /* optional mask for C*/ \
const GrB_BinaryOp accum, /* optional accum for Z=accum(C,T) */ \
const GrB_IndexUnaryOp op, /* operator to apply to the entries */ \
const GrB_Matrix A, /* first input: matrix A */ \
const type thunk, /* second input: scalar thunk */ \
const GrB_Descriptor desc /* descriptor for C, M, and A */ \
) \
{ \
GB_WHERE (C, GB_STR(prefix) "_Matrix_apply_IndexOp_" GB_STR(T) \
" (C, M, accum, op, A, thunk, desc)") ; \
GB_SCALAR_WRAP (scalar, thunk, GB_EVAL3 (prefix, _, T)) ; \
return (GB_2nd (C, M, accum, (GB_Operator) op, A, scalar, desc, Context)) ;\
}
GB_IDXUNOP (GrB, bool , BOOL )
GB_IDXUNOP (GrB, int8_t , INT8 )
GB_IDXUNOP (GrB, int16_t , INT16 )
GB_IDXUNOP (GrB, int32_t , INT32 )
GB_IDXUNOP (GrB, int64_t , INT64 )
GB_IDXUNOP (GrB, uint8_t , UINT8 )
GB_IDXUNOP (GrB, uint16_t , UINT16)
GB_IDXUNOP (GrB, uint32_t , UINT32)
GB_IDXUNOP (GrB, uint64_t , UINT64)
GB_IDXUNOP (GrB, float , FP32 )
GB_IDXUNOP (GrB, double , FP64 )
GB_IDXUNOP (GxB, GxB_FC32_t, FC32 )
GB_IDXUNOP (GxB, GxB_FC64_t, FC64 )
//------------------------------------------------------------------------------
// GrB_Matrix_apply_IndexOp_UDT: apply a binary operator: op(A,i,j,thunk)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_IndexOp_UDT
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_IndexUnaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const void *thunk, // second input: scalar thunk
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_IndexOp_UDT"
" (C, M, accum, op, A, thunk, desc)") ;
GB_SCALAR_WRAP_UDT (scalar, thunk, (op == NULL) ? NULL : op->ytype) ;
return (GB_2nd (C, M, accum, (GB_Operator) op, A, scalar, desc, Context)) ;
}
//------------------------------------------------------------------------------
// GrB_Matrix_apply_IndexOp_Scalar: apply a binary operator: op(A,i,j,thunk)
//------------------------------------------------------------------------------
GrB_Info GrB_Matrix_apply_IndexOp_Scalar // C<M>=accum(C,op(A,i,j,thunk))
(
GrB_Matrix C, // input/output matrix for results
const GrB_Matrix M, // optional mask for C, unused if NULL
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_IndexUnaryOp op, // operator to apply to the entries
const GrB_Matrix A, // first input: matrix A
const GrB_Scalar thunk, // second input: scalar thunk
const GrB_Descriptor desc // descriptor for C, M, and A
)
{
GB_WHERE (C, "GrB_Matrix_apply_IndexOp_Scalar"
" (C, M, accum, op, A, thunk, desc)") ;
return (GB_2nd (C, M, accum, (GB_Operator) op, A, thunk, desc, Context)) ;
}
|