Thread Subject: parfor doesn't work when it's inside a function ??

Subject: parfor doesn't work when it's inside a function ??

From: Juliette Salexa

Date: 8 Aug, 2009 20:37:02

Message: 1 of 5

It seems as if parfor doesn't work when it's in a funciton that's called externally.
I made a very simple example to demonstrate this:
-----------------------------------------------
WORKING CODE:
-----------------------------------------------
a=2;
matlabpool open local 4
parfor (i=1:4)
   g(i)=a*i;
end
matlabpool close

g=2 4 6 8 [WORKS !]
---------------------------------------------
NOT WORKING CODE:
---------------------------------------------
file 1:

function g = tempFunction(a)
  matlabpool open local 4
  parfor (i=1:4)
     g(i)=a*i;
  end
  matlabpool close
end

file 2:

a=2;
g=tempFunction(a);

DOES NOT WORK!

??? Error using ==> parallel_function>make_general_channel/channel_general at 829
Undefined function handle.

Error in ==> parallel_function>distributed_execution at 741
    [tags, out] = P.getCompleteIntervals(chunkSize);

Error in ==> parallel_function at 553
R = distributed_execution(...

Error in ==> tempFunction at 4
parfor (i=1:4)

Warning: Could not find appropriate function on path loading function handle
C:\Users\..\tempFunction.m>makeF%1/F%
Warning: Could not find appropriate function on path loading function handle
C:\Users\..\tempFunction.m>makeF%1/F%
Warning: Could not find appropriate function on path loading function handle
C:\Users\..\tempFunction.m>makeF%1/F%
Warning: Could not find appropriate function on path loading function handle
C:\Users\..\tempFunction.m>makeF%1/F%

------------------------



It really puzzles me that such a simple code, that works on its own, doesn't work when it's inside a function.

I read the documentaiton for parfor very thoroughly and it said nothing about this happening.

I'm using R2007b ... is this just a bug or am I supposed to do something ELSE to specify that the parfor will be done within a function ??

Subject: parfor doesn't work when it's inside a function ??

From: Juliette Salexa

Date: 12 Aug, 2009 03:57:03

Message: 2 of 5

I'm not sure whether or not I should report this as a bug,
since it's possible that I'm just missing an extra command of some sort.

If no one replies tomorrow then I'll go ahead and submit the bug report,
but if it's not actually a bug then it would save some time for the MW staff if I didn't bother them with it.

Thanks.

Subject: parfor doesn't work when it's inside a function ??

From: Edric M Ellis

Date: 12 Aug, 2009 10:57:47

Message: 3 of 5

"Juliette Salexa" <juliette.physicist@gmail.com> writes:

> It seems as if parfor doesn't work when it's in a funciton that's called externally.
> I made a very simple example to demonstrate this:
> -----------------------------------------------
> WORKING CODE:
> -----------------------------------------------
> a=2;
> matlabpool open local 4
> parfor (i=1:4)
> g(i)=a*i;
> end
> matlabpool close
>
> g=2 4 6 8 [WORKS !]
> ---------------------------------------------
> NOT WORKING CODE:
> ---------------------------------------------
> file 1:
>
> function g = tempFunction(a)
> matlabpool open local 4
> parfor (i=1:4)
> g(i)=a*i;
> end
> matlabpool close
> end
>
> file 2:
>
> a=2;
> g=tempFunction(a);
>
> DOES NOT WORK!

This could be a problem with the way that tempFunction is getting onto the
MATLAB path. Does it make any difference if you open the matlabpool outside of
your function? What we try to do with matlabpool is sychnronise the MATLAB path
on the workers after launching them, but it appears as though this might be
failing for some reason.

Cheers,

Edric.

Subject: parfor doesn't work when it's inside a function ??

From: Juliette Salexa

Date: 12 Aug, 2009 23:25:25

Message: 4 of 5

Thanks a lot for the response Edris!

The same error message comes up when the matlabpool open/close commands are embedded within the function (like in my above example), AND when they are applied outside of the function, for example if I were to modify "file 2" in my last example into:

matlabpool open local 4
a=2;
g=tempFunction(a);
matlabpool close

The error message that comes up in both cases is this:
??? Error using ==> parallel_function>make_general_channel/channel_general at 829
Undefined function handle.

Error in ==> parallel_function>distributed_execution at 741
    [tags, out] = P.getCompleteIntervals(chunkSize);

Error in ==> parallel_function at 553
R = distributed_execution(...

Error in ==> tempFunction at 3
    parfor (i=1:4)

Warning: Could not find appropriate function on path loading function handle
C:\..\tempFunction.m>makeF%1/F%
Warning: Could not find appropriate function on path loading function handle
C:\..\tempFunction.m>makeF%1/F%
Warning: Could not find appropriate function on path loading function handle
C:\..\tempFunction.m>makeF%1/F%

------------------
I'd be quite surprised if no one's ever used PARFOR within a function though. In fact, wouldn't that mean that PARFOR can't be used in a program that's sent to a remote cluster ?? As far as I know, sending a job to a remote cluster involves calling the job as a function from an external file...

Subject: parfor doesn't work when it's inside a function ??

From: Edric M Ellis

Date: 13 Aug, 2009 07:51:04

Message: 5 of 5

"Juliette Salexa" <juliette.physicist@gmail.com> writes:

> The same error message comes up when the matlabpool open/close commands are
> embedded within the function (like in my above example), AND when they are
> applied outside of the function, for example if I were to modify "file 2" in
> my last example into:
>
> matlabpool open local 4
> a=2;
> g=tempFunction(a);
> matlabpool close
>
> The error message that comes up in both cases is this:
> ??? Error using ==> parallel_function>make_general_channel/channel_general at 829
> Undefined function handle.
> [...]
> Warning: Could not find appropriate function on path loading function handle
> C:\..\tempFunction.m>makeF%1/F%
> [...]

Error messages like that still suggest to me that the problem you're having is
with the MATLAB path. Where is "tempFunction.m", and how did that directory get
onto the MATLAB path? The workers need access to the same function via the
MATLAB path. With the "local" scheduler, we normally do a good job of setting up
the path to be in sync, but maybe something's going wrong.

I managed to get precisely the same error that you saw, but only by adding a
path in the client MATLAB, and then modifying the path inside PARFOR to remove
the path to "tempFunction.m".

Cheers,

Edric.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
parfor Juliette Salexa 8 Aug, 2009 16:39:02
parallel Juliette Salexa 8 Aug, 2009 16:39:02
pct Juliette Salexa 8 Aug, 2009 16:39:02
bug Juliette Salexa 8 Aug, 2009 16:39:02
rssFeed for this Thread

Contact us at files@mathworks.com