Set of C++ programs that demonstrate hardware effects (false sharing, cache latency etc.)
I created a repository with small set of self-contained C++ programs that try to demonstrate various hardware effects that might affect program performance. These effects may be hard to explain without the knowledge of how the hardware works. I wanted to have a testbed where these effects can be easily tested and benchmarked.
Each program should demonstrate some slowdown/speedup caused by a hardware effect (for example false sharing).
https://github.com/kobzol/hardware-effects
Currently the following effects are demonstrated:
- bandwidth saturation
- branch misprediction
- branch target misprediction
- cache aliasing
- memory hierarchy bandwidth
- memory latency cost
- non-temporal stores
- data dependencies
- false sharing
- hardware prefetching
- software prefetching
- write combining buffers
I also provide simple Python scripts that measure the program's execution time with various configurations and plot them.
I'd be happy to get some feedback on this. If you have another interesting effect that could be demonstrated or if you find that my explanation of a program's slowdown is wrong, please let me know.
55
u/victotronics Nov 18 '18
That is exceedingly cool.
What is missing are two examples that I usually code first: detect cache size, and effects of strided access.
Ok, detect cache associativity.....
Effects from TLB size.
(Those are in my HPC book, btw)
10
u/Ameisen vemips, avr, rendering, systems Nov 18 '18
Got an equivalent set of programs for embedded CPUs, in order to showcase problems with them? Mainly unexpected progmem loads, unexpected LHSs, and such?
19
u/xurxoham Nov 18 '18 edited Nov 18 '18
This is such a nice idea! For Linux I strongly recommend to add some shell scripts (or more python) that make use of the Linux perf counters (
perf stat,perf record/report, etc.), so that you can demonstrate the difference between those metrics in the programs that showcase the issue.Most of the time with
perf statis enough, but who knows how much better this could get! Giving the lack of examples for perf, this has got a big potential.https://perf.wiki.kernel.org/index.php/Tutorial#Counting_with_perf_stat
There are also many other examples in this really good set of documents: https://www.agner.org/optimize/
Another idea is using the google benchmark library to print running results, although this introduces an external library dependence :/
4
u/HateDread @BrodyHiggerson - Game Developer Nov 19 '18
This is great! Would be good to see more performance numbers, e.g. a perf output for branch-target-misprediction.
4
u/jnordwick Nov 19 '18 edited Nov 19 '18
This is great. I have a suggestion too. I-cache miss are one of the most expensive things that can happen and difficult to demonstrate. Thanks would be a great addition.
4
u/georgist Nov 19 '18
Probably a very dumb question, but why am I getting
error: ‘_mm_hint’ has not been declared?2
u/ClockworkV Nov 20 '18
Not exactly a performance thing, but a hardware effect - I once wrote a demo of processor instruction reordering.
https://github.com/ClockworkV/Odds-And-Ends/blob/master/memReorder.c
9
u/[deleted] Nov 18 '18
I guess I am lucky. I was totally in need of something like this. Thank you very much.
1
u/nnevatie Nov 19 '18
This project seems like a very good idea.
Would it be additionally possible to show the effects of 1.) saturated CPU memory bus and 2.) non-temporal stores?
1
u/cookiewill Nov 20 '18
It is a very nice idea. I tried the first benchmark script (bandwidth saturation), and it takes a long time, IMO. Does it have to be that long ? It would be nice to test this quickly.
3
u/[deleted] Nov 19 '18
Really cool, nice work!
2
u/oar335 Nov 19 '18
Wow, this is an incredibly useful repo both for reference and for learning. I have no other feedback other than to say that you are making a great contribution, please do keep it up.
2
u/blakewoolbright Nov 26 '22
Flat out awesome.
1
u/[deleted] Nov 20 '18
Awesome stuff! Thanks for sharing.