File: getting_started.html

package info (click to toggle)
petsc 3.23.1%2Bdfsg1-1exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 515,576 kB
  • sloc: ansic: 751,607; cpp: 51,542; python: 38,598; f90: 17,352; javascript: 3,493; makefile: 3,157; sh: 1,502; xml: 619; objc: 445; java: 13; csh: 1
file content (2219 lines) | stat: -rw-r--r-- 235,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219

<!DOCTYPE html>


<html lang="en" data-content_root="../" >

  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Getting Started &#8212; PETSc 3.23.1 documentation</title>
  
  
  
  <script data-cfasync="false">
    document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
    document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
  </script>
  
  <!-- Loaded before other Sphinx assets -->
  <link href="../_static/styles/theme.css?digest=bd9e20870c6007c4c509" rel="stylesheet" />
<link href="../_static/styles/bootstrap.css?digest=bd9e20870c6007c4c509" rel="stylesheet" />
<link href="../_static/styles/pydata-sphinx-theme.css?digest=bd9e20870c6007c4c509" rel="stylesheet" />

  
  <link href="../_static/vendor/fontawesome/6.5.1/css/all.min.css?digest=bd9e20870c6007c4c509" rel="stylesheet" />
  <link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-regular-400.woff2" />

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=8f2a1f02" />
    <link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
    <link rel="stylesheet" type="text/css" href="../_static/sphinx-design.min.css?v=87e54e7c" />
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.css" />
    <link rel="stylesheet" type="text/css" href="../_static/katex-math.css?v=91adb8b6" />
    <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=dbe1606d" />
  
  <!-- Pre-loaded scripts that we'll load fully later -->
  <link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=bd9e20870c6007c4c509" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=bd9e20870c6007c4c509" />
  <script src="../_static/vendor/fontawesome/6.5.1/js/all.min.js?digest=bd9e20870c6007c4c509"></script>

    <script src="../_static/documentation_options.js?v=34da53a5"></script>
    <script src="../_static/doctools.js?v=9a2dae69"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/clipboard.min.js?v=a7894cd8"></script>
    <script src="../_static/copybutton.js?v=a56c686a"></script>
    <script src="../_static/design-tabs.js?v=f930bc37"></script>
    <script src="../_static/katex.min.js?v=be8ff15f"></script>
    <script src="../_static/auto-render.min.js?v=ad136472"></script>
    <script src="../_static/katex_autorenderer.js?v=bebc588a"></script>
    <script>DOCUMENTATION_OPTIONS.pagename = 'manual/getting_started';</script>
    <link rel="icon" href="../_static/petsc_favicon.png"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="The Solvers in PETSc/TAO" href="programming.html" />
    <link rel="prev" title="About This Manual" href="about_this_manual.html" />
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <meta name="docsearch:language" content="en"/>
    <meta name="docbuild:last-update" content="2025-04-30T13:10:40-0500 (v3.23.1)"/>
  </head>
  
  
  <body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">

  
  
  <a id="pst-skip-link" class="skip-link" href="#main-content">Skip to main content</a>
  
  <div id="pst-scroll-pixel-helper"></div>

  
  <button type="button" class="btn rounded-pill" id="pst-back-to-top">
    <i class="fa-solid fa-arrow-up"></i>
    Back to top
  </button>

  
  <input type="checkbox"
          class="sidebar-toggle"
          name="__primary"
          id="__primary"/>
  <label class="overlay overlay-primary" for="__primary"></label>
  
  <input type="checkbox"
          class="sidebar-toggle"
          name="__secondary"
          id="__secondary"/>
  <label class="overlay overlay-secondary" for="__secondary"></label>
  
  <div class="search-button__wrapper">
    <div class="search-button__overlay"></div>
    <div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
      action="../search.html"
      method="get">
  <i class="fa-solid fa-magnifying-glass"></i>
  <input type="search"
         class="form-control"
         name="q"
         id="search-input"
         placeholder="Search the docs ..."
         aria-label="Search the docs ..."
         autocomplete="off"
         autocorrect="off"
         autocapitalize="off"
         spellcheck="false"/>
  <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
  </div>

  <header>
  
    <div class="bd-header navbar navbar-expand-lg bd-navbar">
<div class="bd-header__inner bd-page-width">
  <label class="sidebar-toggle primary-toggle" for="__primary">
    <span class="fa-solid fa-bars"></span>
  </label>
  
  
  <div class="col-lg-3 navbar-header-items__start">
    
      <div class="navbar-item">

  

<a class="navbar-brand logo" href="../index.html">
  
  
  
  
  
    
    
      
    
    
    <img src="../_static/PETSc-TAO_RGB.svg" class="logo__image only-light" alt="PETSc 3.23.1 documentation - Home"/>
    <script>document.write(`<img src="../_static/PETSc-TAO_RGB_white.svg" class="logo__image only-dark" alt="PETSc 3.23.1 documentation - Home"/>`);</script>
  
  
</a></div>
    
  </div>
  
  <div class="col-lg-9 navbar-header-items">
    
    <div class="me-auto navbar-header-items__center">
      
        <div class="navbar-item">
<nav class="navbar-nav">
  <ul class="bd-navbar-elements navbar-nav">
    
                    <li class="nav-item current active">
                      <a class="nav-link nav-internal" href="../overview/index.html">
                        Overview
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../install/index.html">
                        Install
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../tutorials/index.html">
                        Tutorials
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="index.html">
                        User-Guide
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../manualpages/index.html">
                        C/Fortran API
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../petsc4py/index.html">
                        petsc4py API
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../faq/index.html">
                        FAQ
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../community/index.html">
                        Community
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../developers/index.html">
                        Developers
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../miscellaneous/index.html">
                        Misc.
                      </a>
                    </li>
                
  </ul>
</nav></div>
      
    </div>
    
    
    <div class="navbar-header-items__end">
      
        <div class="navbar-item navbar-persistent--container">
          

 <script>
 document.write(`
   <button class="btn navbar-btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
    <i class="fa-solid fa-magnifying-glass"></i>
    <span class="search-button__default-text">Search</span>
    <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
   </button>
 `);
 </script>
        </div>
      
      
        <div class="navbar-item">

<script>
document.write(`
  <button class="btn btn-sm navbar-btn theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
    <span class="theme-switch nav-link" data-mode="light"><i class="fa-solid fa-sun fa-lg"></i></span>
    <span class="theme-switch nav-link" data-mode="dark"><i class="fa-solid fa-moon fa-lg"></i></span>
    <span class="theme-switch nav-link" data-mode="auto"><i class="fa-solid fa-circle-half-stroke fa-lg"></i></span>
  </button>
`);
</script></div>
      
        <div class="navbar-item"><ul class="navbar-icon-links navbar-nav"
    aria-label="Icon Links">
        <li class="nav-item">
          
          
          
          
          
          
          
          
          <a href="https://gitlab.com/petsc/petsc" title="GitLab" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fab fa-gitlab fa-lg" aria-hidden="true"></i></span>
            <span class="sr-only">GitLab</span></a>
        </li>
</ul></div>
      
    </div>
    
  </div>
  
  
    <div class="navbar-persistent--mobile">

 <script>
 document.write(`
   <button class="btn navbar-btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
    <i class="fa-solid fa-magnifying-glass"></i>
    <span class="search-button__default-text">Search</span>
    <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
   </button>
 `);
 </script>
    </div>
  

  
    <label class="sidebar-toggle secondary-toggle" for="__secondary" tabindex="0">
      <span class="fa-solid fa-outdent"></span>
    </label>
  
</div>

    </div>
  
  </header>

  <div class="bd-container">
    <div class="bd-container__inner bd-page-width">
      
      
      
      <div class="bd-sidebar-primary bd-sidebar">
        

  
  <div class="sidebar-header-items sidebar-primary__section">
    
    
      <div class="sidebar-header-items__center">
        
          <div class="navbar-item">
<nav class="navbar-nav">
  <ul class="bd-navbar-elements navbar-nav">
    
                    <li class="nav-item current active">
                      <a class="nav-link nav-internal" href="../overview/index.html">
                        Overview
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../install/index.html">
                        Install
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../tutorials/index.html">
                        Tutorials
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="index.html">
                        User-Guide
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../manualpages/index.html">
                        C/Fortran API
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../petsc4py/index.html">
                        petsc4py API
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../faq/index.html">
                        FAQ
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../community/index.html">
                        Community
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../developers/index.html">
                        Developers
                      </a>
                    </li>
                

                    <li class="nav-item">
                      <a class="nav-link nav-internal" href="../miscellaneous/index.html">
                        Misc.
                      </a>
                    </li>
                
  </ul>
</nav></div>
        
      </div>
    
    
    
      <div class="sidebar-header-items__end">
        
          <div class="navbar-item">

<script>
document.write(`
  <button class="btn btn-sm navbar-btn theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
    <span class="theme-switch nav-link" data-mode="light"><i class="fa-solid fa-sun fa-lg"></i></span>
    <span class="theme-switch nav-link" data-mode="dark"><i class="fa-solid fa-moon fa-lg"></i></span>
    <span class="theme-switch nav-link" data-mode="auto"><i class="fa-solid fa-circle-half-stroke fa-lg"></i></span>
  </button>
`);
</script></div>
        
          <div class="navbar-item"><ul class="navbar-icon-links navbar-nav"
    aria-label="Icon Links">
        <li class="nav-item">
          
          
          
          
          
          
          
          
          <a href="https://gitlab.com/petsc/petsc" title="GitLab" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fab fa-gitlab fa-lg" aria-hidden="true"></i></span>
            <span class="sr-only">GitLab</span></a>
        </li>
</ul></div>
        
      </div>
    
  </div>
  
    <div class="sidebar-primary-items__start sidebar-primary__section">
        <div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
     aria-label="Section Navigation">
  <p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
  <div class="bd-toc-item navbar-nav"><ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../overview/nutshell.html">PETSc in a nutshell</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/features.html">Supported Systems</a></li>

<li class="toctree-l1"><a class="reference internal" href="../overview/gpu_roadmap.html">GPU Support Roadmap</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/vector_table.html">Summary of Vector Types Available In PETSc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/matrix_table.html">Summary of Matrix Types Available In PETSc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/linear_solve_table.html">Summary of Sparse Linear Solvers Available In PETSc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/nonlinear_solve_table.html">Summary of Nonlinear Solvers Available In PETSc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/integrator_table.html">Summary of Time Integrators Available In PETSc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/tao_solve_table.html">Summary of Tao Solvers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/discrete_table.html">Summary of Discretization Management Systems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/plex_transform_table.html">Summary of Unstructured Mesh Transformations</a></li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="index.html">User-Guide</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-1"><i class="fa-solid fa-chevron-down"></i></label><ul class="current">
<li class="toctree-l2 current active has-children"><a class="reference internal" href="introduction.html">Introduction to PETSc</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-2"><i class="fa-solid fa-chevron-down"></i></label><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="about_this_manual.html">About This Manual</a></li>
<li class="toctree-l3 current active"><a class="current reference internal" href="#">Getting Started</a></li>






</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="programming.html">The Solvers in PETSc/TAO</a><input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-3"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="vec.html">Vectors and Parallel Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="mat.html">Matrices</a></li>
<li class="toctree-l3"><a class="reference internal" href="ksp.html">KSP: Linear System Solvers</a></li>
<li class="toctree-l3"><a class="reference internal" href="snes.html">SNES: Nonlinear Solvers</a></li>
<li class="toctree-l3"><a class="reference internal" href="ts.html">TS: Scalable ODE and DAE Solvers</a></li>

<li class="toctree-l3"><a class="reference internal" href="tao.html">TAO: Optimization Solvers</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="dm.html">DM: Interfacing Between Solvers and Models/Discretizations</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-4"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="dmbase.html">DM Basics</a></li>
<li class="toctree-l3"><a class="reference internal" href="section.html">PetscSection: Connecting Grids to Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="dmplex.html">DMPlex: Unstructured Grids</a></li>
<li class="toctree-l3"><a class="reference internal" href="dmstag.html">DMSTAG: Staggered, Structured Grid</a></li>
<li class="toctree-l3"><a class="reference internal" href="dmnetwork.html">Networks</a></li>
<li class="toctree-l3"><a class="reference internal" href="dt.html">PetscDT: Discretization Technology in PETSc</a></li>
<li class="toctree-l3"><a class="reference internal" href="fe.html">PetscFE: Finite Element Infrastructure in PETSc</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="additional.html">Additional Information</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-5"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="fortran.html">PETSc for Fortran Users</a></li>
<li class="toctree-l3"><a class="reference internal" href="versionchecking.html">Checking the PETSc version</a></li>
<li class="toctree-l3"><a class="reference internal" href="matlab.html">Using MATLAB with PETSc</a></li>
<li class="toctree-l3"><a class="reference internal" href="profiling.html">Profiling</a></li>
<li class="toctree-l3"><a class="reference internal" href="performance.html">Hints for Performance Tuning</a></li>
<li class="toctree-l3"><a class="reference internal" href="streams.html">STREAMS: Example Study</a></li>
<li class="toctree-l3"><a class="reference internal" href="blas-lapack.html">The Use of BLAS and LAPACK in PETSc and external libraries</a></li>
<li class="toctree-l3"><a class="reference internal" href="other.html">Other PETSc Features</a></li>

