How to optimize code for GPU and remove for loops?

4 次查看(过去 30 天)
Hello, I am trying to optimize this code for GPU in order to speed up calculations. Here is small data sample, but mine data is quite big (1000000x100) and there can be a number of this size arrays. I was trying to use arrayfun but due to indexing it was not an option. 3D arrays were used as well with no luck to achieve my goal. The idea of code is to eliminate pair duplicates. Here is sample for, which due to for loops and other is very slow for big data:
x1=[2 1 4 5 3;5 2 3 3 4;5 4 1 1 2;3 5 1 4 2;3 3 2 1 2];
idx1=[1 1 0 0 1;1 1 1 0 0;1 1 0 1 1;1 0 1 1 1;1 1 1 1 1];
idx=gpuArray(idx1);
x=gpuArray(x1);
[n1 n2]=size(x);
for i=1:n1
c(i,:)=x(i,x(i,:));
end
for j=1:n1
idx2(j)={find(idx(j,:)>0)};
tempIdx=ones(size(idx(j,:)));
idx3(j)={find(idx(j,:)>0)};
for i=1:n2
if(i==c(j,i))
if any(c(j,i)==idx2{j})
if tempIdx(c(j,i)==idx2{j})==1
idx2{j}(c(j,i)==idx2{j})=NaN;
tempIdx(x(j,i))=0;
end
end
end
end
end
Array x represents pairs and array idx represents the invoking of pair.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 GPU Computing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by