kmp_gsupport.cpp 92.8 KB
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 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
/*
 * kmp_gsupport.cpp
 */

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "kmp.h"
#include "kmp_atomic.h"

#if OMPT_SUPPORT
#include "ompt-specific.h"
#endif

enum {
  KMP_GOMP_TASK_UNTIED_FLAG = 1,
  KMP_GOMP_TASK_FINAL_FLAG = 2,
  KMP_GOMP_TASK_DEPENDS_FLAG = 8
};

// This class helps convert gomp dependency info into
// kmp_depend_info_t structures
class kmp_gomp_depends_info_t {
  void **depend;
  kmp_int32 num_deps;
  size_t num_out, num_mutexinout, num_in;
  size_t offset;

public:
  kmp_gomp_depends_info_t(void **depend) : depend(depend) {
    size_t ndeps = (kmp_intptr_t)depend[0];
    size_t num_doable;
    // GOMP taskdep structure:
    // if depend[0] != 0:
    // depend =  [ ndeps | nout | &out | ... | &out | &in | ... | &in ]
    //
    // if depend[0] == 0:
    // depend = [ 0 | ndeps | nout | nmtx | nin | &out | ... | &out | &mtx |
    //            ... | &mtx | &in   | ...  | &in  | &depobj | ... | &depobj ]
    if (ndeps) {
      num_out = (kmp_intptr_t)depend[1];
      num_in = ndeps - num_out;
      num_mutexinout = 0;
      num_doable = ndeps;
      offset = 2;
    } else {
      ndeps = (kmp_intptr_t)depend[1];
      num_out = (kmp_intptr_t)depend[2];
      num_mutexinout = (kmp_intptr_t)depend[3];
      num_in = (kmp_intptr_t)depend[4];
      num_doable = num_out + num_mutexinout + num_in;
      offset = 5;
    }
    // TODO: Support gomp depobj
    if (ndeps != num_doable) {
      KMP_FATAL(GompFeatureNotSupported, "depobj");
    }
    num_deps = static_cast<kmp_int32>(ndeps);
  }
  kmp_int32 get_num_deps() const { return num_deps; }
  kmp_depend_info_t get_kmp_depend(size_t index) const {
    kmp_depend_info_t retval;
    memset(&retval, '\0', sizeof(retval));
    KMP_ASSERT(index < (size_t)num_deps);
    retval.base_addr = (kmp_intptr_t)depend[offset + index];
    retval.len = 0;
    // Because inout and out are logically equivalent,
    // use inout and in dependency flags. GOMP does not provide a
    // way to distinguish if user specified out vs. inout.
    if (index < num_out) {
      retval.flags.in = 1;
      retval.flags.out = 1;
    } else if (index >= num_out && index < (num_out + num_mutexinout)) {
      retval.flags.mtx = 1;
    } else {
      retval.flags.in = 1;
    }
    return retval;
  }
};

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#define MKLOC(loc, routine)                                                    \
  static ident_t loc = {0, KMP_IDENT_KMPC, 0, 0, ";unknown;unknown;0;0;;"};

#include "kmp_ftn_os.h"

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_BARRIER)(void) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_barrier");
  KA_TRACE(20, ("GOMP_barrier: T#%d\n", gtid));
#if OMPT_SUPPORT && OMPT_OPTIONAL
  ompt_frame_t *ompt_frame;
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmpc_barrier(&loc, gtid);
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    ompt_frame->enter_frame = ompt_data_none;
  }
#endif
}

// Mutual exclusion

// The symbol that icc/ifort generates for unnamed for unnamed critical sections
// - .gomp_critical_user_ - is defined using .comm in any objects reference it.
// We can't reference it directly here in C code, as the symbol contains a ".".
//
// The RTL contains an assembly language definition of .gomp_critical_user_
// with another symbol __kmp_unnamed_critical_addr initialized with it's
// address.
extern kmp_critical_name *__kmp_unnamed_critical_addr;

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_START)(void) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_critical_start");
  KA_TRACE(20, ("GOMP_critical_start: T#%d\n", gtid));
#if OMPT_SUPPORT && OMPT_OPTIONAL
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmpc_critical(&loc, gtid, __kmp_unnamed_critical_addr);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_END)(void) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_critical_end");
  KA_TRACE(20, ("GOMP_critical_end: T#%d\n", gtid));
#if OMPT_SUPPORT && OMPT_OPTIONAL
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmpc_end_critical(&loc, gtid, __kmp_unnamed_critical_addr);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_NAME_START)(void **pptr) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_critical_name_start");
  KA_TRACE(20, ("GOMP_critical_name_start: T#%d\n", gtid));
  __kmpc_critical(&loc, gtid, (kmp_critical_name *)pptr);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_NAME_END)(void **pptr) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_critical_name_end");
  KA_TRACE(20, ("GOMP_critical_name_end: T#%d\n", gtid));
  __kmpc_end_critical(&loc, gtid, (kmp_critical_name *)pptr);
}

// The Gnu codegen tries to use locked operations to perform atomic updates
// inline.  If it can't, then it calls GOMP_atomic_start() before performing
// the update and GOMP_atomic_end() afterward, regardless of the data type.
void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ATOMIC_START)(void) {
  int gtid = __kmp_entry_gtid();
  KA_TRACE(20, ("GOMP_atomic_start: T#%d\n", gtid));

#if OMPT_SUPPORT
  __ompt_thread_assign_wait_id(0);
#endif

  __kmp_acquire_atomic_lock(&__kmp_atomic_lock, gtid);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ATOMIC_END)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_atomic_end: T#%d\n", gtid));
  __kmp_release_atomic_lock(&__kmp_atomic_lock, gtid);
}

int KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_START)(void) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_single_start");
  KA_TRACE(20, ("GOMP_single_start: T#%d\n", gtid));

  if (!TCR_4(__kmp_init_parallel))
    __kmp_parallel_initialize();
  __kmp_resume_if_soft_paused();

  // 3rd parameter == FALSE prevents kmp_enter_single from pushing a
  // workshare when USE_CHECKS is defined.  We need to avoid the push,
  // as there is no corresponding GOMP_single_end() call.
  kmp_int32 rc = __kmp_enter_single(gtid, &loc, FALSE);

#if OMPT_SUPPORT && OMPT_OPTIONAL
  kmp_info_t *this_thr = __kmp_threads[gtid];
  kmp_team_t *team = this_thr->th.th_team;
  int tid = __kmp_tid_from_gtid(gtid);

  if (ompt_enabled.enabled) {
    if (rc) {
      if (ompt_enabled.ompt_callback_work) {
        ompt_callbacks.ompt_callback(ompt_callback_work)(
            ompt_work_single_executor, ompt_scope_begin,
            &(team->t.ompt_team_info.parallel_data),
            &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
            1, OMPT_GET_RETURN_ADDRESS(0));
      }
    } else {
      if (ompt_enabled.ompt_callback_work) {
        ompt_callbacks.ompt_callback(ompt_callback_work)(
            ompt_work_single_other, ompt_scope_begin,
            &(team->t.ompt_team_info.parallel_data),
            &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
            1, OMPT_GET_RETURN_ADDRESS(0));
        ompt_callbacks.ompt_callback(ompt_callback_work)(
            ompt_work_single_other, ompt_scope_end,
            &(team->t.ompt_team_info.parallel_data),
            &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
            1, OMPT_GET_RETURN_ADDRESS(0));
      }
    }
  }
#endif

  return rc;
}

