Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
I am trying to write a simple reduction kernel, which seems to only work for certain datatypes.
The datatypes it works for is inconsistent across platforms.
Here is the kernel, which computes the sum of squares for a vector of 6000 ones -
using KernelAbstractions
using Atomix: @atomic
@kernel function self_dot_kernel!(result, @Const(vec))
BLOCK_SIZE = @uniform @groupsize()[1]
I = @index(Global, Linear)
li = @index(Local, Linear)
intermediateresults = @localmem eltype(vec) (BLOCK_SIZE)
if I <= length(vec)
intermediateresults[li] = vec[I] * vec[I]
else
intermediateresults[li] = 0
end
@synchronize()
for i in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
if li + i <= BLOCK_SIZE
intermediateresults[li] += intermediateresults[li+i]
end
@synchronize()
end
@synchronize()
if li == 1
@atomic result[1] += intermediateresults[1]
end
end
backend = CPU()
types = [Float16, Float32, Float64, Int16, Int32, Int64, UInt16, UInt32, UInt64]
kernel! = self_dot_kernel!(backend, 32)
platform = Sys.MACHINE
for T in types
X = KernelAbstractions.ones(backend, T, 6000)
println("\n=== Type: $T ($platform) ==\n")
for i in 1:6
dotresult = KernelAbstractions.zeros(backend, T, 1)
kernel!(dotresult, X, ndrange=length(X))
KernelAbstractions.synchronize(backend)
if i % 3 == 0
println(" $(dotresult[1])")
else
print(" $(dotresult[1]),")
end
end
endAnd here are some outputs, ran using julia 1.10.2 -
=== Type: Float16 (arm64-apple-darwin23.2.0) ==
6.0e3, 6.0e3, 5.996e3
6.0e3, 7.424e3, 6.0e3
=== Type: Float32 (arm64-apple-darwin23.2.0) ==
6000.0, 6000.0, 6000.0
6000.0, 6000.0, 6000.0
=== Type: Float64 (arm64-apple-darwin23.2.0) ==
6000.0, 6000.0, 6000.0
6000.0, 6000.0, 6000.0
=== Type: Int16 (arm64-apple-darwin23.2.0) ==
6001, 6004, 6003
6000, 6003, 6001
=== Type: Int32 (arm64-apple-darwin23.2.0) ==
476212432, 49993592, 6001
6004, 6004, 49993592
=== Type: Int64 (arm64-apple-darwin23.2.0) ==
6000, 6004, 6000
4547881696, 4546395904, 6000
=== Type: UInt16 (arm64-apple-darwin23.2.0) ==
6003, 6003, 6003
6003, 6003, 6003
=== Type: UInt32 (arm64-apple-darwin23.2.0) ==
254459536, 230300864, 468297712
6000, 6000, 50026560
=== Type: UInt64 (arm64-apple-darwin23.2.0) ==
6038, 6001, 4551972480
4554654336, 6001, 6000
Float32, and Float64 always work on apple-aarch64, making me think that there's nothing wrong with the algorithm. And on windows-x64 -
=== Type: Float16 (x86_64-w64-mingw32) ==
6.0e3, 6.0e3, 6.0e3
6.0e3, 6.0e3, 6.0e3
=== Type: Float32 (x86_64-w64-mingw32) ==
6000.0, 6000.0, 6000.0
-2.7825533e35, 6000.0, 6000.0
=== Type: Float64 (x86_64-w64-mingw32) ==
6000.0, 6000.0, 6000.0
6000.0, 6000.0, 6000.0
=== Type: Int16 (x86_64-w64-mingw32) ==
6004, 6000, 6001
6002, 6000, 6003
=== Type: Int32 (x86_64-w64-mingw32) ==
-556641600, -150726792, -150726792
-108881600, -91191952, 6041
=== Type: Int64 (x86_64-w64-mingw32) ==
42949678970, 1846738422272, 1846692115360
1846740674704, 6000, 1846740275056
=== Type: UInt16 (x86_64-w64-mingw32) ==
6002, 6027, 6003
6004, 6003, 6004
=== Type: UInt32 (x86_64-w64-mingw32) ==
6011, 4144240504, 4165309344
4170999664, 569204, 6000
=== Type: UInt64 (x86_64-w64-mingw32) ==
140724418903328, 1846746033440, 1846275974656
6000, 6009, 1846685210488
On windows-x64, only Float64 consistently works. What is going on?
Activity
DhruvDh commentedon Mar 8, 2024
I tried to run this on AMDGPU and Metal, on both backends I get something very similar to the following -
The only change I made was
using AMDGPUand changedbackend = ROCBackend()orMetalBackendvchuravy commentedon Mar 8, 2024
This is #330 I think. On the CPU this currently won't work.
vchuravy commentedon Mar 8, 2024
Use a tuple
()insteadDhruvDh commentedon Mar 8, 2024
Thanks! That works but I am still getting inconsistent results on GPU. I'll close this issue in favor of #330 .
vchuravy commentedon Mar 8, 2024
Hm on the GPU you shouldn't get inconsistent results, on the CPU yes.
DhruvDh commentedon Mar 8, 2024
So, on AMDGPU, using the following snippet, here is what I get -
output -
On Metal, I get -
vchuravy commentedon Mar 9, 2024
So the only thing that seems fishy to me is.
Do you not have a read-write race here?
E.g.
li=n+1may or may not have updated it's value whenli=naccesses the value?vchuravy commentedon Mar 9, 2024
You are trying to do something similar to https://github.com/JuliaGPU/CUDA.jl/blob/cb14a637e0b7b7be9ae01005ea9bdcf79b320189/src/mapreduce.jl#L62 IIUC?
My long term goal is to provide primitives for these kinds of block reduces #234
So the difference I see that in your first iteration all Workitems in the block are active and are accessing their neighborhood data (and being racy about it).
In the CUDA code half the Workitems are active in the first iteration
DhruvDh commentedon Mar 9, 2024
yes, thanks! There is overlap in how I was indexing which does not occur with the CUDA way. I can confirm it works -
DhruvDh commentedon Mar 9, 2024
Some things still don't make sense to me -
This works -
This outputs only zeros -
Assigning variables outside the loop is fine but inside the loop is troublesome.
vchuravy commentedon Mar 9, 2024
Are you back to testing on the CPU? I should add a pass that errors when synchronize is used inside control flow, KA current design simply can't support that.
DhruvDh commentedon Mar 9, 2024
Sorry, I forgot to mention. My previous message was based on the Metal backend.
I tried it again on the AMD backend, and the full snippet follows, where it crashes.
On Metal, the same kernel outputs only 0.0s
vchuravy commentedon Mar 9, 2024
Are you not missing a
+1inlhsIndex = 2 * d * (li - 1)For
li=1lhsindexwill be 0 which is OOBDhruvDh commentedon Mar 9, 2024
No, actually, it was supposed to be
intermediateresults[li] += intermediateresults[lhsIndex+li]. Sorry! and thanks for all your responses!A pass that errors on when synchronize is used inside control flow would be great!
maleadt commentedon Oct 19, 2026
The original kernel has a read/write race in the reduction loop, so its results were undefined, and the CPU backend now supports
@synchronizein loops (#556). Note that work-items past the end ofndrangedon't run the kernel body (see the 0.9.34 notes), so they can't zero-fill their local memory slot. To pad a partial last workgroup, useunsafe_indices=true. This version returns 6000 for every type, on both CPU and CUDA: