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)
77end
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+
921function 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+
3090end
0 commit comments