void *KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_COPY_START)(void) {
  void *retval;
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_single_copy_start");
  KA_TRACE(20, ("GOMP_single_copy_start: T#%d\n", gtid));

  if (!TCR_4(__kmp_init_parallel))
    __kmp_parallel_initialize();
  __kmp_resume_if_soft_paused();

  // If this is the first thread to enter, return NULL.  The generated code will
  // then call GOMP_single_copy_end() for this thread only, with the
  // copyprivate data pointer as an argument.
  if (__kmp_enter_single(gtid, &loc, FALSE))
    return NULL;

// Wait for the first thread to set the copyprivate data pointer,
// and for all other threads to reach this point.

#if OMPT_SUPPORT && OMPT_OPTIONAL
  ompt_frame_t *ompt_frame;
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);

  // Retrieve the value of the copyprivate data point, and wait for all
  // threads to do likewise, then return.
  retval = __kmp_team_from_gtid(gtid)->t.t_copypriv_data;
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    ompt_frame->enter_frame = ompt_data_none;
  }
#endif
  return retval;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_COPY_END)(void *data) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_single_copy_end: T#%d\n", gtid));

  // Set the copyprivate data pointer fo the team, then hit the barrier so that
  // the other threads will continue on and read it.  Hit another barrier before
  // continuing, so that the know that the copyprivate data pointer has been
  // propagated to all threads before trying to reuse the t_copypriv_data field.
  __kmp_team_from_gtid(gtid)->t.t_copypriv_data = data;
#if OMPT_SUPPORT && OMPT_OPTIONAL
  ompt_frame_t *ompt_frame;
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    ompt_frame->enter_frame = ompt_data_none;
  }
#endif
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ORDERED_START)(void) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_ordered_start");
  KA_TRACE(20, ("GOMP_ordered_start: T#%d\n", gtid));
#if OMPT_SUPPORT && OMPT_OPTIONAL
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmpc_ordered(&loc, gtid);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ORDERED_END)(void) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_ordered_end");
  KA_TRACE(20, ("GOMP_ordered_start: T#%d\n", gtid));
#if OMPT_SUPPORT && OMPT_OPTIONAL
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmpc_end_ordered(&loc, gtid);
}

// Dispatch macro defs
//
// They come in two flavors: 64-bit unsigned, and either 32-bit signed
// (IA-32 architecture) or 64-bit signed (Intel(R) 64).

#if KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_MIPS
#define KMP_DISPATCH_INIT __kmp_aux_dispatch_init_4
#define KMP_DISPATCH_FINI_CHUNK __kmp_aux_dispatch_fini_chunk_4
#define KMP_DISPATCH_NEXT __kmpc_dispatch_next_4
#else
#define KMP_DISPATCH_INIT __kmp_aux_dispatch_init_8
#define KMP_DISPATCH_FINI_CHUNK __kmp_aux_dispatch_fini_chunk_8
#define KMP_DISPATCH_NEXT __kmpc_dispatch_next_8
#endif /* KMP_ARCH_X86 */

#define KMP_DISPATCH_INIT_ULL __kmp_aux_dispatch_init_8u
#define KMP_DISPATCH_FINI_CHUNK_ULL __kmp_aux_dispatch_fini_chunk_8u
#define KMP_DISPATCH_NEXT_ULL __kmpc_dispatch_next_8u

// The parallel construct

#ifndef KMP_DEBUG
static
#endif /* KMP_DEBUG */
    void
    __kmp_GOMP_microtask_wrapper(int *gtid, int *npr, void (*task)(void *),
                                 void *data) {
#if OMPT_SUPPORT
  kmp_info_t *thr;
  ompt_frame_t *ompt_frame;
  ompt_state_t enclosing_state;

  if (ompt_enabled.enabled) {
    // get pointer to thread data structure
    thr = __kmp_threads[*gtid];

    // save enclosing task state; set current state for task
    enclosing_state = thr->th.ompt_thread_info.state;
    thr->th.ompt_thread_info.state = ompt_state_work_parallel;

    // set task frame
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  }
#endif

  task(data);

#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    // clear task frame
    ompt_frame->exit_frame = ompt_data_none;

    // restore enclosing state
    thr->th.ompt_thread_info.state = enclosing_state;
  }
#endif
}

#ifndef KMP_DEBUG
static
#endif /* KMP_DEBUG */
    void
    __kmp_GOMP_parallel_microtask_wrapper(int *gtid, int *npr,
                                          void (*task)(void *), void *data,
                                          unsigned num_threads, ident_t *loc,
                                          enum sched_type schedule, long start,
                                          long end, long incr,
                                          long chunk_size) {
  // Initialize the loop worksharing construct.

  KMP_DISPATCH_INIT(loc, *gtid, schedule, start, end, incr, chunk_size,
                    schedule != kmp_sch_static);

#if OMPT_SUPPORT
  kmp_info_t *thr;
  ompt_frame_t *ompt_frame;
  ompt_state_t enclosing_state;

  if (ompt_enabled.enabled) {
    thr = __kmp_threads[*gtid];
    // save enclosing task state; set current state for task
    enclosing_state = thr->th.ompt_thread_info.state;
    thr->th.ompt_thread_info.state = ompt_state_work_parallel;

    // set task frame
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  }
#endif

  // Now invoke the microtask.
  task(data);

#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    // clear task frame
    ompt_frame->exit_frame = ompt_data_none;

    // reset enclosing state
    thr->th.ompt_thread_info.state = enclosing_state;
  }
#endif
}

static void __kmp_GOMP_fork_call(ident_t *loc, int gtid, unsigned num_threads,
                                 unsigned flags, void (*unwrapped_task)(void *),
                                 microtask_t wrapper, int argc, ...) {
  int rc;
  kmp_info_t *thr = __kmp_threads[gtid];
  kmp_team_t *team = thr->th.th_team;
  int tid = __kmp_tid_from_gtid(gtid);

  va_list ap;
  va_start(ap, argc);

  if (num_threads != 0)
    __kmp_push_num_threads(loc, gtid, num_threads);
  if (flags != 0)
    __kmp_push_proc_bind(loc, gtid, (kmp_proc_bind_t)flags);
  rc = __kmp_fork_call(loc, gtid, fork_context_gnu, argc, wrapper,
                       __kmp_invoke_task_func, kmp_va_addr_of(ap));

  va_end(ap);

  if (rc) {
    __kmp_run_before_invoked_task(gtid, tid, thr, team);
  }

#if OMPT_SUPPORT
  int ompt_team_size;
  if (ompt_enabled.enabled) {
    ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
    ompt_task_info_t *task_info = __ompt_get_task_info_object(0);

    // implicit task callback
    if (ompt_enabled.ompt_callback_implicit_task) {
      ompt_team_size = __kmp_team_from_gtid(gtid)->t.t_nproc;
      ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
          ompt_scope_begin, &(team_info->parallel_data),
          &(task_info->task_data), ompt_team_size, __kmp_tid_from_gtid(gtid), ompt_task_implicit); // TODO: Can this be ompt_task_initial?
      task_info->thread_num = __kmp_tid_from_gtid(gtid);
    }
    thr->th.ompt_thread_info.state = ompt_state_work_parallel;
  }
#endif
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_START)(void (*task)(void *),
                                                       void *data,
                                                       unsigned num_threads) {
  int gtid = __kmp_entry_gtid();

#if OMPT_SUPPORT
  ompt_frame_t *parent_frame, *frame;

  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);
    parent_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif

  MKLOC(loc, "GOMP_parallel_start");
  KA_TRACE(20, ("GOMP_parallel_start: T#%d\n", gtid));
  __kmp_GOMP_fork_call(&loc, gtid, num_threads, 0u, task,
                       (microtask_t)__kmp_GOMP_microtask_wrapper, 2, task,
                       data);
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &frame, NULL, NULL);
    frame->exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  }
#endif
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)(void) {
  int gtid = __kmp_get_gtid();
  kmp_info_t *thr;

  thr = __kmp_threads[gtid];

  MKLOC(loc, "GOMP_parallel_end");
  KA_TRACE(20, ("GOMP_parallel_end: T#%d\n", gtid));

  if (!thr->th.th_team->t.t_serialized) {
    __kmp_run_after_invoked_task(gtid, __kmp_tid_from_gtid(gtid), thr,
                                 thr->th.th_team);
  }
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    // Implicit task is finished here, in the barrier we might schedule
    // deferred tasks,
    // these don't see the implicit task on the stack
    OMPT_CUR_TASK_INFO(thr)->frame.exit_frame = ompt_data_none;
  }
