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
|
function codegen_axb_method (addop, multop, add, addfunc, mult, ztype, ...
xytype, identity, terminal, omp_atomic, omp_microsoft_atomic)
%CODEGEN_AXB_METHOD create a function to compute C=A*B over a semiring
%
% codegen_axb_method (addop, multop, add, addfunc, mult, ztype, xytype, ...
% identity, terminal, omp_atomic, omp_microsoft_atomic)
% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0
if (nargin >= 5 && isempty (mult))
return
end
f = fopen ('control.m4', 'w') ;
is_first = false ;
is_second = false ;
is_pair = false ;
is_positional = false ;
switch (multop)
case { 'firsti', 'firsti1', 'firstj', 'firstj1', 'secondj', 'secondj1' }
is_positional = true ;
case { 'first' }
is_first = true ;
case { 'second' }
is_second = true ;
case { 'pair' }
is_pair = true ;
end
is_any = isequal (addop, 'any') ;
is_max = isequal (addop, 'max') ;
is_min = isequal (addop, 'min') ;
is_eq = isequal (addop, 'eq') ;
is_any_pair = is_any && isequal (multop, 'pair') ;
if (is_any_pair)
% the any_pair_iso semiring does not access any numerical values
add = ' ' ;
addfunc = ' ' ;
mult = ' ' ;
ztype = 'iso' ;
xytype = 'any type' ;
identity = ' ' ;
terminal = 'break' ;
omp_atomic = 1 ;
omp_microsoft_atomic = 0 ;
% the any_pair_iso semiring is never disabled by GBCUDA_DEV
fprintf (f, 'define(`ifndef_GBCUDA_DEV'', `#if 1'')\n') ;
fprintf (f, 'define(`if_not_any_pair_semiring'', `#if 0'')\n') ;
else
% all other semirings are disabled by GBCUDA_DEV
fprintf (f, 'define(`ifndef_GBCUDA_DEV'', `#ifndef GBCUDA_DEV'')\n') ;
fprintf (f, 'define(`if_not_any_pair_semiring'', `#if 1'')\n') ;
end
ztype_is_real = ~codegen_contains (ztype, 'FC') ;
ztype_is_fp = isequal (ztype, 'float') || isequal (ztype, 'double') ;
is_any_complex = is_any && ~ztype_is_real ;
is_plus_pair_real = isequal (addop, 'plus') && isequal (multop, 'pair' ) && ztype_is_real ;
is_plus_times_fp = isequal (addop, 'plus') && isequal (multop, 'times') && ztype_is_fp ;
t_is_simple = isequal (multop, 'pair') || codegen_contains (multop, 'first') || codegen_contains (multop, 'second') ;
t_is_nonnan = isequal (multop (1:2), 'is') || (multop (1) == 'l') ;
switch (ztype)
case { 'iso' }
ztype_is_float = false ;
ztype_ignore_overflow = true ;
nbits = 0 ;
bits = '0' ;
case { 'bool' }
ztype_is_float = false ;
ztype_ignore_overflow = false ;
nbits = 8 ;
bits = '0x1L' ;
case { 'int8_t', 'uint8_t' }
ztype_is_float = false ;
ztype_ignore_overflow = false ;
nbits = 8 ;
bits = '0xffL' ;
xbits = '0xFF' ;
case { 'int16_t', 'uint16_t' }
ztype_is_float = false ;
ztype_ignore_overflow = false ;
nbits = 16 ;
bits = '0xffffL' ;
xbits = '0xFFFF' ;
case { 'int32_t', 'uint32_t' }
ztype_is_float = false ;
ztype_ignore_overflow = false ;
nbits = 32 ;
bits = '0xffffffffL' ;
xbits = '0xFFFFFFFF' ;
case { 'int64_t', 'uint64_t' }
ztype_is_float = false ;
ztype_ignore_overflow = true ;
nbits = 64 ;
bits = '0' ;
xbits = '0xFFFFFFFFFFFFFFFFL' ;
case { 'float' }
ztype_is_float = true ;
ztype_ignore_overflow = true ;
nbits = 32 ;
bits = '0' ;
case { 'double', 'GxB_FC32_t' }
ztype_is_float = true ;
ztype_ignore_overflow = true ;
nbits = 64 ;
bits = '0' ;
case { 'GxB_FC64_t' }
ztype_is_float = true ;
ztype_ignore_overflow = true ;
nbits = 128 ;
bits = '0' ;
otherwise
error ('unknown type') ;
end
% bits: special cases for the PAIR multiplier
fprintf (f, 'define(`GB_ctype_bits'', `%s'')\n', bits) ;
fprintf (f, 'define(`GB_cnbits'', `%d'')\n', nbits) ;
% nbits: # of bits in the type, needed for the atomic compare-exchange:
if (nbits == 0)
% iso semiring: no atomic compare-exchanged needed
fprintf (f, 'define(`GB_atomic_compare_exchange'', `;'')\n') ;
else
fprintf (f, 'define(`GB_atomic_compare_exchange'', `GB_ATOMIC_COMPARE_EXCHANGE_%d (target, expected, desired)'')\n', nbits) ;
end
if (is_pair)
% these semirings are renamed to any_pair, and not thus created
if (isequal (addop, 'land') || isequal (addop, 'eq' ) || ...
isequal (addop, 'lor' ) || isequal (addop, 'max' ) || ...
isequal (addop, 'min' ) || isequal (addop, 'times'))
return
end
end
if (is_any_pair)
fname = 'iso' ;
unsigned = true ;
bits = 0 ;
zname = 'iso' ;
else
[fname, unsigned, bits] = codegen_type (xytype) ;
[zname, ~, ~] = codegen_type (ztype) ;
end
name = sprintf ('%s_%s_%s', addop, multop, fname) ;
% function names
fprintf (f, 'define(`_Adot2B'', `_Adot2B__%s'')\n', name) ;
fprintf (f, 'define(`_Adot3B'', `_Adot3B__%s'')\n', name) ;
fprintf (f, 'define(`_Asaxpy3B'', `_Asaxpy3B__%s'')\n', name) ;
fprintf (f, 'define(`_Asaxpy3B_M'', `_Asaxpy3B_M__%s'')\n', name) ;
fprintf (f, 'define(`_Asaxpy3B_noM'', `_Asaxpy3B_noM__%s'')\n', name) ;
fprintf (f, 'define(`_Asaxpy3B_notM'', `_Asaxpy3B_notM__%s'')\n', name) ;
fprintf (f, 'define(`_AsaxbitB'', `_AsaxbitB__%s'')\n', name) ;
fprintf (f, 'define(`GB_AxB'', `GB_AxB__%s'')\n', name) ;
% type of C, A, and B
fprintf (f, 'define(`GB_ctype'', `%s'')\n', ztype) ;
fprintf (f, 'define(`GB_atype'', `%s'')\n', xytype) ;
fprintf (f, 'define(`GB_btype'', `%s'')\n', xytype) ;
if (is_any_pair)
fprintf (f, 'define(`GB_csize'', `0'')\n', ztype) ;
fprintf (f, 'define(`GB_asize'', `0'')\n', xytype) ;
fprintf (f, 'define(`GB_bsize'', `0'')\n', xytype) ;
else
fprintf (f, 'define(`GB_csize'', `sizeof (%s)'')\n', ztype) ;
fprintf (f, 'define(`GB_asize'', `sizeof (%s)'')\n', xytype) ;
fprintf (f, 'define(`GB_bsize'', `sizeof (%s)'')\n', xytype) ;
end
% flag if ztype can ignore overflow in some computations
fprintf (f, 'define(`GB_ctype_ignore_overflow'', `%d'')\n', ztype_ignore_overflow) ;
% simple typecast from 1 (or 2) real scalars to any other type
switch (ztype)
case { 'GxB_FC32_t' }
fprintf (f, 'define(`GB_ctype_cast'', `GxB_CMPLXF (((float) $1), ((float) $2))'')\n') ;
case { 'GxB_FC64_t' }
fprintf (f, 'define(`GB_ctype_cast'', `GxB_CMPLX (((double) $1), ((double) $2))'')\n') ;
case { 'iso' }
fprintf (f, 'define(`GB_ctype_cast'', `'')\n') ;
otherwise
fprintf (f, 'define(`GB_ctype_cast'', `((GB_ctype) $1)'')\n') ;
end
% simple typecast from 1 (or 2) real scalars to any other type
switch (xytype)
case { 'GxB_FC32_t' }
fprintf (f, 'define(`GB_atype_cast'', `GxB_CMPLXF (((float) $1), ((float) $2))'')\n') ;
case { 'GxB_FC64_t' }
fprintf (f, 'define(`GB_atype_cast'', `GxB_CMPLX (((double) $1), ((double) $2))'')\n') ;
case { 'any type' }
fprintf (f, 'define(`GB_atype_cast'', `'')\n') ;
otherwise
fprintf (f, 'define(`GB_atype_cast'', `((GB_atype) $1)'')\n') ;
end
% identity and terminal values for the monoid
fprintf (f, 'define(`GB_identity'', `%s'')\n', identity) ;
if (is_any_pair)
fprintf (f, 'define(`GB_is_any_pair_semiring'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_any_pair_semiring'', `0'')\n') ;
end
if (is_any_pair)
fprintf (f, 'define(`GB_is_plus_pair_real_semiring'', `0'')\n') ;
fprintf (f, 'define(`GB_cij_declare'', `'')\n') ;
elseif (is_plus_pair_real)
fprintf (f, 'define(`GB_is_plus_pair_real_semiring'', `1'')\n') ;
fprintf (f, 'define(`GB_cij_declare'', `%s cij = 0'')\n', ztype) ;
else
fprintf (f, 'define(`GB_is_plus_pair_real_semiring'', `0'')\n') ;
fprintf (f, 'define(`GB_cij_declare'', `%s cij'')\n', ztype) ;
end
if (is_plus_times_fp)
% plus_times_fp32 and plus_times_fp64 are accelerated with AVX2 or AVX512f instructions.
% More semirings will be accelerated in the future.
fprintf (f, 'define(`GB_semiring_has_avx_implementation'', `1'')\n') ;
else
fprintf (f, 'define(`GB_semiring_has_avx_implementation'', `0'')\n') ;
end
if (is_pair)
fprintf (f, 'define(`GB_is_pair_multiplier'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_pair_multiplier'', `0'')\n') ;
end
if (is_eq)
fprintf (f, 'define(`GB_is_eq_monoid'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_eq_monoid'', `0'')\n') ;
end
if (is_any)
% the ANY monoid terminates on the first entry seen
fprintf (f, 'define(`GB_is_any_monoid'', `1'')\n') ;
fprintf (f, 'define(`GB_terminal'', `break ;'')\n') ;
fprintf (f, 'define(`GB_dot_simd_vectorize'', `;'')\n') ;
fprintf (f, 'define(`GB_monoid_is_terminal'', `1'')\n') ;
elseif (~isempty (terminal))
% terminal monoids terminate when cij equals the terminal value
fprintf (f, 'define(`GB_is_any_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_terminal'', `if (cij == %s) { break ; }'')\n', ...
terminal) ;
fprintf (f, 'define(`GB_dot_simd_vectorize'', `;'')\n') ;
fprintf (f, 'define(`GB_monoid_is_terminal'', `1'')\n') ;
else
% non-terminal monoids
fprintf (f, 'define(`GB_is_any_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_terminal'', `;'')\n') ;
fprintf (f, 'define(`GB_monoid_is_terminal'', `0'')\n') ;
op = '' ;
if (ztype_is_real)
switch (addop)
case { 'plus' }
op = '+' ;
case { 'times' }
op = '*' ;
case { 'lor' }
op = '||' ;
case { 'land' }
op = '&&' ;
case { 'lxor' }
op = '^' ;
case { 'bor' }
op = '|' ;
case { 'band' }
op = '&' ;
case { 'bxor' }
op = '^' ;
otherwise
op = '' ;
end
end
if (isempty (op))
fprintf (f, 'define(`GB_dot_simd_vectorize'', `;'')\n') ;
else
pragma = sprintf ('GB_PRAGMA_SIMD_REDUCTION (%s,$1)', op) ;
fprintf (f, 'define(`GB_dot_simd_vectorize'', `%s'')\n', pragma) ;
end
end
if (ztype_is_real)
% The ANY monoid is atomic on any architecture.
% MIN, MAX, EQ, XNOR are implemented with atomic compare/exchange.
fprintf (f, 'define(`GB_has_atomic'', `1'')\n') ;
if (is_any)
% disable the ANY monoid for saxpy4
fprintf (f, 'define(`_Asaxpy4B'', `_Asaxpy4B__%s'')\n', '(none)') ;
fprintf (f, 'define(`if_saxpy4_enabled'', `#if 0'')\n') ;
else
fprintf (f, 'define(`_Asaxpy4B'', `_Asaxpy4B__%s'')\n', name) ;
fprintf (f, 'define(`if_saxpy4_enabled'', `#if 1'')\n') ;
end
else
% complex monoids are not atomic, except for 'plus'
if (isequal (addop, 'plus'))
fprintf (f, 'define(`GB_has_atomic'', `1'')\n') ;
fprintf (f, 'define(`_Asaxpy4B'', `_Asaxpy4B__%s'')\n', name) ;
fprintf (f, 'define(`if_saxpy4_enabled'', `#if 1'')\n') ;
else
fprintf (f, 'define(`GB_has_atomic'', `0'')\n') ;
fprintf (f, 'define(`_Asaxpy4B'', `_Asaxpy4B__%s'')\n', '(none)') ;
fprintf (f, 'define(`if_saxpy4_enabled'', `#if 0'')\n') ;
end
end
if (is_any)
% dot4 is disabled for the ANY monoid
fprintf (f, 'define(`_Adot4B'', `_Adot4B__%s'')\n', '(none)') ;
fprintf (f, 'define(`if_dot4_enabled'', `#if 0'')\n') ;
else
fprintf (f, 'define(`_Adot4B'', `_Adot4B__%s'')\n', name) ;
fprintf (f, 'define(`if_dot4_enabled'', `#if 1'')\n') ;
end
if (is_any)
% saxpy5 is disabled for the ANY monoid
fprintf (f, 'define(`_Asaxpy5B'', `_Asaxpy5B__%s'')\n', '(none)') ;
fprintf (f, 'define(`if_saxpy5_enabled'', `#if 0'')\n') ;
else
fprintf (f, 'define(`_Asaxpy5B'', `_Asaxpy5B__%s'')\n', name) ;
fprintf (f, 'define(`if_saxpy5_enabled'', `#if 1'')\n') ;
end
% firsti or firsti1 multiply operator
if (codegen_contains (multop, 'firsti'))
fprintf (f, 'define(`GB_is_firsti_multiplier'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_firsti_multiplier'', `0'')\n') ;
end
% firstj or firstj1 multiply operator
if (codegen_contains (multop, 'firstj'))
fprintf (f, 'define(`GB_is_firstj_multiplier'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_firstj_multiplier'', `0'')\n') ;
end
% secondj or secondj1 multiply operator
if (codegen_contains (multop, 'secondj'))
fprintf (f, 'define(`GB_is_secondj_multiplier'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_secondj_multiplier'', `0'')\n') ;
end
% offset for (first,second)*i1 or (first,second)*j1 multiply operator
if (codegen_contains (multop, 'i1') || codegen_contains (multop, 'j1'))
fprintf (f, 'define(`GB_offset'', `1'')\n') ;
else
fprintf (f, 'define(`GB_offset'', `0'')\n') ;
end
% plus_fc32 monoid:
if (isequal (addop, 'plus') && isequal (ztype, 'GxB_FC32_t'))
fprintf (f, 'define(`GB_is_plus_fc32_monoid'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_plus_fc32_monoid'', `0'')\n') ;
end
% plus_fc64 monoid:
if (isequal (addop, 'plus') && isequal (ztype, 'GxB_FC64_t'))
fprintf (f, 'define(`GB_is_plus_fc64_monoid'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_plus_fc64_monoid'', `0'')\n') ;
end
% any_fc32 monoid:
if (isequal (addop, 'any') && isequal (ztype, 'GxB_FC32_t'))
fprintf (f, 'define(`GB_is_any_fc32_monoid'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_any_fc32_monoid'', `0'')\n') ;
end
% any_fc64 monoid:
if (isequal (addop, 'any') && isequal (ztype, 'GxB_FC64_t'))
fprintf (f, 'define(`GB_is_any_fc64_monoid'', `1'')\n') ;
else
fprintf (f, 'define(`GB_is_any_fc64_monoid'', `0'')\n') ;
end
% min monoids:
if (is_min)
if (codegen_contains (ztype, 'int'))
% min monoid for signed or unsigned integers
fprintf (f, 'define(`GB_is_imin_monoid'', `1'')\n') ;
fprintf (f, 'define(`GB_is_fmin_monoid'', `0'')\n') ;
else
% min monoid for float or double
fprintf (f, 'define(`GB_is_imin_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_is_fmin_monoid'', `1'')\n') ;
end
else
% not a min monoid
fprintf (f, 'define(`GB_is_imin_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_is_fmin_monoid'', `0'')\n') ;
end
% max monoids:
if (is_max)
if (codegen_contains (ztype, 'int'))
% max monoid for signed or unsigned integers
fprintf (f, 'define(`GB_is_imax_monoid'', `1'')\n') ;
fprintf (f, 'define(`GB_is_fmax_monoid'', `0'')\n') ;
else
% max monoid for float or double
fprintf (f, 'define(`GB_is_imax_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_is_fmax_monoid'', `1'')\n') ;
end
else
% not a max monoid
fprintf (f, 'define(`GB_is_imax_monoid'', `0'')\n') ;
fprintf (f, 'define(`GB_is_fmax_monoid'', `0'')\n') ;
end
% only PLUS, TIMES, LOR, LAND, and LXOR can be done with OpenMP atomics
% in gcc and icc. However, only PLUS and TIMES work with OpenMP atomics
% in Microsoft Visual Studio; the LOR, LAND, and LXOR atomics don't compile.
fprintf (f, 'define(`GB_has_omp_atomic'', `%d'')\n', omp_atomic) ;
fprintf (f, 'define(`GB_microsoft_has_omp_atomic'', `%d'')\n', omp_microsoft_atomic) ;
% to get an entry from A
if (is_any_pair)
fprintf (f, 'define(`GB_a_is_pattern'', `1'')\n') ;
fprintf (f, 'define(`GB_geta'', `;'')\n') ;
fprintf (f, 'define(`GB_loada'', `;'')\n') ;
elseif (is_second || is_pair || is_positional)
% value of A is ignored for the SECOND and PAIR operators
fprintf (f, 'define(`GB_a_is_pattern'', `1'')\n') ;
fprintf (f, 'define(`GB_geta'', `;'')\n') ;
fprintf (f, 'define(`GB_loada'', `$1 [$2] = GBX ($3, $4, $5)'')\n') ;
else
fprintf (f, 'define(`GB_a_is_pattern'', `0'')\n') ;
fprintf (f, 'define(`GB_geta'', `%s $1 = GBX ($2, $3, $4)'')\n', xytype) ;
fprintf (f, 'define(`GB_loada'', `$1 [$2] = GBX ($3, $4, $5)'')\n') ;
end
% to get an entry from B
if (is_first || is_pair || is_positional)
% value of B is ignored for the FIRST and PAIR operators
fprintf (f, 'define(`GB_b_is_pattern'', `1'')\n') ;
fprintf (f, 'define(`GB_getb'', `;'')\n') ;
fprintf (f, 'define(`GB_loadb'', `;'')\n') ;
else
fprintf (f, 'define(`GB_b_is_pattern'', `0'')\n') ;
fprintf (f, 'define(`GB_getb'', `%s $1 = GBX ($2, $3, $4)'')\n', xytype) ;
fprintf (f, 'define(`GB_loadb'', `$1 [$2] = GBX ($3, $4, $5)'')\n') ;
end
% access the values of C
if (is_any_pair)
fprintf (f, 'define(`GB_cx'', `'')\n') ;
fprintf (f, 'define(`GB_putc'', `'')\n') ;
fprintf (f, 'define(`GB_cij_write'', `'')\n') ;
else
fprintf (f, 'define(`GB_cx'', `Cx [p]'')\n') ;
fprintf (f, 'define(`GB_putc'', `Cx [p] = cij'')\n') ;
fprintf (f, 'define(`GB_cij_write'', `Cx [p] = t'')\n') ;
end
% type-specific idiv
if (~isempty (strfind (mult, 'idiv')))
if (unsigned)
mult = strrep (mult, 'idiv', sprintf ('idiv_uint%d', bits)) ;
else
mult = strrep (mult, 'idiv', sprintf ('idiv_int%d', bits)) ;
end
end
% create the multiply operator (assignment)
if (is_any_pair)
fprintf (f, 'define(`GB_multiply'', `'')\n') ;
else
mult2 = strrep (mult, 'xarg', '`$2''') ;
mult2 = strrep (mult2, 'yarg', '`$3''') ;
fprintf (f, 'define(`GB_multiply'', `$1 = %s'')\n', mult2) ;
end
% create the add update, of the form w += t
if (is_any_pair)
add2 = ';' ;
elseif (is_min)
if (codegen_contains (ztype, 'int'))
% min monoid for signed or unsigned integers
add2 = 'if ($1 > $2) { $1 = $2 ; }' ;
else
% min monoid for float or double, with omitnan property
if (t_is_nonnan)
add2 = 'if (!islessequal ($1, $2)) { $1 = $2 ; }' ;
else
add2 = 'if (!isnan ($2) && !islessequal ($1, $2)) { $1 = $2 ; }' ;
end
end
elseif (is_max)
if (codegen_contains (ztype, 'int'))
% max monoid for signed or unsigned integers
add2 = 'if ($1 < $2) { $1 = $2 ; }' ;
else
% max monoid for float or double, with omitnan property
if (t_is_nonnan)
add2 = 'if (!isgreaterequal ($1, $2)) { $1 = $2 ; }' ;
else
add2 = 'if (!isnan ($2) && !isgreaterequal ($1, $2)) { $1 = $2 ; }';
end
end
else
% use the add function as given
add2 = strrep (add, 'w', '`$1''') ;
add2 = strrep (add2, 't', '`$2''') ;
end
fprintf (f, 'define(`GB_add_update'', `%s'')\n', add2) ;
if (is_any_pair)
fprintf (f, 'define(`GB_hx_write'', `;'')\n') ;
fprintf (f, 'define(`GB_cij_gather'', `;'')\n') ;
fprintf (f, 'define(`GB_cij_memcpy'', `;'')\n') ;
else
fprintf (f, 'define(`GB_hx_write'', `Hx [i] = t'')\n') ;
fprintf (f, 'define(`GB_cij_gather'', `Cx [p] = Hx [i]'')\n') ;
fprintf (f, 'define(`GB_cij_memcpy'', `memcpy (Cx +(p), Hx +(i), (len) * sizeof(%s));'')\n', ztype) ;
end
% create the add function, of the form w + t
if (is_any_pair)
fprintf (f, 'define(`GB_add_function'', `'')\n') ;
else
add2 = strrep (addfunc, 'w', '`$1''') ;
add2 = strrep (add2, 't', '`$2''') ;
fprintf (f, 'define(`GB_add_function'', `%s'')\n', add2) ;
end
% create the multiply-add statement, of the form:
% z += x*y ;
is_imin_or_imax = (isequal (addop, 'min') || isequal (addop, 'max')) && codegen_contains (ztype, 'int') ;
if (is_any_pair)
fprintf (f, 'define(`GB_multiply_add'', `'')\n') ;
elseif (~is_imin_or_imax && ...
(isequal (ztype, 'float') || isequal (ztype, 'double') || ...
isequal (ztype, 'bool') || is_first || is_second || is_pair || is_positional))
% float and double do not get promoted.
% bool is OK since promotion of the result (0 or 1) to int is safe.
% first and second are OK since no promotion occurs.
% is* operators are OK too.
multadd = strrep (add, 't', mult) ;
multadd = strrep (multadd, 'w', '`$1''') ;
multadd = strrep (multadd, 'xarg', '`$2''') ;
multadd = strrep (multadd, 'yarg', '`$3''') ;
fprintf (f, 'define(`GB_multiply_add'', `%s'')\n', multadd) ;
else
% use explicit typecasting to avoid ANSI C integer promotion.
add2 = strrep (add, 'w', '`$1''') ;
add2 = strrep (add2, 't', 'x_op_y') ;
fprintf (f, 'define(`GB_multiply_add'', `{ %s x_op_y = %s ; %s ; }'')\n', ...
ztype, mult2, add2) ;
end
% determine the identity byte
idbyte = '' ;
switch (addop)
% any monoid
case { 'any' }
% boolean monoids (except eq / lxnor)
case { 'lor' }
idbyte = '0' ;
case { 'land' }
idbyte = '1' ;
case { 'lxor' }
idbyte = '0' ;
% min/max monoids:
case { 'min' }
if (codegen_contains (ztype, 'uint'))
idbyte = '0xFF' ;
end
case { 'max' }
if (codegen_contains (ztype, 'uint'))
idbyte = '0' ;
end
% plus monoid: special cases for some multipliers
case { 'plus' }
idbyte = '0' ;
% bitwise monoids (except bxnor)
case { 'bor' }
idbyte = '0' ;
case { 'band' }
idbyte = '0xFF' ;
case { 'bxor' }
idbyte = '0' ;
case { 'eq' }
idbyte = '1' ;
case { 'times' }
idbyte = '' ;
case {'bxnor' }
idbyte = '0xFF' ;
end
if (isempty (idbyte))
fprintf (f, 'define(`GB_has_identity_byte'', `0'')\n') ;
fprintf (f, 'define(`GB_identity_byte'', `(none)'')\n') ;
else
fprintf (f, 'define(`GB_has_identity_byte'', `1'')\n') ;
fprintf (f, 'define(`GB_identity_byte'', `%s'')\n', idbyte) ;
end
% create the disable flag
if (is_any_pair)
% never disable the any_pair_iso semiring
fprintf (f, 'define(`GB_disable'', `0'')\n') ;
fprintf (f, 'define(`if_disabled'', `#if 0'')\n') ;
fprintf (f, 'define(`if_not_disabled'', `#if 1'')\n') ;
else
disable = sprintf ('GxB_NO_%s', upper (addop)) ;
if (~isequal (addop, multop))
disable = [disable (sprintf (' || GxB_NO_%s', upper (multop)))] ;
end
disable = [disable (sprintf (' || GxB_NO_%s', upper (fname)))] ;
disable = [disable (sprintf (' || GxB_NO_%s_%s', upper (addop), upper (zname)))] ;
if (~ (isequal (addop, multop) && isequal (zname, fname)))
disable = [disable (sprintf (' || GxB_NO_%s_%s', upper (multop), upper (fname)))] ;
end
disable = [disable (sprintf (' || GxB_NO_%s_%s_%s', ...
upper (addop), upper (multop), upper (fname))) ] ;
fprintf (f, 'define(`GB_disable'', `(%s)'')\n', disable) ;
fprintf (f, 'define(`if_disabled'', `#if GB_DISABLE'')\n') ;
fprintf (f, 'define(`if_not_disabled'', `#if ( !GB_DISABLE )'')\n') ;
end
fclose (f) ;
nprune = 76 ;
if (is_any_pair)
% the ANY_PAIR_ISO semiring goes in Generated1
k = 1 ;
else
% all other semirings go in Generated2
k = 2 ;
end
% construct the *.c file for the semiring
cmd = sprintf ('cat control.m4 Generator/GB_AxB.c | m4 | tail -n +%d > Generated%d/GB_AxB__%s.c', nprune, k, name) ;
system (cmd) ;
fprintf ('.') ;
% append to the *.h file
cmd = sprintf ('cat control.m4 Generator/GB_AxB.h | m4 | tail -n +%d >> Generated%d/GB_AxB__include%d.h', nprune, k, k) ;
system (cmd) ;
delete ('control.m4') ;
|