Calling C/C++ Code as MEX File vs. Pure C/C++ - c++

I personally love high level programming languages. For proof-of-concept stuff, MATLAB is great. Plus you can easily visualize almost anything with MATLAB.
However, I often need to write C or C++ code for the sake of speed. Visualizations in C/C++ are a pain in the neck though. In an ideal world I want MATLAB visualization tools at C/C++ speeds. For me that implies I should MEX the necessary C/C++ functions and just call them from a MATLAB script, using MATLAB's tools to perform the visualizations. Ideally this gives me the best of both worlds. However, I don't want to end up with slow C/C++ run times as a result of calling the function via MEX files.
Do I sacrifice a the 10x-100x speed gains of C++ when calling C/C++ functions as compiled MEX functions? That is, does mexFunction(param1, param2) as called from a MATLAB script necessarily run slower than running the compiled binary?

I think to answer this question you must think about what really causes the overhead. Each function call to mex itself causes an overhead and further passing the data to mex (to my experience only that direction, not passing back the result) also causes some overhead. I assume the primary reason is that M-Code is copy-on-write optimized which means my code never copied the input data, but the mex implementation does receive a copy.
To give an example where mex behaved "bad", I think we all agree that C++ is faster iterating and that mathworks probably has qualified programmers, so why was I able to beat the performance implementing binary search in MATLAB?. In this case passing the data to the mex function simply made it slow. A lot of data was passed for which you had to pay the overhead and finally the data was barely touched (binary search).
Finally, how large is the overhead really?
For a nop call, it is only 0.00001s, (No input, no output, no calculation).
For passing data, I don't have any detailed benchmark, but from the binary search example I linked above it must be somewhere below 0.5s/GB.
Now do the math for your case and decide if it is worth switching to c++.

Related

C++ and Matlab combination

I am writing a simulation of some differential equation. My idea was the following:
1. Write the core simulation (moving forward in time, takes a lot of time) in C++
2. Do initialisation and the analysis of the results with a program
like Matlab/Scilab
The reason for (1) is that C++ is faster if implemented correctly.
The reason for (2) is that for me it is easier to make analysis, like plotting etc..., with a program like Matlab.
Is it possible to do it like this, how do I call C++ from Matlab?
Or do you have some suggestions to do it in a different way?
You could certainly do as you suggest. But I suggest instead that you start by developing your entire solution in Matlab and only then, if its performance is genuinely holding your work back, consider translating key elements into C++. This will optimise the use of your time, possibly at the cost of your computer's time. But a computer is a modern donkey without a humane society to intervene when you flog it to death.
As you suggest, well written C++ can be expected to be faster than interpreted Matlab. But ask yourself What is Matlab written in ? For much of its computationally-intensive core functionality Matlab calls libraries written in C++ (or whatever). Your task would be not to write code faster than interpreted Matlab, but faster than C++ (or whatever) written by specialists urged on by a huge market of installed software.
Yes, Matlab has a C/C++ API.
This API permits to:
Write C++ functions which can be invoked from Matlab
Read/Write data from a .mat file
Invoke the Matlab engine from C++
I am working to something similar to what you are trying to do, my approach is:
Import in C++ the input data from a .mat file
Run the simulation
Export the results back in a .mat file
The Matlab API is in C, and I suggest you to write a C++ wrapper for your convenience.
In order to work with Matlab mxArray, I suggest to take a look at the boost::multi_array library.
In particular you can initialize an object of type multi_array_ref from a Matlab mxArray like this:
boost::multi_array_ref<double,2> vec ( mxGetPr (p), boost::extents[10][10], boost::fortran_storage_order() );
This approach made the code much more readable.
You can call your own C, C++, or Fortran subroutines from the MATLAB command line as if they were built-in functions. These programs, called binary MEX-files, are dynamically-linked subroutines that the MATLAB interpreter loads and executes.
You should set compiler, look here Setting up mex to use the Visual Studio 2010 compiler.
All about MEX-files here: http://www.mathworks.com/help/matlab/matlab_external/using-mex-files-to-call-c-c-and-fortran-programs.html.

Calling an executable function in matlab, slower than native matlab code?

