| Contents | Index |
This feature provides a lightweight interface for easily accessing MCR data. It allows data to be shared between an MCR instance, the MATLAB code running on that MCR, and the wrapper code that created the MCR. Through calls to the MCR User Data interface API, you access MCR data through creation of a per-MCR-instance associative array of mxArrays, consisting of a mapping from string keys to mxArray values. Reasons for doing this include, but are not limited to:
You need to supply run–time configuration information to a client running an application created with the Parallel Computing Toolbox. Configuration information may be supplied (and change) on a per-execution basis. For example, two instances of the same application may run simultaneously with different configuration files.
You want to initialize the MCR with constant values that can be accessed by all your MATLAB applications
You want to set up a global workspace — a global variable or variables that MATLAB and your client can access
You want to store the state of any variable or group of variables
MATLAB Builder JA software supports per-MCR instance state access through an object-oriented API. Unlike MATLAB Compilersoftware, access to a per-MCR instance state is optional, rather than on by default. You can access this state by adding setmcruserdata.m and getmcruserdata.m to your deployment project or by specifying them on the command line. Alternatively, you use a helper function to call these methods as demonstrated in Example: Supplying Run-Time Configuration Information for Parallel Computing Toolbox Applications.
For more information, see Improving Data Access Using the MCR User Data Interface in the MATLAB Compiler User's Guide.
Following is a complete example of how you can use the MCR User Data Interface as a mechanism to specify a configuration .mat file for Parallel Computing Toolbox applications.
Note Standalone executables and libraries generated from MATLAB Compiler for parallel applications can now launch up to twelve local workers without MATLAB® Distributed Computing Server™. |
Compile sample_pct.m in MATLAB. By default, the code uses the local scheduler, starts the workers, and evaluates the result.
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;
matlabpool('open',4);
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']);
matlabpool('close','force');
disp('done');
speedup = (time1/time2);
Run the code as follows:
a = sample_pct(200)
Verify that you get the following results;
Starting matlabpool using the 'local'
configuration ... connected to 4 labs.
Normal loop times: 1.4625, parallel loop time: 0.82891
parallel speedup: 1.7643 times faster than normal
Sending a stop signal to all the labs ... stopped.
Did not find any pre-existing parallel jobs created
by matlabpool.
done
a =
1.7643
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.
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 configuration 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 Configuration file:
if(isdeployed)
[matfile, matpath,c] = uigetfile('*.mat');
% let the USER select file
setmcruserdata('ParallelConfigurationFile',[matpath matfile]);
endYou 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
Alternately, you can use the Deployment Tool as follows:
Follow the steps in Creating the Magic Square Java Component to compile your application. When the compilation finishes, a new folder (with the same name as the project) is created. This folder contains two subfolders: distrib and src.
| Project Name | parallelComponent |
| Class Name | PctClass |
| File to Compile | pct_sample.m and init_pct_sample.m |
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 a Deployment Tool project, click Add files/directories on the Build tab. If you are using the mcc command, use the -a option. |
To deploy the compiled application, copy the distrib folder, which contains the following, to your end users. The packaging function of deploytool offers a convenient way to do this.
parallelComponent.jar
javabuilder.jar
MAT file containing cluster configuration information
Write the following Java driver application to use the component, 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 MCR 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 MAT file)
Starting matlabpool using the 'myconfig' configuration
... connected to 4 labs.
Normal loop times: 2.6641, parallel loop time: 1.2568
parallel speedup: 2.1198 times faster than normal
Sending a stop signal to all the labs ... stopped.
Did not find any pre-existing parallel jobs created
by matlabpool.
done
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:
javac -classpath .;C:\pct_compile\javaApp\parallelComponent.jar;
install_root/toolbox/javabuilder/jar/javabuilder.jar JavaParallelClass.java Run the application from the command-line, as follows:
java -classpath .;C:\pct_compile\javaApp\parallelComponent.jar;
install_root/toolbox/javabuilder/jar/javabuilder.jar JavaParallelClass
![]() | Managing Native Resources | Dynamically Specifying Run-Time Options to the MCR | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |