[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
STINNER Victor added the comment: I merged my PR 4199 (Document PyObject_Malloc()) and PR 4200 (Cleanup pymalloc) to prepare PR 4089. PR 4089 should now be completed and well tested. The real question is now if we need PyMem_AlignedAlloc()? Stefan Krah and

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread Stefan Krah
Stefan Krah added the comment: > For large allocations, you'll probably be better off implementing your own > aligned allocator on top of calloc than implementing your own calloc on top > of an aligned allocator. (It's O(1) overhead versus O(n).) And once you're > doing

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
STINNER Victor added the comment: New changeset 9ed83c40855b57c10988f76770a4eb825e034cd8 by Victor Stinner in branch 'master': bpo-18835: Cleanup pymalloc (#4200) https://github.com/python/cpython/commit/9ed83c40855b57c10988f76770a4eb825e034cd8 --

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread Nathaniel Smith
Nathaniel Smith added the comment: > But since no fast (kernel-zeroed) aligned_calloc() exists, I must use > memset() anyway. For large allocations, you'll probably be better off implementing your own aligned allocator on top of calloc than implementing your own calloc on

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
Change by STINNER Victor : -- pull_requests: +4168 ___ Python tracker ___ ___

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread Stefan Krah
Stefan Krah added the comment: On Tue, Oct 31, 2017 at 02:55:04PM +, Nathaniel Smith wrote: > 3) also it's not clear what the best approach will look like, given that we > care a lot about using calloc when possible, and have reason to prefer using > regular freeing

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread Antoine Pitrou
Antoine Pitrou added the comment: Le 31/10/2017 à 15:55, Nathaniel Smith a écrit : > > 1) numpy hasn't actually come to a decision about whether to use aligned > allocation at all, or under what circumstances. This isn't the Numpy bug tracker, but I can't help but mention

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread Nathaniel Smith
Nathaniel Smith added the comment: > Can you elaborate why numpy wouldn't use this new API? I designed it with > numpy in mind :-) The reasons I had in mind are: 1) numpy hasn't actually come to a decision about whether to use aligned allocation at all, or under what

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
STINNER Victor added the comment: Stefan Krah: "we care about the C11 restriction? (...) "size - number of bytes to allocate. An integral multiple of alignment" (...) posix_memalign and _aligned_malloc don't care about the multiple." I prefer to ignore this

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
STINNER Victor added the comment: Nathaniel Smith: "Given the complexities here, and that the Track/Untrack functions are public now, I do wonder if the actual aligned allocation routines should just be an internal API (i.e., not exposed in Python.h)." I don't see

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-31 Thread STINNER Victor
STINNER Victor added the comment: Nathaniel: "(...) and numpy won't necessarily use this API anyway." Can you elaborate why numpy wouldn't use this new API? I designed it with numpy in mind :-) Using PyMem_AlignedAlloc() instead of using directly

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-28 Thread Nathaniel Smith
Nathaniel Smith added the comment: Given the complexities here, and that the Track/Untrack functions are public now, I do wonder if the actual aligned allocation routines should just be an internal API (i.e., not exposed in Python.h). --

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-28 Thread Stefan Krah
Stefan Krah added the comment: > The ways we've discussed using aligned allocation in numpy wouldn't follow > this requirement without special checking. Which isn't necessarily a big > deal, and numpy won't necessarily use this API anyway. But I would suggest > being

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-27 Thread Nathaniel Smith
Nathaniel Smith added the comment: > On the other hand, sane requests will have the exact multiple most of the > time anyway. The ways we've discussed using aligned allocation in numpy wouldn't follow this requirement without special checking. Which isn't necessarily a big

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-27 Thread STINNER Victor
STINNER Victor added the comment: PR 4089 becomes much more larger than what I expected, so I propose to defer enhancements to following PR, especially the idea of "emulating" PyMem_AlignedAlloc() on top of PyMem_Malloc() if the user calls PyMem_SetAllocators()

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-27 Thread Stefan Krah
Stefan Krah added the comment: Should we care about the C11 restriction? http://en.cppreference.com/w/c/memory/aligned_alloc "size - number of bytes to allocate. An integral multiple of alignment" posix_memalign and _aligned_malloc don't care about the multiple. On

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-25 Thread Nathaniel Smith
Nathaniel Smith added the comment: > I'm not sure that it's a good idea to provide a "aligned malloc" fallback if > such fallback would be inefficient. For example, we would have to > overallocate the memory block not only for the requested alignement, but also > allocates

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-25 Thread Xavier de Gaye
Xavier de Gaye added the comment: Android has both memalign() [1] and posix_memalign() [2] and does not have aligned_alloc(), posix_memalign() is a wrapper around memalign() [3]. [1] https://android.googlesource.com/platform/bionic/+/master/libc/include/malloc.h#38 [2]

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-25 Thread Stefan Krah
Stefan Krah added the comment: > In Python 3.7, should we also add the "aligned alloc" requirement? Linux, BSD, OSX, MSVC should be covered. According to Stackoverflow MinGW has an internal function. Android, I don't know. Xavier? -- nosy: +xdegaye

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-25 Thread STINNER Victor
STINNER Victor added the comment: Currently, the main question on my PR 4089 was raised by Antoine Pitrou: "Do people have to provide aligned_alloc and aligned_free? Or can they leave those pointers NULL and get a default implementation?" My reply: "Currently, you

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-24 Thread Stefan Krah
Stefan Krah added the comment: [me] > This weakens my use case somewhat [...] I looked at Victor's patch, and thanks to the alignment <= ALIGNMENT optimization it seems that always using the aligned_alloc() and aligned_free() versions for a specific pointer is fast. Nice!

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Benjamin Peterson
Benjamin Peterson added the comment: Having the ability to allocated aligned memory could help avoid some undefined behavior. See #27987 (though, we only need 16-byte alignment there) -- nosy: +benjamin.peterson ___ Python

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Stefan Krah
Stefan Krah added the comment: On Mon, Oct 23, 2017 at 09:16:08PM +, Antoine Pitrou wrote: > > The Arrow memory format for example recommends 64 bit alignment. > > I presume you mean 64 bytes? Yes, I was typing too fast. --

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Antoine Pitrou
Antoine Pitrou added the comment: By the way: > The Arrow memory format for example recommends 64 bit alignment. I presume you mean 64 bytes? -- ___ Python tracker

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Nathaniel Smith
Nathaniel Smith added the comment: There's also aligned calloc, which no native APIs support but is still quite useful. -- ___ Python tracker

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Stefan Krah
Stefan Krah added the comment: On Mon, Oct 23, 2017 at 05:16:53PM +, STINNER Victor wrote: > Memory allocated by PyMem_AlignedAlloc() must be freed with > PyMem_AlignedFree(). > > We cannot reuse PyMem_Free(). On Windows, PyMem_AlignedAlloc() is implemented > with

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread STINNER Victor
STINNER Victor added the comment: I added _PyTraceMalloc_Track() and _PyTraceMalloc_Untrack() private functions to the C API in Python 3.6. These functions were made public in Python 3.7: renamed to PyTraceMalloc_Track() and PyTraceMalloc_Untrack(). I made this

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread STINNER Victor
Change by STINNER Victor : -- pull_requests: +4059 stage: needs patch -> patch review ___ Python tracker ___

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Antoine Pitrou
Change by Antoine Pitrou : -- versions: +Python 3.7 -Python 3.5 ___ Python tracker ___ ___

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Stefan Krah
Stefan Krah added the comment: Yes, I think it is partly convenience. I want to set ... ndt_mallocfunc = PyMem_Malloc; ndt_alignedallocfunc = PyMem_AlignedAlloc; ndt_callocfunc = PyMem_Calloc; ndt_reallocfunc = PyMem_Realloc; ndt_freefunc = PyMem_Free; ...

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Antoine Pitrou
Antoine Pitrou added the comment: Do you need aligned allocation even on small objects? The Python allocator doesn't handle allocations > 512 bytes. -- ___ Python tracker

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-10-23 Thread Stefan Krah
Stefan Krah added the comment: I need this too. I would like to set this https://github.com/plures/ndtypes/commit/c260fdbae707da0dfefef499621a0a9f37a3e509#diff-2402fff6223084b74d97237c0d620b29R50 to something line PyMem_AlignedAlloc(), because the Python allocator is

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2017-01-23 Thread STINNER Victor
STINNER Victor added the comment: Antoine Pitrou: "Benchmarks and Intel's recommendation show that aligned allocation is actually important for AVX performance, and NumPy depends on CPython providing the right allocation APIs (for integration with tracemalloc):

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2015-01-15 Thread Antoine Pitrou
Antoine Pitrou added the comment: Due to the realloc() problem, I'm thinking that the best course for Numpy would be to use its own allocator wrapper like Nathaniel outligned. Also, Numpy wants calloc() for faster allocation of zeroed arrays... --

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2015-01-14 Thread William Scullin
Changes by William Scullin wscul...@gmail.com: -- nosy: +wscullin ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18835 ___ ___ Python-bugs-list

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-08 Thread Trent Nelson
Changes by Trent Nelson tr...@snakebite.org: -- nosy: +trent ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18835 ___ ___ Python-bugs-list mailing

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread Antoine Pitrou
Antoine Pitrou added the comment: Benchmarks and Intel's recommendation show that aligned allocation is actually important for AVX performance, and NumPy depends on CPython providing the right allocation APIs (for integration with tracemalloc): https://github.com/numpy/numpy/issues/5312 So I

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread STINNER Victor
STINNER Victor added the comment: Windows provides: void * _aligned_malloc( size_t size, size_t alignment ); http://msdn.microsoft.com/en-US/library/8z34s9c6%28v=vs.80%29.aspx How should we handle platforms which don't provide a memory allocator with an alignment? The simplest

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread Antoine Pitrou
Antoine Pitrou added the comment: How should we handle platforms which don't provide a memory allocator with an alignment? The simplest option is to return NULL (MemoryError). Are there such platforms? posix_memalign() is a POSIX standard, even OpenBSD has it. --

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread STINNER Victor
STINNER Victor added the comment: PyMem_GetAllocator() and PyMem_SetAllocator() have a domain parameter which can take 3 values: PYMEM_DOMAIN_RAW, PYMEM_DOMAIN_MEM and PYMEM_DOMAIN_OBJ. I don't think that we need 3 flavors of allocators (PyMem_Raw, PyMem, PyObject). Maybe the PYMEM_DOMAIN_RAW

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread Nathaniel Smith
Nathaniel Smith added the comment: It's not terribly difficult to write a crude-but-effective aligned allocator on top of raw malloc: def aligned_malloc(size, alignment): assert alignment 255 raw_pointer = (uint8*) malloc(size + alignment) shift = alignment - (raw_pointer %

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread Antoine Pitrou
Antoine Pitrou added the comment: Le 05/12/2014 23:15, STINNER Victor a écrit : I don't think that we need 3 flavors of allocators (PyMem_Raw, PyMem, PyObject). Maybe the PYMEM_DOMAIN_RAW domain is enough: OS functions don't require the GIL. In this case, should we add a new pair of

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread Nathaniel Smith
Nathaniel Smith added the comment: Re: msg232219: If you go down the route of adding both aligned_malloc and aligned_free to the Allocator structure, I think you might as well support it for all domains? For the PyMem and PyObject domains you can just literally set the default functions to be

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-12-05 Thread STINNER Victor
STINNER Victor added the comment: You cannot just add a new domain because the function prototypes are different (need an extra alignement parameter). You have to add new members to the structure or add a new structure. -- ___ Python tracker

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2014-04-26 Thread STINNER Victor
STINNER Victor added the comment: It looks like a memory allocator with a given alignment would help numpy, for SIMD instructions: https://mail.python.org/pipermail/python-dev/2014-April/134092.html (but Numpy does not currently use aligned allocation, and it's not clear how important it is)

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-09-03 Thread Antoine Pitrou
Antoine Pitrou added the comment: We don't have to align EVERY data structure. But I do have immediate beneficial use cases for set tables and for data blocks in deque objects. Can you explain what the use cases are, and post some benchmarking code? Also, what would be the strategy? Would

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-09-03 Thread STINNER Victor
STINNER Victor added the comment: Linux provides the following functions: int posix_memalign(void **memptr, size_t alignment, size_t size); void *valloc(size_t size); # obsolete void *memalign(size_t boundary, size_t size); # obsolete Windows provides the following functions: void*

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-09-01 Thread Charles-François Natali
Charles-François Natali added the comment: Please don't FUD this one to death. Aligned memory access is sometimes important and we currently have no straight-forward way to achieve it. I guess that a simple way to cut the discussion short would be to have a first implementation, and run

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-08-31 Thread Raymond Hettinger
Changes by Raymond Hettinger raymond.hettin...@gmail.com: -- title: Add aligned memroy variants to the suite of PyMem functions/macros - Add aligned memory variants to the suite of PyMem functions/macros ___ Python tracker rep...@bugs.python.org

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-08-31 Thread Raymond Hettinger
Changes by Raymond Hettinger raymond.hettin...@gmail.com: -- Removed message: http://bugs.python.org/msg196692 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18835 ___

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-08-31 Thread Raymond Hettinger
Raymond Hettinger added the comment: Adding yet another API to allocate memory has a cost Please don't FUD this one to death. Aligned memory access is sometimes important and we currently have no straight-forward way to achieve it. If you're truly worried about adding single new function

[issue18835] Add aligned memory variants to the suite of PyMem functions/macros

2013-08-31 Thread Raymond Hettinger
Raymond Hettinger added the comment: Attaching a patch for what I would like to do with the alignment functions and macros. -- keywords: +patch Added file: http://bugs.python.org/file31543/align.diff ___ Python tracker rep...@bugs.python.org