#endif

  __kmp_join_call(&loc, gtid
#if OMPT_SUPPORT
                  ,
                  fork_context_gnu
#endif
                  );
}

// Loop worksharing constructs

// The Gnu codegen passes in an exclusive upper bound for the overall range,
// but the libguide dispatch code expects an inclusive upper bound, hence the
// "end - incr" 5th argument to KMP_DISPATCH_INIT (and the " ub - str" 11th
// argument to __kmp_GOMP_fork_call).
//
// Conversely, KMP_DISPATCH_NEXT returns and inclusive upper bound in *p_ub,
// but the Gnu codegen expects an exclusive upper bound, so the adjustment
// "*p_ub += stride" compensates for the discrepancy.
//
// Correction: the gnu codegen always adjusts the upper bound by +-1, not the
// stride value.  We adjust the dispatch parameters accordingly (by +-1), but
// we still adjust p_ub by the actual stride value.
//
// The "runtime" versions do not take a chunk_sz parameter.
//
// The profile lib cannot support construct checking of unordered loops that
// are predetermined by the compiler to be statically scheduled, as the gcc
// codegen will not always emit calls to GOMP_loop_static_next() to get the
// next iteration.  Instead, it emits inline code to call omp_get_thread_num()
// num and calculate the iteration space using the result.  It doesn't do this
// with ordered static loop, so they can be checked.

#if OMPT_SUPPORT
#define IF_OMPT_SUPPORT(code) code
#else
#define IF_OMPT_SUPPORT(code)
#endif

#define LOOP_START(func, schedule)                                             \
  int func(long lb, long ub, long str, long chunk_sz, long *p_lb,              \
           long *p_ub) {                                                       \
    int status;                                                                \
    long stride;                                                               \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n",  \
         gtid, lb, ub, str, chunk_sz));                                        \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
      KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
                        (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,        \
                        (schedule) != kmp_sch_static);                         \
      IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
      status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
                                 (kmp_int *)p_ub, (kmp_int *)&stride);         \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",    \
         gtid, *p_lb, *p_ub, status));                                         \
    return status;                                                             \
  }

#define LOOP_RUNTIME_START(func, schedule)                                     \
  int func(long lb, long ub, long str, long *p_lb, long *p_ub) {               \
    int status;                                                                \
    long stride;                                                               \
    long chunk_sz = 0;                                                         \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz %d\n", \
         gtid, lb, ub, str, chunk_sz));                                        \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
      KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
                        (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz, TRUE); \
      IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
      status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
                                 (kmp_int *)p_ub, (kmp_int *)&stride);         \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",    \
         gtid, *p_lb, *p_ub, status));                                         \
    return status;                                                             \
  }

#define KMP_DOACROSS_FINI(status, gtid)                                        \
  if (!status && __kmp_threads[gtid]->th.th_dispatch->th_doacross_flags) {     \
    __kmpc_doacross_fini(NULL, gtid);                                          \
  }

#define LOOP_NEXT(func, fini_code)                                             \
  int func(long *p_lb, long *p_ub) {                                           \
    int status;                                                                \
    long stride;                                                               \
    int gtid = __kmp_get_gtid();                                               \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(20, (KMP_STR(func) ": T#%d\n", gtid));                            \
                                                                               \
    IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                          \
    fini_code status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,    \
                                         (kmp_int *)p_ub, (kmp_int *)&stride); \
    if (status) {                                                              \
      *p_ub += (stride > 0) ? 1 : -1;                                          \
    }                                                                          \
    KMP_DOACROSS_FINI(status, gtid)                                            \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, stride 0x%lx, " \
                       "returning %d\n",                                       \
         gtid, *p_lb, *p_ub, stride, status));                                 \
    return status;                                                             \
  }

LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_START), kmp_sch_static)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_NEXT), {})
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_START),
           kmp_sch_dynamic_chunked)
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_DYNAMIC_START),
           kmp_sch_dynamic_chunked)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_NEXT), {})
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_DYNAMIC_NEXT), {})
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_START),
           kmp_sch_guided_chunked)
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_GUIDED_START),
           kmp_sch_guided_chunked)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_NEXT), {})
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_GUIDED_NEXT), {})
LOOP_RUNTIME_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_START),
                   kmp_sch_runtime)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_NEXT), {})
LOOP_RUNTIME_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_MAYBE_NONMONOTONIC_RUNTIME_START),
    kmp_sch_runtime)
LOOP_RUNTIME_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_RUNTIME_START),
    kmp_sch_runtime)
LOOP_NEXT(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_MAYBE_NONMONOTONIC_RUNTIME_NEXT), {})
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_RUNTIME_NEXT), {})

LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_START),
           kmp_ord_static)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_NEXT),
          { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_START),
           kmp_ord_dynamic_chunked)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_NEXT),
          { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_START),
           kmp_ord_guided_chunked)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_NEXT),
          { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
LOOP_RUNTIME_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_START),
    kmp_ord_runtime)
LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_NEXT),
          { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })

#define LOOP_DOACROSS_START(func, schedule)                                    \
  bool func(unsigned ncounts, long *counts, long chunk_sz, long *p_lb,         \
            long *p_ub) {                                                      \
    int status;                                                                \
    long stride, lb, ub, str;                                                  \
    int gtid = __kmp_entry_gtid();                                             \
    struct kmp_dim *dims =                                                     \
        (struct kmp_dim *)__kmp_allocate(sizeof(struct kmp_dim) * ncounts);    \
    MKLOC(loc, KMP_STR(func));                                                 \
    for (unsigned i = 0; i < ncounts; ++i) {                                   \
      dims[i].lo = 0;                                                          \
      dims[i].up = counts[i] - 1;                                              \
      dims[i].st = 1;                                                          \
    }                                                                          \
    __kmpc_doacross_init(&loc, gtid, (int)ncounts, dims);                      \
    lb = 0;                                                                    \
    ub = counts[0];                                                            \
    str = 1;                                                                   \
    KA_TRACE(20, (KMP_STR(func) ": T#%d, ncounts %u, lb 0x%lx, ub 0x%lx, str " \
                                "0x%lx, chunk_sz "                             \
                                "0x%lx\n",                                     \
                  gtid, ncounts, lb, ub, str, chunk_sz));                      \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
                        (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,        \
                        (schedule) != kmp_sch_static);                         \
      status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
                                 (kmp_int *)p_ub, (kmp_int *)&stride);         \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
    KMP_DOACROSS_FINI(status, gtid);                                           \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",    \
         gtid, *p_lb, *p_ub, status));                                         \
    __kmp_free(dims);                                                          \
    return status;                                                             \
  }

#define LOOP_DOACROSS_RUNTIME_START(func, schedule)                            \
  int func(unsigned ncounts, long *counts, long *p_lb, long *p_ub) {           \
    int status;                                                                \
    long stride, lb, ub, str;                                                  \
    long chunk_sz = 0;                                                         \
    int gtid = __kmp_entry_gtid();                                             \
    struct kmp_dim *dims =                                                     \
        (struct kmp_dim *)__kmp_allocate(sizeof(struct kmp_dim) * ncounts);    \
    MKLOC(loc, KMP_STR(func));                                                 \
    for (unsigned i = 0; i < ncounts; ++i) {                                   \
      dims[i].lo = 0;                                                          \
      dims[i].up = counts[i] - 1;                                              \
      dims[i].st = 1;                                                          \
    }                                                                          \
    __kmpc_doacross_init(&loc, gtid, (int)ncounts, dims);                      \
    lb = 0;                                                                    \
    ub = counts[0];                                                            \
    str = 1;                                                                   \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz %d\n", \
         gtid, lb, ub, str, chunk_sz));                                        \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
                        (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz, TRUE); \
      status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
                                 (kmp_int *)p_ub, (kmp_int *)&stride);         \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
    KMP_DOACROSS_FINI(status, gtid);                                           \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",    \
         gtid, *p_lb, *p_ub, status));                                         \
    __kmp_free(dims);                                                          \
    return status;                                                             \
  }

