I have a MATLAB code where I am using parfor
to reduce the amount of time taken by for
to do some image processing tasks. Basically,it is taking two images and after doing some mathematical calculations, it produces a scalar qunatity called EucDist
. For this, one image is kept fixed and another image is generated by a FORTRAN code which is taking around 20 seconds to do that. Below is the outline of my code:
matlabpool open
gray1 = some_image(8192,200);
dep = 0.04:0.01:0.40; % Parameter 1
vel = 1.47:0.01:1.72; % Parameter 2
dist = zeros(length(dep),length(vel));
tic
parfor i = 1:length(dep)
ans = zeros(1,length(vel));
for j = 1:length(vel)
% Updating the Input.txt file
fname = sprintf('Input_%.2d%s',i,'.txt');
fid=fopen(fname,'w');
fprintf(fid,'%-5.2f\n%-5.2f\n%.2d',dep(i),vel(j),i);
fclose(fid);
% Running my fortran code to generate another .dat file (Note that I have already compiled this code outside these loops)
system(['./editcrfl ' fname]);
% Calling IMAGE_GEN script incorporating the above .dat file
system('IMAGE_GEN');
system(sprintf('IMAGE_GEN %d',i));
gray2 = some_image(8192,200);
% Doing some mathematical calculations and getting a value say 'EucDist'
- - - - - - -
- - - - - - -
ans(j) = EucDist;
end
dist(i,:) = ans;
fclose('all');
end
fprintf('Total time taken: %f\n',toc);
matlabpool close
There are two major problems that I am facing with the above code.
First, the dist
matrix is not able to store all the EucDist
generated. Ideally dist
matrix should be of size 37 X 26 but it is only 37 X 1 and all the values are zeros in that. Though I have checked that all 37 X 26 values are getting calculated but don't know why it is not getting stored in dist
.
Second, the total time taken when I am using parfor
is somewhere around 9.5 hours whereas for normal for
it is taking only 5.5 hours.
Can someone please help me to get rid of the above two problems?
Thanks in advance.