mcabbott / SliceMap.jl

Same-same but different
Other
31 stars 3 forks source link

Map to scalar -> Error #10

Closed blacksph3re closed 4 years ago

blacksph3re commented 4 years ago

Hey, when I map to a scalar, this throws an error: slicemap(x -> sum(x), rand(10, 10), dims=1)

Results in

ERROR: MethodError: no method matching JuliennedArrays.Align(::Array{Float64,1}, ::JuliennedArrays.True, ::JuliennedArrays.False)
Closest candidates are:
  JuliennedArrays.Align(::AbstractArray{#s13,OuterDimensions} where #s13<:AbstractArray{Item,InnerDimensions}, ::JuliennedArrays.TypedBool...) where {Item, InnerDimensions, OuterDimensions} at .julia/packages/JuliennedArrays/furG7/src/JuliennedArrays.jl:229
Stacktrace:
 [1] slicemap(::Function, ::Array{Float64,2}; dims::Int64, rev::Bool) at .julia/packages/SliceMap/ZAHrB/src/SliceMap.jl:110
 [2] top-level scope at REPL[13]:1

If I use slicemap(x -> [sum(x)], rand(10, 10), dims=1) it works

mcabbott commented 4 years ago

This won't work, I'm afraid, slicemap expects a function which gives back the same shape. Because it calls Align with the same parameters as used for Slices. But if you only need one half, or you need other mismatched slice/align, it should be dead simple to borrow what you need:

https://github.com/mcabbott/SliceMap.jl/blob/0b2b7d96ec0e282a0b5a1717154b0e5ef41b275e/src/SliceMap.jl#L89-L111

However, mapcols and MapCols and friends do accept vector -> scalar functions.

blacksph3re commented 4 years ago

Ah okay, cool - thanks for the lead. I needed slicemap because I am working on a GPU and mapcols returns a CPU array, but I will try directly with JuliennedArrays.

mcabbott commented 4 years ago

GPU things are tricky... I think mapcols is more-or-less doing a CPU iteration over GPU sub-arrays. When those return CPU arrays then reduce(hcat, Array{CuArray{... makes the final GPU array, but when they return scalars there is nothing to do. Copying the result back to the GPU may not be the worst idea.

Thread from someone who actually knows what he's talking about:

https://discourse.julialang.org/t/map-performance-with-cuarrays/33497/4