Hi everyone.
I want to know how would I use the parfor statement (if it's possible) or something similar to get optimal CPU use.
I have a i7 7700k processor, which has 4 cores and 8 threads.
For example, if I run this code to calculate number pi:
% START
clear all
format long
d=1e-09;
tic
a=0;
for i=0:d:1
a=a+sqrt(1-i*i)*d;
end
toc
p=(a-atan(1))*4
% END
I obtain:
Elapsed time is 26.742359 seconds.
p =
1.999863386004108e-09
where "p" is the difference between the number I'm calculating and the number pi.
So now, if I cut the "for" statement in four parts (all of them independent of each other), I obtain:
%START
clear all
format long
d=1e-09;
tic
a1=0;
a2=0;
a3=0;
a4=0;
for i=0:d:1/4-d
a1=a1+sqrt(1-i*i)*d;
end
for i=1/4:d:2/4-d
a2=a2+sqrt(1-i*i)*d;
end
for i=2/4:d:3/4-d
a3=a3+sqrt(1-i*i)*d;
end
for i=3/4:d:1
a4=a4+sqrt(1-i*i)*d;
end
a=a1+a2+a3+a4;
toc
p=(a-atan(1))*4
% END
getting this result:
Elapsed time is 26.957344 seconds.
p =
2.000035692617530e-09
which is practically the same result (in time and calculation).
In both cases, the use of the total CPU is 14%.
So I want to assignate, in the second case, one core per "for" statement (I mean, core 1 to calculate a1, core 2 to calculate a2, and so on), and when the four calculations are finalized, and only then, calculate "a". Time processing would be four times shorter? Is possible to do that?
Thanks for your time!

 Accepted Answer

Walter Roberson
Walter Roberson on 7 Jun 2017
Edited: Walter Roberson on 7 Jun 2017
a = zeros(1,4);
parfor s = 1 : 4
b = 0;
for i = (s-1)/4 : d: s/4-d
b = b + sqrt(1-i*i) * d;
end
a(s) = b;
end
Note: your code has the upper ranges end at something minus d for the a1, a2, a3, but for a4 the upper range ends at 1 instead of 1 - d. Your code would therefore do one extra iteration for a4, and that is not coded for here. However, when i becomes 1 exactly for a4, then sqrt(1-i*i) becomes 0, and so the iteration would have been wasted, for this particular formula. In the more general case, that final iteration might be needed, so remember to account for the boundary conditions carefully.

3 Comments

Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers. >> pi2 Elapsed time is 6.776339 seconds.
p =
2.000035692617530e-09
CPU perfomance: 58-60%
thanks thanks thanks...
charly
charly on 7 Jun 2017
Edited: charly on 7 Jun 2017
Trying to thank you, just give you a tip. Instead use a=zeros(1,4) use a(4)=0. See the difference in this example:
% START
clear all
N=100000;
tic
for i=1:N
c=zeros(1,N);
end
toc
% END
.
>> d
Elapsed time is 2.804412 seconds.
.
% START
clear all
N=100000;
tic
for i=1:N
c(N)=0;
end
toc
% END
.
>> d
Elapsed time is 0.000911 seconds.
Both cases just create N times the same zero vector of N elements. When you define c(N)=0, all the elements from 1 to N are zero also. Probably you knew it, just for the case.
c = zeros(1,N) always gives a result of 1 x N exactly.
c(N) = 0; with an existing variable c will have one of several possible results:
  • If c is scalar, then c will be extended to a row vector with N elements with the initial value left alone and the rest set to 0
  • If c is non-scalar with N or more elements, then element N in logical indexing order will be set to 0 and the rest will be left alone
  • If c is a non-scalar vector with fewer than N elements, then it will be extended in its current orientation with the existing values left alone and the rest set to 0
  • otherwise c is an array with 2 or more dimensions and fewer than N elements; MATLAB will error about attempting to grow a vector along an ambiguous dimension
The certainty of what size and orientation and contents that c will end up with make it worth using zeros(1,N) instead of c(N) = 0

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 7 Jun 2017

0 votes

It is possible ONLY if you have the parallel computing toolbox. Otherwise, you can want for MATLAB to know what it is that you want to do, but it just won't happen.
Of course, if you happen to have the mindreading toolbox, then you need not write any code at all, and MATLAB will just do as you wish. This toolbox seems to be forever under beta test though.

1 Comment

Ok, but how? I have Par Com Toolbox, but I want to learn the basic code to start using it. That's why I put the simple example of pi's calculation. Do you know how would I write the same code but using par com toolbox?
Thanks

Sign in to comment.

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!