How can I reuse an MCR instance for multiple MATLAB Compiler standalone application restarts?

2 views (last 30 days)
At each start of a MATLAB Compiler generated standalone application, the MATLAB Compiler Runtime (MCR) is loaded which takes some time. In order to reduce the startup time it would be handy if one could reuse an MCR instance for multiple application starts.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Feb 2016
The following article shows you one way to achieve that.
 
The three major steps are:
 
1.) Create a server main application which hosts the MCR and offers some interprocess communication (IPC)
2.) Create a shared library instead of a standalone application with the MATLAB Compiler
3.) Write a client application, which uses IPC to connect to the server to tell it which library it should call
 
 
This example will work on Windows only because it utilizes Windows specific features. However, the overall approach can be ported to Linux or MAC.
 
Attached is a ZIP file, which contains the source files and the compiled binaries also.
 
Compiling your MATLAB application
 
Normally you compile a standalone application out of your MATLAB. You do this by selecting the corresponding deploytool target/Application Compiler and by specifying your main routine as entry point.
Now, you need to compile a shared library.  To do so, select the shared library target in the deploytool/Library Compiler and select your main application as function to export.
You have two options in naming your entry point function on the MATLAB side:
 
1.) Arbitrary name
 
You can give your entry point any name you like but if you do so, you need to create a small <LIBRARYNAME>.ini file, which provides the functions the server needs to call later on.
The format of the file is the following:
INIT=<LIBRARY_INITIALIZE_FUNCTION>
MAIN=<LIBRARY_MAIN_FUNCTION>
TERM=<LIBRARY_TERMINATE_FUNCTION>
 
The correct entries can be obtained from the .exports file MATLAB Compiler creates during the compilation.
For example, let us assume we compile a hallo.m file into shared library with the name mylib. Then a mylib.exports file is created with the following content:
 
mylibInitialize
mylibInitializeWithHandlers
mylibTerminate
mylibPrintStackTrace
mlxHallo
mlfHallo
 
These is a list of all functions which are exported (can be called).
Using this information, we can create a mylib.ini file with the following content:
 
INIT=mylibInitializeWithHandlers
MAIN=mlxHallo
TERM=mylibTerminate
 
Providing as INIT function the <LIBRARYNAME>InitializeWithHandlers routines also tells the sever to show the command window output. If you do not want to see the command window output discard the “InitializeWithHandlers” part. So use as INIT function:
 
INIT=mylibInitialize
 
The overall mylib.ini for not seeing any command window output would be
 
INIT=mylibInitialize
MAIN=mlxHallo
TERM=mylibTerminate
 
 
2.) Fixed name “main.m”
 
When you call your entry point function main.m you do not need to provide an .ini file. The server will automatically call the “mlxMain” routine of your library. In addition, the server will not display any command window output. If you like to obtain it, you would need to create an .ini file like described in the point above.
 
 
Running the Server
 
The server requires a .cfg file next to the server application. This .cfg file need to have the same name as the server application and has a .cfg extension. For example, the server is named MCRServer.exe then a MCRServer.cfg would be needed. 
That cfg file points to the MCR used and to the folder where the MATLAB Compiler generated libraries are placed.
The format of the file is the following:
 
MCRROOT=MCR_INSTALLATION_FOLDER\runtime\$ARCH
LIBROOT=FOLDER_WHERE_THE_LIBRARIES_ARE_PLACED
 
Where $ARCH is one of the following: win32, win64
 
(Note: Please do not use quotation marks around the entries.)
 
One example for a MCR_WORKER.cfg would be:
 
MCRROOT=C:\Program Files\MATLAB\MATLAB Compiler Runtime\v83\runtime\win64
LIBROOT=C:\libFolder
 
Once you have created/edited the MCR_WORKER.cfg you can start the server. 
Make sure that your system is configured properly for running the MCR. Especially make sure your environment variable PATH on Windows is configured correctly. For more information, see the "Working with the MCR" section of the MATLAB Compiler documentation.
 
In addition, make sure that the needed Visual Studio redistributable package is installed on the target machine.  In the case, you are using the precompiled binaries you would need the Visual Studio 2010 redistributable package to be installed. Otherwise, you need to make sure that the Visual Studio redistributable matching the Visual Studio version used to compile the server is installed.
 
Running the Client
 
Running the client is easier. Simply name the client application like the library you need to call and start it. Let us assume you like to call the library called “myLib.dll” you would need to rename you client application to “myLib.exe” and run it.
 
(Note: When running a client application named “123shutdown.exe” the server will close itself.)
 
In addition, make sure that the needed Visual Studio redistributable package is installed. The requirements are the same and for the Server. 
 
The LIBFOLDER and the .ini files
 
In the LIBFOLDER you store all libraries you like to call and if created the corresponding .ini files. The server locks the library during the usage of it. However, if the sever does not access the library, you can remove or replace them without restarting the server.
 
 
Limitations
1.) You can run one library at a time
2.) You cannot pass input arguments to it
3.) You cannot call libraries compiled with a different version of MATLAB than the version of the MCR you are using
4.) In the case you obtain command window output, it will close itself after 10 seconds the library  finished running
 
Performance Note: The first call might take longer than the successive calls because at the first call, the MCR gets fully loaded and unfortunately one cannot fully load the MCR before the first call.
 
Compiling the Server
 
The code for the server is called “MCRServer.cpp” and can be compiled (works with a Visual Studio compiler only) in MATLAB using
 
>>mbuild MCRServer.cpp
The mbuild command is part of the MATLAB Compiler. In the case you don’t have the MATLAB Compiler available create Visual Studio project and compile the code in there or use the precompiled binaries.
 (Note: Make sure that the bittedness of the server matches the bittedness of the MCR you like to use later on!)
 
Compiling the Client
 
The code for the client is called “client.cpp” and can be compiled (works with a Visual Studio compiler only) in MATLAB using
 
>>mbuild client.cpp
 (Note: The bittedness of the client and the server do not need to match.)
 

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!