<li class="toctree-l3"><a class="reference internal" href="advanced.html">Advanced Features of Matrices and Solvers</a></li>
<li class="toctree-l3"><a class="reference internal" href="tests.html">Running PETSc Tests</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../manualpages/index.html">C/Fortran API</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-6"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Vector.html">Vectors and Index Sets</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-7"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Vec/index.html">Vector Operations (Vec)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/IS/index.html">Index sets (IS)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Matrix.html">Matrices and Matrix Operations</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-8"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Mat/index.html">Matrix Operations (Mat)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/MatGraphOperations/index.html">Matrix colorings (MatColoring), orderings (MatOrdering), partitionings (MatPartitioning), and coarsening (MatCoarsen)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/MatFD/index.html">Finite difference computation of Jacobians (MatFD)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/DataLayout.html">Data Layout and Communication</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-9"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/PetscSF/index.html">Star Forest Communication (PetscSF)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/PetscSection/index.html">Section Data Layout (PetscSection)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/AO/index.html">Application Orderings (AO)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/DataManagement.html">Data Management between Vec and Mat, and Distributed Mesh Data Structures</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-10"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DM/index.html">Data Management (DM)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMDA/index.html">Structured Grids (DMDA)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMStag/index.html">Staggered, Structured Grids (DMSTAG)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMPlex/index.html">Unstructured Grids and Cell Complexes (DMPLEX)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMNetwork/index.html">Graphs and Networks (DMNETWORK)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMForest/index.html">A Forest of Trees and Structured Adaptive Refinement (DMFOREST)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMPatch/index.html">Sequences of parallel mesh patches (DMPATCH)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMSwarm/index.html">Particle Discretizations (DMSWARM)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMMOAB/index.html">MOAB Mesh Representation (DMMOAB)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMLabel/index.html">Selecting Parts of Meshes (DMLabel)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMPRODUCT/index.html">Tensor products of meshes (DMRODUCT)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DMComposite/index.html">DMComposite</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Discretization.html">Discretization and Function Spaces</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-11"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DT/index.html">Discretization Technology and Quadrature (DT)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/SPACE/index.html">Function Spaces (PetscSpace)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/DUALSPACE/index.html">Dual Spaces (PetscDualSpace)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/FE/index.html">Finite Elements (PetscFE)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/FV/index.html">Finite Volumes (PetscFV)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/PF/index.html">Defining your own mathematical functions (PF)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/LANDAU/index.html">Landau Collision Operator</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/LinearSolvers.html">Linear Solvers and Preconditioners</a><input class="toctree-checkbox" id="toctree-checkbox-12" name="toctree-checkbox-12" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-12"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/KSP/index.html">Linear Solvers and Krylov Methods (KSP)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/PC/index.html">Preconditioners (PC)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/NonlinearSolvers.html">Nonlinear Solvers</a><input class="toctree-checkbox" id="toctree-checkbox-13" name="toctree-checkbox-13" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-13"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/SNES/index.html">Nonlinear Solvers (SNES)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/SNESFAS/index.html">Full Approximation Scheme (FAS) nonlinear multigrid</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Timestepping.html">Forward and Adjoint Timestepping</a><input class="toctree-checkbox" id="toctree-checkbox-14" name="toctree-checkbox-14" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-14"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/TS/index.html">Time Stepping ODE and DAE Solvers (TS)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Sensitivity/index.html">Sensitivity Analysis for ODE and DAE</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Characteristic/index.html">Semi-Lagrangian Solves using the Method of Characteristics</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Optimization.html">Optimization</a><input class="toctree-checkbox" id="toctree-checkbox-15" name="toctree-checkbox-15" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-15"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Tao/index.html">Optimization Solvers (Tao)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/TaoLineSearch/index.html">Optimization Line Search (TaoLineSearch)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/Visualization.html">Graphics and Visualization</a><input class="toctree-checkbox" id="toctree-checkbox-16" name="toctree-checkbox-16" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-16"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Draw/index.html">Graphics (Draw)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Viewer/index.html">Viewing Objects (Viewer)</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../manualpages/System.html">System Routines, Profiling, Data Structures</a><input class="toctree-checkbox" id="toctree-checkbox-17" name="toctree-checkbox-17" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-17"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Sys/index.html">PETSc Options, IO, and System Utilities</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/PetscH/index.html">Hash Tables</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Log/index.html">Profiling and Logging</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Device/index.html">Device</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Matlab/index.html">Matlab</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/Bag/index.html">Bag</a></li>
<li class="toctree-l3"><a class="reference internal" href="../manualpages/BM/index.html">Benchmarks (BM)</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../changes/index.html">Changes for each release</a></li>
<li class="toctree-l2"><a class="reference internal" href="../manualpages/singleindex.html">Single Index of all PETSc Manual Pages</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../changes/index.html">Changes for each release</a></li>
<li class="toctree-l1"><a class="reference internal" href="../manualpages/singleindex.html">Single Index of all PETSc Manual Pages</a></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../overview/previous_release_docs.html">Documentation for previous PETSc releases</a><input class="toctree-checkbox" id="toctree-checkbox-18" name="toctree-checkbox-18" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-18"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.22/docs"> 3.22</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.21/docs"> 3.21</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.20/docs"> 3.20</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.19/docs"> 3.19</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.18/docs"> 3.18</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.17/docs"> 3.17</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.16/docs"> 3.16</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.15/docs"> 3.15</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.14/docs"> 3.14</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.13/docs"> 3.13</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.12/docs"> 3.12</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.11/docs"> 3.11</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.10/docs"> 3.10</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.9/docs"> 3.9</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.8/docs"> 3.8</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.7/docs"> 3.7</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.6/docs"> 3.6</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.5/docs"> 3.5</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.4/docs"> 3.4</a></li>
<li class="toctree-l2"><a class="reference external" href="https://web.cels.anl.gov/projects/petsc/vault/petsc-3.3/docs"> 3.3</a></li>
</ul>
</li>
</ul>
</div>
</nav></div>
    </div>
  
  
  <div class="sidebar-primary-items__end sidebar-primary__section">
  </div>
  
  <div id="rtd-footer-container"></div>


      </div>
      
      <main id="main-content" class="bd-main">
        
        
          <div class="bd-content">
            <div class="bd-article-container">
              
              <div class="bd-header-article">
<div class="header-article-items header-article__inner">
  
    <div class="header-article-items__start">
      
        <div class="header-article-item">





<nav aria-label="Breadcrumb">
  <ul class="bd-breadcrumbs">
    
    <li class="breadcrumb-item breadcrumb-home">
      <a href="../index.html" class="nav-link" aria-label="Home">
        <i class="fa-solid fa-home"></i>
      </a>
    </li>
    
    <li class="breadcrumb-item"><a href="../overview/index.html" class="nav-link">Overview</a></li>
    
    
    <li class="breadcrumb-item"><i class="fa-solid fa-ellipsis"></i></li>
    
    
    <li class="breadcrumb-item"><a href="introduction.html" class="nav-link">Introduction to PETSc</a></li>
    
    <li class="breadcrumb-item active" aria-current="page">Getting Started</li>
  </ul>
</nav>
</div>
      
    </div>
  
  
</div>
</div>
              
              
              
                
<div id="searchbox"></div>
                <article class="bd-article">
                  
  <section class="tex2jax_ignore mathjax_ignore" id="getting-started">
