Parfor Data Collection in Matlab

8 views (last 30 days)
John Grant
John Grant on 13 Sep 2020
Answered: Gaurav Garg on 16 Sep 2020
Good evening,
May I please get help with saving data from a parfor loop? I have the following:
% Run Time
tic
% Preallocate memory to increase speed
b=zeros(24,1); %Make space for this array.
%c=zeros(500000,1);
% d=zeros(500000,1);
% e=zeros(500000,1);
% f=zeros(500000,1);
% g=zeros(500000,1);
%h=zeros(500000,1);
%table=[];
for j = 1:length(out(1,:)) %iterate over each run
parfor i = 1:length(out(1,j).PN.time) % Set length of vector
b=out(1,j).PN.signals.values(:,i); % Find the values to work on
c(i)=b(19,:); % Distance to target (m)
d(i)=b(20,:); % Lat. Accelerations, integrated twice (m)
e(i)=b(21,:); % Long. Acceleration, integrated twice (m)
f(i)=b(22,:); % Lat. Guidance Error
g(i)=b(23,:); % Long. Guidance Error
h(i)=b(24,:); % time to target (sec)
end
%For c_min, there's extranous zeros popping up, exclude them
[c_min,I_1]=min(c(c>0)); % Collect the closest missile/target approach (most critical value)
[d_max,I_2]=max(d); % We need to find the max value per run, but wish for the min value over all runs.
[e_max,I_3]=max(e); % We need to find the max value per run, but wish for the min value over all runs.
[f_min,I_4]=min(f); % We just want the minimum value here.
[g_min,I_5]=min(g); % We just want the minimum value here.
[h_max,I_6]=max(h); % The minimum time is 2nd most critical value, after distance to target.
table(:,j)=[c_min d_max e_max f_min g_min h_max];
end
toc
% tf=out(1,1).PN.signals.values(:,6)
I have a parsim data set consisting of 10 individual data sets, each an array of 24 rows, and a varying number of columns (usually between 200,000 and 300,000 columns). I wish to run the parfor loop, collect maximum and minimum values from each data set and compile the results in a table. What I'm finding is that, the compiled data is not consistent (the data is not lining up with the data set it should have come from). For example, a value I may expect to see from data set 2 might be from data set 1, or the results of data set 3 may repeat over and over until data set 7, etc.
  4 Comments
Walter Roberson
Walter Roberson on 13 Sep 2020
Is the overall problem solved, or just that that one small section now works better and the main problem remains?
John Grant
John Grant on 13 Sep 2020
That small section is solved, but overall I'm still stuck. What I was hoping to do was have parfor go over each data set, and generate an output which goes to a table (which the outer for loop would help with). For example, if there is 10 data sets, I should end up with a table with 10 columns in it. I don't know if that helps, but if you wish to take a crack at this problem, I'll be glad to offer any more details that might help.

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 16 Sep 2020
Hi John,
In order to achieve what you want (ending up with a table with 10 columns, in parallel manner), you can run the outside for loop as parfor and inner parfor can be serialized using an ordniary for loop.
Since there is no dependence of data between 2 iterations of the loop, you can parallelize the outer loop. However, you will be needing to declare table outside the loop and you cannot run both the loops using parfor.
To know more about parfor, you can refer to the doc here.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!