Chapter Contents
NAG Toolbox

Multi-threaded Functions

A number of functions in this implementation of the toolbox have been parallelized using OpenMP to allow them to exploit multiple cores. This should deliver improved performance for moderate or large datasets when run on a multicore system. Parallelism has been implemented in many key functions in the areas of: Other NAG functions may also benefit to some extent because they internally call one or more of the parallelized functions. The complete list is available here.

Enabling Multi-threading

The parallelism within the NAG Toolbox is implemented using OpenMP and so the environment variable OMP_NUM_THREADS should be set to a number greater than 1 to enable this. The number chosen will be the maximum number of processor cores which the toolbox can use. As a rule of thumb this should be the number of available (idle) processor cores on the computer running MATLAB. However: Users are also advised to turn off any power saving OS features, such as facilities that reduce the clock speed of idle cores, if they wish to achieve the best performance.

Note that other applications use OpenMP so it is possible that the OMP_NUM_THREADS environment variable has already been defined on your system. To check this, type

getenv 'OMP_NUM_THREADS'
in MATLAB. If the answer is an integer then the variable has been set. If it is an empty string ('') then it has not.

To set an environment variable on Windows do the following:

  1. Click the Start button, go to the Settings menu, and select Control Panel.
  2. On the Control Panel menu, select System.
  3. In the System Properties dialog box, click the Advanced tab.
  4. On the Advanced panel, click the Environment Variables button.
  5. In the Environment Variables dialog box, click the New button in the User Variables section.
  6. In the New User Variable dialog box, enter the name of the variable as OMP_NUM_THREADS and set the value of the variable as described above.

On Linux the precise syntax for setting environment variables depends on the user's shell.

Resetting the value of the environment variable OMP_NUM_THREADS within MATLAB using the setenv command will not change the value used by multi-threaded NAG functions. Thus NAG Toolbox also includes functions that can be used within your MATLAB program to get and set the number of threads to be used in subsequent calls to multi-threaded NAG functions. See Chapter X06 for details.