Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search code in Julia #4

Open
una-dinosauria opened this issue Nov 12, 2017 · 2 comments
Open

Search code in Julia #4

una-dinosauria opened this issue Nov 12, 2017 · 2 comments

Comments

@una-dinosauria
Copy link
Owner

una-dinosauria commented Nov 12, 2017

Without multithreading and due to JuliaLang/julia#939, implementing the lookup-table-based search of MCQ in Julia is simply not competitive. We should revisit this once Julia gets decent multithreading and faster partial sort.

@una-dinosauria
Copy link
Owner Author

This would also remove the C++ code that we have and, save the CUDA code, make the project fully-Julian.

@una-dinosauria
Copy link
Owner Author

una-dinosauria commented Apr 1, 2018

I have written a Julia version here

function linscan_pq_julia(
B::Matrix{Int16}, # m-by-n. The database, encoded
X::Matrix{Cfloat}, # d-by-nq. The queries.
C::Vector{Matrix{Cfloat}}, # The cluster centers
knn:: Int = 10000) # Number of knn results to return
m, n = size( B )
d, nq = size( X )
subdims = splitarray( 1:d, m )
@show knn, nq
dists = zeros( Cfloat, knn, nq )
idx = zeros( Cuint, knn, nq )
Bt = B'
# Compute distance tables between queries and codebooks
tables = Vector{Matrix{Cfloat}}(m)
for i = 1:m
# tables[i] = Distances.pairwise(Distances.SqEuclidean(), X[subdims[i],:], C[i])
tables[i] = Distances.pairwise(Distances.SqEuclidean(), C[i], X[subdims[i],:])
end
# Compute approximate distances and sort
@inbounds for i = 1:nq # Loop over each query
# println(i)
xq_dists = zeros(Cfloat, n)
p = zeros(Cuint, n)
for j = 1:m # Loop over each codebook
t = tables[j][:,i]
for k = 1:n # Loop over each code
xq_dists[k] += t[ Bt[k,j] ]
end
end
# p = sortperm(xq_dists; alg=PartialQuickSort(knn))
sortperm!(p, xq_dists; alg=PartialQuickSort(knn))
# @simd for j=1:n; p[j]=j; end
# sortperm!(p, xq_dists; alg=PartialQuickSort(knn), initialized=true)
# sort(xq_dists; alg=PartialQuickSort(knn))
dists[:,i] = xq_dists[ p[1:knn] ]
idx[:,i] = p[1:knn]
end # @inbounds
return dists, idx
end

Too slow in the meantime.

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

No branches or pull requests

1 participant