File: GB_AxB_dot_cij.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (660 lines) | stat: -rw-r--r-- 22,356 bytes parent folder | download | duplicates (2)
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
//------------------------------------------------------------------------------
// GB_AxB_dot_cij: compute C(i,j) = A(:,i)'*B(:,j)
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// Computes C(i,j) = A (:,i)'*B(:,j) via sparse dot product.  This template is
// used for all three cases: C=A'*B, C<M>=A'*B, and C<!M>=A'*B in dot2 when C
// is bitmap, and for C<M>=A'*B when C and M are sparse or hyper in dot3.

// if A_NOT_TRANSPOSED is #defined, then dot2 is computing A(i,:)*B(:,j)
// for C<#M>=A*B.  In this case A is either bitmap or full, and B is always
// sparse.

// When used as the multiplicative operator, the PAIR operator provides some
// useful special cases.  Its output is always one, for any matching pair of
// entries A(k,i)'*B(k,j) for some k.  If the monoid is ANY, then C(i,j)=1 if
// the intersection for the dot product is non-empty.  This intersection has to
// be found, in general.  However, suppose B(:,j) is dense.  Then every entry
// in the pattern of A(:,i)' will produce a 1 from the PAIR operator.  If the
// monoid is ANY, then C(i,j)=1 if A(:,i)' is nonempty.  If the monoid is PLUS,
// then C(i,j) is simply nnz(A(:,i)), assuming no overflow.  The XOR monoid
// acts like a 1-bit summation, so the result of the XOR_PAIR_BOOL semiring
// will be C(i,j) = mod (nnz(A(:,i)'*B(:,j)),2).

// If both A(:,i) and B(:,j) are sparse, then the intersection must still be
// found, so these optimizations can be used only if A(:,i) and/or B(:,j) are
// entirely populated.

// The #include'ing file must use GB_DECLARE_TERMINAL_CONST (zterminal),
// or define zterminal another way (see mxm/factory/GB_AxB_dot_generic.c).

#ifndef GB_MXM_SHARED_DEFINITIONS_H
#error "undefined"
#endif

#undef GB_A_INDEX
#ifdef GB_A_NOT_TRANSPOSED
#define GB_A_INDEX(k) (pA+(k)*vlen)
#else
#define GB_A_INDEX(k) (pA+(k))
#endif

//------------------------------------------------------------------------------
// C(i,j) = A(:,i)'*B(:,j): a single dot product
//------------------------------------------------------------------------------