<span id="sec-getting-started"></span><h1>Getting Started<a class="headerlink" href="#getting-started" title="Link to this heading">#</a></h1>
<p>PETSc consists of a collection of classes,
which are discussed in detail in later parts of the manual (<a class="reference internal" href="programming.html"><span class="doc">The Solvers in PETSc/TAO</span></a> and <a class="reference internal" href="additional.html"><span class="doc">Additional Information</span></a>).
The important PETSc classes include</p>
<ul class="simple">
<li><p>index sets (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/IS/IS.html">IS</a></span></code>), for indexing into
vectors, renumbering, permuting, etc;</p></li>
<li><p><a class="reference internal" href="vec.html#ch-vectors"><span class="std std-ref">Vectors and Parallel Data</span></a> (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/Vec.html">Vec</a></span></code>);</p></li>
<li><p>(generally sparse) <a class="reference internal" href="mat.html#ch-matrices"><span class="std std-ref">Matrices</span></a> (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/Mat.html">Mat</a></span></code>)</p></li>
<li><p><a class="reference internal" href="ksp.html#ch-ksp"><span class="std std-ref">KSP: Linear System Solvers</span></a> (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code>);</p></li>
<li><p>preconditioners, including multigrid, block solvers, patch solvers, and
sparse direct solvers (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/PC/PC.html">PC</a></span></code>);</p></li>
<li><p><a class="reference internal" href="snes.html#ch-snes"><span class="std std-ref">SNES: Nonlinear Solvers</span></a> (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code>);</p></li>
<li><p><a class="reference internal" href="ts.html#ch-ts"><span class="std std-ref">TS: Scalable ODE and DAE Solvers</span></a> for solving time-dependent (nonlinear) PDEs, including
support for differential-algebraic-equations, and the computation of
adjoints (sensitivities/gradients of the solutions) (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TS.html">TS</a></span></code>);</p></li>
<li><p>scalable <a class="reference internal" href="tao.html#ch-tao"><span class="std std-ref">TAO: Optimization Solvers</span></a> including a rich set of gradient-based optimizers,
Newton-based optimizers and optimization with constraints (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Tao/Tao.html">Tao</a></span></code>).</p></li>
<li><p><a class="reference internal" href="dmbase.html#ch-dmbase"><span class="std std-ref">DM Basics</span></a> code for managing interactions between mesh data structures and vectors,
matrices, and solvers (<code class="docutils notranslate"><span class="pre"><a href="../manualpages/DM/DM.html">DM</a></span></code>);</p></li>
</ul>
<p>Each class consists of an abstract interface (simply a set of calling
sequences corresponding to an abstract base class in C++) and an implementation for each algorithm and data structure.
This design enables easy comparison and use of different
algorithms (for example, experimenting with different Krylov subspace
methods, preconditioners, or truncated Newton methods). Hence, PETSc
provides a rich environment for modeling scientific applications as well
as for rapid algorithm design and prototyping.</p>
<p>The classes enable easy customization and extension of both algorithms
and implementations. This approach promotes code reuse and flexibility.
The PETSc infrastructure creates a foundation for building large-scale
applications.</p>
<p>It is useful to consider the interrelationships among different pieces
of PETSc. <a class="reference internal" href="#fig-library"><span class="std std-ref">Numerical Libraries in PETSc</span></a> is a diagram of some
of these pieces. The figure illustrates the library’s hierarchical
organization, enabling users to employ the most appropriate solvers for a particular problem.</p>
<figure class="align-default" id="fig-library">
<img alt="PETSc numerical libraries" src="../_images/library_structure.svg" /><figcaption>
<p><span class="caption-number">Fig. 1 </span><span class="caption-text">Numerical Libraries in PETSc</span><a class="headerlink" href="#fig-library" title="Link to this image">#</a></p>
</figcaption>
</figure>
<section id="suggested-reading">
<h2>Suggested Reading<a class="headerlink" href="#suggested-reading" title="Link to this heading">#</a></h2>
<p>The manual is divided into four parts:</p>
<ul class="simple">
<li><p><a class="reference internal" href="introduction.html"><span class="doc">Introduction to PETSc</span></a></p></li>
<li><p><a class="reference internal" href="programming.html"><span class="doc">The Solvers in PETSc/TAO</span></a></p></li>
<li><p><a class="reference internal" href="dm.html"><span class="doc">DM: Interfacing Between Solvers and Models/Discretizations</span></a></p></li>
<li><p><a class="reference internal" href="additional.html"><span class="doc">Additional Information</span></a></p></li>
</ul>
<p><a class="reference internal" href="introduction.html"><span class="doc">Introduction to PETSc</span></a> describes the basic procedure for using the PETSc library and
presents simple examples of solving linear systems with PETSc. This
section conveys the typical style used throughout the library and
enables the application programmer to begin using the software
immediately.</p>
<p><a class="reference internal" href="programming.html"><span class="doc">The Solvers in PETSc/TAO</span></a> explains in detail the use of the various PETSc algebraic objects, such
as vectors, matrices, index sets, and PETSc solvers, including linear and nonlinear solvers, time integrators,
and optimization support.</p>
<p><a class="reference internal" href="dm.html"><span class="doc">DM: Interfacing Between Solvers and Models/Discretizations</span></a> details how a user’s models and discretizations can easily be interfaced with the
solvers by using the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/DM/DM.html">DM</a></span></code> construct.</p>
<p><a class="reference internal" href="additional.html"><span class="doc">Additional Information</span></a> describes a variety of useful information, including
profiling, the options database, viewers, error handling, and some
details of PETSc design.</p>
<p><a class="reference external" href="https://code.visualstudio.com/">Visual Studio Code</a>, Eclipse, Emacs, and Vim users may find their development environment’s options for
searching in the source code are
useful for exploring the PETSc source code. Details of this feature are provided in <a class="reference internal" href="other.html#sec-developer-environments"><span class="std std-ref">Developer Environments</span></a>.</p>
<p><strong>Note to Fortran Programmers</strong>: In most of the manual, the examples and calling sequences are given
for the C/C++ family of programming languages. However, Fortran
programmers can use all of the functionality of PETSc from Fortran,
with only minor differences in the user interface.
<a class="reference internal" href="fortran.html#ch-fortran"><span class="std std-ref">PETSc for Fortran Users</span></a> provides a discussion of the differences between
using PETSc from Fortran and C, as well as several complete Fortran
examples.</p>
<p><strong>Note to Python Programmers</strong>: To program with PETSc in Python, you need to enable Python bindings
(i.e. petsc4py) with the configure option <code class="docutils notranslate"><span class="pre">--with-petsc4py=1</span></code>. See the
<a class="reference internal" href="../install/index.html"><span class="doc">PETSc installation guide</span></a>
for more details.</p>
</section>
<section id="running-petsc-programs">
<span id="sec-running"></span><h2>Running PETSc Programs<a class="headerlink" href="#running-petsc-programs" title="Link to this heading">#</a></h2>
<p>Before using PETSc, the user must first set the environmental variable
<code class="docutils notranslate"><span class="pre">PETSC_DIR</span></code> to indicate the full path of the PETSc home directory. For
example, under the Unix bash shell, a command of the form</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nb">export</span><span class="w"> </span><span class="nv">PETSC_DIR</span><span class="o">=</span><span class="nv">$HOME</span>/petsc
</pre></div>
</div>
<p>can be placed in the user’s <code class="docutils notranslate"><span class="pre">.bashrc</span></code> or other startup file. In
addition, the user may need to set the environment variable
<code class="docutils notranslate"><span class="pre">$PETSC_ARCH</span></code> to specify a particular configuration of the PETSc
libraries. Note that <code class="docutils notranslate"><span class="pre">$PETSC_ARCH</span></code> is just a name selected by the
installer to refer to the libraries compiled for a particular set of
compiler options and machine type. Using different values of
<code class="docutils notranslate"><span class="pre">$PETSC_ARCH</span></code> allows one to switch between several different sets (say
debug and optimized versions) of libraries easily. To determine if you need to
set <code class="docutils notranslate"><span class="pre">$PETSC_ARCH</span></code>, look in the directory indicated by <code class="docutils notranslate"><span class="pre">$PETSC_DIR</span></code>, if
there are subdirectories beginning with <code class="docutils notranslate"><span class="pre">arch</span></code> then those
subdirectories give the possible values for <code class="docutils notranslate"><span class="pre">$PETSC_ARCH</span></code>.</p>
<p>See <a class="reference internal" href="../tutorials/handson.html#handson"><span class="std std-ref">Tutorials, by Mathematical Problem</span></a> to immediately jump in and run PETSc code.</p>
<p>All PETSc programs use the MPI (Message Passing Interface) standard for
message-passing communication <span id="id1">[<a class="reference internal" href="#id1190" title="MPI Forum. MPI: a message-passing interface standard. International J. Supercomputing Applications, 1994.">For94</a>]</span>. Thus, to
execute PETSc programs, users must know the procedure for beginning MPI
jobs on their selected computer system(s). For instance, when using the
<a class="reference external" href="https://www.mpich.org/">MPICH</a> implementation of MPI and many
others, the following command initiates a program that uses eight
processors:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>mpiexec<span class="w"> </span>-n<span class="w"> </span><span class="m">8</span><span class="w"> </span>./petsc_program_name<span class="w"> </span>petsc_options
</pre></div>
</div>
<p>PETSc also provides a script that automatically uses the correct
<code class="docutils notranslate"><span class="pre">mpiexec</span></code> for your configuration.</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nv">$PETSC_DIR</span>/lib/petsc/bin/petscmpiexec<span class="w"> </span>-n<span class="w"> </span><span class="m">8</span><span class="w"> </span>./petsc_program_name<span class="w"> </span>petsc_options
</pre></div>
</div>
<p>Certain options are supported by all PETSc programs. We list a few
particularly useful ones below; a complete list can be obtained by
running any PETSc program with the option <code class="docutils notranslate"><span class="pre">-help</span></code>.</p>
<ul class="simple">
<li><p><code class="docutils notranslate"><span class="pre">-log_view</span></code> - summarize the program’s performance (see <a class="reference internal" href="profiling.html#ch-profiling"><span class="std std-ref">Profiling</span></a>)</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-fp_trap</span></code> - stop on floating-point exceptions; for example divide
by zero</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-malloc_dump</span></code> - enable memory tracing; dump list of unfreed memory
at conclusion of the run, see
<a class="reference internal" href="performance.html#detecting-memory-problems"><span class="std std-ref">Detecting Memory Allocation Problems and Memory Usage</span></a>,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-malloc_debug</span></code> - enable memory debugging (by default, this is
activated for the debugging version of PETSc), see
<a class="reference internal" href="performance.html#detecting-memory-problems"><span class="std std-ref">Detecting Memory Allocation Problems and Memory Usage</span></a>,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-start_in_debugger</span></code> <code class="docutils notranslate"><span class="pre">[noxterm,gdb,lldb]</span></code>
<code class="docutils notranslate"><span class="pre">[-display</span> <span class="pre">name]</span></code> - start all (or a subset of the) processes in a debugger. See
<a class="reference internal" href="other.html#sec-debugging"><span class="std std-ref">Debugging</span></a>, for more information on
debugging PETSc programs.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-on_error_attach_debugger</span></code> <code class="docutils notranslate"><span class="pre">[noxterm,gdb,lldb]</span></code>
<code class="docutils notranslate"><span class="pre">[-display</span> <span class="pre">name]</span></code> - start debugger only on encountering an error</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-info</span></code> - print a great deal of information about what the program
is doing as it runs</p></li>
<li><p><code class="docutils notranslate"><span class="pre">-version</span></code> - display the version of PETSc being used</p></li>
</ul>
</section>
<section id="writing-petsc-programs">
<span id="sec-writing"></span><h2>Writing PETSc Programs<a class="headerlink" href="#writing-petsc-programs" title="Link to this heading">#</a></h2>
<p>Most PETSc programs begin with a call to</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a></span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">argc</span><span class="p">,</span><span class="kt">char</span><span class="w"> </span><span class="o">***</span><span class="n">argv</span><span class="p">,</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">file</span><span class="p">,</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">help</span><span class="p">);</span>
</pre></div>
</div>
<p>which initializes PETSc and MPI. The arguments <code class="docutils notranslate"><span class="pre">argc</span></code> and <code class="docutils notranslate"><span class="pre">argv</span></code> are
the usual command line arguments in C and C++ programs. The
argument <code class="docutils notranslate"><span class="pre">file</span></code> optionally indicates an alternative name for the PETSc
options file, <code class="docutils notranslate"><span class="pre">.petscrc</span></code>, which resides by default in the user’s home
directory. <a class="reference internal" href="other.html#sec-options"><span class="std std-ref">Runtime Options</span></a> provides details
regarding this file and the PETSc options database, which can be used
for runtime customization. The final argument, <code class="docutils notranslate"><span class="pre">help</span></code>, is an optional
character string that will be printed if the program is run with the
<code class="docutils notranslate"><span class="pre">-help</span></code> option. In Fortran, the initialization command has the form</p>
<div class="highlight-fortran notranslate"><div class="highlight"><pre><span></span><span class="k">call </span><span class="n"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a></span><span class="p">(</span><span class="kt">character</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">file</span><span class="p">,</span><span class="kt">integer </span><span class="n">ierr</span><span class="p">)</span>
</pre></div>
</div>
<p>where the file argument is optional.</p>
<p><code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a>()</span></code> automatically calls <code class="docutils notranslate"><span class="pre"><a href="http://www.mpich.org/static/docs/latest/www3/MPI_Init.html#MPI_Init">MPI_Init</a>()</span></code> if MPI has not
been not previously initialized. In certain circumstances in which MPI
needs to be initialized directly (or is initialized by some other
library), the user can first call <code class="docutils notranslate"><span class="pre"><a href="http://www.mpich.org/static/docs/latest/www3/MPI_Init.html#MPI_Init">MPI_Init</a>()</span></code> (or have the other
library do it), and then call <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a>()</span></code>. By default,
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a>()</span></code> sets the PETSc “world” communicator
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span></code> to <code class="docutils notranslate"><span class="pre">MPI_COMM_WORLD</span></code>.</p>
<p>For those unfamiliar with MPI, a <em>communicator</em> indicates
a collection of processes that will be involved in a
calculation or communication. Communicators have the variable type
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span></code>. In most cases, users can employ the communicator
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span></code> to indicate all processes in a given run and
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span></code> to indicate a single process.</p>
<p>MPI provides routines for generating new communicators consisting of
subsets of processors, though most users rarely need to use these. The
book <em>Using MPI</em>, by Lusk, Gropp, and Skjellum
<span id="id2">[<a class="reference internal" href="#id1269" title="William Gropp, Ewing Lusk, and Anthony Skjellum. Using MPI: Portable Parallel Programming with the Message Passing Interface. MIT Press, 1994.">GLS94</a>]</span> provides an excellent introduction to the
concepts in MPI. See also the <a class="reference external" href="https://www.mcs.anl.gov/research/projects/mpi/">MPI homepage</a>.
Note that PETSc users
need not program much message passing directly with MPI, but they must
be familiar with the basic concepts of message passing and distributed
memory computing.</p>
<p>All PETSc programs should call <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a>()</span></code> as their final (or
nearly final) statement. This routine handles options to be called at the conclusion of the
program and calls <code class="docutils notranslate"><span class="pre"><a href="http://www.mpich.org/static/docs/latest/www3/MPI_Finalize.html#MPI_Finalize">MPI_Finalize</a>()</span></code> if <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a>()</span></code> began
MPI. If MPI was initiated externally from PETSc (by either the user or
another software package), the user is responsible for calling
<code class="docutils notranslate"><span class="pre"><a href="http://www.mpich.org/static/docs/latest/www3/MPI_Finalize.html#MPI_Finalize">MPI_Finalize</a>()</span></code>.</p>
<section id="error-checking">
<h3>Error Checking<a class="headerlink" href="#error-checking" title="Link to this heading">#</a></h3>
<p>Most PETSc functions return a <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a></span></code>, an integer
indicating whether an error occurred during the call. The error code
is set to be nonzero if an error has been detected; otherwise, it is
zero. For the C/C++ interface, the error variable is the routine’s
return value, while for the Fortran version, each PETSc routine has an integer error variable as
its final argument.</p>
<p>One should always check these routine values as given below in the C/C++
formats, respectively:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n">PetscFunction</span><span class="p">(</span><span class="n">Args</span><span class="p">));</span>
</pre></div>
</div>
<p>or for Fortran</p>
<div class="highlight-fortran notranslate"><div class="highlight"><pre><span></span><span class="c">! within the main program</span>
<span class="n"><a href="../manualpages/Sys/PetscCallA.html">PetscCallA</a></span><span class="p">(</span><span class="n">PetscFunction</span><span class="p">(</span><span class="n">Args</span><span class="p">,</span><span class="n">ierr</span><span class="p">))</span>
</pre></div>
</div>
<div class="highlight-fortran notranslate"><div class="highlight"><pre><span></span><span class="c">! within any subroutine</span>
<span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n">PetscFunction</span><span class="p">(</span><span class="n">Args</span><span class="p">,</span><span class="n">ierr</span><span class="p">))</span>
</pre></div>
</div>
<p>These macros check the returned error code, and if it is nonzero, they call the PETSc error
handler and then return from the function with the error code. The macros above should be used on all PETSc calls to enable
a complete error traceback. See <a class="reference internal" href="#sec-error2"><span class="std std-ref">Error Checking</span></a> for more details on PETSc error handling.</p>
</section>
</section>
<section id="simple-petsc-examples">
<span id="sec-simple"></span><h2>Simple PETSc Examples<a class="headerlink" href="#simple-petsc-examples" title="Link to this heading">#</a></h2>
<p>To help the user use PETSc immediately, we begin with a simple
uniprocessor example that
solves the one-dimensional Laplacian problem with finite differences.
This sequential code illustrates the solution of
a linear system with <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code>, the interface to the preconditioners,
Krylov subspace methods and direct linear solvers of PETSc. Following
the code, we highlight a few of the most important parts of this example.</p>
<div class="admonition-listing-a-href-petsc-doc-out-root-placeholder-src-ksp-ksp-tutorials-ex1-c-html-ksp-tutorial-src-ksp-ksp-tutorials-ex1-c-a admonition" id="ksp-ex1">
<p class="admonition-title">Listing: <a href="../src/ksp/ksp/tutorials/ex1.c.html">KSP Tutorial src/ksp/ksp/tutorials/ex1.c</a></p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">help</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Solves a tridiagonal linear system with <a href="../manualpages/KSP/KSP.html">KSP</a>.</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">;</span>

<span class="cm">/*</span>
<span class="cm">  Include &quot;petscksp.h&quot; so that we can use <a href="../manualpages/KSP/KSP.html">KSP</a> solvers.  Note that this file</span>
<span class="cm">  automatically includes:</span>
<span class="cm">     petscsys.h    - base PETSc routines   petscvec.h - vectors</span>
<span class="cm">     petscmat.h    - matrices              petscpc.h  - preconditioners</span>
<span class="cm">     petscis.h     - index sets</span>
<span class="cm">     petscviewer.h - viewers</span>

<span class="cm">  Note:  The corresponding parallel example is ex23.c</span>
<span class="cm">*/</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;petscksp.h&gt;</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w">         </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">;</span><span class="w"> </span><span class="cm">/* approx solution, RHS, exact solution */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w">         </span><span class="n">A</span><span class="p">;</span><span class="w">       </span><span class="cm">/* linear system matrix */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w">         </span><span class="n">ksp</span><span class="p">;</span><span class="w">     </span><span class="cm">/* linear solver context */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/PC/PC.html">PC</a></span><span class="w">          </span><span class="n">pc</span><span class="p">;</span><span class="w">      </span><span class="cm">/* preconditioner context */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscReal.html">PetscReal</a></span><span class="w">   </span><span class="n">norm</span><span class="p">;</span><span class="w">    </span><span class="cm">/* norm of solution error */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w">    </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span><span class="w"> </span><span class="n">its</span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscMPIInt.html">PetscMPIInt</a></span><span class="w"> </span><span class="n">size</span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="n">value</span><span class="p">[</span><span class="mi">3</span><span class="p">];</span>

<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscFunctionBeginUser.html">PetscFunctionBeginUser</a></span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">help</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCallMPI.html">PetscCallMPI</a></span><span class="p">(</span><span class="n"><a href="http://www.mpich.org/static/docs/latest/www3/MPI_Comm_size.html#MPI_Comm_size">MPI_Comm_size</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">size</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCheck.html">PetscCheck</a></span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PetscErrorCode.html">PETSC_ERR_WRONG_MPI_SIZE</a></span><span class="p">,</span><span class="w"> </span><span class="s">&quot;This is a uniprocessor example only!&quot;</span><span class="p">);</span>

<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscOptionsGetInt.html">PetscOptionsGetInt</a></span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-n&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">         Compute the matrix and right-hand-side vector that define</span>
<span class="cm">         the linear system, Ax = b.</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Create vectors.  Note that we form 1 vector from scratch and</span>
<span class="cm">     then duplicate as needed.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecCreate.html">VecCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscObjectSetName.html">PetscObjectSetName</a></span><span class="p">((</span><span class="n"><a href="../manualpages/Sys/PetscObject.html">PetscObject</a></span><span class="p">)</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Solution&quot;</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSetSizes.html">VecSetSizes</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSetFromOptions.html">VecSetFromOptions</a></span><span class="p">(</span><span class="n">x</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDuplicate.html">VecDuplicate</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDuplicate.html">VecDuplicate</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">u</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Create matrix.  When using <a href="../manualpages/Mat/MatCreate.html">MatCreate</a>(), the matrix format can</span>
<span class="cm">     be specified at runtime.</span>

<span class="cm">     Performance tuning note:  For problems of substantial size,</span>
<span class="cm">     preallocation of matrix memory is crucial for attaining good</span>
<span class="cm">     performance. See the matrix chapter of the users manual for details.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatCreate.html">MatCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">A</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetSizes.html">MatSetSizes</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetFromOptions.html">MatSetFromOptions</a></span><span class="p">(</span><span class="n">A</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetUp.html">MatSetUp</a></span><span class="p">(</span><span class="n">A</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Assemble matrix</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">;</span>
<span class="w">  </span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2.0</span><span class="p">;</span>
<span class="w">  </span><span class="n">value</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">;</span>
<span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="n">col</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/INSERT_VALUES.html">INSERT_VALUES</a></span><span class="p">));</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">i</span><span class="w">      </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="w">    </span><span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/INSERT_VALUES.html">INSERT_VALUES</a></span><span class="p">));</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="n">i</span><span class="w">        </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">  </span><span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w">   </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">  </span><span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w">   </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">  </span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2.0</span><span class="p">;</span>
<span class="w">  </span><span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/INSERT_VALUES.html">INSERT_VALUES</a></span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set exact solution; then compute right-hand-side vector.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSet.html">VecSet</a></span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatMult.html">MatMult</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                Create the linear solver and set various options</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPCreate.html">KSPCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set operators. Here the matrix that defines the linear system</span>
<span class="cm">     also serves as the matrix that defines the preconditioner.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetOperators.html">KSPSetOperators</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set linear solver defaults for this problem (optional).</span>
<span class="cm">     - By extracting the <a href="../manualpages/KSP/KSP.html">KSP</a> and <a href="../manualpages/PC/PC.html">PC</a> contexts from the <a href="../manualpages/KSP/KSP.html">KSP</a> context,</span>
<span class="cm">       we can then directly call any <a href="../manualpages/KSP/KSP.html">KSP</a> and <a href="../manualpages/PC/PC.html">PC</a> routines to set</span>
<span class="cm">       various options.</span>
<span class="cm">     - The following four statements are optional; all of these</span>
<span class="cm">       parameters could alternatively be specified at runtime via</span>
<span class="cm">       <a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a>();</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PCMPIServerActive</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* cannot directly set <a href="../manualpages/KSP/KSP.html">KSP</a>/<a href="../manualpages/PC/PC.html">PC</a> options when using the MPI linear solver server */</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPGetPC.html">KSPGetPC</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">pc</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/PC/PCSetType.html">PCSetType</a></span><span class="p">(</span><span class="n">pc</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/PC/PCJACOBI.html">PCJACOBI</a></span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetTolerances.html">KSPSetTolerances</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="mf">1.</span><span class="n">e</span><span class="mi">-5</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_CURRENT.html">PETSC_CURRENT</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_CURRENT.html">PETSC_CURRENT</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_CURRENT.html">PETSC_CURRENT</a></span><span class="p">));</span>
<span class="w">  </span><span class="p">}</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">    Set runtime options, e.g.,</span>
<span class="cm">        -ksp_type &lt;type&gt; -pc_type &lt;type&gt; -ksp_monitor -ksp_rtol &lt;rtol&gt;</span>
<span class="cm">    These options will override those specified above as long as</span>
<span class="cm">    <a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a>() is called _after_ any other customization</span>
<span class="cm">    routines.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                      Solve the linear system</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                      Check the solution and clean up</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecAXPY.html">VecAXPY</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecNorm.html">VecNorm</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Vec/NORM_2.html">NORM_2</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">norm</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPGetIterationNumber.html">KSPGetIterationNumber</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">its</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscPrintf.html">PetscPrintf</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Norm of error %g, Iterations %&quot;</span><span class="w"> </span><span class="n">PetscInt_FMT</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">norm</span><span class="p">,</span><span class="w"> </span><span class="n">its</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* check that <a href="../manualpages/KSP/KSP.html">KSP</a> automatically handles the fact that the new non-zero values in the matrix are propagated to the <a href="../manualpages/KSP/KSP.html">KSP</a> solver */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatShift.html">MatShift</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Free work space.  All PETSc objects should be destroyed when they</span>
<span class="cm">     are no longer needed.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPDestroy.html">KSPDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* test if prefixes properly propagate to <a href="../manualpages/PC/PCMPI.html">PCMPI</a> objects */</span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PCMPIServerActive</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPCreate.html">KSPCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetOptionsPrefix.html">KSPSetOptionsPrefix</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;prefix_test_&quot;</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetOptionsPrefix.html">MatSetOptionsPrefix</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;prefix_test_&quot;</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetOperators.html">KSPSetOperators</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">));</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPDestroy.html">KSPDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>
<span class="w">  </span><span class="p">}</span>

<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">b</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatDestroy.html">MatDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">A</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Always call <a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a>() before exiting a program.  This routine</span>
<span class="cm">       - finalizes the PETSc libraries as well as MPI</span>
<span class="cm">       - provides summary and diagnostic information if certain runtime</span>
<span class="cm">         options are chosen (e.g., -log_view).</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a></span><span class="p">());</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

</pre></div>
</div>
</div>
<section id="include-files">
<h3>Include Files<a class="headerlink" href="#include-files" title="Link to this heading">#</a></h3>
<p>The C/C++ include files for PETSc should be used via statements such as</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;petscksp.h&gt;</span>
</pre></div>
</div>
<p>where <a href="../include/petscksp.h.html">petscksp.h</a>
is the include file for the linear solver library.
Each PETSc program must specify an include file corresponding to the
highest level PETSc objects needed within the program; all of the
required lower level include files are automatically included within the
higher level files. For example, <a href="../include/petscksp.h.html">petscksp.h</a> includes
<a href="../include/petscmat.h.html">petscmat.h</a>
(matrices),
<a href="../include/petscvec.h.html">petscvec.h</a>
(vectors), and
<a href="../include/petscsys.h.html">petscsys.h</a>
(base PETSc
file). The PETSc include files are located in the directory
<a href="../include/index.html">$PETSC_DIR/include</a>.
See <a class="reference internal" href="fortran.html#sec-fortran-includes"><span class="std std-ref">Modules and Include Files</span></a>
for a discussion of PETSc include files in Fortran programs.</p>
</section>
<section id="the-options-database">
<span id="id3"></span><h3>The Options Database<a class="headerlink" href="#the-options-database" title="Link to this heading">#</a></h3>
<p>As shown in <a class="reference internal" href="#sec-simple"><span class="std std-ref">Simple PETSc Examples</span></a>, the user can
input control data at run time using the options database. In this
example the command <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscOptionsGetInt.html">PetscOptionsGetInt</a>(NULL,NULL,&quot;-n&quot;,&amp;n,NULL);</span></code>
checks whether the user has provided a command line option to set the
value of <code class="docutils notranslate"><span class="pre">n</span></code>, the problem dimension. If so, the variable <code class="docutils notranslate"><span class="pre">n</span></code> is set
accordingly; otherwise, <code class="docutils notranslate"><span class="pre">n</span></code> remains unchanged. A complete description
of the options database may be found in <a class="reference internal" href="other.html#sec-options"><span class="std std-ref">Runtime Options</span></a>.</p>
</section>
<section id="vectors">
<span id="sec-vecintro"></span><h3>Vectors<a class="headerlink" href="#vectors" title="Link to this heading">#</a></h3>
<p>One creates a new parallel or sequential vector, <code class="docutils notranslate"><span class="pre">x</span></code>, of global
dimension <code class="docutils notranslate"><span class="pre">M</span></code> with the commands</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Vec/VecCreate.html">VecCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">);</span>
<span class="n"><a href="../manualpages/Vec/VecSetSizes.html">VecSetSizes</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">M</span><span class="p">);</span>
</pre></div>
</div>
<p>where <code class="docutils notranslate"><span class="pre">comm</span></code> denotes the MPI communicator and <code class="docutils notranslate"><span class="pre">m</span></code> is the optional
local size which may be <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span></code>. The type of storage for the
vector may be set with either calls to <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/VecSetType.html">VecSetType</a>()</span></code> or
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/VecSetFromOptions.html">VecSetFromOptions</a>()</span></code>. Additional vectors of the same type can be
formed with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Vec/VecDuplicate.html">VecDuplicate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">old</span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="o">*</span><span class="n">new</span><span class="p">);</span>
</pre></div>
</div>
<p>The commands</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Vec/VecSet.html">VecSet</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="n">value</span><span class="p">);</span>
<span class="n"><a href="../manualpages/Vec/VecSetValues.html">VecSetValues</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="o">*</span><span class="n">indices</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="o">*</span><span class="n">values</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/INSERT_VALUES.html">INSERT_VALUES</a></span><span class="p">);</span>
</pre></div>
</div>
<p>respectively set all the components of a vector to a particular scalar
value and assign a different value to each component. More detailed
information about PETSc vectors, including their basic operations,
scattering/gathering, index sets, and distributed arrays is available
in Chapter <a class="reference internal" href="vec.html#ch-vectors"><span class="std std-ref">Vectors and Parallel Data</span></a>.</p>
<p>Note the use of the PETSc variable type <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span></code> in this example.
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span></code> is defined to be <code class="docutils notranslate"><span class="pre">double</span></code> in C/C++ (or
correspondingly <code class="docutils notranslate"><span class="pre">double</span> <span class="pre">precision</span></code> in Fortran) for versions of PETSc
that have <em>not</em> been compiled for use with complex numbers. The
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span></code> data type enables identical code to be used when the
PETSc libraries have been compiled for use with complex numbers.
<a class="reference internal" href="other.html#sec-complex"><span class="std std-ref">Numbers</span></a> discusses the use of complex
numbers in PETSc programs.</p>
</section>
<section id="matrices">
<span id="sec-matintro"></span><h3>Matrices<a class="headerlink" href="#matrices" title="Link to this heading">#</a></h3>
<p>The usage of PETSc matrices and vectors is similar. The user can create a
new parallel or sequential matrix, <code class="docutils notranslate"><span class="pre">A</span></code>, which has <code class="docutils notranslate"><span class="pre">M</span></code> global rows
and <code class="docutils notranslate"><span class="pre">N</span></code> global columns, with the routines</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatCreate.html">MatCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="o">*</span><span class="n">A</span><span class="p">);</span>
<span class="n"><a href="../manualpages/Mat/MatSetSizes.html">MatSetSizes</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">M</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">N</span><span class="p">);</span>
</pre></div>
</div>
<p>where the matrix format can be specified at runtime via the options
database. The user could alternatively specify each processes’ number of
local rows and columns using <code class="docutils notranslate"><span class="pre">m</span></code> and <code class="docutils notranslate"><span class="pre">n</span></code>.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatSetSizes.html">MatSetSizes</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">m</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PETSC_DETERMINE.html">PETSC_DETERMINE</a></span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PETSC_DETERMINE.html">PETSC_DETERMINE</a></span><span class="p">);</span>
</pre></div>
</div>
<p>Generally, one then sets the “type” of the matrix, with, for example,</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatSetType.html">MatSetType</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/MATAIJ.html">MATAIJ</a></span><span class="p">);</span>
</pre></div>
</div>
<p>This causes the matrix <code class="docutils notranslate"><span class="pre">A</span></code> to use the compressed sparse row storage
format to store the matrix entries. See <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MatType.html">MatType</a></span></code> for a list of all
matrix types. Values can then be set with the command</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">m</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="o">*</span><span class="n">im</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w"> </span><span class="o">*</span><span class="n">in</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="o">*</span><span class="n">values</span><span class="p">,</span><span class="n"><a href="../manualpages/Sys/INSERT_VALUES.html">INSERT_VALUES</a></span><span class="p">);</span>
</pre></div>
</div>
<p>After all elements have been inserted into the matrix, it must be
processed with the pair of commands</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">);</span>
<span class="n"><a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">);</span>
</pre></div>
</div>
<p><a class="reference internal" href="mat.html#ch-matrices"><span class="std std-ref">Matrices</span></a> discusses various matrix formats as
well as the details of some basic matrix manipulation routines.</p>
</section>
<section id="linear-solvers">
<h3>Linear Solvers<a class="headerlink" href="#linear-solvers" title="Link to this heading">#</a></h3>
<p>After creating the matrix and vectors that define a linear system,
<code class="docutils notranslate"><span class="pre">Ax</span></code> <span class="math">\(=\)</span> <code class="docutils notranslate"><span class="pre">b</span></code>, the user can then use <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code> to solve the
system with the following sequence of commands:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/KSP/KSPCreate.html">KSPCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="o">*</span><span class="n">ksp</span><span class="p">);</span>
<span class="n"><a href="../manualpages/KSP/KSPSetOperators.html">KSPSetOperators</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="n">ksp</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="n">Amat</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="n">Pmat</span><span class="p">);</span>
<span class="n"><a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="n">ksp</span><span class="p">);</span>
<span class="n"><a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="n">ksp</span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="n">x</span><span class="p">);</span>
<span class="n"><a href="../manualpages/KSP/KSPDestroy.html">KSPDestroy</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="n">ksp</span><span class="p">);</span>
</pre></div>
</div>
<p>The user first creates the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code> context and sets the operators
associated with the system (matrix that defines the linear system,
<code class="docutils notranslate"><span class="pre">Amat</span></code> and matrix from which the preconditioner is constructed,
<code class="docutils notranslate"><span class="pre">Pmat</span></code> ). The user then sets various options for customized solutions,
solves the linear system, and finally destroys the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code> context. The command <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a>()</span></code> enables the user to
customize the linear solution method at runtime using the options
database, which is discussed in <a class="reference internal" href="other.html#sec-options"><span class="std std-ref">Runtime Options</span></a>. Through this database, the
user not only can select an iterative method and preconditioner, but
can also prescribe the convergence tolerance, set various monitoring
routines, etc. (see, e.g., <a class="reference internal" href="#sec-profiling-programs"><span class="std std-ref">Profiling Programs</span></a>).</p>
<p><a class="reference internal" href="ksp.html#ch-ksp"><span class="std std-ref">KSP: Linear System Solvers</span></a> describes in detail the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code> package,
including the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/PC/PC.html">PC</a></span></code> and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code> packages for preconditioners and Krylov
subspace methods.</p>
</section>
<section id="nonlinear-solvers">
<h3>Nonlinear Solvers<a class="headerlink" href="#nonlinear-solvers" title="Link to this heading">#</a></h3>
<p>PETSc provides
an interface to tackle nonlinear problems called <code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code>.
<a class="reference internal" href="snes.html#ch-snes"><span class="std std-ref">SNES: Nonlinear Solvers</span></a> describes the nonlinear
solvers in detail. We highly recommend most PETSc users work directly with
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code>, rather than using PETSc for the linear problem and writing their own
nonlinear solver. Similarly, users should use <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TS.html">TS</a></span></code> rather than rolling their own time integrators.</p>
</section>
<section id="sec-error2">
<span id="id4"></span><h3>Error Checking<a class="headerlink" href="#sec-error2" title="Link to this heading">#</a></h3>
<p>As noted above, PETSc functions return a <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a></span></code>, which is an integer
indicating whether an error has occurred during the call. Below, we indicate a traceback
generated by error detection within a sample PETSc program. The error
occurred on line 3618 of the file
<code class="docutils notranslate"><span class="pre">$PETSC_DIR/src/mat/impls/aij/seq/aij.c</span></code> and was caused by trying to
allocate too large an array in memory. The routine was called in the
program <code class="docutils notranslate"><span class="pre">ex3.c</span></code> on line 66. See
<a class="reference internal" href="fortran.html#sec-fortran-errors"><span class="std std-ref">Error Checking</span></a> for details regarding error checking
when using the PETSc Fortran interface.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ cd $PETSC_DIR/src/ksp/ksp/tutorials
$ make ex3
$ mpiexec -n 1 ./ex3 -m 100000
[0]PETSC ERROR: --------------------- Error Message --------------------------------
[0]PETSC ERROR: Out of memory. This could be due to allocating
[0]PETSC ERROR: too large an object or bleeding by not properly
[0]PETSC ERROR: destroying unneeded objects.
[0]PETSC ERROR: Memory allocated 11282182704 Memory used by process 7075897344
[0]PETSC ERROR: Try running with -malloc_dump or -malloc_view for info.
[0]PETSC ERROR: Memory requested 18446744072169447424
[0]PETSC ERROR: PETSc Development Git Revision: v3.7.1-224-g9c9a9c5 Git Date: 2016-05-18 22:43:00 -0500
[0]PETSC ERROR: ./ex3 on a arch-darwin-double-debug named Patricks-MacBook-Pro-2.local by patrick Mon Jun 27 18:04:03 2016
[0]PETSC ERROR: Configure options PETSC_DIR=/Users/patrick/petsc PETSC_ARCH=arch-darwin-double-debug --download-mpich --download-f2cblaslapack --with-cc=clang --with-cxx=clang++ --with-fc=gfortran --with-debugging=1 --with-precision=double --with-scalar-type=real --with-viennacl=0 --download-c2html -download-sowing
[0]PETSC ERROR: #1 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
[0]PETSC ERROR: #2 PetscTrMallocDefault() line 188 in /Users/patrick/petsc/src/sys/memory/mtr.c
[0]PETSC ERROR: #3 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
[0]PETSC ERROR: #4 <a href="../manualpages/Mat/MatSeqAIJSetPreallocation.html">MatSeqAIJSetPreallocation</a>() line 3562 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
[0]PETSC ERROR: #5 main() line 66 in /Users/patrick/petsc/src/ksp/ksp/tutorials/ex3.c
[0]PETSC ERROR: PETSc Option Table entries:
[0]PETSC ERROR: -m 100000
[0]PETSC ERROR: ----------------End of Error Message ------- send entire error message to petsc-maint@mcs.anl.gov----------
</pre></div>
</div>
<p>When running the debug version <a class="footnote-reference brackets" href="#debug-footnote" id="id5" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> of the PETSc libraries, it checks for memory corruption (writing outside of array bounds
, etc.). The macro <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/CHKMEMQ.html">CHKMEMQ</a></span></code> can be called anywhere in the code to check
the current status of the memory for corruption. By putting several (or
many) of these macros into your code, you can usually easily track down
in what small segment of your code the corruption has occurred. One can
also use Valgrind to track down memory errors; see the <a class="reference external" href="https://petsc.org/release/faq/">FAQ</a>.</p>
<p>For complete error handling, calls to MPI functions should be made with <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscCallMPI.html">PetscCallMPI</a>(MPI_Function(Args))</span></code>.
In Fortran subroutines use <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscCallMPI.html">PetscCallMPI</a>(MPI_Function(Args,</span> <span class="pre">ierr))</span></code> and in Fortran main use
<code class="docutils notranslate"><span class="pre">PetscCallMPIA(MPI_Function(Args,</span> <span class="pre">ierr))</span></code>.</p>
<p>PETSc has a small number of C/C++-only macros that do not explicitly return error codes. These are used in the style</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">XXXBegin</span><span class="p">(</span><span class="n">Args</span><span class="p">);</span>
<span class="n">other</span><span class="w"> </span><span class="n">code</span>
<span class="n">XXXEnd</span><span class="p">();</span>
</pre></div>
</div>
<p>and include <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscOptionsBegin.html">PetscOptionsBegin</a>()</span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscOptionsEnd.html">PetscOptionsEnd</a>()</span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscObjectOptionsBegin.html">PetscObjectOptionsBegin</a>()</span></code>,
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscOptionsHeadBegin.html">PetscOptionsHeadBegin</a>()</span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscOptionsHeadEnd.html">PetscOptionsHeadEnd</a>()</span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Draw/PetscDrawCollectiveBegin.html">PetscDrawCollectiveBegin</a>()</span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Draw/PetscDrawCollectiveEnd.html">PetscDrawCollectiveEnd</a>()</span></code>,
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MatPreallocateEnd.html">MatPreallocateEnd</a>()</span></code>, and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MatPreallocateBegin.html">MatPreallocateBegin</a>()</span></code>. These should not be checked for error codes.
Another class of functions with the <code class="docutils notranslate"><span class="pre">Begin()</span></code> and <code class="docutils notranslate"><span class="pre">End()</span></code> paradigm
including <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a>()</span></code>, and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a>()</span></code> do return error codes that should be checked.</p>
<p>PETSc also has a set of C/C++-only macros that return an object, or <code class="docutils notranslate"><span class="pre">NULL</span></code> if an error has been detected. These include
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/Viewer/PETSC_VIEWER_STDOUT_WORLD.html">PETSC_VIEWER_STDOUT_WORLD</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Viewer/PETSC_VIEWER_DRAW_WORLD.html">PETSC_VIEWER_DRAW_WORLD</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Viewer/PETSC_VIEWER_STDOUT_.html">PETSC_VIEWER_STDOUT_</a>(<a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a>)</span></code>, and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Viewer/PETSC_VIEWER_DRAW_.html">PETSC_VIEWER_DRAW_</a>(<a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a>)</span></code>.</p>
<p>Finally <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscObjectComm.html">PetscObjectComm</a>((<a href="../manualpages/Sys/PetscObject.html">PetscObject</a>)x)</span></code> returns the communicator associated with the object <code class="docutils notranslate"><span class="pre">x</span></code> or <code class="docutils notranslate"><span class="pre">MPI_COMM_NULL</span></code> if an
error was detected.</p>
</section>
</section>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="parallel-and-gpu-programming">
<span id="sec-parallel"></span><h1>Parallel and GPU Programming<a class="headerlink" href="#parallel-and-gpu-programming" title="Link to this heading">#</a></h1>
<p>Numerical computing today has multiple levels of parallelism (concurrency).</p>
<ul class="simple">
<li><p>Low-level, single instruction multiple data (SIMD) parallelism or, somewhat similar, on-GPU parallelism,</p></li>
<li><p>medium-level, multiple instruction multiple data shared memory parallelism (thread parallelism), and</p></li>
<li><p>high-level, distributed memory parallelism.</p></li>
</ul>
<p>Traditional CPUs support the lower two levels via, for example, Intel AVX-like instructions (<a class="reference internal" href="#sec-cpu-simd"><span class="std std-ref">CPU SIMD parallelism</span></a>) and Unix threads, often managed by using OpenMP pragmas (<a class="reference internal" href="#sec-cpu-openmp"><span class="std std-ref">CPU OpenMP parallelism</span></a>),
(or multiple processes). GPUs also support the lower two levels via kernel functions (<a class="reference internal" href="#sec-gpu-kernels"><span class="std std-ref">GPU kernel parallelism</span></a>) and streams (<a class="reference internal" href="#sec-gpu-streams"><span class="std std-ref">GPU stream parallelism</span></a>).
Distributed memory parallelism is created by combining multiple
CPUs and/or GPUs and using MPI for communication (<a class="reference internal" href="#sec-mpi"><span class="std std-ref">MPI Parallelism</span></a>).</p>
<p>In addition, there is also concurrency between computations (floating point operations) and data movement (from memory to caches and registers
and via MPI between distinct memory nodes).</p>
<p>PETSc supports all these parallelism levels, but its strongest support is for MPI-based distributed memory parallelism.</p>
<section id="mpi-parallelism">
<span id="sec-mpi"></span><h2>MPI Parallelism<a class="headerlink" href="#mpi-parallelism" title="Link to this heading">#</a></h2>
<p>Since PETSc uses the message-passing model for parallel programming and
employs MPI for all interprocessor communication, the user can
employ MPI routines as needed throughout an application code. However,
by default, the user is shielded from many of the details of message
passing within PETSc since these are hidden within parallel objects,
such as vectors, matrices, and solvers. In addition, PETSc provides
tools such as vector scatter and gather to assist in the
management of parallel data.</p>
<p>Recall that the user must specify a communicator upon creation of any
PETSc object (such as a vector, matrix, or solver) to indicate the
processors over which the object is to be distributed. For example, as
mentioned above, some commands for matrix, vector, and linear solver
creation are:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/Mat/MatCreate.html">MatCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w"> </span><span class="o">*</span><span class="n">A</span><span class="p">);</span>
<span class="n"><a href="../manualpages/Vec/VecCreate.html">VecCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">);</span>
<span class="n"><a href="../manualpages/KSP/KSPCreate.html">KSPCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="w"> </span><span class="n">comm</span><span class="p">,</span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w"> </span><span class="o">*</span><span class="n">ksp</span><span class="p">);</span>
</pre></div>
</div>
<p>The creation routines are collective on all processes in the
communicator; thus, all processors in the communicator <em>must</em> call the
creation routine. In addition, if a sequence of collective routines is
being used, they <em>must</em> be called in the same order on each process.</p>
<p>The next example, given below,
illustrates the solution of a linear system in parallel. This code,
corresponding to
<a href="../src/ksp/ksp/tutorials/ex2.c.html">KSP Tutorial ex2</a>,
handles the two-dimensional Laplacian discretized with finite
differences, where the linear system is again solved with KSP. The code
performs the same tasks as the sequential version within
<a class="reference internal" href="#sec-simple"><span class="std std-ref">Simple PETSc Examples</span></a>. Note that the user interface
for initiating the program, creating vectors and matrices, and solving
the linear system is <em>exactly</em> the same for the uniprocessor and
multiprocessor examples. The primary difference between the examples in
<a class="reference internal" href="#sec-simple"><span class="std std-ref">Simple PETSc Examples</span></a> and
here is each processor forms only its
local part of the matrix and vectors in the parallel case.</p>
<div class="admonition-listing-a-href-petsc-doc-out-root-placeholder-src-ksp-ksp-tutorials-ex2-c-html-ksp-tutorial-src-ksp-ksp-tutorials-ex2-c-a admonition" id="ksp-ex2">
<p class="admonition-title">Listing: <a href="../src/ksp/ksp/tutorials/ex2.c.html">KSP Tutorial src/ksp/ksp/tutorials/ex2.c</a></p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">help</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Solves a linear system in parallel with <a href="../manualpages/KSP/KSP.html">KSP</a>.</span><span class="se">\n</span><span class="s">\</span>
<span class="s">Input parameters include:</span><span class="se">\n</span><span class="s">\</span>
<span class="s">  -view_exact_sol   : write exact solution vector to stdout</span><span class="se">\n</span><span class="s">\</span>
<span class="s">  -m &lt;mesh_x&gt;       : number of mesh points in x-direction</span><span class="se">\n</span><span class="s">\</span>
<span class="s">  -n &lt;mesh_y&gt;       : number of mesh points in y-direction</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">;</span>

