parfor with knnsearch, why is so slow?

3 ビュー (過去 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 Center および File ExchangeSpeed Up Statistical Computations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by