From: Lee Finn on
Hi All,

I'm trying to build a mental model to help me recognize when I should use parfor and when should I use spmd. Consider the following two code snippets

% a is a cell array of arguments to some function f
spmd
a = codistributed(a);
y = cellfun(@f,a,'UniformOutput',false);
end
y = gather(y);

versus

% a is a cell array of arguments to some function f
nA = numel(a);
y = cell(nA,1);
parfor k = 1:nA
y{k} = f(y{k});
end

Both lead to the same result. Which is preferable, and why?

Thanks much for your thoughts and insights.

Sam
From: Edric M Ellis on
"Lee Finn" <lsfinn+mw(a)psu.edu> writes:

> I'm trying to build a mental model to help me recognize when I should use
> parfor and when should I use spmd. Consider the following two code snippets
>
> % a is a cell array of arguments to some function f
> spmd
> a = codistributed(a);
> y = cellfun(@f,a,'UniformOutput',false);
> end
> y = gather(y);
>
> versus
>
> % a is a cell array of arguments to some function f
> nA = numel(a);
> y = cell(nA,1);
> parfor k = 1:nA
> y{k} = f(y{k});
> end
>
> Both lead to the same result. Which is preferable, and why?

In this case, I would lean towards PARFOR since it allows for dynamic load
balancing (i.e. handles the case where the different invocations of "f" take
different amounts of time), and doesn't need the explicit "gather" after the
block. You may well find that it performs slightly better because of this, and
the communication required will be slightly less.

In general, if PARFOR can be used straightforwardly (i.e. without making the
code too convoluted to fit in with the PARFOR constraints), then I would use
that. codistributed arrays work best for problems that are too large to handle
on a single machine.

Just as an aside:

a = { ... };
a = distributed( a );

is more efficient than

a = { ... };
spmd
a = distributed( a );
end

since in the first case, the client only sends the required elements of a to
each worker, whereas in the second case, the whole of a is broadcast to each
worker, and then most of it is discarded. (The ideal situation is one where a
can be constructed in parallel on the workers though).

Cheers,

Edric.