Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Reduction to a tuple (or any other complex type) #28

Closed
ghost opened this issue Oct 2, 2017 · 3 comments
Closed

Reduction to a tuple (or any other complex type) #28

ghost opened this issue Oct 2, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 2, 2017

Use case - collect both sum(x) and sum(abs(x)) in one pass

a = randn(3, 3)
reduce((x0,x)->(x0[1]+x, x0[2]+abs(x)), (0.,0.), a)

works, but:

a = cu(a)
reduce((x0,x)->(x0[1]+x, x0[2]+abs(x)), (0.,0.), a)
ERROR: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of 
type Float64                                                                                 
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] reduce(::Function, ::Tuple{Float64,Float64}, ::CuArray{Float64,2}) at /.julia/v0
.6/CuArrays/src/reduction.jl:87 

Probably harder, but would be nice to make it work along some dimension, for example to collect sum(x, 1) and sum(abs(x), 1) in one pass.

@MikeInnes
Copy link
Collaborator

This happens because the reduction code allocates scratch space which it assumes to have the same output type as the input. It also expects v0 to have the same type. There's no particular need for this, so if you can tweak those type hints it should all work.

(You could use Core.Inference.return_type to work out the return type)

@maleadt
Copy link
Member

maleadt commented May 3, 2019

This also doesn't work on Array:

julia> a = randn(3, 3)
3×3 Array{Float64,2}:
  0.192888  -1.0884    -1.00464 
 -0.958028  -1.6191    -1.88764 
 -0.364282  -0.857092  -0.145727

julia> reduce((x0,x)->(x0[1]+x, x0[2]+abs(x)), (0.,0.), a)
ERROR: MethodError: no method matching reduce(::getfield(Main, Symbol("##3#4")), ::Tuple{Float64,Float64}, ::Array{Float64,2})

@maleadt maleadt closed this as completed May 3, 2019
@MikeInnes
Copy link
Collaborator

MikeInnes commented May 3, 2019

Yeah, but that's a bug, JuliaLang/julia#31427; reduce just isn't written in a generic way when it clearly should support this case. The particular case above is also pretty easy to fix.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants