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
|
//------------------------------------------------------------------------------
// GB_select: apply a select operator
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// C<M> = accum (C, select(A,Thunk)) or select(A,Thunk)')
#define GB_FREE_ALL \
{ \
GB_Matrix_free (&T) ; \
}
#include "select/GB_select.h"
#include "mask/GB_accum_mask.h"
#include "transpose/GB_transpose.h"
#include "scalar/GB_Scalar_wrap.h"
GrB_Info GB_select // C<M> = accum (C, select(A,k)) or select(A',k)
(
GrB_Matrix C, // input/output matrix for results
const bool C_replace, // C descriptor
const GrB_Matrix M, // optional mask for C, unused if NULL
const bool Mask_comp, // descriptor for M
const bool Mask_struct, // if true, use the only structure of M
const GrB_BinaryOp accum, // optional accum for Z=accum(C,T)
const GrB_IndexUnaryOp op_in,
const GrB_Matrix A, // input matrix
const GrB_Scalar Thunk, // always present
const bool A_transpose, // A matrix descriptor
GB_Werk Werk
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
// C may be aliased with M and/or A
GrB_Info info ;
GrB_IndexUnaryOp op = op_in ;
GB_RETURN_IF_FAULTY_OR_POSITIONAL (accum) ;
GB_RETURN_IF_NULL_OR_INVALID (Thunk) ;
GB_RETURN_IF_NULL_OR_FAULTY (op) ;
ASSERT_MATRIX_OK (C, "C input for GB_select", GB0) ;
ASSERT_MATRIX_OK_OR_NULL (M, "M for GB_select", GB0) ;
ASSERT_BINARYOP_OK_OR_NULL (accum, "accum for GB_select", GB0) ;
ASSERT_INDEXUNARYOP_OK (op, "indexunaryop for GB_select", GB0) ;
ASSERT_MATRIX_OK (A, "A input for GB_select", GB0) ;
ASSERT_SCALAR_OK (Thunk, "Thunk for GB_select", GB0) ;
struct GB_Matrix_opaque T_header ;
GrB_Matrix T = NULL ;
// check domains and dimensions for C<M> = accum (C,T)
GB_OK (GB_compatible (C->type, C, M, Mask_struct, accum, A->type, Werk));
GB_Type_code xcode = (op->xtype == NULL) ? GB_ignore_code : op->xtype->code;
GB_Opcode opcode = op->opcode ;
ASSERT (GB_IS_INDEXUNARYOP_CODE (opcode)) ;
ASSERT (opcode != GB_FLIPDIAGINDEX_idxunop_code) ;
// A must also be compatible with op->xtype
if (!GB_Type_compatible (A->type, op->xtype))
{
GB_ERROR (GrB_DOMAIN_MISMATCH,
"Incompatible type for C=%s(A,Thunk):\n"
"input A type [%s]\n"
"cannot be typecast to operator input of type [%s]",
op->name, A->type->name, op->xtype->name) ;
}
// check the dimensions
int64_t tnrows = (A_transpose) ? GB_NCOLS (A) : GB_NROWS (A) ;
int64_t tncols = (A_transpose) ? GB_NROWS (A) : GB_NCOLS (A) ;
if (GB_NROWS (C) != tnrows || GB_NCOLS (C) != tncols)
{
GB_ERROR (GrB_DIMENSION_MISMATCH,
"Dimensions not compatible:\n"
"output is " GBd "-by-" GBd "\n"
"input is " GBd "-by-" GBd "%s",
GB_NROWS (C), GB_NCOLS (C),
tnrows, tncols, A_transpose ? " (transposed)" : "") ;
}
// finish any pending work on the Thunk
GrB_Type ttype = Thunk->type ;
GB_MATRIX_WAIT (Thunk) ;
// check the GrB_IndexUnaryOp
if (GB_nnz ((GrB_Matrix) Thunk) == 0)
{
// Thunk cannot be empty for GrB_select
GB_ERROR (GrB_EMPTY_OBJECT, "Thunk for C=%s(A,Thunk)"
" cannot be an empty scalar\n", op->name) ;
}
if (!GB_Type_compatible (GrB_BOOL, op->ztype))
{
// GrB_IndexUnaryOp ztype must be compatible with GrB_BOOL
GB_ERROR (GrB_DOMAIN_MISMATCH,
"Output of user-defined IndexUnaryOp %s is %s\n"
"which cannot be typecasted to bool\n",
op->name, op->ztype->name) ;
}
if (!GB_Type_compatible (ttype, op->ytype))
{
// Thunk must be typecasted to the op->ytype
GB_ERROR (GrB_DOMAIN_MISMATCH,
"Incompatible type for C=%s(A,Thunk):\n"
"input Thunk type [%s] and op thunk type [%s]"
" not compatible",
op->name, ttype->name, op->ytype->name) ;
}
// quick return if an empty mask is complemented
GB_RETURN_IF_QUICK_MASK (C, C_replace, M, Mask_comp, Mask_struct) ;
//--------------------------------------------------------------------------
// delete any lingering zombies and assemble any pending tuples
//--------------------------------------------------------------------------
GB_MATRIX_WAIT (M) ; // TODO: delay until accum/mask phase
GB_MATRIX_WAIT (A) ; // TODO: could tolerate jumbled in some cases
GB_BURBLE_DENSE (C, "(C %s) ") ;
GB_BURBLE_DENSE (M, "(M %s) ") ;
GB_BURBLE_DENSE (A, "(A %s) ") ;
//--------------------------------------------------------------------------
// handle the CSR/CSC format and the transposed case
//--------------------------------------------------------------------------
// A and C can be in CSR or CSC format (in any combination), and A can be
// transposed first via A_transpose. However, A is not explicitly
// transposed first. Instead, the selection operation is modified by
// changing the operator, and the resulting matrix T is transposed, if
// needed.
// Instead of explicitly transposing the input matrix A and output T:
// If A in CSC format and not transposed: treat as if A and T were CSC
// If A in CSC format and transposed: treat as if A and T were CSR
// If A in CSR format and not transposed: treat as if A and T were CSR
// If A in CSR format and transposed: treat as if A and T were CSC
bool A_csc = (A->is_csc == !A_transpose) ;
// The final transpose, if needed, is accomplished in GB_accum_mask, by
// tagging T as the same CSR/CSC format as A_csc. If the format of T and C
// do not match, GB_accum_mask transposes T, computing C<M>=accum(C,T').
//--------------------------------------------------------------------------
// change the op if needed
//--------------------------------------------------------------------------
bool flipij = !A_csc ;
ASSERT_SCALAR_OK (Thunk, "Thunk now GB_select", GB0) ;
bool make_copy = false ;
bool is_empty = false ;
bool negate_thunk = false ;
bool bthunk = false ;
bool op_is_bool_valued = (xcode == GB_BOOL_code &&
(opcode >= GB_VALUENE_idxunop_code && opcode <= GB_VALUELE_idxunop_code)) ;
if (op_is_bool_valued)
{
GB_cast_scalar (&bthunk, GB_BOOL_code, Thunk->x, ttype->code,
sizeof (bool)) ;
}
if (flipij && GB_IS_INDEXUNARYOP_CODE_POSITIONAL (opcode))
{
//----------------------------------------------------------------------
// tril, triu, diag, offdiag, ...: handle the flip
//----------------------------------------------------------------------
// The built-in operators are modified so they can always work as if A
// were in CSC format. If A is not in CSC, then the operation is
// flipped.
switch (opcode)
{
// TRIL becomes TRIU with thunk negated
case GB_TRIL_idxunop_code :
negate_thunk = true ;
op = GrB_TRIU ;
break ;
// TRIU becomes TRIL with thunk negated
case GB_TRIU_idxunop_code :
negate_thunk = true ;
op = GrB_TRIL ;
break ;
// DIAG, OFFDIAG, DIAGINDEX: same op, but negate the thunk
case GB_DIAG_idxunop_code :
case GB_OFFDIAG_idxunop_code :
case GB_DIAGINDEX_idxunop_code :
negate_thunk = true ;
break ;
// ROWINDEX becomes COLINDEX
case GB_ROWINDEX_idxunop_code :
// i+thunk becomes j+thunk: no change to thunk
op = (xcode == GB_INT32_code) ? GrB_COLINDEX_INT32
: GrB_COLINDEX_INT64 ;
break ;
// COLINDEX becomes ROWINDEX
case GB_COLINDEX_idxunop_code :
// j+thunk becomes i+thunk: no change to thunk
op = (xcode == GB_INT32_code) ? GrB_ROWINDEX_INT32
: GrB_ROWINDEX_INT64 ;
break ;
// COLLE becomes ROWLE
case GB_COLLE_idxunop_code :
// j <= thunk becomes i <= thunk: no change to thunk
op = GrB_ROWLE ;
break ;
// COLGT becomes ROWGT
case GB_COLGT_idxunop_code :
// j > thunk becomes i > thunk: no change to thunk
op = GrB_ROWGT ;
break ;
// ROWLE becomes COLLE
case GB_ROWLE_idxunop_code :
// i <= thunk becomes j <= thunk: no change to thunk
op = GrB_COLLE ;
break ;
// ROWGT becomes COLGT
case GB_ROWGT_idxunop_code :
// i > thunk becomes j > thunk: no change to thunk
op = GrB_COLGT ;
break ;
default:;
}
// flipij is now false for any positional operator
flipij = false ;
}
else if (op_is_bool_valued)
{
//----------------------------------------------------------------------
// convert all VALUE* bool cases to VALUEEQ
//----------------------------------------------------------------------
op = GrB_VALUEEQ_BOOL ;
switch (opcode)
{
case GB_VALUENE_idxunop_code : // A(i,j) != thunk
// use A(i,j) == !thunk
bthunk = !bthunk ;
break ;
case GB_VALUEGT_idxunop_code : // A(i,j) > thunk
if (bthunk)
{
// if thunk is true, return an empty matrix
is_empty = true ;
}
else
{
// otherwise, use A(i,j) == true
bthunk = true ;
}
break ;
case GB_VALUEGE_idxunop_code : // A(i,j) >= thunk
if (!bthunk)
{
// if thunk is false, make a copy
make_copy = true ;
}
else
{
// otherwise, use A(i,j) == true
bthunk = true ;
}
break ;
case GB_VALUELT_idxunop_code : // A(i,j) < thunk
// if thunk is false, return an empty matrix
if (!bthunk)
{
is_empty = true ;
}
else
{
// otherwise, use A(i,j) == false
bthunk = false ;
}
break ;
case GB_VALUELE_idxunop_code : // A(i,j) <= thunk
// if thunk is true, make a copy
if (bthunk)
{
make_copy = true ;
}
else
{
// otherwise, use A(i,j) == false
bthunk = false ;
}
break ;
default : ;
}
}
if (opcode != GB_USER_idxunop_code)
{
// flipij can still be true but is only needed for if the
// GrB_IndexUnaryOp is user-defined. So set here it to false for all
// but user-defined ops.
flipij = false ;
}
//--------------------------------------------------------------------------
// negate the Thunk if needed
//--------------------------------------------------------------------------
GrB_Scalar Thunk2 ;
struct GB_Scalar_opaque Thunk2_header ;
int64_t ithunk = 0 ;
if (negate_thunk)
{
// Thunk = -(int64_t) Thunk
GB_cast_scalar (&ithunk, GB_INT64_code, Thunk->x, ttype->code,
sizeof (int64_t)) ;
ithunk = -ithunk ;
Thunk2 = GB_Scalar_wrap (&Thunk2_header, GrB_INT64, &ithunk) ;
}
else if (op_is_bool_valued)
{
// Thunk = bthunk
Thunk2 = GB_Scalar_wrap (&Thunk2_header, GrB_BOOL, &bthunk) ;
}
else
{
// use Thunk as-is
Thunk2 = Thunk ;
}
//--------------------------------------------------------------------------
// create T
//--------------------------------------------------------------------------
GB_CLEAR_MATRIX_HEADER (T, &T_header) ;
if (make_copy)
{
// T = A
GB_OK (GB_shallow_copy (T, A_csc, A, Werk)) ;
}
else if (is_empty)
{
// get the integer sizes for the new empty matrix T
bool Cp_is_32, Cj_is_32, Ci_is_32 ;
GB_determine_pji_is_32 (&Cp_is_32, &Cj_is_32, &Ci_is_32,
GxB_SPARSE, 0, A->vlen, A->vdim, Werk) ;
// T is an empty non-iso matrix
GB_OK (GB_new (&T, // auto (sparse or hyper), existing header
A->type, A->vlen, A->vdim, GB_ph_calloc, A_csc,
GxB_SPARSE + GxB_HYPERSPARSE, GB_Global_hyper_switch_get ( ), 1,
Cp_is_32, Cj_is_32, Ci_is_32)) ;
}
else
{
// T = select (A, Thunk)
GB_OK (GB_selector (T, op, flipij, A, Thunk2, Werk)) ;
}
T->is_csc = A_csc ;
ASSERT_MATRIX_OK (T, "T=select(A,Thunk) output", GB0) ;
//--------------------------------------------------------------------------
// C<M> = accum (C,T): accumulate the results into C via the mask
//--------------------------------------------------------------------------
return (GB_accum_mask (C, M, NULL, accum, &T, C_replace, Mask_comp,
Mask_struct, Werk)) ;
}
|