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
|
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-09-29 Thu 11:40 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TEXT back end</title>
<meta name="author" content="TREX-CoE" />
<meta name="generator" content="Org Mode" />
<style>
#content { max-width: 60em; margin: auto; }
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #e6e6e6;
border-radius: 3px;
background-color: #f2f2f2;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: auto;
}
pre.src:before {
display: none;
position: absolute;
top: -8px;
right: 12px;
padding: 3px;
color: #555;
background-color: #f2f2f299;
}
pre.src:hover:before { display: inline; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-authinfo::before { content: 'Authinfo'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { }
</style>
<link rel="stylesheet" title="Standard" href="trexio.css" type="text/css" />
<script src="org-info.js">
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
// @license-end
</script>
<script>
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
org_html_manager.set("TOC_DEPTH", "4");
org_html_manager.set("LINK_HOME", "index.html");
org_html_manager.set("LINK_UP", "");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "0");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "1");
org_html_manager.set("VIEW", "info");
org_html_manager.setup(); // activate after the parameters are set
// @license-end
</script>
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href=""> UP </a>
|
<a accesskey="H" href="index.html"> HOME </a>
</div><div id="content" class="content">
<h1 class="title">TEXT back end</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org0cf1cc7">1. Template for group-related structures in text back end</a></li>
<li><a href="#org4c9b714">2. Template for general structure in text back end</a></li>
<li><a href="#orgc3710cd">3. Initialize function (constant part)</a></li>
<li><a href="#org5b888f3">4. Deinitialize function (templated part)</a></li>
<li><a href="#org5601d7a">5. Flush function (templated part)</a></li>
<li><a href="#orgf95a3f6">6. Template for text read a group</a></li>
<li><a href="#org0600582">7. Template for text has a group</a></li>
<li><a href="#org758ffb2">8. Template for text flush a group</a></li>
<li><a href="#org48ef6cf">9. Template for text free memory</a></li>
<li><a href="#orgcd0b080">10. Template for has/read/write a numerical attribute</a></li>
<li><a href="#orgde379cb">11. Template for has/read/write a dataset of numerical data</a></li>
<li><a href="#org234bb91">12. Template for has/read/write a dataset of strings</a></li>
<li><a href="#org3bb6e41">13. Template for has/read/write a string attribute</a></li>
<li><a href="#orgad857be">14. Template for has/read/write the dataset of sparse data</a></li>
<li><a href="#org653d0b0">15. Template for text delete a group (UNSAFE mode)</a></li>
<li><a href="#org85b8a1d">16. Source code for the determinant part</a></li>
</ul>
</div>
</div>
<p>
The "file" produced by the text back end is a directory with one
file per group.
</p>
<p>
When the file is open, it is locked by the current process. No other
process can read/write the same file. This guarantees that the
representation in memory is consistent with the file and avoid
re-reading the file before writing.
To lock the file, we lock the <code>.lock</code> file which is present in the
directory.
</p>
<p>
The file is written when closed, or when the flush function is called.
</p>
<div id="outline-container-org0cf1cc7" class="outline-2">
<h2 id="org0cf1cc7"><span class="section-number-2">1.</span> Template for group-related structures in text back end</h2>
<div class="outline-text-2" id="text-1">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> $group$_s {
$group_num_dtype_double$ $group_num$;
$group_dset_dtype$* $group_dset$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims_$group_dset$</span>[16];
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">len_$group_str$</span>;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank_$group_dset$</span>;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">to_flush</span>;
bool $group_num$_isSet;
<span style="color: #228b22;">char</span>* $group_str$;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_name</span>[TREXIO_MAX_FILENAME_LENGTH];
} $group$_t;
</pre>
</div>
</div>
</div>
<div id="outline-container-org4c9b714" class="outline-2">
<h2 id="org4c9b714"><span class="section-number-2">2.</span> Template for general structure in text back end</h2>
<div class="outline-text-2" id="text-2">
<p>
Polymorphism of the <code>trexio_t</code> type is handled by ensuring that the
corresponding types for all back ends can be safely casted to
<code>trexio_t</code>. This is done by making the back-end structs start with
<code>trexio_t parent</code> attribute:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">trexio_text_s</span> {
<span style="color: #228b22;">trexio_t</span> <span style="color: #a0522d;">parent</span> ;
$group$_t* $group$;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">lock_file</span>;
} <span style="color: #228b22;">trexio_text_t</span>;
</pre>
</div>
</div>
</div>
<div id="outline-container-orgc3710cd" class="outline-2">
<h2 id="orgc3710cd"><span class="section-number-2">3.</span> Initialize function (constant part)</h2>
<div class="outline-text-2" id="text-3">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">bool</span>
<span style="color: #0000ff;">trexio_text_file_exists</span> (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">file_name</span>)
{
/* <span style="color: #b22222;">Check if the file with "file_name" exists </span>*/
<span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat</span> <span style="color: #a0522d;">st</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span> = stat(file_name, &st);
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span> = rc == 0;
<span style="color: #a020f0;">return</span> file_exists;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_inquire</span> (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">file_name</span>)
{
/* <span style="color: #b22222;">Check if the file with "file_name" exists and that it is a directory </span>*/
<span style="color: #a020f0;">struct</span> <span style="color: #228b22;">stat</span> <span style="color: #a0522d;">st</span>;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span> = stat(file_name, &st);
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span> = rc == 0;
<span style="color: #a020f0;">if</span> (file_exists) {
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">is_a_directory</span> = <span style="color: #008b8b;">false</span>;
<span style="color: #483d8b;">#ifdef</span> S_IFDIR
is_a_directory = st.st_mode & S_IFDIR;
<span style="color: #483d8b;">#elif</span> S_ISDIR
is_a_directory = S_ISDIR(s.st_mode);
<span style="color: #483d8b;">#else</span>
printf(<span style="color: #8b2252;">"Some important macros are missing for directory handling.\n"</span>);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #483d8b;">#endif</span>
<span style="color: #a020f0;">if</span> (!is_a_directory) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_init</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">f</span> = (<span style="color: #228b22;">trexio_text_t</span>*) file;
/* <span style="color: #b22222;">Put all pointers to NULL but leave parent untouched </span>*/
memset(&(f->parent)+1,0,<span style="color: #a020f0;">sizeof</span>(trexio_text_t)-<span style="color: #a020f0;">sizeof</span>(trexio_t));
/* <span style="color: #b22222;">Check if directory exists </span>*/
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
rc = trexio_text_inquire(file->file_name);
/* <span style="color: #b22222;">TREXIO file exists but is not a directory </span>*/
<span style="color: #a020f0;">if</span> (rc == TREXIO_FILE_ERROR) <span style="color: #a020f0;">return</span> rc;
/* <span style="color: #b22222;">If directory does not exist - create it in write mode </span>*/
<span style="color: #a020f0;">if</span> (rc == TREXIO_FAILURE) {
<span style="color: #a020f0;">if</span> (file->mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc_dir</span> = mkdir(file->file_name, 0777);
<span style="color: #a020f0;">if</span> (rc_dir != 0) <span style="color: #a020f0;">return</span> TREXIO_ERRNO;
}
/* <span style="color: #b22222;">Create the lock file in the directory </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">lock_file_name</span> = <span style="color: #8b2252;">"/.lock"</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_name</span>[TREXIO_MAX_FILENAME_LENGTH];
strncpy (file_name, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name));
<span style="color: #a020f0;">if</span> (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != <span style="color: #8b2252;">'\0'</span>) {
<span style="color: #a020f0;">return</span> TREXIO_LOCK_ERROR;
}
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
<span style="color: #a020f0;">if</span> (f->lock_file <= 0) {
<span style="color: #a020f0;">if</span> (file->mode != <span style="color: #8b2252;">'r'</span>) {
<span style="color: #a020f0;">return</span> TREXIO_ERRNO;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">if</span> (errno == EACCES) {
/* <span style="color: #b22222;">The directory is read-only and the lock file can't be written.</span>
<span style="color: #b22222;"> Create a dummy temporary file for dummy locking.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">dirname</span>[TREXIO_MAX_FILENAME_LENGTH] = <span style="color: #8b2252;">"/tmp/trexio.XXXXXX"</span>;
<span style="color: #a020f0;">if</span> (mkdtemp(dirname) == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ERRNO;
strncpy (file_name, dirname, TREXIO_MAX_FILENAME_LENGTH);
strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name));
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
remove(file_name);
rmdir(dirname);
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_ERRNO;
}
}
}
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_lock</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>) {
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">f</span> = (<span style="color: #228b22;">trexio_text_t</span>*) file;
<span style="color: #a020f0;">struct</span> <span style="color: #228b22;">flock</span> <span style="color: #a0522d;">fl</span>;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span> = fcntl(f->lock_file, F_SETLKW, &fl);
<span style="color: #a020f0;">if</span> (rc == -1) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_unlock</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">f</span> = (<span style="color: #228b22;">trexio_text_t</span>*) file;
<span style="color: #a020f0;">struct</span> <span style="color: #228b22;">flock</span> <span style="color: #a0522d;">fl</span>;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(f->lock_file, F_SETLK, &fl);
close(f->lock_file);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org5b888f3" class="outline-2">
<h2 id="org5b888f3"><span class="section-number-2">4.</span> Deinitialize function (templated part)</h2>
<div class="outline-text-2" id="text-4">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_deinit</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_text_free_$group$( (<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org5601d7a" class="outline-2">
<h2 id="org5601d7a"><span class="section-number-2">5.</span> Flush function (templated part)</h2>
<div class="outline-text-2" id="text-5">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_flush</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a0522d;">f</span> = (<span style="color: #228b22;">trexio_text_t</span>*) file;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_text_flush_$group$(f);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf95a3f6" class="outline-2">
<h2 id="orgf95a3f6"><span class="section-number-2">6.</span> Template for text read a group</h2>
<div class="outline-text-2" id="text-6">
<div class="org-src-container">
<pre class="src src-c">$group$_t*
trexio_text_read_$group$ (<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
/* <span style="color: #b22222;">If the data structure exists, return it </span>*/
<span style="color: #a020f0;">if</span> (file->$group$ != <span style="color: #008b8b;">NULL</span>) {
<span style="color: #a020f0;">return</span> file->$group$;
}
/* <span style="color: #b22222;">Allocate the data structure </span>*/
$group$_t* $group$ = MALLOC($group$_t);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
memset($group$,0,<span style="color: #a020f0;">sizeof</span>($group$_t));
/* <span style="color: #b22222;">Build the file name </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* $group$_file_name = <span style="color: #8b2252;">"/$group$.txt"</span>;
strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat ($group$->file_name, $group$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
<span style="color: #a020f0;">if</span> ($group$->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != <span style="color: #8b2252;">'\0'</span>) {
FREE($group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
/* <span style="color: #b22222;">If the file exists, read it </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen($group$->file_name,<span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f != <span style="color: #008b8b;">NULL</span>) {
/* <span style="color: #b22222;">Find size of file to allocate the max size of the string buffer </span>*/
fseek(f, 0L, SEEK_END);
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">sz</span> = ftell(f);
fseek(f, 0L, SEEK_SET);
sz = (sz < 1024) ? (1024) : (sz);
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">buffer</span> = CALLOC(sz, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> (buffer == <span style="color: #008b8b;">NULL</span>) {
fclose(f);
FREE($group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span> = 0;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc_free</span> = TREXIO_FAILURE;
/* <span style="color: #b22222;">workaround for the case of missing blocks in the file </span>*/
// <span style="color: #b22222;">START REPEAT GROUP_DSET_ALL</span>
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">size_$group_dset$</span> = 0;
// <span style="color: #b22222;">END REPEAT GROUP_DSET_ALL</span>
<span style="color: #a020f0;">while</span>(fscanf(f, <span style="color: #8b2252;">"%1023s"</span>, buffer) != EOF) {
<span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"EXIT"</span>) == 0) {
<span style="color: #a020f0;">break</span>;
// <span style="color: #b22222;">START REPEAT GROUP_DSET_ALL</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"rank_$group_dset$"</span>) == 0) {
rc = fscanf(f, <span style="color: #8b2252;">"%u"</span>, &($group$->rank_$group_dset$));
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ != 0) size_$group_dset$ = 1UL;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i<$group$->rank_$group_dset$; ++i){
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">j</span>=0;
rc = fscanf(f, <span style="color: #8b2252;">"%1023s %u"</span>, buffer, &j);
<span style="color: #a020f0;">if</span> ((rc != 2) || (strcmp(buffer, <span style="color: #8b2252;">"dims_$group_dset$"</span>) != 0) || (j!=i)) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
rc = fscanf(f, <span style="color: #8b2252;">"%"</span> SCNu64 <span style="color: #8b2252;">"\n"</span>, &($group$->dims_$group_dset$[i]));
assert(!(rc != 1));
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_ALL</span>
// <span style="color: #b22222;">START REPEAT GROUP_DSET_NUM</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"$group_dset$"</span>) == 0) {
/* <span style="color: #b22222;">Allocate arrays </span>*/
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ == <span style="color: #008b8b;">NULL</span>) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<size_$group_dset$ ; ++i) {
rc = fscanf(f, <span style="color: #8b2252;">"%$group_dset_format_scanf$"</span>, &($group$->$group_dset$[i]));
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_NUM</span>
// <span style="color: #b22222;">START REPEAT GROUP_DSET_STR</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"$group_dset$"</span>) == 0) {
<span style="color: #a020f0;">if</span> (size_$group_dset$ != 0) {
/* <span style="color: #b22222;">Allocate arrays </span>*/
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ == <span style="color: #008b8b;">NULL</span>) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
/* <span style="color: #b22222;">WARNING: this tmp array allows to avoid allocation of space for each element of array of string</span>
<span style="color: #b22222;"> * BUT it's size has to be number_of_str*max_len_str where max_len_str is somewhat arbitrary, e.g. 32.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">tmp_$group_dset$</span>;
tmp_$group_dset$ = CALLOC(size_$group_dset$*32, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<size_$group_dset$ ; ++i) {
$group$->$group_dset$[i] = tmp_$group_dset$;
/* <span style="color: #b22222;">conventional fcanf with "%s" only return the string before the first space character</span>
<span style="color: #b22222;"> * to read string with spaces use "%[^\n]" possible with space before or after, i.e. " %[^\n]"</span>
<span style="color: #b22222;"> </span>*/
rc = fscanf(f, <span style="color: #8b2252;">" %1023[^\n]"</span>, buffer);
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">tmp_$group_dset$_len</span> = strlen(buffer);
strncpy(tmp_$group_dset$, buffer, 32);
tmp_$group_dset$ += tmp_$group_dset$_len + 1;
}
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_STR</span>
// <span style="color: #b22222;">START REPEAT GROUP_NUM</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"$group_num$_isSet"</span>) == 0) {
<span style="color: #228b22;">unsigned</span> <span style="color: #228b22;">int</span> $group_num$_isSet;
/* <span style="color: #b22222;">additional parameter $group_num$_isSet is needed to suppress warning when fscanf into bool variable using %u or %d </span>*/
rc = fscanf(f, <span style="color: #8b2252;">"%u"</span>, &($group_num$_isSet));
$group$->$group_num$_isSet = (bool) $group_num$_isSet;
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> ($group$->$group_num$_isSet == <span style="color: #008b8b;">true</span>) {
rc = fscanf(f, <span style="color: #8b2252;">"%1023s"</span>, buffer);
<span style="color: #a020f0;">if</span> ((rc != 1) || (strcmp(buffer, <span style="color: #8b2252;">"$group_num$"</span>) != 0)) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
rc = fscanf(f, <span style="color: #8b2252;">"%$group_num_format_scanf$"</span>, &($group$->$group_num$));
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
}
// <span style="color: #b22222;">END REPEAT GROUP_NUM</span>
// <span style="color: #b22222;">START REPEAT GROUP_ATTR_STR</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (strcmp(buffer, <span style="color: #8b2252;">"len_$group_str$"</span>) == 0) {
rc = fscanf(f, <span style="color: #8b2252;">"%"</span> SCNu64 <span style="color: #8b2252;">""</span>, &($group$->len_$group_str$));
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
rc = fscanf(f, <span style="color: #8b2252;">"%1023s"</span>, buffer);
<span style="color: #a020f0;">if</span> ((rc != 1) || (strcmp(buffer, <span style="color: #8b2252;">"$group_str$"</span>) != 0)) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> ($group$->len_$group_str$ != 0) {
$group$->$group_str$ = CALLOC($group$->len_$group_str$, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> ($group$->$group_str$ == <span style="color: #008b8b;">NULL</span>) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
rc = fscanf(f, <span style="color: #8b2252;">" %1023[^\n]"</span>, buffer);
<span style="color: #a020f0;">if</span> (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
/* <span style="color: #b22222;">Safer string conversion to avoid buffer overflow in fscanf </span>*/
strncpy($group$->$group_str$, buffer, $group$->len_$group_str$);
}
// <span style="color: #b22222;">END REPEAT GROUP_ATTR_STR</span>
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">continue</span>;
}
}
FREE(buffer);
fclose(f);
f = <span style="color: #008b8b;">NULL</span>;
}
file->$group$ = $group$;
<span style="color: #a020f0;">return</span> $group$;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org0600582" class="outline-2">
<h2 id="org0600582"><span class="section-number-2">7.</span> Template for text has a group</h2>
<div class="outline-text-2" id="text-7">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_has_$group$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
/* <span style="color: #b22222;">Flush the group to make sure the group.txt file is created </span>*/
<span style="color: #a020f0;">if</span> (file->mode != <span style="color: #8b2252;">'r'</span>) {
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span> = trexio_text_flush_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
/* <span style="color: #b22222;">Build the file name </span>*/
<span style="color: #228b22;">char</span> $group$_full_path[TREXIO_MAX_FILENAME_LENGTH];
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* $group$_file_name = <span style="color: #8b2252;">"/$group$.txt"</span>;
strncpy ($group$_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat ($group$_full_path, $group$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
<span style="color: #a020f0;">if</span> ($group$_full_path[TREXIO_MAX_FILENAME_LENGTH-1] != <span style="color: #8b2252;">'\0'</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span>;
file_exists = trexio_text_file_exists($group$_full_path);
<span style="color: #a020f0;">if</span> (file_exists) {
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org758ffb2" class="outline-2">
<h2 id="org758ffb2"><span class="section-number-2">8.</span> Template for text flush a group</h2>
<div class="outline-text-2" id="text-8">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_flush_$group$</span> (<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (file->parent.mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
$group$_t* $group$ = file->$group$;
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
<span style="color: #a020f0;">if</span> ($group$->to_flush == 0) <span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
assert (file->parent.mode == <span style="color: #8b2252;">'w'</span> || file->parent.mode == <span style="color: #8b2252;">'u'</span>);
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen($group$->file_name, <span style="color: #8b2252;">"w"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
/* <span style="color: #b22222;">Write the dimensioning variables </span>*/
// <span style="color: #b22222;">START REPEAT GROUP_DSET_ALL</span>
fprintf(f, <span style="color: #8b2252;">"rank_$group_dset$ %u\n"</span>, $group$->rank_$group_dset$);
// <span style="color: #b22222;">workaround for the case of missing blocks in the file</span>
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">size_$group_dset$</span> = 0;
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ != 0) size_$group_dset$ = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">unsigned</span> <span style="color: #228b22;">int</span> <span style="color: #a0522d;">i</span>=0; i<$group$->rank_$group_dset$; ++i){
fprintf(f, <span style="color: #8b2252;">"dims_$group_dset$ %u %"</span> PRIu64 <span style="color: #8b2252;">"\n"</span>, i, $group$->dims_$group_dset$[i]);
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_ALL</span>
// <span style="color: #b22222;">START REPEAT GROUP_NUM</span>
fprintf(f, <span style="color: #8b2252;">"$group_num$_isSet %u \n"</span>, $group$->$group_num$_isSet);
<span style="color: #a020f0;">if</span> ($group$->$group_num$_isSet == <span style="color: #008b8b;">true</span>) fprintf(f, <span style="color: #8b2252;">"$group_num$ %$group_num_format_printf$ \n"</span>, $group$->$group_num$);
// <span style="color: #b22222;">END REPEAT GROUP_NUM</span>
// <span style="color: #b22222;">START REPEAT GROUP_ATTR_STR</span>
fprintf(f, <span style="color: #8b2252;">"len_$group_str$ %"</span> PRIu64 <span style="color: #8b2252;">"\n"</span>, $group$->len_$group_str$);
fprintf(f, <span style="color: #8b2252;">"$group_str$\n"</span>);
<span style="color: #a020f0;">if</span> ($group$->len_$group_str$ != 0) fprintf(f, <span style="color: #8b2252;">"%s\n"</span>, $group$->$group_str$);
// <span style="color: #b22222;">END REPEAT GROUP_ATTR_STR</span>
/* <span style="color: #b22222;">Write arrays </span>*/
// <span style="color: #b22222;">START REPEAT GROUP_DSET_ALL</span>
fprintf(f, <span style="color: #8b2252;">"$group_dset$\n"</span>);
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<size_$group_dset$ ; ++i) {
fprintf(f, <span style="color: #8b2252;">"%$group_dset_format_printf$\n"</span>, $group$->$group_dset$[i]);
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_ALL</span>
fclose(f);
$group$->to_flush = 0;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org48ef6cf" class="outline-2">
<h2 id="org48ef6cf"><span class="section-number-2">9.</span> Template for text free memory</h2>
<div class="outline-text-2" id="text-9">
<p>
Memory is allocated when reading. The following function frees memory.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_free_$group$</span> (<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (file->parent.mode != <span style="color: #8b2252;">'r'</span>) {
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span> = trexio_text_flush_$group$(file);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
$group$_t* $group$ = file->$group$;
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
// <span style="color: #b22222;">START REPEAT GROUP_DSET_NUM</span>
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ != <span style="color: #008b8b;">NULL</span>) FREE ($group$->$group_dset$);
// <span style="color: #b22222;">END REPEAT GROUP_DSET_NUM</span>
// <span style="color: #b22222;">START REPEAT GROUP_DSET_STR</span>
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ != <span style="color: #008b8b;">NULL</span>) {
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ != 0) FREE ($group$->$group_dset$[0]);
FREE ($group$->$group_dset$);
}
// <span style="color: #b22222;">END REPEAT GROUP_DSET_STR</span>
// <span style="color: #b22222;">START REPEAT GROUP_ATTR_STR</span>
<span style="color: #a020f0;">if</span> ($group$->$group_str$ != <span style="color: #008b8b;">NULL</span>) FREE ($group$->$group_str$);
// <span style="color: #b22222;">END REPEAT GROUP_ATTR_STR</span>
FREE ($group$);
file->$group$ = <span style="color: #008b8b;">NULL</span>;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<p>
This function is called upon the non-successful exit from the <code>trexio_text_read_group</code> function.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_free_read_$group$</span> (<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">buffer</span>, <span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">txt_file</span>, <span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a0522d;">trexio_file</span>, $group$_t* $group$)
{
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc_free</span>;
FREE(buffer);
fclose(txt_file);
/* <span style="color: #b22222;">Set pointer to the struct so that the garbage collector can do the job on file handle </span>*/
trexio_file->$group$ = $group$;
rc_free = trexio_text_free_$group$(trexio_file);
assert(rc_free == TREXIO_SUCCESS);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcd0b080" class="outline-2">
<h2 id="orgcd0b080"><span class="section-number-2">10.</span> Template for has/read/write a numerical attribute</h2>
<div class="outline-text-2" id="text-10">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_read_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_num_dtype_double$* <span style="color: #a020f0;">const</span> num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (num == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
$group$_t* $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
*num = $group$->$group_num$;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_write_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_num_dtype_double$ num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (file->mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
$group$_t* $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
$group$->$group_num$ = num;
$group$->$group_num$_isSet = <span style="color: #008b8b;">true</span>;
$group$->to_flush = 1;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_has_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
$group$_t* $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->$group_num$_isSet == <span style="color: #008b8b;">true</span>){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgde379cb" class="outline-2">
<h2 id="orgde379cb"><span class="section-number-2">11.</span> Template for has/read/write a dataset of numerical data</h2>
<div class="outline-text-2" id="text-11">
<p>
The <code>group_dset</code> array is assumed allocated with the appropriate size.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_read_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype$* <span style="color: #a020f0;">const</span> $group_dset$,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> (rank != $group$->rank_$group_dset$) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i<rank; ++i){
<span style="color: #a020f0;">if</span> (dims[i] != $group$->dims_$group_dset$[i]) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_4;
dim_size *= dims[i];
}
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<dim_size ; ++i) {
$group_dset$[i] = $group$->$group_dset$[i];
}
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_write_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype$* $group_dset$,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (file->mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ != <span style="color: #008b8b;">NULL</span>) {
FREE($group$->$group_dset$);
}
$group$->rank_$group_dset$ = rank;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i<$group$->rank_$group_dset$; ++i){
$group$->dims_$group_dset$[i] = dims[i];
dim_size *= dims[i];
}
$group$->$group_dset$ = CALLOC(dim_size, $group_dset_dtype$);
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<dim_size ; ++i) {
$group$->$group_dset$[i] = $group_dset$[i];
}
$group$->to_flush = 1;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_has_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ > 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org234bb91" class="outline-2">
<h2 id="org234bb91"><span class="section-number-2">12.</span> Template for has/read/write a dataset of strings</h2>
<div class="outline-text-2" id="text-12">
<p>
The <code>group_dset</code> array is assumed allocated with the appropriate size.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_read_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">char</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">dset</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> (rank != $group$->rank_$group_dset$) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0 ; i<rank ; ++i) {
<span style="color: #a020f0;">if</span> (dims[i] != $group$->dims_$group_dset$[i]) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_4;
}
strcpy(dset, <span style="color: #8b2252;">""</span>);
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<dims[0] ; ++i) {
strncat(dset, $group$->$group_dset$[i], max_str_len);
strcat(dset, TREXIO_DELIM);
}
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_write_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>** <span style="color: #a0522d;">dset</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (file->mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ != <span style="color: #008b8b;">NULL</span>) {
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ != 0) FREE($group$->$group_dset$[0]);
FREE($group$->$group_dset$);
}
$group$->rank_$group_dset$ = rank;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i<$group$->rank_$group_dset$; ++i){
$group$->dims_$group_dset$[i] = dims[i];
}
$group$->$group_dset$ = CALLOC(dims[0], <span style="color: #228b22;">char</span>*);
<span style="color: #a020f0;">if</span> ($group$->$group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">tmp_str</span> = CALLOC(dims[0]*32 + 1, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> (tmp_str == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0 ; i<dims[0] ; ++i) {
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">tmp_len</span> = strlen(dset[i]);
$group$->$group_dset$[i] = tmp_str;
strncpy(tmp_str, dset[i], tmp_len);
tmp_str += tmp_len + 1;
}
$group$->to_flush = 1;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_has_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->rank_$group_dset$ > 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org3bb6e41" class="outline-2">
<h2 id="org3bb6e41"><span class="section-number-2">13.</span> Template for has/read/write a string attribute</h2>
<div class="outline-text-2" id="text-13">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_read_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">char</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">str</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (str == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
strncpy(str, $group$->$group_str$, max_str_len);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_write_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> *<span style="color: #a0522d;">str</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (str == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (file->mode == <span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">return</span> TREXIO_READONLY;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->$group_str$ != <span style="color: #008b8b;">NULL</span>) FREE($group$->$group_str$);
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">tmp_len</span> = strlen(str);
$group$->$group_str$ = CALLOC(tmp_len + 1, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> ($group$->$group_str$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
$group$->len_$group_str$ = tmp_len + 1;
strncpy($group$->$group_str$, str, tmp_len + 1);
$group$->to_flush = 1;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_has_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
$group$_t* <span style="color: #a020f0;">const</span> $group$ = trexio_text_read_$group$((<span style="color: #228b22;">trexio_text_t</span>*) file);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #a020f0;">if</span> ($group$->len_$group_str$ > 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgad857be" class="outline-2">
<h2 id="orgad857be"><span class="section-number-2">14.</span> Template for has/read/write the dataset of sparse data</h2>
<div class="outline-text-2" id="text-14">
<p>
Each sparse array is stored in a separate <code>.txt</code> file due to the fact that sparse I/O has to be decoupled
from conventional write/read/flush behaviour of the TEXT back end. Chunks are used to read/write sparse data
to prevent memory overflow. Chunks have a given <code>int64_t size</code>
(size specifies the number of sparse data items, e.g. integrals).
</p>
<p>
User provides indices and values of the sparse array as two separate variables.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_write_$group_dset$</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size_max</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size_start</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span>* <span style="color: #a0522d;">index_sparse</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">double</span>* <span style="color: #a0522d;">value_sparse</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
/* <span style="color: #b22222;">Build the name of the file with sparse data</span>*/
/* <span style="color: #b22222;">The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed? </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> $group_dset$_file_name[256] = <span style="color: #8b2252;">"/$group_dset$.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* <span style="color: #b22222;">Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"a"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Specify the line length in order to offset properly. For example, for 4-index quantities</span>
<span style="color: #b22222;"> the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char.</span>
<span style="color: #b22222;"> CURRENTLY NO OFFSET IS USED WHEN WRITING !</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">line_length</span> = 0L;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">format_str</span>[256];
/* <span style="color: #b22222;">Determine the optimal type for storing indices depending on the size_max (usually mo_num or ao_num) </span>*/
<span style="color: #a020f0;">if</span> (size_max < UINT8_MAX) {
line_length = $sparse_line_length_8$; // <span style="color: #b22222;">41 for 4 indices</span>
strncpy(format_str, $sparse_format_printf_8$, 256);
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (size_max < UINT16_MAX) {
line_length = $sparse_line_length_16$; // <span style="color: #b22222;">49 for 4 indices</span>
strncpy(format_str, $sparse_format_printf_16$, 256);
} <span style="color: #a020f0;">else</span> {
line_length = $sparse_line_length_32$; //<span style="color: #b22222;">69 for 4 indices</span>
strncpy(format_str, $sparse_format_printf_32$, 256);
}
strncat(format_str, <span style="color: #8b2252;">"\n"</span>, 2);
/* <span style="color: #b22222;">Get the starting position of the IO stream to be written in the .size file.</span>
<span style="color: #b22222;"> This is error-prone due to the fact that for large files (>2 GB) in 32-bit systems ftell will fail.</span>
<span style="color: #b22222;"> One can use ftello function which is adapted for large files.</span>
<span style="color: #b22222;"> For now, we can use front-end-provided size_start, which has been checked for INT64_MAX overflow.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">io_start_pos</span> = size_start * line_length;
/* <span style="color: #b22222;">Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < (<span style="color: #228b22;">uint64_t</span>) size; ++i) {
rc = fprintf(f, format_str,
$group_dset_sparse_indices_printf$,
*(value_sparse + i));
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Append .size to the file_full_path in order to write additional info about the written buffer of data </span>*/
strncat(file_full_path, <span style="color: #8b2252;">".size"</span>, 6);
/* <span style="color: #b22222;">Open the new file in "a" (append) mode to append info about the buffer that has been just written </span>*/
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">f_wSize</span> = fopen(file_full_path, <span style="color: #8b2252;">"a"</span>);
<span style="color: #a020f0;">if</span> (f_wSize == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Write the buffer_size </span>*/
rc = fprintf(f_wSize, <span style="color: #8b2252;">"%"</span> PRId64 <span style="color: #8b2252;">" %"</span> PRId64 <span style="color: #8b2252;">"\n"</span>, size, io_start_pos);
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f_wSize);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f_wSize);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> $group$_file_name[256] = <span style="color: #8b2252;">"/$group$.txt"</span>;
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, $group$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span> = trexio_text_file_exists(file_full_path);
/* <span style="color: #b22222;">Create an empty file for the trexio_text_has_group to work </span>*/
<span style="color: #a020f0;">if</span> (!file_exists) {
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span> = fopen(file_full_path, <span style="color: #8b2252;">"ab+"</span>);
fclose(fp);
}
/* <span style="color: #b22222;">Exit upon success </span>*/
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_read_$group_dset$</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size_max</span>,
<span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">eof_read_size</span>,
<span style="color: #228b22;">int32_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">index_sparse</span>,
<span style="color: #228b22;">double</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">value_sparse</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (eof_read_size == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_5;
/* <span style="color: #b22222;">Build the name of the file with sparse data.</span>
<span style="color: #b22222;"> The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> $group_dset$_file_name[256] = <span style="color: #8b2252;">"/$group_dset$.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* <span style="color: #b22222;">Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Specify the line length in order to offset properly. For example, for 4-index quantities</span>
<span style="color: #b22222;"> the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">line_length</span> = 0UL;
/* <span style="color: #b22222;">Determine the line length depending on the size_max (usually mo_num or ao_num) </span>*/
<span style="color: #a020f0;">if</span> (size_max < UINT8_MAX) {
line_length = $sparse_line_length_8$; // <span style="color: #b22222;">41 for 4 indices</span>
} <span style="color: #a020f0;">else</span> <span style="color: #a020f0;">if</span> (size_max < UINT16_MAX) {
line_length = $sparse_line_length_16$; // <span style="color: #b22222;">49 for 4 indices</span>
} <span style="color: #a020f0;">else</span> {
line_length = $sparse_line_length_32$; //<span style="color: #b22222;">69 for 4 indices</span>
}
/* <span style="color: #b22222;">Offset in the file according to the provided value of offset_file and optimal line_length </span>*/
fseek(f, (<span style="color: #228b22;">long</span>) <span style="color: #228b22;">offset_file</span> * <span style="color: #a0522d;">line_length</span>, <span style="color: #a0522d;">SEEK_SET</span>);
/* <span style="color: #b22222;">Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buffer</span>[1024];
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">count</span> = 0UL;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < (<span style="color: #228b22;">uint64_t</span>) size; ++i) {
memset(buffer, 0, <span style="color: #a020f0;">sizeof</span>(buffer));
<span style="color: #a020f0;">if</span> (fgets(buffer, 1023, f) == <span style="color: #008b8b;">NULL</span>){
fclose(f);
*eof_read_size = count;
<span style="color: #a020f0;">return</span> TREXIO_END;
} <span style="color: #a020f0;">else</span> {
rc = sscanf(buffer, <span style="color: #8b2252;">"$group_dset_format_scanf$"</span>,
$group_dset_sparse_indices_scanf$,
value_sparse + i);
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
count += 1UL;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_read_$group_dset$_size</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">size_max</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
/* <span style="color: #b22222;">Build the name of the file with sparse data.</span>
<span style="color: #b22222;"> The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> $group_dset$_file_name[256] = <span style="color: #8b2252;">"/$group_dset$.txt.size"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* <span style="color: #b22222;">Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size_item</span>, <span style="color: #a0522d;">offset_item</span>, <span style="color: #a0522d;">size_accum</span>=0L;
/* <span style="color: #b22222;">Read the values from the file. BEWARE OF POSSIBLE MAX_INT64 OVERFLOW ! </span>*/
<span style="color: #a020f0;">while</span>(fscanf(f, <span style="color: #8b2252;">"%"</span> SCNd64 <span style="color: #8b2252;">" %"</span> SCNd64 <span style="color: #8b2252;">""</span>, &size_item, &offset_item) != EOF) {
/* <span style="color: #b22222;">Check that summation will not overflow the int64_t value </span>*/
<span style="color: #a020f0;">if</span> (INT64_MAX - size_accum > size_item) {
size_accum += size_item;
} <span style="color: #a020f0;">else</span> {
fclose(f);
*size_max = -1L;
<span style="color: #a020f0;">return</span> TREXIO_INT_SIZE_OVERFLOW;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Overwrite the value at the input address and return TREXIO_SUCCESS </span>*/
*size_max = size_accum;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_has_$group_dset$</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
/* <span style="color: #b22222;">Build the name of the file with sparse data.</span>
<span style="color: #b22222;"> The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> $group_dset$_file_name[256] = <span style="color: #8b2252;">"/$group_dset$.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* <span style="color: #b22222;">Check the return code of access function to determine whether the file with sparse data exists or not </span>*/
<span style="color: #a020f0;">if</span> (access(file_full_path, F_OK) == 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org653d0b0" class="outline-2">
<h2 id="org653d0b0"><span class="section-number-2">15.</span> Template for text delete a group (UNSAFE mode)</h2>
<div class="outline-text-2" id="text-15">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_delete_$group$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">trexio_text_t</span>* <span style="color: #a0522d;">f</span> = (<span style="color: #228b22;">trexio_text_t</span>*) file;
$group$_t* $group$ = trexio_text_read_$group$(f);
<span style="color: #a020f0;">if</span> ($group$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span> = remove($group$->file_name);
<span style="color: #a020f0;">if</span> (rc == -1) <span style="color: #a020f0;">return</span> TREXIO_FAILURE;
$group$->to_flush = 0;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc_free</span> = trexio_text_free_$group$(f);
<span style="color: #a020f0;">if</span> (rc_free != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc_free;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org85b8a1d" class="outline-2">
<h2 id="org85b8a1d"><span class="section-number-2">16.</span> Source code for the determinant part</h2>
<div class="outline-text-2" id="text-16">
<p>
Each array is stored in a separate <code>.txt</code> file due to the fact that determinant I/O has to be decoupled
from conventional write/read/flush behaviour of the TEXT back end. Chunks are used to read/write the data
to prevent memory overflow. Chunks have a given <code>int64_t size</code>.
Size specifies the number of data items, e.g. determinants.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_read_determinant_list</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>,
<span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">eof_read_size</span>,
<span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">list</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (eof_read_size == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_5;
<span style="color: #a020f0;">if</span> (list == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_6;
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">determinant_list_file_name</span>[256] = <span style="color: #8b2252;">"/determinant_list.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, determinant_list_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(determinant_list_file_name));
/* <span style="color: #b22222;">Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Specify the line length in order to offset properly.</span>
<span style="color: #b22222;"> Each 64-bit integer takes at most 10 slots and requires one space,</span>
<span style="color: #b22222;"> we have int_num integers per up-spin determinant,</span>
<span style="color: #b22222;"> then this number is doubled because we have the same number for down-spin electrons,</span>
<span style="color: #b22222;"> and then one newline char.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">line_length</span> = dims[1]*11UL + 1UL; // <span style="color: #b22222;">10 digits per int64_t bitfield + 1 space = 11 spots + 1 newline char</span>
/* <span style="color: #b22222;">Offset in the file according to the provided value of offset_file and optimal line_length </span>*/
fseek(f, (<span style="color: #228b22;">long</span>) <span style="color: #228b22;">offset_file</span> * <span style="color: #a0522d;">line_length</span>, <span style="color: #a0522d;">SEEK_SET</span>);
/* <span style="color: #b22222;">Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
/* <span style="color: #b22222;">Declare fixed buffer which will be used to read the determinant string <a1 a2 ... a/\ b1 b2 ... b\/> </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buffer</span>[1024];
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">buf_size</span> = <span style="color: #a020f0;">sizeof</span>(buffer);
/* <span style="color: #b22222;">Parameters to post-process the buffer and to get bit fields integers </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">accum</span> = 0UL;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">shift_int64</span> = 11U;
/* <span style="color: #b22222;">Counter for number of elements beind processed </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">count</span> = 0UL;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < dims[0]; ++i) {
accum = 0UL;
memset(buffer, 0, buf_size);
<span style="color: #a020f0;">if</span> (fgets(buffer, buf_size-1, f) == <span style="color: #008b8b;">NULL</span>){
fclose(f);
*eof_read_size = count;
<span style="color: #a020f0;">return</span> TREXIO_END;
} <span style="color: #a020f0;">else</span> {
/* <span style="color: #b22222;">The format string is not anymore static but rather dynamic (the number of ints depend on the mo_num)</span>
<span style="color: #b22222;"> Thus, we parse the buffer string int_num*2 times to get the bit field determinants.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">j</span>=0; j < (<span style="color: #228b22;">uint32_t</span>) dims[1]; ++j) {
rc = sscanf(buffer+accum, <span style="color: #8b2252;">"%10"</span> SCNd64, list + dims[1]*i + j);
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
accum += shift_int64;
}
count += 1UL;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_read_determinant_coefficient</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>,
<span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">eof_read_size</span>,
<span style="color: #228b22;">double</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">coeff</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (eof_read_size == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_5;
<span style="color: #a020f0;">if</span> (coeff == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_6;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">coeff_file_name</span>[256];
memset(coeff_file_name, 0, <span style="color: #a020f0;">sizeof</span>(coeff_file_name));
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">trexio_state</span> = file->state;
<span style="color: #a020f0;">if</span> (trexio_state != 0) {
sprintf(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient_state_%"</span> PRId32 <span style="color: #8b2252;">".txt"</span>, trexio_state);
} <span style="color: #a020f0;">else</span> {
strncpy(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient.txt"</span>, 32);
}
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* <span style="color: #b22222;">Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Specify the line length in order to offset properly.</span>
<span style="color: #b22222;"> Each double value 24 elements + one newline char.</span>
<span style="color: #b22222;"> </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">line_length</span> = 25UL;
/* <span style="color: #b22222;">Offset in the file according to the provided value of offset_file and optimal line_length </span>*/
fseek(f, (<span style="color: #228b22;">long</span>) <span style="color: #228b22;">offset_file</span> * <span style="color: #a0522d;">line_length</span>, <span style="color: #a0522d;">SEEK_SET</span>);
/* <span style="color: #b22222;">Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
/* <span style="color: #b22222;">Declare fixed buffer which will be used to read the determinant string <a1 a2 ... a/\ b1 b2 ... b\/> </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">buffer</span>[64];
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">buf_size</span> = <span style="color: #a020f0;">sizeof</span>(buffer);
/* <span style="color: #b22222;">Counter for number of elements beind processed </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">count</span> = 0UL;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < dims[0]; ++i) {
memset(buffer, 0, buf_size);
<span style="color: #a020f0;">if</span> (fgets(buffer, buf_size-1, f) == <span style="color: #008b8b;">NULL</span>){
fclose(f);
*eof_read_size = count;
<span style="color: #a020f0;">return</span> TREXIO_END;
} <span style="color: #a020f0;">else</span> {
rc = sscanf(buffer, <span style="color: #8b2252;">"%lf"</span>, coeff + i);
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
count += 1UL;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
<span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_text_read_determinant_coefficient_size</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">int64_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">size_max</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (size_max == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">coeff_file_name</span>[256];
memset(coeff_file_name, 0, <span style="color: #a020f0;">sizeof</span>(coeff_file_name));
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">trexio_state</span> = file->state;
<span style="color: #a020f0;">if</span> (trexio_state != 0) {
sprintf(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient_state_%"</span> PRId32 <span style="color: #8b2252;">".txt.size"</span>, trexio_state);
} <span style="color: #a020f0;">else</span> {
strncpy(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient.txt.size"</span>, 64);
}
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* <span style="color: #b22222;">Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"r"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">size_item</span>, <span style="color: #a0522d;">size_accum</span>=0L;
/* <span style="color: #b22222;">Read the values from the file. BEWARE OF POSSIBLE MAX_INT64 OVERFLOW ! </span>*/
<span style="color: #a020f0;">while</span>(fscanf(f, <span style="color: #8b2252;">"%"</span> SCNd64, &size_item) != EOF) {
/* <span style="color: #b22222;">Check that summation will not overflow the int64_t value </span>*/
<span style="color: #a020f0;">if</span> (INT64_MAX - size_accum > size_item) {
size_accum += size_item;
} <span style="color: #a020f0;">else</span> {
fclose(f);
*size_max = -1L;
<span style="color: #a020f0;">return</span> TREXIO_INT_SIZE_OVERFLOW;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Overwrite the value at the input address and return TREXIO_SUCCESS </span>*/
*size_max = size_accum;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_write_determinant_list</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span>* <span style="color: #a0522d;">list</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (list == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_5;
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">determinant_list_file_name</span>[256] = <span style="color: #8b2252;">"/determinant_list.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, determinant_list_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(determinant_list_file_name));
/* <span style="color: #b22222;">Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"a"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < dims[0]; ++i) {
/* <span style="color: #b22222;">The loop below is needed to write a line with int bit fields for alpha and beta electrons </span>*/
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">j</span>=0; j < (<span style="color: #228b22;">uint32_t</span>) dims[1]; ++j) {
rc = fprintf(f, <span style="color: #8b2252;">"%10"</span> PRId64 <span style="color: #8b2252;">" "</span>, *(list + i*dims[1] + j));
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
}
fprintf(f, <span style="color: #8b2252;">"%s"</span>, <span style="color: #8b2252;">"\n"</span>);
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Additional part for the trexio_text_has_group to work </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">det_file_name</span>[256] = <span style="color: #8b2252;">"/determinant.txt"</span>;
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, det_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(det_file_name));
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span> = trexio_text_file_exists(file_full_path);
/* <span style="color: #b22222;">Create an empty file for the trexio_text_has_group to work </span>*/
<span style="color: #a020f0;">if</span> (!file_exists) {
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span> = fopen(file_full_path, <span style="color: #8b2252;">"ab+"</span>);
fclose(fp);
}
/* <span style="color: #b22222;">Exit upon success </span>*/
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_write_determinant_coefficient</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset_file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint64_t</span>* <span style="color: #a0522d;">dims</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">double</span>* <span style="color: #a0522d;">coeff</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (coeff == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_5;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">coeff_file_name</span>[256];
memset(coeff_file_name, 0, <span style="color: #a020f0;">sizeof</span>(coeff_file_name));
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">trexio_state</span> = file->state;
<span style="color: #a020f0;">if</span> (trexio_state != 0) {
sprintf(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient_state_%"</span> PRId32 <span style="color: #8b2252;">".txt"</span>, trexio_state);
} <span style="color: #a020f0;">else</span> {
strncpy(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient.txt"</span>, 32);
}
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* <span style="color: #b22222;">Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes </span>*/
<span style="color: #228b22;">FILE</span>* <span style="color: #a0522d;">f</span> = fopen(file_full_path, <span style="color: #8b2252;">"a"</span>);
<span style="color: #a020f0;">if</span> (f == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0UL; i < dims[0]; ++i) {
rc = fprintf(f, <span style="color: #8b2252;">"%24.16e\n"</span>, *(coeff + i));
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Append .size to the file_full_path in order to write additional info about the written buffer of data </span>*/
strncat(file_full_path, <span style="color: #8b2252;">".size"</span>, 6);
/* <span style="color: #b22222;">Open the new file in "a" (append) mode to append info about the buffer that has been just written </span>*/
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">f_wSize</span> = fopen(file_full_path, <span style="color: #8b2252;">"a"</span>);
<span style="color: #a020f0;">if</span> (f_wSize == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Write the buffer_size </span>*/
rc = fprintf(f_wSize, <span style="color: #8b2252;">"%"</span> PRIu64 <span style="color: #8b2252;">"\n"</span>, dims[0]);
<span style="color: #a020f0;">if</span> (rc <= 0) {
fclose(f_wSize);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
/* <span style="color: #b22222;">Close the TXT file </span>*/
rc = fclose(f_wSize);
<span style="color: #a020f0;">if</span> (rc != 0) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
/* <span style="color: #b22222;">Additional part for the trexio_text_has_group to work </span>*/
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">det_file_name</span>[256] = <span style="color: #8b2252;">"/determinant.txt"</span>;
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, det_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(det_file_name));
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">file_exists</span> = trexio_text_file_exists(file_full_path);
/* <span style="color: #b22222;">Create an empty file for the trexio_text_has_group to work </span>*/
<span style="color: #a020f0;">if</span> (!file_exists) {
<span style="color: #228b22;">FILE</span> *<span style="color: #a0522d;">fp</span> = fopen(file_full_path, <span style="color: #8b2252;">"ab+"</span>);
fclose(fp);
}
/* <span style="color: #b22222;">Exit upon success </span>*/
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_has_determinant_list</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">determinant_list_file_name</span>[256] = <span style="color: #8b2252;">"/determinant_list.txt"</span>;
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, determinant_list_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(determinant_list_file_name));
/* <span style="color: #b22222;">Check the return code of access function to determine whether the file with data exists or not </span>*/
<span style="color: #a020f0;">if</span> (access(file_full_path, F_OK) == 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_text_has_determinant_coefficient</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">coeff_file_name</span>[256];
memset(coeff_file_name, 0, <span style="color: #a020f0;">sizeof</span>(coeff_file_name));
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">trexio_state</span> = file->state;
<span style="color: #a020f0;">if</span> (trexio_state != 0) {
sprintf(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient_state_%"</span> PRId32 <span style="color: #8b2252;">".txt"</span>, trexio_state);
} <span style="color: #a020f0;">else</span> {
strncpy(coeff_file_name, <span style="color: #8b2252;">"/determinant_coefficient.txt"</span>, 32);
}
/* <span style="color: #b22222;">The full path to the destination TXT file with sparse data. This will include TREXIO directory name. </span>*/
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_full_path</span>[TREXIO_MAX_FILENAME_LENGTH];
/* <span style="color: #b22222;">Copy directory name in file_full_path </span>*/
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* <span style="color: #b22222;">Append name of the file with sparse data </span>*/
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* <span style="color: #b22222;">Check the return code of access function to determine whether the file with data exists or not </span>*/
<span style="color: #a020f0;">if</span> (access(file_full_path, F_OK) == 0){
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">return</span> TREXIO_HAS_NOT;
}
}
</pre>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: TREX-CoE</p>
<p class="date">Created: 2022-09-29 Thu 11:40</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>
|