January 27

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).

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
January 27

On Monday, 27 January 2025 at 15:55:17 UTC, Guillaume Piolat wrote:

>

You may use intel-intrinsics who:

  1. guarantees float8 is there
  2. have aligned malloc _mm_malloc

I have heard about intel-intrinsics and it's really good idea to use it in my code, but I wanted to try some SIMD operations with core.simd. But I didn't know about aligned malloc, thanks!

1 2
Next ›   Last »