convert for loop into parfor loop

Can somebody point out how I have to modify the code below to allow it being used in a parfor loop
H = height(RTImport)
location = 'location..'
ds = datastore(location)
TT = tall(ds)
func = @(x) mean(x,'omitnan');
x1{H} = {};
x2{H} = {};
tic
for i = 1:H
Strt = minutes(RTImport.Start(i))
endT = Strt + minutes(1)
S = timerange(Strt,endT,'closed')
TT1 = TT(S,:)
Strt = Strt + minutes(1)
endT = Strt + minutes(1)
S = timerange(Strt,endT,'closed')
TT2 = TT(S,:)
x1{i} = varfun(func,TT1,'OutputFormat','table')
x2{i} = varfun(func,TT2,'OutputFormat','table')
gather(x1{:},x2{:})
end
toc
I get the information unable to classify x1 in the body of the parfor. Going through the information, I couldnt really figure out what wrong with it...Appreciate any help

6 Comments

Did you try pre-allocating it to the correct size? And why aren't you storing the result of gather in a variable? And why are you only using 3 semicolons? There is no reason to omit them if you are already at the stage that you worry enough about the performance that you want to use parfor.
Rik, please correct me if I'm wrong but I thought I pre-allocated them by doing this: x1{H} = {} with H corresponding to the number of loop iterations. At least I dont get any warning messages anymore from Matlab that the size of the variable changes over time. I added the semicolons. I'm (obviously) ne and have no previous experience with coding and have dig myself through. No question I make some obvious mistakes. I appreciate you pointing this out which helps to improve my code.
Why I didnt write gather in a variable? The way I'm doing it seems to work. Sorry if I'm asking the obvious, but Im writing my results from each iteration into x1 and x2 and since its deferred result I gather it before going into the next cycle. Why is it better to have an additional variable?
I prefer a more explicit pre-allocation like this:
x1=cell(1,H);
Your call to gather doesn't actually do anything here. It has an output, but that isn't stored, so your x1 variable remains unchanged. It is very rare in Matlab that a call to a function will modify the input in the calling workspace. I don't know of any examples outside the symbolic toolbox with functions like assume. In short: your call doesn't do anything, so it can be omitted. If you need it, you should probably use this instead:
[x1{i},x2{1}]=gather(x1{i},x2{i});
(but I am not sure why you would need it)
A small note: because you're using a parallel loop you can't be sure about the order, and no iteration can depend on any other (so the concept of needing to gather variables before doing the next iteration may be flawed). For some calculations it takes more time to copy the variables to all the separate workers that to do the calculation in a single thread. This overhead is the reason why parfor is in some cases slower than for.
ok the semikolon make a big difference, I had no clue
Rik, thanks! I learnt a lot today. I appreciate your input!
You're welcome.

Sign in to comment.

Answers (0)

Categories

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

Tags

Asked:

on 17 Nov 2020

Commented:

Rik
on 17 Nov 2020

Community Treasure Hunt

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

Start Hunting!