align dynamic array (for avx friendliness) hints? / possible??

kinke noone at nowhere.com
Tue Aug 3 16:32:34 UTC 2021


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!
```


More information about the Digitalmars-d-learn mailing list