Main Content

Specify Maximum Number of Threads to Run Parallel for-Loops in the Generated Code

Using MATLAB® Coder™, you can specify the maximum number of threads to run parallel for-loops in the generated C/C++ code. You can also cross-compile the code, that is, generate the code on the host hardware processor and execute it on the target hardware processor. Depending on the target hardware platform, you can specify the number of threads.

You can set the number of threads in the generated code in different ways. The table lists these options with their precedence order. The precedence determines the order of execution of these options in which the MATLAB Coder sets the number of threads. If the value of these options equals their default value, the precedence order is moved to the next option listed in the table.

If you do not set any of these options, then, by default, the generated parallel code uses the maximum number of threads available on the target hardware during run time. This use of the maximum number of threads is enabled by Open Multiprocessing (OpenMP) pragma omp_get_max_threads in the generated code.

PrecedenceOptions to Set Number of ThreadsCommands to Set Number of Threads
1

Parfor-loop with number of threads specified

% u specifies the maximum number of threads 
parfor (i = 1:10, u)
2

Configuration property (default value = 0):

NumberOfCpuThreads

cfg.NumberOfCpuThreads = 8;
3

Target processor properties (default value = 1):

NumberOfCores,

NumberofThreadsPerCore

processor.NumberOfCores = 4; 
processor.NumberOfThreadsPerCore = 2;

Note

If NumberOfCpuThreads equals 1, then Automatic parallelization of for-loop is disabled. See Automatically Parallelize for Loops in Generated Code.

Specify Number of Threads by Using MATLAB Coder App

To set the Maximum number of CPU threads:

  1. Open the MATLAB Coder app.

  2. In the Generate Code window, click More Settings.

  3. In the Speed tab, enter Maximum number of CPU threads.

The value of Maximum number of CPU threads applies to parfor-loops and automatically parallelized for-loops enabled by the setting Enable automatic parallelization.

GUI for Maximum number of CPU threads

Specify Number of Threads at the Command Line

Consider these MATLAB functions parforExample and autoparExample.

The parforExample function uses parfor-loop with the maximum number of threads set to 6.

function y = parforExample(n) %#codegen
    y = ones (1,n); 
    parfor (i = 1:n, 6) 
        y(i) = 1; 
    end 
end

The autoparExample function uses a for-loop.

function y = autoparExample(n) %#codegen
    y = ones (1,n); 
    for i = 1:n 
        y(i) = 1; 
    end 
end

Using the MATLAB functions previously specified, this table lists different examples for setting the number of threads in the parallel generated code.

Commands to Generate CodeDescriptionGenerated Code
n = 1000;
cfg = coder.config('lib');
cfg.NumberOfCpuThreads = 8;

codegen –config cfg ...
parforExample –args {n} -report

Parfor-loop sets the maximum number of threads to 6.

#pragma omp parallel for num_threads(
    6 > omp_get_max_threads() ? omp_get_max_threads() : 6)

for (i = 0; i <= ub_loop; i++) {
    y_data[i] = 1.0;
}
n = 1000;
cfg = coder.config('lib');
cfg.EnableAutoParallelization = true;
cfg.NumberOfCpuThreads = 8;

codegen –config cfg ...
autoparExample –args {n} -report

Configuration property sets the maximum number of threads to 8.

#pragma omp parallel for num_threads(
    8 > omp_get_max_threads() ? omp_get_max_threads() : 8)

for (b_i = 0; b_i < i; b_i++) {
    y_data[b_i] = 1.0;
}
n = 1000;
cfg = coder.config('lib');
cfg.EnableAutoParallelization = true;

codegen –config cfg ...
autoparExample –args {n} -report

The maximum number of threads is set to omp_get_max_threads().

#pragma omp parallel for num_threads(omp_get_max_threads())

for (b_i = 0; b_i < i; b_i++) {
    y_data[b_i] = 1.0;
}

Create Custom Hardware Processor

To add a target processor:

  1. Create a copy of an existing target processor.

    processor = target.get('Processor', 'ARM Compatible-ARM Cortex-A');
  2. Update the number of cores, number of threads per core, and the name of the new processor.

    processor.NumberOfCores = 4;
    processor.NumberOfThreadsPerCore = 2;
    processor.Name = '4coreprocessor';
  3. Add the target.Processor object to an internal database.

    target.add(processor);
  4. Select the new processor as the target processor.

    cfg = coder.config('lib');
    cfg.HardwareImplementation.ProdHWDeviceType = 'ARM Compatible->4coreprocessor';

In the MATLAB Coder app, you can choose the custom hardware processor that you have created at command line by using target.get and target.add classes.

Alternatively, you can create a target processor by using target.Processor and target.LanguageImplementation classes. For more information, see Register New Hardware Devices.

Commands to Generate CodeDescriptionGenerated Code
n = 1000;
cfg = coder.config('lib');
cfg.EnableAutoParallelization = true;
cfg.HardwareImplementation.ProdHWDeviceType ...
    ... = "ARM Compatible->4coreprocessor";

codegen –config cfg autoparExample –args {n} -report

Target processor sets the maximum number of threads to 4.

#pragma omp parallel for num_threads(
4 > omp_get_max_threads() ? omp_get_max_threads() : 4)
for (b_i = 0; b_i < i; b_i++) {
y_data[b_i] = 1.0;
}
n = 1000;
cfg = coder.config('lib');
cfg.EnableAutoParallelization = true;
cfg.NumberOfCpuThreads = 2;
cfg.HardwareImplementation.ProdHWDeviceType ...
    ... = "ARM Compatible->4coreprocessor";

codegen –config cfg autoparExample –args {n} -report

Configuration property sets the maximum number of threads to 2.

#pragma omp parallel for num_threads(
2 > omp_get_max_threads() ? omp_get_max_threads() : 2)
for (b_i = 0; b_i < i; b_i++) {
y_data[b_i] = 1.0;
}

See Also

| | | | |

Related Topics