LOOP_DOACROSS_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DOACROSS_STATIC_START),
    kmp_sch_static)
LOOP_DOACROSS_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DOACROSS_DYNAMIC_START),
    kmp_sch_dynamic_chunked)
LOOP_DOACROSS_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DOACROSS_GUIDED_START),
    kmp_sch_guided_chunked)
LOOP_DOACROSS_RUNTIME_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DOACROSS_RUNTIME_START),
    kmp_sch_runtime)

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_loop_end: T#%d\n", gtid))

#if OMPT_SUPPORT && OMPT_OPTIONAL
  ompt_frame_t *ompt_frame;
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
#if OMPT_SUPPORT && OMPT_OPTIONAL
  if (ompt_enabled.enabled) {
    ompt_frame->enter_frame = ompt_data_none;
  }
#endif

  KA_TRACE(20, ("GOMP_loop_end exit: T#%d\n", gtid))
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END_NOWAIT)(void) {
  KA_TRACE(20, ("GOMP_loop_end_nowait: T#%d\n", __kmp_get_gtid()))
}

// Unsigned long long loop worksharing constructs
//
// These are new with gcc 4.4

#define LOOP_START_ULL(func, schedule)                                         \
  int func(int up, unsigned long long lb, unsigned long long ub,               \
           unsigned long long str, unsigned long long chunk_sz,                \
           unsigned long long *p_lb, unsigned long long *p_ub) {               \
    int status;                                                                \
    long long str2 = up ? ((long long)str) : -((long long)str);                \
    long long stride;                                                          \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
                                                                               \
    KA_TRACE(20, (KMP_STR(func) ": T#%d, up %d, lb 0x%llx, ub 0x%llx, str "    \
                                "0x%llx, chunk_sz 0x%llx\n",                   \
                  gtid, up, lb, ub, str, chunk_sz));                           \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
                            (str2 > 0) ? (ub - 1) : (ub + 1), str2, chunk_sz,  \
                            (schedule) != kmp_sch_static);                     \
      status =                                                                 \
          KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
                                (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str2);                                      \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n",  \
         gtid, *p_lb, *p_ub, status));                                         \
    return status;                                                             \
  }

#define LOOP_RUNTIME_START_ULL(func, schedule)                                 \
  int func(int up, unsigned long long lb, unsigned long long ub,               \
           unsigned long long str, unsigned long long *p_lb,                   \
           unsigned long long *p_ub) {                                         \
    int status;                                                                \
    long long str2 = up ? ((long long)str) : -((long long)str);                \
    unsigned long long stride;                                                 \
    unsigned long long chunk_sz = 0;                                           \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
                                                                               \
    KA_TRACE(20, (KMP_STR(func) ": T#%d, up %d, lb 0x%llx, ub 0x%llx, str "    \
                                "0x%llx, chunk_sz 0x%llx\n",                   \
                  gtid, up, lb, ub, str, chunk_sz));                           \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
                            (str2 > 0) ? (ub - 1) : (ub + 1), str2, chunk_sz,  \
                            TRUE);                                             \
      status =                                                                 \
          KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
                                (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT((long long)stride == str2);                           \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n",  \
         gtid, *p_lb, *p_ub, status));                                         \
    return status;                                                             \
  }

#define LOOP_NEXT_ULL(func, fini_code)                                         \
  int func(unsigned long long *p_lb, unsigned long long *p_ub) {               \
    int status;                                                                \
    long long stride;                                                          \
    int gtid = __kmp_get_gtid();                                               \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(20, (KMP_STR(func) ": T#%d\n", gtid));                            \
                                                                               \
    fini_code status =                                                         \
        KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,            \
                              (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);       \
    if (status) {                                                              \
      *p_ub += (stride > 0) ? 1 : -1;                                          \
    }                                                                          \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, stride 0x%llx, "  \
                   "returning %d\n",                                           \
         gtid, *p_lb, *p_ub, stride, status));                                 \
    return status;                                                             \
  }

LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_START),
               kmp_sch_static)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_NEXT), {})
LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_START),
               kmp_sch_dynamic_chunked)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_NEXT), {})
LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_START),
               kmp_sch_guided_chunked)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_NEXT), {})
LOOP_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_DYNAMIC_START),
               kmp_sch_dynamic_chunked)
LOOP_NEXT_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_DYNAMIC_NEXT), {})
LOOP_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_GUIDED_START),
               kmp_sch_guided_chunked)
LOOP_NEXT_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_GUIDED_NEXT), {})
LOOP_RUNTIME_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_START), kmp_sch_runtime)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_NEXT), {})
LOOP_RUNTIME_START_ULL(
    KMP_EXPAND_NAME(
        KMP_API_NAME_GOMP_LOOP_ULL_MAYBE_NONMONOTONIC_RUNTIME_START),
    kmp_sch_runtime)
LOOP_RUNTIME_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_RUNTIME_START),
    kmp_sch_runtime)
LOOP_NEXT_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_MAYBE_NONMONOTONIC_RUNTIME_NEXT),
    {})
LOOP_NEXT_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_RUNTIME_NEXT), {})

LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_START),
               kmp_ord_static)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_NEXT),
              { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
LOOP_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_START),
    kmp_ord_dynamic_chunked)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_NEXT),
              { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_START),
               kmp_ord_guided_chunked)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_NEXT),
              { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
LOOP_RUNTIME_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_START),
    kmp_ord_runtime)
LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_NEXT),
              { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })

#define LOOP_DOACROSS_START_ULL(func, schedule)                                \
  int func(unsigned ncounts, unsigned long long *counts,                       \
           unsigned long long chunk_sz, unsigned long long *p_lb,              \
           unsigned long long *p_ub) {                                         \
    int status;                                                                \
    long long stride, str, lb, ub;                                             \
    int gtid = __kmp_entry_gtid();                                             \
    struct kmp_dim *dims =                                                     \
        (struct kmp_dim *)__kmp_allocate(sizeof(struct kmp_dim) * ncounts);    \
    MKLOC(loc, KMP_STR(func));                                                 \
    for (unsigned i = 0; i < ncounts; ++i) {                                   \
      dims[i].lo = 0;                                                          \
      dims[i].up = counts[i] - 1;                                              \
      dims[i].st = 1;                                                          \
    }                                                                          \
    __kmpc_doacross_init(&loc, gtid, (int)ncounts, dims);                      \
    lb = 0;                                                                    \
    ub = counts[0];                                                            \
    str = 1;                                                                   \
                                                                               \
    KA_TRACE(20, (KMP_STR(func) ": T#%d, lb 0x%llx, ub 0x%llx, str "           \
                                "0x%llx, chunk_sz 0x%llx\n",                   \
                  gtid, lb, ub, str, chunk_sz));                               \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
                            (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,    \
                            (schedule) != kmp_sch_static);                     \
      status =                                                                 \
          KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
                                (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
    KMP_DOACROSS_FINI(status, gtid);                                           \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n",  \
         gtid, *p_lb, *p_ub, status));                                         \
    __kmp_free(dims);                                                          \
    return status;                                                             \
  }

#define LOOP_DOACROSS_RUNTIME_START_ULL(func, schedule)                        \
  int func(unsigned ncounts, unsigned long long *counts,                       \
           unsigned long long *p_lb, unsigned long long *p_ub) {               \
    int status;                                                                \
    unsigned long long stride, str, lb, ub;                                    \
    unsigned long long chunk_sz = 0;                                           \
    int gtid = __kmp_entry_gtid();                                             \
    struct kmp_dim *dims =                                                     \
        (struct kmp_dim *)__kmp_allocate(sizeof(struct kmp_dim) * ncounts);    \
    MKLOC(loc, KMP_STR(func));                                                 \
    for (unsigned i = 0; i < ncounts; ++i) {                                   \
      dims[i].lo = 0;                                                          \
      dims[i].up = counts[i] - 1;                                              \
      dims[i].st = 1;                                                          \
    }                                                                          \
    __kmpc_doacross_init(&loc, gtid, (int)ncounts, dims);                      \
    lb = 0;                                                                    \
    ub = counts[0];                                                            \
    str = 1;                                                                   \
    KA_TRACE(20, (KMP_STR(func) ": T#%d, lb 0x%llx, ub 0x%llx, str "           \
                                "0x%llx, chunk_sz 0x%llx\n",                   \
                  gtid, lb, ub, str, chunk_sz));                               \
                                                                               \
    if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
      KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
                            (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,    \
                            TRUE);                                             \
      status =                                                                 \
          KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
                                (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
      if (status) {                                                            \
        KMP_DEBUG_ASSERT(stride == str);                                       \
        *p_ub += (str > 0) ? 1 : -1;                                           \
      }                                                                        \
    } else {                                                                   \
      status = 0;                                                              \
    }                                                                          \
    KMP_DOACROSS_FINI(status, gtid);                                           \
                                                                               \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n",  \
         gtid, *p_lb, *p_ub, status));                                         \
    __kmp_free(dims);                                                          \
    return status;                                                             \
  }

LOOP_DOACROSS_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_STATIC_START),
    kmp_sch_static)
LOOP_DOACROSS_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_DYNAMIC_START),
    kmp_sch_dynamic_chunked)
