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
|
//------------------------------------------------------------------------------
// GB_AxB_saxpy: compute C=A*B, C<M>=A*B, or C<!M>=A*B
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
#include "GB_mxm.h"
#include "GB_bitmap_AxB_saxpy.h"
#include "GB_stringify.h"
// TODO: allow bitmap multiply to work in-place as well
GrB_Info GB_AxB_saxpy // C = A*B using Gustavson/Hash/Bitmap
(
GrB_Matrix C, // output, static header
GrB_Matrix C_in, // original input matrix
const GrB_Matrix M, // optional mask matrix
const bool Mask_comp, // if true, use !M
const bool Mask_struct, // if true, use the only structure of M
const GrB_BinaryOp accum,
const GrB_Matrix A, // input matrix A
const GrB_Matrix B, // input matrix B
const GrB_Semiring semiring, // semiring that defines C=A*B
const bool flipxy, // if true, do z=fmult(b,a) vs fmult(a,b)
bool *mask_applied, // if true, then mask was applied
bool *done_in_place, // if true, C was computed in-place
const GrB_Desc_Value AxB_method,
const int do_sort, // if nonzero, try to sort in saxpy3
GB_Context Context
)
{
// double tt1 = omp_get_wtime ( ) ;
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
GrB_Info info ;
(*mask_applied) = false ;
ASSERT (C != NULL && (C->static_header || GBNSTATIC)) ;
ASSERT_MATRIX_OK_OR_NULL (M, "M for saxpy A*B", GB0) ;
ASSERT (!GB_PENDING (M)) ;
ASSERT (GB_JUMBLED_OK (M)) ;
ASSERT (!GB_ZOMBIES (M)) ;
ASSERT_MATRIX_OK (A, "A for saxpy A*B", GB0) ;
ASSERT (!GB_PENDING (A)) ;
ASSERT (GB_JUMBLED_OK (A)) ;
ASSERT (!GB_ZOMBIES (A)) ;
ASSERT_MATRIX_OK (B, "B for saxpy A*B", GB0) ;
ASSERT (!GB_PENDING (B)) ;
ASSERT (GB_JUMBLED_OK (B)) ;
ASSERT (!GB_ZOMBIES (B)) ;
ASSERT_SEMIRING_OK (semiring, "semiring for saxpy A*B", GB0) ;
ASSERT (A->vdim == B->vlen) ;
//--------------------------------------------------------------------------
// determine the sparsity of C
//--------------------------------------------------------------------------
int C_sparsity, saxpy_method ;
GB_AxB_saxpy_sparsity (&C_sparsity, &saxpy_method,
M, Mask_comp, A, B, Context) ;
//--------------------------------------------------------------------------
// determine if C is iso
//--------------------------------------------------------------------------
GrB_Type ztype = semiring->add->op->ztype ;
size_t zsize = ztype->size ;
GB_void cscalar [GB_VLA(zsize)] ;
bool C_iso = GB_iso_AxB (cscalar, A, B, A->vdim, semiring, flipxy, false) ;
if (C_iso)
{
// revise the method if A and B are both iso and full
if (A->iso && GB_as_if_full (A) && B->iso && GB_as_if_full (B))
{
saxpy_method = GB_SAXPY_METHOD_ISO_FULL ;
C_sparsity = GxB_FULL ;
}
}
//--------------------------------------------------------------------------
// determine if saxpy4 or saxpy5 can be used: C += A*B where C is full
//--------------------------------------------------------------------------
if (!C_iso // C must be non-iso on output
&& C_in != NULL // GB_AxB_meta says it is OK
&& GB_as_if_full (C_in) // C must be "as if" full
&& M == NULL // no mask present
&& (accum != NULL) // accum is present
&& (accum == semiring->add->op) // accum is same as monoid
&& (C_in->type == accum->ztype)) // no typecast from accum output
{
if ((GB_IS_SPARSE (A) || GB_IS_HYPERSPARSE (A))
&& (GB_IS_BITMAP (B) || GB_as_if_full (B)))
{
// GB_AxB_saxpy4 computes C += A*B where C is as-if-full, no mask
// is present, accum is present and matches the monoid, no
// typecasting, A is sparse or hypersparse, and B is bitmap or
// as-if-full. Only built-in semirings are supported, but not all:
// (1) the ANY monoid is not supported since it would be unusual to
// use ANY as the accum, and (2) only monoids that can be done
// atomically without a critical section are supported. The method
// is not used if A*B is iso; C may be iso on input but it is
// non-iso on output.
#ifdef GB_DEBUGIFY_DEFN
GB_debugify_mxm (C_iso, GB_sparsity (C_in), ztype, M,
Mask_struct, Mask_comp, semiring, flipxy, A, B) ;
#endif
info = GB_AxB_saxpy4 (C_in, A, B, semiring, flipxy, done_in_place,
Context) ;
if (info != GrB_NO_VALUE)
{
// return if saxpy4 has handled this case, otherwise fall
// through to saxpy3, dot2, or bitmap_saxpy below.
return (info) ;
}
}
else if ((GB_IS_BITMAP (A) || GB_as_if_full (A))
&& (GB_IS_SPARSE (B) || GB_IS_HYPERSPARSE (B)))
{
// GB_AxB_saxpy5 computes C+=A*B where C is as-if-full, just like
// GB_AxB_saxpy4, except that the sparsity format of A and B are
// reversed. A is bitmap or full, and B is sparse or hypersparse.
// Only built-in semirings are supported, except for the ANY
// monoid. Unlike GB_AxB_saxpy4, built-in monoids without their
// own atomics (TIMES for complex) are supported.
#ifdef GB_DEBUGIFY_DEFN
GB_debugify_mxm (C_iso, GB_sparsity (C_in), ztype, M,
Mask_struct, Mask_comp, semiring, flipxy, A, B) ;
#endif
info = GB_AxB_saxpy5 (C_in, A, B, semiring, flipxy, done_in_place,
Context) ;
if (info != GrB_NO_VALUE)
{
// return if saxpy5 has handled this case, otherwise fall
// through to saxpy3, dot2, or bitmap_saxpy below.
return (info) ;
}
}
}
//--------------------------------------------------------------------------
// burble
//--------------------------------------------------------------------------
if (M == NULL)
{
GBURBLE ("(%s = %s*%s, anz: %g bnz: %g) ",
GB_sparsity_char (C_sparsity),
GB_sparsity_char_matrix (A),
GB_sparsity_char_matrix (B),
(double) GB_nnz (A), (double) GB_nnz (B)) ;
}
else
{
GBURBLE ("(%s%s%s%s%s = %s*%s) ",
GB_sparsity_char (C_sparsity),
Mask_struct ? "{" : "<",
Mask_comp ? "!" : "",
GB_sparsity_char_matrix (M),
Mask_struct ? "}" : ">",
GB_sparsity_char_matrix (A),
GB_sparsity_char_matrix (B)) ;
}
//--------------------------------------------------------------------------
// select the method to use
//--------------------------------------------------------------------------
if (saxpy_method == GB_SAXPY_METHOD_ISO_FULL)
{
//----------------------------------------------------------------------
// C is iso and full; do not apply the mask
//----------------------------------------------------------------------
GBURBLE ("(iso full saxpy) ") ;
ASSERT (C_sparsity == GxB_FULL) ;
// set C->iso = true OK
info = GB_new_bix (&C, // existing header
ztype, A->vlen, B->vdim, GB_Ap_null, true, GxB_FULL, false,
GB_HYPER_SWITCH_DEFAULT, -1, 1, true, true, Context) ;
if (info == GrB_SUCCESS)
{
C->magic = GB_MAGIC ;
memcpy (C->x, cscalar, zsize) ;
}
}
else if (saxpy_method == GB_SAXPY_METHOD_3)
{
//----------------------------------------------------------------------
// saxpy3: general-purpose Gustavson/Hash method, C is sparse/hyper
//----------------------------------------------------------------------
// C is sparse or hypersparse
// This method allocates its own workspace, which is very small if the
// Hash method is used. The workspace for Gustavson's method is
// larger, but saxpy3 selects that method only if the total work is
// high enough so that the time to initialize the space. C is sparse
// or hypersparse.
#ifdef GB_DEBUGIFY_DEFN
GB_debugify_mxm (C_iso, C_sparsity, ztype, M,
Mask_struct, Mask_comp, semiring, flipxy, A, B) ;
#endif
ASSERT (C_sparsity == GxB_HYPERSPARSE || C_sparsity == GxB_SPARSE) ;
info = GB_AxB_saxpy3 (C, C_iso, cscalar, C_sparsity, M, Mask_comp,
Mask_struct, A, B, semiring, flipxy, mask_applied, AxB_method,
do_sort, Context) ;
if (info == GrB_NO_VALUE)
{
// The mask is present but has been discarded since it results in
// too much work. The analysis must be redone, which is done by
// calling this function once again, recursively, without the mask.
// GB_AxB_saxpy_sparsity will be called again, and it might choose
// the bitmap method instead. If saxpy3 is still chosen, this
// results in a different analysis in GB_AxB_saxpy3, with no mask
// present. Otherwise, GB_bitmap_AxB_saxpy, below, is called.
ASSERT (M != NULL) ;
info = GB_AxB_saxpy (C, NULL, NULL, false, false, NULL, A, B,
semiring, flipxy, mask_applied, done_in_place, AxB_method,
do_sort, Context) ;
}
}
else
{
//----------------------------------------------------------------------
// bitmap method: C is bitmap
//----------------------------------------------------------------------
ASSERT (C_sparsity == GxB_BITMAP) ;
if ((GB_IS_BITMAP (A) || GB_IS_FULL (A)) &&
(GB_IS_SPARSE (B) || GB_IS_HYPERSPARSE (B)))
{
// C<#M> = A*B via dot products, where A is bitmap or full and B is
// sparse or hypersparse, using the dot2 method with A not
// explicitly transposed.
info = GB_AxB_dot2 (C, C_iso, cscalar, M, Mask_comp, Mask_struct,
true, A, B, semiring, flipxy, Context) ;
}
else
{
#ifdef GB_DEBUGIFY_DEFN
GB_debugify_mxm (C_iso, GxB_BITMAP, ztype, M,
Mask_struct, Mask_comp, semiring, flipxy, A, B) ;
#endif
// C<#M> = A*B via bitmap saxpy method
info = GB_bitmap_AxB_saxpy (C, C_iso, cscalar, M,
Mask_comp, Mask_struct, A, B, semiring, flipxy, Context) ;
}
// the mask is always applied if present
(*mask_applied) = (M != NULL && info == GrB_SUCCESS) ;
}
// tt1 = omp_get_wtime ( ) - tt1 ; printf ("saxpy time: %g\n", tt1) ;
return (info) ;
}
|