zig icon indicating copy to clipboard operation
zig copied to clipboard

Atomic values should allow vectors

Open WGrav01 opened this issue 7 months ago • 8 comments

Atomic values only allow for a pointer, a bool, a float, an integer or an enum. It would be really useful for a critical part of my project to allow @Vector types to be stored in atomics.

WGrav01 avatar May 14 '25 19:05 WGrav01

I would be curious to know why you need atomic operations on a simd type: can't you operate on each component atomically instead ? (I am genuinely curious, not being snarky) It looks like intel supports this feature and also ARM but not all operations are supported.

EtienneParmentier avatar May 15 '25 11:05 EtienneParmentier

I can, but it would be a lot more convenient to operate atomically on the entire vector at once, instead of working on each element like an array.

WGrav01 avatar May 15 '25 11:05 WGrav01

I am trying to understand a use case for this. Could you explain it a bit more?

Rexicon226 avatar May 15 '25 11:05 Rexicon226

While I can operate on the vector on a per-field basis, it would be a lot more concice (and faster, if the SIMD instructions are preserved) to operate on the entire vector with another vector like so:

const std = @import("std");
const expect = std.testing.expect;
const Value = std.atomic.Value;
const monotonic = std.builtin.AtomicOrder.monotonic;
const vec3 = @Vector(3, u32);

test "atomic vector example" {
    var vector = Value(vec3).init(vec3{ 1, 2, 3 });
    _ = vector.fetchAdd(vec3{ 4, 5, 6 }, monotonic);

    try expect(vector == vec3{ 5, 7, 9 });
}

WGrav01 avatar May 15 '25 23:05 WGrav01

You've described what you're proposing, but when would you need this? What is a real-world usecase?

Rexicon226 avatar May 16 '25 01:05 Rexicon226

I'm writing a software raytracer. I have multiple threads that, for each pixel, generate a set amount of samples, then combine those samples into the final color for the pixel, which is then reset to zero for the next pixel. That's what I need the atomic vectors for - combining the samples that were generated into a single vector for the pixel itself, without a possibly dangerous race condition. While I can do it with a mutex, it would be really inefficient, and I would rather eliminate them entirely.

WGrav01 avatar May 16 '25 11:05 WGrav01

This proposal does not make sense, since, AFAIK, no mainstream CPU supports atomic vector operations other than plain loads and stores—this feature cannot be added to Zig since it requires CPU instructions that do not exist.

Johan-Mi avatar May 18 '25 20:05 Johan-Mi

not only does this operation not exist on most CPUs, it also wouldn't really be that much faster than a mutex, due to having the same amount of contention. you should probably be using a parallel reduction instead.

silversquirl avatar May 18 '25 20:05 silversquirl

Closing since this indeed doesn't seem implementable other than by lowering to compiler-rt functions which use a mutex, which would render the whole exercise a bit silly.

alexrp avatar May 27 '25 12:05 alexrp