Parfor loop stuck when saving plots

4 views (last 30 days)
Alexandre
Alexandre on 23 Oct 2023
Commented: Rik on 25 Oct 2023
Hello everyone,
I am encountering a strange issue when using a parfor loop to perform calculations on a set of data and saving the plots.
I used to do this for my files and to plot and save multiples data (2D data of electrical field, 1D profile, FWHM size as function of the file, etc) and I never had any issue.
However, I recently try to do the same for a 3D plot (using surface function) and then the parfor loop does not respond and stop doing anything (but still my CPU is 100 %...). It happens wether I used 1 or 16 (my max) workers, and in 2 differents PC with R2020a.
While debugging, the issue is coming from the print (or saveas, I tried both) command...but this happened only for the full size of my data (X, Y and E are 2000x2000, with each a 32 MB size) ; if I plot for example only 300x300 points it works. [Furthermore I have up to 128 GB of RAM memory and never more than 20 GB is used)
I also try to plot 600x600 with 1 worker or 300x300 with 16 workers and the first case is blocked while the second one works. Also, if I put a condition in my loop so that the saving is done only in the first case (k=1), then every workers are running except the one that met that condition. What is more, if I change X and Y to a 1D array (1x2000) and keep E 2D (2000x2000), then a imagesc plot is working without issue, so there is no issue with my data I believe.
And, of course, if I run the same code with a normal for loop, everything goes well (but very slooooow !)
I have no more ideas about that, so if you have some suggestions I will be glad :)!
Here is an example of my code:
myfolder = pwd;
destdirectory = [pwd,'/3Dsave'];
mkdir(destdirectory);
L = dir('./*dat');
parfor k=1:length(L)
cd(myfolder)
pause(0.01);
N = L(k).name;
Md = table2array(readtable(N));
x = Md(:,1);
nx = length(unique(x));
y = Md(:,2);
ny = length(unique(y));
yl = reshape(y,[nx,ny]);
xl = reshape(x,[nx,ny]);
Ez = Md(:,3);
Ez2 = reshape(Ez,[nx,ny]);
Ez2=abs(Ez2.^2);
fl=figure('visible','off');
surface(xl*1e6,yl*1e6,Ez3)
shading interp
colormap jet
set(gca,'FontSize',11,'fontweight','bold')
hcb=colorbar;
hcb.Title.String = "A_0^2";
title(N)
cd(destdirectory)
pause(0.01)
print(['iml_',num2str(k),'_apng'],'-dpng','-r1200')
close(fl);
end
  4 Comments
Walter Roberson
Walter Roberson on 25 Oct 2023
length(A(1,:)) is significantly less efficient than size(A,2) . A(1,:) has to first extract all of row 1 from the matrix, even though the row elements are not going to be in consecutive memory (unless it just happened to be a vector). This results in a bunch of unnessary memory copying.
length(A(1,:)) also fails if A has no rows, such as if it A were 0 x 10 . You would get an error rather than either 0 or 10.
size() is one of the very fastest operations for any class that does not override the definition of the size() method: it only has to look at the header block for the expression. ( sym is an example of a class that redefines size. )
Rik
Rik on 25 Oct 2023
For the graphics handles I meant you should specify the parent object in every call that supports it. Perhaps that will help.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!