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
|
//------------------------------------------------------------------------------
// GB_AxB_dot3: compute C<M> = A'*B in parallel
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// This function only computes C<M>=A'*B. The mask must be present, and not
// complemented, and can be either valued or structural. The mask is always
// applied. C and M are both sparse or hypersparse, and have the same sparsity
// structure.
#include "GB_mxm.h"
#include "GB_binop.h"
#include "GB_AxB__include1.h"
#ifndef GBCUDA_DEV
#include "GB_AxB__include2.h"
#endif
#include "GB_unused.h"
#define GB_FREE_WORKSPACE \
{ \
GB_FREE_WORK (&TaskList, TaskList_size) ; \
}
#define GB_FREE_ALL \
{ \
GB_FREE_WORKSPACE ; \
GB_phybix_free (C) ; \
}
GB_PUBLIC
GrB_Info GB_AxB_dot3 // C<M> = A'*B using dot product method
(
GrB_Matrix C, // output matrix, static header
const bool C_iso, // true if C is iso
const GB_void *cscalar, // iso value of C
const GrB_Matrix M, // mask matrix
const bool Mask_struct, // if true, use the only structure of M
const GrB_Matrix A, // input matrix
const GrB_Matrix B, // input matrix
const GrB_Semiring semiring, // semiring that defines C=A*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 (M, "M for dot3 A'*B", GB0) ;
ASSERT_MATRIX_OK (A, "A for dot3 A'*B", GB0) ;
ASSERT_MATRIX_OK (B, "B for dot3 A'*B", GB0) ;
ASSERT (!GB_ZOMBIES (M)) ;
ASSERT (GB_JUMBLED_OK (M)) ; // C is jumbled if M is jumbled
ASSERT (!GB_PENDING (M)) ;
ASSERT (!GB_ZOMBIES (A)) ;
ASSERT (!GB_JUMBLED (A)) ;
ASSERT (!GB_PENDING (A)) ;
ASSERT (!GB_ZOMBIES (B)) ;
ASSERT (!GB_JUMBLED (B)) ;
ASSERT (!GB_PENDING (B)) ;
ASSERT (!GB_IS_BITMAP (M)) ;
ASSERT (!GB_IS_FULL (M)) ;
ASSERT_SEMIRING_OK (semiring, "semiring for numeric A'*B", GB0) ;
int ntasks, nthreads ;
GB_task_struct *TaskList = NULL ; size_t TaskList_size = 0 ;
//--------------------------------------------------------------------------
// get the semiring operators
//--------------------------------------------------------------------------
GrB_BinaryOp mult = semiring->multiply ;
GrB_Monoid add = semiring->add ;
ASSERT (mult->ztype == add->op->ztype) ;
bool op_is_first = mult->opcode == GB_FIRST_binop_code ;
bool op_is_second = mult->opcode == GB_SECOND_binop_code ;
bool op_is_pair = mult->opcode == GB_PAIR_binop_code ;
bool A_is_pattern = false ;
bool B_is_pattern = false ;
if (flipxy)
{
// z = fmult (b,a) will be computed
A_is_pattern = op_is_first || op_is_pair ;
B_is_pattern = op_is_second || op_is_pair ;
ASSERT (GB_IMPLIES (!A_is_pattern,
GB_Type_compatible (A->type, mult->ytype))) ;
ASSERT (GB_IMPLIES (!B_is_pattern,
GB_Type_compatible (B->type, mult->xtype))) ;
}
else
{
// z = fmult (a,b) will be computed
A_is_pattern = op_is_second || op_is_pair ;
B_is_pattern = op_is_first || op_is_pair ;
ASSERT (GB_IMPLIES (!A_is_pattern,
GB_Type_compatible (A->type, mult->xtype))) ;
ASSERT (GB_IMPLIES (!B_is_pattern,
GB_Type_compatible (B->type, mult->ytype))) ;
}
//--------------------------------------------------------------------------
// get M, A, and B
//--------------------------------------------------------------------------
const int64_t *restrict Mp = M->p ;
const int64_t *restrict Mh = M->h ;
const int64_t *restrict Mi = M->i ;
const GB_void *restrict Mx = (GB_void *) (Mask_struct ? NULL : (M->x)) ;
const size_t msize = M->type->size ;
const int64_t mvlen = M->vlen ;
const int64_t mvdim = M->vdim ;
const int64_t mnz = GB_nnz (M) ;
const int64_t mnvec = M->nvec ;
const bool M_is_hyper = GB_IS_HYPERSPARSE (M) ;
const bool M_is_sparse = GB_IS_SPARSE (M) ;
const int64_t *restrict Ap = A->p ;
const int64_t *restrict Ah = A->h ;
const int64_t vlen = A->vlen ;
const int64_t anvec = A->nvec ;
const bool A_is_hyper = GB_IS_HYPERSPARSE (A) ;
const bool A_is_sparse = GB_IS_SPARSE (A) ;
const bool A_is_bitmap = GB_IS_BITMAP (A) ;
const int64_t *restrict Bp = B->p ;
const int64_t *restrict Bh = B->h ;
const int64_t bnvec = B->nvec ;
const bool B_is_hyper = GB_IS_HYPERSPARSE (B) ;
const bool B_is_sparse = GB_IS_SPARSE (B) ;
const bool B_is_bitmap = GB_IS_BITMAP (B) ;
ASSERT (A->vlen == B->vlen) ;
ASSERT (vlen > 0) ;
const GrB_Matrix A_Y = A->Y ;
const int64_t *restrict A_Yp = (A_is_hyper) ? A_Y->p : NULL ;
const int64_t *restrict A_Yi = (A_is_hyper) ? A_Y->i : NULL ;
const int64_t *restrict A_Yx = (A_is_hyper) ? A_Y->x : NULL ;
const int64_t A_hash_bits = (A_is_hyper) ? (A_Y->vdim - 1) : 0 ;
const GrB_Matrix B_Y = B->Y ;
const int64_t *restrict B_Yp = (B_is_hyper) ? B_Y->p : NULL ;
const int64_t *restrict B_Yi = (B_is_hyper) ? B_Y->i : NULL ;
const int64_t *restrict B_Yx = (B_is_hyper) ? B_Y->x : NULL ;
const int64_t B_hash_bits = (B_is_hyper) ? (B_Y->vdim - 1) : 0 ;
//--------------------------------------------------------------------------
// allocate C, the same size and # of entries as M
//--------------------------------------------------------------------------
GrB_Type ctype = add->op->ztype ;
int64_t cvlen = mvlen ;
int64_t cvdim = mvdim ;
int64_t cnz = mnz ;
int64_t cnvec = mnvec ;
int C_sparsity = (M_is_hyper) ? GxB_HYPERSPARSE : GxB_SPARSE ;
// C is sparse or hypersparse, not full or bitmap
// set C->iso = C_iso OK
GB_OK (GB_new_bix (&C, // sparse or hyper (from M), existing header
ctype, cvlen, cvdim, GB_Ap_malloc, true,
C_sparsity, true, M->hyper_switch, cnvec,
cnz+1, // add one to cnz for GB_cumsum of Cwork in GB_AxB_dot3_slice
true, C_iso, Context)) ;
int64_t *restrict Cp = C->p ;
int64_t *restrict Ch = C->h ;
int64_t *restrict Cwork = C->i ; // use C->i as workspace
//--------------------------------------------------------------------------
// determine the # of threads to use
//--------------------------------------------------------------------------
GB_GET_NTHREADS_MAX (nthreads_max, chunk, Context) ;
//--------------------------------------------------------------------------
// copy Mp and Mh into C
//--------------------------------------------------------------------------
// M is sparse or hypersparse; C is the same as M
nthreads = GB_nthreads (cnvec, chunk, nthreads_max) ;
// TODO: try this with Cp and Ch shallow
GB_memcpy (Cp, Mp, (cnvec+1) * sizeof (int64_t), nthreads) ;
if (M_is_hyper)
{
GB_memcpy (Ch, Mh, cnvec * sizeof (int64_t), nthreads) ;
}
C->nvec_nonempty = M->nvec_nonempty ;
C->nvec = M->nvec ;
C->nvals = M->nvals ;
C->magic = GB_MAGIC ;
//--------------------------------------------------------------------------
// construct the tasks for the first phase
//--------------------------------------------------------------------------
nthreads = GB_nthreads (cnz, chunk, nthreads_max) ;
GB_OK (GB_AxB_dot3_one_slice (&TaskList, &TaskList_size, &ntasks, &nthreads,
M, Context)) ;
//--------------------------------------------------------------------------
// phase1: estimate the work to compute each entry in C
//--------------------------------------------------------------------------
// The work to compute C(i,j) is held in Cwork [p], if C(i,j) appears in
// as the pth entry in C.
#define GB_DOT3
#define GB_DOT3_PHASE1
if (M_is_sparse && Mask_struct)
{
// special case: M is sparse and structural
#define GB_MASK_SPARSE_AND_STRUCTURAL
#include "GB_meta16_factory.c"
#undef GB_MASK_SPARSE_AND_STRUCTURAL
// TODO: skip phase1 if A and B are both bitmap/full.
}
else
{
// general case: M sparse/hyper, structural/valued
#include "GB_meta16_factory.c"
}
#undef GB_DOT3
#undef GB_DOT3_PHASE1
//--------------------------------------------------------------------------
// free the current tasks and construct the tasks for the second phase
//--------------------------------------------------------------------------
GB_FREE_WORK (&TaskList, TaskList_size) ;
GB_OK (GB_AxB_dot3_slice (&TaskList, &TaskList_size, &ntasks, &nthreads,
C, Context)) ;
GBURBLE ("nthreads %d ntasks %d ", nthreads, ntasks) ;
//--------------------------------------------------------------------------
// C<M> = A'*B, via masked dot product method and built-in semiring
//--------------------------------------------------------------------------
if (C_iso)
{
//----------------------------------------------------------------------
// C is iso; compute the pattern of C<M>=A'*B with the any_pair semiring
//----------------------------------------------------------------------
memcpy (C->x, cscalar, ctype->size) ;
GB_OK (GB (_Adot3B__any_pair_iso) (C, M, Mask_struct, A, B,
TaskList, ntasks, nthreads)) ;
}
else
{
//----------------------------------------------------------------------
// C is non-iso
//----------------------------------------------------------------------
bool done = false ;
#ifndef GBCUDA_DEV
//------------------------------------------------------------------
// define the worker for the switch factory
//------------------------------------------------------------------
#define GB_Adot3B(add,mult,xname) \
GB (_Adot3B_ ## add ## mult ## xname)
#define GB_AxB_WORKER(add,mult,xname) \
{ \
info = GB_Adot3B (add,mult,xname) (C, M, Mask_struct, A, B, \
TaskList, ntasks, nthreads) ; \
done = (info != GrB_NO_VALUE) ; \
} \
break ;
//------------------------------------------------------------------
// launch the switch factory
//------------------------------------------------------------------
GB_Opcode mult_binop_code, add_binop_code ;
GB_Type_code xcode, ycode, zcode ;
if (GB_AxB_semiring_builtin (A, A_is_pattern, B, B_is_pattern,
semiring, flipxy, &mult_binop_code, &add_binop_code, &xcode,
&ycode, &zcode))
{
#include "GB_AxB_factory.c"
}
#endif
//----------------------------------------------------------------------
// C<M> = A'*B, via masked dot product method and typecasting
//----------------------------------------------------------------------
if (!done)
{
#define GB_DOT3_GENERIC
GB_BURBLE_MATRIX (C, "(generic C<M>=A'*B) ") ;
#include "GB_AxB_dot_generic.c"
}
}
//--------------------------------------------------------------------------
// free workspace and return result
//--------------------------------------------------------------------------
GB_FREE_WORKSPACE ;
C->jumbled = GB_JUMBLED (M) ; // C is jumbled if M is jumbled
ASSERT_MATRIX_OK (C, "dot3: C<M> = A'*B output", GB0) ;
ASSERT (GB_ZOMBIES_OK (C)) ;
ASSERT (GB_JUMBLED_OK (C)) ;
ASSERT (!GB_PENDING (C)) ;
return (GrB_SUCCESS) ;
}
|