Parallel.pool.consant slowing down code by a factor of 3, how to distribute many arrays to different variables

1 view (last 30 days)
I am trying to solve many different linear systems using gmres, each system has it's own individual matrix, but they all use the same preconditioners. I am also using a function handle to evaluate the preconditioner backsolve. Essentially,
parfor i=1:N
X{i} = gmres(A{i}, b{i}, maxit, tol, @(x) mfun(x,M{i}));
end
The individual solves take about 9 iterations and take about 0.02 seconds each, I found changing it to essentially
parfor i=1:N
for j =1:n
X{j} = gmres(A{j}, b{j}, maxit, tol, @(x) mfun(x,M{j}));
end
Xstore(i) = X;
end
was faster and was able to beat doing things in serial for small systems. In this implimentation each worker uses every preconditioner.
I tried implementing both
Mpool = parallel.pool.Constant(@() M);
and
Mpool = parallel.pool.Constant(M);
both slowed down code significantly. CPU utilization was lower and the individual gmres solves were significantly slower, there was some memory improvement.
What am I doing wrong?

Accepted Answer

Edric Ellis
Edric Ellis on 26 Jul 2019
parallel.pool.Constant is useful in essentially three different ways:
  1. It can offer a performance gain by avoiding repeated transfers of data from the client to the workers (i.e. when you have several parfor loops accessing the same large dataset)
  2. It can make stuff work that otherwise would be difficult by constructing objects directly on the workers (such as a database connection, connection to a hardware device, etc.)
  3. Similar to (2), if a really large piece of data can be constructed on the workers more quickly than transferring from the client, this can be beneficial even for only a single parfor loop.
It seems like your case doesn't fit into any of those categories - you have only a single parfor loop, and the data can be transferred from the client. Perhaps the only possible benefit you could get from parallel.pool.Constant is if you could create it like this:
Mpool = parallel.pool.Constant(@buildM)
where buildM is a function returning M.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!