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
댓글 수: 0
답변 (1개)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!