LOOP_DOACROSS_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_GUIDED_START),
    kmp_sch_guided_chunked)
LOOP_DOACROSS_RUNTIME_START_ULL(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_RUNTIME_START),
    kmp_sch_runtime)

// Combined parallel / loop worksharing constructs
//
// There are no ull versions (yet).

#define PARALLEL_LOOP_START(func, schedule, ompt_pre, ompt_post)               \
  void func(void (*task)(void *), void *data, unsigned num_threads, long lb,   \
            long ub, long str, long chunk_sz) {                                \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n",  \
         gtid, lb, ub, str, chunk_sz));                                        \
                                                                               \
    ompt_pre();                                                                \
                                                                               \
    __kmp_GOMP_fork_call(&loc, gtid, num_threads, 0u, task,                    \
                         (microtask_t)__kmp_GOMP_parallel_microtask_wrapper,   \
                         9, task, data, num_threads, &loc, (schedule), lb,     \
                         (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz);      \
    IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid));                          \
                                                                               \
    KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                              \
                      (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,          \
                      (schedule) != kmp_sch_static);                           \
                                                                               \
    ompt_post();                                                               \
                                                                               \
    KA_TRACE(20, (KMP_STR(func) " exit: T#%d\n", gtid));                       \
  }

#if OMPT_SUPPORT && OMPT_OPTIONAL

#define OMPT_LOOP_PRE()                                                        \
  ompt_frame_t *parent_frame;                                                  \
  if (ompt_enabled.enabled) {                                                  \
    __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);   \
    parent_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);                 \
    OMPT_STORE_RETURN_ADDRESS(gtid);                                           \
  }

#define OMPT_LOOP_POST()                                                       \
  if (ompt_enabled.enabled) {                                                  \
    parent_frame->enter_frame = ompt_data_none;                                \
  }

#else

#define OMPT_LOOP_PRE()

#define OMPT_LOOP_POST()

#endif

PARALLEL_LOOP_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC_START),
    kmp_sch_static, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC_START),
    kmp_sch_dynamic_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED_START),
    kmp_sch_guided_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP_START(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME_START),
    kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)

// Tasking constructs

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data,
                                             void (*copy_func)(void *, void *),
                                             long arg_size, long arg_align,
                                             bool if_cond, unsigned gomp_flags,
                                             void **depend) {
  MKLOC(loc, "GOMP_task");
  int gtid = __kmp_entry_gtid();
  kmp_int32 flags = 0;
  kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;

  KA_TRACE(20, ("GOMP_task: T#%d\n", gtid));

  // The low-order bit is the "untied" flag
  if (!(gomp_flags & KMP_GOMP_TASK_UNTIED_FLAG)) {
    input_flags->tiedness = 1;
  }
  // The second low-order bit is the "final" flag
  if (gomp_flags & KMP_GOMP_TASK_FINAL_FLAG) {
    input_flags->final = 1;
  }
  input_flags->native = 1;
  // __kmp_task_alloc() sets up all other flags

  if (!if_cond) {
    arg_size = 0;
  }

  kmp_task_t *task = __kmp_task_alloc(
      &loc, gtid, input_flags, sizeof(kmp_task_t),
      arg_size ? arg_size + arg_align - 1 : 0, (kmp_routine_entry_t)func);

  if (arg_size > 0) {
    if (arg_align > 0) {
      task->shareds = (void *)((((size_t)task->shareds) + arg_align - 1) /
                               arg_align * arg_align);
    }
    // else error??

    if (copy_func) {
      (*copy_func)(task->shareds, data);
    } else {
      KMP_MEMCPY(task->shareds, data, arg_size);
    }
  }

#if OMPT_SUPPORT
  kmp_taskdata_t *current_task;
  if (ompt_enabled.enabled) {
    OMPT_STORE_RETURN_ADDRESS(gtid);
    current_task = __kmp_threads[gtid]->th.th_current_task;
    current_task->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  }
#endif

  if (if_cond) {
    if (gomp_flags & KMP_GOMP_TASK_DEPENDS_FLAG) {
      KMP_ASSERT(depend);
      kmp_gomp_depends_info_t gomp_depends(depend);
      kmp_int32 ndeps = gomp_depends.get_num_deps();
      kmp_depend_info_t dep_list[ndeps];
      for (kmp_int32 i = 0; i < ndeps; i++)
        dep_list[i] = gomp_depends.get_kmp_depend(i);
      __kmpc_omp_task_with_deps(&loc, gtid, task, ndeps, dep_list, 0, NULL);
    } else {
      __kmpc_omp_task(&loc, gtid, task);
    }
  } else {
#if OMPT_SUPPORT
    ompt_thread_info_t oldInfo;
    kmp_info_t *thread;
    kmp_taskdata_t *taskdata;
    if (ompt_enabled.enabled) {
      // Store the threads states and restore them after the task
      thread = __kmp_threads[gtid];
      taskdata = KMP_TASK_TO_TASKDATA(task);
      oldInfo = thread->th.ompt_thread_info;
      thread->th.ompt_thread_info.wait_id = 0;
      thread->th.ompt_thread_info.state = ompt_state_work_parallel;
      taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
      OMPT_STORE_RETURN_ADDRESS(gtid);
    }
#endif
    if (gomp_flags & KMP_GOMP_TASK_DEPENDS_FLAG) {
      KMP_ASSERT(depend);
      kmp_gomp_depends_info_t gomp_depends(depend);
      kmp_int32 ndeps = gomp_depends.get_num_deps();
      kmp_depend_info_t dep_list[ndeps];
      for (kmp_int32 i = 0; i < ndeps; i++)
        dep_list[i] = gomp_depends.get_kmp_depend(i);
      __kmpc_omp_wait_deps(&loc, gtid, ndeps, dep_list, 0, NULL);
    }

    __kmpc_omp_task_begin_if0(&loc, gtid, task);
    func(data);
    __kmpc_omp_task_complete_if0(&loc, gtid, task);

#if OMPT_SUPPORT
    if (ompt_enabled.enabled) {
      thread->th.ompt_thread_info = oldInfo;
      taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
    }
#endif
  }
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    current_task->ompt_task_info.frame.enter_frame = ompt_data_none;
  }
