-
Research - MATLAB/C-MEX
Oct. 3 , 2013 | Posted by Suayb S, Arslan
-
Fundamentals about MEX files:
This page is in essense maintained to serve developersin an attempt to assist on using/compiling mex files for MATLAB as if they are written matlab function i.e., higher level m-files. Engineers of Mathworks develop optimized mex files to write matlab functions.
-
Matlab Codes in *.m files becomes slower as if we use more loops in our code. Vectorization is one option to speed up the run. C/C++ languages are actually compile the code thus are much more effective in handling loops in the code. C-MEX on the other hand is a C based matlab code compiled by Matlab itself to accelarate Matlab *.m codes.
In what follows I drop some *.m files and C-MEX files for general communication and coding theory. The letter is way faster than their *.m correspondances -
How to compile *.mex files? - OS: Windows platform.
- First you need to set up your compiler in Matlab command line: mex -setup.
-
mex -setup
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Digital Visual Fortran version 6.0 in C:\Program Files\Microsoft Visual Studio
[2] Lcc C version 2.4 in C:\MATLAB7\sys\lcc
[3] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio
[0] None
Compiler: 3
Please verify your choices:
Compiler: Microsoft Visual C/C++ 6.0
Location: C:\Program Files\Microsoft Visual Studio
Are these correct?([y]/n): y
Try to update options file: C:\Users\Shapo\Application Data\MathWorks\MATLAB\R14\mexopts.bat
From template: C:\MATLAB7\BIN\WIN32\mexopts\msvc60opts.bat
Done . . .You also can have more info using help mex option. Then, you can simply call Matlab compiler to compile mex files. The final output is corresponding *.dll file for OS Windows. Then it is going to be ready for use as if it is written as an m file:
>> mex deneme.cOn the other hand, probably the most popular question "How to easily convert *.m files to *.mex files?" has an answer from my side. The command mcc built on Matlab developpers does not help you at all in new versions. Although there are arguments on net that mcc –x deneme.c will work out the conversion is far from representing the truth. Although stand alone *.exe files are possible with mex -m, mex -x is no longer supported by new versions and does not give you desirable results on old versions. Please use help mcc to have more information on that. All in all, I strongly advice you to start coding in C or C++/C# with #include "mex.h" in your script, then crate your gateway function/s on behalf of main subroutine in a standard C program. Finally compile it using mex command.
More on Mex tutorial click here!Gateway function/s have a special structure and are basically in a known format. You can have some oppinion in your review of the following functions found in this web page.
There are also commercial products sold as one-button conversion tool from Matlab to C or C-MEX files.Agility is one of them. For more info: click here!. So far as I am concerned there are also simulink based MEX file creation procedures, yet I am barely concerned with extra wrapper files which can complicate the process even more. One additional note: If you have problems with already built in Matlab Lcc, then try Borland C/C++ compiler.
Whats the raising trend?
Raising trend for Big data applications is the well known massive parallel processing tools such as CPUs, GPUs, FPGAs scattered across the computing clusters. Regardless of what software is on, this massively parallel hardware architectures call for different software implementations to beneft from clusters' simultaneous processing capabilities. Thus, two important and interesting trends regarding Matlab's MEX code development environment became
1. Multi Threaded Mex Code Development. (Uses CPU cores for parallel implementation. More to follow later.)
2. MATLAB/GPU/C-MEX Code Development. (Uses GPU cores for parallel implementation.)
Preface:
-
Functions/Scripts:
Following function is written to give a glipse of C-MEX structure and relationship to a simple C functions/codes. It also includes "call from a Matlab function" that is written in *.m file format. Other functions are for specific purpose and subject oriented. -
Basic Operations:
toolkit.c
- Includes calling matlab *.m files from a C-MEX content, decimal to binary conversion and bubble sort.cfunctions.
toolkit.dllCRCsimulation
- CRC appendance and generator polynomial evaluation. Also available here!Source Encoding:
Jpeg.m
Spiht - by Mustafa Sakalli at RIT. (web site)
Entropy Encoding: -
Channel Encoding Decoding:
viterbialg.c
- Popular Viterbi Algorithm, compatible with Matlab trellis structure(poly2trellis).
viterbialg.dlllistVAharddecode.m
listviterbi.c
- List Viterbi algorithm. This implements the complex list Viterbi by parallely constructing lists of best paths.
listviterbi.dll
STBC and MIMO related:
alamouti.m
Cooperative Communications: - Amplify and Forward (AF) and Decode and Forward(DF) Protocols
Coding Theory: Zyablov Bound (MTLB code).
Rateless Codes: Optimization of Degree Distribution for LT codes (linear program -MTLB)
Reliability Theory: MEL Algorithm (MTLB code) [3]
Implementations of several journals of mine: Concatenated Coding and GLT for UEP
- References:Notes:
Based on my experience, if the mex code when executed on Matlab command line causes your program to exit: It might be that you put exit(); command somewhere in your source code. The other deadly reason could be because of a memory allocation problem in the code. Inproper memory allocation may cause system mulfunction or damage. The codes found here or quoted in some way does not refrain the user from any damage or malicious outcome in case of misuse. You use it at your own risk.
[1] J. G. Proakis, Digital Communications, 3rd edition, New York: McGraw Hill, 1995.
[2] Shu Lin and Daniel J. Costello Jr., ``Error Control Coding", Second Edition, Prentice Hall,
[3] J. J. Wylie and R. Swaminathan, “Determining fault tolerance of XOR based erasure codes efficiently,” in DSN-2007. IEEE, June 2007, pp. 206–215.