-
Notifications
You must be signed in to change notification settings - Fork 87
Introduce AsyncNumber
to lazily copy numeric mapreduce
results to the host
#550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
cf88dfe
16bb79f
aa095e8
982e7cc
59f0a88
8e48354
d951132
f01a7d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Custom GPU-compatible `Number` interface. | ||
struct GPUNumber{T <: AbstractGPUArray} <: AN.AbstractNumber{T} | ||
val::T | ||
|
||
function GPUNumber(val::T) where T <: AbstractGPUArray | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pxl-th it might be a good idea to do a a = CUDA.rand(1,1,1,1) |> GPUNumber
x = CUDA.rand(4,4)
size(x .+ a) # want (4,4) but will likely get (4,4,1,1) I haven't run your code but it seems like it would do something similar to julia> rand(1,1,1,1) .+ rand(4,4) |> size
(4, 4, 1, 1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, thanks! |
||
length(val) != 1 && error( | ||
"`GPUNumber` accepts only 1-element GPU arrays, " * | ||
"instead `$(length(val))`-element array was given.") | ||
new{T}(val) | ||
end | ||
end | ||
|
||
AN.number(g::GPUNumber) = @allowscalar g.val[] | ||
maybe_number(g::GPUNumber) = AN.number(g) | ||
maybe_number(g) = g | ||
|
||
number_type(::GPUNumber{T}) where T = eltype(T) | ||
|
||
# When operations involve other `::Number` types, | ||
# do not convert back to `GPUNumber`. | ||
AN.like(::Type{<: GPUNumber}, x) = x | ||
|
||
# When broadcasting, just pass the array itself. | ||
Base.broadcastable(g::GPUNumber) = g.val | ||
|
||
# Overload to avoid copies. | ||
Base.one(g::GPUNumber) = one(number_type(g)) | ||
Base.one(::Type{GPUNumber{T}}) where T = one(eltype(T)) | ||
Base.zero(g::GPUNumber) = zero(number_type(g)) | ||
Base.zero(::Type{GPUNumber{T}}) where T = zero(eltype(T)) | ||
Base.identity(g::GPUNumber) = g | ||
|
||
Base.getindex(g::GPUNumber) = AN.number(g) | ||
|
||
Base.isequal(g::GPUNumber, v::Number) = isequal(g[], v) | ||
Base.isequal(v::Number, g::GPUNumber) = isequal(v, g[]) | ||
|
||
Base.nextpow(a, x::GPUNumber) = nextpow(a, x[]) | ||
Base.nextpow(a::GPUNumber, x) = nextpow(a[], x) | ||
Base.nextpow(a::GPUNumber, x::GPUNumber) = nextpow(a[], x[]) | ||
|
||
Base.convert(::Type{Number}, g::GPUNumber) = g[] |
Uh oh!
There was an error while loading. Please reload this page.