Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mex openmp on mac

Subject: mex openmp on mac

From: Jveer

Date: 30 Oct, 2010 18:45:04

Message: 1 of 3

Hi

Could anyone please tell me how to properly mex c code containing openmp on mac (leopard/snow leopard running matlab R2010b)

The code seems to be running on both processors (as seen on activity monitor) but is about 3 times slower than the serial version.

Matlab support was unsuccessful at resolving the issue. I really need this code to run in parallel. I'm hoping someone here has the answer.

The mex command i've used is:

mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp" -lgomp PinC_C32.c

The relevant part of the code is:

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

void PinC_C32(unsigned int PiC[], const double P[][3], const int M,
        const double C[][6], const int N, const unsigned int Cores[]) {
    
    int i;
    #pragma omp parallel for num_threads(Cores[0])
    for (i=0; i<M; ++i) {
        int j;
        for (j=0; j<N; ++j) {

Btw on windows, same machine, the parallel code is about 1.5 times faster than the serial version.

Please advise.

The above mex command configures my environment as follows:
 >> mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp" -lgomp PinC_C32.c -v
-> mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
   FILE = /Users/JV33R/.matlab/R2010b/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /Applications/MATLAB_R2010b.app
-> CC = gcc-4.2
-> CC flags:
         CFLAGS = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fexceptions
         CDEBUGFLAGS = -g
         COPTIMFLAGS = -fopenmp
         CLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments = -DMX_COMPAT_32
-> CXX = g++-4.0
-> CXX flags:
         CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
         CXXDEBUGFLAGS = -g
         CXXOPTIMFLAGS = -O2 -DNDEBUG
         CXXLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
         FFLAGS = -fexceptions -m64 -fbackslash
         FDEBUGFLAGS = -g
         FOPTIMFLAGS = -O
         FLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
         arguments = -DMX_COMPAT_32
-> LD = gcc-4.2
-> Link flags:
         LDFLAGS = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map
         LDDEBUGFLAGS = -g
         LDOPTIMFLAGS = -O
         LDEXTENSION = .mexmaci64
         arguments = -lgomp
-> LDCXX =
-> Link flags:
         LDCXXFLAGS =
         LDCXXDEBUGFLAGS =
         LDCXXOPTIMFLAGS =
         LDCXXEXTENSION =
         arguments = -lgomp
----------------------------------------------------------------

-> gcc-4.2 -c -I/Applications/MATLAB_R2010b.app/extern/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fexceptions -DMX_COMPAT_32 -fopenmp "PinC_C32.c"

-> gcc-4.2 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map -o "PinC_C32.mexmaci64" PinC_C32.o -lgomp -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

Subject: mex openmp on mac

From: Jveer

Date: 30 Oct, 2010 19:12:03

Message: 2 of 3

UPDATE:

I just tried adding -O and it improved the performance. However, the speedup should be closer to x2 considering 99% of the code runs in parallel. Also, the speed up on windows (same machine, same matlab version) is x1.5

Any ideas?

I can provide the c code and a test .mat if needed.

At this point I don't think it's about the compiler or the c code. It's about mex flags as the following proves.

Note that not adding -O is what made the code about x3 times slower in parallel compared to the serial! That confused me for ages.

Serial
>> mex PinC_C32.c
>> clear;load('/Users/JV33R/Desktop/PinC[Final]/PinCData1.mat');
>> tic;PinC_C32(PiC,P,C,N_Cores);toc
Elapsed time is 3.493521 seconds.

Parallel
>> mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp -O" -lgomp PinC_C32.c
>> tic;PinC_C32(PiC,P,C,N_Cores);toc
Elapsed time is 3.056082 seconds.
>> 3.493521/3.056082

ans =

   1.1431

Parallel with -O
>> mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp" -lgomp PinC_C32.c
>> clear;load('/Users/JV33R/Desktop/PinC[Final]/PinCData1.mat');
>> tic;PinC_C32(PiC,P,C,N_Cores);toc
Elapsed time is 12.117606 seconds.
>> 12.117606/3.493521

ans =

    3.4686

Subject: mex openmp on mac

From: Colin

Date: 2 Jan, 2012 21:32:08

Message: 3 of 3

Did you ever solve this issue? I am having trouble getting mex to compile with openMP on a mac running Matlab 2011b with GCC4.2. Any links to documentation would be great. I am unclear on the proper mex commands to link everything.


"Jveer " <jveer@jveer.com> wrote in message <iahp3g$qha$1@fred.mathworks.com>...
> Hi
>
> Could anyone please tell me how to properly mex c code containing openmp on mac (leopard/snow leopard running matlab R2010b)
>
> The code seems to be running on both processors (as seen on activity monitor) but is about 3 times slower than the serial version.
>
> Matlab support was unsuccessful at resolving the issue. I really need this code to run in parallel. I'm hoping someone here has the answer.
>
> The mex command i've used is:
>
> mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp" -lgomp PinC_C32.c
>
> The relevant part of the code is:
>
> #include <omp.h>
> #include <mex.h>
>
> void PinC_C32(unsigned int PiC[], const double P[][3], const int M,
> const double C[][6], const int N, const unsigned int Cores[]) {
>
> int i;
> #pragma omp parallel for num_threads(Cores[0])
> for (i=0; i<M; ++i) {
> int j;
> for (j=0; j<N; ++j) {
>
> Btw on windows, same machine, the parallel code is about 1.5 times faster than the serial version.
>
> Please advise.
>
> The above mex command configures my environment as follows:
> >> mex CC="gcc-4.2" LD="gcc-4.2" COPTIMFLAGS="$COPTIMFLAGS -fopenmp" -lgomp PinC_C32.c -v
> -> mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
> FILE = /Users/JV33R/.matlab/R2010b/mexopts.sh
> ----------------------------------------------------------------
> -> MATLAB = /Applications/MATLAB_R2010b.app
> -> CC = gcc-4.2
> -> CC flags:
> CFLAGS = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fexceptions
> CDEBUGFLAGS = -g
> COPTIMFLAGS = -fopenmp
> CLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
> arguments = -DMX_COMPAT_32
> -> CXX = g++-4.0
> -> CXX flags:
> CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
> CXXDEBUGFLAGS = -g
> CXXOPTIMFLAGS = -O2 -DNDEBUG
> CXXLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
> arguments = -DMX_COMPAT_32
> -> FC = gfortran
> -> FC flags:
> FFLAGS = -fexceptions -m64 -fbackslash
> FDEBUGFLAGS = -g
> FOPTIMFLAGS = -O
> FLIBS = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
> arguments = -DMX_COMPAT_32
> -> LD = gcc-4.2
> -> Link flags:
> LDFLAGS = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map
> LDDEBUGFLAGS = -g
> LDOPTIMFLAGS = -O
> LDEXTENSION = .mexmaci64
> arguments = -lgomp
> -> LDCXX =
> -> Link flags:
> LDCXXFLAGS =
> LDCXXDEBUGFLAGS =
> LDCXXOPTIMFLAGS =
> LDCXXEXTENSION =
> arguments = -lgomp
> ----------------------------------------------------------------
>
> -> gcc-4.2 -c -I/Applications/MATLAB_R2010b.app/extern/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fexceptions -DMX_COMPAT_32 -fopenmp "PinC_C32.c"
>
> -> gcc-4.2 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map -o "PinC_C32.mexmaci64" PinC_C32.o -lgomp -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us