You have a wide range of options for deploying and sharing an application developed in MATLAB®. One of the most straightforward options is to package individual algorithms and applications into a single file and distribute the file to other MATLAB users. However, in many situations this simple approach is not practical. Production requirements, the IT landscape, or performance needs may call for a more advanced deployment strategy.
This article guides you through the process of selecting the best deployment approach for your MATLAB application. It covers the three main strategies available: using batch processing to deploy MATLAB code directly, generating C code with MATLAB Coder™, and deploying with MATLAB Compiler™.
This overview of deployment strategies is by no means exhaustive. There is no one-size-fits-all strategy, and it is important to thoroughly analyze your requirements before choosing a strategy to meet them.
Application Structure: Front End and Back End
Before considering a deployment strategy, it is important to understand the structure of your application and how it affects the options available. All MATLAB applications consist of two parts: a front end and a back end. The front end is the part that the end user interacts with. It is the interface or the web page, usually consisting of control elements (including buttons and boxes where the user can type numbers or other information) and display elements (including tables and graphs).
The back end is the computational engine that computes results and produces the graphics to be displayed. The back end and front end in combination govern your options for deployment. Since the deployment of a MATLAB based front end is fairly straightforward, this article will focus on options for back-end deployment.
Deploying Native MATLAB Code to Other MATLAB Users
There are two basic strategies for deploying uncompiled MATLAB code: use MATLAB as a batch processing engine, or use MATLAB Parallel Server™.
Using MATLAB Batch Processing
One advantage of the batch processing approach is that it’s easy to implement—updating the server code is a trivial task. However, its scalability is limited, so it may not be suitable for processing multiple concurrent requests. This approach also requires a concurrent network license for MATLAB since the batch processing is not bound to one specific user.
In MATLAB batch processing, MATLAB is used, not as an interactive environment, but as a process to execute a single command or, for more complex calculations, a script containing a sequence of commands. The process that invokes MATLAB (typically, the front end) must be able to launch an external program (in this case, MATLAB) on the same machine. Specifically, the invoking process launches MATLAB using the “-r” command-line option. The command
matlab -r "statement" will start MATLAB and execute the specified MATLAB
statement, which can be a MATLAB function or script. For example, the following statement could be used to start batch processing of a file named
blackscholes.m located in a directory in the MATLAB search path:
matlab –r “blackscholes;quit”
blackscholes.m script performs any steps needed to complete the batch processing, such as reading parameters or data from a file or database, computing results, and saving the results to another file or database.
In practice, a more advanced command is used to handle errors and suppress unnecessary visual elements:
matlab –nodesktop –nosplash –wait –r “try,blackscholes;catch,end;quit”
Batch processing is suitable when a simple call to an external executable is sufficient to complete the processing and high availability is not a strict requirement. Note that there is little benefit to using this approach when you intend to use MATLAB for the front end as well, because it would involve starting a second instance of MATLAB with another license.
Using MATLAB Parallel Server
Whereas batch processing simplifies the execution of uncompiled MATLAB code on the same machine as the front end, MATLAB Parallel Server makes it easy to offload computationally intensive MATLAB code from the desktop to a server environment. With MATLAB Parallel Server, you can run computationally intensive MATLAB applications on computer clusters or on Amazon EC2. This approach supports applications that require parallel computing and processing of large data sets stored in distributed arrays. Because MATLAB Parallel Server provides licenses for MATLAB toolboxes, you can execute MATLAB applications on a cluster without acquiring product-specific licenses for each computer.
The typical workflow is as follows: Starting with Parallel Computing Toolbox™, prepare the code for parallel execution using
parfor or jobs and tasks. Once the code is running in parallel as expected—excluding performance considerations—to prepare the code to run on the MATLAB Parallel Server cluster you do not need to make any code changes; it is merely a question of using another configuration.
Generating C/C++ Code
MATLAB Coder can be used to generate self-contained, deployable C/C++ code. The generated ANSI/ISO compliant C and C++ code does not depend on static or dynamic libraries or run-time environments provided by MathWorks; it can be used on any platform that provides a suitable C/C++ compiler. This approach is recommended when the target deployment platform is not supported by MATLAB—such platforms include embedded systems and platforms such as AIX or IRIX. For these platforms, you can generate C/C++ code that is integrated into a larger application or invoked as a shared library.
A principal drawback of this approach is that MATLAB Coder supports a subset of core MATLAB language features—including program control constructs, functions, and matrix operations—but not all functionality that MATLAB and related toolboxes provide. See MATLAB Language Features Supported for C/C++ Code Generation.
Deploying with MATLAB Compiler
MATLAB Compiler enables you to deploy MATLAB programs as standalone applications or as shared libraries (.lib or .dll files). Applications and libraries created using MATLAB Compiler rely on the MATLAB Runtime, which enables users to run compiled MATLAB applications without installing MATLAB. MATLAB Runtime can be packaged and installed with your application or downloaded during the installation process. Because you can freely distribute MATLAB Runtime, MATLAB Compiler enables you to run multiple instances of your code without acquiring additional licenses.
Code produced by MATLAB Compiler and processed by MATLAB Runtime is an intermediate (encrypted) representation, comparable to .class files in Java®—it is not machine code. MATLAB Runtime interprets this intermediate representation using the same engine that MATLAB uses to process standard MATLAB scripts and commands.
You can use MATLAB Compiler to deploy MATLAB code as a standalone Windows application, a shared library, an Excel® add-in, a .NET assembly, or a generic COM component, among other options (Figure 1).
Creating a Standalone Application
The simplest way to deploy code with MATLAB Compiler is to generate a standalone application. It is possible to generate an application with a built-in interface (front end) or one that is intended for use solely as a processing engine. Standalone applications intended for use as processing engines are similar to uncompiled MATLAB applications used in batch processing in that they are invoked by a front-end process that may pass parameters via the command line.
The advantage of this approach is that, apart from the MATLAB Compiler license, no additional licenses are required—you can distribute the application and run it on systems that do not have MATLAB installed. One limitation of this approach is that it is more difficult to debug compiled applications than MATLAB scripts. Input parameters provided to the MATLAB code must be passed as strings, which makes it difficult to exchange complex data sets between the front end and back end. For large or structured data, it is best to use a file-based data exchange mechanism based on XML, comma-separated values (CSV), or MATLAB files.
To start the compilation process, you invoke the Application Compiler from the Apps tab in MATLAB. The compiler automatically detects dependencies on other MATLAB functions and adds them during the compilation process. You only need to add dependencies not detected by the compiler, such as data files or images.
Creating a Shared Library
You can also use MATLAB Compiler to create a C or C++ shared library (for example, a .lib or .dll file). This enables an in-process approach in which the library and the code that invokes it are part of a single operating system process. In contrast, the previous section described an out-of-process approach involving two separate operating system processes. To use the shared library approach, your front end must be able to call into an external library using a C wrapper interface.
The advantage of calling a function in a shared library instead of invoking a standalone executable is that passing parameters is much easier, especially when the data sets are structured or large. Using the MATLAB C API, you can structure the data as a matrix, cell array, or structured array, among other data types. To access the results, the calling application extracts native C data from the structures provided by the API.
Creating a Java Component for an Application Server
When you deploy your MATLAB program as a web application or as an application server component, authorized users can access it via a web browser. A principal advantage of this approach is that it obviates the need to develop and maintain client software and install that software on each user’s computer. MATLAB Compiler SDK™ work with MATLAB Compiler to create software components that can be deployed to application servers and as web applications.
Follow these steps to deploy a MATLAB application to a Java-based application server:
- Compile the MATLAB application into a Java ARchive (JAR) or Web application ARchive (WAR) file using MATLAB Compiler SDK. MATLAB Compiler SDK generates a wrapper Java class that calls the compiled code.
- Install the Java class and the MATLAB Runtime on the application server.
Creating a .NET Assembly for an Application Server
The Microsoft analog to Java-based application servers is Internet Information Services (IIS), formerly Internet Information Server. You can use MATLAB Compiler SDK to generate a .NET or COM component from your MATLAB code, and then deploy IIS for use with ASP.NET applications. The components you generate can be invoked from any language that complies with the Common Language Specification, such as C#, VB.NET, and ASP.NET, as well as any software technology capable of accessing a COM component, including Visual Basic®, ASP, and Excel.
The workflow for Web deployment is similar to the workflow for application server deployment described for both Java and .NET. You use MATLAB Compiler SDK to compile your MATLAB code into a Java or .NET component, which then is invoked by the web server. You can use the Web Figures interface to display interactive graphics on the web page without installing any client-side software or plug-ins.
MATLAB Production Server
Compiled MATLAB code running on an application server can reliably and quickly process requests as long as the number of concurrent requests remains low. For large numbers of concurrent requests, however, performance can become an issue for an application server running compiled MATLAB code. At RWE, for example, developers used MATLAB Compiler SDK and MATLAB Compiler to build a Java component for an automatic natural gas pricing and risk management system. To enable the system to handle many concurrent requests, the developers added a layer between the application server and the compiled MATLAB code. In this layer, a system of redundant RMI servers concurrently serves the main application server in parallel. However, implementation of such a layer is a non-trivial task.
To handle many concurrent requests without developing your own middleware solution, you can use MATLAB Production Server™. Simple to administer, MATLAB Production Server is both scalable with respect to concurrent requests and fast (with low latency).
Scalability is achieved by having multiple MATLAB Runtime instances (or “workers”) running and ready to respond to requests from the server. For added reliability and scalability, multiple instances of MATLAB Production Server can be combined. The workers are shared among instances to maximize performance and minimize response time. A load balance responsible for accessing the different MATLAB Production Server instances (Figure 2).
MATLAB Production Server and Microsoft SQL Server
In addition to the relatively straightforward case of using MATLAB Production Server as a computational back end for an enterprise application, there is a special situation in which the client system is a relational database—specifically, Microsoft® SQL Server®.
Within stored procedures, Microsoft SQL Server is capable of calling external code using the .NET framework. In doing so, the database may fill columns within a table by calling an external .NET component. Using the .NET client API, such a component may call into the MATLAB Production Server synchronously to retrieve computed values (other columns of the table may be used as input to the computations).
For this setup, when rows are added to the table, only those columns that contain the necessary data are filled. Others columns are filled by the MATLAB based back end.
Selecting the Right Strategy
As was pointed out at the beginning of this article, no single deployment strategy fits every possible scenario. Begin by selecting a strategy that fulfills your highest priority requirements. You can then develop a more detailed solution that addresses all your organization’s needs.