I have an executable function that I compiled which takes an input and spits out some outputs when I run it in a terminal. The function is written in C++, and I know that its supposed to be much faster than matlab. But what if my matlab code is using this external function like it's own, would the fact that its externally called make it slower in any way?
I'm trying to debate if there is any point in me writing a matlab version of this function, when I could just execute it with system() in matlab.
It depends... You have to try it and test the performance.
A better approach would be to write your code as a MEX-function. This gives you the best of both worlds (think of MEX-files as DLL's): C code performance, with a lower overhead to call than an external process.
Since you already implemented your program in C\C++, you should be able to wrap as MEX-function with little effort..

tool for finding which functions can ultimately cause a call to a (list of) low level functions

I have a very large C++ program where certain low level functions should only be called from certain contexts or while taking specific precautions. I am looking for a tool that shows me which of these low-level functions are called by much higher level functions. I would prefer this to be visible in the IDE with some drop down or labeling, possibly in annotated source output, but any easier method than manually searching the call-graph will help.
This is a problem of static analysis and I'm not helped by a profiler.
I am mostly working on mac, linux is OK, and if something is only available on windows then I can live with that.
Update
Just having the call-graph does not make it that much quicker to answer the question, "does foo() potentially cause a call to x() y() or z()". (or I'm missing something about the call-graph tools, perhaps I need to write a program that traverses it to get a solution?)
There exists Clang Static Analyzer which uses LLVM which should also be present on OS X. Actually i'm of the opinion that this is integrated in Xcode. Anyway, there exists a GUI.
Furthermore there are several LLVM passes, where you can generate call graphs, but i'm not sure if this is what you want.
The tool Scientific Toolworks "Understand" tool is supposed to be able to produce call graphs for C and C++.
Doxygen also supposedly produces call graphs.
I don't have any experience with either of these, but some harsh opinions. You need to keep in mind that I'm a vendor of another tool, so take this opinion with a big grain of salt.
I have experience building reasonably accurate call graphs for massive C systems (25 million lines) with 250,000 functions.
One issue I encounter in building a realistic call graph are indirect function calls, and for C++, overloaded method function calls. In big systems, there are a lot of both of these. To determine what gets called when FOO gets invoked, your tool has to have to deep semantic understanding of how the compiler/language resolves an overloaded call, and for indirect function calls, a reasonably precise determination of what a function pointer might actually point-to in a big system. If you don't get these reasonably right, your call graph will contain a lot of false positives (e.g., bogus claims of A calls B), and on scale false positives are a disaster.
For C++, you must have what amounts to the full compiler front end. Neither Understand or Doxygen have this, so I don't see how they can actually understand C++'s overloading/Koenig lookup rules. Neither Understand or Doxygen make any attempt that I know of to reason about indirect function calls.
Our DMS Software Reengineering Toolkit does build calls graphs for C reasonably well, even with indirect function pointers, using a C-language precise front end.
We have C++ language precise front end, and it does the overload resolution correctly (to the extent the C++ committee agrees on it, and we understand what they said, and what the individual compilers do [they don't always agree]), and we have something like Doxygen that shows this information. We don't presently have function pointer analysis for C++ but we are working on it (we have full control flow graphs within methods and that's a big step).
I understand CLANG has some option for computing call graphs, and I'd expect that to be accurate on overloads since Clang is essentially a C++ compiler implemented with a bunch of components. I don't know what, if anything Clang does to analyze function pointers.

Matlab to C or C++

I am working on an image processing project using Matlab. We should run our program (intended to be an application) on a cell phone.We were then asked to convert our code into C or C++ language so we get a feel of how long it would take for execution and then choose a platform. So far we didn't figure out how to do this conversion.. Any ideas of what to do to convert Matlab to C or C++??
The first thing you need to realise is that porting code from one language to another (especially languages as different as Matlab and C++) is generally non-trivial and time-consuming. You need to know both languages well, and you need to have similar facilities available in both. In the case of Matlab and C++, Matlab gives you a lot of stuff that you just won't have available in C++ without using libraries. So the first thing to do is identify which libraries you're going to need to use in C++. (You can write some of the stuff yourself, but you'll be there a long time if you write all of it yourself.)
If you're doing image processing, I highly recommend looking into something like ITK at http://www.itk.org -- I've written my image processing software twice in C++, once without ITK (coding everything myself) and once with, and the version that used ITK was finished faster, performed better and was ten times more fun to work on. FWIW.
Matlab can gererate C code for you.
See:
http://www.mathworks.com/products/featured/embeddedmatlab/
The generated code does however depend on matlab libraries. So you probably can't use it for a cell phone. But it might save you some time anyways.
I also used the MATLAB Coder to convert some functions consisting of a few hundred lines of MATLAB into C. This included using MATLAB's eigenvalue solver and matrix inversion functions.
Although Coder was able to produce C code (which theoretically was identical), it was very convoluted, bloated, impossible to decipher, and appeared to be extremely inefficient. It literally created about 10x as many lines of code as it should have needed. I ended up converting it all by hand so that I would actually be able to comprehend the C code later and make further changes/updates. This task however, can be very tedious/dangerous, as the array indexing in Matlab is 1-based and in C it's 0-based. You're likely to add bugs into the code, as I experienced. you'll also have to convert any vector/matrix arithmetic into loops that handle scalars (or use some type of C matrix algebra package)
The MathWorks provides a product called MATLAB Coder that claims to generate "readable and portable C and C++ code from MATLABĀ® code". I haven't tried it myself, so I can't comment on how well it accomplishes these goals.
With regard to the Image Processing Toolbox, this list (presumably for R2016b) shows which functions have been enabled for code generation and any limitations they may have.
Matlab has a tool called "Matlab Coder" which can convert your matlab file to C code or mex file. My code is relatively simple so it works fine. Speed up gain is about 10 times faster. This saves me time coding a few hundreds lines. Hope it's helpful for you too
Quick Start Guide for MATLAB Coder Confirmation
The links describe the process of converting your code in 3 major steps:
First you need to make a few simplifications in your present code so that it would be simple enough for the Coder to translate.
Second, you will use the tool to generate a mex file and test if everything is actually working.
Finally you would change some setting and generate the C code. In my case, the C code has about 700 lines including all the original matlab code (about 150 lines) as comments. I think it's quite readable and could be improve upon. However, I already get a 10 times speed up gain from the mex file anyway. So this is definitely a good thing.
We can't not be sure that this will work in all case but it's definitely worth trying.
I remember there is a tool to export m-files as c(++)-files. But I could never get that running. You need to add some obscure MATLAB-headers in the c/c++code, ... And I think it is also not recommended.
If you have running MATLAB-code, it shouldn't take too much effort to do the conversion "by hand". I have been working on several project where MATLAB was used and it was never consider to use any tools to convert the code to C/C++. It was always done "by hand".
I believe to have been the only one who ever investigate into using a tool.
Well there is not straight conversion from matlab to c/c++ You will need to understand the language and the differences between matlab and c/c++ and then start coding it in c/c++. Code a little test a little until it works.

Performance considerations when mixing C++ and Objective-C in same file

I have some high-performance C++ that I need to interface with Objective-C, is there any performance penalty to just dumping this code into a .mm file that contains my Objective-C code vs. leaving this code in a separate .cpp file and extern-ing all the functions I need to call from the .mm file?
There are a handful of issues here.
(1) if your C++ engine code is running in isolation -- if the Objective-C is acting as the front-end that triggers the underlying engine -- then there is no penalty at all. The C++ bits in ObjC++ compile just like regular C++.
(2) If you are calling into Objective-C from within the calculation engine, then you might have a performance issue on your hands. There is overhead in calling an Objective-C method -- objc_msgSend() isn't free (but close to it) -- but generally not enough to be a problem in comparison to, say, a function call. However, in highly optimized C++, the compiler there may be optimizations that eliminate, even, function call overhead (it gets complex) to a large extent. An Objective-C method call cannot be inlined or optimized away.
(3) If you haven't measured it and discovered a performance problem, don't worry about it...
At first glance it seems there would be no performance hit, but really, unless you have intimate knowledge of what the compiler and runtime is doing internally especially during optimization then you will never know until you try it.
So give it a shot and profile it and find out if there is a performance hit.
No penalty, it's the same compiler processing the C++ portion regardless of which file it is in.
I suspect there may even be some gains if you don't have to wrap your C++ code in "extern C" functions.
Objective-C compliles down to to assembly, just like C++, I couldn't see how you would take a perf hit.