core.simd and dynamic arrays

Johan j at j.nl
Mon Jan 27 19:14:45 UTC 2025


On Monday, 27 January 2025 at 05:53:09 UTC, John C. wrote:
> On Sunday, 26 January 2025 at 16:43:19 UTC, Johan wrote:
>> The `align(32)` applies to the slice `a`, not the contents of 
>> `a` (where `a` points to).
>
> Thank you, seems that it is reason for that errors. I remember 
> that dynamic array can be represented as structure with size_t 
> len and pointer to memory location, so do we need to align 
> memory for this memory location, not dynamic array? Even if we 
> align dynamic array structure, we get five zeros at the end of 
> it's address, but memory location pointed to is still 
> unaligned, so do I have align it manually?

Exactly.


>> Does it work when you create an array of `float8`?  (`float8[] 
>> a = new float8[128/8];`)
>
> No, I have modified original code version and errors are the 
> same, except for dmd with "-mcpu=avx" flag set (error changed 
> to "program killed by signal 11" on run.dlang.io).
> ```d
> import std.stdio : writeln, writefln;
> import std.random : uniform01;
> import core.memory : GC;
> import core.simd;
>
> void main() {
>     float8[] a = new float8[128];
>     float8[] b = new float8[128];
>     float8[] c = new float8[128];
>
>     writeln(&a, " ", &b, " ", &c);
>     writeln(a.ptr, " ", b.ptr, " ", c.ptr);
>
>     writeln("Filling array...");
>     for (size_t i = 0; i < c.length; ++i) {
>         // If I understand correctly, lines below assign 8 
> equal float values to float8 (does not matter in this test?)
>         a[i] = uniform01();
>         b[i] = uniform01();
>     }
>
>     writeln("Performing arithmetics...");
>     for (size_t i = 0; i < c.length; ++i) {
>         c[i] = a[i] * b[i];
>     }
>
>     writeln("Checking array...");
>     for (size_t i = 0; i < c.length; i += 8) {
>         if (c[i].array != (a[i] * b[i]).array) {
>             writefln("Value in array c is not product (i = %s): 
> %s != %s + %s", i, c[i], a[i], b[i]);
>             break;
>         }
>     }
> }
>
> ```
> Output:
> ```
> 7FFF602EF5A0 7FFF602EF590 7FFF602EF580
> 7F15CB784010 7F15CB786010 7F15CB788010
> Filling array...
> Error: /tmp/onlineapp-835ef2 failed with status: -2
>        message: Segmentation fault (core dumped)
> Error: program received signal 2 (Interrupt)
> ```

This is a long-standing druntime bug: alignment of the type is 
not taken into account for GC allocations... Wow.
https://github.com/dlang/dmd/issues/17259

-Johan


- Johan



More information about the Digitalmars-d-learn mailing list