<span class="cm">/*</span>
<span class="cm">  Include &quot;petscksp.h&quot; so that we can use <a href="../manualpages/KSP/KSP.html">KSP</a> solvers.</span>
<span class="cm">*/</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;petscksp.h&gt;</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w">         </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">;</span><span class="w"> </span><span class="cm">/* approx solution, RHS, exact solution */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Mat/Mat.html">Mat</a></span><span class="w">         </span><span class="n">A</span><span class="p">;</span><span class="w">       </span><span class="cm">/* linear system matrix */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/KSP/KSP.html">KSP</a></span><span class="w">         </span><span class="n">ksp</span><span class="p">;</span><span class="w">     </span><span class="cm">/* linear solver context */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscReal.html">PetscReal</a></span><span class="w">   </span><span class="n">norm</span><span class="p">;</span><span class="w">    </span><span class="cm">/* norm of solution error */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscInt.html">PetscInt</a></span><span class="w">    </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">,</span><span class="w"> </span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="n">J</span><span class="p">,</span><span class="w"> </span><span class="n">Istart</span><span class="p">,</span><span class="w"> </span><span class="n">Iend</span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="n">its</span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscBool.html">PetscBool</a></span><span class="w">   </span><span class="n">flg</span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="n">v</span><span class="p">;</span>

