Skip to content

Commit 0060568

Browse files
authored
Merge pull request #57 from JuliaGPU/vc/accumulate_alg
Vc/accumulate alg: made ScanPrefixes the default accumulate algorithm; added atomic orderings to DecoupledLookback.
2 parents 60c379f + 97b6331 commit 0060568

File tree

7 files changed

+33
-80
lines changed

7 files changed

+33
-80
lines changed

Project.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
88
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
99
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
1010
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
11+
UnsafeAtomics = "013be700-e6cd-48c3-b4a1-df204f14c38f"
1112

1213
[weakdeps]
13-
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
1414
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"
1515

1616
[extensions]
17-
AcceleratedKernelsMetalExt = "Metal"
1817
AcceleratedKernelsoneAPIExt = "oneAPI"
1918

2019
[compat]
2120
ArgCheck = "2"
2221
GPUArraysCore = "0.2.0"
2322
KernelAbstractions = "0.9.34"
2423
Markdown = "1"
25-
Metal = "1"
26-
oneAPI = "1, 2"
24+
UnsafeAtomics = "0.3.0"
2725
julia = "1.10"
26+
oneAPI = "1, 2"

ext/AcceleratedKernelsMetalExt.jl

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/AcceleratedKernels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module AcceleratedKernels
1414
using ArgCheck: @argcheck
1515
using GPUArraysCore: AnyGPUArray, @allowscalar
1616
using KernelAbstractions
17+
import UnsafeAtomics
1718

1819

1920
# Exposed functions from upstream packages

src/accumulate/accumulate.jl

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ include("accumulate_nd.jl")
4040
min_elems::Int=2,
4141
4242
# Algorithm choice
43-
alg::AccumulateAlgorithm=DecoupledLookback(),
43+
alg::AccumulateAlgorithm=ScanPrefixes(),
4444
4545
# GPU settings
4646
block_size::Int=256,
@@ -60,7 +60,7 @@ include("accumulate_nd.jl")
6060
min_elems::Int=2,
6161
6262
# Algorithm choice
63-
alg::AccumulateAlgorithm=DecoupledLookback(),
63+
alg::AccumulateAlgorithm=ScanPrefixes(),
6464
6565
# GPU settings
6666
block_size::Int=256,
@@ -89,13 +89,13 @@ becomes faster if it is a more compute-heavy operation to hide memory latency -
8989
9090
## GPU
9191
For the 1D case (`dims=nothing`), the `alg` can be one of the following:
92-
- `DecoupledLookback()`: the default algorithm, using opportunistic lookback to reuse earlier
93-
blocks' results; requires device-level memory consistency guarantees, which Apple Metal does not
94-
provide.
95-
- `ScanPrefixes()`: a simpler algorithm that scans the prefixes of each block, with no lookback; it
96-
has similar performance as `DecoupledLookback()` for large block sizes, and small to medium arrays,
92+
- `ScanPrefixes()`: the default algorithm that scans the prefixes of each block, with no lookback; it
93+
has better performance than `DecoupledLookback()` for large block sizes, and small to medium arrays,
9794
but poorer scaling for many blocks; there is no performance degradation below `block_size^2`
98-
elements.
95+
elements, but it remains fast well into millions of elements.
96+
- `DecoupledLookback()`: a more complex algorithm using opportunistic lookback to reuse earlier
97+
blocks' results; requires device-level memory consistency guarantees (which Apple Metal does not
98+
provide) and atomic orderings; theoretically more scalable for many blocks.
9999
100100
A different, unique algorithm is used for the multi-dimensional case (`dims` is an integer).
101101
@@ -105,13 +105,7 @@ The temporaries are only used for the 1D case (`dims=nothing`): `temp` stores pe
105105
`temp_flags` is only used for the `DecoupledLookback()` algorithm for flagging if blocks are ready;
106106
they should both have at least `(length(v) + 2 * block_size - 1) ÷ (2 * block_size)` elements; also,
107107
`eltype(v) === eltype(temp)` is required; the elements in `temp_flags` can be any integers, but
108-
`Int8` is used by default to reduce memory usage.
109-
110-
# Platform-Specific Notes
111-
On Metal, the `alg=ScanPrefixes()` algorithm is used by default, as Apple Metal GPUs do not have
112-
strong enough memory consistency guarantees for the `DecoupledLookback()` algorithm - which
113-
produces incorrect results about 0.38% of the time (the beauty of parallel algorithms, ey). Also,
114-
`block_size=1024` is used here by default to reduce the number of coupled lookbacks.
108+
`UInt8` is used by default to reduce memory usage.
115109
116110
# Examples
117111
Example computing an inclusive prefix sum (the typical GPU "scan"):
@@ -123,7 +117,7 @@ v = oneAPI.ones(Int32, 100_000)
123117
AK.accumulate!(+, v, init=0)
124118
125119
# Use a different algorithm
126-
AK.accumulate!(+, v, alg=AK.ScanPrefixes())
120+
AK.accumulate!(+, v, alg=AK.DecoupledLookback())
127121
```
128122
"""
129123
function accumulate!(
@@ -160,8 +154,6 @@ function _accumulate_impl!(
160154
dims::Union{Nothing, Int}=nothing,
161155
inclusive::Bool=true,
162156

163-
# FIXME: Switch back to `DecoupledLookback()` as the default algorithm
164-
# once https://github.com/JuliaGPU/AcceleratedKernels.jl/pull/44 is merged.
165157
alg::AccumulateAlgorithm=ScanPrefixes(),
166158

167159
# CPU settings
@@ -214,7 +206,7 @@ end
214206
min_elems::Int=2,
215207
216208
# Algorithm choice
217-
alg::AccumulateAlgorithm=DecoupledLookback(),
209+
alg::AccumulateAlgorithm=ScanPrefixes(),
218210
219211
# GPU settings
220212
block_size::Int=256,

src/accumulate/accumulate_1d_gpu.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ end
169169
running_prefix = prefixes[iblock - 0x1 + 0x1]
170170
inspected_block = signed(typeof(iblock))(iblock) - 0x2
171171
while inspected_block >= 0x0
172-
173172
# Opportunistic: a previous block finished everything
174-
if flags[inspected_block + 0x1] == ACC_FLAG_A
173+
if UnsafeAtomics.load(pointer(flags, inspected_block + 0x1), UnsafeAtomics.monotonic) == ACC_FLAG_A
174+
UnsafeAtomics.fence(UnsafeAtomics.acquire) # (fence before reading from v)
175175
# Previous blocks (except last) always have filled values in v, so index is inbounds
176176
running_prefix = op(running_prefix, v[(inspected_block + 0x1) * block_size * 0x2])
177177
break
@@ -194,11 +194,17 @@ end
194194
end
195195

196196
# Set flag for "aggregate of all prefixes up to this block finished"
197-
@synchronize() # This is needed so that the flag is not set before copying into v, but
198-
# there should be better memory fences to guarantee ordering without
199-
# thread synchronization...
197+
# There are two synchronization concerns here:
198+
# 1. Withing a group we want to ensure that all writed to `v` have occured before setting the flag.
199+
# 2. Between groups we need to use a fence and atomic load/store to ensure that memory operations are not re-ordered
200+
@synchronize() # within-block
201+
# Note: This fence is needed to ensure that the flag is not set before copying into v.
202+
# See https://doc.rust-lang.org/std/sync/atomic/fn.fence.html
203+
# for more details.
204+
# We use the happens-before relation between stores to `v` and the store to `flags`.
205+
UnsafeAtomics.fence(UnsafeAtomics.release)
200206
if ithread == 0x0
201-
flags[iblock + 0x1] = ACC_FLAG_A
207+
UnsafeAtomics.store!(pointer(flags, iblock + 0x1), convert(eltype(flags), ACC_FLAG_A), UnsafeAtomics.monotonic)
202208
end
203209
end
204210

@@ -285,7 +291,7 @@ function accumulate_1d_gpu!(
285291
end
286292

287293
if isnothing(temp_flags)
288-
flags = similar(v, Int8, num_blocks)
294+
flags = similar(v, UInt8, num_blocks)
289295
else
290296
@argcheck eltype(temp_flags) <: Integer
291297
@argcheck length(temp_flags) >= num_blocks

src/arithmetics.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ end
307307
dims::Union{Nothing, Int}=nothing,
308308
309309
# Algorithm choice
310-
alg::AccumulateAlgorithm=DecoupledLookback(),
310+
alg::AccumulateAlgorithm=ScanPrefixes(),
311311
312312
# GPU settings
313313
block_size::Int=256,
@@ -318,9 +318,6 @@ end
318318
Cumulative sum of elements of an array, with optional `init` and `dims`. Arguments are the same as
319319
for [`accumulate`](@ref).
320320
321-
## Platform-Specific Notes
322-
On Apple Metal, the `alg=ScanPrefixes()` algorithm is used by default.
323-
324321
# Examples
325322
Simple cumulative sum of elements in a vector:
326323
```julia
@@ -360,7 +357,7 @@ end
360357
dims::Union{Nothing, Int}=nothing,
361358
362359
# Algorithm choice
363-
alg::AccumulateAlgorithm=DecoupledLookback(),
360+
alg::AccumulateAlgorithm=ScanPrefixes(),
364361
365362
# GPU settings
366363
block_size::Int=256,
@@ -371,9 +368,6 @@ end
371368
Cumulative product of elements of an array, with optional `init` and `dims`. Arguments are the same
372369
as for [`accumulate`](@ref).
373370
374-
## Platform-Specific Notes
375-
On Apple Metal, the `alg=ScanPrefixes()` algorithm is used by default.
376-
377371
# Examples
378372
Simple cumulative product of elements in a vector:
379373
```julia

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ elseif "--oneAPI" in ARGS
2121
using oneAPI
2222
oneAPI.versioninfo()
2323
const BACKEND = oneAPIBackend()
24-
TEST_DL[] = true
24+
25+
# FIXME: need atomic orderings for `DecoupledLookback` in oneAPI
26+
# TEST_DL[] = true
2527
elseif "--AMDGPU" in ARGS
2628
Pkg.add("AMDGPU")
2729
using AMDGPU

0 commit comments

Comments
 (0)