#endif

  KA_TRACE(20, ("GOMP_task exit: T#%d\n", gtid));
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKWAIT)(void) {
  MKLOC(loc, "GOMP_taskwait");
  int gtid = __kmp_entry_gtid();

#if OMPT_SUPPORT
  if (ompt_enabled.enabled)
    OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  KA_TRACE(20, ("GOMP_taskwait: T#%d\n", gtid));

  __kmpc_omp_taskwait(&loc, gtid);

  KA_TRACE(20, ("GOMP_taskwait exit: T#%d\n", gtid));
}

// Sections worksharing constructs
//
// For the sections construct, we initialize a dynamically scheduled loop
// worksharing construct with lb 1 and stride 1, and use the iteration #'s
// that its returns as sections ids.
//
// There are no special entry points for ordered sections, so we always use
// the dynamically scheduled workshare, even if the sections aren't ordered.

unsigned KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_START)(unsigned count) {
  int status;
  kmp_int lb, ub, stride;
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_sections_start");
  KA_TRACE(20, ("GOMP_sections_start: T#%d\n", gtid));

  KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);

  status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, &lb, &ub, &stride);
  if (status) {
    KMP_DEBUG_ASSERT(stride == 1);
    KMP_DEBUG_ASSERT(lb > 0);
    KMP_ASSERT(lb == ub);
  } else {
    lb = 0;
  }

  KA_TRACE(20, ("GOMP_sections_start exit: T#%d returning %u\n", gtid,
                (unsigned)lb));
  return (unsigned)lb;
}

unsigned KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_NEXT)(void) {
  int status;
  kmp_int lb, ub, stride;
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_sections_next");
  KA_TRACE(20, ("GOMP_sections_next: T#%d\n", gtid));

#if OMPT_SUPPORT
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, &lb, &ub, &stride);
  if (status) {
    KMP_DEBUG_ASSERT(stride == 1);
    KMP_DEBUG_ASSERT(lb > 0);
    KMP_ASSERT(lb == ub);
  } else {
    lb = 0;
  }

  KA_TRACE(
      20, ("GOMP_sections_next exit: T#%d returning %u\n", gtid, (unsigned)lb));
  return (unsigned)lb;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_SECTIONS_START)(
    void (*task)(void *), void *data, unsigned num_threads, unsigned count) {
  int gtid = __kmp_entry_gtid();

#if OMPT_SUPPORT
  ompt_frame_t *parent_frame;

  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);
    parent_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif

  MKLOC(loc, "GOMP_parallel_sections_start");
  KA_TRACE(20, ("GOMP_parallel_sections_start: T#%d\n", gtid));

  __kmp_GOMP_fork_call(&loc, gtid, num_threads, 0u, task,
                       (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, 9,
                       task, data, num_threads, &loc, kmp_nm_dynamic_chunked,
                       (kmp_int)1, (kmp_int)count, (kmp_int)1, (kmp_int)1);

#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    parent_frame->enter_frame = ompt_data_none;
  }
#endif

  KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);

  KA_TRACE(20, ("GOMP_parallel_sections_start exit: T#%d\n", gtid));
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_sections_end: T#%d\n", gtid))

#if OMPT_SUPPORT
  ompt_frame_t *ompt_frame;
  if (ompt_enabled.enabled) {
    __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
    ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    ompt_frame->enter_frame = ompt_data_none;
  }
#endif

  KA_TRACE(20, ("GOMP_sections_end exit: T#%d\n", gtid))
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END_NOWAIT)(void) {
  KA_TRACE(20, ("GOMP_sections_end_nowait: T#%d\n", __kmp_get_gtid()))
}

// libgomp has an empty function for GOMP_taskyield as of 2013-10-10
void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKYIELD)(void) {
  KA_TRACE(20, ("GOMP_taskyield: T#%d\n", __kmp_get_gtid()))
  return;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL)(void (*task)(void *),
                                                 void *data,
                                                 unsigned num_threads,
                                                 unsigned int flags) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_parallel");
  KA_TRACE(20, ("GOMP_parallel: T#%d\n", gtid));

#if OMPT_SUPPORT
  ompt_task_info_t *parent_task_info, *task_info;
  if (ompt_enabled.enabled) {
    parent_task_info = __ompt_get_task_info_object(0);
    parent_task_info->frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  __kmp_GOMP_fork_call(&loc, gtid, num_threads, flags, task,
                       (microtask_t)__kmp_GOMP_microtask_wrapper, 2, task,
                       data);
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    task_info = __ompt_get_task_info_object(0);
    task_info->frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  }
#endif
  task(data);
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    OMPT_STORE_RETURN_ADDRESS(gtid);
  }
#endif
  KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();
#if OMPT_SUPPORT
  if (ompt_enabled.enabled) {
    task_info->frame.exit_frame = ompt_data_none;
    parent_task_info->frame.enter_frame = ompt_data_none;
  }
#endif
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_SECTIONS)(void (*task)(void *),
                                                          void *data,
                                                          unsigned num_threads,
                                                          unsigned count,
                                                          unsigned flags) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_parallel_sections");
  KA_TRACE(20, ("GOMP_parallel_sections: T#%d\n", gtid));

#if OMPT_SUPPORT
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  __kmp_GOMP_fork_call(&loc, gtid, num_threads, flags, task,
                       (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, 9,
                       task, data, num_threads, &loc, kmp_nm_dynamic_chunked,
                       (kmp_int)1, (kmp_int)count, (kmp_int)1, (kmp_int)1);

#if OMPT_SUPPORT
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);

  task(data);
  KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();
  KA_TRACE(20, ("GOMP_parallel_sections exit: T#%d\n", gtid));
}

#define PARALLEL_LOOP(func, schedule, ompt_pre, ompt_post)                     \
  void func(void (*task)(void *), void *data, unsigned num_threads, long lb,   \
            long ub, long str, long chunk_sz, unsigned flags) {                \
    int gtid = __kmp_entry_gtid();                                             \
    MKLOC(loc, KMP_STR(func));                                                 \
    KA_TRACE(                                                                  \
        20,                                                                    \
        (KMP_STR(                                                              \
             func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n",  \
         gtid, lb, ub, str, chunk_sz));                                        \
                                                                               \
    ompt_pre();                                                                \
    __kmp_GOMP_fork_call(&loc, gtid, num_threads, flags, task,                 \
                         (microtask_t)__kmp_GOMP_parallel_microtask_wrapper,   \
                         9, task, data, num_threads, &loc, (schedule), lb,     \
                         (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz);      \
                                                                               \
    IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                          \
    KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                              \
                      (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,          \
                      (schedule) != kmp_sch_static);                           \
    task(data);                                                                \
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();                         \
    ompt_post();                                                               \
                                                                               \
    KA_TRACE(20, (KMP_STR(func) " exit: T#%d\n", gtid));                       \
  }

PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC),
              kmp_sch_static, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC),
              kmp_sch_dynamic_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_GUIDED),
              kmp_sch_guided_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_DYNAMIC),
              kmp_sch_dynamic_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED),
              kmp_sch_guided_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME),
              kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_MAYBE_NONMONOTONIC_RUNTIME),
    kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)
PARALLEL_LOOP(
    KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_RUNTIME),
    kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKGROUP_START)(void) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_taskgroup_start");
  KA_TRACE(20, ("GOMP_taskgroup_start: T#%d\n", gtid));

#if OMPT_SUPPORT
  if (ompt_enabled.enabled)
    OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  __kmpc_taskgroup(&loc, gtid);

  return;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKGROUP_END)(void) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_taskgroup_end");
  KA_TRACE(20, ("GOMP_taskgroup_end: T#%d\n", gtid));

#if OMPT_SUPPORT
  if (ompt_enabled.enabled)
    OMPT_STORE_RETURN_ADDRESS(gtid);
#endif

  __kmpc_end_taskgroup(&loc, gtid);

  return;
}

