You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Regarding the f2py regression in NumPy 1.9 with failures on 32-bit Windows, the question is whether NumPy should start to use an allocator which gives guaranteed alignment.
There are not many ways to do this and similar code is floating around on the net, so extending it in this way is probable ok. (And besides it does not implement realloc.)
Dropping this code into numpy/core/include/numpy/ndarraytypes.hshould ensure that freshly allocated ndarrays are properly aligned on all platforms.
This platform-independent code could possibly be replaced with posix_memalign() on POSIX and _aligned_malloc() on Windows. However, combining posix_memalign() with realloc() is not possible, so implementing it ourselves is probably better.
I have already a branch which adds an aligned allocator, i'll dig it out.
By using something like this we throw away the option to uses pythons tracemalloc framework and sparse memory (there is no aligned_calloc). @njsmith would you be willing to engage with python devs again to add yet another allocator to their slot before 3.5 is released? They already added calloc only for us, would be a schame if we now couldn't use it.
Presumably one could pass in alignment in the context data of PyMemAllocatorEx? But NumPy has to support Python versions from 2.6 and up, so doing this in Python 3.5 might not solve the problem.
I do think engaging with the python devs on this before 3.5 is a good idea,
but I still am not convinced we have a good reason to use an aligned
allocator in the near term. It cannot possibly be the case that struct {
double, double } actually requires better-than-malloc alignment on win32 or
SPARC, because if that were true then nothing would work.
On 26 Nov 2014 09:10, "Julian Taylor" [email protected] wrote:
I have already a branch which adds an aligned allocator, i'll dig it out.
By using something like this we throw away the option to uses pythons
tracemalloc framework and sparse memory (there is no aligned_calloc). @njsmithhttps://github.com/njsmith would you be willing to engage with
python devs again to add yet another allocator to their slot before 3.5 is
released? They already added calloc only for us, would be a schame if we
now couldn't use it.
—
Reply to this email directly or view it on GitHub #5312 (comment).
The question with regard to f2py was what alignment Fortran would need, not the minimum requirement of C. Speed is also an issue. Both indexing and SIMD works better if the data is properly aligned.
A reason for using aligned allocator could indeed be speed, and ensuring SSE/AVX
compatibility would remove the numerical jitter that comes from taking different
code paths for differently aligned data.
.
f2py is older than the ISO C binding standard in Fortran, and the way it
works is essentially the de facto standard way on interfacing Fortran
with C, used extensively by everyone. In light of this experience, it's
clear that alignment provided by system malloc is sufficient for the
Fortran compilers that matter in practice for us.
The main complication on providing aligned allocation is that ATM we can
either hook into the tracemalloc infrastructure xor do aligned allocation,
and fixing this will require some coordination with CPython upstream (see #4663).
On 5 Dec 2014 16:44, "Sturla Molden" [email protected] wrote:
@pitrouhttps://github.com/pitrou And 64 bit is recommended for Xeon
Phi. Take a look at the comment behind the definition of NPY_MEMALIGN in
my code example.
—
Reply to this email directly or view it on GitHub #5312 (comment).
Given the complications with realloc(), it might not be realistic to expect CPython to solve this in the 3.5 timeframe. Numpy should perhaps use its own aligned allocated wrapper instead (which should be able to defer to the PyMem API, and take advantage of tracemalloc, anyway).
Code for such an allocator is included above. I don't understand @juliantaylor 's argument, but he probably understands this better than me.
I can understand what he meant about calloc though. A calloc is not simply a malloc and a memset to zero. A memset will require the OS to fetch the pages before they are needed. AFAIK there is no PyMem_Calloc.
Actually CPython 3.5 has PyMem_Calloc and friends.
I think @juliantaylor was considering the implementation case of using OS functions (posix_memalign, etc.). But that doesn't sound necessary.
By the way @sturlamolden, your snippet redefines PyArray_Malloc and friends, but array allocation seems to use PyDataMem_NEW. Am I misunderstanding something?
On Sun, Nov 17, 2019, 12:04 Matti Picus ***@***.***> wrote:
what was the original source of the code?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#5312?email_source=notifications&email_token=ABKTSRKKJC4K6C4LW4GFYULQUEXNDA5CNFSM4AYDJQ42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEIKJZY#issuecomment-554738919>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKTSRIVMQFEJ5EP227PXL3QUEXNDANCNFSM4AYDJQ4Q>
.
It was a github post from Sturla. There was no original code file.
On Sun, Nov 17, 2019, 12:04 Matti Picus ***@***.***> wrote:
> what was the original source of the code?
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#5312?email_source=notifications&email_token=ABKTSRKKJC4K6C4LW4GFYULQUEXNDA5CNFSM4AYDJQ42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEIKJZY#issuecomment-554738919>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABKTSRIVMQFEJ5EP227PXL3QUEXNDANCNFSM4AYDJQ4Q>
> .
>
Does everyone who contributes 50 lines of code to Numpy get a dedicated copyright header? I might go through my contributions and see if any applies :-)
It looks like I am no longer contributor for the code I have written for NumPy 🧐:
You are and will always be:)
Does everyone who contributes 50 lines of code to Numpy get a dedicated copyright header? I might go through my contributions and see if any applies :-)
Nope. We try to avoid encoding such things inside the source code, since that will always be wildly incomplete and hard to maintain. We do ask people to list themselves in THANKS.txt; I'm looking at a better alternative to that, because that file often gives merge conflicts.
I am going to close the issue. Happy about a new one though! (It seems most of the discussion is simply outdated and should be re-evaluated based on Matti's work in gh-17582)
Note that it is now – with the next release – possible to write a context manager outside NumPy to swap in an aligned allocator. Which alleviates the need to push it directly into NumPy and gives a chance for much clearer testing of the benefits.
Activity
sturlamolden commentedon Nov 26, 2014
Here is one example of an allocator that should work on all platforms. It is shamelessly based on this:
https://sites.google.com/site/ruslancray/lab/bookshelf/interview/ci/low-level/write-an-aligned-malloc-free-function
There are not many ways to do this and similar code is floating around on the net, so extending it in this way is probable ok. (And besides it does not implement realloc.)
Dropping this code into
numpy/core/include/numpy/ndarraytypes.hshould ensure that freshly allocated ndarrays are properly aligned on all platforms.This platform-independent code could possibly be replaced with
posix_memalign()on POSIX and_aligned_malloc()on Windows. However, combiningposix_memalign()withrealloc()is not possible, so implementing it ourselves is probably better.[-]Use an aligned allocator for NumPy[/-][+]Use an aligned allocator for NumPy?[/+]juliantaylor commentedon Nov 26, 2014
I have already a branch which adds an aligned allocator, i'll dig it out.
By using something like this we throw away the option to uses pythons tracemalloc framework and sparse memory (there is no aligned_calloc).
@njsmith would you be willing to engage with python devs again to add yet another allocator to their slot before 3.5 is released? They already added calloc only for us, would be a schame if we now couldn't use it.
sturlamolden commentedon Nov 26, 2014
Presumably one could pass in alignment in the context data of
PyMemAllocatorEx? But NumPy has to support Python versions from 2.6 and up, so doing this in Python 3.5 might not solve the problem.njsmith commentedon Nov 26, 2014
I do think engaging with the python devs on this before 3.5 is a good idea,
but I still am not convinced we have a good reason to use an aligned
allocator in the near term. It cannot possibly be the case that struct {
double, double } actually requires better-than-malloc alignment on win32 or
SPARC, because if that were true then nothing would work.
On 26 Nov 2014 09:10, "Julian Taylor" [email protected] wrote:
sturlamolden commentedon Nov 26, 2014
The question with regard to f2py was what alignment Fortran would need, not the minimum requirement of C. Speed is also an issue. Both indexing and SIMD works better if the data is properly aligned.
pv commentedon Nov 26, 2014
A reason for using aligned allocator could indeed be speed, and ensuring SSE/AVX
compatibility would remove the numerical jitter that comes from taking different
code paths for differently aligned data.
.
f2py is older than the ISO C binding standard in Fortran, and the way it
works is essentially the de facto standard way on interfacing Fortran
with C, used extensively by everyone. In light of this experience, it's
clear that alignment provided by system malloc is sufficient for the
Fortran compilers that matter in practice for us.
pitrou commentedon Dec 5, 2014
Note a 32-byte alignment is recommended for AVX by Intel : https://software.intel.com/en-us/articles/practical-intel-avx-optimization-on-2nd-generation-intel-core-processors
sturlamolden commentedon Dec 5, 2014
@pitrou And 64 byte alignment is recommended for Xeon Phi. Take a look at the comment behind the definition of
NPY_MEMALIGNin my code example.njsmith commentedon Dec 5, 2014
The main complication on providing aligned allocation is that ATM we can
either hook into the tracemalloc infrastructure xor do aligned allocation,
and fixing this will require some coordination with CPython upstream (see
#4663).
On 5 Dec 2014 16:44, "Sturla Molden" [email protected] wrote:
pitrou commentedon Dec 5, 2014
So the CPython issue is at http://bugs.python.org/issue18835.
pitrou commentedon Jan 15, 2015
Given the complications with realloc(), it might not be realistic to expect CPython to solve this in the 3.5 timeframe. Numpy should perhaps use its own aligned allocated wrapper instead (which should be able to defer to the PyMem API, and take advantage of tracemalloc, anyway).
sturlamolden commentedon Jan 15, 2015
Code for such an allocator is included above. I don't understand @juliantaylor 's argument, but he probably understands this better than me.
I can understand what he meant about calloc though. A calloc is not simply a malloc and a memset to zero. A memset will require the OS to fetch the pages before they are needed. AFAIK there is no PyMem_Calloc.
pitrou commentedon Jan 15, 2015
Actually CPython 3.5 has
PyMem_Callocand friends.I think @juliantaylor was considering the implementation case of using OS functions (posix_memalign, etc.). But that doesn't sound necessary.
By the way @sturlamolden, your snippet redefines PyArray_Malloc and friends, but array allocation seems to use PyDataMem_NEW. Am I misunderstanding something?
44 remaining items
mattip commentedon Sep 27, 2019
In merging randomgen, we gained
PyArray_realloc_alignedand friends. Should we move these routines intonumpy/core/include?jakirkham commentedon Nov 5, 2019
That would certainly be useful, @mattip. Would it be possible to access this functionality from Python as well?
sturlamolden commentedon Nov 17, 2019
Ripped off my code, did ya? 😂
sturlamolden commentedon Nov 17, 2019
It looks like I am no loger contributor for the code I have written for NumPy 🧐:
https://github.com/numpy/numpy/blob/v1.17.2/numpy/_build_utils/src/apple_sgemv_fix.c
https://github.com/numpy/numpy/blob/v1.17.2/numpy/random/src/aligned_malloc/aligned_malloc.h
bashtage commentedon Nov 17, 2019
mattip commentedon Nov 17, 2019
what was the original source of the code?
sturlamolden commentedon Nov 17, 2019
The link is dead, but it was adapted from an aligned malloc that looked like this:
https://tianrunhe.wordpress.com/2012/04/23/aligned-malloc-in-c/
bashtage commentedon Nov 17, 2019
sturlamolden commentedon Nov 17, 2019
#5312 (comment)
bashtage commentedon Nov 17, 2019
pitrou commentedon Nov 17, 2019
Does everyone who contributes 50 lines of code to Numpy get a dedicated copyright header? I might go through my contributions and see if any applies :-)
rgommers commentedon Nov 17, 2019
You are and will always be:)
Nope. We try to avoid encoding such things inside the source code, since that will always be wildly incomplete and hard to maintain. We do ask people to list themselves in
THANKS.txt; I'm looking at a better alternative to that, because that file often gives merge conflicts.seberg commentedon Nov 5, 2021
I am going to close the issue. Happy about a new one though! (It seems most of the discussion is simply outdated and should be re-evaluated based on Matti's work in gh-17582)
Note that it is now – with the next release – possible to write a context manager outside NumPy to swap in an aligned allocator. Which alleviates the need to push it directly into NumPy and gives a chance for much clearer testing of the benefits.
jakirkham commentedon Nov 5, 2021
Also there is a tracking issue with follow up items ( #20193 ). One of them is allocators with specific alignment ( #20193 (comment) )