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 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
|
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas at Austin
Copyright (C) 2018 - 2019, Advanced Micro Devices, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name(s) of the copyright holder(s) nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "blis.h"
thrinfo_t BLIS_PACKM_SINGLE_THREADED = {};
thrinfo_t BLIS_GEMM_SINGLE_THREADED = {};
thrcomm_t BLIS_SINGLE_COMM = {};
// The global rntm_t structure. (The definition resides in bli_rntm.c.)
extern rntm_t global_rntm;
// A mutex to allow synchronous access to global_rntm. (The definition
// resides in bli_rntm.c.)
extern bli_pthread_mutex_t global_rntm_mutex;
// -----------------------------------------------------------------------------
void bli_thread_init( void )
{
bli_thrcomm_init( 1, &BLIS_SINGLE_COMM );
bli_packm_thrinfo_init_single( &BLIS_PACKM_SINGLE_THREADED );
bli_l3_thrinfo_init_single( &BLIS_GEMM_SINGLE_THREADED );
// Read the environment variables and use them to initialize the
// global runtime object.
bli_thread_init_rntm_from_env( &global_rntm );
}
void bli_thread_finalize( void )
{
}
// -----------------------------------------------------------------------------
void bli_thread_range_sub
(
thrinfo_t* thread,
dim_t n,
dim_t bf,
bool handle_edge_low,
dim_t* start,
dim_t* end
)
{
dim_t n_way = bli_thread_n_way( thread );
if ( n_way == 1 ) { *start = 0; *end = n; return; }
dim_t work_id = bli_thread_work_id( thread );
dim_t all_start = 0;
dim_t all_end = n;
dim_t size = all_end - all_start;
dim_t n_bf_whole = size / bf;
dim_t n_bf_left = size % bf;
dim_t n_bf_lo = n_bf_whole / n_way;
dim_t n_bf_hi = n_bf_whole / n_way;
// In this function, we partition the space between all_start and
// all_end into n_way partitions, each a multiple of block_factor
// with the exception of the one partition that recieves the
// "edge" case (if applicable).
//
// Here are examples of various thread partitionings, in units of
// the block_factor, when n_way = 4. (A '+' indicates the thread
// that receives the leftover edge case (ie: n_bf_left extra
// rows/columns in its sub-range).
// (all_start ... all_end)
// n_bf_whole _left hel n_th_lo _hi thr0 thr1 thr2 thr3
// 12 =0 f 0 4 3 3 3 3
// 12 >0 f 0 4 3 3 3 3+
// 13 >0 f 1 3 4 3 3 3+
// 14 >0 f 2 2 4 4 3 3+
// 15 >0 f 3 1 4 4 4 3+
// 15 =0 f 3 1 4 4 4 3
//
// 12 =0 t 4 0 3 3 3 3
// 12 >0 t 4 0 3+ 3 3 3
// 13 >0 t 3 1 3+ 3 3 4
// 14 >0 t 2 2 3+ 3 4 4
// 15 >0 t 1 3 3+ 4 4 4
// 15 =0 t 1 3 3 4 4 4
// As indicated by the table above, load is balanced as equally
// as possible, even in the presence of an edge case.
// First, we must differentiate between cases where the leftover
// "edge" case (n_bf_left) should be allocated to a thread partition
// at the low end of the index range or the high end.
if ( handle_edge_low == FALSE )
{
// Notice that if all threads receive the same number of
// block_factors, those threads are considered "high" and
// the "low" thread group is empty.
dim_t n_th_lo = n_bf_whole % n_way;
//dim_t n_th_hi = n_way - n_th_lo;
// If some partitions must have more block_factors than others
// assign the slightly larger partitions to lower index threads.
if ( n_th_lo != 0 ) n_bf_lo += 1;
// Compute the actual widths (in units of rows/columns) of
// individual threads in the low and high groups.
dim_t size_lo = n_bf_lo * bf;
dim_t size_hi = n_bf_hi * bf;
// Precompute the starting indices of the low and high groups.
dim_t lo_start = all_start;
dim_t hi_start = all_start + n_th_lo * size_lo;
// Compute the start and end of individual threads' ranges
// as a function of their work_ids and also the group to which
// they belong (low or high).
if ( work_id < n_th_lo )
{
*start = lo_start + (work_id ) * size_lo;
*end = lo_start + (work_id+1) * size_lo;
}
else // if ( n_th_lo <= work_id )
{
*start = hi_start + (work_id-n_th_lo ) * size_hi;
*end = hi_start + (work_id-n_th_lo+1) * size_hi;
// Since the edge case is being allocated to the high
// end of the index range, we have to advance the last
// thread's end.
if ( work_id == n_way - 1 ) *end += n_bf_left;
}
}
else // if ( handle_edge_low == TRUE )
{
// Notice that if all threads receive the same number of
// block_factors, those threads are considered "low" and
// the "high" thread group is empty.
dim_t n_th_hi = n_bf_whole % n_way;
dim_t n_th_lo = n_way - n_th_hi;
// If some partitions must have more block_factors than others
// assign the slightly larger partitions to higher index threads.
if ( n_th_hi != 0 ) n_bf_hi += 1;
// Compute the actual widths (in units of rows/columns) of
// individual threads in the low and high groups.
dim_t size_lo = n_bf_lo * bf;
dim_t size_hi = n_bf_hi * bf;
// Precompute the starting indices of the low and high groups.
dim_t lo_start = all_start;
dim_t hi_start = all_start + n_th_lo * size_lo
+ n_bf_left;
// Compute the start and end of individual threads' ranges
// as a function of their work_ids and also the group to which
// they belong (low or high).
if ( work_id < n_th_lo )
{
*start = lo_start + (work_id ) * size_lo;
*end = lo_start + (work_id+1) * size_lo;
// Since the edge case is being allocated to the low
// end of the index range, we have to advance the
// starts/ends accordingly.
if ( work_id == 0 ) *end += n_bf_left;
else { *start += n_bf_left;
*end += n_bf_left; }
}
else // if ( n_th_lo <= work_id )
{
*start = hi_start + (work_id-n_th_lo ) * size_hi;
*end = hi_start + (work_id-n_th_lo+1) * size_hi;
}
}
}
siz_t bli_thread_range_l2r
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
num_t dt = bli_obj_dt( a );
dim_t m = bli_obj_length_after_trans( a );
dim_t n = bli_obj_width_after_trans( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
bli_thread_range_sub( thr, n, bf,
FALSE, start, end );
return m * ( *end - *start );
}
siz_t bli_thread_range_r2l
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
num_t dt = bli_obj_dt( a );
dim_t m = bli_obj_length_after_trans( a );
dim_t n = bli_obj_width_after_trans( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
bli_thread_range_sub( thr, n, bf,
TRUE, start, end );
return m * ( *end - *start );
}
siz_t bli_thread_range_t2b
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
num_t dt = bli_obj_dt( a );
dim_t m = bli_obj_length_after_trans( a );
dim_t n = bli_obj_width_after_trans( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
bli_thread_range_sub( thr, m, bf,
FALSE, start, end );
return n * ( *end - *start );
}
siz_t bli_thread_range_b2t
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
num_t dt = bli_obj_dt( a );
dim_t m = bli_obj_length_after_trans( a );
dim_t n = bli_obj_width_after_trans( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
bli_thread_range_sub( thr, m, bf,
TRUE, start, end );
return n * ( *end - *start );
}
// -----------------------------------------------------------------------------
dim_t bli_thread_range_width_l
(
doff_t diagoff_j,
dim_t m,
dim_t n_j,
dim_t j,
dim_t n_way,
dim_t bf,
dim_t bf_left,
double area_per_thr,
bool handle_edge_low
)
{
dim_t width;
// In this function, we assume that we are somewhere in the process of
// partitioning an m x n lower-stored region (with arbitrary diagonal
// offset) n_ways along the n dimension (into column panels). The value
// j identifies the left-to-right subpartition index (from 0 to n_way-1)
// of the subpartition whose width we are about to compute using the
// area per thread determined by the caller. n_j is the number of
// columns in the remaining region of the matrix being partitioned,
// and diagoff_j is that region's diagonal offset.
// If this is the last subpartition, the width is simply equal to n_j.
// Note that this statement handles cases where the "edge case" (if
// one exists) is assigned to the high end of the index range (ie:
// handle_edge_low == FALSE).
if ( j == n_way - 1 ) return n_j;
// At this point, we know there are at least two subpartitions left.
// We also know that IF the submatrix contains a completely dense
// rectangular submatrix, it will occur BEFORE the triangular (or
// trapezoidal) part.
// Here, we implement a somewhat minor load balancing optimization
// that ends up getting employed only for relatively small matrices.
// First, recall that all subpartition widths will be some multiple
// of the blocking factor bf, except perhaps either the first or last
// subpartition, which will receive the edge case, if it exists.
// Also recall that j represents the current thread (or thread group,
// or "caucus") for which we are computing a subpartition width.
// If n_j is sufficiently small that we can only allocate bf columns
// to each of the remaining threads, then we set the width to bf. We
// do not allow the subpartition width to be less than bf, so, under
// some conditions, if n_j is small enough, some of the reamining
// threads may not get any work. For the purposes of this lower bound
// on work (ie: width >= bf), we allow the edge case to count as a
// "full" set of bf columns.
{
dim_t n_j_bf = n_j / bf + ( bf_left > 0 ? 1 : 0 );
if ( n_j_bf <= n_way - j )
{
if ( j == 0 && handle_edge_low )
width = ( bf_left > 0 ? bf_left : bf );
else
width = bf;
// Make sure that the width does not exceed n_j. This would
// occur if and when n_j_bf < n_way - j; that is, when the
// matrix being partitioned is sufficiently small relative to
// n_way such that there is not even enough work for every
// (remaining) thread to get bf (or bf_left) columns. The
// net effect of this safeguard is that some threads may get
// assigned empty ranges (ie: no work), which of course must
// happen in some situations.
if ( width > n_j ) width = n_j;
return width;
}
}
// This block computes the width assuming that we are entirely within
// a dense rectangle that precedes the triangular (or trapezoidal)
// part.
{
// First compute the width of the current panel under the
// assumption that the diagonal offset would not intersect.
width = ( dim_t )bli_round( ( double )area_per_thr / ( double )m );
// Adjust the width, if necessary. Specifically, we may need
// to allocate the edge case to the first subpartition, if
// requested; otherwise, we just need to ensure that the
// subpartition is a multiple of the blocking factor.
if ( j == 0 && handle_edge_low )
{
if ( width % bf != bf_left ) width += bf_left - ( width % bf );
}
else // if interior case
{
// Round up to the next multiple of the blocking factor.
//if ( width % bf != 0 ) width += bf - ( width % bf );
// Round to the nearest multiple of the blocking factor.
if ( width % bf != 0 ) width = bli_round_to_mult( width, bf );
}
}
// We need to recompute width if the panel, according to the width
// as currently computed, would intersect the diagonal.
if ( diagoff_j < width )
{
dim_t offm_inc, offn_inc;
// Prune away the unstored region above the diagonal, if it exists.
// Note that the entire region was pruned initially, so we know that
// we don't need to try to prune the right side. (Also, we discard
// the offset deltas since we don't need to actually index into the
// subpartition.)
bli_prune_unstored_region_top_l( &diagoff_j, &m, &n_j, &offm_inc );
//bli_prune_unstored_region_right_l( &diagoff_j, &m, &n_j, &offn_inc );
// We don't need offm_inc, offn_inc here. These statements should
// prevent compiler warnings.
( void )offm_inc;
( void )offn_inc;
// Prepare to solve a quadratic equation to find the width of the
// current (jth) subpartition given the m dimension, diagonal offset,
// and area.
// NOTE: We know that the +/- in the quadratic formula must be a +
// here because we know that the desired solution (the subpartition
// width) will be smaller than (m + diagoff), not larger. If you
// don't believe me, draw a picture!
const double a = -0.5;
const double b = ( double )m + ( double )diagoff_j + 0.5;
const double c = -0.5 * ( ( double )diagoff_j *
( ( double )diagoff_j + 1.0 )
) - area_per_thr;
const double r = b * b - 4.0 * a * c;
// If the quadratic solution is not imaginary, round it and use that
// as our width, but make sure it didn't round to zero. Otherwise,
// discard the quadratic solution and leave width, as previously
// computed, unchanged.
if ( r >= 0.0 )
{
const double x = ( -b + sqrt( r ) ) / ( 2.0 * a );
width = ( dim_t )bli_round( x );
if ( width == 0 ) width = 1;
}
// Adjust the width, if necessary.
if ( j == 0 && handle_edge_low )
{
if ( width % bf != bf_left ) width += bf_left - ( width % bf );
}
else // if interior case
{
// Round up to the next multiple of the blocking factor.
//if ( width % bf != 0 ) width += bf - ( width % bf );
// Round to the nearest multiple of the blocking factor.
if ( width % bf != 0 ) width = bli_round_to_mult( width, bf );
}
}
// Make sure that the width, after being adjusted, does not cause the
// subpartition to exceed n_j.
if ( width > n_j ) width = n_j;
return width;
}
siz_t bli_find_area_trap_l
(
dim_t m,
dim_t n,
doff_t diagoff
)
{
dim_t offm_inc = 0;
dim_t offn_inc = 0;
double tri_area;
double area;
// Prune away any rectangular region above where the diagonal
// intersects the left edge of the subpartition, if it exists.
bli_prune_unstored_region_top_l( &diagoff, &m, &n, &offm_inc );
// Prune away any rectangular region to the right of where the
// diagonal intersects the bottom edge of the subpartition, if
// it exists. (This shouldn't ever be needed, since the caller
// would presumably have already performed rightward pruning,
// but it's here just in case.)
bli_prune_unstored_region_right_l( &diagoff, &m, &n, &offn_inc );
( void )offm_inc;
( void )offn_inc;
// Compute the area of the empty triangle so we can subtract it
// from the area of the rectangle that bounds the subpartition.
if ( bli_intersects_diag_n( diagoff, m, n ) )
{
double tri_dim = ( double )( n - diagoff - 1 );
tri_area = tri_dim * ( tri_dim + 1.0 ) / 2.0;
}
else
{
// If the diagonal does not intersect the trapezoid, then
// we can compute the area as a simple rectangle.
tri_area = 0.0;
}
area = ( double )m * ( double )n - tri_area;
return ( siz_t )area;
}
// -----------------------------------------------------------------------------
siz_t bli_thread_range_weighted_sub
(
thrinfo_t* restrict thread,
doff_t diagoff,
uplo_t uplo,
dim_t m,
dim_t n,
dim_t bf,
bool handle_edge_low,
dim_t* restrict j_start_thr,
dim_t* restrict j_end_thr
)
{
dim_t n_way = bli_thread_n_way( thread );
dim_t my_id = bli_thread_work_id( thread );
dim_t bf_left = n % bf;
dim_t j;
dim_t off_j;
doff_t diagoff_j;
dim_t n_left;
dim_t width_j;
dim_t offm_inc, offn_inc;
double tri_dim, tri_area;
double area_total, area_per_thr;
siz_t area = 0;
// In this function, we assume that the caller has already determined
// that (a) the diagonal intersects the submatrix, and (b) the submatrix
// is either lower- or upper-stored.
if ( bli_is_lower( uplo ) )
{
// Prune away the unstored region above the diagonal, if it exists,
// and then to the right of where the diagonal intersects the bottom,
// if it exists. (Also, we discard the offset deltas since we don't
// need to actually index into the subpartition.)
bli_prune_unstored_region_top_l( &diagoff, &m, &n, &offm_inc );
bli_prune_unstored_region_right_l( &diagoff, &m, &n, &offn_inc );
// We don't need offm_inc, offn_inc here. These statements should
// prevent compiler warnings.
( void )offm_inc;
( void )offn_inc;
// Now that pruning has taken place, we know that diagoff >= 0.
// Compute the total area of the submatrix, accounting for the
// location of the diagonal, and divide it by the number of ways
// of parallelism.
tri_dim = ( double )( n - diagoff - 1 );
tri_area = tri_dim * ( tri_dim + 1.0 ) / 2.0;
area_total = ( double )m * ( double )n - tri_area;
area_per_thr = area_total / ( double )n_way;
// Initialize some variables prior to the loop: the offset to the
// current subpartition, the remainder of the n dimension, and
// the diagonal offset of the current subpartition.
off_j = 0;
diagoff_j = diagoff;
n_left = n;
// Iterate over the subpartition indices corresponding to each
// thread/caucus participating in the n_way parallelism.
for ( j = 0; j < n_way; ++j )
{
// Compute the width of the jth subpartition, taking the
// current diagonal offset into account, if needed.
width_j =
bli_thread_range_width_l
(
diagoff_j, m, n_left,
j, n_way,
bf, bf_left,
area_per_thr,
handle_edge_low
);
// If the current thread belongs to caucus j, this is his
// subpartition. So we compute the implied index range and
// end our search.
if ( j == my_id )
{
*j_start_thr = off_j;
*j_end_thr = off_j + width_j;
area = bli_find_area_trap_l( m, width_j, diagoff_j );
break;
}
// Shift the current subpartition's starting and diagonal offsets,
// as well as the remainder of the n dimension, according to the
// computed width, and then iterate to the next subpartition.
off_j += width_j;
diagoff_j -= width_j;
n_left -= width_j;
}
}
else // if ( bli_is_upper( uplo ) )
{
// Express the upper-stored case in terms of the lower-stored case.
// First, we convert the upper-stored trapezoid to an equivalent
// lower-stored trapezoid by rotating it 180 degrees.
bli_rotate180_trapezoid( &diagoff, &uplo, &m, &n );
// Now that the trapezoid is "flipped" in the n dimension, negate
// the bool that encodes whether to handle the edge case at the
// low (or high) end of the index range.
bli_toggle_bool( &handle_edge_low );
// Compute the appropriate range for the rotated trapezoid.
area = bli_thread_range_weighted_sub
(
thread, diagoff, uplo, m, n, bf,
handle_edge_low,
j_start_thr, j_end_thr
);
// Reverse the indexing basis for the subpartition ranges so that
// the indices, relative to left-to-right iteration through the
// unrotated upper-stored trapezoid, map to the correct columns
// (relative to the diagonal). This amounts to subtracting the
// range from n.
bli_reverse_index_direction( n, j_start_thr, j_end_thr );
}
return area;
}
siz_t bli_thread_range_mdim
(
dir_t direct,
thrinfo_t* thr,
obj_t* a,
obj_t* b,
obj_t* c,
cntl_t* cntl,
cntx_t* cntx,
dim_t* start,
dim_t* end
)
{
bszid_t bszid = bli_cntl_bszid( cntl );
opid_t family = bli_cntl_family( cntl );
// This is part of trsm's current implementation, whereby right side
// cases are implemented in left-side micro-kernels, which requires
// we swap the usage of the register blocksizes for the purposes of
// packing A and B.
if ( family == BLIS_TRSM )
{
if ( bli_obj_root_is_triangular( a ) ) bszid = BLIS_MR;
else bszid = BLIS_NR;
}
blksz_t* bmult = bli_cntx_get_bmult( bszid, cntx );
obj_t* x;
bool use_weighted;
// Use the operation family to choose the one of the two matrices
// being partitioned that potentially has structure, and also to
// decide whether or not we need to use weighted range partitioning.
// NOTE: It's important that we use non-weighted range partitioning
// for hemm and symm (ie: the gemm family) because the weighted
// function will mistakenly skip over unstored regions of the
// structured matrix, even though they represent part of that matrix
// that will be dense and full (after packing).
if ( family == BLIS_GEMM ) { x = a; use_weighted = FALSE; }
else if ( family == BLIS_GEMMT ) { x = c; use_weighted = TRUE; }
else if ( family == BLIS_TRMM ) { x = a; use_weighted = TRUE; }
else /*family == BLIS_TRSM*/ { x = a; use_weighted = FALSE; }
if ( use_weighted )
{
if ( direct == BLIS_FWD )
return bli_thread_range_weighted_t2b( thr, x, bmult, start, end );
else
return bli_thread_range_weighted_b2t( thr, x, bmult, start, end );
}
else
{
if ( direct == BLIS_FWD )
return bli_thread_range_t2b( thr, x, bmult, start, end );
else
return bli_thread_range_b2t( thr, x, bmult, start, end );
}
}
siz_t bli_thread_range_ndim
(
dir_t direct,
thrinfo_t* thr,
obj_t* a,
obj_t* b,
obj_t* c,
cntl_t* cntl,
cntx_t* cntx,
dim_t* start,
dim_t* end
)
{
bszid_t bszid = bli_cntl_bszid( cntl );
opid_t family = bli_cntl_family( cntl );
// This is part of trsm's current implementation, whereby right side
// cases are implemented in left-side micro-kernels, which requires
// we swap the usage of the register blocksizes for the purposes of
// packing A and B.
if ( family == BLIS_TRSM )
{
if ( bli_obj_root_is_triangular( b ) ) bszid = BLIS_MR;
else bszid = BLIS_NR;
}
blksz_t* bmult = bli_cntx_get_bmult( bszid, cntx );
obj_t* x;
bool use_weighted;
// Use the operation family to choose the one of the two matrices
// being partitioned that potentially has structure, and also to
// decide whether or not we need to use weighted range partitioning.
// NOTE: It's important that we use non-weighted range partitioning
// for hemm and symm (ie: the gemm family) because the weighted
// function will mistakenly skip over unstored regions of the
// structured matrix, even though they represent part of that matrix
// that will be dense and full (after packing).
if ( family == BLIS_GEMM ) { x = b; use_weighted = FALSE; }
else if ( family == BLIS_GEMMT ) { x = c; use_weighted = TRUE; }
else if ( family == BLIS_TRMM ) { x = b; use_weighted = TRUE; }
else /*family == BLIS_TRSM*/ { x = b; use_weighted = FALSE; }
if ( use_weighted )
{
if ( direct == BLIS_FWD )
return bli_thread_range_weighted_l2r( thr, x, bmult, start, end );
else
return bli_thread_range_weighted_r2l( thr, x, bmult, start, end );
}
else
{
if ( direct == BLIS_FWD )
return bli_thread_range_l2r( thr, x, bmult, start, end );
else
return bli_thread_range_r2l( thr, x, bmult, start, end );
}
}
siz_t bli_thread_range_weighted_l2r
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
siz_t area;
// This function assigns area-weighted ranges in the n dimension
// where the total range spans 0 to n-1 with 0 at the left end and
// n-1 at the right end.
if ( bli_obj_intersects_diag( a ) &&
bli_obj_is_upper_or_lower( a ) )
{
num_t dt = bli_obj_dt( a );
doff_t diagoff = bli_obj_diag_offset( a );
uplo_t uplo = bli_obj_uplo( a );
dim_t m = bli_obj_length( a );
dim_t n = bli_obj_width( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
// Support implicit transposition.
if ( bli_obj_has_trans( a ) )
{
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
}
area =
bli_thread_range_weighted_sub
(
thr, diagoff, uplo, m, n, bf,
FALSE, start, end
);
}
else // if dense or zeros
{
area = bli_thread_range_l2r
(
thr, a, bmult,
start, end
);
}
return area;
}
siz_t bli_thread_range_weighted_r2l
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
siz_t area;
// This function assigns area-weighted ranges in the n dimension
// where the total range spans 0 to n-1 with 0 at the right end and
// n-1 at the left end.
if ( bli_obj_intersects_diag( a ) &&
bli_obj_is_upper_or_lower( a ) )
{
num_t dt = bli_obj_dt( a );
doff_t diagoff = bli_obj_diag_offset( a );
uplo_t uplo = bli_obj_uplo( a );
dim_t m = bli_obj_length( a );
dim_t n = bli_obj_width( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
// Support implicit transposition.
if ( bli_obj_has_trans( a ) )
{
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
}
bli_rotate180_trapezoid( &diagoff, &uplo, &m, &n );
area =
bli_thread_range_weighted_sub
(
thr, diagoff, uplo, m, n, bf,
TRUE, start, end
);
}
else // if dense or zeros
{
area = bli_thread_range_r2l
(
thr, a, bmult,
start, end
);
}
return area;
}
siz_t bli_thread_range_weighted_t2b
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
siz_t area;
// This function assigns area-weighted ranges in the m dimension
// where the total range spans 0 to m-1 with 0 at the top end and
// m-1 at the bottom end.
if ( bli_obj_intersects_diag( a ) &&
bli_obj_is_upper_or_lower( a ) )
{
num_t dt = bli_obj_dt( a );
doff_t diagoff = bli_obj_diag_offset( a );
uplo_t uplo = bli_obj_uplo( a );
dim_t m = bli_obj_length( a );
dim_t n = bli_obj_width( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
// Support implicit transposition.
if ( bli_obj_has_trans( a ) )
{
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
}
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
area =
bli_thread_range_weighted_sub
(
thr, diagoff, uplo, m, n, bf,
FALSE, start, end
);
}
else // if dense or zeros
{
area = bli_thread_range_t2b
(
thr, a, bmult,
start, end
);
}
return area;
}
siz_t bli_thread_range_weighted_b2t
(
thrinfo_t* thr,
obj_t* a,
blksz_t* bmult,
dim_t* start,
dim_t* end
)
{
siz_t area;
// This function assigns area-weighted ranges in the m dimension
// where the total range spans 0 to m-1 with 0 at the bottom end and
// m-1 at the top end.
if ( bli_obj_intersects_diag( a ) &&
bli_obj_is_upper_or_lower( a ) )
{
num_t dt = bli_obj_dt( a );
doff_t diagoff = bli_obj_diag_offset( a );
uplo_t uplo = bli_obj_uplo( a );
dim_t m = bli_obj_length( a );
dim_t n = bli_obj_width( a );
dim_t bf = bli_blksz_get_def( dt, bmult );
// Support implicit transposition.
if ( bli_obj_has_trans( a ) )
{
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
}
bli_reflect_about_diag( &diagoff, &uplo, &m, &n );
bli_rotate180_trapezoid( &diagoff, &uplo, &m, &n );
area = bli_thread_range_weighted_sub
(
thr, diagoff, uplo, m, n, bf,
TRUE, start, end
);
}
else // if dense or zeros
{
area = bli_thread_range_b2t
(
thr, a, bmult,
start, end
);
}
return area;
}
// -----------------------------------------------------------------------------
void bli_prime_factorization( dim_t n, bli_prime_factors_t* factors )
{
factors->n = n;
factors->sqrt_n = ( dim_t )sqrt( ( double )n );
factors->f = 2;
}
dim_t bli_next_prime_factor( bli_prime_factors_t* factors )
{
// Return the prime factorization of the original number n one-by-one.
// Return 1 after all factors have been exhausted.
// Looping over possible factors in increasing order assures we will
// only return prime factors (a la the Sieve of Eratosthenes).
while ( factors->f <= factors->sqrt_n )
{
// Special cases for factors 2-7 handle all numbers not divisible by 11
// or another larger prime. The slower loop version is used after that.
// If you use a number of threads with large prime factors you get
// what you deserve.
if ( factors->f == 2 )
{
if ( factors->n % 2 == 0 )
{
factors->n /= 2;
return 2;
}
factors->f = 3;
}
else if ( factors->f == 3 )
{
if ( factors->n % 3 == 0 )
{
factors->n /= 3;
return 3;
}
factors->f = 5;
}
else if ( factors->f == 5 )
{
if ( factors->n % 5 == 0 )
{
factors->n /= 5;
return 5;
}
factors->f = 7;
}
else if ( factors->f == 7 )
{
if ( factors->n % 7 == 0 )
{
factors->n /= 7;
return 7;
}
factors->f = 11;
}
else
{
if ( factors->n % factors->f == 0 )
{
factors->n /= factors->f;
return factors->f;
}
factors->f++;
}
}
// To get here we must be out of prime factors, leaving only n (if it is
// prime) or an endless string of 1s.
dim_t tmp = factors->n;
factors->n = 1;
return tmp;
}
bool bli_is_prime( dim_t n )
{
bli_prime_factors_t factors;
bli_prime_factorization( n, &factors );
dim_t f = bli_next_prime_factor( &factors );
if ( f == n ) return TRUE;
else return FALSE;
}
void bli_thread_partition_2x2
(
dim_t n_thread,
dim_t work1,
dim_t work2,
dim_t* restrict nt1,
dim_t* restrict nt2
)
{
// Partition a number of threads into two factors nt1 and nt2 such that
// nt1/nt2 ~= work1/work2. There is a fast heuristic algorithm and a
// slower optimal algorithm (which minimizes |nt1*work2 - nt2*work1|).
// Return early small prime numbers of threads.
if ( n_thread < 4 )
{
*nt1 = ( work1 >= work2 ? n_thread : 1 );
*nt2 = ( work1 < work2 ? n_thread : 1 );
return;
}
#if 1
bli_thread_partition_2x2_fast( n_thread, work1, work2, nt1, nt2 );
#else
bli_thread_partition_2x2_slow( n_thread, work1, work2, nt1, nt2 );
#endif
}
//#define PRINT_FACTORS
void bli_thread_partition_2x2_fast
(
dim_t n_thread,
dim_t work1,
dim_t work2,
dim_t* restrict nt1,
dim_t* restrict nt2
)
{
// Compute with these local variables until the end of the function, at
// which time we will save the values back to nt1 and nt2.
dim_t tn1 = 1;
dim_t tn2 = 1;
// Both algorithms need the prime factorization of n_thread.
bli_prime_factors_t factors;
bli_prime_factorization( n_thread, &factors );
// Fast algorithm: assign prime factors in increasing order to whichever
// partition has more work to do. The work is divided by the number of
// threads assigned at each iteration. This algorithm is sub-optimal in
// some cases. We attempt to mitigate the cases that involve at least one
// factor of 2. For example, in the partitioning of 12 with equal work
// this algorithm tentatively finds 6x2. This factorization involves a
// factor of 2 that can be reallocated, allowing us to convert it to the
// optimal solution of 4x3. But some cases cannot be corrected this way
// because they do not contain a factor of 2. For example, this algorithm
// factors 105 (with equal work) into 21x5 whereas 7x15 would be optimal.
#ifdef PRINT_FACTORS
printf( "w1 w2 = %d %d (initial)\n", (int)work1, (int)work2 );
#endif
dim_t f;
while ( ( f = bli_next_prime_factor( &factors ) ) > 1 )
{
#ifdef PRINT_FACTORS
printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d ... f = %d\n",
(int)work1, (int)work2, (int)tn1, (int)tn2, (int)f );
#endif
if ( work1 > work2 ) { work1 /= f; tn1 *= f; }
else { work2 /= f; tn2 *= f; }
}
#ifdef PRINT_FACTORS
printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d\n",
(int)work1, (int)work2, (int)tn1, (int)tn2 );
#endif
// Sometimes the last factor applied is prime. For example, on a square
// matrix, we tentatively arrive (from the logic above) at:
// - a 2x6 factorization when given 12 ways of parallelism
// - a 2x10 factorization when given 20 ways of parallelism
// - a 2x14 factorization when given 28 ways of parallelism
// These factorizations are suboptimal under the assumption that we want
// the parallelism to be as balanced as possible. Below, we make a final
// attempt at rebalancing nt1 and nt2 by checking to see if the gap between
// work1 and work2 is narrower if we reallocate a factor of 2.
if ( work1 > work2 )
{
// Example: nt = 12
// w1 w2 (initial) = 3600 3600; nt1 nt2 = 1 1
// w1 w2 (tentative) = 1800 600; nt1 nt2 = 2 6
// w1 w2 (ideal) = 900 1200; nt1 nt2 = 4 3
if ( tn2 % 2 == 0 )
{
dim_t diff = work1 - work2;
dim_t diff_mod = bli_abs( work1/2 - work2*2 );
if ( diff_mod < diff ) { tn1 *= 2; tn2 /= 2; }
}
}
else if ( work1 < work2 )
{
// Example: nt = 40
// w1 w2 (initial) = 3600 3600; nt1 nt2 = 1 1
// w1 w2 (tentative) = 360 900; nt1 nt2 = 10 4
// w1 w2 (ideal) = 720 450; nt1 nt2 = 5 8
if ( tn1 % 2 == 0 )
{
dim_t diff = work2 - work1;
dim_t diff_mod = bli_abs( work2/2 - work1*2 );
if ( diff_mod < diff ) { tn1 /= 2; tn2 *= 2; }
}
}
#ifdef PRINT_FACTORS
printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d (final)\n",
(int)work1, (int)work2, (int)tn1, (int)tn2 );
#endif
// Save the final result.
*nt1 = tn1;
*nt2 = tn2;
}
#include "limits.h"
void bli_thread_partition_2x2_slow
(
dim_t n_thread,
dim_t work1,
dim_t work2,
dim_t* restrict nt1,
dim_t* restrict nt2
)
{
// Slow algorithm: exhaustively constructs all factor pairs of n_thread and
// chooses the best one.
// Compute with these local variables until the end of the function, at
// which time we will save the values back to nt1 and nt2.
dim_t tn1 = 1;
dim_t tn2 = 1;
// Both algorithms need the prime factorization of n_thread.
bli_prime_factors_t factors;
bli_prime_factorization( n_thread, &factors );
// Eight prime factors handles n_thread up to 223092870.
dim_t fact[8];
dim_t mult[8];
// There is always at least one prime factor, so use if for initialization.
dim_t nfact = 1;
fact[0] = bli_next_prime_factor( &factors );
mult[0] = 1;
// Collect the remaining prime factors, accounting for multiplicity of
// repeated factors.
dim_t f;
while ( ( f = bli_next_prime_factor( &factors ) ) > 1 )
{
if ( f == fact[nfact-1] )
{
mult[nfact-1]++;
}
else
{
nfact++;
fact[nfact-1] = f;
mult[nfact-1] = 1;
}
}
// Now loop over all factor pairs. A single factor pair is denoted by how
// many of each prime factor are included in the first factor (ntaken).
dim_t ntake[8] = {0};
dim_t min_diff = INT_MAX;
// Loop over how many prime factors to assign to the first factor in the
// pair, for each prime factor. The total number of iterations is
// \Prod_{i=0}^{nfact-1} mult[i].
bool done = FALSE;
while ( !done )
{
dim_t x = 1;
dim_t y = 1;
// Form the factors by integer exponentiation and accumulation.
for ( dim_t i = 0 ; i < nfact ; i++ )
{
x *= bli_ipow( fact[i], ntake[i] );
y *= bli_ipow( fact[i], mult[i]-ntake[i] );
}
// Check if this factor pair is optimal by checking
// |nt1*work2 - nt2*work1|.
dim_t diff = llabs( x*work2 - y*work1 );
if ( diff < min_diff )
{
min_diff = diff;
tn1 = x;
tn2 = y;
}
// Go to the next factor pair by doing an "odometer loop".
for ( dim_t i = 0 ; i < nfact ; i++ )
{
if ( ++ntake[i] > mult[i] )
{
ntake[i] = 0;
if ( i == nfact-1 ) done = TRUE;
else continue;
}
break;
}
}
// Save the final result.
*nt1 = tn1;
*nt2 = tn2;
}
#if 0
void bli_thread_partition_2x2_orig
(
dim_t n_thread,
dim_t work1,
dim_t work2,
dim_t* restrict nt1,
dim_t* restrict nt2
)
{
// Copy nt1 and nt2 to local variables and then compute with those local
// variables until the end of the function, at which time we will save the
// values back to nt1 and nt2.
dim_t tn1; // = *nt1;
dim_t tn2; // = *nt2;
// Partition a number of threads into two factors nt1 and nt2 such that
// nt1/nt2 ~= work1/work2. There is a fast heuristic algorithm and a
// slower optimal algorithm (which minimizes |nt1*work2 - nt2*work1|).
// Return early small prime numbers of threads.
if ( n_thread < 4 )
{
tn1 = ( work1 >= work2 ? n_thread : 1 );
tn2 = ( work1 < work2 ? n_thread : 1 );
return;
}
tn1 = 1;
tn2 = 1;
// Both algorithms need the prime factorization of n_thread.
bli_prime_factors_t factors;
bli_prime_factorization( n_thread, &factors );
#if 1
// Fast algorithm: assign prime factors in increasing order to whichever
// partition has more work to do. The work is divided by the number of
// threads assigned at each iteration. This algorithm is sub-optimal in
// some cases. We attempt to mitigate the cases that involve at least one
// factor of 2. For example, in the partitioning of 12 with equal work
// this algorithm tentatively finds 6x2. This factorization involves a
// factor of 2 that can be reallocated, allowing us to convert it to the
// optimal solution of 4x3. But some cases cannot be corrected this way
// because they do not contain a factor of 2. For example, this algorithm
// factors 105 (with equal work) into 21x5 whereas 7x15 would be optimal.
//printf( "w1 w2 = %d %d (initial)\n", (int)work1, (int)work2 );
dim_t f;
while ( ( f = bli_next_prime_factor( &factors ) ) > 1 )
{
//printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d ... f = %d\n", (int)work1, (int)work2, (int)tn1, (int)tn2, (int)f );
if ( work1 > work2 )
{
work1 /= f;
tn1 *= f;
}
else
{
work2 /= f;
tn2 *= f;
}
}
//printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d\n", (int)work1, (int)work2, (int)tn1, (int)tn2 );
// Sometimes the last factor applied is prime. For example, on a square
// matrix, we tentatively arrive (from the logic above) at:
// - a 2x6 factorization when given 12 ways of parallelism
// - a 2x10 factorization when given 20 ways of parallelism
// - a 2x14 factorization when given 28 ways of parallelism
// These factorizations are suboptimal under the assumption that we want
// the parallelism to be as balanced as possible. Below, we make a final
// attempt at rebalancing nt1 and nt2 by checking to see if the gap between
// work1 and work2 is narrower if we reallocate a factor of 2.
if ( work1 > work2 )
{
// Example: nt = 12
// w1 w2 (initial) = 3600 3600; nt1 nt2 = 1 1
// w1 w2 (tentative) = 1800 600; nt1 nt2 = 2 6
// w1 w2 (ideal) = 900 1200; nt1 nt2 = 4 3
if ( tn2 % 2 == 0 )
{
dim_t diff = work1 - work2;
dim_t diff_mod = bli_abs( work1/2 - work2*2 );
if ( diff_mod < diff ) { tn1 *= 2; tn2 /= 2; }
}
}
else if ( work1 < work2 )
{
// Example: nt = 40
// w1 w2 (initial) = 3600 3600; nt1 nt2 = 1 1
// w1 w2 (tentative) = 360 900; nt1 nt2 = 10 4
// w1 w2 (ideal) = 720 450; nt1 nt2 = 5 8
if ( tn1 % 2 == 0 )
{
dim_t diff = work2 - work1;
dim_t diff_mod = bli_abs( work2/2 - work1*2 );
if ( diff_mod < diff ) { tn1 /= 2; tn2 *= 2; }
}
}
//printf( "w1 w2 = %4d %4d nt1 nt2 = %d %d (final)\n", (int)work1, (int)work2, (int)tn1, (int)tn2 );
#else
// Slow algorithm: exhaustively constructs all factor pairs of n_thread and
// chooses the best one.
// Eight prime factors handles n_thread up to 223092870.
dim_t fact[8];
dim_t mult[8];
// There is always at least one prime factor, so use if for initialization.
dim_t nfact = 1;
fact[0] = bli_next_prime_factor( &factors );
mult[0] = 1;
// Collect the remaining prime factors, accounting for multiplicity of
// repeated factors.
dim_t f;
while ( ( f = bli_next_prime_factor( &factors ) ) > 1 )
{
if ( f == fact[nfact-1] )
{
mult[nfact-1]++;
}
else
{
nfact++;
fact[nfact-1] = f;
mult[nfact-1] = 1;
}
}
// Now loop over all factor pairs. A single factor pair is denoted by how
// many of each prime factor are included in the first factor (ntaken).
dim_t ntake[8] = {0};
dim_t min_diff = INT_MAX;
// Loop over how many prime factors to assign to the first factor in the
// pair, for each prime factor. The total number of iterations is
// \Prod_{i=0}^{nfact-1} mult[i].
bool done = FALSE;
while ( !done )
{
dim_t x = 1;
dim_t y = 1;
// Form the factors by integer exponentiation and accumulation.
for (dim_t i = 0 ; i < nfact ; i++ )
{
x *= bli_ipow( fact[i], ntake[i] );
y *= bli_ipow( fact[i], mult[i]-ntake[i] );
}
// Check if this factor pair is optimal by checking
// |nt1*work2 - nt2*work1|.
dim_t diff = llabs( x*work2 - y*work1 );
if ( diff < min_diff )
{
min_diff = diff;
tn1 = x;
tn2 = y;
}
// Go to the next factor pair by doing an "odometer loop".
for ( dim_t i = 0 ; i < nfact ; i++ )
{
if ( ++ntake[i] > mult[i] )
{
ntake[i] = 0;
if ( i == nfact-1 ) done = TRUE;
else continue;
}
break;
}
}
#endif
// Save the final result.
*nt1 = tn1;
*nt2 = tn2;
}
#endif
// -----------------------------------------------------------------------------
dim_t bli_gcd( dim_t x, dim_t y )
{
while ( y != 0 )
{
dim_t t = y;
y = x % y;
x = t;
}
return x;
}
dim_t bli_lcm( dim_t x, dim_t y)
{
return x * y / bli_gcd( x, y );
}
dim_t bli_ipow( dim_t base, dim_t power )
{
dim_t p = 1;
for ( dim_t mask = 0x1 ; mask <= power ; mask <<= 1 )
{
if ( power & mask ) p *= base;
base *= base;
}
return p;
}
// -----------------------------------------------------------------------------
dim_t bli_thread_get_jc_nt( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_jc_ways( &global_rntm );
}
dim_t bli_thread_get_pc_nt( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_pc_ways( &global_rntm );
}
dim_t bli_thread_get_ic_nt( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_ic_ways( &global_rntm );
}
dim_t bli_thread_get_jr_nt( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_jr_ways( &global_rntm );
}
dim_t bli_thread_get_ir_nt( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_ir_ways( &global_rntm );
}
dim_t bli_thread_get_num_threads( void )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
return bli_rntm_num_threads( &global_rntm );
}
// ----------------------------------------------------------------------------
void bli_thread_set_ways( dim_t jc, dim_t pc, dim_t ic, dim_t jr, dim_t ir )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
// Acquire the mutex protecting global_rntm.
bli_pthread_mutex_lock( &global_rntm_mutex );
bli_rntm_set_ways_only( jc, pc, ic, jr, ir, &global_rntm );
// Release the mutex protecting global_rntm.
bli_pthread_mutex_unlock( &global_rntm_mutex );
}
void bli_thread_set_num_threads( dim_t n_threads )
{
// We must ensure that global_rntm has been initialized.
bli_init_once();
// Acquire the mutex protecting global_rntm.
bli_pthread_mutex_lock( &global_rntm_mutex );
bli_rntm_set_num_threads_only( n_threads, &global_rntm );
// Release the mutex protecting global_rntm.
bli_pthread_mutex_unlock( &global_rntm_mutex );
}
// ----------------------------------------------------------------------------
void bli_thread_init_rntm_from_env
(
rntm_t* rntm
)
{
// NOTE: We don't need to acquire the global_rntm_mutex here because this
// function is only called from bli_thread_init(), which is only called
// by bli_init_once().
bool auto_factor = FALSE;
dim_t nt;
dim_t jc, pc, ic, jr, ir;
#ifdef BLIS_ENABLE_MULTITHREADING
// Try to read BLIS_NUM_THREADS first.
nt = bli_env_get_var( "BLIS_NUM_THREADS", -1 );
// If BLIS_NUM_THREADS was not set, try to read OMP_NUM_THREADS.
if ( nt == -1 )
nt = bli_env_get_var( "OMP_NUM_THREADS", -1 );
// Read the environment variables for the number of threads (ways
// of parallelism) for each individual loop.
jc = bli_env_get_var( "BLIS_JC_NT", -1 );
pc = bli_env_get_var( "BLIS_PC_NT", -1 );
ic = bli_env_get_var( "BLIS_IC_NT", -1 );
jr = bli_env_get_var( "BLIS_JR_NT", -1 );
ir = bli_env_get_var( "BLIS_IR_NT", -1 );
// If any BLIS_*_NT environment variable was set, then we ignore the
// value of BLIS_NUM_THREADS or OMP_NUM_THREADS and use the
// BLIS_*_NT values instead (with unset variables being treated as if
// they contained 1).
if ( jc != -1 || pc != -1 || ic != -1 || jr != -1 || ir != -1 )
{
if ( jc == -1 ) jc = 1;
if ( pc == -1 ) pc = 1;
if ( ic == -1 ) ic = 1;
if ( jr == -1 ) jr = 1;
if ( ir == -1 ) ir = 1;
// Unset the value for nt.
nt = -1;
}
// By this time, one of the following conditions holds:
// - nt is -1 and the ways for each loop are -1.
// - nt is -1 and the ways for each loop are all set.
// - nt is set and the ways for each loop are -1.
// If nt is set (ie: not -1), then we know we will perform an automatic
// thread factorization (later, in bli_rntm.c).
if ( nt != -1 ) auto_factor = TRUE;
#else
// When multithreading is disabled, always set the rntm_t ways
// values to 1.
nt = -1;
jc = pc = ic = jr = ir = 1;
#endif
// Save the results back in the runtime object.
bli_rntm_set_auto_factor_only( auto_factor, rntm );
bli_rntm_set_num_threads_only( nt, rntm );
bli_rntm_set_ways_only( jc, pc, ic, jr, ir, rntm );
#if 0
printf( "bli_thread_init_rntm_from_env()\n" );
bli_rntm_print( rntm );
#endif
}
|