matlab gcc mex,GCC C/C++ MEX Matlab R2015 Mac OS X (with OpenMP) doesn't work

问题

I'm trying to compile a very simple MEX file in Matlab using GCC/G++...

First I checked that this is already installed in matlab by:

!which gcc

output:

/usr/bin/gcc ...

After, I tried to do the following:

mex -v GCC='/usr/bin/gcc' hello.c

but it stills compiling with XCode Clang...

This is to use G++ with OPENMP library for parallel computing.

Is known that is NOT OFFICIALLY Supported by Mathworks, but POSSIBLE.

回答1:

MATLAB only supports XCode 5.1+ or 6.0 on Mac.

See this list of supported compilers, from Mathworks:

Supported Compilers and Compatible Compilers - Release R2015a

EDIT!!!

Even Windows doesn't support a GCC compiler. However, if you can build your library in Windows there is a 3rd party library called Gnumex designed to provide MinGW or CygWin GCC compiler capabilities for Matlab MEX. Take a look.

An update on their website says that changed to Matlab have broken their utility. However, there seems to be a workaround available via this StackOverflow post.

回答2:

Finally, I found a proper way to solve it...

First, the file mexopts.sh doesn't appear in the folder by default, is necessary to open a terminal and look for it and create it (then Matlab will redirect to it automatically when compiling with MEX):

find ~/ -name 'mexopts.sh'

and will appear:

/Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh

Then, copy it as:

cp /Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh ~/.matlab/R2014a

then go to the folder cd ~/.matlab/R2014a

and change permissions for your user as:

chmod u+rwx mexopts.sh

then, open it with your default text editor (Sublime text recommended) as:

open mexopts.sh

and edit the following:

Change where appears macosx10.7 to your current version, in my case macos10.10

then, modify the following lines as describen in (http://www.mathworks.com/matlabcentral/newsreader/view_thread/334250):

# CC='xcrun -sdk macosx10.7 clang' #OLD

CC='xcrun /usr/local/bin/gcc' #NEW

# CXX='xcrun -sdk macosx10.7 clang++' #OLD

CXX='xcrun /usr/local/bin/g++' #NEW

# CLIBS="$CLIBS -lstdc++" #OLD

CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory #NEW

#CXXLIBS="$MLIBS -lstdc++" #OLD

CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp" #NEW

IMPORTANT NOTE:

Make sure that your current G++/G++4.9 is able to compile with OpenMP, trying to include in a hello world file doing in the command line:

g++-4.9 -o test hello.cpp -fopenmp

or

g++ -o test hello.cpp -fopenmp

is also possible that a file is corrupted and is necessary to do Can not compile fortran because dyld: Library not loaded :

brew rm cloog

brew install cloog

(But check first)...

Is also possible that if you're not able to compile with OMP is necessary to do first a couple of things as described here (Compile OpenMP programs with gcc compiler on OS X Yosemite):

1. Got a new gcc complier from http://hpc.sourceforge.net/

2. Place a new executable folder by $ sudo tar -xvf gcc-4.9-bin.tar -C /

3. Switched to it by export PATH=/usr/local/bin:$PATH

Finally, try to compile your MEX file with:

mex hello.cpp COMPFLAGS="/openmp $COMPFLAGS"

That's it

... .

回答3:

I was unable to get the mexopts.sh approach to work.

Instead I followed this first to make sure that I could get omp to work with xcode: clang-omp in Xcode under El Capitan

Then the mex compile st

mex CC='/usr/local/bin/clang-omp' -I/usr/local/include -I/usr/local/lib /usr/local/lib/libiomp5.dylib test.c

However, I get some weird results when comparing between Matlab and pure terminal based c. The common work, I put in the file "do_work.c":

double do_work(int maxit){

double tmp,x,x2;

int numThreads=0;

numThreads = omp_get_max_threads() ;

// numThreads = 4;

printf("Setting max num threads to %d.\n",numThreads);

omp_set_num_threads(numThreads);

// int Nthreads=omp_get_num_threads();

tmp = 0.0;

x2 = 0.0;

#pragma omp parallel for shared(tmp,x2) private(x)

for (int i=0;i

x = 0.0;

for (int k=0; k<10000; k++) x += pow(.011,1.0/.5); // does some heavy computations

tmp += (double) i ;

if (i%1000==0){

printf("Hello, %d\n",i);

}

x2 += x;

}

printf("x2 = %f\n",x2);

return tmp;

}

I can compile this from command line using this wrapper file:

#include "stdio.h"

#include "math.h"

#include

#include "do_work.c"

double do_work(int maxit);

int main(int argc, const char * argv[]) {

do_work(10000);

return 0;

}

I compile it by running

clang-omp -fopenmp test_c_wrapper.c

I can also interface to it from Matlab with this code:

#include "mex.h"

#include

#include "math.h"

#include "stdio.h"

#include "do_work.c"

double do_work(int maxit);

void mexFunction(int nlhs, mxArray *plhs[],int nrhs,const mxArray *prhs[])

{

do_work(10000);

return;

}

To compile this from Matlab, save it as test.c and run the first mex statement that I mentioned higher up. However, the results are really strange. It looks as if Matlab is not even using omp. At the same time, Matlab actually runs faster than the terminal based one, even though Matlab appears to run in serial mode? Strange... Also, the omp_get_max_threads() is acting weird, it doesn't return the same number every time when called from Matlab even though it does from terminal.

来源:https://stackoverflow.com/questions/32155724/gcc-c-c-mex-matlab-r2015-mac-os-x-with-openmp-doesnt-work

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值