parfor slower than for in my code.

2 visualizaciones (últimos 30 días)
pb
pb el 10 de Dic. de 2018
Comentada: pb el 11 de Dic. de 2018
Hi,
My code is the following
parfor nn=1:Na
temp=A{nn}*B{nn};
temp(1,1) = 0; temp(Nb,1) = 0;
D(nn,:)=((C{nn}\temp));
end
Where A ,C are cell arrays storing Na number of sparse matrices-(NbxNb), B is a cell array storing Na vectors of Nbx1 size
I am finding that my above loop is faster when i use 'for' loop instead of 'parfor' loop. Does anyone know why? (assume that I am only intrested in the reason for-' the code being slower with parfor compared to for'.)
Thank you.
  2 comentarios
Steven Lord
Steven Lord el 10 de Dic. de 2018
What are typical values for Na and Nb in your application?
pb
pb el 10 de Dic. de 2018
Hi Steven,
Na and Nb are order of 10^3

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Dic. de 2018
For sufficiently large matrices the * operation is handled by high performance multi threaded library that uses all available cores. When you parfor then by default each worker gets one core so in each worker the high performance library operates in effective serial . You have several of those serial operation at the same time but each one takes more clock time than the original multithreaded operation. It is at best equivalent total time but probably worse since your number of cores used in multithreaded mode might exceed your parpool size.
... and meanwhile each parpool worker is aa separate process that has to be created and be set up to run MATLAB . It is certain that you will have that overhead when you parfor so parfor is often slower than not using parfor.
parfor wins under two circumstances:
  1. the code would not be automatically parallelized with the high performance library , such as if the arrays are too small or the operation does not match an implemented pattern . In such a case you still have the overhead of creating the workers but you can get several done in the same clock time
  2. the code involves waiting for external resources such as disk or network . In such aa case with multiple workers some could be making progress while others are waiting which is better than having the cpus sit idle for every wait.
  3 comentarios
Edric Ellis
Edric Ellis el 11 de Dic. de 2018
Further to Walter's answer, parfor can also win if you have a MATLAB Distributed Computing Server cluster available. In that case, you can have more computational resources available to you than those in your desktop computer.
pb
pb el 11 de Dic. de 2018
Thank you Edric for the comment. I am trying computing on cluster now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing Fundamentals en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by