Matrix multiplication unroll SIMD

Im trying to follow the matrix multiplication Mojo is using

From this post:

Function is giving me an error, is there another way, better way?

fn unrollSimdMatrixMultiply(C: anytype, A: anytype, B: anytype) void {
    const N = B.len;
    const vec_len = 32;
    for (C, A) |*C_row, *A_row| {
        var j: u32 = 0;
        while (j <= N - vec_len) : (j += vec_len) {
            for (0..N) |k| {
                const u: @Vector(vec_len, f64) = B[k][j..][0..vec_len].*;
                const y: @Vector(vec_len, f64) = C_row.*[j..][0..vec_len].*;
                const w: @Vector(vec_len, f64) = @splat(vec_len, A_row.*[k]);
                const slice: [vec_len]f64 = (u * w) + y;
                @memcpy(C_row.*[j .. j + vec_len], &slice);
            }
        }
        while (j < N) : (j += 1) {
            for (0..N) |k| {
                C_row.*[j] += A_row.*[k] * B[k][j];
            }
        }
    }
}

Hey @miles-monoid, welcome to the forum.

Please post your error so people can see what problem you’re having.

In newer version of zig @splat infers the size of the vector, instead of taking it as an argument:

const w: @Vector(vec_len, f64) = @splat(A_row.*[k]);
2 Likes