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
|
//------------------------------------------------------------------------------
// GB_convert_b2s: construct triplets or CSC/CSR from bitmap
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Constructs a triplet or CSC/CSR form (in Cp, Ci, Cj, and Cx_new) from the
// bitmap input matrix A. If A is iso or Cx_new is NULL then no values are
// extracted. The iso case is handled by the caller.
// Ci, Cj, and Cx_new may be NULL.
// FUTURE: make a separate function for constructing triplets
#include "GB.h"
#include "jitifyer/GB_stringify.h"
#include "unaryop/GB_unop.h"
#define GB_FREE_ALL GB_FREE_MEMORY (&W, W_size) ;
GrB_Info GB_convert_b2s // extract CSC/CSR or triplets from bitmap
(
// outputs:
void *Cp, // vector pointers for CSC/CSR form
void *Ci, // indices for CSC/CSR or triplet form
void *Cj, // vector indices for triplet form
void *Cx_new, // values for CSC/CSR or triplet form
int64_t *cnvec_nonempty, // # of non-empty vectors
// inputs: not modified
const bool Cp_is_32, // if true, Cp is uint32_t; otherwise uint64_t
const bool Ci_is_32, // if true, Ci is uint32_t; otherwise uint64_t
const bool Cj_is_32, // if true, Cj is uint32_t; otherwise uint64_t
const GrB_Type ctype, // type of Cx
const GrB_Matrix A, // matrix to extract; not modified
GB_Werk Werk
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
ASSERT (GB_IS_BITMAP (A)) ;
ASSERT (Cp != NULL) ; // must be provided on input, size avdim+1
ASSERT_MATRIX_OK (A, "A for b2s", GB0) ;
ASSERT_TYPE_OK (ctype, "ctype for b2s", GB0) ;
ASSERT ((Cp_is_32 && A->nvals < UINT32_MAX) || !Cp_is_32) ;
//--------------------------------------------------------------------------
// get inputs and determine tasks
//--------------------------------------------------------------------------
void *W = NULL ; size_t W_size = 0 ;
GB_IDECL (W , , u) ;
GB_IDECL (Cp, , u) ; GB_IPTR (Cp, Cp_is_32) ;
GB_IDECL (Ci, , u) ; GB_IPTR (Ci, Ci_is_32) ;
GB_IDECL (Cj, , u) ; GB_IPTR (Cj, Cj_is_32) ;
size_t psize = Cp_is_32 ? sizeof (uint32_t) : sizeof (uint64_t) ;
const int64_t avdim = A->vdim ;
const int64_t avlen = A->vlen ;
const size_t asize = A->type->size ;
const int8_t *restrict Ab = A->b ;
int nthreads_max = GB_Context_nthreads_max ( ) ;
double chunk = GB_Context_chunk ( ) ;
int nthreads = GB_nthreads (avlen*avdim, chunk, nthreads_max) ;
bool by_vector = (nthreads <= avdim) ;
//--------------------------------------------------------------------------
// count the entries in each vector
//--------------------------------------------------------------------------
if (by_vector)
{
//----------------------------------------------------------------------
// compute all vectors in parallel (no workspace)
//----------------------------------------------------------------------
int64_t j ;
#pragma omp parallel for num_threads(nthreads) schedule(static)
for (j = 0 ; j < avdim ; j++)
{
// ajnz = nnz (A (:,j))
uint64_t ajnz = 0 ;
int64_t pA_start = j * avlen ;
for (int64_t i = 0 ; i < avlen ; i++)
{
// see if A(i,j) is present in the bitmap
int64_t p = i + pA_start ;
ajnz += Ab [p] ;
ASSERT (Ab [p] == 0 || Ab [p] == 1) ;
}
// Cp [j] = ajnz ;
GB_ISET (Cp, j, ajnz) ;
}
}
else
{
//----------------------------------------------------------------------
// compute blocks of rows in parallel
//----------------------------------------------------------------------
// allocate one row of W per thread, each row of length avdim
W = GB_MALLOC_MEMORY (nthreads * avdim, psize, &W_size) ;
if (W == NULL)
{
// out of memory
return (GrB_OUT_OF_MEMORY) ;
}
GB_IPTR (W, Cp_is_32) ;
//----------------------------------------------------------------------
// count each block
//----------------------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) schedule(static)
for (tid = 0 ; tid < nthreads ; tid++)
{
uint32_t *restrict Wtask32 = Cp_is_32 ? (W32 + tid * avdim) : NULL ;
uint64_t *restrict Wtask64 = Cp_is_32 ? NULL : (W64 + tid * avdim) ;
int64_t istart, iend ;
GB_PARTITION (istart, iend, avlen, tid, nthreads) ;
for (int64_t j = 0 ; j < avdim ; j++)
{
// ajnz = nnz (A (istart:iend-1,j))
uint64_t ajnz = 0 ;
int64_t pA_start = j * avlen ;
for (int64_t i = istart ; i < iend ; i++)
{
// see if A(i,j) is present in the bitmap
int64_t p = i + pA_start ;
ajnz += Ab [p] ;
ASSERT (Ab [p] == 0 || Ab [p] == 1) ;
}
// Wtask [j] = ajnz ;
GB_ISET (Wtask, j, ajnz) ;
}
}
//----------------------------------------------------------------------
// cumulative sum to compute nnz(A(:,j)) for each vector j
//----------------------------------------------------------------------
int64_t j ;
if (Cp_is_32)
{
#pragma omp parallel for num_threads(nthreads) schedule(static)
for (j = 0 ; j < avdim ; j++)
{
uint32_t ajnz = 0 ;
for (int tid = 0 ; tid < nthreads ; tid++)
{
uint32_t *restrict Wtask32 = W32 + tid * avdim ;
uint32_t c = Wtask32 [j] ;
Wtask32 [j] = ajnz ;
ajnz += c ;
}
Cp32 [j] = ajnz ;
}
}
else
{
#pragma omp parallel for num_threads(nthreads) schedule(static)
for (j = 0 ; j < avdim ; j++)
{
uint64_t ajnz = 0 ;
for (int tid = 0 ; tid < nthreads ; tid++)
{
uint64_t *restrict Wtask64 = W64 + tid * avdim ;
uint64_t c = Wtask64 [j] ;
Wtask64 [j] = ajnz ;
ajnz += c ;
}
Cp64 [j] = ajnz ;
}
}
}
//--------------------------------------------------------------------------
// cumulative sum of Cp
//--------------------------------------------------------------------------
// This cannot overflow if Cp is uint32_t, because in that case A->nvals
// is < UINT32_MAX (see assertion above).
int nth = GB_nthreads (avdim, chunk, nthreads_max) ;
GB_cumsum (Cp, Cp_is_32, avdim, cnvec_nonempty, nth, Werk) ;
ASSERT (GB_IGET (Cp, avdim) == A->nvals) ;
//--------------------------------------------------------------------------
// gather the pattern and values from the bitmap
//--------------------------------------------------------------------------
GrB_Info info = GrB_NO_VALUE ;
if (Cx_new == NULL || A->x == NULL || A->iso)
{
//----------------------------------------------------------------------
// via the symbolic kernel
//----------------------------------------------------------------------
#undef GB_COPY
#define GB_COPY(Cx,pC,Ax,pA)
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
}
else
{
//----------------------------------------------------------------------
// via an inline kernel for types of size 1, 2, 4, 8, or 16
//----------------------------------------------------------------------
if (ctype == A->type)
{
#undef GB_COPY
#define GB_COPY(Cx,pC,Ax,pA) Cx [pC] = Ax [pA] ;
#ifndef GBCOMPACT
GB_IF_FACTORY_KERNELS_ENABLED
{
switch (asize)
{
case GB_1BYTE : // uint8, int8, bool, or 1-byte user
#define GB_C_TYPE uint8_t
#define GB_A_TYPE uint8_t
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
break ;
case GB_2BYTE : // uint16, int16, or 2-byte user-defined
#define GB_C_TYPE uint16_t
#define GB_A_TYPE uint16_t
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
break ;
case GB_4BYTE : // uint32, int32, float, or 4-byte user
#define GB_C_TYPE uint32_t
#define GB_A_TYPE uint32_t
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
break ;
case GB_8BYTE : // uint64, int64, double, float complex,
// or 8-byte user defined
#define GB_C_TYPE uint64_t
#define GB_A_TYPE uint64_t
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
break ;
case GB_16BYTE : // double complex or 16-byte user-defined
#define GB_C_TYPE GB_blob16
#define GB_A_TYPE GB_blob16
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
break ;
default:;
}
}
#endif
}
//----------------------------------------------------------------------
// via the JIT kernel
//----------------------------------------------------------------------
if (info == GrB_NO_VALUE)
{
struct GB_UnaryOp_opaque op_header ;
GB_Operator op = GB_unop_identity (ctype, &op_header) ;
info = GB_convert_b2s_jit (Cp, Ci, Cj, Cx_new,
Cp_is_32, Ci_is_32, Cj_is_32, ctype, op, A, W, nthreads) ;
}
//----------------------------------------------------------------------
// via the generic kernel
//----------------------------------------------------------------------
if (info == GrB_NO_VALUE)
{
GB_Type_code ccode = ctype->code ;
GB_Type_code acode = A->type->code ;
const size_t csize = ctype->size ;
GB_cast_function cast_A_to_C = GB_cast_factory (ccode, acode) ;
#define GB_C_TYPE GB_void
#define GB_A_TYPE GB_void
#undef GB_COPY
#define GB_COPY(Cx,pC,Ax,pA) \
cast_A_to_C (Cx +(pC)*csize, Ax +(pA)*asize, asize)
#include "convert/template/GB_convert_b2s_template.c"
info = GrB_SUCCESS ;
}
}
//--------------------------------------------------------------------------
// free workspace return result
//--------------------------------------------------------------------------
GB_FREE_ALL ;
return (info) ;
}
|