Skip to content

Commit e5eba4f

Browse files
committed
adding a few more tests
1 parent 359c85f commit e5eba4f

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/KernelAbstractions.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module KernelAbstractions
22

33
export @kernel
44
export @Const, @localmem, @private, @uniform, @synchronize, @index, @groupsize, @print
5-
export @atomic, atomic_add!
65
export Device, GPU, CPU, Event, MultiEvent, NoneEvent
76
export async_copy!
87

src/atomics.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# Atomics
33
###
44

5+
export @atomic, atomic_add!, atomic_sub!, atomic_and!, atomic_or!, atomic_xor!,
6+
atomic_min!, atomic_max!, atomic_inc!, atomic_dec!, atomic_xchg!,
7+
atomic_op!, atomic_cas!
8+
59
"""
610
@atomic macro
711
@@ -54,20 +58,22 @@ function atomic_cas!(ptr::Ptr{T}, old::T, new::T) where T
5458
end
5559

5660
# Implementation of: (((old == 0) | (old > b)) ? b : (old-1)), returns old
61+
# Currently broken
5762
function atomic_dec!(ptr::Ptr{T}, b::T) where T
63+
Core.Intrinsics.atomic_fence(:monotonic)
5864
if (unsafe_load(ptr) == 0 | unsafe_load(ptr) > b)
5965
Core.Intrinsics.atomic_pointerswap(ptr, b, :monotonic)
6066
else
61-
Core.Intrinsics.atomic_pointermodify(ptr, -, 1, :monotonic)
67+
Core.Intrinsics.atomic_pointermodify(ptr, -, T(1), :monotonic)
6268
end
6369
end
6470

6571
# implementation of: ((old >= b) ? 0 : (old+1)), returns old
6672
function atomic_inc!(ptr::Ptr{T}, b::T) where T
6773
if unsafe_load(ptr) >= b
68-
Core.Intrinsics.atomic_pointerswap(ptr, 0, :monotonic)
74+
Core.Intrinsics.atomic_pointerswap(ptr, T(0), :monotonic)
6975
else
70-
Core.Intrinsics.atomic_pointermodify(ptr, +, 1, :monotonic)
76+
Core.Intrinsics.atomic_pointermodify(ptr, +, T(1), :monotonic)
7177
end
7278
end
7379

test/atomic_test.jl

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
using KernelAbstractions, Test
1+
using KernelAbstractions, CUDA, Test
22

33
# Note: kernels affect second element because some CPU defaults will affect the
44
# first element of a pointer if not specified, so I am covering the bases
55
@kernel function atomic_add_kernel(input, b)
66
atomic_add!(pointer(input,2),b)
77
end
88

9+
@kernel function atomic_sub_kernel(input, b)
10+
atomic_sub!(pointer(input,2),b)
11+
end
12+
13+
@kernel function atomic_inc_kernel(input, b)
14+
atomic_inc!(pointer(input,2),b)
15+
end
16+
17+
@kernel function atomic_dec_kernel(input, b)
18+
atomic_dec!(pointer(input,2),b)
19+
end
20+
921
function atomics_testsuite(backend)
1022

1123
@testset "atomic addition tests" begin
1224
types = [Int32, Int64, UInt32, UInt64, Float32]
13-
if ArrayT == CuArray
25+
if ArrayT != CuArray
1426
CUDA.capability(CUDA.device()) >= v"6.0" && push!(types, Float64)
1527
CUDA.capability(CUDA.device()) >= v"7.0" && push!(types, Float16)
1628
else
@@ -27,4 +39,52 @@ function atomics_testsuite(backend)
2739
@test Array(A)[2] == 1024
2840
end
2941
end
42+
43+
@testset "atomic subtraction tests" begin
44+
types = [Int32, Int64, UInt32, UInt64, Float32]
45+
if ArrayT == CuArray
46+
CUDA.capability(CUDA.device()) >= v"6.0" && push!(types, Float64)
47+
CUDA.capability(CUDA.device()) >= v"7.0" && push!(types, Float16)
48+
else
49+
push!(types, Float64)
50+
push!(types, Float16)
51+
end
52+
53+
for T in types
54+
A = ArrayT{T}([2048,2048])
55+
56+
kernel = atomic_sub_kernel(backend(), 4)
57+
wait(kernel(A, one(T), ndrange=(1024)))
58+
59+
@test Array(A)[2] == 1024
60+
end
61+
end
62+
63+
@testset "atomic inc tests" begin
64+
types = [Int32]
65+
66+
for T in types
67+
A = ArrayT{T}([0,0])
68+
69+
kernel = atomic_inc_kernel(backend(), 4)
70+
wait(kernel(A, T(512), ndrange=(768)))
71+
72+
@test Array(A)[2] == 255
73+
end
74+
end
75+
76+
@testset "atomic dec tests" begin
77+
types = [Int32]
78+
79+
for T in types
80+
A = ArrayT{T}([1024,1024])
81+
82+
kernel = atomic_inc_kernel(backend(), 4)
83+
wait(kernel(A, T(512), ndrange=(256)))
84+
85+
@test Array(A)[2] == 257
86+
end
87+
end
88+
89+
3090
end

0 commit comments

Comments
 (0)