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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
// SuiteSparse/MATLAB_Tools/SSMULT/ssmult_template.c
// SSMULT, Copyright (c) 2007-2011, Timothy A Davis. All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0+
/* ========================================================================== */
/* === ssmult_template.c ==================================================== */
/* ========================================================================== */
/* C = A*B, where A and B are sparse. The column pointers for C (the Cp array)
* have already been computed. entries are dropped. This code fragment is
* #include'd into ssmult.c four times, with all four combinations of ACOMPLEX
* (defined or not) and BCOMPLEX (defined or not).
*
* By default, C is returned with sorted column indices, and with explicit
* zero entries dropped. If C is complex with an all-zero imaginary part, then
* the imaginary part is freed and C becomes real. Thus, C is a pure MATLAB
* sparse matrix.
*
* If UNSORTED is defined (-DUNSORTED), then the nonzero pattern of C is
* returned with unsorted column indices. This is much faster than returning a
* pure MATLAB sparse matrix, but the result must eventually be sorted prior to
* returning to MATLAB.
*
* If the compiler bug discussed below does not affect you, then uncomment the
* following line, or compile with code with -DNO_GCC_BUG.
#define NO_GCC_BUG
* The gcc bug occurs when cij underflows to zero:
*
* cij = aik * bkj ;
* if (cij == 0)
* {
* drop this entry
* }
*
* If cij underflows, cij is zero but the above test is incorrectly FALSE with
* gcc -O, using gcc version 4.1.0 on an Intel Pentium. The bug does not appear
* on an AMD Opteron with the same compiler. The solution is to store cij to
* memory first, and then to read it back in and test it, which is slower.
*/
/* -------------------------------------------------------------------------- */
/* MULT: multiply (or multiply and accumulate, depending on op) */
/* -------------------------------------------------------------------------- */
/* op can be "=" or "+=" */
#ifdef ACOMPLEX
#ifdef BCOMPLEX
#define MULT(x,z,op) \
azik = (ac ? (-Az [pa]) : (Az [pa])) ; \
x op Ax [pa] * bkj - azik * bzkj ; \
z op azik * bkj + Ax [pa] * bzkj ;
#else
#define MULT(x,z,op) \
azik = (ac ? (-Az [pa]) : (Az [pa])) ; \
x op Ax [pa] * bkj ; \
z op azik * bkj ;
#endif
#else
#ifdef BCOMPLEX
#define MULT(x,z,op) \
x op Ax [pa] * bkj ; \
z op Ax [pa] * bzkj ;
#else
#define MULT(x,z,op) \
x op Ax [pa] * bkj ;
#endif
#endif
/* -------------------------------------------------------------------------- */
/* ASSIGN_BKJ: copy B(k,j) into a local scalar */
/* -------------------------------------------------------------------------- */
#ifdef BCOMPLEX
#define ASSIGN_BKJ \
bkj = Bx [pb] ; \
bzkj = (bc ? (-Bz [pb]) : (Bz [pb])) ;
#else
#define ASSIGN_BKJ \
bkj = Bx [pb] ;
#endif
/* -------------------------------------------------------------------------- */
/* DROP_CHECK: check if an entry must be dropped */
/* -------------------------------------------------------------------------- */
#if defined (ACOMPLEX) || defined (BCOMPLEX)
#define DROP_CHECK(x,z) \
if (x == 0 && z == 0) drop = 1 ; \
if (z != 0) zallzero = 0 ;
#else
#define DROP_CHECK(x,z) if (x == 0) drop = 1 ;
#endif
/* -------------------------------------------------------------------------- */
/* sparse matrix multiply template */
/* -------------------------------------------------------------------------- */
{
#ifdef ACOMPLEX
double azik ;
#endif
/* ---------------------------------------------------------------------- */
/* initialize drop tests */
/* ---------------------------------------------------------------------- */
drop = 0 ; /* true if any entry in C is zero */
zallzero = 1 ; /* true if Cz is all zero */
/* ---------------------------------------------------------------------- */
/* quick check if A is diagonal, or a permutation matrix */
/* ---------------------------------------------------------------------- */
if (Anrow == Ancol && Ap [Ancol] == Ancol)
{
/* A is square, with n == nnz (A); check the pattern */
A_is_permutation = 1 ;
A_is_diagonal = 1 ;
for (j = 0 ; j < Ancol ; j++)
{
if (Ap [j] != j)
{
/* A has a column with no entries, or more than 1 entry */
A_is_permutation = 0 ;
A_is_diagonal = 0 ;
break ;
}
}
mark-- ; /* Flag [0..n-1] != mark is now true */
for (j = 0 ; j < Ancol && (A_is_permutation || A_is_diagonal) ; j++)
{
/* A has one entry in each column, so j == Ap [j] */
i = Ai [j] ;
if (i != j)
{
/* A is not diagonal, but might still be a permutation */
A_is_diagonal = 0 ;
}
if (Flag [i] == mark)
{
/* row i appears twice; A is neither permutation nor diagonal */
A_is_permutation = 0 ;
A_is_diagonal = 0 ;
}
/* mark row i, so we know if we see it again */
Flag [i] = mark ;
}
}
else
{
/* A is not square, or nnz (A) is not equal to n */
A_is_permutation = 0 ;
A_is_diagonal = 0 ;
}
/* ---------------------------------------------------------------------- */
/* allocate workspace */
/* ---------------------------------------------------------------------- */
#ifndef UNSORTED
W = NULL ;
if (!A_is_diagonal)
{
#if defined (ACOMPLEX) || defined (BCOMPLEX)
W = mxMalloc (Anrow * 2 * sizeof (double)) ;
Wz = W + Anrow ;
#else
W = mxMalloc (Anrow * sizeof (double)) ;
#endif
}
#endif
/* ---------------------------------------------------------------------- */
/* compute C one column at a time */
/* ---------------------------------------------------------------------- */
if (A_is_diagonal)
{
/* ------------------------------------------------------------------ */
/* C = A*B where A is diagonal */
/* ------------------------------------------------------------------ */
pb = 0 ;
for (j = 0 ; j < Bncol ; j++)
{
pcstart = pb ;
pbend = Bp [j+1] ; /* column B is in Bi,Bx,Bz [pb ... pbend+1] */
for ( ; pb < pbend ; pb++)
{
k = Bi [pb] ; /* nonzero entry B(k,j) */
ASSIGN_BKJ ;
Ci [pb] = k ;
pa = k ;
MULT (Cx [pb], Cz [pb], =) ; /* C(k,j) = A(k,k)*B(k,j) */
#ifdef NO_GCC_BUG
DROP_CHECK (Cx [pb], Cz [pb]) ; /* check if C(k,j) == 0 */
#endif
}
#ifndef NO_GCC_BUG
for (pc = pcstart ; pc < pbend ; pc++)
{
DROP_CHECK (Cx [pc], Cz [pc]) ; /* check if C(k,j) == 0 */
}
#endif
}
}
else
{
/* ------------------------------------------------------------------ */
/* C = A*B, general case, or A permutation */
/* ------------------------------------------------------------------ */
pb = 0 ;
cnz = 0 ;
for (j = 0 ; j < Bncol ; j++)
{
/* -------------------------------------------------------------- */
/* compute jth column of C: C(:,j) = A * B(:,j) */
/* -------------------------------------------------------------- */
pbend = Bp [j+1] ; /* column B is in Bi,Bx,Bz [pb ... pbend+1] */
pcstart = cnz ; /* start of column j in C */
blen = pbend - pb ; /* number of entries in B */
needs_sorting = 0 ; /* true if column j needs sorting */
if (blen == 0)
{
/* ---------------------------------------------------------- */
/* nothing to do, B(:,j) and C(:,j) are empty */
/* ---------------------------------------------------------- */
continue ;
}
else if (blen == 1)
{
/* ---------------------------------------------------------- */
/* B(:,j) contains only one nonzero */
/* ---------------------------------------------------------- */
/* since there is only one entry in B, just scale column A(:,k):
* C(:,j) = A(:,k) * B(k,j)
* C is sorted only if A is sorted on input */
k = Bi [pb] ; /* nonzero entry B(k,j) */
ASSIGN_BKJ ;
paend = Ap [k+1] ;
for (pa = Ap [k] ; pa < paend ; pa++, cnz++)
{
Ci [cnz] = Ai [pa] ; /* nonzero entry A(i,k) */
MULT (Cx [cnz], Cz [cnz], =) ; /* C(i,j) = A(i,k)*B(k,j) */
#ifdef NO_GCC_BUG
DROP_CHECK (Cx [cnz], Cz [cnz]) ; /* check C(i,j) == 0 */
#endif
}
pb++ ;
#ifndef NO_GCC_BUG
for (pc = pcstart ; pc < cnz ; pc++)
{
DROP_CHECK (Cx [pc], Cz [pc]) ; /* check if C(i,j) == 0 */
}
#endif
}
else
{
/* ---------------------------------------------------------- */
/* B(:,j) has two or more entries */
/* ---------------------------------------------------------- */
if (A_is_permutation)
{
/* ------------------------------------------------------ */
/* A is a permutation matrix */
/* ------------------------------------------------------ */
needs_sorting = 1 ;
for ( ; pb < pbend ; pb++)
{
k = Bi [pb] ; /* nonzero entry B(k,j) */
ASSIGN_BKJ ;
i = Ai [k] ; /* nonzero entry A(i,k) */
Ci [pb] = i ;
pa = k ;
/* C(i,j) = A(i,k)*B(k,j) */
#ifndef UNSORTED
MULT (W [i], Wz [i], =) ;
#else
MULT (Cx [pb], Cz [pb], =) ;
#endif
}
cnz = pbend ;
}
else
{
/* ------------------------------------------------------ */
/* general case */
/* ------------------------------------------------------ */
/* first entry in jth column of B is simpler */
/* C(:,j) = A (:,k) * B (k,j) */
k = Bi [pb] ; /* nonzero entry B(k,j) */
ASSIGN_BKJ ;
paend = Ap [k+1] ;
for (pa = Ap [k] ; pa < paend ; pa++)
{
i = Ai [pa] ; /* nonzero entry A(i,k) */
Flag [i] = cnz ;
Ci [cnz] = i ; /* new entry C(i,j) */
/* C(i,j) = A(i,k)*B(k,j) */
#ifndef UNSORTED
MULT (W [i], Wz [i], =) ;
#else
MULT (Cx [cnz], Cz [cnz], =) ;
#endif
cnz++ ;
}
pb++ ;
for ( ; pb < pbend ; pb++)
{
k = Bi [pb] ; /* nonzero entry B(k,j) */
ASSIGN_BKJ ;
/* C(:,j) += A (:,k) * B (k,j) */
paend = Ap [k+1] ;
for (pa = Ap [k] ; pa < paend ; pa++)
{
i = Ai [pa] ; /* nonzero entry A(i,k) */
pc = Flag [i] ;
if (pc < pcstart)
{
pc = cnz++ ;
Flag [i] = pc ;
Ci [pc] = i ; /* new entry C(i,j) */
/* C(i,j) = A(i,k)*B(k,j) */
#ifndef UNSORTED
MULT (W [i], Wz [i], =) ;
needs_sorting = 1 ;
#else
MULT (Cx [pc], Cz [pc], =) ;
#endif
}
else
{
/* C(i,j) += A(i,k)*B(k,j) */
#ifndef UNSORTED
MULT (W [i], Wz [i], +=) ;
#else
MULT (Cx [pc], Cz [pc], +=) ;
#endif
}
}
}
}
/* ---------------------------------------------------------- */
/* sort the pattern of C(:,j) and gather the values of C(:,j) */
/* ---------------------------------------------------------- */
#ifndef UNSORTED
/* Sort the row indices in C(:,j). Use Cx as Int workspace.
* This assumes sizeof (Int) < sizeof (double). If blen <= 1,
* or if subsequent entries in B(:,j) appended entries onto C,
* there is no need to sort C(:,j), assuming A is sorted. */
if (needs_sorting)
{
ssmergesort (Ci + pcstart, (Int *) (Cx + pcstart),
cnz - pcstart) ;
}
for (pc = pcstart ; pc < cnz ; pc++)
{
#if defined (ACOMPLEX) || defined (BCOMPLEX)
i = Ci [pc] ;
cij = W [i] ; /* get C(i,j) from W */
czij = Wz [i] ;
Cx [pc] = cij ; /* copy C(i,j) into C */
Cz [pc] = czij ;
#else
cij = W [Ci [pc]] ; /* get C(i,j) from W */
Cx [pc] = cij ; /* copy C(i,j) into C */
#endif
DROP_CHECK (cij, czij) ; /* check if C(i,j) == 0 */
}
#else
/* no need to sort, but we do need to check for drop */
for (pc = pcstart ; pc < cnz ; pc++)
{
DROP_CHECK (Cx [pc], Cz [pc]) ; /* check if C(i,j) == 0 */
}
#endif
}
}
}
/* ---------------------------------------------------------------------- */
/* free workspace */
/* ---------------------------------------------------------------------- */
#ifndef UNSORTED
mxFree (W) ;
#endif
}
#undef ACOMPLEX
#undef BCOMPLEX
#undef MULT
#undef ASSIGN_BKJ
#undef DROP_CHECK
|