Main Content

Deploy Parallel-Enabled MATLAB Function as Standalone Application

This example shows how to deploy a Monte-Carlo simulation that runs in parallel as a standalone application using MATLAB® Compiler™.

When you deploy a MATLAB® function that runs in parallel as a standalone application, the cluster profile of the cluster where the code runs must be available to the MATLAB function. You can set the cluster profile by:

  • Including the path to the cluster profile in the MATLAB function code.

  • Including the cluster profile as an additional file when packaging the MATLAB function into a standalone application.

  • Passing the cluster profile to the standalone application at run time.

You can use the setmcruserdata function to set the cluster profile within the MATLAB function and the mcruserdata option to pass the cluster profile to the standalone application at run time. For details, see Use Parallel Computing Toolbox in Deployed Applications.

In this example, the cluster profile is included as an additional file when packaging the MATLAB function into a standalone application. For details on how to pass the cluster profile at run time, see Pass Cluster Profile at Run Time.

Create MATLAB Function

Write a MATLAB function named mcsim that runs a simple stochastic simulation based on the dollar auction. The function runs multiple simulations to find the market value for a one dollar bill using a Monte-Carlo method. Save the function in a file named mcsim.m. For details, see Use parfor to Speed Up Monte-Carlo Code (Parallel Computing Toolbox).

function B = mcsim(nTrials, nPlayers, incr, dropoutRate)

%% specify cluster profile
mpSettingsPath = which('deployLocal.mlsettings'); 
setmcruserdata('ParallelProfile', mpSettingsPath);

%% parfor-loop to produce nTrials samples
% store the last bid from each trial in B
t = zeros(1,5);
for j = 1:5
    tic
    parfor i = 1:nTrials
        bids = dollarAuction(nPlayers,incr,dropoutRate);
        B(i) = bids.Bid(end);
    end
    t(j) = toc;
end
parforTime = min(t);

%% display results
disp(['parforTime = ' num2str(parforTime)])
disp(['Average market value = ' num2str(mean(B))])

In this example, the cluster profile is included as an additional file when packaging the MATLAB function into a standalone application. Since any files added to the application while packaging are added to the application's search path, using the which command in the above code returns the absolute path to the cluster profile. You can then use the setmcruserdata function to set the cluster profile using the path to the cluster profile.

Create Standalone Application Using compiler.build.standaloneApplication

Use the compiler.build.standaloneApplication function to create a standalone application from the MATLAB function.

appFile = "mcsim.m";
parallelProfileFile = fullfile(pwd,'deployLocal.mlsettings');
buildResults = compiler.build.standaloneApplication(appFile,...
    "AdditionalFiles",parallelProfileFile,...
    "TreatInputsAsNumeric",'on')
buildResults = 
  Results with properties:

                  BuildType: 'standaloneApplication'
                      Files: {2×1 cell}
    IncludedSupportPackages: {}
                    Options: [1×1 compiler.build.StandaloneApplicationOptions]

The function generates the executable within a folder named mcsimstandaloneApplication in your current working directory.

NOTE:

  1. The generated standalone application is not cross platform, and the executable type depends on the platform on which it was generated.

  2. The generated standalone application does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, use the compiler.package.installer function.

Test Standalone Application

To run mcsim from within MATLAB, navigate to the mcsimstandaloneApplication folder from within the MATLAB desktop environment and execute one of the following commands based on your operating system:

Windows

!mcsim.exe 10000 20 0.05 0.01
Starting parallel pool (parpool) using the 'deployLocal' profile ... 
Connected to parallel pool with 4 workers. 
parforTime = 5.3772 
Average market value = 5.8624 

Linux

!./mcsim 10000 20 0.05 0.01

macOS

system(['./run_mcsim.sh', matlabroot, '10000','20', '0.05', '0.01']);

Run Standalone Application

In your system command prompt, navigate to the folder containing your standalone executable.

Run mcsim using one of the following commands based on your operating system:

Windows

mcsim.exe 10000 20 0.05 0.01

Linux

Using the shell script:

./run_mcsim.sh <MATLAB_RUNTIME_INSTALL_DIR> 10000 20 0.05 0.01

Using the executable:

./mcsim 10000 20 0.05 0.01

macOS

Using the shell script:

./run_mcsim.sh <MATLAB_RUNTIME_INSTALL_DIR> 10000 20 0.05 0.01

Using the executable:

./mcsim.app/Contents/macOS/mcsim 10000 20 0.05 0.01

Pass Cluster Profile at Run Time

To pass the cluster profile at run time, use the mcruserdata option when executing the application.

mcsim 10000 20 0.05 0.01 -mcruserdata ParallelProfile:'<path>\clusterProfile.mlsettings'

See Also

Related Topics