static kmp_int32 __kmp_gomp_to_omp_cancellation_kind(int gomp_kind) {
  kmp_int32 cncl_kind = 0;
  switch (gomp_kind) {
  case 1:
    cncl_kind = cancel_parallel;
    break;
  case 2:
    cncl_kind = cancel_loop;
    break;
  case 4:
    cncl_kind = cancel_sections;
    break;
  case 8:
    cncl_kind = cancel_taskgroup;
    break;
  }
  return cncl_kind;
}

// Return true if cancellation should take place, false otherwise
bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CANCELLATION_POINT)(int which) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_cancellation_point");
  KA_TRACE(20, ("GOMP_cancellation_point: T#%d which:%d\n", gtid, which));
  kmp_int32 cncl_kind = __kmp_gomp_to_omp_cancellation_kind(which);
  return __kmpc_cancellationpoint(&loc, gtid, cncl_kind);
}

// Return true if cancellation should take place, false otherwise
bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CANCEL)(int which, bool do_cancel) {
  int gtid = __kmp_get_gtid();
  MKLOC(loc, "GOMP_cancel");
  KA_TRACE(20, ("GOMP_cancel: T#%d which:%d do_cancel:%d\n", gtid, which,
                (int)do_cancel));
  kmp_int32 cncl_kind = __kmp_gomp_to_omp_cancellation_kind(which);

  if (do_cancel == FALSE) {
    return __kmpc_cancellationpoint(&loc, gtid, cncl_kind);
  } else {
    return __kmpc_cancel(&loc, gtid, cncl_kind);
  }
}

// Return true if cancellation should take place, false otherwise
bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_BARRIER_CANCEL)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_barrier_cancel: T#%d\n", gtid));
  return __kmp_barrier_gomp_cancel(gtid);
}

// Return true if cancellation should take place, false otherwise
bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END_CANCEL)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_sections_end_cancel: T#%d\n", gtid));
  return __kmp_barrier_gomp_cancel(gtid);
}

// Return true if cancellation should take place, false otherwise
bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END_CANCEL)(void) {
  int gtid = __kmp_get_gtid();
  KA_TRACE(20, ("GOMP_loop_end_cancel: T#%d\n", gtid));
  return __kmp_barrier_gomp_cancel(gtid);
}

// All target functions are empty as of 2014-05-29
void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET)(int device, void (*fn)(void *),
                                               const void *openmp_target,
                                               size_t mapnum, void **hostaddrs,
                                               size_t *sizes,
                                               unsigned char *kinds) {
  return;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_DATA)(
    int device, const void *openmp_target, size_t mapnum, void **hostaddrs,
    size_t *sizes, unsigned char *kinds) {
  return;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_END_DATA)(void) { return; }

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_UPDATE)(
    int device, const void *openmp_target, size_t mapnum, void **hostaddrs,
    size_t *sizes, unsigned char *kinds) {
  return;
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TEAMS)(unsigned int num_teams,
                                              unsigned int thread_limit) {
  return;
}

// Task duplication function which copies src to dest (both are
// preallocated task structures)
static void __kmp_gomp_task_dup(kmp_task_t *dest, kmp_task_t *src,
                                kmp_int32 last_private) {
  kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(src);
  if (taskdata->td_copy_func) {
    (taskdata->td_copy_func)(dest->shareds, src->shareds);
  }
}

#ifdef __cplusplus
} // extern "C"
#endif

template <typename T>
void __GOMP_taskloop(void (*func)(void *), void *data,
                     void (*copy_func)(void *, void *), long arg_size,
                     long arg_align, unsigned gomp_flags,
                     unsigned long num_tasks, int priority, T start, T end,
                     T step) {
  typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
  MKLOC(loc, "GOMP_taskloop");
  int sched;
  T *loop_bounds;
  int gtid = __kmp_entry_gtid();
  kmp_int32 flags = 0;
  int if_val = gomp_flags & (1u << 10);
  int nogroup = gomp_flags & (1u << 11);
  int up = gomp_flags & (1u << 8);
  p_task_dup_t task_dup = NULL;
  kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
#ifdef KMP_DEBUG
  {
    char *buff;
    buff = __kmp_str_format(
        "GOMP_taskloop: T#%%d: func:%%p data:%%p copy_func:%%p "
        "arg_size:%%ld arg_align:%%ld gomp_flags:0x%%x num_tasks:%%lu "
        "priority:%%d start:%%%s end:%%%s step:%%%s\n",
        traits_t<T>::spec, traits_t<T>::spec, traits_t<T>::spec);
    KA_TRACE(20, (buff, gtid, func, data, copy_func, arg_size, arg_align,
                  gomp_flags, num_tasks, priority, start, end, step));
    __kmp_str_free(&buff);
  }
#endif
  KMP_ASSERT((size_t)arg_size >= 2 * sizeof(T));
  KMP_ASSERT(arg_align > 0);
  // The low-order bit is the "untied" flag
  if (!(gomp_flags & 1)) {
    input_flags->tiedness = 1;
  }
  // The second low-order bit is the "final" flag
  if (gomp_flags & 2) {
    input_flags->final = 1;
  }
  // Negative step flag
  if (!up) {
    // If step is flagged as negative, but isn't properly sign extended
    // Then manually sign extend it.  Could be a short, int, char embedded
    // in a long.  So cannot assume any cast.
    if (step > 0) {
      for (int i = sizeof(T) * CHAR_BIT - 1; i >= 0L; --i) {
        // break at the first 1 bit
        if (step & ((T)1 << i))
          break;
        step |= ((T)1 << i);
      }
    }
  }
  input_flags->native = 1;
  // Figure out if none/grainsize/num_tasks clause specified
  if (num_tasks > 0) {
    if (gomp_flags & (1u << 9))
      sched = 1; // grainsize specified
    else
      sched = 2; // num_tasks specified
    // neither grainsize nor num_tasks specified
  } else {
    sched = 0;
  }

  // __kmp_task_alloc() sets up all other flags
  kmp_task_t *task =
      __kmp_task_alloc(&loc, gtid, input_flags, sizeof(kmp_task_t),
                       arg_size + arg_align - 1, (kmp_routine_entry_t)func);
  kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
  taskdata->td_copy_func = copy_func;
  taskdata->td_size_loop_bounds = sizeof(T);

  // re-align shareds if needed and setup firstprivate copy constructors
  // through the task_dup mechanism
  task->shareds = (void *)((((size_t)task->shareds) + arg_align - 1) /
                           arg_align * arg_align);
  if (copy_func) {
    task_dup = __kmp_gomp_task_dup;
  }
  KMP_MEMCPY(task->shareds, data, arg_size);

  loop_bounds = (T *)task->shareds;
  loop_bounds[0] = start;
  loop_bounds[1] = end + (up ? -1 : 1);
  __kmpc_taskloop(&loc, gtid, task, if_val, (kmp_uint64 *)&(loop_bounds[0]),
                  (kmp_uint64 *)&(loop_bounds[1]), (kmp_int64)step, nogroup,
                  sched, (kmp_uint64)num_tasks, (void *)task_dup);
}

// 4 byte version of GOMP_doacross_post
// This verison needs to create a temporary array which converts 4 byte
// integers into 8 byte integers
template <typename T, bool need_conversion = (sizeof(long) == 4)>
void __kmp_GOMP_doacross_post(T *count);

template <> void __kmp_GOMP_doacross_post<long, true>(long *count) {
  int gtid = __kmp_entry_gtid();
  kmp_info_t *th = __kmp_threads[gtid];
  MKLOC(loc, "GOMP_doacross_post");
  kmp_int64 num_dims = th->th.th_dispatch->th_doacross_info[0];
  kmp_int64 *vec =
      (kmp_int64 *)__kmp_thread_malloc(th, sizeof(kmp_int64) * num_dims);
  for (kmp_int64 i = 0; i < num_dims; ++i) {
    vec[i] = (kmp_int64)count[i];
  }
  __kmpc_doacross_post(&loc, gtid, vec);
  __kmp_thread_free(th, vec);
}

