Does Matlab Coder produces single threaded C applications only? - c

Is Matlab Coder only able to produce single threaded applications?
I attempted to convert some Matlab scripts, used for image analysis, and found that the code produced by Matlab Coder was much slower. This confused me because I thought the produced C code would be at-least as fast or faster then the M code. I then checked how many threads were being used for both the M code and the produced C code. The result was 1 thread used by the C code and there were many threads being used by the Matlab code. At this point I can only assume the image processing toolkit implements its function as mex functions which are multi-threaded.

While in general the generated code can be expected to be faster, there are some exceptions. Some implementations which are used by matlab are not available for generated code. I have no reference about the technical background, but I assume that these are fortran and/or assembler written libraries. An example for such a function is eig which is known to produce different (correct) results in generated code.
The matlab coder comes with a code examples which explains how a parfor is translated to openmp code. As a first step make sure that your code contains the openmp relevant pragmas. If not try rewriting your code using a parfor loop.
In a last step, make sure that your compiler is configured to use openmp.

Related

Is there any difference between a mex file and a function called with coder.ceval?

related
My goal is to use a mix of C code and Matlab code, and ultimately have the whole thing run in C by using the Coder tool.
I've found 2 ways of incorporating C into Matlab, writing a Mex file, and using coder.ceval on a C program.
Is there any difference in these 2 methods, beyond just calling syntax?
I'll compare creating a hand-written MEX file against using MATLAB Coder to integrate custom C code using coder.ceval.
Similarities
In both cases, a MEX file can be produced that you can call in MATLAB like any other MATLAB function. A hand-written MEX function will be compiled using the mex command from C source code you write. With MATLAB Coder the MEX file will be automatically generated from MATLAB code that calls your C code via coder.ceval using either the codegen command or the MATLAB Coder App.
Some Basic Differences
(note I use C throughout, but C++ can be used for MEX files as well)
When writing a MEX file, it is necessary to manually move your data between mxArray values and native C types. You'll need to use the MEX library and the C/C++ Matrix Library to do this. If coder.ceval is used, a MEX file can be auto-generated from your MATLAB code that does this data marshalling for you.
A single hand-written MEX file can be made to work with a variety of MATLAB data types. MATLAB Coder requires the type, size (arrays can also be made variable-size), and complexity of each argument to be declared. For example, if you want a MEX file that takes double and single values for a given input, then one MEX file must be generated for each input type.
With a handwritten MEX file, once the data is retrieved from the mxArray values provided by MATLAB, arbitrary C code can be written to manipulate it. coder.ceval requires that you write MATLAB Coder compatible MATLAB code to call the C functions using the external code interfaces it provides. For functions with simple interfaces, e.g. those taking numeric arrays, strings, etc., this can be simple. For those that take other datatypes, more advanced tools like coder.opaque, coder.cstructname and custom enumeration definitions must be used which can take time. One needs to weigh the cost of developing this interface for MATLAB Coder versus learning and using the MATLAB libraries mentioned in the first bullet.
If you eventually want to use the code in C outside of MATLAB, with MATLAB Coder and coder.ceval, the target can simply be changed from MEX to a standalone target like a static or dynamic library or executable. With a handwritten MEX file, one typically factors the C code so that the MEX interface, mexFunction, is separate from the C functional kernel. Then, this kernel can be called outside of MEX. If you are planning to use MATLAB Coder anyway, you'll have to integrate the MATLAB Coder code with this kernel somehow.
If the code is to be used with MATLAB Coder eventually, calling MEX files using Coder when the target is MEX requires using coder.extrinsic. They also cannot be called directly in standalone targets. Instead, the C computational kernel underlying the MEX file needs to be integrated with the generated code either during code generation using coder.ceval or after code generating using a traditional C development environment.
Factors to Consider When Deciding
Do the benefits of integrating the C code early using MATLAB Coder and having the MEX interface auto-generated outweigh the work required to use the MATLAB Coder external code interfaces versus a hand-written MEX file?
Is integrating the external C code using coder.ceval easier or harder than writing a MEX file that exposes it to MATLAB and then later integrating your MATLAB Coder generated code with the computational kernel underlying your MEX file?

Mex C profiler Mac

