| 1 |
= Performance Measurement in GNU APL |
| 2 |
:Author: Jürgen Sauermann, GNU APL |
| 3 |
:page-width: 80em |
| 4 |
|
| 5 |
== Abstract |
| 6 |
|
| 7 |
This document describes two methods for measuring the performance of GNU APL. |
| 8 |
The purpose of such measurements is to find locations in the source code that |
| 9 |
perform poorly. |
| 10 |
|
| 11 |
== Methods |
| 12 |
|
| 13 |
=== gprof profiling |
| 14 |
|
| 15 |
*gprof* is a command line tool that measures how the CPU time of an APL |
| 16 |
session is distributed over the different GNU APL source files. |
| 17 |
|
| 18 |
==== Preparation |
| 19 |
|
| 20 |
Profiling with *gprof* requires that the enire GNU APL code is compiled |
| 21 |
with specific compiler flags. The GNU APL build system sets these flags |
| 22 |
if *gprof* is desired. The simplest way to set these flags is to simply run: |
| 23 |
|
| 24 |
---- |
| 25 |
make gprof # set proper compiler flags in make |
| 26 |
make -C src clean all |
| 27 |
---- |
| 28 |
|
| 29 |
in the top-level GNU APL directory. |
| 30 |
|
| 31 |
==== Measurement |
| 32 |
|
| 33 |
For example: |
| 34 |
|
| 35 |
---- |
| 36 |
cd src |
| 37 |
./apl -T testcases_3/*tc # produce file gmon.out |
| 38 |
gprof ./apl gmon.out > profile # produce file 'profile' from file gmon.out |
| 39 |
---- |
| 40 |
|
| 41 |
The example above profiles GNU APL over all testcases in directory |
| 42 |
*src/testcases_3*. It is up to the user to determine what is being profiled (a |
| 43 |
somewhat broad profile over all GNU APL primitives in the example above. To |
| 44 |
obtain a more narrow profile, say for specific APL primitives, you should: |
| 45 |
|
| 46 |
. Write an APL workspace (or a testcase file) that executes the function of |
| 47 |
interest in a narrow APL loop, then |
| 48 |
. Run apl with that workspace, and finally |
| 49 |
. run gprof with the output file created by step 2. above (by convention this |
| 50 |
file is named *gprof.out*). |
| 51 |
|
| 52 |
As another, more detailed, example, say we want to profile the APL matrix |
| 53 |
division, i.e. the APL code *A ⌹ B*. The workspace in step 1. above could |
| 54 |
then be this: |
| 55 |
|
| 56 |
---- |
| 57 |
#!./apl --script |
| 58 |
|
| 59 |
∇LOOP COUNT |
| 60 |
L: ⊣A⌹B ◊ →(0 < COUNT←COUNT-1)⍴L |
| 61 |
∇ |
| 62 |
|
| 63 |
loop_count←1000000 ⍝ keep APL busy with A ⌹ B |
| 64 |
N←10 |
| 65 |
|
| 66 |
A←⎕RVAL 1 (,N) (0 0 0 1) ⍝ a random 10 element complex vector |
| 67 |
B←⎕RVAL 2 (N,N) (0 0 0 1) ⍝ a random 10×10 complex matrix |
| 68 |
|
| 69 |
|
| 70 |
LOOP loop_count ⍝ run the loop loop_count times |
| 71 |
----- |
| 72 |
|
| 73 |
In step 2. we run the workpace (in directory src): |
| 74 |
|
| 75 |
---- |
| 76 |
./apl -f workspaces/GPROF_LOOP.apl # invoke apl with the above script file |
| 77 |
---- |
| 78 |
|
| 79 |
In step 3. we analyze the result *gmon.out* of step 2. The file *gmon.out* is |
| 80 |
a hard to read binary file with the measurement results. Step 3 converts this |
| 81 |
binary file into a human-readable statistics file named *profile*: |
| 82 |
|
| 83 |
---- |
| 84 |
gprof ./apl gmon.out > profile |
| 85 |
---- |
| 86 |
|
| 87 |
The text file *profile* then contains a breakdown of the CPU time spent in |
| 88 |
different source code functions. Some notes may help to achieve a meaningful |
| 89 |
profile: |
| 90 |
|
| 91 |
. make sure that the APL code focuses on the function(s) of interest, |
| 92 |
. avoid producing any output (the interpreter blocks on output, which may |
| 93 |
obfuscate the profile). |
| 94 |
. make the loop run long enough; particularly when the profiled footprint is |
| 95 |
small. Otherwise some small function may be missing entirely in the profile. |
| 96 |
The longer the loop runs the more reliable is the result. An execution time |
| 97 |
of one minute is a good starting point for a simple APL primitive. |
| 98 |
|
| 99 |
|
| 100 |
=== CPU cycle counters |
| 101 |
|
| 102 |
GNU APL has a number of built-in CPU cycle counters that are spread over |
| 103 |
some GNU APL source files of interest. The existing counters are defined |
| 104 |
in *src/Performance.def*. The counters generate a small runtime overhead |
| 105 |
and therefore have to be enabled explicitly. For example with: |
| 106 |
|
| 107 |
---- |
| 108 |
./configure PERFORMANCE_COUNTERS_WANTED=yes ,,, |
| 109 |
---- |
| 110 |
|
| 111 |
The *./configure* option *DEVELOP_WANTED=yes* automatically sets |
| 112 |
*PERFORMANCE_COUNTERS_WANTED=yes*, but also other debug features that may |
| 113 |
have performance impacts. |
| 114 |
|
| 115 |
The counters are mananged from APL via *⎕FIO*. Every counter has a unique |
| 116 |
ID and can be cleared (before a measurement is started) and read (after a |
| 117 |
measurement). For example: |
| 118 |
|
| 119 |
---- |
| 120 |
⊣ ⎕FIO[200] 16 ⍝ clear CPU cycle counter for dyadic A ÷ B |
| 121 |
⊣ 42.1 ÷ ⍳ 1000 ⍝ perform 1000 divisions |
| 122 |
8 ⎕CR (⎕FIO[201] 16) ⍝ read counter for dyadic A ÷ B |
| 123 |
┌→────────────────────────────────────────────┐ |
| 124 |
│2 ┌→────┐ 1 3087 9529569 999 370713 182857673│ |
| 125 |
│ │A ÷ B│ │ |
| 126 |
│ └─────┘ │ |
| 127 |
└ϵ────────────────────────────────────────────┘ |
| 128 |
---- |
| 129 |
|
| 130 |
The result *Z* of *Z←⎕FIO[201]* is an 8 item vector: |
| 131 |
|
| 132 |
* *Z[1]* statistics type. The statistics types are: |
| 133 |
** 1 for the Cell operations of a monadic scalar APL function. That is, the |
| 134 |
time for _one vector item_, |
| 135 |
** 2 for the Cell operations of a dyadic scalar APL function. That is, the |
| 136 |
time for _one vector item_, |
| 137 |
** 3 for a monadic APL primitive. That is, the time for _one vector_, or |
| 138 |
** 4 for a dyadic APL primitive. That is, the time for _one vector_. |
| 139 |
* *Z[2]* statistics name (the APL operation that was measured), |
| 140 |
* *Z[3]* the number of executions of the first APL vector item. *Z[3]* is *N* |
| 141 |
if, e.g. *N* vectors were measured in a loop, and is 1 if no loop is used |
| 142 |
(like in the example above). |
| 143 |
* *Z[4]* the (sum of) CPU cycles of the first APL vector item. Therefore |
| 144 |
*Z[4] ÷ Z[3]* is the average number of CPU cycles |
| 145 |
* *Z[5]* the (sum of) the squares of the CPU cycles of all the first APL vector |
| 146 |
items. (To compute the variance). If *Z[3] ←→ 1* (one vectror measured) then |
| 147 |
*Z[4]×Z[4] ←→ Z[5]*, |
| 148 |
* *Z[6]* number of executions of all subsequent APL vector items |
| 149 |
* *Z[7]* the (sum of) CPU cycles of all subsequent APL vector items, |
| 150 |
* *Z[8]* the (sum of) the squares of the CPU cycles of the all subsequent APL |
| 151 |
vector items. (To compute the variance), |
| 152 |
|
| 153 |
In 1990 a particular APL operation, when executed again and again, would have |
| 154 |
always used the same number of CPU cycles in every pass. These days, modern |
| 155 |
CPUs feature a number of optimizations (caching, branch prediction, etc.) |
| 156 |
that cause the number of CPU cycles for the same APL operations to differ |
| 157 |
considerably. To get a better understanding of the varying CPU cycles: |
| 158 |
|
| 159 |
* Z[2 3 4] are the CPU cycles for the first item of every APL vector while |
| 160 |
* Z[5 6 7] are the CPU cycles for all subsequent vector items. The former is |
| 161 |
normally considerably larger due to the instruction and data caches of the |
| 162 |
CPU, |
| 163 |
* *Z[3] ÷ Z[2]* and *Z[6] ÷ Z[5]* are the *average number* of CPU cycles for |
| 164 |
the respective (i.e. first vs. subsequent) measurements, and |
| 165 |
* *(Z[4]⋆0.5) ÷ Z[2]* and *Z[7] ÷ Z[5]* are the statistical *variances* of the |
| 166 |
first and subsequent CPU cycles respectively. |
| 167 |
|
| 168 |
The output above, i.e.: |
| 169 |
|
| 170 |
---- |
| 171 |
┌→────────────────────────────────────────────┐ |
| 172 |
│2 ┌→────┐ 1 3087 9529569 999 370713 182857673│ |
| 173 |
│ │A ÷ B│ │ |
| 174 |
│ └─────┘ │ |
| 175 |
└ϵ────────────────────────────────────────────┘ |
| 176 |
|
| 177 |
---- |
| 178 |
|
| 179 |
tells us: The statistics type *Z[1]* was *2* (which, BTW, corresponds to macro |
| 180 |
*perfo_2()* in *Performance.def*. The operation *Z[2]* measured by that |
| 181 |
statistics was *A ÷ B*. The first division, i.e. *42.1 ÷ ↑⍳1000 ←→ 42.1 ÷ 1*, |
| 182 |
took Z[4] ←→ 3087 CPU cycles. The Z[6] ←→ 999 subsequent divisions, |
| 183 |
i.e. *42.1 ÷ 1↓⍳1000 ←→ 42.1 ÷ 2 3 4 ... 1000* took 370713 CPU cycles total, |
| 184 |
or 370713 ÷ 999 ←→ 371 CPU cycles per division. The comparison between the |
| 185 |
first division (3087 CPU cycles) and the average of the subsequent |
| 186 |
divisions (371 CPU cycles) demonstarates the effect of caching, etc. Since |
| 187 |
there was only one first vector item, the variance is the square of that item, |
| 188 |
i.e. Z[5] ←→ 9529569 ←→ 3087×3087 ←→ Z[4]×Z[4] (a single item always has a |
| 189 |
variance of 0). If we had run *A ÷ B* in a loop, say N times, then: |
| 190 |
|
| 191 |
* Z[3] ←→ N, |
| 192 |
* Z[4] ÷ N ←→ the average CPU cycles for the first item, |
| 193 |
* Z[6] ÷ (N×999) ←→ the average CPU cycles for subsequent items, |
| 194 |
* usually Z[5] ≠ Z[4]×Z[4] because the CPU cycles for the first element (in |
| 195 |
each of the N iterations) vary. |
| 196 |
|
| 197 |
In statistics we learn that: |
| 198 |
|
| 199 |
* Variance(X) ←→ Expectation((X - Expectation(X))²), and |
| 200 |
* StandardDeviation(X) ←→ Variance(X) ⋆ 0.5. |
| 201 |
|
| 202 |
The triples *Z[3 4 5]* and *Z[6 7 8]* therefore provide all the information |
| 203 |
that is needed to compute the Expectation and the Variance of the first and |
| 204 |
of all subsequent vector items. GNU APL ships with a handful of workspaces |
| 205 |
(search for *get_statistics* in the *.apl* files) that measure and plot the |
| 206 |
benchmarking of some scalar APL functions. |
| 207 |
|
| 208 |
|
| 209 |
=== Parallel Tuning |
| 210 |
|