Sending data to workers
In my personal experience, I found that using parfeval
is better regarding memory usage than parfor
. In addition, your problem seems to be more breakable, so you can use parfeval
for submitting more smaller jobs to MATLAB workers.
Let's say that you have workerCnt
MATLAB workers to which you are gonna handle jobCnt
jobs. Let data
be a cell array of size jobCnt x 1
, and each of its elements corresponds to a data input for function getOutput
which does the analysis on data. The results are then stored in cell array output
of size jobCnt x 1
.
in the following code, jobs are assigned in the first for
loop and the results are retrieved in the second while
loop. The boolean variable doneJobs
indicates which job is done.
poolObj = parpool(workerCnt);
jobCnt = length(data); % number of jobs
output = cell(jobCnt,1);
for jobNo = 1:jobCnt
future(jobNo) = parfeval(poolObj,@getOutput,...
nargout('getOutput'),data{jobNo});
end
doneJobs = false(jobCnt,1);
while ~all(doneJobs)
[idx,result] = fetchnext(future);
output{idx} = result;
doneJobs(idx) = true;
end
Also, you can take this approach one step further if you want to save up more memory. What you could do is that after fetching the results of a done job, you can delete the corresponding member of future
. The reason is that this object stores all the input and output data of getOutput
function which probably is going to be huge. But you need to be careful, as deleting members of future
results index shift.
The following is the code I wrote for this porpuse.
poolObj = parpool(workerCnt);
jobCnt = length(data); % number of jobs
output = cell(jobCnt,1);
for jobNo = 1:jobCnt
future(jobNo) = parfeval(poolObj,@getOutput,...
nargout('getOutput'),data{jobNo});
end
doneJobs = false(jobCnt,1);
while ~all(doneJobs)
[idx,result] = fetchnext(future);
furure(idx) = []; % remove the done future object
oldIdx = 0;
% find the index offset and correct index accordingly
while oldIdx ~= idx
doneJobsInIdxRange = sum(doneJobs((oldIdx + 1):idx));
oldIdx = idx
idx = idx + doneJobsInIdxRange;
end
output{idx} = result;
doneJobs(idx) = true;
end
The comment from @m.s is correct - when parfor
slices an array, then each worker is sent only the slice necessary for the loop iterations that it is working on. However, you might well see the RAM usage increase beyond what you originally expect as unfortunately copies of the data are required as it is passed from the client to the workers via the parfor
communication mechanism.
If you need the data only on the workers, then the best solution is to create/load/access it only on the workers if possible. It sounds like you're after data parallelism rather than task parallelism, for which spmd
is indeed a better fit (as @Kostas suggests).
I would suggest to use the spmd
command of MATLAB.
You can write code almost as it would be for a non-parallel implementation and also have access to the current worker by the labindex
"system" variable.
Have a look here:
http://www.mathworks.com/help/distcomp/spmd.html
And also at this SO question about spmd
vs parfor
:
SPMD vs. Parfor