Repository navigation
Description
History
This holds a few of my notes on how to speed up numpy for large arrays. This conversation started with a comment from @mrocklin that he was able to achieve 5GB/s copy speed first mentioned in #7544
This is on the lower end of the copy bandwidth for modern workstations that can have quad channel memory that can anywhere between 5-10GB/s without the use of openmp.
@juliantaylor mentionned that part of the slowdown is due to pagefaults that cause calls to the OS on every memory page of the OS.
Question:
- Does anybody have a good guide to setup hugepages on linux for use with numpy?
- Does an analog exist for windows?
- Does an analog exist for Mac?
New benchmarks
Following the lead that @juliantaylor mentioned, I decided to go in look into the difference that HugePages would make.
Installation instructions
I don't really know the correct install procedure, maybe somebody can chime in here, Some combination of ``` sudo apt install libhugetlbfs0 ``` The instructions [here](https://help.ubuntu.com/community/KVM%20-%20Using%20Hugepages) This stack exchange [post](https://askubuntu.com/questions/597372/how-do-i-modify-sys-kernel-mm-transparent-hugepage-enabled)To turn Huge pages on, I had to do something like
echo always > /sys/kernel/mm/transparent_hugepage/enabled
Setting it to madvise did not allocate huge pages for numpy/python. To turn huge pages off, one of these two lines
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/enabled
Memory benchmarks -- Practical maximum copy speed 10GB/s
System:- Ubuntu 16.04
- i7-7700HQ
- 6MB cache
- 16GB Ram, dual channel (2x8GB) 2400MHz
Theoretical maximum bandwidth:
In [4]: 2400E6 * 64//8 * 2 /1E9
Out[4]: 38.4 [GB/s]
Following what this article is saying:
http://codearcana.com/posts/2013/05/18/achieving-maximum-memory-bandwidth.html
and using their benchmark https://github.com/awreece/memory-bandwidth-demo
benchmark results (SIZE = 2GB):
read_memory_rep_lodsq: 11.93 GiB/s
read_memory_loop: 19.76 GiB/s
read_memory_sse: 20.10 GiB/s
read_memory_avx: 19.88 GiB/s
read_memory_prefetch_avx: 18.73 GiB/s
write_memory_loop: 11.79 GiB/s
write_memory_rep_stosq: 26.00 GiB/s
write_memory_sse: 14.04 GiB/s
write_memory_nontemporal_sse: 31.43 GiB/s
write_memory_avx: 11.75 GiB/s
write_memory_nontemporal_avx: 31.55 GiB/s
write_memory_memset: 26.78 GiB/s
read_memory_rep_lodsq_omp: 26.72 GiB/s
read_memory_loop_omp: 29.10 GiB/s
read_memory_sse_omp: 29.03 GiB/s
read_memory_avx_omp: 28.92 GiB/s
read_memory_prefetch_avx_omp: 29.06 GiB/s
write_memory_loop_omp: 11.87 GiB/s
write_memory_rep_stosq_omp: 27.74 GiB/s
write_memory_sse_omp: 13.40 GiB/s
write_memory_nontemporal_sse_omp: 30.08 GiB/s
write_memory_avx_omp: 11.73 GiB/s
write_memory_nontemporal_avx_omp: 30.94 GiB/s
write_memory_memset_omp: 27.45 GiB/s
- Maximum write speed without openmp:
write_memory_nontemporal_avx: 31.55 GiB/s
- Maximum read speed without openmp
read_memory_avx: 19.88 GiB/s
The copy speed will be min(read, write) / 2 since each copy requries 1 read and 1 write.
Without huge pages:
import numpy as np
N_GB = 2
a = np.ones(N_GB * 1024 * 1024 * 1024, 'ubyte')
%timeit a.copy()I get 2.3 GB/s (copy speed) on 2400MHz dual channel memory.
b = np.ones_like(a)
%timeit np.copyto(a, b)Gets
10GB/s. (copy speed)
now enabling Hugepages. I think my hugepages are 2MB in size.
The same bechmark gets
- 5.8GB/s for
a.copy()(copy speed) more than a 2x increase in speed - 9.13 GB/s for
np.copyto(a, b)(copy speed) a 10% decrease in speed. Why?
@mrocklin's original benchmark sees the same 2.3 -> 5.8GB/s increase in speed.
For reference @mrocklin's original [benchmark](https://github.com//issues/7544#issue-148203616) also improves significantlyWithout huge pages:
In [1]: b = b'0' * 2 * 1024 * 1024 * 1024
^[[A
In [2]: %time len(b[1:])
CPU times: user 296 ms, sys: 560 ms, total: 856 ms
Wall time: 855 ms
Out[2]: 2147483647
In [3]: 2 / 0.855
Out[3]: 2.3391812865497075- 2.3GB/s copy speed.
With HugePages (2MB)
In [1]: b = b'0' * 2 * 1024 * 1024 * 1024
In [2]: %time len(b[1:])
CPU times: user 244 ms, sys: 104 ms, total: 348 ms
Wall time: 347 ms
Out[2]: 2147483647
In [3]: 2 / 0.347
Out[3]: 5.763688760806917- 5.7GB/s copy speed.
Importance
Many algorithms require arrays to be contiguous. I've found a few places in scipy/numpy where np.ascontiguous is called meaning that this would create significant speedups for people slicing with output arrays on the order of 1000x1000 floats which would become out of cache memory really quickly.
Here is a link from intel discussing this:
https://software.intel.com/en-us/articles/large-matrix-operations-with-scipy-and-numpy-tips-and-best-practices
Activity
juliantaylor commentedon Sep 13, 2018
setting /sys/kernel/mm/transparent_hugepage/enabled to always is usually fine on non ancient kernels.
though for some applications the synchronous defragmentation should be off (/sys/kernel/mm/transparent_hugepage/defrag to defer or madvise)
libhugetablefs should only be needed when you want to mount a ramdisk using hugepages for applications to consume (e.g. java has support for that)
I see no penalty for it enabled.
You can thank redhat for the reason why it is off by default on most distributions, they backported to their enterprise frankenkernels before it was ready and so it broke a lots off stuff (mainly due to concurrency issues in the synchronous defrag), still today you find lots of bad press from back then about it :(
With synchronous defragmentation off I have never seen any issues with it and in really new kernels it they can also be swapped without splitting.
windows and mac also have hugepages, I would not be surprised when they enabled transparent pages by default.
hmaarrfk commentedon Sep 13, 2018
@juliantaylor Thanks for the explanation.
What kernel and distro are you running?
Is Ubuntu 16.04 considered ancient?
juliantaylor commentedon Sep 13, 2018
no, I was speaking more about redhat el6 which is so far I remember based on a heavily patched linux 2.6.32
fwiw as distros have the automatic transparent hugepages turned off it might be a good idea to just manually get them via
madvise, a quick test I did does bring the expected improvement with default distro configuration.hmaarrfk commentedon Sep 13, 2018
Sorry julian, how do you "get them manually via
madvise"Is your quick test sharable?
juliantaylor commentedon Sep 13, 2018
just running your
a.copy()benchmark with this patch:hmaarrfk commentedon Sep 13, 2018
ah ok, so this would need numpy itself to get patched.
Is this a reasonable patch to include in the regular distribution of numpy?
I've never compiled numpy myself.
That might be this weekend's task.
juliantaylor commentedon Sep 13, 2018
yes I think it is reasonable when only performed on large arrays. I could create a PR this weekend.
hmaarrfk commentedon Sep 13, 2018
Your current implementation does it for arrays of 2MB. Do you think it would be reasonable for slightly smaller arrays. Does numpy have any benchmarks to see where the optimal transition might be?
juliantaylor commentedon Sep 13, 2018
below 2MiB does not make very much sense as the smallest hugepage you can get is 2MiB in size, putting these into hugepages would waste memory.
Also numpy caches arrays up to 1MiB anyway and then typically the C library will also use methods to avoid paging small allocations.
In glibc there is a dynamic threshold when it begins to use allocate fresh memory from the system (via mmap as opposed to sbrk), the threshold adapts itself based on the programs usage and caps at 64MiB large allocations.
So imo madvise should only be used around that size arrays.
hmaarrfk commentedon Sep 13, 2018
Thanks for the great explanation and all the help Julian!