Parallel MATLAB with openmp mex files

October 21st, 2009 | Categories: matlab, programming | Tags:

Slowly but surely more and more MATLAB functions are becoming able to take advantage of multi-core processors.  For example, in MATLAB 2009b, functions such as sort, bsxfun, filter and erf (among others) gained the ability to spread the calculational load across several processor cores.  This is good news because if your code uses these functions, and if you have a multi-core processor, then you will get faster execution times without having to modify your program.   This kind of parallelism is called implicit parallelism because it doesn’t require any special commands in order to take advantage of multiple cores – MATLAB just does it automagically.  Faster code for free!

For many people though, this isn’t enough and they find themselves needing to use explicit parallelism where it becomes the programmer’s responsibility to tell MATLAB exactly how to spread the work between the multiple processing cores available.  The standard way of doing this in MATLAB is to use the Parallel Computing Toolbox but, unlike packages such as Mathematica and Maple, this functionality is going to cost you extra.

There is another way though…

I’ve recently been working with David Szotten, a postgraduate student at Manchester University, on writing parallel mex files for MATLAB using openmp and C.  Granted, it’s not as easy as using the Parallel Computing Toolbox but it does work and the results can be blisteringly fast since we are working in C.  It’s also not quite as difficult as we originally thought it might be.

There is one golden rule you need to observe:

Never, ever call any of the mex api functions inside the portion of your code that you are trying to parallelise using openmp.  Don’t try to interact with MATLAB at all during the parallel portion of your code.

The reason for the golden rule is because, at the time of writing at least (MATLAB 2009b), all mex api functions are not thread-safe and that includes the printf function since printf is defined to be mexPrintf in the mex.h header file.  Stick to the golden rule and you’ll be fine.  Move away from the golden rule,however, and you’ll find dragons.  Trust me on this!

Let’s start with an example and find the sum of foo(x) where foo is a moderately complicated function and x is a large number of values.  We have implemented such an example in the mex function mex_sum_openmp.

I assume you are running MATLAB 2009b on 32bit Ubuntu Linux version 9.04 – just like me.  If your setup differs from this then you may need to modify the following instructions accordingly.

  • Before you start.  Close all currently running instances of MATLAB.
  • Download and unzip the file  mex_sum_openmp.zip to give you mex_sum_openmp.c
  • Open up a bash terminal and enter export OMP_NUM_THREADS=2 (replace 2 with however many cores your machine has)
  • start MATLAB from this terminal by entering matlab.
  • Inside MATLAB, navigate to the directory containing mex_sum_openmp.c
  • Inside MATLAB, enter the following to compile the .c file
    mex mex_sum_openmp.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"
  • Inside MATLAB, test the code by entering
    x=rand(1,9999999); %create an array of random x values
    mex_sum_openmp(x) %calculate the sum of foo(x(i))
  • If all goes well, this calculation will be done in parallel and you will be rewarded with a single number.  Time the calculation as follows.
    tic;mex_sum_openmp(x);toc
  • My dual core laptop did this in about 0.6 seconds on average.  To see how much faster this is compared to single core mode just repeat all of the steps above but start off with export OMP_NUM_THREADS=1 (You won’t need to recompile the mex function).  On doing this, my laptop did it in 1.17 seconds and so the 2 core mode is almost exactly 2 times faster.

Let’s take a look at the code.

#include "mex.h"
#include <math.h>
#include <omp.h>

double spawn_threads(double x[],int cols)
{
	double sum=0;
	int count;
        #pragma omp parallel for shared(x, cols) private(count) reduction(+: sum)
	for(count = 0;count<cols;count++)
	{
	     sum += sin(x[count]*x[count]*x[count])/cos(x[count]+1.0);;
	}

	return sum;
}