<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscFunctionBeginUser.html">PetscFunctionBeginUser</a></span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">help</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscOptionsGetInt.html">PetscOptionsGetInt</a></span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-m&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscOptionsGetInt.html">PetscOptionsGetInt</a></span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-n&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">         Compute the matrix and right-hand-side vector that define</span>
<span class="cm">         the linear system, Ax = b.</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Create parallel matrix, specifying only its global dimensions.</span>
<span class="cm">     When using <a href="../manualpages/Mat/MatCreate.html">MatCreate</a>(), the matrix format can be specified at</span>
<span class="cm">     runtime. Also, the parallel partitioning of the matrix is</span>
<span class="cm">     determined by PETSc at runtime.</span>

<span class="cm">     Performance tuning note:  For problems of substantial size,</span>
<span class="cm">     preallocation of matrix memory is crucial for attaining good</span>
<span class="cm">     performance. See the matrix chapter of the users manual for details.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatCreate.html">MatCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">A</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetSizes.html">MatSetSizes</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetFromOptions.html">MatSetFromOptions</a></span><span class="p">(</span><span class="n">A</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatMPIAIJSetPreallocation.html">MatMPIAIJSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSeqAIJSetPreallocation.html">MatSeqAIJSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSeqSBAIJSetPreallocation.html">MatSeqSBAIJSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatMPISBAIJSetPreallocation.html">MatMPISBAIJSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatMPISELLSetPreallocation.html">MatMPISELLSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSeqSELLSetPreallocation.html">MatSeqSELLSetPreallocation</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Currently, all PETSc parallel matrix formats are partitioned by</span>
<span class="cm">     contiguous chunks of rows across the processors.  Determine which</span>
<span class="cm">     rows of the matrix are locally owned.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatGetOwnershipRange.html">MatGetOwnershipRange</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Istart</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Iend</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set matrix elements for the 2-D, five-point stencil in parallel.</span>
<span class="cm">      - Each processor needs to insert only elements that it owns</span>
<span class="cm">        locally (but any non-local elements will be sent to the</span>
<span class="cm">        appropriate processor during matrix assembly).</span>
<span class="cm">      - Always specify global rows and columns of matrix entries.</span>

<span class="cm">     Note: this uses the less common natural ordering that orders first</span>
<span class="cm">     all the unknowns for x = h then for x = 2h etc; Hence you see J = Ii +- n</span>
<span class="cm">     instead of J = I +- m as you might expect. The more standard ordering</span>
<span class="cm">     would first do all variables for y = h, then y = 2h etc.</span>

<span class="cm">   */</span>
<span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">Ii</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Istart</span><span class="p">;</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">Iend</span><span class="p">;</span><span class="w"> </span><span class="n">Ii</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">;</span>
<span class="w">    </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w">    </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">J</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w">      </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">J</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/ADD_VALUES.html">ADD_VALUES</a></span><span class="p">));</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">J</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w">      </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">J</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/ADD_VALUES.html">ADD_VALUES</a></span><span class="p">));</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">J</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">      </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">J</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/ADD_VALUES.html">ADD_VALUES</a></span><span class="p">));</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">J</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Ii</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">      </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">J</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/ADD_VALUES.html">ADD_VALUES</a></span><span class="p">));</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">4.0</span><span class="p">;</span>
<span class="w">    </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetValues.html">MatSetValues</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ii</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/ADD_VALUES.html">ADD_VALUES</a></span><span class="p">));</span>
<span class="w">  </span><span class="p">}</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Assemble matrix, using the 2-step process:</span>
<span class="cm">       <a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a>(), <a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a>()</span>
<span class="cm">     Computations can be done while messages are in transition</span>
<span class="cm">     by placing code between these two statements.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Mat/MatAssemblyType.html">MAT_FINAL_ASSEMBLY</a></span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* A is symmetric. Set symmetric flag to enable ICC/Cholesky preconditioner */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatSetOption.html">MatSetOption</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Mat/MatOption.html">MAT_SYMMETRIC</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_TRUE.html">PETSC_TRUE</a></span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Create parallel vectors.</span>
<span class="cm">      - We form 1 vector from scratch and then duplicate as needed.</span>
<span class="cm">      - When using <a href="../manualpages/Vec/VecCreate.html">VecCreate</a>(), <a href="../manualpages/Vec/VecSetSizes.html">VecSetSizes</a> and <a href="../manualpages/Vec/VecSetFromOptions.html">VecSetFromOptions</a>()</span>
<span class="cm">        in this example, we specify only the</span>
<span class="cm">        vector&#39;s global dimension; the parallel partitioning is determined</span>
<span class="cm">        at runtime.</span>
<span class="cm">      - When solving a linear system, the vectors and matrices MUST</span>
<span class="cm">        be partitioned accordingly.  PETSc automatically generates</span>
<span class="cm">        appropriately partitioned matrices and vectors when <a href="../manualpages/Mat/MatCreate.html">MatCreate</a>()</span>
<span class="cm">        and <a href="../manualpages/Vec/VecCreate.html">VecCreate</a>() are used with the same communicator.</span>
<span class="cm">      - The user can alternatively specify the local vector and matrix</span>
<span class="cm">        dimensions when more sophisticated partitioning is needed</span>
<span class="cm">        (replacing the <a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a> argument in the <a href="../manualpages/Vec/VecSetSizes.html">VecSetSizes</a>() statement</span>
<span class="cm">        below).</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecCreate.html">VecCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSetSizes.html">VecSetSizes</a></span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_DECIDE.html">PETSC_DECIDE</a></span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSetFromOptions.html">VecSetFromOptions</a></span><span class="p">(</span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDuplicate.html">VecDuplicate</a></span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">b</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDuplicate.html">VecDuplicate</a></span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set exact solution; then compute right-hand-side vector.</span>
<span class="cm">     By default we use an exact solution of a vector with all</span>
<span class="cm">     elements of 1.0;</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecSet.html">VecSet</a></span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatMult.html">MatMult</a></span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     View the exact solution vector if desired</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n">flg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_FALSE.html">PETSC_FALSE</a></span><span class="p">;</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscOptionsGetBool.html">PetscOptionsGetBool</a></span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-view_exact_sol&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">flg</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">));</span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flg</span><span class="p">)</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecView.html">VecView</a></span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Viewer/PETSC_VIEWER_STDOUT_WORLD.html">PETSC_VIEWER_STDOUT_WORLD</a></span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                Create the linear solver and set various options</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPCreate.html">KSPCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set operators. Here the matrix that defines the linear system</span>
<span class="cm">     also serves as the preconditioning matrix.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetOperators.html">KSPSetOperators</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Set linear solver defaults for this problem (optional).</span>
<span class="cm">     - By extracting the <a href="../manualpages/KSP/KSP.html">KSP</a> and <a href="../manualpages/PC/PC.html">PC</a> contexts from the <a href="../manualpages/KSP/KSP.html">KSP</a> context,</span>
<span class="cm">       we can then directly call any <a href="../manualpages/KSP/KSP.html">KSP</a> and <a href="../manualpages/PC/PC.html">PC</a> routines to set</span>
<span class="cm">       various options.</span>
<span class="cm">     - The following statement is optional; all of these</span>
<span class="cm">       parameters could alternatively be specified at runtime via</span>
<span class="cm">       <a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a>().  All of these defaults can be</span>
<span class="cm">       overridden at runtime, as indicated below.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetTolerances.html">KSPSetTolerances</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="mf">1.</span><span class="n">e</span><span class="mi">-2</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">((</span><span class="n">m</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)),</span><span class="w"> </span><span class="mf">1.</span><span class="n">e</span><span class="mi">-50</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_CURRENT.html">PETSC_CURRENT</a></span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PETSC_CURRENT.html">PETSC_CURRENT</a></span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">    Set runtime options, e.g.,</span>
<span class="cm">        -ksp_type &lt;type&gt; -pc_type &lt;type&gt; -ksp_monitor -ksp_rtol &lt;rtol&gt;</span>
<span class="cm">    These options will override those specified above as long as</span>
<span class="cm">    <a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a>() is called _after_ any other customization</span>
<span class="cm">    routines.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSetFromOptions.html">KSPSetFromOptions</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                      Solve the linear system</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>

<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</span>
<span class="cm">                      Check the solution and clean up</span>
<span class="cm">     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecAXPY.html">VecAXPY</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="mf">-1.0</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecNorm.html">VecNorm</a></span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/Vec/NORM_2.html">NORM_2</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">norm</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPGetIterationNumber.html">KSPGetIterationNumber</a></span><span class="p">(</span><span class="n">ksp</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">its</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Print convergence information.  <a href="../manualpages/Sys/PetscPrintf.html">PetscPrintf</a>() produces a single</span>
<span class="cm">     print statement from all processes that share a communicator.</span>
<span class="cm">     An alternative is <a href="../manualpages/Sys/PetscFPrintf.html">PetscFPrintf</a>(), which prints to a file.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscPrintf.html">PetscPrintf</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Norm of error %g iterations %&quot;</span><span class="w"> </span><span class="n">PetscInt_FMT</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">norm</span><span class="p">,</span><span class="w"> </span><span class="n">its</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Free work space.  All PETSc objects should be destroyed when they</span>
<span class="cm">     are no longer needed.</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/KSP/KSPDestroy.html">KSPDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">ksp</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">u</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Vec/VecDestroy.html">VecDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">b</span><span class="p">));</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Mat/MatDestroy.html">MatDestroy</a></span><span class="p">(</span><span class="o">&amp;</span><span class="n">A</span><span class="p">));</span>

