Using Parfor for two separate matrix multiplication
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am trying to use parfor for independent matrix multiplication inside a time loop. I am measuring the cpu time and actual calculation time. I have a dual core computer and I aspect cpu time being double the execution time but they are not. In addition I don't gain any time with parfor. I don't know what I am doing wrong or if I am doing something wrong. A simplified version of my code would look like
if matlabpool('size') == 0 % checking to see if my pool is already open
matlabpool open 2
end
D=rand(1000,1000);
D2=rand(1000,1000);
R=rand(1000,1);
qum=cputime;
tickron=tic;
parfor (t=1:1000,1)
u1=D*R;
u2=D2*R;
end
Taking=toc(tickron)
muq=cputime-qum
if I use parfor
Taking = 1.76698075488077 (execution time)
muq = 0.062400400000115 (cpu time)
if I use for
Taking = 1.26792301952307
muq = 2.51161610000008
why there is no gain when I use parfor and also why cpu time is much smaller in parfor?
Thank you
Erdem
0 comentarios
Respuestas (2)
Edric Ellis
el 20 de Abr. de 2015
I think the problem here is that your parfor syntax explicitly asks the loop to run on only one worker:
parfor (t=1:1000,1)
I would suggest removing the trailing ,1 from that. See the parfor reference page for more about the syntax.
Your cputime call is measuring the CPU time on the MATLAB client, not on the workers. If you wanted to measure the CPU time on the workers, you'd need to do something like this:
spmd, cpu0 = cputime(); end
parfor idx = 1:100, rand(1000)*rand(1000); end
spmd, cpu1 = cputime() - cpu0, end
This records the initial CPU time in cpu0 which is a different value on each worker. After the parfor loop, each worker then gets a value for cpu1 which is the elapsed CPU time. You can calculate the total CPU time like so:
spmd, gplus(cpu1), end
0 comentarios
Kamuran
el 22 de Abr. de 2015
Editada: Edric Ellis
el 24 de Abr. de 2015
3 comentarios
James Tursa
el 1 de Mayo de 2015
Editada: James Tursa
el 1 de Mayo de 2015
Are you only trying to get multiple matrix multiplies to happen in parallel? Or is your concern really more general than that for your real problem? E.g., an OpenMP mex routine could easily be written to call the BLAS library in parallel for multiple matrix multiplies, but it is not clear from your examples what you are really trying to do and how the parallel calculations would occur for your real problem. I.e., a mex routine can be written that does the D=A*A and the C=B*B matrix multiplies in parallel on each iteration, but I am not sure how this would apply to your real problem.
Ver también
Categorías
Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!