-
Notifications
You must be signed in to change notification settings - Fork 98
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
add Vectorization implementation for GPU #223
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -220,13 +220,27 @@ result_type(dist::UnionMetrics, ::Type{Ta}, ::Type{Tb}, ::Nothing) where {Ta,Tb} | |
result_type(dist::UnionMetrics, ::Type{Ta}, ::Type{Tb}, p) where {Ta,Tb} = | ||
typeof(_evaluate(dist, oneunit(Ta), oneunit(Tb), oneunit(_eltype(p)))) | ||
|
||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b) | ||
_evaluate(d, a, b, parameters(d)) | ||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, p=parameters(d)) | ||
_evaluate(evaluate_strategy(d, a, b), d, a, b, p) | ||
end | ||
for M in (metrics..., weightedmetrics...) | ||
@eval @inline (dist::$M)(a, b) = _evaluate(dist, a, b) | ||
end | ||
|
||
# breaks the implementation into eval_start, eval_op, eval_reduce and eval_end | ||
function _evaluate(::Broadcasting, d::UnionMetrics, a, b, ::Nothing) | ||
map_op(x,y) = eval_op(d, x, y) | ||
reduce_op(x, y) = eval_reduce(d, x, y) | ||
eval_end(d, reduce(reduce_op, map_op.(a, b); init=eval_start(d, a, b))) | ||
end | ||
function _evaluate(::Broadcasting, d::UnionMetrics, a, b, p) | ||
map_op(x,y,p) = eval_op(d, x, y, p) | ||
reduce_op(x, y) = eval_reduce(d, x, y) | ||
eval_end(d, reduce(reduce_op, map_op.(a, b, p); init=eval_start(d, a, b))) | ||
end | ||
_evaluate(::AbstractEvaluateStrategy, d::UnionMetrics, a, b, p) = error("Not implemented.") | ||
|
||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, ::Nothing) | ||
Base.@propagate_inbounds function _evaluate(::MapReduce1, d::UnionMetrics, a, b, ::Nothing) | ||
@boundscheck if length(a) != length(b) | ||
throw(DimensionMismatch("first collection has length $(length(a)) which does not match the length of the second, $(length(b)).")) | ||
end | ||
|
@@ -239,7 +253,7 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, ::Nothing) | |
end | ||
return eval_end(d, s) | ||
end | ||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a::AbstractArray, b::AbstractArray, ::Nothing) | ||
Base.@propagate_inbounds function _evaluate(::MapReduce1, d::UnionMetrics, a::AbstractArray, b::AbstractArray, ::Nothing) | ||
@boundscheck if length(a) != length(b) | ||
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b)).")) | ||
end | ||
|
@@ -263,7 +277,7 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a::AbstractArray, b | |
end | ||
end | ||
|
||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, p) | ||
Base.@propagate_inbounds function _evaluate(::MapReduce1, d::UnionMetrics, a, b, p) | ||
@boundscheck if length(a) != length(b) | ||
throw(DimensionMismatch("first collection has length $(length(a)) which does not match the length of the second, $(length(b)).")) | ||
end | ||
|
@@ -279,7 +293,7 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, p) | |
end | ||
return eval_end(d, s) | ||
end | ||
Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a::AbstractArray, b::AbstractArray, p::AbstractArray) | ||
Base.@propagate_inbounds function _evaluate(::MapReduce1, d::UnionMetrics, a::AbstractArray, b::AbstractArray, p::AbstractArray) | ||
@boundscheck if length(a) != length(b) | ||
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b)).")) | ||
end | ||
|
@@ -308,8 +322,8 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a::AbstractArray, b | |
end | ||
end | ||
|
||
_evaluate(dist::UnionMetrics, a::Number, b::Number, ::Nothing) = eval_end(dist, eval_op(dist, a, b)) | ||
function _evaluate(dist::UnionMetrics, a::Number, b::Number, p) | ||
_evaluate(::MapReduce1, dist::UnionMetrics, a::Number, b::Number, ::Nothing) = eval_end(dist, eval_op(dist, a, b)) | ||
function _evaluate(::MapReduce1, dist::UnionMetrics, a::Number, b::Number, p) | ||
length(p) != 1 && throw(DimensionMismatch("inputs are scalars but parameters have length $(length(p)).")) | ||
eval_end(dist, eval_op(dist, a, b, first(p))) | ||
end | ||
|
@@ -324,10 +338,6 @@ _eval_start(d::UnionMetrics, ::Type{Ta}, ::Type{Tb}, p) where {Ta,Tb} = | |
eval_reduce(::UnionMetrics, s1, s2) = s1 + s2 | ||
eval_end(::UnionMetrics, s) = s | ||
|
||
for M in (metrics..., weightedmetrics...) | ||
@eval @inline (dist::$M)(a, b) = _evaluate(dist, a, b, parameters(dist)) | ||
end | ||
|
||
# Euclidean | ||
@inline eval_op(::Euclidean, ai, bi) = abs2(ai - bi) | ||
eval_end(::Euclidean, s) = sqrt(s) | ||
|
@@ -373,7 +383,14 @@ totalvariation(a, b) = TotalVariation()(a, b) | |
@inline eval_op(::Chebyshev, ai, bi) = abs(ai - bi) | ||
@inline eval_reduce(::Chebyshev, s1, s2) = max(s1, s2) | ||
# if only NaN, will output NaN | ||
Base.@propagate_inbounds eval_start(::Chebyshev, a, b) = abs(first(a) - first(b)) | ||
Base.@propagate_inbounds function eval_start(d::Chebyshev, a, b) | ||
T = result_type(d, a, b) | ||
if any(isnan, a) || any(isnan, b) | ||
return convert(T, NaN) | ||
else | ||
zero(T) # lower bound of chebyshev distance | ||
end | ||
end | ||
Comment on lines
+386
to
+393
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. This is rewritten to support CuArray; scalar indexing |
||
chebyshev(a, b) = Chebyshev()(a, b) | ||
|
||
# Minkowski | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This for loop is moved from L327-L329.