MATLAB Answers

How to optimize code for GPU and remove for loops?

3 views (last 30 days)
Mantas Vaitonis
Mantas Vaitonis on 23 Jun 2018
Commented: Mantas Vaitonis on 5 Jul 2018
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.

  3 Comments

umichguy84
umichguy84 on 5 Jul 2018
Can you explain in words what your trying to achieve. I'm not sure what you mean by pair duplicates.
Mantas Vaitonis
Mantas Vaitonis on 5 Jul 2018
Hello, It is no longer necessary, did find a solution.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by