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.
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; tic if(ischar(n)) n=str2double(n); end for ii = 1:n (cov(sin(magic(n)+rand(n,n)))); end time1 =toc; parpool; tic parfor ii = 1:n (cov(sin(magic(n)+rand(n,n)))); end time2 =toc; disp(['Normal loop times: ' num2str(time1) ... ',parallel loop time: ' num2str(time2) ]); disp(['parallel speedup: ' num2str(1/(time2/time1)) ... ' times faster than normal']); delete(gcp); disp('done'); speedup = (time1/time2);
Run the code as follows after changing the default
profile to local
, if needed.
a = sample_pct(200)
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. done a = 0.2530
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: if(isdeployed) [profile, profpath] = uigetfile('*.settings'); % let the USER select file setmcruserdata('ParallelProfile', fullfile(profpath, profile)); end
Tip
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.
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 Name | parallelComponent |
Class Name | PctClass |
File to Compile | pct_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
Note
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.
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; try { C = new PctClass(); /* Set up the runtime with Parallel Data */ C.init_sample_pct(); 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()); } finally { MWArray.disposeArray(A); C.dispose(); } } }
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. done The Speed Up was:2.1198
If you are not using an IDE, compile the application using command-line Java, as follows:
Note
Enter these commands on a single line, using the semi-colon as a delimiter.
javac -classpath .;C:\pct_compile\javaApp\parallelComponent.jar; matlabroot/toolbox/javabuilder/jar/javabuilder.jar JavaParallelClass.java
Run the application from the command-line, as follows:
java -classpath .;C:\pct_compile\javaApp\parallelComponent.jar; matlabroot/toolbox/javabuilder/jar/javabuilder.jar JavaParallelClass