Use of cores and performance having multiple cores

4 Ansichten (letzte 30 Tage)
Rub Ron
Rub Ron am 23 Sep. 2024
Beantwortet: Ayush am 24 Sep. 2024
I have a script my_script.m , within the script there is a function my_function(var), where var is a one dimention variable. I need to run the function multiple times changing var (e.g over [3 7 9]). Lets say I want to run that script in a machine that has 32 cores and I want to utilize most of the cores to reduce the computation time. What is the differences in the performace in running the script with a parfor tool like in option 1, and in running it using three scripts separatelly like in option 2. I have notice that option 2 seems to perform better in term of core usage and reduced computation time. Is there a way I can get the perfomance of option 2 using a single script? Any information will be appreciated.
Option 1:
% my_script.m
parm = [3 7 9];
parfor x=parm
my_function(x)
end
Option 2:
% my_script_a
my_function(3)
% my_script_b
my_function(7)
% my_script_c
my_function(9)

Antworten (1)

Ayush
Ayush am 24 Sep. 2024
I understand you are getting performance differences in the two workflows : using “parfor” (option-1) and running separate scripts(option-2). This difference can be attributed to how MATLAB handles parallel execution and resource allocation.
Some key factors because of which option-2 is performing better are as follows:
  1. parforoccurs some overhead due to parallel pool setup, data transfer and synchronization. This overhead can sometimes outweigh the benefits of parallel execution, especially for small or quick computations.
  2. All iterations in “parfor” share same MATLAB session, which can lead to contention for shared resources like memory and I/O.
  3. Option-2’s better performance can also be attributed to the fact that in this situation, the operating system can schedule each script to run on different cores, potentially leading to better utilization.
To achieve Option-2 performance in single script, you can use following strategies:
  1. Manual Parallel execution:
You can use system calls to invoke separate MATLAB sessions for each task. Following is the pseudo code for your reference:
system('matlab -batch "my_function(3)" &');
system('matlab -batch "my_function(7)" &');
system('matlab -batch "my_function(9)" &');
2. Batch Processing: you can use MATLAB’s batch function to run tasks in the background, which can help distribute tasks more evenly across cores. Following is the pseudo code for your reference:
jobs = [
batch(@my_function, 0, {3}),
batch(@my_function, 0, {7}),
batch(@my_function, 0, {9})
];
wait(jobs);
You can read more about "batch" function here: https://www.mathworks.com/help/parallel-computing/batch.html
You can read more about "system" function here: https://www.mathworks.com/help/matlab/ref/system.html
Hope this helps!

Kategorien

Mehr zu Parallel Computing Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by