// 8 byte versions of GOMP_doacross_post
// This version can just pass in the count array directly instead of creating
// a temporary array
template <> void __kmp_GOMP_doacross_post<long, false>(long *count) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_doacross_post");
  __kmpc_doacross_post(&loc, gtid, RCAST(kmp_int64 *, count));
}

template <typename T> void __kmp_GOMP_doacross_wait(T first, va_list args) {
  int gtid = __kmp_entry_gtid();
  kmp_info_t *th = __kmp_threads[gtid];
  MKLOC(loc, "GOMP_doacross_wait");
  kmp_int64 num_dims = th->th.th_dispatch->th_doacross_info[0];
  kmp_int64 *vec =
      (kmp_int64 *)__kmp_thread_malloc(th, sizeof(kmp_int64) * num_dims);
  vec[0] = (kmp_int64)first;
  for (kmp_int64 i = 1; i < num_dims; ++i) {
    T item = va_arg(args, T);
    vec[i] = (kmp_int64)item;
  }
  __kmpc_doacross_wait(&loc, gtid, vec);
  __kmp_thread_free(th, vec);
  return;
}

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKLOOP)(
    void (*func)(void *), void *data, void (*copy_func)(void *, void *),
    long arg_size, long arg_align, unsigned gomp_flags, unsigned long num_tasks,
    int priority, long start, long end, long step) {
  __GOMP_taskloop<long>(func, data, copy_func, arg_size, arg_align, gomp_flags,
                        num_tasks, priority, start, end, step);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKLOOP_ULL)(
    void (*func)(void *), void *data, void (*copy_func)(void *, void *),
    long arg_size, long arg_align, unsigned gomp_flags, unsigned long num_tasks,
    int priority, unsigned long long start, unsigned long long end,
    unsigned long long step) {
  __GOMP_taskloop<unsigned long long>(func, data, copy_func, arg_size,
                                      arg_align, gomp_flags, num_tasks,
                                      priority, start, end, step);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_DOACROSS_POST)(long *count) {
  __kmp_GOMP_doacross_post(count);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_DOACROSS_WAIT)(long first, ...) {
  va_list args;
  va_start(args, first);
  __kmp_GOMP_doacross_wait<long>(first, args);
  va_end(args);
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_DOACROSS_ULL_POST)(
    unsigned long long *count) {
  int gtid = __kmp_entry_gtid();
  MKLOC(loc, "GOMP_doacross_ull_post");
  __kmpc_doacross_post(&loc, gtid, RCAST(kmp_int64 *, count));
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_DOACROSS_ULL_WAIT)(
    unsigned long long first, ...) {
  va_list args;
  va_start(args, first);
  __kmp_GOMP_doacross_wait<unsigned long long>(first, args);
  va_end(args);
}

// fn: the function each master thread of new team will call
// data: argument to fn
// num_teams, thread_limit: max bounds on respective ICV
// flags: unused
void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TEAMS_REG)(void (*fn)(void *),
                                                  void *data,
                                                  unsigned num_teams,
                                                  unsigned thread_limit,
                                                  unsigned flags) {
  MKLOC(loc, "GOMP_teams_reg");
  int gtid = __kmp_entry_gtid();
  KA_TRACE(20, ("GOMP_teams_reg: T#%d num_teams=%u thread_limit=%u flag=%u\n",
                gtid, num_teams, thread_limit, flags));
  __kmpc_push_num_teams(&loc, gtid, num_teams, thread_limit);
  __kmpc_fork_teams(&loc, 2, (microtask_t)__kmp_GOMP_microtask_wrapper, fn,
                    data);
  KA_TRACE(20, ("GOMP_teams_reg exit: T#%d\n", gtid));
}

void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKWAIT_DEPEND)(void **depend) {
  MKLOC(loc, "GOMP_taskwait_depend");
  int gtid = __kmp_entry_gtid();
  KA_TRACE(20, ("GOMP_taskwait_depend: T#%d\n", gtid));
  kmp_gomp_depends_info_t gomp_depends(depend);
  kmp_int32 ndeps = gomp_depends.get_num_deps();
  kmp_depend_info_t dep_list[ndeps];
  for (kmp_int32 i = 0; i < ndeps; i++)
    dep_list[i] = gomp_depends.get_kmp_depend(i);
#if OMPT_SUPPORT
  OMPT_STORE_RETURN_ADDRESS(gtid);
#endif
  __kmpc_omp_wait_deps(&loc, gtid, ndeps, dep_list, 0, NULL);
  KA_TRACE(20, ("GOMP_taskwait_depend exit: T#%d\n", gtid));
}

/* The following sections of code create aliases for the GOMP_* functions, then
   create versioned symbols using the assembler directive .symver. This is only
   pertinent for ELF .so library. The KMP_VERSION_SYMBOL macro is defined in
   kmp_os.h  */

#ifdef KMP_USE_VERSION_SYMBOLS
// GOMP_1.0 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ATOMIC_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ATOMIC_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_BARRIER, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_NAME_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_NAME_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DYNAMIC_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DYNAMIC_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END_NOWAIT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_GUIDED_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_GUIDED_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_RUNTIME_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_RUNTIME_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_STATIC_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_STATIC_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ORDERED_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ORDERED_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC_START, 10,
                   "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_SECTIONS_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END_NOWAIT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_NEXT, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_COPY_END, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_COPY_START, 10, "GOMP_1.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_START, 10, "GOMP_1.0");

// GOMP_2.0 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASK, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKWAIT, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_NEXT, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_START, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_NEXT, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_START, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_NEXT, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_START, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_NEXT, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_START, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_NEXT, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_START, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_NEXT, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_START, 20,
                   "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_NEXT, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_START, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_NEXT, 20, "GOMP_2.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_START, 20, "GOMP_2.0");

// GOMP_3.0 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKYIELD, 30, "GOMP_3.0");

// GOMP_4.0 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_SECTIONS, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKGROUP_START, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKGROUP_END, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_BARRIER_CANCEL, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CANCEL, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CANCELLATION_POINT, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END_CANCEL, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END_CANCEL, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_DATA, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_END_DATA, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_UPDATE, 40, "GOMP_4.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TEAMS, 40, "GOMP_4.0");

// GOMP_4.5 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKLOOP, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKLOOP_ULL, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_DOACROSS_POST, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_DOACROSS_WAIT, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DOACROSS_STATIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DOACROSS_DYNAMIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DOACROSS_GUIDED_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DOACROSS_RUNTIME_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_DOACROSS_ULL_POST, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_DOACROSS_ULL_WAIT, 45, "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_STATIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_DYNAMIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_GUIDED_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DOACROSS_RUNTIME_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_DYNAMIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_DYNAMIC_NEXT, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_GUIDED_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_GUIDED_NEXT, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_DYNAMIC_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_DYNAMIC_NEXT, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_GUIDED_START, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_GUIDED_NEXT, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_DYNAMIC, 45,
                   "GOMP_4.5");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_GUIDED, 45,
                   "GOMP_4.5");

// GOMP_5.0 versioned symbols
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_MAYBE_NONMONOTONIC_RUNTIME_NEXT, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_MAYBE_NONMONOTONIC_RUNTIME_START, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_RUNTIME_NEXT, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_NONMONOTONIC_RUNTIME_START, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_MAYBE_NONMONOTONIC_RUNTIME_NEXT,
                   50, "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_MAYBE_NONMONOTONIC_RUNTIME_START,
                   50, "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_RUNTIME_NEXT, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_NONMONOTONIC_RUNTIME_START, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_NONMONOTONIC_RUNTIME, 50,
                   "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_MAYBE_NONMONOTONIC_RUNTIME,
                   50, "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TEAMS_REG, 50, "GOMP_5.0");
KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKWAIT_DEPEND, 50, "GOMP_5.0");

#endif // KMP_USE_VERSION_SYMBOLS

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus