Main Content

Specify Parallel Computing Toolbox Profile in Java Application

This example shows how to use the MATLAB® Runtime User Data Interface to specify the profile of a Parallel Computing Toolbox™ cluster in a Java® application.

For more details, see Using MATLAB Runtime User Data Interface.

Step 1: Write Parallel Computing Toolbox Code

  1. Create sample_pct.m in MATLAB.

    This example code uses the cluster defined in the default profile for Parallel Computing Toolbox.

    function speedup = sample_pct (n)
    warning off all;
    for ii = 1:n
    time1 =toc;
    parfor ii = 1:n
    time2 =toc;
    disp(['Normal loop time: ' num2str(time1) ...
        ', parallel loop time: ' num2str(time2) ]);
    disp(['parallel speedup: ' num2str(1/(time2/time1)) ...
        ' times faster than normal']);
    speedup = (time1/time2);
  2. Run the function with the input 400.

    a = sample_pct(400)

  3. The following is an example of the output, assuming the default profile is set to local:

    Starting parallel pool (parpool) using the 'local' profile ...
    Connected to the parallel pool (number of workers: 6).
    Normal loop time: 2.5651, parallel loop time: 1.6371
    parallel speedup: 1.5668 times faster than normal
    Parallel pool using the 'local' profile is shutting down.
    ans =

Step 2: Set Parallel Computing Toolbox Profile

To access the MATLAB Runtime User Data interface using a Java package built with MATLAB Compiler SDK™, you must set mcruserdata directly from MATLAB. There is no Java API to access mcruserdata as there is for C and C++ applications built with MATLAB Compiler SDK.

To set the mcruserdata from MATLAB, create an init function. This separate MATLAB function uses setmcruserdata to set the Parallel Computing Toolbox profile once. You then call your other functions to utilize the Parallel Computing Toolbox.

Create the following init_sample_pct function:

function init_sample_pct
% Set the Parallel Computing Toolbox Profile:
    % Let the USER select the cluster profile.
    [profile, profpath] = uigetfile('*.mlsettings'); 
    setmcruserdata('ParallelProfile', fullfile(profpath, profile));

To export an existing profile to an .mlsettings file, use the parallel.exportProfile (Parallel Computing Toolbox) function. For example,



If you need to change your profile in the application, use parallel.importProfile (Parallel Computing Toolbox) and parallel.defaultClusterProfile (Parallel Computing Toolbox). For more information, see Discover Clusters and Use Cluster Profiles (Parallel Computing Toolbox).

Step 3: Compile Your Code into Java Package

Build the Java package with the Library Compiler app or

Use the following information for your project:

Package NameparallelComponent
Class NamePctClass
Files to Compilesample_pct.m and init_pct_sample.m

For example, if you are using, type:

buildResults = ...
{'sample_pct.m','init_sample_pct.m'}, ...

For more details, see the instructions in Generate Java Package and Build Java Application.


If you are using the GPU feature of Parallel Computing Toolbox, you must manually add the PTX and CU files.

  • If you are using the Library Compiler app, click Add files/directories on the Build tab.

  • If you are using a function, use the AdditionalFiles option.

  • If you are using the mcc command, use the -a option.

Step 4: Write Java Application

Write source code for a Java application that accesses the MATLAB functions. Save this code as in the folder that contains the generated parallelComponent.jar package.

A sample application for this example is provided below.

import com.mathworks.toolbox.javabuilder.*;
import parallelComponent.*;
public class JavaParallelClass 
	public static void main(String[] args) 
		 MWArray A = null;
		 PctClass C = null;
		 Object[] B = null;
			 C = new PctClass();
			 /* Set up the runtime with Parallel Data */
			 A = new MWNumericArray(400);
			 B = C.sample_pct(1, A);
			 System.out.println("The speed up was: " + B[0]);
		 catch (Exception e)
			 System.out.println("The error is " + e.toString());

Compile and Run Application

Compile your Java application using an IDE or at the command prompt.

  • On Windows®, execute the following command:

    javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\parallelComponent.jar
  • On UNIX®, execute the following command:

    javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./parallelComponent.jar

Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the default path is C:\Program Files\MATLAB\R2024a.

Run the JavaParallelClass application.

  • On Windows, execute the following command:

    java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\parallelComponent.jar JavaParallelClass
  • On UNIX, execute the following command:

    java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./parallelComponent.jar JavaParallelClass


If you are running the application on the Mac 64-bit platform, you must add the -d64 flag in the java command.

The JavaParallelClass application prompts you to select the cluster profile to use. After you select the .mlsettings file, the application displays output similar to the following:

Starting parallel pool (parpool) using the 'local_mcruserdata' profile ...
Connected to the parallel pool (number of workers: 6).
Normal loop time: 2.428, parallel loop time: 1.6515
parallel speedup: 1.4701 times faster than normal
Parallel pool using the 'local_mcruserdata' profile is shutting down.
The speed up was: 1.4701

See Also


Related Topics