About PARFOR using 2d array

8 views (last 30 days)
Leonardo Filho
Leonardo Filho on 31 Mar 2015
Commented: Leonardo Filho on 11 Mar 2017
Dear all,
I'm trying to use a parfor in the 2d array with the code below:
parfor j = 1:6000
for k = 1:8100
SR(k+8100,j+6000,1) = CAM04R(k,j,1);
SG(k+8100,j+6000,2) = CAM04G(k,j,1);
SB(k+8100,j+6000,3) = CAM04B(k,j,1);
end
end
But the Matlab its showing the follow messages:
The parfor loop cannot run due to the way variable 'SR' is used
The parfor loop cannot run due to the way variable 'SG' is used
The parfor loop cannot run due to the way variable 'SB' is used
Any suggestion about how I can try to fix this?
Thanks!

Answers (2)

Edric Ellis
Edric Ellis on 31 Mar 2015
To make SR, SG, and SB be "sliced" output variables from the parfor loop, you need to follow the rules described here. Basically, you need to remove the offset to the indexing expressions. For example, the following works just fine:
parfor i = 1:10
for j = 1:10
x(j, i, :) = rand(3,1);
end
end

Leonardo Filho
Leonardo Filho on 1 Apr 2015
Hello Edric,
Thanks by your prompt feedback!
Yes, I know that I can't use offset inside the PARFOR, however I need try to use this because I am processing sub-images to generate an synthetic image. Below you have the new entire code [modified]:
%CAM01[RGB]:
parfor j = 1:8100
for k = 1:6000
S(j+8100,k,:) = CAM01(j,k,:);
end
end
%CAM02[RGB]:
parfor j = 1:8100
for k = 1:6000
S(j,k,:) = CAM02(j,k,:);
end
end
%CAM03[RGB]:
parfor j = 1:6000
for k = 1:8100
S(k,j+6000,:) = CAM03(k,j,:);
end
end
%CAM04[RGB]:
parfor j = 1:6000
for k = 1:8100
S(k+8100,j+6000,:) = CAM04(k,j,:);
end
end
For CAM01, CAM02, CAM03 I am using offset inside the PARFOR but I dont have issues. Only for CAM04.
Any suggestion about?
Thanks,
Leonardo.
  2 Comments
Edric Ellis
Edric Ellis on 1 Apr 2015
If that's all you are doing inside parfor, it's surely much quicker to use indexing expressions like so:
S(8101:16200, 1:6000, :) = CAM01(1:8100, 1:6000, :);
S(1:8100, 1:6000, :) = CAM02(1:8100, 1:6000, :);
S(1:8100, 6001:12000, :) = CAM03(1:8100, 1:6000, :);
S(8101:16200, 6001:12000, :) = CAM04(1:8100, 1:6000, :);
(I'm not sure why parfor can handle one offset but not two...)
Leonardo Filho
Leonardo Filho on 11 Mar 2017
Hi Edrick, Thanks again by your response. The issue is that is not a simply "indexing question". In the parloop I need to use i,j values of the synthetic image to calculate new values for i and j (named "c" and "l") to get RGB values in each camera (CAM01,02,03 and 04). Bellow you can see all code for one CAM01:
parfor j=8101:16200 for i=1:6000
%projective equations DX/DZ e DY/DZ
DX = ms1(1,1)*((res_pixel*((i-1)-Cx)+(s1(16,1)*res_pixel))-s1(10,1)) + ms1(1,2)*((-res_pixel*((j-1)-Cy)+(s1(17,1)*res_pixel))-s1(11,1)) + ms1(1,3)*(Zp-s1(12,1));
DY = ms1(2,1)*((res_pixel*((i-1)-Cx)+(s1(16,1)*res_pixel))-s1(10,1)) + ms1(2,2)*((-res_pixel*((j-1)-Cy)+(s1(17,1)*res_pixel))-s1(11,1)) + ms1(2,3)*(Zp-s1(12,1));
DZ = ms1(3,1)*((res_pixel*((i-1)-Cx)+(s1(16,1)*res_pixel))-s1(10,1)) + ms1(3,2)*((-res_pixel*((j-1)-Cy)+(s1(17,1)*res_pixel))-s1(11,1)) + ms1(3,3)*(Zp-s1(12,1));
%image coordinates without distortion
x = -s1(1,1)*(DX/DZ);
y = -s1(1,1)*(DY/DZ);
%Radial simmetric distortion k0,k1,k2,k3
r = sqrt((x^2)+(y^2));
drad_x = (s1(4,1)+(s1(5,1)*r^2)+(s1(6,1)*r^4)+(s1(7,1)*r^6))*x;
drad_y = (s1(4,1)+(s1(5,1)*r^2)+(s1(6,1)*r^4)+(s1(7,1)*r^6))*y;
%Decentering distortion p1,p2
ddesc_x = s1(8,1)*(r^2+(2*x^2))+2*s1(9,1)*x*y;
ddesc_y = s1(9,1)*(r^2+(2*y^2))+2*s1(8,1)*x*y;
%Coordinates with distortion
x_dist = x + s1(2,1) + drad_x + ddesc_x;
y_dist = y + s1(3,1) + drad_y + ddesc_y;
%double value (column and line)
c1 = ((x_dist/res_pixel2)+Cx2)+1;
l1 = ((-1*(y_dist/res_pixel2))+Cy2)+1;
%int value (column and line)
c2 = fix(((x_dist/res_pixel2)+Cx2)+1);
l2 = fix(((-1*(y_dist/res_pixel2))+Cy2)+1);
%Bilinear interpolation:
dx1 = double(c1 - c2);
dy1 = double(l1 - l2);
a1 = double(CAM01(l2,c2,:));
a2 = double(CAM01(l2,c2+1,:));
a3 = double(CAM01(l2+1,c2,:));
a4 = double(CAM01(l2+1,c2+1,:));
RGB = uint8(a1 + (dx1*(a2 - a1)) + (dy1*(a3 - a1)) + (dx1*dy1*(a1 - a2 - a3 + a4)));
S(j,i,:) = RGB;
end
end
The parloop is working fine but the main problem is the speed. Is too slow....
Any suggestion?
Thanks!

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!