-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2104 lines (1974 loc) · 121 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mastering C++ for scientific computing: tools, tips, and tricks</title>
<meta name="description"
content="Thanks to its impressive performance and its unrivaled abstraction power, C++ remains one of the most widely used languages in scientific computing: It lies at the core of machine learning frameworks like TensorFlow and PyTorch, is the foundation of vendor-specific tools for programming accelerators like SYCL, NVIDIA's CUDA or AMD's HIP, and is the go-to language for scientific and engineering projects spanning the full spectrum from tiny embedded control applications, all the way up to analyzing the petabytes of data generated by physics experiments at CERN. This seminar will cover tools and guidelines to help you to efficiently develop safer, more performant, and more maintainable C++ code. We will go over the following topics: development tools, runtime sanitizers and static analysis, C++ Core Guidelines, the pitfalls of undefined behavior, build systems and packaging, portability and compatibility, interfacing with Python, performance, linear algebra ... The main goal is to cover a broad range of topics, and provide pointers to further resources to dive into. While aimed primarily towards PhD researchers and students who use C++ in their research or master's thesis, the talk should provide valuable insights for anyone with an interest in C++.">
<meta name="author" content="Pieter Pas">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="node_modules/reveal.js/dist/reset.css">
<link rel="stylesheet" href="node_modules/reveal.js/dist/reveal.css">
<link rel="stylesheet" href="node_modules/reveal.js/dist/theme/serif.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="node_modules/reveal.js/plugin/highlight/monokai.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<div style="display: flex; flex-direction: column; justify-content: space-around; height: 700px;">
<div>
<h3>Mastering C++ for scientific computing:<br>tools, tips, and tricks</h3>
<hr>
<p>
<small><a href="https://github.com/tttapa">Pieter Pas</a></small>
</p>
</div>
</div>
<div
style="text-align: right; font-size: 0.5em; color: var(--r-link-color); opacity: 0.7; height: 0; overflow: visible;">
<div style="display: inline-block; text-align: left;">
<kbd>Space</kbd>: Next slide<br>
<kbd>Shift</kbd>+<kbd>Space</kbd>: Previous slide<br>
<kbd>←</kbd>/<kbd>→</kbd>: Previous/next chapter<br>
<kbd>Esc</kbd>: Overview
<kbd>F11</kbd>: Full screen
</div>
</div>
</section>
<section class="full">
<h3>Outline</h3>
<hr>
<ol style="margin-top: 1.5em;">
<li><a style="font-family: inherit;" href="#/sec-devtools">Development tools</a></li>
<li><a style="font-family: inherit;" href="#/sec-example">Building an example project</a></li>
<li><a style="font-family: inherit;" href="#/sec-cmake">CMake concepts</a></li>
<li><a style="font-family: inherit;" href="#/sec-pkg">Package management</a></li>
<li><a style="font-family: inherit;" href="#/sec-bugs">Catching bugs early</a></li>
<li><a style="font-family: inherit;" href="#/sec-practices">Best practices</a></li>
<li><a style="font-family: inherit;" href="#/sec-optimization">Optimization</a></li>
<li><a style="font-family: inherit;" href="#/sec-interop">Interoperability with Python</a></li>
<li><a style="font-family: inherit;" href="#/sec-rsrc">Useful resources</a></li>
</ol>
</section>
<section>
<section id="sec-devtools">
<h3>Development tools</h3>
<hr>
</section>
<section class="full">
<h4>Compilers</h4>
<hr>
<ul>
<li><b>Linux</b>
<ul>
<li>GCC <small class="rmrk">(<code>sudo apt install g++</code>) (<a
href="https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test">ppa:ubuntu-toolchain-r/test</a>)</small>
</li>
<li>Clang <small class="rmrk">(<code>sudo apt install clang</code>) (<a
href="https://apt.llvm.org">apt.llvm.org</a>)</small></li>
</ul>
</li>
<li><b>macOS</b>
<ul>
<li>Apple Clang <small class="rmrk">(<a
href="https://developer.apple.com/xcode">developer.apple.com/xcode</a>)</small>
</li>
<li>Clang <small class="rmrk">(<code>brew install llvm</code>)</small></li>
<li>GCC <small class="rmrk">(<code>brew install gcc</code>)</small></li>
</ul>
</li>
<li><b>Windows</b>
<ul>
<li>MSVC <small class="rmrk">(<a
href="https://visualstudio.microsoft.com">visualstudio.microsoft.com</a>)</small>
</li>
<li><s>MinGW</s> <small class="rmrk">(not recommended)</small></li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Compilers</h4>
<hr>
<ul>
<li><b>Intel</b>
<ul>
<li>Intel oneAPI DPC++/C++ Compiler <br><small class="rmrk">(<a
href="https://www.intel.com/content/www/us/en/developer/tools/oneapi/dpc-compiler.html">www.intel.com/content/www/us/en/developer/tools/oneapi/dpc-compiler.html</a>)</small>
</li>
</ul>
</li>
<li><b>AMD</b>
<ul>
<li>AMD Optimizing C++ Compiler <br><small class="rmrk">(<a
href="https://www.amd.com/en/developer/aocc.html">www.amd.com/en/developer/aocc.html</a>)</small>
</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Integrated development environments (IDEs)</h4>
<hr>
<ul>
<li><b>Visual Studio</b> <small class="rmrk">(Windows only)</small></li>
<li><b>Xcode</b> <small class="rmrk">(macOS only)</small></li>
<li><b>Clion</b> <small class="rmrk">(€12/month, free student license)</small></li>
<li><b>Visual Studio Code</b>
<ul>
<li><em>C/C++ extension</em></li>
<li><em>Clangd extension</em></li>
<li><em>CMake Tools extension</em></li>
</ul>
</li>
<li><b>Other editors</b>
<ul>
<li><em>Clangd language server</em></li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Integrated development environments (IDEs): Go to definition</h4>
<video data-autoplay loop src="media/goto-definition.mp4"></video>
</section>
<section class="full">
<h4>Integrated development environments (IDEs): Refactoring</h4>
<video data-autoplay loop src="media/refactor.mp4"></video>
</section>
<section class="full">
<h4>Integrated development environments (IDEs): Warnings and static analysis
</h4>
<video data-autoplay loop src="media/clang-tidy.mp4"></video>
</section>
<section class="full">
<h4>Integrated development environments (IDEs): Debugging</h4>
<video data-autoplay loop src="media/debug.mp4"></video>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li>Locate <span class="alert">compiler</span> and other tools</li>
<li>Detect compiler <span class="alert">features</span></li>
<li>Allow configuration of project <span class="alert">options</span></li>
<li>Locate <span class="alert">dependencies</span></li>
<li>Propagate compiler and linker <span class="alert">flags</span></li>
<li>Drive the <span class="alert">build</span> process</li>
<li><span class="alert">Install</span> resulting binaries</li>
<li><span class="alert">Package</span> resulting binaries</li>
<li>Drive <span class="alert">test</span> runners</li>
</ul>
</section>
<section class="full" data-auto-animate>
<h4>Build system generators: example</h4>
<hr>
<pre data-id="code-animation"><code class="hljs bash" data-trim data-line-numbers="|1-2|3-4|5-6|7-8"><script type="text/template">
# Download a handy text formatting library as an example
git clone https://github.com/fmtlib/fmt --branch 10.1.1 --single-branch --depth 1
# Configure the project using CMake
cmake -S fmt -B fmt/build -G "Ninja Multi-Config" -D FMT_TEST=Off
# Build the project using CMake
cmake --build fmt/build --config Release -j
# Install the project using CMake
cmake --install fmt/build --config Release --prefix="$HOME/opt/fmt-10.1.1"
</script></code></pre>
</section>
<section class="full" data-auto-animate>
<h4>Build system generators: example</h4>
<hr>
<pre data-id="code-animation"><code class="hljs bash" data-trim data-line-numbers="1-13|15-18|19-39"><script type="text/template">
cmake -S fmt -B fmt/build -G "Ninja Multi-Config" -D FMT_TEST=Off
-- CMake version: 3.27.1
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Version: 10.1.0
-- Build type: Release
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/fmt/build
cmake --build fmt/build --config Release -j
[1/3] Building CXX object CMakeFiles/fmt.dir/Release/src/os.cc.o
[2/3] Building CXX object CMakeFiles/fmt.dir/Release/src/format.cc.o
[3/3] Linking CXX static library Release/libfmt.a
cmake --install fmt/build --config Release --prefix="$HOME/opt/fmt-10.1.1"
-- Installing: ~/opt/fmt-10.1.1/lib/libfmt.a
-- Installing: ~/opt/fmt-10.1.1/include/fmt/args.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/chrono.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/color.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/compile.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/core.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/format.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/format-inl.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/os.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/ostream.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/printf.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/ranges.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/std.h
-- Installing: ~/opt/fmt-10.1.1/include/fmt/xchar.h
-- Installing: ~/opt/fmt-10.1.1/lib/cmake/fmt/fmt-config.cmake
-- Installing: ~/opt/fmt-10.1.1/lib/cmake/fmt/fmt-config-version.cmake
-- Installing: ~/opt/fmt-10.1.1/lib/cmake/fmt/fmt-targets.cmake
-- Installing: ~/opt/fmt-10.1.1/lib/cmake/fmt/fmt-targets-release.cmake
-- Installing: ~/opt/fmt-10.1.1/lib/pkgconfig/fmt.pc
</script></code></pre>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>CMake</b> <small class="rmrk">(<code>CMakeLists.txt</code>)</small>
<ul class="plus fragment">
<li>De facto standard</li>
<li>Huge ecosystem, loads of online resources</li>
<li>Compatible with all major IDEs and toolchains</li>
<li>Fast</li>
<li>Flexible</li>
<li>Interoperability with package managers</li>
<li>Supports testing and packaging</li>
<li>Easy to use for users of your project</li>
</ul>
<ul class="min fragment">
<li>Awkward syntax</li>
<li>Lots of poorly written legacy CMake code</li>
</ul>
<ul class="arrow fragment">
<li>If you are going to learn one build system, learn CMake</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>Meson</b> <small class="rmrk">(<code>meson.build</code>)</small>
<ul class="plus">
<li>Declarative syntax</li>
<li>Intuitive command line interface</li>
<li>Fast</li>
<li>Gaining popularity</li>
<li>Not turing complete</li>
</ul>
<ul class="min">
<li>Not turing complete</li>
<li>Smaller community compared to CMake</li>
<li>Requires Python</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>Bazel</b> <small class="rmrk">(<code>BUILD</code>)</small>
<ul class="plus">
<li>Scalable to very large projects</li>
<li>Remote building, caching and testing</li>
<li>Arguably simpler syntax compared to CMake</li>
</ul>
<ul class="min">
<li>Developed by Google for Google</li>
<li>Poor Windows support</li>
<li>Painful dependency management</li>
<li>Requires Java</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>SCons</b> <small class="rmrk">(<code>SConstruct</code>)</small>
<ul class="plus">
<li>Extensible</li>
<li>Reproducible</li>
</ul>
<ul class="min">
<li>Very slow</li>
<li>No package detection</li>
<li>Very poor/nonexistent cross-compilation support</li>
<li>Low-level, no transitive options</li>
<li>Lots of manual plumbing</li>
<li>Requires creating a custom, one-off build system</li>
<li>Requires Python</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>Xmake</b> <small class="rmrk">(<code>xmake.lua</code>)</small>
<ul class="plus">
<li>Many features and many supported backends</li>
<li>Built-in package manager compatible with external repositories</li>
</ul>
<ul class="min">
<li>Quite new</li>
<li>Mostly a single-developer effort</li>
<li>Most of the discussions and issues are in Chinese</li>
<li>“Everything but the kitchen sink”</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system generators</h4>
<hr>
<ul>
<li><b>GNU Autotools</b> <small class="rmrk">(<code>autogen.sh</code>,
<code>configure.sh</code>)</small>
<ul class="plus">
<li>Many older GNU/Linux projects use it</li>
</ul>
<ul class="min">
<li>Outdated</li>
<li>Primitive and fragile dependency detection <small class="rmrk">(manual or
<code>pkg-config</code>)</small></li>
<li>Poor macOS and Windows compatibility</li>
<li>Very slow</li>
<li>Hard to understand, debug or patch</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system <span class="fragment strike">generators</span></h4>
<hr>
<ul>
<li><b>Make</b> <small class="rmrk">(<code>Makefile</code>)</small>
<ul class="plus">
<li>Many older Linux projects use it</li>
<li>Available as CMake or Autotools backend</li>
</ul>
<ul class="min">
<li>Very low-level build rules</li>
<li>Not a build system generator (no dependency or compiler detection etc.)</li>
<li>Cryptic syntax</li>
<li>Static recipes</li>
<li>Does not allow spaces in paths</li>
<li>Primitive timestamp-based checks</li>
<li>Manual dependency generation</li>
<li>Poor Windows compatibility</li>
<li>Not portable</li>
</ul>
</li>
</ul>
</section>
<section class="full">
<h4>Build system <s>generators</s></h4>
<hr>
<ul>
<li><b>Ninja</b> <small class="rmrk">(<code>build.ninja</code>)</small>
<ul class="plus">
<li>A more modern Make alternative</li>
<li>Fast, easy multi-threading</li>
<li>Light-weight stand-alone binary</li>
<li>Support for C++ and Fortran modules</li>
<li>Excellent backend for CMake</li>
</ul>
<ul class="min">
<li>Intended for code generation by a higher-level tool like CMake</li>
</ul>
<ul class="arrow fragment">
<li>Ideal CMake backend on Linux and macOS <small class="rmrk">(use VS on
Windows)</small>
</li>
</ul>
</li>
</ul>
<pre class="fragment" style="width:68%"><code class="hljs bash" data-trim data-line-numbers><script type="text/template">
cmake -S fmt -B fmt/build -G "Ninja Multi-Config" -D FMT_TEST=Off</script></code></pre>
</section>
</section>
<section>
<section id="sec-example">
<h3>Building an example project</h3>
<hr>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<pre><code class="hljs text" data-trim data-line-numbers="|2-9|3-6|7-8|9|10-12|13" data-noescape>
poly
├── src -- library source code
│ ├── include -- public API header files
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src -- implementation files
│ │ └── interpolate.cpp
│ └── CMakeLists.txt -- library build script
├── test -- unit tests
│ ├── CMakeLists.txt -- testing framework build script
│ └── test-interpolate.cpp
└── CMakeLists.txt -- user-facing build script</code></pre>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="13">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cmake" data-trim data-line-numbers><script type="text/template">
cmake_minimum_required(VERSION 3.21)
project(poly VERSION 1.2.3 LANGUAGES CXX)
# Import CMake testing support
include(CTest)
# Define user options
option(POLY_INIT_NAN "Initialize all matrices and vectors to NaN for debugging purposes" Off)
# Include the build script in the src directory
add_subdirectory("src")
# Include the build script in the test (if enabled)
if (BUILD_TESTING)
add_subdirectory("test")
endif()</script></code></pre>
</div>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="6">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cpp" data-trim data-line-numbers="|3-6|10-15|17-37"><script type="text/template">
#pragma once
#include <Eigen/Dense>
#include <algorithm>
#include <initializer_list>
#include <utility>
namespace poly {
// Handy type aliases to make the rest of the code more readable.
using matrix_t = Eigen::MatrixX<double>;
using vector_t = Eigen::VectorX<double>;
using vector_ref_t = Eigen::Ref<const vector_t>;
using vector_mut_ref_t = Eigen::Ref<vector_t>;
using index_t = Eigen::Index;
/// Representation of a polynomial in Chebyshev basis.
struct ChebyshevPolynomial {
/// Type of the polynomial coefficients.
using coeff_t = vector_t;
/// Create an empty polynomial.
ChebyshevPolynomial() = default;
/// Create a polynomial with the given coefficients.
explicit ChebyshevPolynomial(coeff_t init_coeff)
: coefficients {std::move(init_coeff)} {}
/// Create a polynomial with all zero coefficients of the given degree.
explicit ChebyshevPolynomial(index_t degree)
: coefficients {coeff_t::Zero(degree + 1)} {}
/// Create a polynomial with the given coefficients.
ChebyshevPolynomial(std::initializer_list<double> init_coeff)
: coefficients {init_coeff.size()} {
std::ranges::copy(init_coeff, std::begin(this->coefficients));
}
/// Vector storing the polynomial coefficients in Chebyshev basis.
coeff_t coefficients;
};
} // namespace poly</script></code></pre>
</div>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="5">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cpp" data-trim data-line-numbers><script type="text/template">
#pragma once
#include <poly/poly.hpp>
namespace poly {
/// Given the vectors @f$ x @f$ and @f$ y @f$, interpolate a Chebyshev
/// polynomial @f$ p @f$ such that @f$ p(x_i) = y_i @f$.
/// @param x Samples of the independent variable.
/// @param y Samples of the dependent variable corresponding to @p x.
/// @pre @p x and @p y should have the same length.
/// @return The coefficients of polynomial @f$ p @f$ in Chebyshev basis.
ChebyshevPolynomial interpolate_cheby(vector_ref_t x, vector_ref_t y);
} // namespace poly</script></code></pre>
</div>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="8">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cpp" data-trim data-line-numbers="|8-19|22-34"><script type="text/template">
#include <Eigen/LU>
#include <cassert>
#include <poly/interpolate.hpp>
namespace poly {
namespace {
auto make_chebyshev_basis_matrix(vector_ref_t x, index_t degree) {
assert(degree >= 0);
const index_t N = x.size();
matrix_t V(N, degree + 1);
V.col(0) = vector_t::Ones(N);
if (degree >= 1) {
V.col(1) = x;
for (index_t i = 0; i < degree - 1; ++i)
V.col(i + 2) = 2 * V.col(i + 1).cwiseProduct(x) - V.col(i);
}
return V;
}
} // namespace
ChebyshevPolynomial interpolate_cheby(vector_ref_t x, vector_ref_t y) {
assert(x.size() == y.size());
assert(x.size() > 0);
// Construct Vandermonde/basis matrix
auto V = make_chebyshev_basis_matrix(x, x.size() - 1);
// Scale the system
const vector_t scaling = V.colwise().norm().cwiseInverse();
V *= scaling.asDiagonal();
// Solve the system, undo scaling
vector_t solution = V.fullPivLu().solve(y);
solution.transpose() *= scaling.asDiagonal();
return solution;
}
} // namespace poly</script></code></pre>
</div>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="9">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cmake" data-trim data-line-numbers="|1-2|3-8|9-10|11-12|13-14|15-18|"><script type="text/template">
# Find dependencies we rely on
find_package(Eigen3 REQUIRED)
# Define the "poly" library and specify the source files it consists of
add_library(poly
"include/poly/poly.hpp"
"include/poly/interpolate.hpp"
"src/interpolate.cpp"
)
# The library requires the 2020 revision of the C++ standard
target_compile_features(poly PUBLIC cxx_std_20)
# Specify the path of the header files
target_include_directories(poly PUBLIC "include")
# Link the Eigen library into our poly library
target_link_libraries(poly PUBLIC Eigen3::Eigen)
# Add custom compiler options or macros
if (POLY_INIT_NAN)
target_compile_definitions(poly PUBLIC EIGEN_INITIALIZE_MATRICES_BY_NAN)
endif()</script></code></pre>
</div>
<em class="fragment" style="margin-top: 2em; display: block;">Questions?</em>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="12">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cpp" data-trim data-line-numbers><script type="text/template">
#include <gtest/gtest.h>
#include <poly/interpolate.hpp>
#include <poly/poly.hpp>
TEST(poly, interpolateCheby) {
// Generate some arbitrary data vectors
const poly::index_t N = 5;
poly::vector_t x = poly::vector_t::LinSpaced(N, -1.0, 1.0);
poly::vector_t y(N);
y << 15.0, 3.5625, 1.0, 0.5625, 3.0;
// Interpolate the data
auto p = poly::interpolate_cheby(x, y);
// Check the coefficients of the interpolant
poly::ChebyshevPolynomial expected {4.375, -5.0, 4.0, -1.0, 0.625};
auto error = p.coefficients - expected.coefficients;
EXPECT_LE(error.norm(), 1e-12);
}</script></code></pre>
</div>
</section>
<section class="full">
<h4>Example project</h4>
<hr>
<div style="display: flex; gap: 0.5em;">
<pre style="flex: 30%; font-size: 0.5em;"><code class="hljs text no-ln" data-trim data-noescape data-line-numbers="11">
poly
├── src
│ ├── include
│ │ └── poly
│ │ ├── interpolate.hpp
│ │ └── poly.hpp
│ ├── src
│ │ └── interpolate.cpp
│ └── CMakeLists.txt
├── test
│ ├── CMakeLists.txt
│ └── test-interpolate.cpp
└── CMakeLists.txt</code></pre>
<pre style="flex: 70%" data-id="code-animation"><code class="hljs cmake" data-trim data-line-numbers="|1-2|3-4|5-6|7-8|9-10"><script type="text/template">
# Import the Google Test CMake module
include(GoogleTest)
# Find the Google Test library
find_package(GTest REQUIRED)
# Build the test program
add_executable(tests "test-interpolate.cpp")
# The tests depend on the library under test, and on the Google Test framework
target_link_libraries(tests PRIVATE poly GTest::gtest_main)
# Let CMake/CTest discover the available tests
gtest_discover_tests(tests)</script></code></pre>
</div>
</section>
<section class="full" data-auto-animate>
<h4>Example project: build commands</h4>
<hr>
<ul>
<li>Use Bash or Zsh shell on Linux/macOS, or in <em>“Visual Studio Developer PowerShell”</em> on
Windows</li>
</ul>
<pre data-id="cmake-build-poly"><code class="hljs bash" data-trim data-noescape data-line-numbers>
# Configure
cmake -S . -B build -G "Ninja Multi-Config" -D POLY_INIT_NAN=On
# Build
cmake --build build --config Debug -j
# Run tests
cmake --build build --config Debug --target test</code></pre>
</section>
<section class="full" data-auto-animate>
<h4>Example project: build commands</h4>
<hr>
<pre data-id="cmake-build-poly"><code class="hljs bash" data-trim data-noescape data-line-numbers="1-12|14-19|21-30">
# Configure
cmake -S . -B build -G "Ninja Multi-Config" -D POLY_INIT_NAN=On
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake (found version "1.11.0")
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /home/pieter/GitHub/Cpp-Presentation/code/poly/build
# Build
cmake --build build --config Debug -j
[1/4] Building CXX object test/CMakeFiles/tests.dir/Debug/test-interpolate.cpp.o
[2/4] Building CXX object src/CMakeFiles/poly.dir/Debug/src/interpolate.cpp.o
[3/4] Linking CXX static library src/Debug/libpoly.a
[4/4] Linking CXX executable test/Debug/tests
# Run tests
cmake --build build --config Debug --target test
[0/1] Running tests...
Test project /home/pieter/GitHub/Cpp-Presentation/code/poly/build
Start 1: poly.interpolateCheby
1/1 Test #1: poly.interpolateCheby ............ Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec</code></pre>
</section>
<section class="full">
<h4>Example project: Visual Studio Code</h4>
<video data-autoplay loop src="media/configure.mp4"></video>
</section>
<section class="full">
<h4>Example project: Visual Studio Code</h4>
<video data-autoplay loop src="media/test.mp4"></video>
</section>
</section>
<section>
<section id="sec-cmake">
<h3>CMake concepts</h3>
<hr>
</section>
<section class="full">
<h4>CMake concepts</h4>
<hr>
<ol>
<li>Import and define <span class="alert">targets</span>
<ul style="font-size: 0.66em;">
<li><code>find_package(name)</code></li>
<li><code>add_library(target sources...)</code></li>
<li><code>add_executable(target sources...)</code></li>
<li><code>add_custom_target(target command args...)</code></li>
</ul>
</li>
<li>Set target <span class="alert">properties</span> and <span class="alert">usage
requirements</span>
<ul style="font-size: 0.66em;">
<li><code>target_compile_features(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> features...)</code>
</li>
<li><code>target_compile_definitions(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> definitions...)</code>
</li>
<li><code>target_compile_options(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> options...)</code>
</li>
<li><code>target_include_directories(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> directories...)</code>
</li>
<li><code>target_link_options(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> options...)</code>
</li>
<li><code>set_target_properties(targets PROPERTIES property value)</code></li>
</ul>
</li>
<li>Express <span class="alert">dependencies</span> between targets
<ul style="font-size: 0.66em;">
<li><code>target_link_libraries(target <span class="fragment highlight-red" data-fragment-index="3">SCOPE</span> dependency)</code>
</li>
</ul>
</li>
</ol>
<ul class="arrow fragment" data-fragment-index="2">
<li>Use targets and properties, do not set global flags</li>
</ul>
</section>
<section class="full">
<h4>CMake concepts: scope and usage requirements</h4>
<hr>
<ul>
<li><b><code>PUBLIC</code></b>
<ul>
<li>Applies to current target <em>and</em> dependents <small
class="rmrk">(transitive)</small></li>
</ul>
</li>
<li><b><code>PRIVATE</code></b>
<ul>
<li>Applies to current target <em>only</em></li>
</ul>
</li>
<li><b><code>INTERFACE</code></b>
<ul>
<li>Applies to dependents <em>only</em> <small class="rmrk">(transitive)</small></li>
</ul>
</li>
</ul>
<pre style="width: 70%; font-size: 0.7em; margin-top: 1em;" class="fragment"><code class="hljs cmake" data-trim data-line-numbers><script type="text/template">
add_library(liba ...)
target_compile_options(liba <SCOPE> "-flag")
add_library(libb ...)
target_link_libraries(libb PRIVATE liba)</script></code></pre>
<table class="fragment vertline" style="margin-top: 1em; font-size: 0.9em;">
<thead>
<tr>
<th></th>
<th><code>PUBLIC</code></th>
<th><code>PRIVATE</code></th>
<th><code>INTERFACE</code></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>liba</code></td>
<td><code>-flag</code></td>
<td><code>-flag</code></td>
<td></td>
</tr>
<tr>
<td><code>libb</code></td>
<td><code>-flag</code></td>
<td></td>
<td><code>-flag</code></td>
</tr>
</tbody>
</table>
<small class="rmrk" style="font-size: 0.56em;"><a
href="https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-usage-requirements">https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-usage-requirements</a></small>
</section>
<section class="full">
<h4>CMake concepts: scope and usage requirements</h4>
<hr>
<ul>
<li>Reconsider the <code>poly</code> example from earlier:</li>
</ul>
<pre><code class="hljs cmake" data-trim data-line-numbers><script type="text/template">
# Find dependencies we rely on
find_package(Eigen3 REQUIRED)
# Define the "poly" library and specify the source files it consists of
add_library(poly
"include/poly/poly.hpp"
"include/poly/interpolate.hpp"
"src/interpolate.cpp"
)
# The library requires the 2020 revision of the C++ standard
target_compile_features(poly PUBLIC cxx_std_20)
# Specify the path of the header files
target_include_directories(poly PUBLIC "include")
# Link the Eigen library into our poly library
target_link_libraries(poly PUBLIC Eigen3::Eigen)
# Add custom compiler options or macros
if (POLY_INIT_NAN)
target_compile_definitions(poly PUBLIC EIGEN_INITIALIZE_MATRICES_BY_NAN)
endif()</script></code></pre>
</section>
<section class="full">
<h4>CMake concepts: scope and usage requirements</h4>
<hr>
<ul>
<li>Which scope to use for compiler <span class="alert">warnings</span>?
<ul style="font-size: 0.8em;">
<li>Want to apply them to our own code ...</li>
<li>... but not impose them on our users!</li>
</ul>
<ul class="arrow fragment">
<li style="margin-top: 0.25em;"><code>PRIVATE</code></li>
</ul>
</li>
</ul>
<pre style="font-size: 0.7em;" class="fragment"><code class="hljs cmake" data-trim data-line-numbers>
# Interface libraries have no source files, only compiler flags etc.
add_library(warnings INTERFACE)
# Add compiler warnings for GCC
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(warnings INTERFACE "-Wall" "-Wextra" "-pedantic")
endif()
# Create an actual library target
add_library(poly "src/interpolate.cpp" ...)
# Apply warnings only when building poly, not when building dependents
target_link_libraries(poly PRIVATE warnings)
# This could be a user program that uses the poly library
add_executable(user_program "main.cpp")
# Compiled without warnings, thanks to private link between poly and warnings
target_link_libraries(user_program PRIVATE poly)</code></pre>
</section>
<section class="full">
<h4>CMake: further reading</h4>
<hr>
<ul style="font-size: 0.9em;">
<li>Variable scope and cache variables <br><small class="rmrk">(<a
href="https://cmake.org/cmake/help/book/mastering-cmake/chapter/CMake%20Cache.html">cmake.org/cmake/help/book/mastering-cmake/chapter/CMake
Cache.html</a>)</small></li>
<li>Find modules and package configuration files <br><small class="rmrk">(<a
href="https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html">cmake.org/cmake/help/book/mastering-cmake/chapter/Finding
Packages.html</a>)</small></li>
<li>Variables, strings, lists <br><small class="rmrk">(<a
href="https://cmake.org/cmake/help/latest/manual/cmake-language.7.html">cmake.org/cmake/help/latest/manual/cmake-language.7.html</a>)</small>
</li>
<li>Export headers for shared libraries <br><small class="rmrk">(<a
href="https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html">cmake.org/cmake/help/latest/module/GenerateExportHeader.html</a>)</small>
</li>
<li>Installation of files and targets <br><small class="rmrk">(<a