<span class="w">  </span><span class="cm">/*</span>
<span class="cm">     Always call <a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a>() before exiting a program.  This routine</span>
<span class="cm">       - finalizes the PETSc libraries as well as MPI</span>
<span class="cm">       - provides summary and diagnostic information if certain runtime</span>
<span class="cm">         options are chosen (e.g., -log_view).</span>
<span class="cm">  */</span>
<span class="w">  </span><span class="n"><a href="../manualpages/Sys/PetscCall.html">PetscCall</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a></span><span class="p">());</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

</pre></div>
</div>
</div>
</section>
<section id="cpu-simd-parallelism">
<span id="sec-cpu-simd"></span><h2>CPU SIMD parallelism<a class="headerlink" href="#cpu-simd-parallelism" title="Link to this heading">#</a></h2>
<p>SIMD parallelism occurs most commonly in the Intel advanced vector extensions (AVX) families of instructions (see <a class="reference external" href="https://en.wikipedia.org/wiki/Advanced_Vector_Extensions">Wikipedia</a>).
It may be automatically used by the optimizing compiler or in low-level libraries that PETSc uses, such as BLAS
(see <a class="reference external" href="https://github.com/flame/blis">BLIS</a>), or rarely,
directly in PETSc C/C++ code, as in <a class="reference external" href="https://petsc.org/main/src/mat/impls/sell/seq/sell.c.html#MatMult_SeqSELL">MatMult_SeqSELL</a>.</p>
</section>
<section id="cpu-openmp-parallelism">
<span id="sec-cpu-openmp"></span><h2>CPU OpenMP parallelism<a class="headerlink" href="#cpu-openmp-parallelism" title="Link to this heading">#</a></h2>
<p>OpenMP parallelism is thread parallelism. Multiple threads (independent streams of instructions) process data and perform computations on different
parts of memory that is
shared (accessible) to all of the threads. The OpenMP model is based on inserting pragmas into code, indicating that a series of instructions
(often within a loop) can be run in parallel. This is also called a fork-join model of parallelism since much of the code remains sequential and only the
computationally expensive parts in the ‘parallel region’ are parallel. Thus, OpenMP makes it relatively easy to add some
parallelism to a conventional sequential code in a shared memory environment.</p>
<p>POSIX threads (pthreads) is a library that may be called from C/C++. The library contains routines to create, join, and remove threads, plus manage communications and
synchronizations between threads. Pthreads is rarely used directly in numerical libraries and applications. Sometimes OpenMP is implemented on top of pthreads.</p>
<p>If one adds
OpenMP parallelism to an MPI code, one must not over-subscribe the hardware resources. For example, if MPI already has one MPI process (rank)
per hardware core, then
using four OpenMP threads per MPI process will slow the code down since now one core must switch back and forth between four OpenMP threads.</p>
<p>For application codes that use certain external packages, including BLAS/LAPACK, SuperLU_DIST, MUMPS, MKL, and SuiteSparse, one can build PETSc and these
packages to take advantage of OpenMP by using the configure option <code class="docutils notranslate"><span class="pre">--with-openmp</span></code>. The number of OpenMP threads used in the application can be controlled with
the PETSc command line option <code class="docutils notranslate"><span class="pre">-omp_num_threads</span> <span class="pre">&lt;num&gt;</span></code> or the environmental variable <code class="docutils notranslate"><span class="pre">OMP_NUM_THREADS</span></code>. Running a PETSc program with <code class="docutils notranslate"><span class="pre">-omp_view</span></code> will display the
number of threads used. The default number is often absurdly high for the given hardware, so we recommend always setting it appropriately.</p>
<p>Users can also put OpenMP pragmas into their own code. However, since standard PETSc is not thread-safe, they should not, in general,
call PETSc routines from inside the parallel regions.</p>
<p>There is an OpenMP thread-safe subset of PETSc that may be configured for using <code class="docutils notranslate"><span class="pre">--with-threadsafety</span></code> (often used along with <code class="docutils notranslate"><span class="pre">--with-openmp</span></code> or
<code class="docutils notranslate"><span class="pre">--download-concurrencykit</span></code>). <a href="../src/ksp/ksp/tutorials/ex61f.F90.html">KSP Tutorial ex61f</a> demonstrates
how this may be used with OpenMP. In this mode, one may have individual OpenMP threads that each manage their own
(sequential) PETSc objects (each thread can interact only with its own objects). This
is useful when one has many small systems (or sets of ODEs) that must be integrated in an
“embarrassingly parallel” fashion on multicore systems.</p>
<p>The ./configure option <code class="docutils notranslate"><span class="pre">--with-openmp-kernels</span></code> causes some PETSc numerical kernels to be compiled using OpenMP pragmas to take advantage of multiple cores.
One must be careful to ensure the number of threads used by each MPI process <strong>times</strong> the number of MPI processes is less than the number of
cores on the system; otherwise the code will slow down dramatically.</p>
<p>PETSc’s MPI-based linear solvers may be accessed from a sequential or non-MPI OpenMP program, see <a class="reference internal" href="ksp.html#sec-pcmpi"><span class="std std-ref">Using PETSc’s MPI parallel linear solvers from a non-MPI program</span></a>.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p>Edward A. Lee, <a class="reference external" href="https://digitalassets.lib.berkeley.edu/techreports/ucb/text/EECS-2006-1.pdf">The Problem with Threads</a>, Technical Report No. UCB/EECS-2006-1 January <a class="reference external" href="https://doi.org/10.1109/MC.2006.180">[DOI]</a>
10, 2006</p>
</div>
</section>
<section id="gpu-kernel-parallelism">
<span id="sec-gpu-kernels"></span><h2>GPU kernel parallelism<a class="headerlink" href="#gpu-kernel-parallelism" title="Link to this heading">#</a></h2>
<p>GPUs offer at least two levels of clearly defined parallelism. Kernel-level parallelism is much like SIMD parallelism applied to loops;
many “iterations” of the loop index run on different hardware in “lock-step”.
PETSc utilizes this parallelism with three similar but slightly different models:</p>
<ul class="simple">
<li><p>CUDA, which is provided by NVIDIA and runs on NVIDIA GPUs</p></li>
<li><p>HIP, provided by AMD, which can, in theory, run on both AMD and NVIDIA GPUs</p></li>
<li><p>and Kokkos, an open-source package that provides a slightly higher-level programming model to utilize GPU kernels.</p></li>
</ul>
<p>To utilize this one configures PETSc with either <code class="docutils notranslate"><span class="pre">--with-cuda</span></code> or <code class="docutils notranslate"><span class="pre">--with-hip</span></code> and, if they plan to use Kokkos, also <code class="docutils notranslate"><span class="pre">--download-kokkos</span> <span class="pre">--download-kokkos-kernels</span></code>.</p>
<p>In the GPU programming model that PETSc uses, the GPU memory is distinct from the CPU memory. This means that data that resides on the CPU
memory must be copied to the GPU (often, this copy is done automatically by the libraries, and the user does not need to manage it)
if one wishes to use the GPU computational power on it. This memory copy is slow compared to the GPU speed; hence, it is crucial to minimize these copies. This often
translates to trying to do almost all the computation on the GPU and not constantly switching between computations on the CPU and the GPU on the same data.</p>
<p>PETSc utilizes GPUs by providing vector and matrix classes (Vec and Mat) specifically written to run on the GPU. However, since it is difficult to
write an entire PETSc code that runs only on the GPU, one can also access and work with (for example, put entries into) the vectors and matrices
on the CPU. The vector classes
are <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/VECCUDA.html">VECCUDA</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MATAIJCUSPARSE.html">MATAIJCUSPARSE</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/VECKOKKOS.html">VECKOKKOS</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/MATAIJKOKKOS.html">MATAIJKOKKOS</a></span></code>, and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/VECHIP.html">VECHIP</a></span></code> (matrices are not yet supported by PETSc with HIP).</p>
<p>More details on using GPUs from PETSc will follow in this document.</p>
</section>
<section id="gpu-stream-parallelism">
<span id="sec-gpu-streams"></span><h2>GPU stream parallelism<a class="headerlink" href="#gpu-stream-parallelism" title="Link to this heading">#</a></h2>
<p>Please contribute to this document.</p>
</section>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="compiling-and-running-programs">
<h1>Compiling and Running Programs<a class="headerlink" href="#compiling-and-running-programs" title="Link to this heading">#</a></h1>
<p>The output below illustrates compiling and running a
PETSc program using MPICH on a macOS laptop. Note that different
machines will have compilation commands as determined by the
configuration process. See <a class="reference internal" href="#sec-writing-application-codes"><span class="std std-ref">Writing C/C++ or Fortran Applications</span></a> for
a discussion about how to compile your PETSc programs. Users who are
experiencing difficulties linking PETSc programs should refer to the <a class="reference external" href="https://petsc.org/release/faq/">FAQ</a>.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ cd $PETSC_DIR/src/ksp/ksp/tutorials
$ make ex2
/Users/patrick/petsc/arch-debug/bin/mpicc -o ex2.o -c -g3   -I/Users/patrick/petsc/include -I/Users/patrick/petsc/arch-debug/include `pwd`/ex2.c
/Users/patrick/petsc/arch-debug/bin/mpicc -g3  -o ex2 ex2.o  -Wl,-rpath,/Users/patrick/petsc/arch-debug/lib -L/Users/patrick/petsc/arch-debug/lib  -lpetsc -lf2clapack -lf2cblas -lmpifort -lgfortran -lgcc_ext.10.5 -lquadmath -lm -lclang_rt.osx -lmpicxx -lc++ -ldl -lmpi -lpmpi -lSystem
/bin/rm -f ex2.o
$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex2
Norm of error 0.000156044 iterations 6
$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 2 ./ex2
Norm of error 0.000411674 iterations 7
</pre></div>
</div>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="profiling-programs">
<span id="sec-profiling-programs"></span><h1>Profiling Programs<a class="headerlink" href="#profiling-programs" title="Link to this heading">#</a></h1>
<p>The option
<code class="docutils notranslate"><span class="pre">-log_view</span></code> activates printing of a performance summary, including
times, floating point operation (flop) rates, and message-passing
activity. <a class="reference internal" href="profiling.html#ch-profiling"><span class="std std-ref">Profiling</span></a> provides details about
profiling, including the interpretation of the output data below.
This particular example involves
the solution of a linear system on one processor using GMRES and ILU.
The low floating point operation (flop) rates in this example are because the code solved a tiny system. We include this example
merely to demonstrate the ease of extracting performance information.</p>
<div class="highlight-none notranslate" id="listing-exprof"><div class="highlight"><pre><span></span>$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex1 -n 1000 -pc_type ilu -ksp_type gmres -ksp_rtol 1.e-7 -log_view
...
------------------------------------------------------------------------------------------------------------------------
Event                Count      Time (sec)     Flops                             --- Global ---  --- Stage ----  Total
                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %T %F %M %L %R  %T %F %M %L %R Mflop/s
------------------------------------------------------------------------------------------------------------------------

<a href="../manualpages/Vec/VecMDot.html">VecMDot</a>                1 1.0 3.2830e-06 1.0 2.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0  5  0  0  0   0  5  0  0  0   609
<a href="../manualpages/Vec/VecNorm.html">VecNorm</a>                3 1.0 4.4550e-06 1.0 6.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 14  0  0  0   0 14  0  0  0  1346
<a href="../manualpages/Vec/VecScale.html">VecScale</a>               2 1.0 4.0110e-06 1.0 2.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0  5  0  0  0   0  5  0  0  0   499
<a href="../manualpages/Vec/VecCopy.html">VecCopy</a>                1 1.0 3.2280e-06 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
<a href="../manualpages/Vec/VecSet.html">VecSet</a>                11 1.0 2.5537e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
<a href="../manualpages/Vec/VecAXPY.html">VecAXPY</a>                2 1.0 2.0930e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 10  0  0  0   0 10  0  0  0  1911
<a href="../manualpages/Vec/VecMAXPY.html">VecMAXPY</a>               2 1.0 1.1280e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 10  0  0  0   0 10  0  0  0  3546
<a href="../manualpages/Vec/VecNormalize.html">VecNormalize</a>           2 1.0 9.3970e-06 1.0 6.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 14  0  0  0   1 14  0  0  0   638
<a href="../manualpages/Mat/MatMult.html">MatMult</a>                2 1.0 1.1177e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   894
<a href="../manualpages/Mat/MatSolve.html">MatSolve</a>               2 1.0 1.9933e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   501
MatLUFactorNum         1 1.0 3.5081e-05 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  2 10  0  0  0   2 10  0  0  0   114
MatILUFactorSym        1 1.0 4.4259e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  3  0  0  0  0   3  0  0  0  0     0
<a href="../manualpages/Mat/MatAssemblyBegin.html">MatAssemblyBegin</a>       1 1.0 8.2015e-08 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
<a href="../manualpages/Mat/MatAssemblyEnd.html">MatAssemblyEnd</a>         1 1.0 3.3536e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
<a href="../manualpages/Mat/MatGetRowIJ.html">MatGetRowIJ</a>            1 1.0 1.5960e-06 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
<a href="../manualpages/MatGraphOperations/MatGetOrdering.html">MatGetOrdering</a>         1 1.0 3.9791e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  3  0  0  0  0   3  0  0  0  0     0
<a href="../manualpages/Mat/MatView.html">MatView</a>                2 1.0 6.7909e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  5  0  0  0  0   5  0  0  0  0     0
KSPGMRESOrthog         1 1.0 7.5970e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 10  0  0  0   1 10  0  0  0   526
<a href="../manualpages/KSP/KSPSetUp.html">KSPSetUp</a>               1 1.0 3.4424e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
<a href="../manualpages/KSP/KSPSolve.html">KSPSolve</a>               1 1.0 2.7264e-04 1.0 3.30e+04 1.0 0.0e+00 0.0e+00 0.0e+00 19 79  0  0  0  19 79  0  0  0   121
<a href="../manualpages/PC/PCSetUp.html">PCSetUp</a>                1 1.0 1.5234e-04 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00 11 10  0  0  0  11 10  0  0  0    26
<a href="../manualpages/PC/PCApply.html">PCApply</a>                2 1.0 2.1022e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   475
------------------------------------------------------------------------------------------------------------------------

Memory usage is given in bytes:

Object Type          Creations   Destructions     Memory  Descendants&#39; Mem.
Reports information only for process 0.

