Using Parfor for two separate matrix multiplication

2 次查看(过去 30 天)
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

回答(2 个)

Edric Ellis
Edric Ellis 2015-4-20
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

Kamuran
Kamuran 2015-4-22
编辑:Edric Ellis 2015-4-24
Thank for the reply. Lets forget about the CPU time and simply work with tic and toc. In the following code is it possible to make the C=B*B and D=A*A in parallel.
matlabpool open 2
A=rand(2000);
B=rand(2000);
C=zeros(2000,2000);
D=zeros(2000,2000);
tic
parfor idx = 1:10,
D=A*A;
C=B*B;
E=A*B;
end
toc
Thanks
  3 个评论
Kamuran
Kamuran 2015-5-1
I thought what this code does is run: for example idx=1 and idx=2 ... in parallel but not when idx=1 run calculation of D and calculation of C in parallel.
James Tursa
James Tursa 2015-5-1
编辑:James Tursa 2015-5-1
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by