MATLAB Answers

How to plot in a loop with the help of GPUs (using parallelization)?

48 views (last 30 days)
Soumya Bhattacharya
Soumya Bhattacharya on 8 Feb 2017
Commented: Walter Roberson on 25 Jan 2020
I'm overlapping 4 to 5 different plots (scatter, quiver, plot etc) on a single figure for each loop cycle, and then I'm looping the whole process over 1000 cycles to make a video of the compound plots. The data set I'm using is very large and therefore the whole process is taking painfully long time. I have two GPU cards, Quadro M4000 (1664 cores) and Tesla K40c (2880 cores), and I want to use them to make the whole process faster. Is there any way to distribute the process over GPUs, like using matlab command for parallelization, to make it faster? Thank you in advance.

  1 Comment

Joss Knight
Joss Knight on 9 Feb 2017
Do you mean you want both cards to render plots separately? Because, even if your Tesla card could display graphics (which it can't), the OS will only let you use one GPU for display.

Sign in to comment.

Answers (1)

Joss Knight
Joss Knight on 9 Feb 2017
Edited: Joss Knight on 9 Feb 2017
Yes, if mean spreading computation between the GPUs, and not just rendering. Start a parallel pool with 2 workers
parpool('local', 2);
The default behaviour when the pool opens is for each worker to select a different GPU.
Depending on what you are doing and the dependencies that you have between one iteration and the next, you may find that parfeval or parfor is appropriate. If you just want to run the same code on two workers then you could use spmd. But in that case you might find it is easier just to start two MATLAB sessions and use gpuDevice to select different GPUs on each one.
Looking at your post I'm not sure any of this is useful to you because you are running plot commands, and they cannot use the GPU (except to actually draw the graphics).


Show 6 older comments
Walter Roberson
Walter Roberson on 21 Jan 2020
Also when doing heavy plotting, you should try to reduce the number of graphics objects. For example above we see
This can be merged into a single graphics object:
scatter([x_1(:,1,k); x_2(:,1,k); x_3(:,1,k); x_4(:,1,k)], ...
[y_1(:,1,k); y_2(:,1,k); y_3(:,1,k); y_4(:,1,k)], ...
It looks likely to me that a number of quiver() calls could be merged.
Rafael Freire
Rafael Freire on 24 Jan 2020
Hi Walter.
Thank you for the answer. However it is not holding on the plots. In every loop it is replacing the previous plot. When the loop finishes, you end up only with the last interaction plotted. I think it is replacing the XData and YData in each loop interaction and not beeing added. Please, do you have any ideia about it? Thank you in advance
The plot that I expected
hold on
for i=1:100
plot([0 i]-i, [0 i], 'color', cmap(i,:))
plot([0 i]+i, [0 i], 'color', cmap(i,:))
In this code it is only keeping the last interaction in the plot
for i=1:100
if i == 1
h(1) = plot([0 i]-i, [0 i], 'color', cmap(i,:));
hold on
h(2) = plot([0 i]+i, [0 i], 'color', cmap(i,:));
hold off
set(h(1), 'XData', [0 i]-i, 'YData', [0 i], 'color', cmap(i,:));
set(h(2), 'XData', [0 i]+i, 'YData', [0 i], 'color', cmap(i,:));
Walter Roberson
Walter Roberson on 25 Jan 2020
In your actual code, you have a "hold off" at the end of the loop, so I ended up mislead into thinking that you only wanted the graphics generated within the loop rather than cumulative graphics from all of the loops. However, with the "hold on" at the top of your loop, your code does accumulate everything.
With your changing colors each time, it is not possible to merge your plot() into a single object by just pulling out the previous coordinates and adding the new ones: you do need a new object for each different color.
Your graphics is being slowed down by having too many individual graphics objects. You will need to work harder to merge objects, like I showed for your scatter() call.
If you get too many line() objects of different colors, you might end up needing to merge everything into a single patch object that you keep updating the properties of.

Sign in to comment.

Sign in to answer this question.