Comments (13)
Some basic testing on my laptop shows that hardcoding the loop count for dim = 3 is about 2 times faster than the general function.
@code_llvm
also shows that the loop is unrolled.
So this is something that probably should be done. Not sure how to create the trees in a type stable way though...
from nearestneighbors.jl.
Although on full knn search the timing is kind of irrelevant. It looks like we are bottled necked by memory accesses.
from nearestneighbors.jl.
Perhaps one way to do this if you're sticking with matrices would be to pass the dimension along as either N
(for large N) or Val{N}
for very small N. With the dimension encoded in a type rather than a value, the compiler might have a chance of inlining it and unrolling the loop. I'm not sure if that's a good idea or it's trying to write C++ in julia. This would force things like knn_kernel!
to be compiled once for each fixed dimension, probably ok if the Val
wrapping is restricted to very small N.
Does the memory access bottleneck depend on the spatial access pattern? It's probably possible to arrange for spatially coherent queries in a lot of cases (though that's up to the user).
from nearestneighbors.jl.
You are right that this would cause a recompile for all runs with different dimensional data. However, how often is it that one needs to run a lot of queries on data with widely different dimensions? My expectation is that one wants to do a lot of run on a large data set of a fixed dimension and want that to run as fast as possible.
There are two ways to do it:
- Force the user to give the dimension as a
Val{N}
. This would make things type stable but be awakward for the user. - Just read of the dimension of the input data and create the correctly parameterized tree. This would be type unstable but I'm not sure how much it matters since unless one is querying with very mall data sets the overhead from the instability should be insignificant.
from nearestneighbors.jl.
Yes, a fixed dimension is certainly my use case.
I wasn't imagining that the tree itself be parameterized on the dimension, simply that knn()
would dynamically determine the dimension of the data in the tree, construct a Val{N}
from that and pass it through the layers into knn_kernel!()
. That should involve a single layer of dynamic dispatch to get from knn()
into the guts, and all function calls from then in would be parameterized on the dim type.
from nearestneighbors.jl.
Ah! That sounds like a much better solution indeed!
from nearestneighbors.jl.
Hmm, creating the Val
on the fly would probably kill performance unless the compiler optimized out any allocation... but you can probably get around that by just storing the Type{Val{N}}
in the tree itself as a generic Any
.
Anyway, all of this is a bit academic unless it really improves things :-) It would be unfortunate if the extra layers of dispatch just made the code more obscure but barely faster...
from nearestneighbors.jl.
Wouldn't the Val
be created only once before the loop over all query points?
from nearestneighbors.jl.
Yeah, that would probably work. It'd be nice if the single point query interface wasn't penalized too much though.
Come to think of it, your second option seems roughly equivalent to what I'm suggesting, but actually simpler and cleaner to implement. Both should involve a single layer of dynamic dispatch to get into the guts of things. It's just a question of whether it makes sense to parameterize the tree type on the dimension or not.
from nearestneighbors.jl.
It is possible that for smallish trees that fits in the cache that there could be a noticeable change. Gotta benchmark though zzz... :)
from nearestneighbors.jl.
Hi guys,
I've been thinking about this in the context of StaticArrays. I would really like to use a Vector{SVector{3,Float64}}
(for instance) to describe my points, rather than a matrix. I'm wondering if this would make NearestNeighbors a bit faster? Moving to vectors of points, rather than matrices, will really help in generalizing the package.
Perhaps one way to do this if you're sticking with matrices would be to pass the dimension along as either N (for large N) or Val{N} for very small N.
Some ideas: you can either build the dimension directly into the tree parameters, or rely on size()
and length()
being defined on the type for static arrays (so length(eltype(::Vector{SVector{3,Float64}})) = 3
), and use that as a compile-time constant (or in a @generated
function if that is faster).
from nearestneighbors.jl.
I think just putting the vector dimension as a parameter is the easiest. However, parameterizing on the actual vector type is more general and probably better. The type for a nearest neighbor tree could move to something like abstract NNTree{T <: AbstractFloat, P <: Metric, V <: AbstractVector}
and then all vectors should be supported.
We then just need to make the choice of what to do when the user gives a set of points as a matrix since I don't want to break that. The easiest would probable be to just add a dependency on StaticArrays
and then reinterpret the input matrix A
to a Vector{SVector{size(A, 1), T}}(size(A, 2)). I'm not sure how
StaticArraysscale with dimensions but when using trees like this you mostly use it in
dim < 20` and that should be fine.
from nearestneighbors.jl.
Closed by #29
from nearestneighbors.jl.
Related Issues (20)
- knn: skipped items output when there is a skip function has always the last index and not 0 index HOT 3
- `nn` lacking docstring HOT 1
- AssertionError in kmedoids alg
- bug: BoundsError when the skip function returns true for all points HOT 3
- Is there a reason sqeuclidean distance is not supported? HOT 4
- README.md Misleading Custom Metric Documentation
- Document that `inrangecount` also counts the point itself HOT 2
- [Question] Can you insert new data into an existing KDTree object? HOT 2
- Compilation time issues with very high dimensions HOT 3
- Reverse Cuthill-McKee ordering option HOT 1
- Querying number of distance evaluations HOT 2
- Make datatypes of the KNN results selectable for potentially lower memory overhead
- Does ball tree work with any metric? HOT 2
- Add example with `skip` option to documentation
- Julia 1.10 is waiting on IO to finish during compilation HOT 3
- It should be possible to make `KDNode` smaller
- KDTree: Wrong results for non-Euclidean metrics
- Cannot build KDTree with Subarrays since v0.4.14 HOT 3
- KDTree with Matrix{ComplexF64}
- Can't do `knn` on `AbstractVector{SVector}`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nearestneighbors.jl.