Compiling a MEX file using object files in another folder - matlab

I'm writing some MATLAB code, and I want to use some optimized C routines. I have the C source code, and it works just fine. I've created a MEX file and am capable of compiling it provided that it is in the same folder as the optimized C routines. However, I want to be able to distribute this code to others on various platforms. Since MEX files are binaries, each individual must (likely) recompile on their own machines. That is fine, but I want to make the process as painless as possible.
Currently, if all of the files are in the same directory, then calling something like
mex mexfile.c other1.o other2.o other3.o
from that directory works just fine as you would expect. For organizational purposes, however, I'd like for the C code to be in its own (sub)directory, say code. Unfortunately, if I structure things like this, the mex command errors out (the errors differ based on the different things that I've tried). I've tried things like
mex mexfile.c code/other1.o code/other2.o code/other3.o
and the -Ipathname and -Lfolder options on the mex command, but these haven't worked for me. I would think that there has to be a simple way to do what I'm wanting to do, but I just can't find the appropriate documentation or figure it out myself. Any help would be appreciated.

[OP's solution converted to answer below]
In the setup given above, using the following command works as desired for me:
mex -Icode mexfile.c code/other1.o code/other2.o code/other3.o
I stumbled across this solution while regenerating error messages.
I'm guessing that this is just the appropriate syntax for the -Ipathname option to the mex command, but I can't find documentation to support this conclusion. If anyone else can provide a link to actual documentation, that would certainly be better than a potential solution that just happened to work for me.

I use something along the lines of
emlc -I './somedir/' -o sourcefilemex -T mex sourcefile
By changing the -T argument you can generate mex code or embeddable C. That's handy.

Related

Executing CUDA code In Matlab

I wanted to ask if anyone have run some C code containing CUDA code on Matlab?
I have read the documentation on Mathworks website but I still cant quite wrap my head around it. I do understand that it is two main type of ways you could do this either executing a CUDA kernel by constructing a object with the function parallel.gpu.CUDAKernel or by constructing a mex file out of a .cu file. There are some things though I do not understand when using these two methods.
Using the mex approach should I use another IDE like Visual Studio to compile a .cu file first before compiling the mex file in Matlab? If so how can I compile the .cu file without a main() function in the .cu file, I always get errors when I try to compile it that way in VS, or is it okay to have a main function in the .cu file and pass the pointers to the GPU arrays to the main function?
For the CUDA kernel approach, should compile the kernel in VS, and in that case how?
Both things can work.
If you want to have flexibility, my suggestion is to write your .cu files with .c (or .cpp) files*. Once you have some basic thing working, you should be able to write a mex wrapper around it in order to grab MATLAB variables and convert them co C/C++ so you can pass them to and from CUDA. This requires you to have a compiler that is both compatible with your versions of MATLAB, CUDA and OS. An example is Visual Studio 2013 in widows and most versions of MATLAB and CUDA, but please check. Generally this is done by linking nvcc to the mex compiler after setup with some xml files (see example here from my toolbox). This approach gives you full flexibility, not only for working with CUDA, but also with working with the anything that you may want to use together with your kernels e.g. tensorflow, eigen, SQL, ... Its full flexibility.
If instead you just want a few operations accelerated with simple methods, use the Parallel computing toolbox with gpuarrays for standard MATLAB operations or with parallel.gpu.CUDAKernel for your own kernels. To use this second one you need to compile a ptx file, which seems pretty straightforward. A priori this gives you less flexibility, as it will run just a kernel, but often complex GPU programs may need several kernels and data handling techniques, as well as communication between kernels etc. However I have personally haven't tried it and perhaps you can achieve full flexibility. Let me know and I will edit the answer.
In short, you choice depends on your application/needs.
*You might not need .c or .cpp files with modern versions of MATLAB and mexcuda.

How to make an executable file on matlab and run on raspberry pi 3B [duplicate]

I was wondering if there is a way to create a '.exe' file from ' .m' file in MATLAB, such that it can be run in machine which does not have MATLAB (like it can be done in C, C++).
I know writing a MATLAB function is one way, but I am not sure if it can run in machine without MATLAB.
Also I would like to hide my code and just create a script which can be run by a user using his own data files.
The Matlab Compiler is the standard way to do this. mcc is the command. The Matlab Runtime is required to run the programs; I'm not sure if it can be directly integrated with the executable or not.
If you have MATLAB Compiler installed, there's a GUI option for compiling. Try entering
deploytool
in the command line. Mathworks does a pretty good job documenting how to use it in this video tutorial: http://www.mathworks.com/products/demos/compiler/deploytool/index.html
Also, if you want to include user input such as choosing a file or directory, look into
uigetfile % or uigetdir if you need every file in a directory
for use in conjunction with
guide
Try:
mcc -m yourfile
Also see help mcc
If your code is more of a data analysis routine (vs. visualization / GUI), try GNU Octave. It's free and many of its functions are compatible with MATLAB. (Not 100% but maybe 99.5%.)
mcc -?
explains that the syntax to make *.exe (Standalone Application) with *.m is:
mcc -m <matlabFile.m>
For example:
mcc -m file.m
will create file.exe in the curent directory.
It used to be possible to compile Matlab to C with older versions of Matlab. Check out other tools that Matlab comes with.
Newest Matlab code can be exported as a Java's jar or a .Net Dll, etc. You can then write an executable against that library - it will be obfuscated by the way. The users will have to install a freely available Matlab Runtime.
Like others mentioned, mcc / mcc.exe is what you want to convert matlab code to C code.
The "StandAlone" method to compile .m file (or files) requires a set of Matlab published library (.dll) files on a target (non-Matlab) platform to allow execution of the compiler generated .exe.
Check MATLAB main site for their compiler products and their limitations.
I developed a non-matlab software for direct compilation of m-files (TMC Compiler). This is an open-source converter of m-files projects to C. The compiler produces the C code that may be linked with provided open-source run-time library to produce a stand-alone application. The library implements a set of build-in functions; the linear-algebra operations use LAPACK code. It is possible to expand the set of the build-in functions by custom implementation as described in the documentation.

Mex or Compile (mcc) Matlab function that uses toolkits

Environment:
Matlab R2012a (I have access to others if necessary)
All Toolboxes/Compiler installed
Ubuntu 12.04 64bit and/or Windows 7 64bit
I am working with the source for a software package written in Matlab (unfortunately its proprietary so no code examples...sorry), and one function briefly uses the Control System Toolbox and the Signal Processing Toolbox.
I have no problem running the code on my personal computer because I have every toolbox installed, however I would like to compile (mex or mcc) JUST the function using those two toolboxes. The goal, of course, is to run the software on a machine without those toolboxes, while leaving the remaining code open to change.
According to matlab, they have no problem with you compiling code that uses almost any toolbox. Here is the list of toolboxes that support mcc compilation:
http://www.mathworks.com/products/compiler/supported/compiler_support.html
The problem arises in that mcc no longer allows compiling with the -x option to create a mex-ed version of the function, so I'm forced to create a C executable (maybe? hopefully not). This particular function takes large matrices as parameters (impractical to write as a command line argument) and returns a structure of cell arrays.
The only way around this (as I see it now) would be to write the arguments (large matrices) to the hard drive in a binary .mat file , have the compiled C binary read in the arguments, run the algorithm, and finally save the return values in another .mat for the parent thread to load back into memory.
This seems totally impractical. I would greatly appreciate alternative suggestions. Please let me know if anything here in unclear. Thanks in advance!
[Edit 1] The codegen package does not support tf.m. It seems like this should be possible (and used to be possible with the mex -x option), but I'm at a loss. Any suggestions would be greatly appreciated!
I think the reason -x is not supported anymore is the fact that Matlab now has a product called "coder", which converts .m files to .c files and can also create .mex files from "suitable" .m files using the option -args to specify the input arguments: http://www.mathworks.com/videos/generating-c-code-from-matlab-code-68964.html

way to handle to write CUDA+MEX code in linux?

I try to write matlab mex code with Cuda integrated but it is just hard enough to compile and debug all around. Is there any better approach to code and test? I am on Matlab 2012b.
Currently I am writing code in sublime then compile it on matlab but I am also newbie at CUDA as well thus it is just hard to code it without seeing the result instantly.
The comment by Robert Crovella is interesting.
I just wanted to mention the way I was used to compile mex file with CUDA instructions (and which works also on different versions of MATLAB).
1) Compile by nvcc and transform the source code in C++ code by the command
system(sprintf('nvcc -I"%s/extern/include" -cuda "mex-fun.cu" -output-file "mexfun.cpp"', matlabroot));
2) Link it to Matlab by
mex -I/opt/cuda/include -L/opt/cuda/lib -lcudart mex-fun.cpp
This was originally suggested at the MATLAB Newsreader page.
I have both a matlab entry point (i.e. a file with the function "mexFunction") and a C++ entry point (a file with "main"), and the rest of my actual CUDA code is independant of what entry point was used.
This way, I can debug the code used for my MEX files using the standard set of CUDA tools (i.e. nvprof, cuda-memcheck, etc) without having to deal with the MEX file. Then once I'm sure I have no bugs or memory leaks, I just compile it to a MEX file. Alternately you can always just attach cuda-gdb to your MEX file, although your mileage may vary with this.

octave install matlab package

Recently I choose to use Octave instead of Matlab in Ubuntu12.04 due to the fact that it is open sourced. One problem I encounter is that when I try to install some additional package of matlab, it always fails.
For example, I want to install the randomforest matlab package (http://code.google.com/p/randomforest-matlab/), it needs to run command "make" to compile the c++ code etc. But first it tells me there's no mex command. To solve this, I modified the Makefile by replacing "mex" with "mkotfile --mex", after the second run, there are errors associated with the source code of the package.
This happens for multiple times, like I fails to install the libsvm in Octave.
Does anyone know how to solve this? Or I have to return to use Matlab.
Octave and Matlab are different enough to cause situations like these. Things developed for Matlab will usually not work as-is on Octave, especially external packages (i.e., not developed by the Mathworks).
You'll just have to persist -- re-write some parts of the code, install additional libraries here and there, etc. to get it to work.
It's always possible, but whether it's practical, that's for you to decide.