|
1 | 1 | using SPIRVIntrinsics: @builtin_ccall, @typed_ccall, LLVMPtr, known_intrinsics |
2 | 2 |
|
3 | | -@testset "atomics" begin |
| 3 | +# Define the types to test |
| 4 | +integer_types = [Int32, UInt32, Int64, UInt64] |
| 5 | +float_types = [Float32, Float64] |
| 6 | +all_types = vcat(integer_types, float_types) |
| 7 | + |
| 8 | +dev = OpenCL.cl.device() |
4 | 9 |
|
5 | | -function atomic_count(counter) |
6 | | - OpenCL.@atomic counter[] += 1 |
| 10 | +# Arithmetic operations |
| 11 | +function test_atomic_add(counter::AbstractArray{T}) where T |
| 12 | + OpenCL.@atomic counter[] += one(T) |
| 13 | + return |
| 14 | +end |
| 15 | +function test_atomic_sub(counter::AbstractArray{T}) where T |
| 16 | + OpenCL.@atomic counter[] -= one(T) |
| 17 | + return |
| 18 | +end |
| 19 | +# Bitwise operations |
| 20 | +function test_atomic_and(counter::AbstractArray{T}) where T |
| 21 | + OpenCL.@atomic counter[] &= ~(one(T) << (get_global_id() - 1)) |
| 22 | + return |
| 23 | +end |
| 24 | +function test_atomic_or(counter::AbstractArray{T}) where T |
| 25 | + OpenCL.@atomic counter[] |= one(T) << (get_global_id() - 1) |
| 26 | + return |
| 27 | +end |
| 28 | +function test_atomic_xor(counter::AbstractArray{T}) where T |
| 29 | + OpenCL.@atomic counter[] ⊻= one(T) << ((get_global_id() - 1) % 32) |
| 30 | + return |
| 31 | +end |
| 32 | +# Min/max operations - use low-level API directly |
| 33 | +function test_atomic_max(counter::AbstractArray{T}) where T |
| 34 | + OpenCL.atomic_max!(pointer(counter), T(get_global_id())) |
| 35 | + return |
| 36 | +end |
| 37 | +function test_atomic_min(counter::AbstractArray{T}) where T |
| 38 | + OpenCL.atomic_min!(pointer(counter), T(get_global_id())) |
| 39 | + return |
| 40 | +end |
| 41 | +# Exchange operation - use low-level API directly |
| 42 | +function test_atomic_xchg(counter::AbstractArray{T}) where T |
| 43 | + OpenCL.atomic_xchg!(pointer(counter), one(T)) |
7 | 44 | return |
8 | 45 | end |
| 46 | +# Compare-and-swap operation - use low-level API directly |
| 47 | +function test_atomic_cas(counter::AbstractArray{T}) where T |
| 48 | + OpenCL.atomic_cmpxchg!(pointer(counter), zero(T), one(T)) |
| 49 | + return |
| 50 | +end |
| 51 | + |
| 52 | +# Define atomic operations to test |
| 53 | +atomic_operations = [ |
| 54 | + # op, init_val, expected_val |
| 55 | + (test_atomic_add, 0, 1000), |
| 56 | + (test_atomic_sub, 1000, 0), |
| 57 | + (test_atomic_and, typemax(UInt64), 0), |
| 58 | + (test_atomic_or, 0, typemax(UInt64)), |
| 59 | + (test_atomic_xor, 0, typemax(UInt32) << 8), |
| 60 | + (test_atomic_max, 0, 1000), |
| 61 | + (test_atomic_min, 1000, 1), |
| 62 | + (test_atomic_xchg, 0, 1), |
| 63 | + (test_atomic_cas, 0, 1), |
| 64 | +] |
| 65 | +@testset "atomics" begin |
| 66 | +@testset "$kernel_func - $T" for (kernel_func, init_val, expected_val) in atomic_operations, T in all_types |
| 67 | + # Skip Int64/UInt64 if not supported |
| 68 | + if sizeof(T) == 8 && T <: Integer && !("cl_khr_int64_extended_atomics" in dev.extensions) |
| 69 | + continue |
| 70 | + end |
| 71 | + |
| 72 | + # Skip Float64 if not supported |
| 73 | + if T == Float64 && !("cl_khr_fp64" in dev.extensions) |
| 74 | + continue |
| 75 | + end |
9 | 76 |
|
10 | | -@testset "atomic_add! ($T)" for T in [Int32, UInt32, Int64, UInt64] |
11 | | - if sizeof(T) == 4 || "cl_khr_int64_extended_atomics" in cl.device().extensions |
12 | | - a = OpenCL.zeros(T) |
13 | | - @opencl global_size=1000 atomic_count(a) |
14 | | - @test OpenCL.@allowscalar a[] == 1000 |
| 77 | + # Bitwise operations (only valid for integers) |
| 78 | + if kernel_func in [test_atomic_and, test_atomic_or, test_atomic_xor] && T <: AbstractFloat |
| 79 | + continue |
15 | 80 | end |
| 81 | + |
| 82 | + # Min/max operations (only supported for 32-bit integers in OpenCL) |
| 83 | + if kernel_func in [test_atomic_min, test_atomic_max] && !(T in [Int32, UInt32]) |
| 84 | + continue |
| 85 | + end |
| 86 | + |
| 87 | + if T <: Integer |
| 88 | + init_val %= T |
| 89 | + expected_val %= T |
| 90 | + end |
| 91 | + |
| 92 | + a = OpenCL.fill(T(init_val)) |
| 93 | + @opencl global_size=1000 kernel_func(a) |
| 94 | + result_val = OpenCL.@allowscalar a[] |
| 95 | + @test result_val === T(expected_val) |
16 | 96 | end |
17 | 97 |
|
| 98 | + |
| 99 | +@testset "atomic_add! ($T)" for T in [Float32, Float64] |
| 100 | + # Float64 requires cl_khr_fp64 extension |
| 101 | + if T == Float64 && !("cl_khr_fp64" in cl.device().extensions) |
| 102 | + continue |
| 103 | + end |
18 | 104 | if "cl_ext_float_atomics" in cl.device().extensions |
19 | | - function atomic_float_add(counter, val) |
| 105 | + @eval function atomic_float_add(counter, val::$T) |
20 | 106 | @builtin_ccall( |
21 | | - "atomic_add", Float32, |
22 | | - (LLVMPtr{Float32, AS.CrossWorkgroup}, Float32), |
| 107 | + "atomic_add", $T, |
| 108 | + (LLVMPtr{$T, AS.CrossWorkgroup}, $T), |
23 | 109 | pointer(counter), val, |
24 | 110 | ) |
25 | 111 | return |
26 | 112 | end |
27 | 113 |
|
28 | 114 | @testset "SPV_EXT_shader_atomic_float_add extension" begin |
29 | | - a = OpenCL.zeros(Float32) |
30 | | - @opencl global_size = 1000 extensions = ["SPV_EXT_shader_atomic_float_add"] atomic_float_add(a, 1.0f0) |
31 | | - @test OpenCL.@allowscalar a[] == 1000.0f0 |
| 115 | + a = OpenCL.zeros(T) |
| 116 | + @opencl global_size = 1000 extensions = ["SPV_EXT_shader_atomic_float_add"] atomic_float_add(a, one(T)) |
| 117 | + @test OpenCL.@allowscalar a[] == T(1000.0) |
32 | 118 |
|
33 | 119 | spv = sprint() do io |
34 | | - OpenCL.code_native(io, atomic_float_add, Tuple{CLDeviceArray{Float32, 0, 1}, Float32}; extensions = ["SPV_EXT_shader_atomic_float_add"]) |
| 120 | + OpenCL.code_native(io, atomic_float_add, Tuple{CLDeviceArray{T, 0, 1}, T}; extensions = ["SPV_EXT_shader_atomic_float_add"]) |
35 | 121 | end |
36 | 122 | @test occursin("OpExtension \"SPV_EXT_shader_atomic_float_add\"", spv) |
37 | 123 | @test occursin("OpAtomicFAddEXT", spv) |
38 | 124 | end |
39 | 125 | end |
40 | 126 |
|
41 | 127 | end |
| 128 | +end |
0 commit comments