| 1 |
|
| 2 |
GNU APL supports, to some extent, execution of APL programs on multiple |
| 3 |
CPU cores in parallel. |
| 4 |
|
| 5 |
1. Supported Platforms |
| 6 |
====================== |
| 7 |
|
| 8 |
As of SVN 484 parallel execution in GNU APL requires the following: |
| 9 |
|
| 10 |
a. POSIX thread (pthread) support |
| 11 |
|
| 12 |
b. functions pthread_getaffinity_np() and pthread_setaffinity_np() to |
| 13 |
set core affinities (i.e. which thread runs on which CPU core) of the |
| 14 |
threads created by GNU APL for parallel execution. |
| 15 |
|
| 16 |
c. an atomic fetch-and-add function |
| 17 |
|
| 18 |
While a. is normally available on all platforms supported by GNU APL, b. and |
| 19 |
c. may not be available. Recent Linux versions provide a., b., and c., Other |
| 20 |
platforms may be added later. |
| 21 |
|
| 22 |
2. ./configure Options |
| 23 |
====================== |
| 24 |
|
| 25 |
Parallel execution is currently experimental and must be ./configure'd |
| 26 |
explicitly (see also README-2-configure). There are two ./configure options |
| 27 |
related to parallel execution: |
| 28 |
|
| 29 |
CORE_COUNT_WANTED and |
| 30 |
PERFORMANCE_COUNTERS_WANTED |
| 31 |
|
| 32 |
The first option, CORE_COUNT_WANTED, controls how many cores shall be used |
| 33 |
for parallel execution, while the second option, PERFORMANCE_COUNTERS_WANTED, |
| 34 |
enables performance counters in GNU APL. Performance counters are needed for |
| 35 |
fine-tuning the parallelism as explained in the following. |
| 36 |
|
| 37 |
There are also two top-level make targets called 'parallel' and 'parallel1' |
| 38 |
'make parallel' calls ./configure with CORE_COUNT_WANTED=syl while |
| 39 |
'make parallel1 calls ./configure with CORE_COUNT_WANTED=syl and |
| 40 |
PERFORMANCE_COUNTERS_WANTED=yes. |
| 41 |
|
| 42 |
3. Parallelized Functions |
| 43 |
========================= |
| 44 |
|
| 45 |
As of SVN 484 the following APL functions were parallelized: |
| 46 |
|
| 47 |
a. all monadic and dyadic scalar functions |
| 48 |
b. inner products of all dyadic scalar functions |
| 49 |
c. outer products of all dyadic scalar functions |
| 50 |
|
| 51 |
More APL functions may be parallelized later. |
| 52 |
|
| 53 |
|
| 54 |
4. Performance measurements and Fine-tuning |
| 55 |
=========================================== |
| 56 |
|
| 57 |
The first benchmarks performed with the parallelized scalar functions have |
| 58 |
shown that 'light-weight' scalar functions such as + , -, or ⌈ may execute |
| 59 |
faster than their sequential counterparts on one platform but slower on another |
| 60 |
platform. The difference between platforms can be dramatic. |
| 61 |
|
| 62 |
For that reason there is a possibility to fine-tune the break-even points |
| 63 |
where parallel execution is performed in favor of sequential execution. |
| 64 |
There is a break-even point for every monadic and for every dyadic scalar |
| 65 |
function. |
| 66 |
|
| 67 |
|
| 68 |
4.1 Theory |
| 69 |
----------- |
| 70 |
|
| 71 |
The sequential execution time of a scalar function is: |
| 72 |
|
| 73 |
A0 + (B0 × N) cycles, |
| 74 |
|
| 75 |
where A0 is the 'start-up time' for the function, B0 is the 'per-item time |
| 76 |
for the function, and N is the vector length. The actual shape does not matter |
| 77 |
for scalar functions, so adding 1 2 3 4 to itself takes the same time as |
| 78 |
adding 2 2⍴1 2 3 4 to itself. |
| 79 |
|
| 80 |
The sequential start-up time A0 is the same for all monadic scalar functions |
| 81 |
and for all dyadic scalar functions; the start-up time for dyadic functions |
| 82 |
is a little longer than for monadic scalar functions. |
| 83 |
|
| 84 |
Now let c be the number of cores. The parallel execution time on c cores |
| 85 |
is then: |
| 86 |
|
| 87 |
Ac + (Bc × N) cycles, |
| 88 |
|
| 89 |
at least when N is a multiple of c. The difference when N is a not a multiple |
| 90 |
of c can be neglected. Again the parallel start-up time Ac is the same for |
| 91 |
all monadic scalar functions and for all dyadic scalar functions. And Ac ≥ A0; |
| 92 |
the difference (Ac - A0) is the additional start-up and join time for the |
| 93 |
thread pool computing the function in parallel. |
| 94 |
|
| 95 |
From the 4 numbers A0, B0, Ac, and Bc one can compute the break-even point BE, |
| 96 |
which is the vector length at which parallel execution becomes faster than |
| 97 |
sequential execution. The break-even point is: |
| 98 |
|
| 99 |
BE ← (Ac - A0) ÷ (B0 - Bc) |
| 100 |
|
| 101 |
The break-even point only exists if Bc < B0, that is if the parallel per-item |
| 102 |
cost Bc is smaller than than the sequential per-item cost B0. On some |
| 103 |
architectures Bc = B0 ÷ c (and then (Ac - A0) is the only concern) but |
| 104 |
unfortunately for multi-core CPUs with a shared memory this is not the case. |
| 105 |
|
| 106 |
4.2 Measuring A0, Ac, B0, and Bc |
| 107 |
-------------------------------- |
| 108 |
|
| 109 |
There is a workspace 'ScalarBenchmark.apl' shipped with GNU APL that measures |
| 110 |
the parameters A0, Ac, B0, and Bc and writes a file 'parallel_thresholds; that |
| 111 |
can be fed back to GNU APL in order to set the break-even points for all |
| 112 |
scalar functions. The workspace can also plot the results using aplplot. |
| 113 |
|
| 114 |
The workspace is used as follows: |
| 115 |
|
| 116 |
a. 'make parallel1' as discussed above |
| 117 |
b. adjust some parameters at the beginning of ScalarBenchmark.apl (number |
| 118 |
of cores, plotting yes/no, and loop counts. |
| 119 |
c. run, e.g.: src/apl -f workspaces/ScalarBenchmark.apl |
| 120 |
|
| 121 |
The last step c. creates a file 'parallel_thresholds' in the current directory. |
| 122 |
If that file is placed in one of the directories used for the 'preferences' |
| 123 |
file (/etc/gnu-apl.d/ or /usr/local/etc/gnu-apl.d/ depending on ./configure |
| 124 |
prefix, or $HOME/.gnu-apl.d, or $HOME/.config/gnu-apl.d) the GNU APL reads |
| 125 |
it on start-up and sets the break-even points for the scalar functions |
| 126 |
accordingly. |
| 127 |
|
| 128 |
The 'ScalarBenchmark.apl' first determines A0 and Ac using small vector |
| 129 |
lengths N. With today's CPUs the measurement results vary considerably, |
| 130 |
Therefore several iterations are performed for each vector length and the |
| 131 |
minimum of all iterations is taken. From these minimums the least-square |
| 132 |
regression line for all lengths is computed. |
| 133 |
|
| 134 |
Then 'ScalarBenchmark.apl' determines the per-item costs B0 and Bc using a |
| 135 |
fairly large vector length N. |
| 136 |
|
| 137 |
4.3 Example 1: Intel 4-core i5-4570 CPU using 3 cores on 64-bit linux |
| 138 |
--------------------------------------------------------------------- |
| 139 |
|
| 140 |
We discuss only two dyadic functions, + and ⋆. + is 'light-weight' and ⋆ |
| 141 |
is not. The summary shown by 'ScalarBenchmark.apl' for + and ⋆ is: |
| 142 |
|
| 143 |
-------------- + Mix_IRC -------------- |
| 144 |
average sequential startup cost: 58 cycles |
| 145 |
average parallel startup cost: 690 cycles |
| 146 |
per item cost sequential: 61 cycles |
| 147 |
per item cost parallel: 95 cycles |
| 148 |
parallel break-even length: not reached |
| 149 |
|
| 150 |
The good news is that the extra cost for parallel start-up and join of |
| 151 |
(690 - 58) = 632 cycles are fairly low. We had earlier made benchmarks with |
| 152 |
other parallel libraries which were semaphore based and showed start-up cost |
| 153 |
in the 10000-20000 cycle ballpark. |
| 154 |
|
| 155 |
The bad news is that the parallel per-item cost for + are higher than the |
| 156 |
sequential per-item cost and therefore there is no break-even point for +. |
| 157 |
The per-item cost of 61 cycles is rather close to the values of a separate |
| 158 |
benchmark that measured the main memory access times alone (see |
| 159 |
tools/memory_benchmark). Our best explanation for not reaching a break-even |
| 160 |
for + (on this machine) is that there are more extra cycles caused by |
| 161 |
main-memory access conflicts of the different cores than cycles saved |
| 162 |
by parallelizing +. |
| 163 |
|
| 164 |
For ⋆ the result is different because ⋆ is not as light-weight as +: |
| 165 |
|
| 166 |
-------------- ⋆ Mix_IRC -------------- |
| 167 |
average sequential startup cost: 58 cycles |
| 168 |
average parallel startup cost: 690 cycles |
| 169 |
per item cost sequential: 147 cycles |
| 170 |
per item cost parallel: 91 cycles |
| 171 |
parallel break-even length: 12 |
| 172 |
|
| 173 |
|
| 174 |
4.4 Example 2: Intel 2-core E6550 CPU using 2 cores on 32-bit linux |
| 175 |
------------------------------------------------------------------- |
| 176 |
|
| 177 |
On this older machine the results were rather different: |
| 178 |
|
| 179 |
-------------- Mix_IRC + Mix1_IRC -------------- |
| 180 |
average sequential startup cost: 2027 cycles |
| 181 |
average parallel startup cost: 2340 cycles |
| 182 |
per item cost sequential: 362 cycles |
| 183 |
per item cost parallel: 210 cycles |
| 184 |
parallel break-even length: 3 |
| 185 |
|
| 186 |
-------------- Mix_IRC ⋆ Mix_IRC -------------- |
| 187 |
average sequential startup cost: 2027 cycles |
| 188 |
average parallel startup cost: 2340 cycles |
| 189 |
per item cost sequential: 647 cycles |
| 190 |
per item cost parallel: 340 cycles |
| 191 |
parallel break-even length: 2 |
| 192 |
|
| 193 |
All functions were faster when executed parallel. The per-item cost were |
| 194 |
considerably higher (in part due to the 32-bit linux) but the parallel |
| 195 |
start-up penalty (2340 - 2027) = 313 was smaller. |
| 196 |
|
| 197 |
Note that the start up cost varies considerably between different runs of |
| 198 |
the same benchmark on the same machine. Therefore you should run several |
| 199 |
measurements before trusting the result. |
| 200 |
|
| 201 |
4.5 Other observations |
| 202 |
---------------------- |
| 203 |
|
| 204 |
Another, somewhat unexpected observation made when looking at several |
| 205 |
benchmarks is that the optimal number of cores seems to be N-1 (i.e. not N) |
| 206 |
on an N-core CPU. |
| 207 |
|
| 208 |
The reason is that other regular operating system activities such |
| 209 |
as timer interrupts occur during the APL execution. The threads used by GNU |
| 210 |
APL get an almost even amount of work and the threads are running at 100% |
| 211 |
load unless the interpreter is blocked on user input. [This is a consequence |
| 212 |
of using a fetch-and-add function instead of semaphores for synchronizing the |
| 213 |
threads.] |
| 214 |
|
| 215 |
If all N cores of a CPU are used by APL then the CPU schedules its activities |
| 216 |
on one of the busy cores (and most likely the same core all the time). |
| 217 |
That core then takes more time than the others and the entire computation\ |
| 218 |
of the APL function is delayed accordingly. |
| 219 |
|
| 220 |
If, however, only N-1 of N cores are used then the OS sees an idle core and |
| 221 |
will most likely perform its activities on that core. These effects are best |
| 222 |
visualized when using the plotting option of ScalarBenchmark.apl. |
| 223 |
|
| 224 |
5. Summary |
| 225 |
========== |
| 226 |
|
| 227 |
The above discussion shows that some speedup can be achieved by parallelizing |
| 228 |
scalar APL functions. However the speedup is not as significant as a naive |
| 229 |
observer might expect (i.e. B0÷Bc = c) and varies considerably between OS |
| 230 |
variants and hardware platforms. |
| 231 |
|
| 232 |
While the start-up costs of the current implementation in GNU APL look good |
| 233 |
compared to other approaches, the per-item costs are somewhat disappointing |
| 234 |
on some machines (and this is due to effects outside GNU APL). A corollary |
| 235 |
of this could be that APL operations with little computational complexity |
| 236 |
(such as A⍴B) will suffer as well and will not show good speedups when |
| 237 |
executed in parallel. |
| 238 |
|
| 239 |
In any case is it worthwhile to fine-tune the break-even points of the |
| 240 |
different scalar functions. |
| 241 |
|