parfor with knnsearch, why is so slow?

2 次查看(过去 30 天)
Hao Zhang
Hao Zhang 2018-12-14
The matlab built-in function knnsearch uses kdtree method to find K nearest neighbors, it is very optimized. However I want to further improve its speed as this part now is the bottleneck of my whole code. So my idea is to send query points by chunks and call knnsearch in parallel using parfor, but I do not understand why the parfor version is even slower than sending the query points all in once, please help or giving some explanations. Thanks! The code I used for the testing are as follows:
clear;clc;close all
n_parallel=4;
if isempty(gcp('nocreate'))==1
pooljob=parpool('local',n_parallel);
end
N=1e5;
knn_K=50;
rx=rand(N,1);
ry=rand(N,1);
BucketSize_kdtree=50;
Mdl=KDTreeSearcher([rx ry],'Distance','euclidean','BucketSize',BucketSize_kdtree); %%% kdtree search object
idx_Neighbor_cell=cell(n_parallel,1);
d_Neighbor_cell=cell(n_parallel,1);
logic_beyor_cell=cell(n_parallel,1);
N_par=round(N/n_parallel);
tic;
[idx_Neighbor1,d_Neighbor1]=knnsearch(Mdl,[rx ry],'K',knn_K);
toc
tic;
rx_query_cell=mat2cell(rx,[N_par*ones(1,n_parallel-1) N-N_par*(n_parallel-1)],1);
ry_query_cell=mat2cell(ry,[N_par*ones(1,n_parallel-1) N-N_par*(n_parallel-1)],1);
parfor i=1:n_parallel
[idx_Neighbor_cell{i},d_Neighbor_cell{i}]=knnsearch(Mdl,[rx_query_cell{i} ry_query_cell{i}],'K',knn_K);
end
idx_Neighbor=cell2mat(idx_Neighbor_cell);
d_Neighbor=cell2mat(d_Neighbor_cell);
toc;
The results:
Elapsed time is 0.389322 seconds.
Elapsed time is 1.199775 seconds.
Could anyone test this code in you machine see if you get similar speed difference? Any suggestion on improve the code further is appreciated. Thanks!

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Speed Up Statistical Computations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by