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
|
//------------------------------------------------------------------------------
// GB_convert_int: convert the integers in a matrix to/from 32/64 bits
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// The integer arrays A->[phi] and A->Y in the matrix A are converted to match
// the requested p_is_32_new, j_is_32_new, and i_is_32_new. If converted,
// A->[phi] are no longer shallow. If A->Y is entirely shallow, it is simply
// removed from A. If A->Y is itself not shallow but contains any shallow
// A->Y->[phi] components, those components are converted and are no longer
// shallow.
// If A has too many entries for p_is_32_new == true, A->p is left unchanged.
// If the dimension of A is too large for j_is_32_new == true, A->h and A->Y
// are left unchanged. If the dimension of A is too large for i_is_32_new ==
// true, A->i. is left unchanged. These are not error conditions.
#include "GB.h"
#define GB_FREE_ALL ;
GrB_Info GB_convert_int // convert the integers of a matrix
(
GrB_Matrix A, // matrix to convert
bool p_is_32_new, // new integer format for A->p
bool j_is_32_new, // new integer format for A->p
bool i_is_32_new, // new integer format for A->h, A->i, and A->Y
bool determine // if true, revise p_is_32_new, j_is_32_new,
// and i_is_32_new based on A->vlen, A->vdim and A->nvals. Otherwise,
// ignore the matrix properties and always convert to the new integer
// sizes.
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
if (A == NULL || A->magic != GB_MAGIC)
{
// nothing to convert
return (GrB_SUCCESS) ;
}
ASSERT (GB_ZOMBIES_OK (A)) ;
ASSERT (GB_JUMBLED_OK (A)) ;
ASSERT (GB_PENDING_OK (A)) ;
//--------------------------------------------------------------------------
// get inputs
//--------------------------------------------------------------------------
if (GB_IS_FULL (A) || GB_IS_BITMAP (A))
{
// quick return: nothing to do
return (GrB_SUCCESS) ;
}
int64_t anz = GB_nnz (A) ;
if (determine)
{
ASSERT_MATRIX_OK (A, "A converting integers", GB0) ;
p_is_32_new = GB_determine_p_is_32 (p_is_32_new, anz) ;
j_is_32_new = GB_determine_j_is_32 (j_is_32_new, A->vdim) ;
i_is_32_new = GB_determine_i_is_32 (i_is_32_new, A->vlen) ;
}
bool p_is_32 = A->p_is_32 ;
bool j_is_32 = A->j_is_32 ;
bool i_is_32 = A->i_is_32 ;
if (p_is_32 == p_is_32_new &&
j_is_32 == j_is_32_new &&
i_is_32 == i_is_32_new)
{
// quick return: nothing to do
return (GrB_SUCCESS) ;
}
//--------------------------------------------------------------------------
// at least some integers must be converted
//--------------------------------------------------------------------------
// simply remove A->Y if it is entirely shallow
if (A->Y_shallow)
{
A->Y = NULL ;
A->Y_shallow = false ;
}
bool A_is_hyper = GB_IS_HYPERSPARSE (A) ; // (A->h != NULL)
int64_t plen = A->plen ;
GrB_Matrix Y = A->Y ;
GB_Pending Pending = A->Pending ;
int64_t ynz = GB_nnz (Y) ;
int64_t yplen = (Y == NULL) ? 0 : Y->plen ;
int64_t npending = (Pending == NULL) ? 0 : Pending->n ;
int64_t nmax_pending = (Pending == NULL) ? 0 : Pending->nmax ;
//--------------------------------------------------------------------------
// allocate new space for A->[phi] and Y->[pix] if present
//--------------------------------------------------------------------------
// Y is not converted via a recurisive call to this method. Instead, it is
// converted directly below. This is because Y->x must also be converted,
// and also so that the conversion will be all-or-nothing, if out of
// memory.
void *Ap_new = NULL ; size_t Ap_new_size = 0 ;
void *Ah_new = NULL ; size_t Ah_new_size = 0 ;
void *Ai_new = NULL ; size_t Ai_new_size = 0 ;
void *Yp_new = NULL ; size_t Yp_new_size = 0 ;
void *Yi_new = NULL ; size_t Yi_new_size = 0 ;
void *Yx_new = NULL ; size_t Yx_new_size = 0 ;
void *Pending_i_new = NULL ; size_t Pending_i_new_size = 0 ;
void *Pending_j_new = NULL ; size_t Pending_j_new_size = 0 ;
bool has_Pending_i = (Pending != NULL) && (Pending->i != NULL) ;
bool has_Pending_j = (Pending != NULL) && (Pending->j != NULL) ;
size_t psize_new = p_is_32_new ? sizeof (uint32_t) : sizeof (uint64_t) ;
size_t jsize_new = j_is_32_new ? sizeof (uint32_t) : sizeof (uint64_t) ;
size_t isize_new = i_is_32_new ? sizeof (uint32_t) : sizeof (uint64_t) ;
bool ok = true ;
if (p_is_32 != p_is_32_new && A->p != NULL)
{
// allocate new space for A->p
Ap_new = GB_MALLOC_MEMORY (plen+1, psize_new, &Ap_new_size) ;
ok = ok && (Ap_new != NULL) ;
}
if (i_is_32 != i_is_32_new)
{
// allocate new space for A->i
if (A->i != NULL)
{
Ai_new = GB_MALLOC_MEMORY (anz, isize_new, &Ai_new_size) ;
ok = ok && (Ai_new != NULL) ;
}
if (has_Pending_i)
{
// allocate new space for Pending->i; matches A->i_is_32
Pending_i_new = GB_MALLOC_MEMORY (nmax_pending, isize_new,
&Pending_i_new_size) ;
ok = ok && (Pending_i_new != NULL) ;
}
}
if (j_is_32 != j_is_32_new)
{
if (A_is_hyper)
{
// allocate new space for A->h
Ah_new = GB_MALLOC_MEMORY (plen, jsize_new, &Ah_new_size) ;
ok = ok && (Ah_new != NULL) ;
}
if (Y != NULL)
{
// allocate new space for Y->[phi]; matches A->j_is_32
Yp_new = GB_MALLOC_MEMORY (yplen+1, jsize_new, &Yp_new_size) ;
Yi_new = GB_MALLOC_MEMORY (ynz, jsize_new, &Yi_new_size) ;
Yx_new = GB_MALLOC_MEMORY (ynz, jsize_new, &Yx_new_size) ;
ok = ok && (Yp_new != NULL && Yi_new != NULL && Yx_new != NULL) ;
}
if (has_Pending_j)
{
// allocate new space for Pending->j; matches A->j_is_32
Pending_j_new = GB_MALLOC_MEMORY (nmax_pending, jsize_new,
&Pending_j_new_size) ;
ok = ok && (Pending_j_new != NULL) ;
}
}
if (!ok)
{
// out of memory: A is unchanged
GB_FREE_MEMORY (&Ap_new, Ap_new_size) ;
GB_FREE_MEMORY (&Ah_new, Ah_new_size) ;
GB_FREE_MEMORY (&Ai_new, Ai_new_size) ;
GB_FREE_MEMORY (&Yp_new, Yp_new_size) ;
GB_FREE_MEMORY (&Yi_new, Yi_new_size) ;
GB_FREE_MEMORY (&Yx_new, Yx_new_size) ;
GB_FREE_MEMORY (&Pending_i_new, Pending_i_new_size) ;
GB_FREE_MEMORY (&Pending_j_new, Pending_j_new_size) ;
return (GrB_OUT_OF_MEMORY) ;
}
// the conversion will now succeed
//--------------------------------------------------------------------------
// convert A->p
//--------------------------------------------------------------------------
int nthreads_max = GB_Context_nthreads_max ( ) ;
if (p_is_32 != p_is_32_new && A->p != NULL)
{
GB_cast_int (Ap_new, p_is_32_new ? GB_UINT32_code : GB_UINT64_code,
A->p , p_is_32 ? GB_UINT32_code : GB_UINT64_code,
plen+1, nthreads_max) ;
if (!A->p_shallow)
{
GB_FREE_MEMORY (&(A->p), A->p_size) ;
}
A->p = Ap_new ;
A->p_size = Ap_new_size ;
A->p_shallow = false ;
}
A->p_is_32 = p_is_32_new ;
//--------------------------------------------------------------------------
// convert A->i and Pending->i
//--------------------------------------------------------------------------
if (i_is_32 != i_is_32_new)
{
bool zombies = (A->nzombies > 0) ;
GB_Type_code zombie32 = zombies ? GB_INT32_code : GB_UINT32_code ;
GB_Type_code zombie64 = zombies ? GB_INT32_code : GB_UINT64_code ;
GB_Type_code icode_new = i_is_32_new ? zombie32 : zombie64 ;
GB_Type_code icode = i_is_32 ? zombie32 : zombie64 ;
GB_Type_code ucode_new = i_is_32_new ? GB_UINT32_code : GB_UINT64_code ;
GB_Type_code ucode = i_is_32 ? GB_UINT32_code : GB_UINT64_code ;
//----------------------------------------------------------------------
// convert A->i
//----------------------------------------------------------------------
if (A->i != NULL)
{
GB_cast_int (Ai_new, icode_new, A->i, icode, anz, nthreads_max) ;
if (!A->i_shallow)
{
GB_FREE_MEMORY (&(A->i), A->i_size) ;
}
A->i = Ai_new ;
A->i_size = Ai_new_size ;
A->i_shallow = false ;
}
//----------------------------------------------------------------------
// convert Pending->i if present
//----------------------------------------------------------------------
if (has_Pending_i)
{
GB_cast_int (Pending_i_new, ucode_new, Pending->i, ucode, npending,
nthreads_max) ;
GB_FREE_MEMORY (&(Pending->i), Pending->i_size) ;
Pending->i = Pending_i_new ;
Pending->i_size = Pending_i_new_size ;
}
}
A->i_is_32 = i_is_32_new ;
//--------------------------------------------------------------------------
// convert A->h, Y->p, Y->i, Y->x, and Pending->j
//--------------------------------------------------------------------------
if (j_is_32 != j_is_32_new)
{
GB_Type_code ucode_new = j_is_32_new ? GB_UINT32_code : GB_UINT64_code ;
GB_Type_code ucode = j_is_32 ? GB_UINT32_code : GB_UINT64_code ;
//----------------------------------------------------------------------
// convert A->h if present
//----------------------------------------------------------------------
if (A_is_hyper)
{
GB_cast_int (Ah_new, ucode_new, A->h, ucode, plen, nthreads_max) ;
if (!A->h_shallow)
{
GB_FREE_MEMORY (&(A->h), A->h_size) ;
}
A->h = Ah_new ;
A->h_size = Ah_new_size ;
A->h_shallow = false ;
}
//----------------------------------------------------------------------
// convert A->Y if present
//----------------------------------------------------------------------
if (Y != NULL)
{
// A is hypersparse, and the integers of Y match A->j_is_32
ASSERT (A_is_hyper) ;
ASSERT (Y->p_is_32 == j_is_32) ;
ASSERT (Y->j_is_32 == j_is_32) ;
ASSERT (Y->i_is_32 == j_is_32) ;
ASSERT_MATRIX_OK (Y, "Y converting integers", GB0) ;
//------------------------------------------------------------------
// convert Y->p
//------------------------------------------------------------------
GB_cast_int (Yp_new, ucode_new, Y->p, ucode, yplen+1, nthreads_max);
if (!Y->p_shallow)
{
GB_FREE_MEMORY (&(Y->p), Y->p_size) ;
}
Y->p = Yp_new ;
Y->p_size = Yp_new_size ;
Y->p_shallow = false ;
Y->p_is_32 = j_is_32_new ;
//------------------------------------------------------------------
// convert Y->i
//------------------------------------------------------------------
GB_cast_int (Yi_new, ucode_new, Y->i, ucode, ynz, nthreads_max) ;
if (!Y->i_shallow)
{
GB_FREE_MEMORY (&(Y->i), Y->i_size) ;
}
Y->i = Yi_new ;
Y->i_size = Yi_new_size ;
Y->i_shallow = false ;
Y->i_is_32 = j_is_32_new ;
//------------------------------------------------------------------
// convert Y->x
//------------------------------------------------------------------
GB_cast_int (Yx_new, ucode_new, Y->x, ucode, ynz, nthreads_max) ;
if (!Y->x_shallow)
{
GB_FREE_MEMORY (&(Y->x), Y->x_size) ;
}
Y->x = Yx_new ;
Y->x_size = Yx_new_size ;
Y->x_shallow = false ;
Y->type = j_is_32_new ? GrB_UINT32 : GrB_UINT64 ;
//------------------------------------------------------------------
// revise Y->j_is_32
//------------------------------------------------------------------
Y->j_is_32 = j_is_32_new ;
ASSERT_MATRIX_OK (Y, "Y converted integers", GB0) ;
}
//----------------------------------------------------------------------
// convert Pending->j if present
//----------------------------------------------------------------------
if (has_Pending_j)
{
GB_cast_int (Pending_j_new, ucode_new, Pending->j, ucode, npending,
nthreads_max) ;
GB_FREE_MEMORY (&(Pending->j), Pending->j_size) ;
Pending->j = Pending_j_new ;
Pending->j_size = Pending_j_new_size ;
}
}
A->j_is_32 = j_is_32_new ;
//--------------------------------------------------------------------------
// return result
//--------------------------------------------------------------------------
ASSERT_MATRIX_OK (A, "A integers converted", GB0) ;
return (GrB_SUCCESS) ;
}
|