{
    int64_t pB = pB_start ;
    ASSERT (vlen > 0) ;
    #if ( GB_B_IS_SPARSE || GB_B_IS_HYPER )
    ASSERT (bjnz > 0) ;
    #endif
    #if ( GB_A_IS_SPARSE || GB_A_IS_HYPER )
    ASSERT (ainz > 0) ;
    #endif

    #if ( GB_A_IS_FULL && GB_B_IS_FULL )
    {

        //----------------------------------------------------------------------
        // both A and B are full
        //----------------------------------------------------------------------

        #if GB_IS_ANY_PAIR_SEMIRING
        { 
            // nothing to do; C is iso
        }
        #elif GB_IS_LXOR_PAIR_SEMIRING
        { 
            // (boolean XOR monoid)_PAIR semiring
            cij = (GB_C_TYPE) (((uint64_t) vlen) & 0x1L) ;
        }
        #elif GB_IS_PLUS_PAIR_8_SEMIRING
        {
            // (PLUS int8, uint8 monoids)_PAIR semirings
            // only used by the JIT if int8 and uint8 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) vlen) & 0xFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_16_SEMIRING
        {
            // (PLUS int16, uint16 monoids)_PAIR semirings
            // only used by the JIT if int16 and uint16 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) vlen) & 0xFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_32_SEMIRING
        { 
            // (PLUS int32, uint32 monoids)_PAIR semirings
            cij = (GB_C_TYPE) (((uint64_t) vlen) & 0xFFFFFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_BIG_SEMIRING
        { 
            // (PLUS int64, uint64, float, or double)_PAIR semirings
            cij = (GB_C_TYPE) vlen ;
        }
        #elif GB_IS_MIN_FIRSTJ_SEMIRING
        { 
            // MIN_FIRSTJ semiring: take the first entry
            cij = GB_OFFSET ;
        }
        #elif GB_IS_MAX_FIRSTJ_SEMIRING
        { 
            // MAX_FIRSTJ semiring: take the last entry
            cij = vlen-1 + GB_OFFSET ;
        }
        #else
        {
            // cij = A(0,i) * B(0,j)
            GB_DECLAREA (aki) ;
            GB_GETA (aki, Ax, pA, A_iso) ;      // aki = A(0,i)
            GB_DECLAREB (bkj) ;
            GB_GETB (bkj, Bx, pB, B_iso) ;      // bkj = B(0,j)
            GB_MULT (cij, aki, bkj, i, 0, j) ;  // cij = aki * bkj
            GB_PRAGMA_SIMD_REDUCTION_MONOID (cij)
            for (int64_t k = 1 ; k < vlen ; k++)
            { 
                // break if cij terminal
                GB_IF_TERMINAL_BREAK (cij, zterminal) ;
                // cij += A(k,i) * B(k,j)
                GB_GETA (aki, Ax, pA+k, A_iso) ;    // aki = A(k,i)
                GB_GETB (bkj, Bx, pB+k, B_iso) ;    // bkj = B(k,j)
                GB_MULTADD (cij, aki, bkj, i, k, j) ; // cij += aki * bkj
            }
        }
        #endif
        GB_DOT_ALWAYS_SAVE_CIJ ;

    }
    #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_exists = true ;
                    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_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #else
        {
            for (int64_t k = 0 ; k < vlen ; k++)
            {
                if (Bb [pB+k])
                { 
                    GB_DOT (k, pA+k, pB+k) ;
                }
            }
        }
        #endif
        GB_DOT_SAVE_CIJ ;

    }
    #elif ( GB_A_IS_FULL && ( GB_B_IS_SPARSE || GB_B_IS_HYPER ) )
    {

        //----------------------------------------------------------------------
        // A is full and B is sparse/hyper (C = A'*B or A*B)
        //----------------------------------------------------------------------

        #if GB_IS_ANY_PAIR_SEMIRING
        { 
            // nothing to do; C is iso
        }
        #elif GB_IS_LXOR_PAIR_SEMIRING
        { 
            // (boolean XOR monoid)_PAIR semiring
            cij = (GB_C_TYPE) (((uint64_t) bjnz) & 0x1L) ;
        }
        #elif GB_IS_PLUS_PAIR_8_SEMIRING
        {
            // (PLUS int8, uint8 monoids)_PAIR semirings
            // only used by the JIT if int8 and uint8 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) bjnz) & 0xFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_16_SEMIRING
        {
            // (PLUS int16, uint16 monoids)_PAIR semirings
            // only used by the JIT if int16 and uint16 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) bjnz) & 0xFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_32_SEMIRING
        { 
            // (PLUS int32, uint32 monoids)_PAIR semirings
            cij = (GB_C_TYPE) (((uint64_t) bjnz) & 0xFFFFFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_BIG_SEMIRING
        { 
            // (PLUS int64, uint64, float, or double)_PAIR semirings
            cij = (GB_C_TYPE) bjnz ;
        }
        #elif GB_IS_MIN_FIRSTJ_SEMIRING
        { 
            // MIN_FIRSTJ semiring: take the first entry in B(:,j)
            cij = GB_IGET (Bi, pB) + GB_OFFSET ;
        }
        #elif GB_IS_MAX_FIRSTJ_SEMIRING
        { 
            // MAX_FIRSTJ semiring: take the last entry in B(:,j)
            cij = GB_IGET (Bi, pB_end-1) + GB_OFFSET ;
        }
        #else
        {
            int64_t k = GB_IGET (Bi, pB) ;      // first row index of B(:,j)
            // cij = (A(k,i) or A(i,k)) * B(k,j)
            GB_DECLAREA (aki) ;
            GB_GETA (aki, Ax, GB_A_INDEX(k), A_iso) ; // aki = A(k,i) or A(i,k)
            GB_DECLAREB (bkj) ;
            GB_GETB (bkj, Bx, pB  , B_iso) ;    // bkj = B(k,j)
            GB_MULT (cij, aki, bkj, i, k, j) ;  // cij = aki * bkj
            GB_PRAGMA_SIMD_REDUCTION_MONOID (cij)
            for (int64_t p = pB+1 ; p < pB_end ; p++)
            { 
                // break if cij terminal
                GB_IF_TERMINAL_BREAK (cij, zterminal) ;
                int64_t k = GB_IGET (Bi, p) ;
                // cij += (A(k,i) or A(i,k)) * B(k,j)
                GB_GETA (aki, Ax, GB_A_INDEX(k), A_iso) ; //aki=A(k,i) or A(i,k)
                GB_GETB (bkj, Bx, p, B_iso) ;           // bkj = B(k,j)
                GB_MULTADD (cij, aki, bkj, i, k, j) ;   // cij += aki * bkj
            }
        }
        #endif
        GB_DOT_ALWAYS_SAVE_CIJ ;

    }
    #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_exists = true ;
                    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_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #else
        {
            for (int64_t k = 0 ; k < vlen ; k++)
            {
                if (Ab [pA+k])
                { 
                    GB_DOT (k, pA+k, pB+k) ;
                }
            }
        }
        #endif
        GB_DOT_SAVE_CIJ ;

    }
    #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_exists = true ;
                    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_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #else
        {
            for (int64_t k = 0 ; k < vlen ; k++)
            {
                if (Ab [pA+k] && Bb [pB+k])
                { 
                    GB_DOT (k, pA+k, pB+k) ;
                }
            }
        }
        #endif
        GB_DOT_SAVE_CIJ ;

    }
    #elif ( GB_A_IS_BITMAP && ( GB_B_IS_SPARSE || GB_B_IS_HYPER ) )
    {

        //----------------------------------------------------------------------
        // A is bitmap and B is sparse/hyper (C = A'*B or A*B)
        //----------------------------------------------------------------------

        #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 = GB_IGET (Bi, p) ;
                if (Ab [GB_A_INDEX (k)])
                { 
                    cij_exists = true ;
                    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 = GB_IGET (Bi, p) ;
                if (Ab [GB_A_INDEX (k)])
                { 
                    cij_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #else
        {
            for (int64_t p = pB ; p < pB_end ; p++)
            {
                int64_t k = GB_IGET (Bi, p) ;
                if (Ab [GB_A_INDEX (k)])
                { 
                    GB_DOT (k, GB_A_INDEX (k), p) ;
                }
            }
        }
        #endif
        GB_DOT_SAVE_CIJ ;

    }
    #elif ( (GB_A_IS_SPARSE || GB_A_IS_HYPER) && GB_B_IS_FULL )
    {

        //----------------------------------------------------------------------
        // A is sparse/hyper and B is full
        //----------------------------------------------------------------------

        #if GB_IS_ANY_PAIR_SEMIRING
        { 
            // nothing to do; C is iso
        }
        #elif GB_IS_LXOR_PAIR_SEMIRING
        { 
            // (boolean XOR monoid)_PAIR semiring
            cij = (GB_C_TYPE) (((uint64_t) ainz) & 0x1L) ;
        }
        #elif GB_IS_PLUS_PAIR_8_SEMIRING
        {
            // (PLUS int8, uint8 monoids)_PAIR semirings
            // only used by the JIT if int8 and uint8 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) ainz) & 0xFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_16_SEMIRING
        {
            // (PLUS int16, uint16 monoids)_PAIR semirings
            // only used by the JIT if int16 and uint16 FactoryKernels disabled
            cij = (GB_C_TYPE) (((uint64_t) ainz) & 0xFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_32_SEMIRING
        { 
            // (PLUS int32, uint32 monoids)_PAIR semirings
            cij = (GB_C_TYPE) (((uint64_t) ainz) & 0xFFFFFFFFL) ;
        }
        #elif GB_IS_PLUS_PAIR_BIG_SEMIRING
        { 
            // (PLUS int64, uint64, float, or double)_PAIR semirings
            cij = (GB_C_TYPE) ainz ;
        }
        #elif GB_IS_MIN_FIRSTJ_SEMIRING
        { 
            // MIN_FIRSTJ semiring: take the first entry in A(:,i)
            cij = GB_IGET (Ai, pA) + GB_OFFSET ;
        }
        #elif GB_IS_MAX_FIRSTJ_SEMIRING
        { 
            // MAX_FIRSTJ semiring: take the last entry in A(:,i)
            cij = GB_IGET (Ai, pA_end-1) + GB_OFFSET ;
        }
        #else
        {
            int64_t k = GB_IGET (Ai, pA) ;      // first row index of A(:,i)
            // cij = A(k,i) * B(k,j)
            GB_DECLAREA (aki) ;
            GB_GETA (aki, Ax, pA  , A_iso) ;    // aki = A(k,i)
            GB_DECLAREB (bkj) ;
            GB_GETB (bkj, Bx, pB+k, B_iso) ;    // bkj = B(k,j)
            GB_MULT (cij, aki, bkj, i, k, j) ;  // cij = aki * bkj
            GB_PRAGMA_SIMD_REDUCTION_MONOID (cij)
            for (int64_t p = pA+1 ; p < pA_end ; p++)
            { 
                // break if cij terminal
                GB_IF_TERMINAL_BREAK (cij, zterminal) ;
                int64_t k = GB_IGET (Ai, p) ;
                // cij += A(k,i) * B(k,j)
                GB_GETA (aki, Ax, p   , A_iso) ;    // aki = A(k,i)
                GB_GETB (bkj, Bx, pB+k, B_iso) ;    // bkj = B(k,j)
                GB_MULTADD (cij, aki, bkj, i, k, j) ;   // cij += aki * bkj
            }
        }
        #endif
        GB_DOT_ALWAYS_SAVE_CIJ ;

    }
    #elif ( (GB_A_IS_SPARSE || GB_A_IS_HYPER) && GB_B_IS_BITMAP )
    {

        //----------------------------------------------------------------------
        // A is sparse/hyper and B is bitmap
        //----------------------------------------------------------------------

        #if GB_IS_MIN_FIRSTJ_SEMIRING
        {
            // MIN_FIRSTJ semiring: take the first entry
            for (int64_t p = pA ; p < pA_end ; p++)
            {
                int64_t k = GB_IGET (Ai, p) ;
                if (Bb [pB+k])
                { 
                    cij_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #elif GB_IS_MAX_FIRSTJ_SEMIRING
        {
            // MAX_FIRSTJ semiring: take the last entry
            for (int64_t p = pA_end-1 ; p >= pA ; p--)
            {
                int64_t k = GB_IGET (Ai, p) ;
                if (Bb [pB+k])
                { 
                    cij_exists = true ;
                    cij = k + GB_OFFSET ;
                    break ;
                }
            }
        }
        #else
        {
            for (int64_t p = pA ; p < pA_end ; p++)
            {
                int64_t k = GB_IGET (Ai, p) ;
                if (Bb [pB+k])
                { 
                    GB_DOT (k, p, pB+k) ;
                }
            }
        }
        #endif
        GB_DOT_SAVE_CIJ ;

    }
    #else
    {

        //----------------------------------------------------------------------
        // 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 (GB_IGET (Ai, pA_end-1) < ib_first || ib_last < GB_IGET (Ai, pA))
        { 

            //------------------------------------------------------------------
            // pattern of A(:,i) and B(:,j) don't overlap; C(i,j) doesn't exist
            //------------------------------------------------------------------

            ASSERT (!GB_CIJ_EXISTS) ;

        }
        else if (ainz > 8 * bjnz)
        {

            //------------------------------------------------------------------
            // B(:,j) is very sparse compared to A(:,i)
            //------------------------------------------------------------------

            while (pA < pA_end && pB < pB_end)
            {
                int64_t ia = GB_IGET (Ai, pA) ;
                int64_t ib = GB_IGET (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, GB_Ai_IS_32,
                        &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 the next entries to merge
                    GB_DOT (ia, pA, pB) ;
                    #if GB_IS_MIN_FIRSTJ_SEMIRING
                    break ;
                    #endif
                    pA++ ;
                    pB++ ;
                }
            }
            GB_DOT_SAVE_CIJ ;

        }
        else if (bjnz > 8 * ainz)
        {

            //------------------------------------------------------------------
            // A(:,i) is very sparse compared to B(:,j)
            //------------------------------------------------------------------

            while (pA < pA_end && pB < pB_end)
            {
                int64_t ia = GB_IGET (Ai, pA) ;
                int64_t ib = GB_IGET (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, GB_Bi_IS_32,
                        &pleft, &pright) ;
                    ASSERT (pleft > pB) ;
                    pB = pleft ;
                }
                else // ia == ib == k
                { 
                    // A(k,i) and B(k,j) are the next entries to merge
                    GB_DOT (ia, pA, pB) ;
                    #if GB_IS_MIN_FIRSTJ_SEMIRING
                    break ;
                    #endif
                    pA++ ;
                    pB++ ;
                }
            }
            GB_DOT_SAVE_CIJ ;

        }
        else
        {

            //------------------------------------------------------------------
            // A(:,i) and B(:,j) have about the same sparsity
            //------------------------------------------------------------------

            while (pA < pA_end && pB < pB_end)
            {
                int64_t ia = GB_IGET (Ai, pA) ;
                int64_t ib = GB_IGET (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 next entries to merge
                    GB_DOT (ia, pA, pB) ;
                    #if GB_IS_MIN_FIRSTJ_SEMIRING
                    break ;
                    #endif
                    pA++ ;
                    pB++ ;
                }
            }
            GB_DOT_SAVE_CIJ ;
        }
    }
    #endif
}