Skip to Main Content Skip to Search
Product Documentation

The MCR User Data Interface

This feature 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 by creating 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:

MATLAB Builder NE software supports a per-MCR instance state access through an object-oriented API. Unlike MATLAB Compiler, 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 can use a helper function to call these methods as shown in Supplying Cluster Profiles for Parallel Computing Toolbox Applications.

For more information, seeImproving Data Access Using the MCR User Data Interface in the MATLAB Compiler User's Guide.

Supplying Cluster Profiles for Parallel Computing Toolbox Applications

Following is a complete example of how you can use the MCR User Data Interface as a mechanism to specify a cluster 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;
    tic
    if(ischar(n))
        n=str2double(n);
    end
    for ii = 1:n
       (cov(sin(magic(n)+rand(n,n))));
    end
    time1 =toc;
    matlabpool('open');
    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');
    disp('done');
    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 matlabpool using the 'local' 
                     profile ... 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.
    done
    a =
       1.7643
    

Step 2: Set the Parallel Computing Toolbox Profile

In order to compile MATLAB code to a .NET component and utilize the Parallel Computing Toolbox, the mcruserdata must be set directly from MATLAB. There is no .NET 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 .NET 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

Step 3: Compile Your Function with the Deployment Tool or the Command Line

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

mcc -W 'dotnet:netPctComp,NetPctClass' 
          init_sample_pct.m sample_pct.m -T link:lib

Alternately, you can use the Deployment Tool as follows:

  1. Follow the steps in Deployable Component Creation 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 NamenetPctComp
    Class NameNetPctClass
    File to Compile sample_pct.m and init_sample_pct.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.

  2. 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.

      Note   The end user's target machine must have access to the cluster.

      Tip   Learn about creating type-safe interfaces for .NET components, in order to avoid data conversion tasks with MWArray. See Type-Safe Interfaces, WCF, and MEF for details.

Step 4: Write the .NET Driver Application

After adding references to your component and to MWArray in your Microsoft Visual Studio project: write the following .NET driver application to use the component, as follows. See Creating a Reference to Your Component and Creating a Reference to the MWArray API in the Getting Started chapter of this User's Guide for more information.

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

The output is as follows:

  


Recommended Products

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