issue with Multithreading in MATLAB?

1 view (last 30 days)
Hello,
I have very long for loops i.e.
for i=1:109771
% Many steps
% STEP FOR APPENDING A MATRIX INTO A gif
end
Each step in for loop takes about 3 seconds. Is there a way to do this in parallel processing?
Can you please help me with an example. On how to modify this above code to get it working in a High Performance Computing environment.
On how many cores can I run this code?
Sincere Regards,
Sanchit

Accepted Answer

Walter Roberson
Walter Roberson on 1 Aug 2019
pool_cluster = 'myCluster';
numframes = 109771;
frame_rows = 512; %adjust as appropriate
frame_cols = 768; %adjust as appropriate
frame_panes = 3; %1 for grayscale or pseudocolor
cmap = []; %256 x 3 for pseudocolor
gif_frame_rate = 22; %fps
gif_delay = 1/gif_frame_rate;
gif_array = zeros(frame_rows, frame_cols, frame_panes, numframes, 'uint8');
myCluster = parcluster(pool_cluster);
parfor (frameidx = 1 : numframes, myCluster)
%many steps
thisframe = something_appropriate; %that is frame_rows x frame_cols x frame_panes
gif_array(:, :, :, frameidx) = thisframe;
end
if frame_panes == 1 %only valid for grayscale or pseudocolor
imwrite(gif_array, cmap, 'YourGifFileName.gif', 'DelayTime', gif_delay);
else %rgb has to be appended a frame at a time
imwrite(gif_array(:,:,:,1), cmap, 'YourGifFileName.gif', 'DelayTime', gif_delay, 'WriteMode', 'overwrite');
for frameidx = 2 : numframes
imwrite(gif_array(:,:,:,frameidx), cmap, 'YourGifFileName.gif', 'DelayTime', gif_delay, 'WriteMode', 'append');
end
end
"On how many cores can I run this code?"
The above code is for the case where you have a Distributed Computing license and a cluster named myCluster has been configured. The number of cores that would be used would depend upon what was specified in the cluster profile. It is common for the number of cores configured in a profile is the number of physical cores on the cluster, but other numbers could be configured for economic or policy reasons.
  5 Comments
Sanchit Sharma
Sanchit Sharma on 2 Aug 2019
Edited: Sanchit Sharma on 2 Aug 2019
Hello,
The code is fine its just that I need to parallize it. Even if this code runs in parallel without the cumulative sum of CSV files it will work for me. I.e. if we can parallize the below code:
Also, I have a Distributed Computing license i.e. it is installed on a high performance computing system, but I do not know if a cluster has been configured.
cd ("C:\Users\sanchitsharma\Desktop\MATLABscripts\NeutronCSV\");
Z=zeros(256,256);
Gamma=2065;
Neutron=109771;
h = figure;
axis tight manual % this ensures that getframe() returns a consistent size
filename = 'PerFrame_neutron_clusters.gif';
for i=1:Neutron
fileName = sprintf('NFrame%d.csv',i);
Gamma=csvread(fileName);
figure(1);
imagesc( Gamma' );
% pick a colormap and show "zero" (approx. A(X,Y)<1) as white
mymap = colormap( parula(1e5) );
mymap(1,:)=1;
colormap( mymap )
colorbar
xlabel('X - Pixels');
ylabel('Y - Pixels');
title('Charge Deposit (0.0253 eV monoenergetic neutron)');
% flip the YAxis
ax = gca;
ax.YAxis.Direction = 'normal';
drawnow
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if i == 1
imwrite(imind,cm,filename,'gif','DelayTime',0, 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','DelayTime',0, 'WriteMode','append');
end
end
Walter Roberson
Walter Roberson on 2 Aug 2019
The writing of the file cannot be within the parfor, but you can save all of the imind, cm as you generate them.
Caution: getframe does not always return the same size output unless you pass it the size to capture. Axes frame sizes turn out to vary slightly depending on titles and tick text and axes labels

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!