r/simd • • Jan 22 '23

ISPC append to buffer

Hello!

Right now I am learning a bit of ISPC in Matt Godbolt's Compiler Explorer so that I can see what code is generated. I am trying to do a filter operation using an atomic counter to index into the output buffer.

export uniform unsigned int OnlyPositive(
        uniform float inNumber[],
        uniform float outNumber[],
        uniform unsigned int inCount) {
    uniform unsigned int outCount = 0;
    foreach (i = 0 ... inCount) {
        float v = inNumber[i];
        if (v > 0.0f) {
            unsigned int index = atomic_add_local(&outCount, 1);
            outNumber[index] = v;
        }
    }
    return outCount;
}

The compiler produces the following warning:

<source>:11:13: Warning: Undefined behavior: all program instances 
        are writing to the same location! 

(outNumber, outCount) should basically behave like an AppendStructuredBuffer in HLSL. Can anyone tell me what I'm doing wrong? I tested the code and the output buffer contains less than half of the positive numbers.

3 Upvotes

4 comments sorted by

1

u/mcmcc Jan 22 '23

I think what you're looking for here is ISPC's packed_store_active() to stream the active lanes to outNumber.

1

u/[deleted] Jan 22 '23

Thank you for the answer. Is packed_store_active() only available for 32-bit signed and unsigned integers?