I'm looking for a way to do very simple profiling in a mex program triggered from matlab. I compile from matlab using:
mex -O CFLAGS="\$CFLAGS -std=c99" rrt.c and then run my program.
Really all I need is a thing to see, which of two functions runs faster. However since it all goes down in about 1/100s time(NULL) is not fast enough.
Is there a simple function in C I could call, or are there any real profiling methods for a mex program in matlab?
I saw this post beeing treated as duplicate, but what I want to know is a way to profile the C code compiled with gcc in matlab, or easier some timing functions.
I use OSX 10.7.5 and matlab 2014b. Thanks for any hints.
Edit: Actually chappjc's hint got me looking for clock(), which does, what I need for the time beeing. An actual profiling would still be nice though.
The reason not to use tic/toc or similar is, that I have a base and a modified code, which both run with random samples. Compiling 2 versions of basically the same code each time I change something and having the extra step of exporting/importing the seed for the random number generator seems like a big hustle for exactly no value to me. I write code such that I don't have to repeat myselft. Having two seperate functions would need quite some duplicate code, since the changes are easy and a few, but deeply integrated in not just one spot.

Call MATLAB function from a C program [duplicate]

I have some code that plots triangles in MATLAB.
I need to be able to somehow execute this code from my C program which generates these points.
Is that possible? How can it be done?
Just a thought:
Can I somehow embed MATLAB code in C, so that it can compile on a C compiler?
The Mathworks site has full details; a demo video of calling the Matlab engine from C, and also the Matlab to C Compiler.
As mentioned previously by answerers, you can call a live copy of MATLAB from C via the MATLAB Engine interface.
If the end-product needs to be used where there is no live copy of MATLAB, you can deploy the application using MATLAB Compiler. However, MATLAB Compiler does not, as another answer has suggested, convert MATLAB programs into C code (and hasn't done for a few versions now). Instead, it archives and encrypts your MATLAB program, and packages it into an executable or shared library that executes against the MATLAB Compiler Runtime (shareable royalty-free). The executable or shared library can then be called from C.
Alternatively you could go the other way around, and call your C code from MATLAB, using either loadlibrary or MATLAB's MEX interface.
Update: As of release R2011a, you can also use MATLAB Coder to generate C code directly from a subset of the MATLAB language.
Look at this presentation about integrating MATLAB Algorithms in C or C++ Applications http://www.mathworks.com/videos/integrating-matlab-algorithms-in-c-or-c-applications-86431.html

Calling MPI from R to run C code

I have an R function which is essentially acting as a wrapper to a set of C functions - the R code calls the C code through .C("..."). This C code could be parallelized and compiled using some MPI implementation. However, having never used MPI before, I have no idea if such MPI-ed code would even be callable from R in a way that would let MPI work?
Does anyone have any experience with this kind of thing? I'm guessing the R MPI libraries are pointless for my purpose, given all the work is done deep within the C code. This would ultimate be run on HPC cluster, if that makes any difference?
Can you use mpicc to create a shared objected, and if such a shared object was called from R would a parallel implementation run, or would you just get the serial version (or indeed, as I suspect, would it just crash)?
I may well be missing info needed to understand the problem, so will update accordingly.
You seem confused.
You could just use the existing Rmpi package to spawn parallel execution of several R instances (on your different nodes) and each of those can use your .C()-called code as well.
Maybe you want to work through some simple examples to get a better feel about what can or cannot be done?

Interfacing MATLAB with C/C++ programs

Hi I wanted to know how to use MATLAB as an external solver from a C program. Specifically in my code I wish
to solve several linear systems of the form Ax=b.
I have heard that to go the other way namely Calling C functions in a MATLAB routine one uses MEX files.But I am not really sure how to use Mex files either.
Thank you
Actually, MEX files allow you to include C code in Matlab programs, for example if you want to use external C libraries in Matlab.
What you want to do is use the Matlab Engine:
http://www.mathworks.com/help/techdoc/matlab_external/f29148.html
As an alternative, you could use linear algebra libraries that are written purely in C, such as LAPACK and BLAS. ( www.netlib.org )
you can use the matlab engine as Lagerbaer points out. However sometimes it can be convenient just calling the matlab process commandline style. I use this often when I don't want to mess with mxArrays etc, or when the amount of matlab code that needs executing gets really large. PseudoCode:
WriteArrayInMFormat( "in.m", myInputNumbers );
LaunchProcess( "matlab", "-nodesktop -r \"myFunction( 'in.m' )\" -logfile out.m" );
ReadArrayInMFormat( "out.m", myResult );
For me this is especially useful when testing things out: instead of having to recompile the C/C++ program each time I change something, I just apply all changes in the myFunction.m file. At that point I don't even need the C program, instead everything can be tested in matlab.

Resources