Clear Filters
Clear Filters

Nested cellfun in parfor loop

4 views (last 30 days)
Ida
Ida on 8 Jun 2017
Commented: Ida on 12 Jun 2017
Hello,
I'm trying to set up a function for cells using cellfun inside a parfor loop. A simplified example seen below:
%Main function
function main
M = rand(100,100);
N = rand(100,100);
const = rand(100,1); %Does not change during parfor loop
parfor i=1:size(M,2)
X = num2cell( M(:,i),1 );
Y = num2cell( N(:,i),1 );
Z = cellfun(@(x,y) nestedFunc(const,x,y), X,Y, 'uniformoutput',0 );
end
%Nested function
function z = nestedFunc(const,x,y)
%Do a bunch of stuff...
end
end
However, using
Z = cellfun(@(x,y) nestedFunc(const,x,y), X,Y, 'uniformoutput',0 );
is not allowed inside parfor: " The nested function 'nestedFunc' cannot be called within a PARFOR loop".
I've read that you can use the feval and the handle to the function to get around this, like this (from Matlab documentation):
function A = pfeg
function out = nfcn(in)
out = 1 + in;
end
fcn = @nfcn;
parfor idx = 1:10
A(idx) = feval(fcn, idx);
end
end
I do not manage to do this when my function is a cellfun however. I can get around the issue by defining nestedFunc in a separate .m file, but I would prefer if it could be done inside the function itself. (Also, I don't know if calling a separate function takes more time compared to a nested function?)
Can anyone please advice me? Thank you!

Accepted Answer

Edric Ellis
Edric Ellis on 9 Jun 2017
Here's some code that I tried combining using nested functions together with parfor and cellfun:
function out = pfeg
const = 7;
function out = nFcn(x, offset)
out = x + offset;
end
fcnHandle = @(in) nFcn(in, const);
parfor idx = 1:10
out{idx} = cellfun(fcnHandle, num2cell(1:idx));
end
end
This works as expected. Note that using nested functions inside parfor is somewhat risky - because each worker gets its own copy of "uplevel variables" - i.e. those variables shared between parent and nested function.
  1 Comment
Ida
Ida on 12 Jun 2017
Thanks Edric,
This works as a charm and was jut what i was looking for!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!