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:
Edit mexopts.bat to compile mex file with OpenMP

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Hugo

Date: 19 Jul, 2010 17:08:04

Message: 1 of 8

Hello,

I'm trying to compile a C file that was paralelized with OpenMP. I know that I have to edit some lines of mexopts.bat, could somebody please tell me which ones? is that the only file I have to edit?

I'm using Windows 32 and Matlab 2007b

Thank you very much in advance.

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: James Tursa

Date: 19 Jul, 2010 18:30:24

Message: 2 of 8

"Hugo " <hugo@tsc.upc.edu> wrote in message <i220pk$m1p$1@fred.mathworks.com>...
> Hello,
>
> I'm trying to compile a C file that was paralelized with OpenMP. I know that I have to edit some lines of mexopts.bat, could somebody please tell me which ones? is that the only file I have to edit?
>
> I'm using Windows 32 and Matlab 2007b
>
> Thank you very much in advance.

Go to the following directory (or similar for your particular installation):

C:\Program Files\MATLAB\R2007b\bin\win32\mexopts

There you will see the original mex opts files. e.g., for VC you will see

    msvc80engmatopts.bat
    msvc80opts.bat

The first one is for engine applications and the second one is for mex routines. Inside these files you will find the compilation options line:

set COMPFLAGS=/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /nologo /MD

Try adding your OpenMP options to this line. My advice would be to copy this line into a new line, REM the old line and modify the new line, so you have a record of the original line in the file. (Or you could create a brand new copy of the options file and modify that and use that downstream).

Then run mex -setup again and select the compiler (that copies the appropriate options file to the [prefdir '\mexopts.bat'] file, which is what is actually used during a mex compilation command (not an engine build). For an engine build you need to specify the appropriate engmatopts.bat file directly in the mex command.

James Tursa

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Hugo

Date: 20 Jul, 2010 17:09:04

Message: 3 of 8

Dear Mr. Tursa,

Thank you for your answer. I'm not very much into OpenMP, so I don't actually know which lines must be added to the files you said, could you please help me with it?

In the file msvc80engmatopts.bat, the line you said is:

COMPFLAGS=/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /nologoset

And in the file msvc80opts.bat, the line is:

OMPFLAGS=/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologoset

Thank you very much!

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: James Tursa

Date: 20 Jul, 2010 20:22:06

Message: 4 of 8

"Hugo " <hugo@tsc.upc.edu> wrote in message <i24l7g$689$1@fred.mathworks.com>...
>
> I'm not very much into OpenMP, so I don't actually know which lines must be added to the files you said, could you please help me with it?

What compiler are you using? There should be documentation with the compiler that specifies what options need to be used to enable OpenMP.

James Tursa

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Hugo

Date: 21 Jul, 2010 10:26:04

Message: 5 of 8

I'm using the one that comes with Matlab 2007b: Lcc-win32 C 2.4.1.

Thank you!

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Stefan

Date: 21 Jul, 2010 11:47:07

Message: 6 of 8

The LCC does not support OpenMP.

See here:
http://openmp.org/wp/openmp-compilers/

Regards,
Stefan


"Hugo " <hugo@tsc.upc.edu> wrote in message <i26hvs$pdl$1@fred.mathworks.com>...
> I'm using the one that comes with Matlab 2007b: Lcc-win32 C 2.4.1.
>
> Thank you!

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Hugo

Date: 21 Jul, 2010 13:13:04

Message: 7 of 8

Thanks for the link, so which compiler that supports OpenMP can you recommend me to use, I'm using Matlab 2007b in Windows 32, but I can even try to install a newer version.

I only want to compile (mex) a C file that has been paralellized with OpenMP.

Thank you!

Subject: Edit mexopts.bat to compile mex file with OpenMP

From: Ramaprasad Kulkarni

Date: 25 Mar, 2011 01:37:04

Message: 8 of 8

I don't know if you got your issue resolved, but here is a simple solution which works for me:

The matopts.bat file to edit can be found at:
C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat

You can also find this file path when you try to compile a c file using mex function in verbose mode:
>>mex -v <somefile.c>
 --> the path will be shown as:
--> Options file = C:\Users\username>\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat

Then once you know this file path, you can use edit command to edit it and make entry for openmp.
>> edit C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat

In the file, edit 'OPTIMFLAGS' line as shown below (add /openmp entry at the end):
OPTIMFLAGS = /O2 /Oy- /DNDEBUG /openmp

Now you should be able to execute your c code with 'omp.h' included and pragmas to parallelize your code (assuming you have installed proper supported compilers - if using MSVC++, use a professional edition as other editions don't support openmp).

Hope this helps, if not for you, for others.

--
Ram.

Tags for 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