How can I plot multiple plots on the same figure using parfor

16 次查看(过去 30 天)
Hi,
I am running an FEA code and the plotting is what takes most of the time as I am plotting 13000 elements.
Basically, I am trying to leverage the parallel computing and the gpu to optimize the plotting of this example (simplified example):
tic
x = linspace(1,10);
parfor i=1:20
y(i,:)=x.^((i+1)/i);
end
figure
hold on
for i = 1:20
plot(x,y(i,:))
end
hold off
cputime=toc
I got the gpu running already by using gpuArray the following way:
tic
x = gpuArray(linspace(1,10));
parfor i=1:20
y(i,:)=x.^((i+1)/i);
end
figure
hold on
for i = 1:20
plot(x,y(i,:))
end
hold off
gputime=toc
but how do I use the parfor for the plot function?
if I do this:
tic
xx = linspace(1,10,1000);
parfor i=1:20
yy(i,:)=xx.^((i+1)/i);
end
figure
hold on
parfor i = 1:20
plot(xx,yy(i,:))
end
hold off
cpuparalleltime=toc
I get a empty figure
moreover, the fastest running code seems to consistently be the single threaded cpu task.
Thank you everybody

回答(1 个)

Walter Roberson
Walter Roberson 2021-12-5
You cannot do that directly. The workers do not have access to the display, and also do not have concurrent access to the graphics objects already created.
What you can do is:
figs = cell(4,1);
parfor K = 1 : 4
figs{K} = figure();
ax = axes('Parent', figs{K});
h = plot(ax, rand(1,20));
end
This creates independent figures, but after the figures are created the figure structure (and all its children) are copied back to the client. At that point you can merge them all together by extracting the axes children and copyobj() them into the axes you want them all to appear in.
  1 个评论
Gi Ru
Gi Ru 2021-12-6
Hi thanks,
I tried it like this:
clc, clear all, close all
x =gpuArray(linspace(1,10));
li=10;
y=gpuArray(zeros(li,length(x)));
parfor i = 1 : li
y(i,:)=gpuArray(x.^((i+1)/i));
end
%%%% parallel plotting
tic
figs = cell(li,1);
parfor K = 1 : li
figs{K} = figure('Visible','Off');
ax = axes('Parent', figs{K});
h = plot(x, y(K,:));
end
for K = 2 : li
copyobj(findobj(K,'type','line'),findobj(1,'type','axes'));
end
fp=toc
%serial plotting
tic
figure
for i=1:li
plot(x,y(i,:))
end
fs=toc
but unfortunately, the parallel version is about 10 times slower than the serial one. In the end, when I have a lot of lines to put on a plot, what method would be the most efficient one? As in my application, it takes me 2 min to run my code to get the results and about 15 min to plot those results.
THanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by