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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
|
//------------------------------------------------------------------------------
// GB_AxB_dot4_template: C+=A'*B via dot products, where C is full
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// C+=A'*B where C is full and computed in-place. The monoid of the semiring
// matches the accum operator, and the type of C matches the ztype of accum.
// The PAIR and FIRSTJ multiplicative operators are important special cases.
// The matrix C is the user input matrix. C is not iso on output, but might
// iso on input, in which case the input iso scalar is cinput, and C->x has
// been expanded to non-iso, and initialized if A and/or B are hypersparse.
// A and/or B can be iso.
// MIN_FIRSTJ or MIN_FIRSTJ1 semirings:
#define GB_IS_MIN_FIRSTJ_SEMIRING (GB_IS_IMIN_MONOID && GB_IS_FIRSTJ_MULTIPLIER)
// MAX_FIRSTJ or MAX_FIRSTJ1 semirings:
#define GB_IS_MAX_FIRSTJ_SEMIRING (GB_IS_IMAX_MONOID && GB_IS_FIRSTJ_MULTIPLIER)
// GB_OFFSET is 1 for the MIN/MAX_FIRSTJ1 semirings, and 0 otherwise.
#if GB_IS_ANY_MONOID
#error "dot4 is not used for the ANY monoid"
#endif
#undef GB_GET4C
#define GB_GET4C(cij,p) cij = (C_in_iso) ? cinput : Cx [p]
#if ((GB_A_IS_BITMAP || GB_A_IS_FULL) && (GB_B_IS_BITMAP || GB_B_IS_FULL ))
{
//--------------------------------------------------------------------------
// C += A'*B where A and B are both bitmap/full
//--------------------------------------------------------------------------
// FUTURE: This method is not particularly efficient when both A and B are
// bitmap/full. A better method would use tiles to reduce memory traffic.
int tid ;
#pragma omp parallel for num_threads(nthreads) schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
//----------------------------------------------------------------------
// get the task descriptor
//----------------------------------------------------------------------
const int a_tid = tid / nbslice ;
const int b_tid = tid % nbslice ;
const int64_t kA_start = A_slice [a_tid] ;
const int64_t kA_end = A_slice [a_tid+1] ;
const int64_t kB_start = B_slice [b_tid] ;
const int64_t kB_end = B_slice [b_tid+1] ;
for (int64_t j = kB_start ; j < kB_end ; j++)
{
//------------------------------------------------------------------
// get B(:,j) and C(:,j)
//------------------------------------------------------------------
const int64_t pC_start = j * cvlen ;
const int64_t pB_start = j * vlen ;
//------------------------------------------------------------------
// C(:,j) += A'*B(:,j)
//------------------------------------------------------------------
for (int64_t i = kA_start ; i < kA_end ; i++)
{
//--------------------------------------------------------------
// get A(:,i)
//--------------------------------------------------------------
const int64_t pA = i * vlen ;
//--------------------------------------------------------------
// get C(i,j)
//--------------------------------------------------------------
int64_t pC = i + pC_start ; // C(i,j) is at Cx [pC]
GB_CTYPE GB_GET4C (cij, pC) ; // cij = Cx [pC]
//--------------------------------------------------------------
// C(i,j) += A (:,i)*B(:,j): a single dot product
//--------------------------------------------------------------
int64_t pB = pB_start ;
#if ( GB_A_IS_FULL && GB_B_IS_FULL )
{
//----------------------------------------------------------
// both A and B are full
//----------------------------------------------------------
#if GB_IS_PAIR_MULTIPLIER
{
#if GB_IS_EQ_MONOID
// EQ_PAIR semiring
cij = (cij == 1) ;
#elif (GB_CTYPE_BITS > 0)
// PLUS, XOR monoids: A(:,i)'*B(:,j) is nnz(A(:,i)),
// for bool, 8-bit, 16-bit, or 32-bit integer
uint64_t t = ((uint64_t) cij) + vlen ;
cij = (GB_CTYPE) (t & GB_CTYPE_BITS) ;
#elif GB_IS_PLUS_FC32_MONOID
// PLUS monoid for float complex
cij = GxB_CMPLXF (crealf (cij) + (float) vlen, 0) ;
#elif GB_IS_PLUS_FC64_MONOID
// PLUS monoid for double complex
cij = GxB_CMPLX (creal (cij) + (double) vlen, 0) ;
#else
// PLUS monoid for float, double, or 64-bit integers
cij += (GB_CTYPE) vlen ;
#endif
}
#elif GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry
if (vlen > 0)
{
int64_t k = GB_OFFSET ;
cij = GB_IMIN (cij, k) ;
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry
if (vlen > 0)
{
int64_t k = vlen-1 + GB_OFFSET ;
cij = GB_IMAX (cij, k) ;
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t k = 0 ; k < vlen ; k++)
{
GB_DOT (k, pA+k, pB+k) ; // cij += A(k,i)*B(k,j)
}
}
#endif
}
#elif ( GB_A_IS_FULL && GB_B_IS_BITMAP )
{
//----------------------------------------------------------
// A is full and B is bitmap
//----------------------------------------------------------
#if GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry in B(:,j)
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Bb [pB+k])
{
cij = GB_IMIN (cij, k + GB_OFFSET) ;
break ;
}
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry in B(:,j)
for (int64_t k = vlen-1 ; k >= 0 ; k--)
{
if (Bb [pB+k])
{
cij = GB_IMAX (cij, k + GB_OFFSET) ;
break ;
}
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Bb [pB+k])
{
GB_DOT (k, pA+k, pB+k) ; // cij += A(k,i)*B(k,j)
}
}
}
#endif
}
#elif ( GB_A_IS_BITMAP && GB_B_IS_FULL )
{
//----------------------------------------------------------
// A is bitmap and B is full
//----------------------------------------------------------
#if GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry in A(:,i)
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Ab [pA+k])
{
cij = GB_IMIN (cij, k + GB_OFFSET) ;
break ;
}
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry in A(:,i)
for (int64_t k = vlen-1 ; k >= 0 ; k--)
{
if (Ab [pA+k])
{
cij = GB_IMAX (cij, k + GB_OFFSET) ;
break ;
}
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Ab [pA+k])
{
GB_DOT (k, pA+k, pB+k) ; // cij += A(k,i)*B(k,j)
}
}
}
#endif
}
#elif ( GB_A_IS_BITMAP && GB_B_IS_BITMAP )
{
//----------------------------------------------------------
// both A and B are bitmap
//----------------------------------------------------------
#if GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Ab [pA+k] && Bb [pB+k])
{
cij = GB_IMIN (cij, k + GB_OFFSET) ;
break ;
}
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry
for (int64_t k = vlen-1 ; k >= 0 ; k--)
{
if (Ab [pA+k] && Bb [pB+k])
{
cij = GB_IMAX (cij, k + GB_OFFSET) ;
break ;
}
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t k = 0 ; k < vlen ; k++)
{
if (Ab [pA+k] && Bb [pB+k])
{
GB_DOT (k, pA+k, pB+k) ; // cij += A(k,i)*B(k,j)
}
}
}
#endif
}
#endif
//--------------------------------------------------------------
// save C(i,j)
//--------------------------------------------------------------
Cx [pC] = cij ;
}
}
}
}
#elif ((GB_A_IS_SPARSE || GB_A_IS_HYPER) && (GB_B_IS_BITMAP || GB_B_IS_FULL ))
{
//--------------------------------------------------------------------------
// C += A'*B when A is sparse/hyper and B is bitmap/full
//--------------------------------------------------------------------------
// special cases: these methods are very fast, but cannot do not need
// to be unrolled.
#undef GB_SPECIAL_CASE_OR_TERMINAL
#define GB_SPECIAL_CASE_OR_TERMINAL \
( GB_IS_PAIR_MULTIPLIER /* the multiply op is PAIR */ \
|| GB_IS_MIN_FIRSTJ_SEMIRING /* min_firstj semiring */ \
|| GB_IS_MAX_FIRSTJ_SEMIRING /* max_firstj semiring */ \
|| GB_MONOID_IS_TERMINAL /* monoid has a terminal value */ \
|| GB_B_IS_PATTERN ) /* B is pattern-only */
// Transpose B and unroll the innermost loop if this condition holds: A
// must be sparse, B must be full, and no special semirings or operators
// can be used. The monoid must not be terminal. These conditions are
// known at compile time.
#undef GB_UNROLL
#define GB_UNROLL \
( GB_A_IS_SPARSE && GB_B_IS_FULL && !( GB_SPECIAL_CASE_OR_TERMINAL ) )
// If GB_UNROLL is true at compile-time, the simpler variant can still be
// used, without unrolling, for any of these conditions: (1) A is very
// sparse (fewer entries than the size of the W workspace) or (2) B is iso.
// The unrolled method does not allow B to be iso or pattern-only (such as
// for the FIRST multiplicative operator. If B is iso or pattern-only, the
// dense matrix G = B' would be a single scalar, or its values would not be
// accessed at all, so there is no benefit to computing G.
#if GB_UNROLL
const int64_t wp = (bvdim == 1) ? 0 : GB_IMIN (bvdim, 4) ;
const int64_t anz = GB_nnz (A) ;
if (anz < wp * vlen || B_iso)
#endif
{
//----------------------------------------------------------------------
// C += A'*B without workspace
//----------------------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
//------------------------------------------------------------------
// get the task descriptor
//------------------------------------------------------------------
const int64_t kA_start = A_slice [tid] ;
const int64_t kA_end = A_slice [tid+1] ;
//------------------------------------------------------------------
// C+=A'*B where A is sparse/hyper and B is bitmap/full
//------------------------------------------------------------------
if (bvdim == 1)
{
//--------------------------------------------------------------
// C += A'*B where C is a single vector
//--------------------------------------------------------------
#define pC_start 0
#define pB 0
#define j 0
for (int64_t kA = kA_start ; kA < kA_end ; kA++)
{
// get A(:,i)
#if GB_A_IS_HYPER
const int64_t i = Ah [kA] ;
#else
const int64_t i = kA ;
#endif
int64_t pA = Ap [kA] ;
const int64_t pA_end = Ap [kA+1] ;
const int64_t ainz = pA_end - pA ;
// C(i) += A(:,i)'*B(:,0)
#include "GB_AxB_dot4_cij.c"
}
#undef pC_start
#undef pB
#undef j
}
else
{
//--------------------------------------------------------------
// C += A'*B where C is a matrix
//--------------------------------------------------------------
for (int64_t kA = kA_start ; kA < kA_end ; kA++)
{
// get A(:,i)
#if GB_A_IS_HYPER
const int64_t i = Ah [kA] ;
#else
const int64_t i = kA ;
#endif
int64_t pA = Ap [kA] ;
const int64_t pA_end = Ap [kA+1] ;
const int64_t ainz = pA_end - pA ;
// C(i,:) += A(:,i)'*B
for (int64_t j = 0 ; j < bvdim ; j++)
{
// get B(:,j) and C(:,j)
const int64_t pC_start = j * cvlen ;
const int64_t pB = j * vlen ;
// C(i,j) += A(:,i)'*B(:,j)
#include "GB_AxB_dot4_cij.c"
}
}
}
}
}
#if GB_UNROLL
else
{
//----------------------------------------------------------------------
// C += A'*B: with workspace W for transposing B, one panel at a time
//----------------------------------------------------------------------
size_t W_size = 0 ;
GB_BTYPE *restrict W = NULL ;
if (bvdim > 1)
{
W = GB_MALLOC_WORK (wp * vlen, GB_BTYPE, &W_size) ;
if (W == NULL)
{
// out of memory
return (GrB_OUT_OF_MEMORY) ;
}
}
for (int64_t j1 = 0 ; j1 < bvdim ; j1 += 4)
{
//------------------------------------------------------------------
// C(:,j1:j2-1) += A * B (:,j1:j2-1) for a single panel
//------------------------------------------------------------------
const int64_t j2 = GB_IMIN (j1 + 4, bvdim) ;
switch (j2 - j1)
{
default :
case 1 :
{
//----------------------------------------------------------
// C(:,j1:j2-1) is a single vector; use B(:,j1) in place
//----------------------------------------------------------
const GB_BTYPE *restrict G = Bx + j1 * vlen ;
int tid ;
#pragma omp parallel for num_threads(nthreads) \
schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
// get the task descriptor
const int64_t kA_start = A_slice [tid] ;
const int64_t kA_end = A_slice [tid+1] ;
for (int64_t i = kA_start ; i < kA_end ; i++)
{
// get A(:,i)
const int64_t pA = Ap [i] ;
const int64_t pA_end = Ap [i+1] ;
// cx [0] = C(i,j1)
GB_CTYPE cx [1] ;
GB_GET4C (cx [0], i + j1*cvlen) ;
// cx [0] += A (:,i)'*G
for (int64_t p = pA ; p < pA_end ; p++)
{
// aki = A(k,i)
const int64_t k = Ai [p] ;
GB_GETA (aki, Ax, p, A_iso) ;
// cx [0] += A(k,i)*G(k,0)
GB_MULTADD (cx [0], aki, G [k], i, k, j1) ;
}
// C(i,j1) = cx [0]
Cx [i + j1*cvlen] = cx [0] ;
}
}
}
break ;
case 2 :
{
//----------------------------------------------------------
// G = B(:,j1:j1+1) and convert to row-form
//----------------------------------------------------------
GB_BTYPE *restrict G = W ;
int64_t k ;
#pragma omp parallel for num_threads(nthreads) \
schedule(static)
for (k = 0 ; k < vlen ; k++)
{
// G (k,0:1) = B (k,j1:j1+1)
const int64_t k2 = k << 1 ;
G [k2 ] = Bx [k + (j1 ) * vlen] ;
G [k2 + 1] = Bx [k + (j1 + 1) * vlen] ;
}
//----------------------------------------------------------
// C += A'*G where G is vlen-by-2 in row-form
//----------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) \
schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
// get the task descriptor
const int64_t kA_start = A_slice [tid] ;
const int64_t kA_end = A_slice [tid+1] ;
for (int64_t i = kA_start ; i < kA_end ; i++)
{
// get A(:,i)
const int64_t pA = Ap [i] ;
const int64_t pA_end = Ap [i+1] ;
// cx [0:1] = C(i,j1:j1+1)
GB_CTYPE cx [2] ;
GB_GET4C (cx [0], i + (j1 )*cvlen) ;
GB_GET4C (cx [1], i + (j1+1)*cvlen) ;
// cx [0:1] += A (:,i)'*G
for (int64_t p = pA ; p < pA_end ; p++)
{
// aki = A(k,i)
const int64_t k = Ai [p] ;
GB_GETA (aki, Ax, p, A_iso) ;
const int64_t k2 = k << 1 ;
// cx [0:1] += A(k,i)*G(k,0:1)
GB_MULTADD (cx [0], aki, G [k2], i, k, j1) ;
GB_MULTADD (cx [1], aki, G [k2+1], i, k, j1+1) ;
}
// C(i,j1:j1+1) = cx [0:1]
Cx [i + (j1 )*cvlen] = cx [0] ;
Cx [i + (j1+1)*cvlen] = cx [1] ;
}
}
}
break ;
case 3 :
{
//----------------------------------------------------------
// G = B(:,j1:j1+2) and convert to row-form
//----------------------------------------------------------
GB_BTYPE *restrict G = W ;
int64_t k ;
#pragma omp parallel for num_threads(nthreads) \
schedule(static)
for (k = 0 ; k < vlen ; k++)
{
// G (k,0:2) = B (k,j1:j1+2)
const int64_t k3 = k * 3 ;
G [k3 ] = Bx [k + (j1 ) * vlen] ;
G [k3 + 1] = Bx [k + (j1 + 1) * vlen] ;
G [k3 + 2] = Bx [k + (j1 + 2) * vlen] ;
}
//----------------------------------------------------------
// C += A'*G where G is vlen-by-3 in row-form
//----------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) \
schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
// get the task descriptor
const int64_t kA_start = A_slice [tid] ;
const int64_t kA_end = A_slice [tid+1] ;
for (int64_t i = kA_start ; i < kA_end ; i++)
{
// get A(:,i)
const int64_t pA = Ap [i] ;
const int64_t pA_end = Ap [i+1] ;
// cx [0:2] = C(i,j1:j1+2)
GB_CTYPE cx [3] ;
GB_GET4C (cx [0], i + (j1 )*cvlen) ;
GB_GET4C (cx [1], i + (j1+1)*cvlen) ;
GB_GET4C (cx [2], i + (j1+2)*cvlen) ;
// cx [0:2] += A (:,i)'*G
for (int64_t p = pA ; p < pA_end ; p++)
{
// aki = A(k,i)
const int64_t k = Ai [p] ;
GB_GETA (aki, Ax, p, A_iso) ;
const int64_t k3 = k * 3 ;
// cx [0:2] += A(k,i)*G(k,0:2)
GB_MULTADD (cx [0], aki, G [k3 ], i, k, j1) ;
GB_MULTADD (cx [1], aki, G [k3+1], i, k, j1+1) ;
GB_MULTADD (cx [2], aki, G [k3+2], i, k, j1+2) ;
}
// C(i,j1:j1+2) = cx [0:2]
Cx [i + (j1 )*cvlen] = cx [0] ;
Cx [i + (j1+1)*cvlen] = cx [1] ;
Cx [i + (j1+2)*cvlen] = cx [2] ;
}
}
}
break ;
case 4 :
{
//----------------------------------------------------------
// G = B(:,j1:j1+3) and convert to row-form
//----------------------------------------------------------
GB_BTYPE *restrict G = W ;
int64_t k ;
#pragma omp parallel for num_threads(nthreads) \
schedule(static)
for (k = 0 ; k < vlen ; k++)
{
// G (k,0:3) = B (k,j1:j1+3)
const int64_t k4 = k << 2 ;
G [k4 ] = Bx [k + (j1 ) * vlen] ;
G [k4 + 1] = Bx [k + (j1 + 1) * vlen] ;
G [k4 + 2] = Bx [k + (j1 + 2) * vlen] ;
G [k4 + 3] = Bx [k + (j1 + 3) * vlen] ;
}
//----------------------------------------------------------
// C += A'*G where G is vlen-by-4 in row-form
//----------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) \
schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
// get the task descriptor
const int64_t kA_start = A_slice [tid] ;
const int64_t kA_end = A_slice [tid+1] ;
for (int64_t i = kA_start ; i < kA_end ; i++)
{
// get A(:,i)
const int64_t pA = Ap [i] ;
const int64_t pA_end = Ap [i+1] ;
// cx [0:3] = C(i,j1:j1+3)
GB_CTYPE cx [4] ;
GB_GET4C (cx [0], i + (j1 )*cvlen) ;
GB_GET4C (cx [1], i + (j1+1)*cvlen) ;
GB_GET4C (cx [2], i + (j1+2)*cvlen) ;
GB_GET4C (cx [3], i + (j1+3)*cvlen) ;
// cx [0:3] += A (:,i)'*G
for (int64_t p = pA ; p < pA_end ; p++)
{
// aki = A(k,i)
const int64_t k = Ai [p] ;
GB_GETA (aki, Ax, p, A_iso) ;
const int64_t k4 = k << 2 ;
// cx [0:3] += A(k,i)*G(k,0:3)
GB_MULTADD (cx [0], aki, G [k4 ], i, k, j1) ;
GB_MULTADD (cx [1], aki, G [k4+1], i, k, j1+1) ;
GB_MULTADD (cx [2], aki, G [k4+2], i, k, j1+2) ;
GB_MULTADD (cx [3], aki, G [k4+3], i, k, j1+3) ;
}
// C(i,j1:j1+3) = cx [0:3]
Cx [i + (j1 )*cvlen] = cx [0] ;
Cx [i + (j1+1)*cvlen] = cx [1] ;
Cx [i + (j1+2)*cvlen] = cx [2] ;
Cx [i + (j1+3)*cvlen] = cx [3] ;
}
}
}
break ;
}
}
// free workspace
GB_FREE_WORK (&W, W_size) ;
}
#endif
}
#elif ( (GB_A_IS_BITMAP || GB_A_IS_FULL) && (GB_B_IS_SPARSE || GB_B_IS_HYPER))
{
//--------------------------------------------------------------------------
// C += A'*B where A is bitmap/full and B is sparse/hyper
//--------------------------------------------------------------------------
// FUTURE: this can be unrolled, like the case above
int tid ;
#pragma omp parallel for num_threads(nthreads) schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
//----------------------------------------------------------------------
// get the task descriptor
//----------------------------------------------------------------------
const int64_t kB_start = B_slice [tid] ;
const int64_t kB_end = B_slice [tid+1] ;
for (int64_t kB = kB_start ; kB < kB_end ; kB++)
{
//------------------------------------------------------------------
// get B(:,j) and C(:,j)
//------------------------------------------------------------------
#if GB_B_IS_HYPER
const int64_t j = Bh [kB] ;
#else
const int64_t j = kB ;
#endif
const int64_t pC_start = j * cvlen ;
const int64_t pB_start = Bp [kB] ;
const int64_t pB_end = Bp [kB+1] ;
const int64_t bjnz = pB_end - pB_start ;
//------------------------------------------------------------------
// C(:,j) += A'*B(:,j)
//------------------------------------------------------------------
for (int64_t i = 0 ; i < avdim ; i++)
{
//--------------------------------------------------------------
// get A(:,i)
//--------------------------------------------------------------
const int64_t pA = i * vlen ;
//--------------------------------------------------------------
// get C(i,j)
//--------------------------------------------------------------
int64_t pC = i + pC_start ; // C(i,j) is at Cx [pC]
GB_CTYPE GB_GET4C (cij, pC) ; // cij = Cx [pC]
//--------------------------------------------------------------
// C(i,j) += A (:,i)*B(:,j): a single dot product
//--------------------------------------------------------------
int64_t pB = pB_start ;
#if ( GB_A_IS_FULL )
{
//----------------------------------------------------------
// A is full and B is sparse/hyper
//----------------------------------------------------------
#if GB_IS_PAIR_MULTIPLIER
{
#if GB_IS_EQ_MONOID
// EQ_PAIR semiring
cij = (cij == 1) ;
#elif (GB_CTYPE_BITS > 0)
// PLUS, XOR monoids: A(:,i)'*B(:,j) is nnz(A(:,i)),
// for bool, 8-bit, 16-bit, or 32-bit integer
uint64_t t = ((uint64_t) cij) + bjnz ;
cij = (GB_CTYPE) (t & GB_CTYPE_BITS) ;
#elif GB_IS_PLUS_FC32_MONOID
// PLUS monoid for float complex
cij = GxB_CMPLXF (crealf (cij) + (float) bjnz, 0) ;
#elif GB_IS_PLUS_FC64_MONOID
// PLUS monoid for double complex
cij = GxB_CMPLX (creal (cij) + (double) bjnz, 0) ;
#else
// PLUS monoid for float, double, or 64-bit integers
cij += (GB_CTYPE) bjnz ;
#endif
}
#elif GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry in B(:,j)
if (bjnz > 0)
{
int64_t k = Bi [pB] + GB_OFFSET ;
cij = GB_IMIN (cij, k) ;
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry in B(:,j)
if (bjnz > 0)
{
int64_t k = Bi [pB_end-1] + GB_OFFSET ;
cij = GB_IMAX (cij, k) ;
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t p = pB ; p < pB_end ; p++)
{
int64_t k = Bi [p] ;
GB_DOT (k, pA+k, p) ; // cij += A(k,i)*B(k,j)
}
}
#endif
}
#else
{
//----------------------------------------------------------
// A is bitmap and B is sparse/hyper
//----------------------------------------------------------
#if GB_IS_MIN_FIRSTJ_SEMIRING
{
// MIN_FIRSTJ semiring: take the first entry
for (int64_t p = pB ; p < pB_end ; p++)
{
int64_t k = Bi [p] ;
if (Ab [pA+k])
{
cij = GB_IMIN (cij, k + GB_OFFSET) ;
break ;
}
}
}
#elif GB_IS_MAX_FIRSTJ_SEMIRING
{
// MAX_FIRSTJ semiring: take the last entry
for (int64_t p = pB_end-1 ; p >= pB ; p--)
{
int64_t k = Bi [p] ;
if (Ab [pA+k])
{
cij = GB_IMAX (cij, k + GB_OFFSET) ;
break ;
}
}
}
#else
{
GB_PRAGMA_SIMD_DOT (cij)
for (int64_t p = pB ; p < pB_end ; p++)
{
int64_t k = Bi [p] ;
if (Ab [pA+k])
{
GB_DOT (k, pA+k, p) ; // cij += A(k,i)*B(k,j)
}
}
}
#endif
}
#endif
//--------------------------------------------------------------
// save C(i,j)
//--------------------------------------------------------------
Cx [pC] = cij ;
}
}
}
}
#elif ( (GB_A_IS_SPARSE || GB_A_IS_HYPER) && (GB_B_IS_SPARSE || GB_B_IS_HYPER))
{
//--------------------------------------------------------------------------
// C+=A'*B where A and B are both sparse/hyper
//--------------------------------------------------------------------------
int tid ;
#pragma omp parallel for num_threads(nthreads) schedule(dynamic,1)
for (tid = 0 ; tid < ntasks ; tid++)
{
//----------------------------------------------------------------------
// get the task descriptor
//----------------------------------------------------------------------
const int a_tid = tid / nbslice ;
const int b_tid = tid % nbslice ;
const int64_t kA_start = A_slice [a_tid] ;
const int64_t kA_end = A_slice [a_tid+1] ;
const int64_t kB_start = B_slice [b_tid] ;
const int64_t kB_end = B_slice [b_tid+1] ;
//----------------------------------------------------------------------
// C+=A'*B via dot products
//----------------------------------------------------------------------
for (int64_t kB = kB_start ; kB < kB_end ; kB++)
{
//------------------------------------------------------------------
// get B(:,j) and C(:,j)
//------------------------------------------------------------------
#if GB_B_IS_HYPER
const int64_t j = Bh [kB] ;
#else
const int64_t j = kB ;
#endif
const int64_t pC_start = j * cvlen ;
const int64_t pB_start = Bp [kB] ;
const int64_t pB_end = Bp [kB+1] ;
const int64_t bjnz = pB_end - pB_start ;
//------------------------------------------------------------------
// C(:,j) += A'*B(:,j) where C is full
//------------------------------------------------------------------
for (int64_t kA = kA_start ; kA < kA_end ; kA++)
{
//--------------------------------------------------------------
// get A(:,i)
//--------------------------------------------------------------
#if GB_A_IS_HYPER
const int64_t i = Ah [kA] ;
#else
const int64_t i = kA ;
#endif
int64_t pA = Ap [kA] ;
const int64_t pA_end = Ap [kA+1] ;
const int64_t ainz = pA_end - pA ;
//--------------------------------------------------------------
// get C(i,j)
//--------------------------------------------------------------
int64_t pC = i + pC_start ; // C(i,j) is at Cx [pC]
GB_CTYPE GB_GET4C (cij, pC) ; // cij = Cx [pC]
//--------------------------------------------------------------
// C(i,j) += A (:,i)*B(:,j): a single dot product
//--------------------------------------------------------------
int64_t pB = pB_start ;
//----------------------------------------------------------
// both A and B are sparse/hyper
//----------------------------------------------------------
// The MIN_FIRSTJ semirings are exploited, by terminating as
// soon as any entry is found. The MAX_FIRSTJ semirings are
// not treated specially here. They could be done with a
// backwards traversal of the sparse vectors A(:,i) and
// B(:,j).
if (ainz == 0 || bjnz == 0 ||
Ai [pA_end-1] < Bi [pB_start] ||
Bi [pB_end-1] < Ai [pA])
{
//------------------------------------------------------
// A(:,i) and B(:,j) don't overlap, or are empty
//------------------------------------------------------
}
else if (ainz > 8 * bjnz)
{
//------------------------------------------------------
// B(:,j) is very sparse compared to A(:,i)
//------------------------------------------------------
while (pA < pA_end && pB < pB_end)
{
int64_t ia = Ai [pA] ;
int64_t ib = Bi [pB] ;
if (ia < ib)
{
// A(ia,i) appears before B(ib,j)
// discard all entries A(ia:ib-1,i)
int64_t pleft = pA + 1 ;
int64_t pright = pA_end - 1 ;
GB_TRIM_BINARY_SEARCH (ib, Ai, pleft, pright) ;
ASSERT (pleft > pA) ;
pA = pleft ;
}
else if (ib < ia)
{
// B(ib,j) appears before A(ia,i)
pB++ ;
}
else // ia == ib == k
{
// A(k,i) and B(k,j) are next entries to merge
GB_DOT (ia, pA, pB) ; // cij += A(k,i)*B(k,j)
#if GB_IS_MIN_FIRSTJ_SEMIRING
break ;
#endif
pA++ ;
pB++ ;
}
}
}
else if (bjnz > 8 * ainz)
{
//------------------------------------------------------
// A(:,i) is very sparse compared to B(:,j)
//------------------------------------------------------
while (pA < pA_end && pB < pB_end)
{
int64_t ia = Ai [pA] ;
int64_t ib = Bi [pB] ;
if (ia < ib)
{
// A(ia,i) appears before B(ib,j)
pA++ ;
}
else if (ib < ia)
{
// B(ib,j) appears before A(ia,i)
// discard all entries B(ib:ia-1,j)
int64_t pleft = pB + 1 ;
int64_t pright = pB_end - 1 ;
GB_TRIM_BINARY_SEARCH (ia, Bi, pleft, pright) ;
ASSERT (pleft > pB) ;
pB = pleft ;
}
else // ia == ib == k
{
// A(k,i) and B(k,j) are next entries to merge
GB_DOT (ia, pA, pB) ; // cij += A(k,i)*B(k,j)
#if GB_IS_MIN_FIRSTJ_SEMIRING
break ;
#endif
pA++ ;
pB++ ;
}
}
}
else
{
//------------------------------------------------------
// A(:,i) and B(:,j) have about the same sparsity
//------------------------------------------------------
while (pA < pA_end && pB < pB_end)
{
int64_t ia = Ai [pA] ;
int64_t ib = Bi [pB] ;
if (ia < ib)
{
// A(ia,i) appears before B(ib,j)
pA++ ;
}
else if (ib < ia)
{
// B(ib,j) appears before A(ia,i)
pB++ ;
}
else // ia == ib == k
{
// A(k,i) and B(k,j) are the entries to merge
GB_DOT (ia, pA, pB) ; // cij += A(k,i)*B(k,j)
#if GB_IS_MIN_FIRSTJ_SEMIRING
break ;
#endif
pA++ ;
pB++ ;
}
}
}
//--------------------------------------------------------------
// save C(i,j)
//--------------------------------------------------------------
Cx [pC] = cij ;
}
}
}
}
#endif
#undef GB_IS_MIN_FIRSTJ_SEMIRING
#undef GB_IS_MAX_FIRSTJ_SEMIRING
#undef GB_GET4C
#undef GB_SPECIAL_CASE_OR_TERMINAL
#undef GB_UNROLL
|