Main Content

Specify Parallel Computing Toolbox Profile in .NET 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 .NET 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 .NET component 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 Function

Build the .NET component with the Library Compiler app or using the following information:

Library NamenetPctComp
Class NameNetPctClass
Files to Compilesample_pct.m and init_sample_pct.m

For example, if you are using, type:

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

For more details, see the instructions in Generate .NET Assembly and Build .NET 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: Build and Run .NET Application

Open Microsoft® Visual Studio® and create a C# Console App called DotNETPCT.

Write source code for a .NET application that accesses the MATLAB functions.

A sample C# application for this example is provided below.

using System;
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;
using netPctComp;
namespace PctNet
    class Program
        static void Main(string[] args)
                NetPctClass A = new NetPctClass();
                // Initialize the PCT setup
                double var = 400;
                MWNumericArray out1;
                MWNumericArray in1 = new MWNumericArray(var);
                out1 = (MWNumericArray)A.sample_pct(in1);
                Console.WriteLine("The speedup is {0}", out1);
                        // Wait for user to exit application
            catch (Exception exception)
                Console.WriteLine("Error: {0}", exception);


This example code was written using Microsoft Visual Studio 2019.

In Visual Studio, add a reference to your assembly file netPctComp.dll located in the folder where you generated or installed the assembly.

Add a reference to the MWArray API.

If MATLAB is installed on your systemmatlabroot\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll
If MATLAB Runtime is installed on your system<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<version>\MWArray.dll

Build and run the DotNETPCT application in Visual Studio.

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

The speedup is 1.5217.

See Also


Related Topics