rprechelt / Vectorize.jl

Cross-platform vectorization of Julia code using Accelerate, VML, and Yeppp!
http://vectorizejl.readthedocs.io/en/latest/
Other
19 stars 7 forks source link

Support multi-dimensional arrays? #13

Open bicycle1885 opened 8 years ago

bicycle1885 commented 8 years ago

Hi,

I'm interested in accelerating computation of the RBF kernel using this library.

The function looks like this:

using Distances

function rbf_kernel(X, γ)
    K = pairwise(SqEuclidean(), X)
    scale!(K, -γ/2)
    return map!(exp, K)
end

I'd like to replace map!(exp, K) with Vectorize.exp(K) but it doesn't accept multi-dimensional arrays ( Matrix{Float64}, in this case).

Is that possible to support multi-dimensional arrays?

rprechelt commented 8 years ago

It would be possible to write a multi-dimensional array wrapper around these libraries, but the underlying frameworks (Accelerate, Yeppp, VML) do not support multi-dimensional arrays - i.e. write a wrapper that iterates over each row or column of the multi-dimensional array and calls Vectorize.exp(K) on it.

I am always looking for PR's if this is a feature that you were interested in implementing!

simonbyrne commented 8 years ago

It should be possible to use it directly, since we store arrays contiguously (so the libraries would just see it as a vector).

rprechelt commented 8 years ago

Simon is right - I have pushed a commit to master that allows for multi-dimensional arrays (the previous X::Vector{$T} on function arguments resulted in a type error). However, due to some idiosyncracies with how I handled allocating return memory, some functions may not preserve array shape i.e.

julia> arr = [1.0 2; 3 4]
2×2 Array{Float64,2}:
 1.0  2.0
 3.0  4.0

julia> using Vectorize

julia> Vectorize.Accelerate.exp(arr) # correct allocation of return array
2×2 Array{Float64,2}:
  2.71828   7.38906
 20.0855   54.5982

julia> Vectorize.exp(arr) # incorrect allocation of return array
4-element Array{Float64,1}:
  2.71828
 20.0855
  7.38906
 54.5982

This should be a small and easy fix that I will try and get done soon - if it's a pressing issue, you are welcome to give it a crack yourself. This line gives an example of what needs to be changed for some functions.

bicycle1885 commented 8 years ago

That's great. This is not in urgent need, but if you're reluctant to do that I'm happy to create a pull request.

rprechelt commented 8 years ago

I'm not reluctant at all! I'm always happy to add new features to this package - it just may take me a couple of days to find the time to locate all the offending instances and write a multi-dimensional test-suite (the current test-suite is 1D only).

However, I am always happy to accept PR's for any part of this project!

bicycle1885 commented 8 years ago

Thank you! I can wait for your work; I can use map!(exp, array) but vectorizing it will improve the performance of my application ~10% as a whole.