--- Event Stage 0: Main Stage

              Vector     8              8        76224     0.
              Matrix     2              2       134212     0.
       Krylov Solver     1              1        18400     0.
      Preconditioner     1              1         1032     0.
           Index Set     3              3        10328     0.
              Viewer     1              0            0     0.
========================================================================================================================
...
</pre></div>
</div>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="writing-c-c-or-fortran-applications">
<span id="sec-writing-application-codes"></span><h1>Writing C/C++ or Fortran Applications<a class="headerlink" href="#writing-c-c-or-fortran-applications" title="Link to this heading">#</a></h1>
<p>The examples throughout the library demonstrate the software usage and
can serve as templates for developing custom applications. We suggest
that new PETSc users examine programs in the directories
<code class="docutils notranslate"><span class="pre">$PETSC_DIR/src/&lt;library&gt;/tutorials</span></code> where <code class="docutils notranslate"><span class="pre">&lt;library&gt;</span></code> denotes any
of the PETSc libraries (listed in the following section), such as
<code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code> or <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code>, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TS.html">TS</a></span></code>, or <code class="docutils notranslate"><span class="pre">TAO</span></code>. The manual pages at
<a class="reference external" href="https://petsc.org/release/documentation/">https://petsc.org/release/documentation/</a> provide links (organized by
routine names and concepts) to the tutorial examples.</p>
<p>To develop an application program that uses PETSc, we suggest the following:</p>
<ul>
<li><p><a class="reference internal" href="../install/download.html#doc-download"><span class="std std-ref">Download</span></a> and <a class="reference internal" href="../install/index.html#doc-install"><span class="std std-ref">install</span></a> PETSc.</p></li>
<li><p>For completely new applications</p>
<blockquote>
<div><ol class="arabic">
<li><p>Make a directory for your source code: for example, <code class="docutils notranslate"><span class="pre">mkdir</span> <span class="pre">$HOME/application</span></code></p></li>
<li><p>Change to that directory, for
example, <code class="docutils notranslate"><span class="pre">cd</span> <span class="pre">$HOME/application</span></code></p></li>
<li><p>Copy an example in the directory that corresponds to the
problems of interest into your directory, for
example, <code class="docutils notranslate"><span class="pre">cp</span> <span class="pre">$PETSC_DIR/src/snes/tutorials/ex19.c</span> <span class="pre">app.c</span></code></p></li>
<li><p>Select an application build process. The <code class="docutils notranslate"><span class="pre">PETSC_DIR</span></code> (and <code class="docutils notranslate"><span class="pre">PETSC_ARCH</span></code> if the <code class="docutils notranslate"><span class="pre">--prefix=directoryname</span></code>
option was not used when configuring PETSc) environmental variable(s) must be
set for any of these approaches.</p>
<ul>
<li><p>make (recommended). It uses the <a class="reference external" href="https://en.wikipedia.org/wiki/Pkg-config">pkg-config</a> tool
and is the recommended approach. Copy $PETSC_DIR/share/petsc/Makefile.user or $PETSC_DIR/share/petsc/Makefile.basic.user
to your directory, for example, <code class="docutils notranslate"><span class="pre">cp</span> <span class="pre">$PETSC_DIR/share/petsc/Makefile.user</span> <span class="pre">makefile</span></code></p>
<p>Examine the comments in this makefile.</p>
<p>Makefile.user uses the <a class="reference external" href="https://en.wikipedia.org/wiki/Pkg-config">pkg-config</a> tool and is the recommended approach.</p>
<p>Use <code class="docutils notranslate"><span class="pre">make</span> <span class="pre">app</span></code> to compile your program.</p>
</li>
<li><p>CMake. Copy $PETSC_DIR/share/petsc/CMakeLists.txt to your directory, for example, <code class="docutils notranslate"><span class="pre">cp</span> <span class="pre">$PETSC_DIR/share/petsc/CMakeLists.txt</span> <span class="pre">CMakeLists.txt</span></code></p>
<p>Edit CMakeLists.txt, read the comments on usage, and change the name of the application from ex1 to your application executable name.</p>
</li>
</ul>
</li>
<li><p>Run the program, for example,
<code class="docutils notranslate"><span class="pre">./app</span></code></p></li>
<li><p>Start to modify the program to develop your application.</p></li>
</ol>
</div></blockquote>
</li>
<li><p>For adding PETSc to an existing application</p>
<blockquote>
<div><ol class="arabic">
<li><p>Start with a working version of your code that you build and run to confirm that it works.</p></li>
<li><p>Upgrade your build process. The <code class="docutils notranslate"><span class="pre">PETSC_DIR</span></code> (and <code class="docutils notranslate"><span class="pre">PETSC_ARCH</span></code> if the <code class="docutils notranslate"><span class="pre">--prefix=directoryname</span></code>
option was not used when configuring PETSc) environmental variable(s) must be
set for any of these approaches.</p>
<ul>
<li><p>Using make. Update the application makefile to add the appropriate PETSc include
directories and libraries.</p>
<ul>
<li><p>Recommended approach. Examine the comments in $PETSC_DIR/share/petsc/Makefile.user and transfer selected portions of
that file to your makefile.</p></li>
<li><p>Minimalist. Add the line</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">include ${PETSC_DIR}/lib/petsc/conf/variables</span>
</pre></div>
</div>
<p>to the bottom of your makefile. This will provide a set of PETSc-specific make variables you may use in your makefile. See
the comments in the file $PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage.</p>
</li>
<li><p>Simple, but hands the build process over to PETSc’s control. Add the lines</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">include ${PETSC_DIR}/lib/petsc/conf/variables</span>
<span class="go">include ${PETSC_DIR}/lib/petsc/conf/rules</span>
</pre></div>
</div>
<p>to the bottom of your makefile. See the comments in the file $PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage.
Since PETSc’s rules now control the build process, you will likely need to simplify and remove much of the material that is in
your makefile.</p>
</li>
<li><p>Not recommended since you must change your makefile for each new configuration/computing system. This approach does not require
the environmental variable <code class="docutils notranslate"><span class="pre">PETSC_DIR</span></code> to be set when building your application since the information will be hardwired in your
makefile. Run the following command in the PETSc root directory to get the information needed by your makefile:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>make<span class="w"> </span>getlinklibs<span class="w"> </span>getincludedirs<span class="w"> </span>getcflags<span class="w"> </span>getcxxflags<span class="w"> </span>getfortranflags<span class="w"> </span>getccompiler<span class="w"> </span>getfortrancompiler<span class="w"> </span>getcxxcompiler
</pre></div>
</div>
<p>All the libraries listed need to be linked into your executable, and the
include directories and flags need to be passed to the compiler(s). Usually,
this is done by setting <code class="docutils notranslate"><span class="pre">LDFLAGS=&lt;list</span> <span class="pre">of</span> <span class="pre">library</span> <span class="pre">flags</span> <span class="pre">and</span> <span class="pre">libraries&gt;</span></code> and
<code class="docutils notranslate"><span class="pre">CFLAGS=&lt;list</span> <span class="pre">of</span> <span class="pre">-I</span> <span class="pre">and</span> <span class="pre">other</span> <span class="pre">flags&gt;</span></code> and <code class="docutils notranslate"><span class="pre">FFLAGS=&lt;list</span> <span class="pre">of</span> <span class="pre">-I</span> <span class="pre">and</span> <span class="pre">other</span> <span class="pre">flags&gt;</span></code> etc in your makefile.</p>
</li>
</ul>
</li>
<li><p>Using CMake. Update the application CMakeLists.txt by examining the code and comments in
$PETSC_DIR/share/petsc/CMakeLists.txt</p></li>
</ul>
</li>
<li><p>Rebuild your application and ensure it still runs correctly.</p></li>
<li><p>Add a <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscInitialize.html">PetscInitialize</a>()</span></code> near the beginning of your code and <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Sys/PetscFinalize.html">PetscFinalize</a>()</span></code> near the end with appropriate include commands
(and use statements in Fortran).</p></li>
<li><p>Rebuild your application and ensure it still runs correctly.</p></li>
<li><p>Slowly start utilizing PETSc functionality in your code, and ensure that your code continues to build and run correctly.</p></li>
</ol>
</div></blockquote>
</li>
</ul>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="petsc-s-object-oriented-design">
<span id="sec-oo"></span><h1>PETSc’s Object-Oriented Design<a class="headerlink" href="#petsc-s-object-oriented-design" title="Link to this heading">#</a></h1>
<p>Though PETSc has a large API, conceptually, it’s rather simple.
There are three abstract basic data objects (classes): index sets, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/IS/IS.html">IS</a></span></code>, vectors, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Vec/Vec.html">Vec</a></span></code>, and matrices, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/Mat.html">Mat</a></span></code>.
Plus, a larger number of abstract algorithm objects (classes) starting with: preconditioners, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/PC/PC.html">PC</a></span></code>, Krylov solvers, <code class="docutils notranslate"><span class="pre"><a href="../manualpages/KSP/KSP.html">KSP</a></span></code>, and so forth.</p>
<p>Let <code class="docutils notranslate"><span class="pre">Object</span></code>
represent any of these objects. Objects are created with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Object</span><span class="w"> </span><span class="n">obj</span><span class="p">;</span>
<span class="n">ObjectCreate</span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/MPI_Comm.html">MPI_Comm</a></span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">obj</span><span class="p">);</span>
</pre></div>
</div>
<p>The object is initially empty, and little can be done with it. A particular implementation of the class is associated with the object by setting the object’s “type”, where type
is merely a string name of an implementation class using</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectSetType</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="s">&quot;ImplementationName&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>Some objects support subclasses, which are specializations of the type. These are set with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectNameSetType</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="s">&quot;ImplementationSubName&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>For example, within <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TS.html">TS</a></span></code> one may do</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/TS/TS.html">TS</a></span><span class="w"> </span><span class="n">ts</span><span class="p">;</span>
<span class="n"><a href="../manualpages/TS/TSCreate.html">TSCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="o">&amp;</span><span class="n">ts</span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSSetType.html">TSSetType</a></span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="n"><a href="../manualpages/TS/TSARKIMEX.html">TSARKIMEX</a></span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSARKIMEXSetType.html">TSARKIMEXSetType</a></span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="n"><a href="../manualpages/TS/TSARKIMEX3.html">TSARKIMEX3</a></span><span class="p">);</span>
</pre></div>
</div>
<p>The abstract class <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TS.html">TS</a></span></code> can embody any ODE/DAE integrator scheme.
This example creates an additive Runge-Kutta ODE/DAE IMEX integrator, whose type name is <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSARKIMEX.html">TSARKIMEX</a></span></code>, using a 3rd-order scheme with an L-stable implicit part,
whose subtype name is <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSARKIMEX3.html">TSARKIMEX3</a></span></code>.</p>
<p>To allow PETSc objects to be runtime configurable, PETSc objects provide a universal way of selecting types (classes) and subtypes at runtime from
what is referred to as the PETSc “options database”. The code above can be replaced with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/TS/TS.html">TS</a></span><span class="w"> </span><span class="n">obj</span><span class="p">;</span>
<span class="n"><a href="../manualpages/TS/TSCreate.html">TSCreate</a></span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PETSC_COMM_WORLD.html">PETSC_COMM_WORLD</a></span><span class="p">,</span><span class="o">&amp;</span><span class="n">obj</span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSSetFromOptions.html">TSSetFromOptions</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
</pre></div>
</div>
<p>now, both the type and subtype can be conveniently set from the command line</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>./app<span class="w"> </span>-ts_type<span class="w"> </span>arkimex<span class="w"> </span>-ts_arkimex_type<span class="w"> </span><span class="m">3</span>
</pre></div>
</div>
<p>The object’s type (implementation class) or subclass can also be changed at any time simply by calling <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSSetType.html">TSSetType</a>()</span></code> again (though to override command line options, the call to <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSSetType.html">TSSetType</a>()</span></code> must be made _after_ <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSSetFromOptions.html">TSSetFromOptions</a>()</span></code>). For example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// (if set) command line options &quot;override&quot; <a href="../manualpages/TS/TSSetType.html">TSSetType</a>()</span>
<span class="n"><a href="../manualpages/TS/TSSetType.html">TSSetType</a></span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/TS/TSGLLE.html">TSGLLE</a></span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSSetFromOptions.html">TSSetFromOptions</a></span><span class="p">(</span><span class="n">ts</span><span class="p">);</span>

<span class="c1">// <a href="../manualpages/TS/TSSetType.html">TSSetType</a>() overrides command line options</span>
<span class="n"><a href="../manualpages/TS/TSSetFromOptions.html">TSSetFromOptions</a></span><span class="p">(</span><span class="n">ts</span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSSetType.html">TSSetType</a></span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/TS/TSGLLE.html">TSGLLE</a></span><span class="p">);</span>
</pre></div>
</div>
<p>Since the later call always overrides the earlier call, the second form shown is rarely – if ever – used, as it is less flexible than configuring command line settings.</p>
<p>The standard methods on an object are of the general form.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectSetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectGetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectYYY</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
</pre></div>
</div>
<p>For example</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/TS/TSSetRHSFunction.html">TSSetRHSFunction</a></span><span class="p">(</span><span class="n">obj</span><span class="p">,...)</span>
</pre></div>
</div>
<p>Particular types and subtypes of objects may have their own methods, which are given in the form</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectNameSetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectNameGetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectNameYYY</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
</pre></div>
</div>
<p>and</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectNameSubNameSetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectNameSubNameGetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
<span class="n">ObjectNameSubNameYYY</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
</pre></div>
</div>
<p>where Name and SubName are the type and subtype names (for example, as above <code class="docutils notranslate"><span class="pre"><a href="../manualpages/TS/TSARKIMEX.html">TSARKIMEX</a></span></code> and <code class="docutils notranslate"><span class="pre">3</span></code>. Most “set” operations have options database versions with the same
names in lower case, separated by underscores, and with the word “set” removed. For example,</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/KSP/KSPGMRESSetRestart.html">KSPGMRESSetRestart</a></span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="mi">30</span><span class="p">);</span>
</pre></div>
</div>
<p>can be set at the command line with</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>./app<span class="w"> </span>-ksp_gmres_restart<span class="w"> </span><span class="m">30</span>
</pre></div>
</div>
<p>A special subset of type-specific methods is ignored if the type does not match the function name. These are usually setter functions that control some aspect specific to the subtype.
Note that we leveraged this functionality in the MPI example above (<a class="reference internal" href="#sec-mpi"><span class="std std-ref">MPI Parallelism</span></a>) by calling <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Mat/Mat.html">Mat</a>*SetPreallocation()</span></code> for a number of different matrix types. As another example,</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/KSP/KSPGMRESSetRestart.html">KSPGMRESSetRestart</a></span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="mi">30</span><span class="p">);</span><span class="w">   </span><span class="c1">// ignored if the type is not <a href="../manualpages/KSP/KSPGMRES.html">KSPGMRES</a></span>
</pre></div>
</div>
<p>These allow cleaner application code since it does not have many if statements to avoid inactive methods. That is, one does not need to write code like</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n"><a href="../manualpages/KSP/KSPGMRES.html">KSPGMRES</a></span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">     </span><span class="c1">// unneeded clutter</span>
<span class="w">  </span><span class="n"><a href="../manualpages/KSP/KSPGMRESSetRestart.html">KSPGMRESSetRestart</a></span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="mi">30</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Many “get” routines give one temporary access to an object’s internal data. They are used in the style</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">XXX</span><span class="w"> </span><span class="n">xxx</span><span class="p">;</span>
<span class="n">ObjectGetXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="o">&amp;</span><span class="n">xxx</span><span class="p">);</span>
<span class="c1">// use xxx</span>
<span class="n">ObjectRestoreXXX</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="o">&amp;</span><span class="n">xxx</span><span class="p">);</span>
</pre></div>
</div>
<p>Objects obtained with a “get” routine should be returned with a “restore” routine, generally within the same function. Objects obtained with a “create” routine should be freed
with a “destroy” routine.</p>
<p>There may be variants of the “get” routines that give more limited access to the obtained object. For example,</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">const</span><span class="w"> </span><span class="n"><a href="../manualpages/Sys/PetscScalar.html">PetscScalar</a></span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span>

<span class="c1">// specialized variant of <a href="../manualpages/Vec/VecGetArray.html">VecGetArray</a>()</span>
<span class="n"><a href="../manualpages/Vec/VecGetArrayRead.html">VecGetArrayRead</a></span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="p">);</span>
<span class="c1">// one can read but not write with x[]</span>
<span class="n"><a href="../manualpages/Sys/PetscReal.html">PetscReal</a></span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
<span class="c1">// don&#39;t forget to restore x after you are done with it</span>
<span class="n"><a href="../manualpages/Vec/VecRestoreArrayRead.html">VecRestoreArrayRead</a></span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="p">);</span>
</pre></div>
</div>
<p>Objects can be displayed (in a large number of ways) with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectView</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="n"><a href="../manualpages/Viewer/PetscViewer.html">PetscViewer</a></span><span class="w"> </span><span class="n">viewer</span><span class="p">);</span>
<span class="n">ObjectViewFromOptions</span><span class="p">(</span><span class="n">obj</span><span class="p">,...);</span>
</pre></div>
</div>
<p>Where <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Viewer/PetscViewer.html">PetscViewer</a></span></code> is an abstract object that can represent standard output, an ASCII or binary file, a graphical window, etc. The second
variant allows the user to delay until runtime the decision of what viewer and format to use to view the object or if to view the object at all.</p>
<p>Objects are destroyed with</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectDestroy</span><span class="p">(</span><span class="o">&amp;</span><span class="n">obj</span><span class="p">)</span>
</pre></div>
</div>
<figure class="align-default" id="fig-objectlife">
<img alt="../_images/objectlife.svg" src="../_images/objectlife.svg" /><figcaption>
<p><span class="caption-number">Fig. 2 </span><span class="caption-text">Sample lifetime of a PETSc object</span><a class="headerlink" href="#fig-objectlife" title="Link to this image">#</a></p>
</figcaption>
</figure>
<section id="user-callbacks">
<h2>User Callbacks<a class="headerlink" href="#user-callbacks" title="Link to this heading">#</a></h2>
<p>The user may wish to override or provide custom functionality in many situations. This is handled via callbacks, which the library will call at the appropriate time. The most general way to apply a callback has this form:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ObjectCallbackSetter</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span><span class="w"> </span><span class="n">callbackfunction</span><span class="p">(),</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">ctx</span><span class="p">,</span><span class="w"> </span><span class="n">contextdestroy</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">ctx</span><span class="p">));</span>
</pre></div>
</div>
<p>where <code class="docutils notranslate"><span class="pre">ObjectCallbackSetter()</span></code> is a callback setter such as <code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNESSetFunction.html">SNESSetFunction</a>()</span></code>. <code class="docutils notranslate"><span class="pre">callbackfunction()</span></code> is what will be called
by the library, <code class="docutils notranslate"><span class="pre">ctx</span></code> is an optional data structure (array, struct, PETSc object) that is used by <code class="docutils notranslate"><span class="pre">callbackfunction()</span></code>
and <code class="docutils notranslate"><span class="pre">contextdestroy(void</span> <span class="pre">*ctx)</span></code> is an optional function that will be called when <code class="docutils notranslate"><span class="pre">obj</span></code> is destroyed to free
anything in <code class="docutils notranslate"><span class="pre">ctx</span></code>. The use of the <code class="docutils notranslate"><span class="pre">contextdestroy()</span></code> allows users to “set and forget”
data structures that will not be needed elsewhere but still need to be deleted when no longer needed. Here is an example of the use of a full-fledged callback</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/TS/TS.html">TS</a></span><span class="w">              </span><span class="n">ts</span><span class="p">;</span>
<span class="n">TSMonitorLGCtx</span><span class="w"> </span><span class="o">*</span><span class="n">ctx</span><span class="p">;</span>

<span class="n"><a href="../manualpages/TS/TSMonitorLGCtxCreate.html">TSMonitorLGCtxCreate</a></span><span class="p">(...,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ctx</span><span class="p">)</span>
<span class="n"><a href="../manualpages/TS/TSMonitorSet.html">TSMonitorSet</a></span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="../manualpages/TS/TSMonitorLGTimeStep.html">TSMonitorLGTimeStep</a></span><span class="p">,</span><span class="w"> </span><span class="n">ctx</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n"><a href="../manualpages/Sys/PetscCtxDestroyFn.html">PetscCtxDestroyFn</a></span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n"><a href="../manualpages/TS/TSMonitorLGCtxDestroy.html">TSMonitorLGCtxDestroy</a></span><span class="p">);</span>
<span class="n"><a href="../manualpages/TS/TSSolve.html">TSSolve</a></span><span class="p">(</span><span class="n">ts</span><span class="p">);</span>
</pre></div>
</div>
<p>Occasionally, routines to set callback functions take additional data objects that will be used by the object but are not context data for the function. For example,</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n"><a href="../manualpages/SNES/SNES.html">SNES</a></span><span class="w"> </span><span class="n">obj</span><span class="p">;</span>
<span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="w">  </span><span class="n">r</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">ctx</span><span class="p">;</span>

<span class="n"><a href="../manualpages/SNES/SNESSetFunction.html">SNESSetFunction</a></span><span class="p">(</span><span class="n">snes</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">UserApplyFunction</span><span class="p">(</span><span class="n"><a href="../manualpages/SNES/SNES.html">SNES</a></span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="p">,</span><span class="n"><a href="../manualpages/Vec/Vec.html">Vec</a></span><span class="p">,</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">ctx</span><span class="p">),</span><span class="w"> </span><span class="n">ctx</span><span class="p">);</span>
</pre></div>
</div>
<p>The <code class="docutils notranslate"><span class="pre">r</span></code> vector is an optional argument provided by the user, which will be used as work-space by <code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code>. Note that this callback does not provide a way for the user
to have the <code class="docutils notranslate"><span class="pre">ctx</span></code> destroyed when the <code class="docutils notranslate"><span class="pre"><a href="../manualpages/SNES/SNES.html">SNES</a></span></code> object is destroyed; the users must ensure that they free it at an appropriate time. There is no logic to the various ways
PETSc accepts callback functions in different places in the code.</p>
<p>See <a class="reference internal" href="tao.html#fig-taocallbacks"><span class="std std-ref">Tao use of PETSc and callbacks</span></a> for a cartoon on callbacks in <code class="docutils notranslate"><span class="pre"><a href="../manualpages/Tao/Tao.html">Tao</a></span></code>.</p>
</section>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="directory-structure">
<span id="sec-directory"></span><h1>Directory Structure<a class="headerlink" href="#directory-structure" title="Link to this heading">#</a></h1>
<p>We conclude this introduction with an overview of the organization of
the PETSc software. The root directory of PETSc contains the following
directories:</p>
<ul class="simple">
<li><p><code class="docutils notranslate"><span class="pre">doc</span></code> The source code and Python scripts for building the website and documentation</p></li>
<li><p><code class="docutils notranslate"><span class="pre">lib/petsc/conf</span></code> - Base PETSc configuration files that define the standard
make variables and rules used by PETSc</p></li>
<li><p><code class="docutils notranslate"><span class="pre">include</span></code> - All include files for PETSc that are visible to the
user.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">include/petsc/finclude</span></code> - PETSc Fortran include files.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">include/petsc/private</span></code> - Private PETSc include files that should
<em>not</em> need to be used by application programmers.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">share</span></code> - Some small test matrices and other data files</p></li>
<li><p><code class="docutils notranslate"><span class="pre">src</span></code> - The source code for all PETSc libraries, which currently
includes</p>
<ul>
<li><p><code class="docutils notranslate"><span class="pre">vec</span></code> - vectors,</p>
<ul>
<li><p><code class="docutils notranslate"><span class="pre">is</span></code> - index sets,</p></li>
</ul>
</li>
<li><p><code class="docutils notranslate"><span class="pre">mat</span></code> - matrices,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">ksp</span></code> - complete linear equations solvers,</p>
<ul>
<li><p><code class="docutils notranslate"><span class="pre">ksp</span></code> - Krylov subspace accelerators,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">pc</span></code> - preconditioners,</p></li>
</ul>
</li>
<li><p><code class="docutils notranslate"><span class="pre">snes</span></code> - nonlinear solvers</p></li>
<li><p><code class="docutils notranslate"><span class="pre">ts</span></code> - ODE/DAE solvers and timestepping,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">tao</span></code> - optimizers,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">dm</span></code> - data management between meshes and solvers, vectors, and
matrices,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">sys</span></code> - general system-related routines,</p>
<ul>
<li><p><code class="docutils notranslate"><span class="pre">logging</span></code> - PETSc logging and profiling routines,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">classes</span></code> - low-level classes</p>
<ul>
<li><p><code class="docutils notranslate"><span class="pre">draw</span></code> - simple graphics,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">viewer</span></code> - a mechanism for printing and visualizing PETSc
objects,</p></li>
<li><p><code class="docutils notranslate"><span class="pre">bag</span></code> - mechanism for saving and loading from disk user
data stored in C structs.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">random</span></code> - random number generators.</p></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Each PETSc source code library directory has the following subdirectories:</p>
<ul class="simple">
<li><p><code class="docutils notranslate"><span class="pre">tutorials</span></code> - Programs designed to teach users about PETSc.
These codes can serve as templates for applications.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">tests</span></code> - Programs designed for thorough testing of PETSc. As
such, these codes are not intended for examination by users.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">interface</span></code> - Provides the abstract base classes for the objects.
The code here does not know about particular implementations and does not perform
operations on the underlying numerical data.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">impls</span></code> - Source code for one or more implementations of the class for particular
data structures or algorithms.</p></li>
<li><p><code class="docutils notranslate"><span class="pre">utils</span></code> - Utility routines. The source here may know about the
implementations, but ideally, will not know about implementations for
other components.</p></li>
</ul>
<p class="rubric">Footnotes</p>
<div class="docutils container" id="id1">
<div role="list" class="citation-list">
<div class="citation" id="id1190" role="doc-biblioentry">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">For94</a><span class="fn-bracket">]</span></span>
<p>MPI Forum. MPI: a message-passing interface standard. <em>International J. Supercomputing Applications</em>, 1994.</p>
</div>
<div class="citation" id="id1269" role="doc-biblioentry">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id2">GLS94</a><span class="fn-bracket">]</span></span>
<p>William Gropp, Ewing Lusk, and Anthony Skjellum. <em>Using MPI: Portable Parallel Programming with the Message Passing Interface</em>. MIT Press, 1994.</p>
</div>
</div>
</div>
</section>
<hr class="footnotes docutils" />
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="debug-footnote" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id5">1</a><span class="fn-bracket">]</span></span>
<p>Configure PETSc with <code class="docutils notranslate"><span class="pre">--with-debugging</span></code>.</p>
</aside>
</aside>


                </article>
              
              
              
              
              
                <footer class="prev-next-footer">
                  
<div class="prev-next-area">
    <a class="left-prev"
       href="about_this_manual.html"
       title="previous page">
      <i class="fa-solid fa-angle-left"></i>
      <div class="prev-next-info">
        <p class="prev-next-subtitle">previous</p>
        <p class="prev-next-title">About This Manual</p>
      </div>
    </a>
    <a class="right-next"
       href="programming.html"
       title="next page">
      <div class="prev-next-info">
        <p class="prev-next-subtitle">next</p>
        <p class="prev-next-title">The Solvers in PETSc/TAO</p>
      </div>
      <i class="fa-solid fa-angle-right"></i>
    </a>
</div>
                </footer>
              
            </div>
            
            
              
                <div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">


  <div class="sidebar-secondary-item">
<div
    id="pst-page-navigation-heading-2"
    class="page-toc tocsection onthispage">
    <i class="fa-solid fa-list"></i> On this page
  </div>
  <nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2">
    <ul class="visible nav section-nav flex-column">
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">Getting Started</a><ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#suggested-reading">Suggested Reading</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#running-petsc-programs">Running PETSc Programs</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#writing-petsc-programs">Writing PETSc Programs</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#error-checking">Error Checking</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simple-petsc-examples">Simple PETSc Examples</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#include-files">Include Files</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#the-options-database">The Options Database</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#vectors">Vectors</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#matrices">Matrices</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#linear-solvers">Linear Solvers</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#nonlinear-solvers">Nonlinear Solvers</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#sec-error2">Error Checking</a></li>
</ul>
</li>
</ul>
</li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#parallel-and-gpu-programming">Parallel and GPU Programming</a><ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mpi-parallelism">MPI Parallelism</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cpu-simd-parallelism">CPU SIMD parallelism</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cpu-openmp-parallelism">CPU OpenMP parallelism</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gpu-kernel-parallelism">GPU kernel parallelism</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gpu-stream-parallelism">GPU stream parallelism</a></li>
</ul>
</li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#compiling-and-running-programs">Compiling and Running Programs</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#profiling-programs">Profiling Programs</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#writing-c-c-or-fortran-applications">Writing C/C++ or Fortran Applications</a></li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#petsc-s-object-oriented-design">PETSc’s Object-Oriented Design</a><ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#user-callbacks">User Callbacks</a></li>
</ul>
</li>
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#directory-structure">Directory Structure</a></li>
</ul>

  </nav></div>

  <div class="sidebar-secondary-item">

  
  <div class="tocsection editthispage">
    <a href="https://gitlab.com/petsc/petsc/-/edit/release/doc/manual/getting_started.md">
      <i class="fa-solid fa-pencil"></i>
      
      
        
          Edit on GitLab
        
      
    </a>
  </div>
</div>

  <div class="sidebar-secondary-item">

  <div class="tocsection sourcelink">
    <a href="../_sources/manual/getting_started.md.txt">
      <i class="fa-solid fa-file-lines"></i> Show Source
    </a>
  </div>
</div>

</div></div>
              
            
          </div>
          <footer class="bd-footer-content">
            
          </footer>
        
      </main>
    </div>
  </div>
  
  <!-- Scripts loaded after <body> so the DOM is not blocked -->
  <script src="../_static/scripts/bootstrap.js?digest=bd9e20870c6007c4c509"></script>
<script src="../_static/scripts/pydata-sphinx-theme.js?digest=bd9e20870c6007c4c509"></script>

  <footer class="bd-footer">
<div class="bd-footer__inner bd-page-width">
  
    <div class="footer-items__start">
      
        <div class="footer-item">

  <p class="copyright">
    
      © Copyright 1991-2025, UChicago Argonne, LLC and the PETSc Development Team.
      <br/>
    
  </p>
</div>
      
        <div class="footer-item">

  <p class="sphinx-version">
    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.3.7.
    <br/>
  </p>
</div>
      
    </div>
  
  
  
    <div class="footer-items__end">
      
        <div class="footer-item">
<p class="theme-version">
  Built with the <a href="https://pydata-sphinx-theme.readthedocs.io/en/stable/index.html">PyData Sphinx Theme</a> 0.15.1.
</p></div>
      
        <div class="footer-item"><p class="last-updated">
  Last updated on 2025-04-30T13:10:40-0500 (v3.23.1).
  <br/>
</p></div>
      
    </div>
  
</div>

  </footer>
  </body>
</html>