File tree Expand file tree Collapse file tree 3 files changed +42
-12
lines changed Expand file tree Collapse file tree 3 files changed +42
-12
lines changed Original file line number Diff line number Diff line change @@ -115,13 +115,34 @@ end
115
115
116
116
"""
117
117
@synchronize()
118
+
119
+ After a `@synchronize` statement all read and writes to global and local memory
120
+ from each thread in the workgroup are visible in from all other threads in the
121
+ workgroup.
118
122
"""
119
123
macro synchronize ()
120
124
quote
121
125
$ __synchronize ()
122
126
end
123
127
end
124
128
129
+ """
130
+ @synchronize(cond)
131
+
132
+ After a `@synchronize` statement all read and writes to global and local memory
133
+ from each thread in the workgroup are visible in from all other threads in the
134
+ workgroup. `cond` is not allowed to have any visible sideffects.
135
+
136
+ # Platform differences
137
+ - `GPU`: This synchronization will only occur if the `cond` evaluates.
138
+ - `CPU`: This synchronization will always occur.
139
+ """
140
+ macro synchronize (cond)
141
+ quote
142
+ $ (esc (cond)) && $ __synchronize ()
143
+ end
144
+ end
145
+
125
146
"""
126
147
@index
127
148
Original file line number Diff line number Diff line change @@ -103,7 +103,13 @@ struct WorkgroupLoop
103
103
private :: Vector{Any}
104
104
end
105
105
106
- is_sync (expr) = @capture (expr, @synchronize )
106
+ is_sync (expr) = @capture (expr, @synchronize () | @synchronize (a_))
107
+
108
+ function is_scope_construct (expr:: Expr )
109
+ expr. head === :block # ||
110
+ # expr.head === :let
111
+ end
112
+
107
113
function find_sync (stmt)
108
114
result = false
109
115
postwalk (stmt) do expr
@@ -131,7 +137,7 @@ function split(stmts,
131
137
push! (new_stmts, emit (loop))
132
138
allocations = Any[]
133
139
current = Any[]
134
- @capture (stmt, @synchronize ) && continue
140
+ is_sync (stmt) && continue
135
141
136
142
# Recurse into scope constructs
137
143
# TODO : This currently implements hard scoping
@@ -140,7 +146,7 @@ function split(stmts,
140
146
recurse (x) = x
141
147
function recurse (expr:: Expr )
142
148
expr = unblock (expr)
143
- if any (is_sync, expr. args)
149
+ if is_scope_construct (expr) && any (is_sync, expr. args)
144
150
new_args = unblock (split (expr. args, deepcopy (indicies), deepcopy (private)))
145
151
return Expr (expr. head, new_args... )
146
152
else
Original file line number Diff line number Diff line change @@ -9,13 +9,16 @@ using StaticArrays
9
9
end
10
10
11
11
@kernel function kernel_unroll! (a, :: Val{N} ) where N
12
- @unroll for i in 1 : N
13
- @inbounds a[i] = i
12
+ let M = N+ 5
13
+ @unroll for i in 6 : M
14
+ @inbounds a[i- 5 ] = i
15
+ end
16
+ @synchronize
14
17
end
15
18
end
16
19
17
20
# Check that nested `@unroll` doesn't throw a syntax error
18
- @kernel function kernel_unroll! (a, :: Val{N} ) where N
21
+ @kernel function kernel_unroll2! (A)
19
22
@uniform begin
20
23
a = MVector {3, Float64} (1 , 2 , 3 )
21
24
b = MVector {3, Float64} (3 , 2 , 1 )
28
31
c[1 , j] = m * a[1 ] * b[j]
29
32
end
30
33
end
31
- a [I] = c[1 , 1 ]
32
- m % 2 == 0 && @synchronize
34
+ A [I] = c[1 , 1 ]
35
+ @synchronize ( m % 2 == 0 )
33
36
end
34
37
end
35
38
36
39
let
37
40
a = zeros (5 )
38
41
kernel! = kernel_unroll! (CPU (), 1 , 1 )
39
- event = kernel! (a)
40
- wait (event )
41
- event = kernel! (a, Val ( 5 ) )
42
- wait (event )
42
+ wait ( kernel! (a) )
43
+ wait (kernel! (a, Val ( 5 )) )
44
+ kernel2! = kernel_unroll2! ( CPU (), 1 , 1 )
45
+ wait (kernel2! (a) )
43
46
end
You can’t perform that action at this time.
0 commit comments