Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Wednesday, May 7, 2014

Michael Hirsch's Blog Post on Compiling Matlab code to run on any PC

Compiling Matlab code to run on any PC

August 8, 2013 at 13:01
The Matlab Compiler allows you to compile your existing .m Matlab script to run as an executable–e.g. on another PC that doesn’t have Matlab installed. Typically you will compile for the same operating system as you have on your Matlab PC (it might be possible to cross-compile, but I have NOT checked this). You can also output code suitable for compilation in another compiler–I’ll leave it to you to read the mcc documentation for now.
There are two caveats:
1) You must have a Matlab Compiler license on the PC you’re using for Matlab–check this by typing in Matlab:
license('test','compiler')
which must return a 1. If it’s 0, you don’t the Compiler license (you or your institution would have to buy a license)
2) The computer where you intend to run the compiled program must have the no-cost Matlab Compiler Runtime (MCR) installed–else you’ll get errors like:
error while loading shared libraries: libmwlaunchermain.so: cannot open shared object file: No such file or directory
Well, that’s the error I get in Linux.
To install the Matlab Compiler Runtime (do this on your Matlab PC as well as obviously the PC where you want to run the compiled Matlab code, go to this website:
http://www.mathworks.com/products/compiler/mcr/
On Linux, just before finishing the installation you will be told to set environment variables LD_LIBRARY_PATH and XAPPLRESDIR with a long string of text. Here’s how to do that
  1. gedit matlabvariables
  2. copy and paste the text into an export command like this:
    ## Matlab compiler Runtime (MCR)
    export LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64:$LD_LIBRARY_PATH
    export XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/X11/app-defaults
  3. chmod +x matlabvariables
Caution: The above .bashrc is only for Matlab R2013a on my PC, maybe your PC is different–cut and paste the text from your installation window.
So before running your MCR program, first run ./matlabvariables, which sets the appropriate variables when you need them on the PC you’re running the MCR code on.
Note: don’t just paste the export statements into .bashrc, because they can interfere with other programs such as nmap–just use them when needed.
————-
Now we write a test program by saving the following test into a file called MyMCC.m
function Y = MyMCC
X = 0:0.01:2*3.14;
Y = sin(X);
plot(X,Y)
title('Test of MCR')
xlabel('x')
ylabel('y')
display('wow I ran an MCR program!')
end

Then type in Matlab console:
mcc -m -v -w enable -R -startmsg -R -completemsg MyMCC.m
When that completes, open Terminal and in your MyMcc.m directory you’ll see an executable file MyMCC.
Type in Terminal ./MyMCC and you’ll see:

./mcctest
Initializing MATLAB Compiler Runtime version 8.1
wow I ran an MCR program!

along with a typical Matlab plot window showing a sine wave. Just close the plot window to end the execution of your program.
Reference:
http://blogs.mathworks.com/loren/2010/11/18/deploying-standalone-applications/

Wednesday, September 25, 2013

MATLAB startup.m

Add these lines to your Matlab startup.m file for some amusement in life:

clc
s = RandStream('mt19937ar','Seed','shuffle');
RandStream.setGlobalStream(s);
disp('Why? Because...')
why

clear all

Friday, November 23, 2012

Concatenation error

Any idea what is wrong here?

>> A = [-1./alpha.^2 -3./2./sig.^2, 1./alpha.^2-1./sig^2, -1./2./sig.^2;... 
1./alpha.^2-1./sig.^2, -2./alpha.^2-1./sig.^2, 1./alpha.^2-1./2./sig.^2;... 
-1./2./sig.^2, 1./alpha.^2-1./2./sig.^2, -1./alpha.^2-1./2./sig.^2];
??? Error using ==> sym.cat>checkDimensions at 76
CAT arguments dimensions are not consistent.

Error in ==> sym.cat>catMany at 39
[resz, ranges] = checkDimensions(sz,dim);

Error in ==> sym.cat at 29
    y = catMany(dim, strs);

Error in ==> sym.vertcat at 26
    y = cat(1,args{:});


Turns out there is an innocuous looking white space on the first line before the -3. Next time be careful when adding extra spaces to improve readability.

Monday, February 6, 2012

*Must* use parfor!

I was recently implementing a Monte Carlo simulation with 50000 particles for estimating a probability density function in which took me 1100 seconds of runtime. Using Matlab's profiling tool, I found that 99% of the time was being spent on generating the new set of 50000 sample points from the old sample points. I immediately switched to a parallel looping strategy with 8 workers in parallel. The new runtime dropped to 80 seconds. Next, I found that I was calling a ``coinflip'' function in every loop. I removed it from the loop and instead stored a sequence of coinflips in memory using Matlab's binornd function and simply accessed this vector of ``already flipped coins'' in the parfor. The final runtime was 40 seconds.