"Steffen" <rileksn@gmail.com> writes:
> Admitting that I have not totally understood the use of ind2sub, it seems that
> I can squeeze the 2 for loops into 1 and thereby use a single parfor, right?
>
> parfor ind=1:300*300
> [i,j]=ind2sub([300 300],ind);
> spectrum=series(i,j,:);
> peaks=func_fit_spectrum(spectrum);
> handles=func_peak_conversion(peaks);
> end
>
> Is my understanding correct that parfor sends now for each 'ind' the spectrum
> plus the two funtions to one worker (eg. ind=1, worker1, ind=2 worker2, etc.)?
> This somehow seems wrong to me, since this would once again be serial
> computing, right?
Each worker works on different values of "ind" in parallel, and then sends the
results back to the client. (In practice, we start by sending out "ind" in large
chunks, which diminish as we get closer to the end of the PARFOR loop).
Unfortunately, using the "ind2sub" approach is not desirable here, since the
PARFOR machinery cannot then tell that you only need a subset of "series" for
each iteration (the machinery can only deduce that if you index "series" using
the loop index, and other constant terms). Something like the following allows
PARFOR to minimise the data transfer:
parfor r = 1:300
thisrow = series( r, :, : ); % index "series" using only "r" and ":"
for c = 1:300
spectrum = thisrow( 1, c, : );
...
end
end
However, if the total size of "series" is small, then the overhead of sending it
to the workers might not be significant.
> Currently, the structuring element 'handle' within the func_peak_conversion is
> growing for each run (peaks can be overwritten, as it is just the input for
> the 2nd fcn). I presume/hope the growing of the handle would still work when
> each worker adds a value separately?
Ah, sorry, I didn't look at your code closely enough to spot that detail.
Because the iterations of the PARFOR loop are executed on different machines in
totally separate MATLAB instances, there can be no interdependence between the
iterations of your PARFOR loop  so, to use PARFOR, you might need to
restructure the way you're using "peaks" and "handles".
In general though, a PARFOR loop can return data structures which grow providing
you use explicit concatenation, like this:
x = [];
parfor ii=1:10
if ii > 3
x = [x, ii];
end
end
Cheers,
Edric.
