Using C/C++ under Matlab (Part 1)

Author

Benoit Guilleminot

Keywords

Matlab, C, C++, Mex Files

Review Status

General review by COMISEF Wiki Admin, 03/2009

1. Tutorial Overview

This is the first of five Tutorials that describe how to optimise Matlab programs using C/C++ functions. For an exhaustive look at optimisating Matlab programs see Matlab's Help Guide. The objective of this Tutorial is to simply help you get started with MEX Files with practical examples with a finance theme. In this first Tutorial we give an overview to optimising code using C/C++, and look at some of the reason for and against doing this.

The Titles and links to all Tutorials are listed below.

  1. An Overview of using C/C++ under Matlab
  2. Creating a project under Visual C++ Express 2008
  3. Basic Principles
  4. Advanced Principles
  5. Conclusion

2. Introduction

Matlab is known to be computationally efficient, especially when dealing with problems structured as matrices. However there may be some situations where using matrices is not recommended (where it is a unacceptable burden on memory resources), or even impossible (owing to extreme dimensionality of the problem).

One solution is to go for some compiled language. (As opposed tp Matlab, which is interpreted language.) Performance may improve drastically by using compiled functions directly under Matlab. As a matter of fact a lot of default built-in Matlab functions are compiled to improve performance.

3. Advantages of compiled languages

Some Advantages of using compiled languages under Matlab include:

  • Compiled functions (improved efficiency)
  • Low-level control of memory
  • Control of multi-thread coding
  • Interaction with Matlab (it is possible to call standard m routines in your C/C++ code, but it is quite slow)
  • Extreme flexibility in the fact that it is possible to use either Matlab features or C/C++ features
  • Control of I/O
  • Possibility to use external libraries

4. The Downside

The flexibility offered by compiled languages may be very powerful when used carefully. However all the difficulties following naturally from using complex languages arise and therefore:

  • Do not go down the C/C++ route unless you understand how to use it properly.

What is better? Using exisiting Matlab code that takes 10 days to run but outputs the expected answer. Or spend 10 full days writing and debugging C/C++ code to get an incorrect answer.

  • Do not try to optimize if it is not worth it.

As already mentioned, only use C/C++ unless if it is really necessary. For example, when there is a clear evidence after some diagnostic tests that it will alleviates/solves a performance bottneck. Remember that Matlab is very efficient in most cases, especially for applying matrix operations. All aspects of programming are usually already optimized (like the number of functions calls, memory allocation […]), so do attempt to optimize Matlab functions unless you are pretty confident that you can do better.

  • C/C++ will not make your algorithm better.

NP-complex problems under Matlab are still NP-complex under C/C++, and usually it is even more like NP+ bug.

  • Do not use C/C++ if you are only interested in relative performance.

5. Recommendation

In my opinion a safe way to use C/C++ functions is to focus on specific performance bottlenecks in your code. As a simple recipe:

  1. First implement your algorithm in Matlab M files
  2. Check that outputs are correct (or at least coherent) before introducing some other potential sources of errors.
  3. Use tic and toc Matlab functions (or any profiler you like) to analyse where exactly your implementation is inefficient. This will really help you to understand if compiled methods are required.
  4. Rewrite the small pieces of slow code in C/C++.

Sometimes, you may have to rewrite all your code,but remember that a lot of libraries (like Boost) can help you in your transition to C/C++.

Internal Links

Concepts
Tutorials
Tips
Related Articles
  1. Creating a project under Visual C++ Express 2008
  2. Basic Principles
  3. Advanced Principles
  4. Conclusion

External links

References
Weblinks

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License