-
Notifications
You must be signed in to change notification settings - Fork 81
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
6 changed files
with
52 additions
and
12 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
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
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,30 @@ | ||
# Custom GPU-compatible `Number` interface. | ||
struct GPUNumber{T <: AbstractGPUArray} <: AN.AbstractNumber{T} | ||
val::T | ||
|
||
function GPUNumber(val::T) where T <: AbstractGPUArray | ||
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.zero(g::GPUNumber) = zero(number_type(g)) | ||
Base.identity(g::GPUNumber) = g |
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
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
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