-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ext/ChainRulesKernelAbstractionsExt/ChainRulesKernelAbstractionsExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module ChainRulesKernelAbstractionsExt | ||
|
||
import Adapt | ||
import Atomix | ||
import ChainRules | ||
import GPUArrays | ||
import KernelAbstractions as KA | ||
|
||
using GPUArraysCore: AbstractGPUArray | ||
using KernelAbstractions | ||
|
||
function ChainRules.∇getindex!(dx::AbstractGPUArray, dy, inds...) | ||
# kab = get_backend(dx) | ||
|
||
# if KA.supports_atomics(kab) | ||
# gids = GPUArrays.to_indices(dx, inds) | ||
# idims = map(length, gids) | ||
# Is = map(Adapt.adapt(GPUArrays.ToGPU(dy)), gids) | ||
# scatter!(kab)(+, dx, dy, idims, Is...; ndrange=length(dy)) | ||
# else | ||
dx_cpu = Adapt.adapt(Array, dx) | ||
view(dx_cpu, Adapt.adapt(Array, inds)...) .+= Adapt.adapt(Array, dy) | ||
copyto!(dx, dx_cpu) | ||
# end | ||
return dx | ||
end | ||
|
||
@kernel function scatter!(op, dest, src, idims, Is::Vararg{Any, N}) where N | ||
_scatter!(@index(Global), op, dest, src, idims, Is...) | ||
end | ||
|
||
@generated function _scatter!(i, op, dest, src, idims, Is::Vararg{Any, N}) where N | ||
quote | ||
is = @inbounds CartesianIndices(idims)[i] | ||
Base.Cartesian.@nexprs $N j -> I_j = @inbounds((Is[j])[is[j]]) | ||
dv = src[i] | ||
Base.Cartesian.@ncall $N _accum! op dest dv j -> I_j | ||
end | ||
end | ||
|
||
function _accum!(op, dest, val, ids...) | ||
Atomix.modify!(Atomix.IndexableRef(dest, (ids...,)), op, val) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters