Parallelization of independent computations to exploit 100% of multicore CPU (all cores)

4 views (last 30 days)
I have a Matlab code
SOL=my_code(INPUTS)
I wish to run my code by sweeping the INPUTS variables over a wide range of values.
My PC has 4 cores. When I run my code my task manager indicates that Matlab is using in the range of 20-23% of the CPU. This is normal, it means that my code is being run on one of the four cores, which is running not far from 100% of it's capacity.
To exploit the full power of my CPU I open four Matlab terminals. Just to be clear: the Matlab program is running four times on my PC, I have four Matlab terminal windows. Each Matlab session is used to run my function my_code(INPUTS) in parallel.
My task manager informs me that each Matlab session is using 20-23% of my CPU, indicating that each code is being run by a dedicated core of my processor, and my net simulation speed is increased four fold.
So instead of running the following code with a single Matlab session (which uses max. 25% of CPU) :
for i=1:4
SOL(i) = my_code( INPUTS(i) );
end
I run in parallel the four following computations, each of which exploits around 25% of CPU:
% in Matlab session 1
SOL1 = my_code( INPUTS1 );
----------------------------------------------
% in Matlab session 2
SOL2 = my_code( INPUTS2 );
----------------------------------------------
% in Matlab session 3
SOL3 = my_code( INPUTS3 );
----------------------------------------------
% in Matlab session 4
SOL4 = my_code( INPUTS4 );
This solution works well, but is far from being 'elegant', and opening up four Matlab sessions is a bit annoying, and I end up having a lot of active windows on my PC. I am sure there must be a way to exploit near 100% of my PC's CPU with a single Matlab session, any help would be greatly appreciated.
Thanks in advance,
F.-B.

Answers (1)

José-Luis
José-Luis on 19 Sep 2016
Edited: José-Luis on 19 Sep 2016

Categories

Find more on Manage Products 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!