patch faster using parallel computing as my code take long time ?

5 views (last 30 days)
I have a strcturce CC having 2499 objects. While ploting the it takes long time due to length of CC. Is it possible to make code to run using parallel computing matlab to plot faster?
colors = colormap(jet);
load("CC.mat"); % You can download the file from Link: https://drive.google.com/file/d/16iDAcBzSN42h-tk6c3AD5ClF5VaGppy6/view?usp=sharing
j=0;
for i = 1:2499
V = zeros([526 526 551]);
pin = CC.PixelIdxList{1,i};
V(pin) = 1;
[faces,verts] = isosurface(V,0.5);
p = patch('Faces',faces,'Vertices',verts);
if mod(i,255) ==0
j=1;
else
j=j+1;
end
p.FaceColor = colors(j,:);
p.EdgeColor = 'none';
hold on
end

Accepted Answer

Zinea
Zinea on 14 May 2024
To accelerate the plotting process of the “CC” structure, the Parallel Computing Toolbox can be used. Since each iteration of the loop is independent of the others (each iteration processes a different object in CC and plots it), this is a suitable case for parallelization.
NOTE: Manipulating figures and creating plots usually requires access to the main MATLAB thread, and direct plotting with parallel workers might not be efficient due to the graphical backend limitations. To overcome this limitation, all computationally intensive tasks (like calculating isosurfaces) should be performed in parallel, and then the results are collected to plot them in the main thread.
Here is the approach using the parallel for loop (parfor) in MATLAB:
  1. It must be ensured that a parallel loop is running. If MATLAB is not configured to automatically start a parallel pool when needed, the following code can be used to open a new pool:
pool = gcp;
  1. The “isosurface” calculations are done in parallel, followed by the plotting of the results in a single thread, as given below:
colors = colormap(jet);
load("CC.mat"); % Assuming CC is loaded successfully
% Preallocate an array to store the results
results = cell(1, 2499);
% Use parfor to distribute the loop iterations
parfor i = 1:2499
V = zeros([526, 526, 551]);
pin = CC.PixelIdxList{1, i};
V(pin) = 1;
[faces, verts] = isosurface(V, 0.5);
results{i} = struct('faces', faces, 'verts', verts, 'colorIndex', mod(i-1, 255)+1);
end
% Plot the results in the main thread
figure;
for i = 1:2499
p = patch('Faces', results{i}.faces, 'Vertices', results{i}.verts);
p.FaceColor = colors(results{i}.colorIndex, :);
p.EdgeColor = 'none';
hold on;
end
You may refer to the documentation of the “Parallel for-Loops (parfor)”: https://www.mathworks.com/help/parallel-computing/
Hope it helps!
  1 Comment
Walter Roberson
Walter Roberson on 14 May 2024
Note that in theory you could
parfor i = 1:2499
V = zeros([526, 526, 551]);
pin = CC.PixelIdxList{1, i};
V(pin) = 1;
[faces, verts] = isosurface(V, 0.5);
r = struct('faces', faces, 'verts', verts, 'colorIndex', mod(i-1, 255)+1);
p = patch('Faces', r.faces, 'Vertices', r.verts);
p.FaceColor = colors(r.colorIndex);
p.EdgeColor = 'none';
results{i} = p;
end
% Plot the results in the main thread
fig = figure;
ax = axes('parent', fig);
hold(ax, 'on')
cellfun(@(P)copyobj(P, ax), results);
hold(ax, 'off')
It is, however, not clear that this would be faster than the above. Maybe???

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!