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

5 次查看(过去 30 天)
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

采纳的回答

Zinea
Zinea 2024-5-14
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 个评论
Walter Roberson
Walter Roberson 2024-5-14
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???

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by