MATLAB Answers

5

Best practices for mex coding

Asked by Jim Hokanson on 26 Aug 2013
Latest activity Edited by Matt J
on 25 Aug 2017
Accepted Answer by Jan
Hi all,
As I'm starting to include mex code in my code library I find myself unaware of what some best practices may be when developing mex code. For example, how do you organize make scripts? How much interdependency do you allow between given pieces of code given the difficulty in organizing code dependencies. How much difficulty do you encounter in providing cross-system support for mex files? How do you avoid these problems? Within os, how much variability is observed due to other non-os issues such that someone needs to recompile code specifically for their system instead of distributing a singular dll (e.g. .mexw64 file)? How do you handle versioning or distribution of other dynamic or static libraries? How do you like to handle class methods (or significant but related variations in a type of function) given that mex functions are designed around a singular method interface. Do you have any documentation tips you've found useful? Do you shadow your files with m-file implementations?
These questions may be confusing, and I'm sure I am not asking other questions that might be relevant, but the basic question is how to use mex files in a code base without having them be, for lack of a better word, awkward.
Thanks, Jim

  4 Comments

Show 1 older comment
True. I guess I was hoping people might share there best little bit of advice. Thanks for your answer.
Jan
on 8 Sep 2013
Currently your question got 3 votes and only about 110 questions have more votes. I assume posting the different questions in multiple threads might be a very good idea. E.g. I've struggled a lot with compiling MEX files on one platform with different compilers (Borland, OpenWatcom, LCC, MSVC), so asking for cross-system support of MEX files is a very wide field.
Thanks Jan. I was hoping to have one location where people could document different issues that they've come across (sort of like the What's Missing from Matlab question). Perhaps this weekend I'll get some time to make separate questions.

Sign in to comment.

1 Answer

Jan
Answer by Jan
on 27 Aug 2013
Edited by Jan
on 27 Aug 2013
 Accepted Answer

Just some related ideas:
M-file implementations do not shadow the compiled MEX, because the compiled MEX files are preferred. So you can provide M-files to keep the help text of the Mex files, such that help will still be useful.
The code dependencies between MEX files do not differ significantly from M-files, so a general approach for the complete source code is required. A versioning system is fundamental here.
I've written a kind of make procedure as M-file, which sets some macros for the compilation, checks the fingerprints of the files and compiles changes files afterwards.
Unit-tests are required for all function, which compare the results with a list of known answers and checks if wrong input leads to the wanted interruption by an error. Such unit-testing can reveal incompatibilities between Matlab versions and dependencies to the OS and compiler. Providing compiled files is reliable and convenient, but allowing the users to rung their own compiler is useful also.

  0 Comments

Sign in to comment.