work in progress
It is useful to know how many operations, and what type of operations, your code is doing. You can compare your actual throughput to the manufacturer's peak throughput to help understand how efficient your code is.
A typical metric is "the number of FLOPs per second". A FLOP is a "floating point operation". This is taking two real numbers and doing a mathematical operation, such as addition, subtraction, multiplication or division. It may include also finding a minimum or a maximum or calculating one number to the power of the other number. It is important to note that not all operations typically take the same number of processor core cycles. For example, division typically takes more cycles than subtraction.
There are some key points to note, particularly as AI drives changes to underlying ISA and hardware
- typically it takes twice as many cycles to do a double precision FLOP than a single precision FLOP
- many architectures (particularly GPUs) support half precision
- many codes also have significant elements of integer arithmetic. Sometimes this is implicit, such as iterators for a DO or for loop
Another common metric is the number of instructions executed by a code. In this case the differences relating to precision are not usually discussed.
Calculate the Number of Operations executed by your code
The number of operations is most likely to be a function of the data being processed. It is important to use representative data when modelling the number of operations a code may take. A typical code has loops over input data and branches dependent upon values of the input data.
In theory, one can view the source code and calculate by hand how many operations a code may take. For example, for a naive matrix multiplication code (for square matrices of extent N) there will be three loops over N values, giving N**3 floating point operations (if the matrices are implemented as real numbers) plus some integer arithmetic, and cycles spent (de)allocating memory and reading/writing the input/result.
There are also various dynamic approaches, where you run the code and a program counts the instructions. This typically requires low level access and, for Linux, setting
/proc/sys/kernel/perf_event_paranoid to 0
This setting is reset at reboot. You can enable it permanently by adding this line
kernel.perf_event_paranoid = 0
to your /etc/sysctl.conf
file and reloading via
sysctl --system
Perf
The perf stat command will give a summary of performance metrics, including total number of cycles and of instructions, number of branches, and average instructions per cycle. An example output looks like:
78,043.88 msec task-clock # 0.994 CPUs utilized
8,472 context-switches # 108.554 /sec
4 cpu-migrations # 0.051 /sec
256,545 page-faults # 3.287 K/sec
301,691,985,868 cycles # 3.866 GHz
94,549,519,295 stalled-cycles-frontend # 31.34% frontend cycles idle
703,268,650,478 instructions # 2.33 insn per cycle
# 0.13 stalled cycles per insn
53,048,053,378 branches # 679.721 M/sec
339,778,504 branch-misses # 0.64% of all branches
78.532257643 seconds time elapsed
77.092582000 seconds user
0.955461000 seconds sys
You can see that absolute numbers are given on the left and a "rate" figure is given on the right. The exact output will vary depending upon your system and the version of perf
If your system does not have perf installed/available but you have Intel VTune (part of Intel oneAPI) available, then you can use
amplxe-perf
from the same bin directory as the vtune executable.
Intel VTune
Intel VTune can profile code and has a GUI interface that also provides some interpretation of the data collected. VTune has several types of analysis types, as defined by the -collect option.
For the -collect hotspots analysis, typically used to get a high level overview, for some (yet unknown!) combination of system and version of VTune you (also) get some high-level information in the GUI about microarchitecture usage
If your CPU is supported (e.g. not too old) then the -collect performance-snapshot analysis gives some information on FLOPS, e.g.
Elapsed Time: 39.917s
IPC: 2.225
SP GFLOPS: 0.000
DP GFLOPS: 6.119
x87 GFLOPS: 0.007
Average CPU Frequency: 3.6 GHz
and on microarchitecture usage, e.g.
Microarchitecture Usage: 57.6% of Pipeline Slots
Retiring: 57.6%
Front-End Bound: 5.2%
Bad Speculation: 2.4%
Back-End Bound: 34.8%
Memory Bound: 9.1%
Core Bound: 25.7%
There is also a -collect uarch-exploration" analysis type to delve into how well the microarchitecture is being used. You can use the GUI or some useful CLI commands are below. Typically, vtune will highlight and explain potential issues, giving some indicators of what could be fixed to improve performance.
Some useful vtune commands to get text reports from a microarchitecture analysis set of results in the directory ${RESDIR}
### timing
vtune -report exec-query -rep-knob row-by="RootGrouping" -rep-knob query-format="tree" -rep-knob column-by="TotalElapsedTime|IPCHybridExpanded|GSPFLOPS|GDPFLOPS|GX87FLOPS|AverageFrequency" -r ${RESDIR}
### cores
vtune -report exec-query -rep-knob row-by="RootGrouping" -rep-knob query-format="tree" -rep-knob column-by="AverageLogicalCPUUtilizationCountingMode|AveragePhysicalCPUUtilizationCountingMode" -r ${RESDIR}
### microarch perf
vtune -report exec-query -rep-knob row-by="RootGrouping" -rep-knob query-format="tree" -rep-knob column-by="UArchEfficiency|GETopDown" -r ${RESDIR}
### memory
vtune -report exec-query -rep-knob row-by="RootGrouping" -rep-knob query-format="tree" -rep-knob column-by="MemBound|MemoryBoundGroupHPCPCSummary|AverageDRAMBandwidth" -r ${RESDIR}
### vectorisation
vtune -report exec-query -rep-knob row-by="RootGrouping" -rep-knob query-format="tree" -rep-knob column-by="VectorizationSummary" -r ${RESDIR}
VTune appears to use perf (as per contents of the log directory generated e.g. when doing a "hotspots" analysis). And you can run
amplxe-perf
from the same bin directory as the vtune executable.