Supply Run-Time Profile Information for Parallel Computing Toolbox Applications

Following is a complete example of how you can use the MATLAB® Runtime User Data Interface as a mechanism to specify a profile for Parallel Computing Toolbox applications.

Step 1: Write Your Parallel Computing Toolbox Code

  1. Compile sample_pct.m in MATLAB.

    This example code uses the cluster defined in the default profile.

    The output assumes that the default profile is local.

    function speedup = sample_pct (n)
    warning off all;
    for ii = 1:n
    time1 =toc;
    parfor ii = 1:n
    time2 =toc;
    disp(['Normal loop times: ' num2str(time1) ...
        ',parallel loop time: ' num2str(time2) ]);
    disp(['parallel speedup:  ' num2str(1/(time2/time1)) ...
        ' times faster than normal']);
    speedup = (time1/time2);
  2. Run the code as follows after changing the default profile to local, if needed.

    a = sample_pct(200)

  3. Verify that you get the following results:

    Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
    Normal loop times: 0.7587,parallel loop time: 2.9988
    parallel speedup:  0.253 times faster than normal
    Parallel pool using the 'local' profile is shutting down.
    a =

Step 2: Set the Parallel Computing Toolbox Profile

In order to compile MATLAB code to a Java® package and utilize the Parallel Computing Toolbox™, the mcruserdata must be set directly from MATLAB. There is no Java API available to access the MCRUserdata as there is for C and C++ applications built with MATLAB Compiler SDK™.

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

Create the following init function:

function init_sample_pct
% Set the Parallel Profile:
    [profile, profpath] = uigetfile('*.settings'); 
                          % let the USER select file
    setmcruserdata('ParallelProfile', fullfile(profpath, profile));


If you need to change your profile in the application, use the parallel.importProfile and parallel.defaultClusterProfile methods. See the Parallel Computing Toolbox documentation for more information.

Step 3: Compile Your Function with the Library Compiler App or the Command Line Compiler

You can compile your function from the command line by entering the following:

mcc -S -W 'java:parallelComponent,PctClass' init_sample_pct.m sample_pct.m

For an example on how to create a Java package using the Library Compiler app, see Generate a Java Package and Build a Java Application.

Use the following information for your project:

Project NameparallelComponent
Class NamePctClass
File to Compilepct_sample.m and init_pct_sample.m

When the compilation finishes, a new folder with the same name as the project is created. This folder contains the following subfolders:

  • for_redistribution

  • for_redistribution_files_only

  • for_testing


If you are using the GPU feature of Parallel Computing Toolbox, you need to 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 the mcc command, use the -a option.

Step 4: Write the Java Driver Application

Write the following Java driver application to use the generated package, as follows, using a Java-compatible IDE such as Eclipse™:

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(200);
			 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());

The output is as follows:

(UIGETFILE brings up the window to select the PROFILE file)
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
Normal loop times: 0.7587,parallel loop time: 2.9988
parallel speedup:  0.253 times faster than normal
Parallel pool using the 'local' profile is shutting down.
The Speed Up was:2.1198

Compiling and Running the Application Without Using an IDE

If you are not using an IDE, compile the application using command-line Java, as follows:


Enter these commands on a single line, using the semi-colon as a delimiter.

javac -classpath .;C:\pct_compile\javaApp\parallelComponent.jar;

Run the application from the command-line, as follows:

java -classpath .;C:\pct_compile\javaApp\parallelComponent.jar;
        matlabroot/toolbox/javabuilder/jar/javabuilder.jar JavaParallelClass 

Related Topics