October 27, 2013
On Sunday, 27 October 2013 at 10:45:31 UTC, Andrei Alexandrescu wrote:
> On 10/26/13 8:00 AM, Andrei Alexandrescu wrote:
>> 3. Mallocator could also implement these on Posix:
>> http://man7.org/linux/man-pages/man3/posix_memalign.3.html. However,
>> Windows requires a specific call for deallocating aligned memory. To
>> accommodate both portably, we leave Mallocator as is and create
>> AlignedMallocator that uses the _aligned_* family on Windows and the
>> respective functions on Posix. On Windows, allocate() requests would
>> pass a default of platformSize (i.e. 16 I suspect) to _aligned_malloc.
>
> Just implemented AlignedMallocator and pushed.
>
> http://erdani.com/d/phobos-prerelease/std_allocator.html#.AlignedMallocator
>
> Untested on Windows.
>
>
> Andrei

In the following line:
----------
On Posix, forwards to realloc. On Windows, calls _aligned_realloc(b.ptr, newSize, platformAlignment).
----------

Link is incorrect (http//, colon is missing).
October 27, 2013
On Sunday, 27 October 2013 at 10:52:06 UTC, Tourist wrote:
> On Sunday, 27 October 2013 at 10:45:31 UTC, Andrei Alexandrescu wrote:
>> On 10/26/13 8:00 AM, Andrei Alexandrescu wrote:
>>> 3. Mallocator could also implement these on Posix:
>>> http://man7.org/linux/man-pages/man3/posix_memalign.3.html. However,
>>> Windows requires a specific call for deallocating aligned memory. To
>>> accommodate both portably, we leave Mallocator as is and create
>>> AlignedMallocator that uses the _aligned_* family on Windows and the
>>> respective functions on Posix. On Windows, allocate() requests would
>>> pass a default of platformSize (i.e. 16 I suspect) to _aligned_malloc.
>>
>> Just implemented AlignedMallocator and pushed.
>>
>> http://erdani.com/d/phobos-prerelease/std_allocator.html#.AlignedMallocator
>>
>> Untested on Windows.
>>
>>
>> Andrei
>
> In the following line:
> ----------
> On Posix, forwards to realloc. On Windows, calls _aligned_realloc(b.ptr, newSize, platformAlignment).
> ----------
>
> Link is incorrect (http//, colon is missing).

That's how Firefox interprets it.
In source code it's:
http://http//msdn.microsoft.com/en-US/library/y69db7sx(v=vs.80).aspx
October 27, 2013
On 10/27/13 3:52 AM, Tourist wrote:
> In the following line:
> ----------
> On Posix, forwards to realloc. On Windows, calls _aligned_realloc(b.ptr,
> newSize, platformAlignment).
> ----------
>
> Link is incorrect (http//, colon is missing).

Thanks, ouch, there were a few more bugs around there as well in the untested code.

Andrei
October 27, 2013
On Sunday, 27 October 2013 at 10:45:31 UTC, Andrei Alexandrescu wrote:
> On 10/26/13 8:00 AM, Andrei Alexandrescu wrote:
>> AlignedMallocator that uses the _aligned_* family on Windows and the
>> respective functions on Posix. On Windows, allocate() requests would
>> pass a default of platformSize (i.e. 16 I suspect) to _aligned_malloc.
>
> Just implemented AlignedMallocator and pushed.
>
> http://erdani.com/d/phobos-prerelease/std_allocator.html#.AlignedMallocator
The constraints on the alignment parameter are neither documented nor checked.

i.e. Alignment must be a power of two, and for posix_memalign it must additionally be greater than sizeof(void*).

I can only think of one use case for needing runtime specified alignment: allocating operating system page sized chunks.
Are there any other use cases?
October 27, 2013
On 10/27/13 11:26 AM, safety0ff wrote:
> On Sunday, 27 October 2013 at 10:45:31 UTC, Andrei Alexandrescu wrote:
>> On 10/26/13 8:00 AM, Andrei Alexandrescu wrote:
>>> AlignedMallocator that uses the _aligned_* family on Windows and the
>>> respective functions on Posix. On Windows, allocate() requests would
>>> pass a default of platformSize (i.e. 16 I suspect) to _aligned_malloc.
>>
>> Just implemented AlignedMallocator and pushed.
>>
>> http://erdani.com/d/phobos-prerelease/std_allocator.html#.AlignedMallocator
>>
> The constraints on the alignment parameter are neither documented nor
> checked.
>
> i.e. Alignment must be a power of two, and for posix_memalign it must
> additionally be greater than sizeof(void*).

Fixed, will push soon.

> I can only think of one use case for needing runtime specified
> alignment: allocating operating system page sized chunks.
> Are there any other use cases?

Cache-line aligned.

http://stackoverflow.com/questions/794632/programmatically-get-the-cache-line-size/1900464

Also some I/O requires aligned buffers.

http://goo.gl/ni860U


Andrei

October 27, 2013
> i.e. Alignment must be a power of two, and for posix_memalign it must additionally be greater than sizeof(void*).

It must be a power of two and a multiple of sizeof(void*), which means that it must be either zero or greater or equal to sizeof(void*).
October 27, 2013
On 10/25/13 7:23 AM, Manu wrote:
> 1. I'm still sad there are no allocAligned() functions or something of
> that type to request explicit alignment with allocations. I'm not sure
> there is sufficient support for requesting alignment with allocations.
> The set-able alignment property approach seems a little weird (and only
> seemed to be supported on one allocator?). I guess experience will tell
> if this is sufficient and/or convenient.
> I'd still like to see an allocWithAlignment() method or something, which
> may be implemented efficiently by allocators that can support it.

Code: http://erdani.com/d/phobos-prerelease/std_allocator.html

Dox: http://erdani.com/d/phobos-prerelease/std_allocator.html

I've made alignedAllocate() and alignedReallocate() parts of the official API, added alignedAlloc to all regions, added AlignedMallocator which taps into the aligned system APIs, and eliminated all that dynamic alignment setting stuff. I think we're in better shape now. Thanks for the suggestion!

I still need to:

1. add stats for aligned calls to AllocatorWithStats

2. define HeapBlock.alignedXxx (which should be interesting)

3. separate IOwns, IAlignedAllocate, IDeallocate from CAllocator and have CAllocatorImpl!alloc conditionally implement them depending on whether alloc implements the respective primitives.


Andrei

October 28, 2013
On 28 October 2013 04:36, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org
> wrote:

> On 10/27/13 11:26 AM, safety0ff wrote:
>
>> On Sunday, 27 October 2013 at 10:45:31 UTC, Andrei Alexandrescu wrote:
>>
>>> On 10/26/13 8:00 AM, Andrei Alexandrescu wrote:
>>>
>>>> AlignedMallocator that uses the _aligned_* family on Windows and the respective functions on Posix. On Windows, allocate() requests would pass a default of platformSize (i.e. 16 I suspect) to _aligned_malloc.
>>>>
>>>
>>> Just implemented AlignedMallocator and pushed.
>>>
>>> http://erdani.com/d/phobos-**prerelease/std_allocator.html#** .AlignedMallocator<http://erdani.com/d/phobos-prerelease/std_allocator.html#.AlignedMallocator>
>>>
>>>  The constraints on the alignment parameter are neither documented nor
>> checked.
>>
>> i.e. Alignment must be a power of two, and for posix_memalign it must additionally be greater than sizeof(void*).
>>
>
> Fixed, will push soon.
>
>
>  I can only think of one use case for needing runtime specified
>> alignment: allocating operating system page sized chunks. Are there any other use cases?
>>
>
> Cache-line aligned.
>
> http://stackoverflow.com/**questions/794632/**programmatically-get-the-** cache-line-size/1900464<http://stackoverflow.com/questions/794632/programmatically-get-the-cache-line-size/1900464>
>
> Also some I/O requires aligned buffers.
>
> http://goo.gl/ni860U


GPU's also tend to deal with strict alignments of various buffers.

SIMD requires >= 16byte alignment, which is higher than the default 4-8 byte alignment of many allocators.


October 28, 2013
On Monday, 28 October 2013 at 02:30:53 UTC, Manu wrote:
> GPU's also tend to deal with strict alignments of various buffers.
>
> SIMD requires >= 16byte alignment, which is higher than the default 4-8
> byte alignment of many allocators.

AFAIK the alignments for those cases are known a priori, the question was regarding alignments that are only known at runtime.
October 28, 2013
On 28 October 2013 12:47, safety0ff <safety0ff.dev@gmail.com> wrote:

> On Monday, 28 October 2013 at 02:30:53 UTC, Manu wrote:
>
>> GPU's also tend to deal with strict alignments of various buffers.
>>
>> SIMD requires >= 16byte alignment, which is higher than the default 4-8 byte alignment of many allocators.
>>
>
> AFAIK the alignments for those cases are known a priori, the question was regarding alignments that are only known at runtime.
>

You don't know the runtime GPU at compile time.