Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Support multi-dimensional arrays? #13

Open
bicycle1885 opened this issue Sep 25, 2016 · 6 comments
Open

Support multi-dimensional arrays? #13

bicycle1885 opened this issue Sep 25, 2016 · 6 comments

Comments

@bicycle1885
Copy link

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
Copy link
Owner

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
Copy link
Collaborator

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

@rprechelt
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

rprechelt commented Sep 29, 2016

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
Copy link
Author

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.

@platawiec platawiec mentioned this issue Aug 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants