align dynamic array (for avx friendliness) hints? / possible??
james.p.leblanc
james.p.leblanc at gmail.com
Tue Aug 3 17:50:20 UTC 2021
On Tuesday, 3 August 2021 at 16:32:34 UTC, kinke wrote:
> On Tuesday, 3 August 2021 at 12:33:56 UTC, james.p.leblanc
> wrote:
>> Concise question:
>> =================
>>
>> I would like to use dynamic arrays, not for their
>> dynamic sizing properties per se' (slicing, appending, etc).
>> But, more for their memory protection and efficiencies (for
>> example,using foreach).
>>
>> However, I must have the start of my array at an avx
>> friendly 32 byte alignment.
>>
>> Is this easily acheivable?
>>
>> Background:
>> ===========
>>
>> I am interfacing with fftw. If I use the fftw_malloc, then
>> I am forced to either:
>>
>> 1) copy to/from the allocated arrays to/from my "standard"
>> dlang dynamic arrays (loss of efficiency). or ...
>>
>> 2) use standard array/pointer mechanisms everywhere(loss
>> of memory safely).
>>
>> My thinking is that I could forego the use of the fftw_malloc,
>> and simply hand fftw functions my (properly aligned) pointer
>> of my dlang dynamic array.
>>
>> All thoughts, comments, hints, greatly appreciated!
>>
>> James
>
> AFAIK, the GC only guarantees an alignment of 16. But you can
> turn any memory allocation into a slice, simply via
>
> ```
> size_t length = ...;
> T* myPtr = cast(T*) fftw_malloc(length * T.sizeof); // or
> aligned_alloc, posix_memalign etc.
> T[] mySlice = myPtr[0 .. length];
> foreach (ref e; mySlice) ...
> // free!
> ```
Dear Kinke,
THANKS IMMENSELY!
-----------------
This is exactly the kind of solution that I was hoping would
be possible (elegant, simply, and clear).
This really is the perfect solution, and opens up many
possibilities for me.
(Perhaps I had been using the wrong search terms, or perhaps
everyone already
knows how to use this "conversion of C arrays/pointers to D
slices" solution...
but I was stumped!)
Again, thanks kindly
**Is there some highly visible place this is already documented?
If not, it would make a great blog post as it would be beneficial
to any D newcomers bridging C/D and needing AVX alignments, etc.**
Best Regards,
James
More information about the Digitalmars-d-learn
mailing list