void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
	double sum;
	 /* Check for proper number of arguments. */
 	if(nrhs!=1) {
    	mexErrMsgTxt("One input required.");
  	} else if(nlhs>1) {
    	mexErrMsgTxt("Too many output arguments.");
  	}

	/*Find number of rows and columns in input data*/
	int rows = mxGetM(prhs[0]);
	int cols = mxGetN(prhs[0]);		

	/*I'm only going to consider row matrices in this code so ensure that rows isn't more than 1*/
	if(rows>1){
	mexErrMsgTxt("Input data has too many Rows.  Just the one please");
	}

	/*Get pointer to input data*/
	double* x = mxGetPr(prhs[0]);

	/*Do the calculation*/
	sum = spawn_threads(x,cols);

	/*create space for output*/
 	 plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);

	/*Get pointer to output array*/
	double* y = mxGetPr(plhs[0]);

	/*Return sum to output array*/
	y[0]=sum;

}

I’m not going to go through this line by line as I assume that you know how to program in openmp and also know how to write basic mex files. The key point to notice is that I don’t have any parallel code inside mexFunction() which serves as MATLAB’s entry point into the C code. Likewise, I don’t have any mex functions inside my spawn_threads() function – the part that is actually parallelised using openmp.

That’s pretty much it – the golden rule in action. I hope you find this example useful and thanks again to David Szotten who helped me figure all of this out.  Comments welcomed.

  1. kyle
    April 9th, 2010 at 15:19
    Reply | Quote | #1

    just curious. Can Matlab 2008b do this job or not?

  2. April 10th, 2010 at 08:47
    Reply | Quote | #2

    Hi Kyle

    I can’t see why not although I haven’t tried it.

    Cheers,
    Mike

  3. Anika
    May 11th, 2010 at 10:29
    Reply | Quote | #3

    Hello.
    I’m working on my thesis and try using OpenMP to parallelize a few things.
    I am using MATLAB 7.1 and Visual Studio Pro 2008. With this two things I’ve done some small c-code and compile it successfully.
    Now I’m trying something different and it not work.
    Do you know if you can call in a parallel for loop MATLAB functions?

    int i;
    mxArray *input[3], *out[1];
    #pragma omp parallel for
    for(i = 1; i <= 6; i++){
    mexCallMATLAB(1,out,3,input,"CalcSomething");
    }

    About Help or ideas I'd be very grateful.
    Greetings
    Anika

  4. May 17th, 2010 at 05:35
    Reply | Quote | #4

    Hi Anika

    I can see WHY you want to do this since it will effectively be a replacement for MATLAB’s parfor function (for which you have to pay extra) but it is just not possible at the moment.

    I’m afraid that what you are trying to do will simply not work since you are calling a mex function inside your parallelised loop. As I said in the article, this does not work since mex functions are not thread-safe.

    So, what can you do in your situation? Well, at the moment you have a couple of options if you want to take the direct parallelisation route.

    *Use the free parallel toolbox from MIT, pMATLAB. http://www.ll.mit.edu/mission/isr/pmatlab/pmatlab.html
    *Pay for MATLAB’s Parallel Computing Toolbox. http://www.mathworks.com/products/parallel-computing/

    Remember,however, that there is always more than one way to crack a nut and, depending on your application, there might be some other things you can use in order to get your results more quickly.

    For example, if you were at my university then I might suggest that you consider high throughput computing (HTC) on our experimental condor pool or replacing some key MATLAB functions with routines from the NAG Toolbox for MATLAB among other things.

    The very first thing I’d suggest, however, would be to run your code through the MATLAB profiler and see if you can speed things up with good, old fashioned optimisation.

    Cheers,
    Mike

  5. August 7th, 2010 at 00:29
    Reply | Quote | #5

    Thanks for this great article. Also thanks for the link to the MIT toolbox. What are the advantages of using this functionality vs the MIT toolbox. When would you pick one over the other?

  6. August 7th, 2010 at 00:42
    Reply | Quote | #6

    Also do I need to install any openmpi from the ubuntu repositories?

  7. Vladko
    August 15th, 2010 at 23:45
    Reply | Quote | #7

    Thanks for the insight. I ran the code on the cluster of my university. Just opening Matlab and running the program gave me no problems and I got what I expected. However, when I used nohup and OMP_NUM_THREADS was bigger than 1, everything was again executed correctly (on more than one threads) but I got the message Segmentation Fault in the end. Do you know why nohup caused that issue and what are the consequences? Any response will be greatly appreciated.