From: Juergen on
Hi,

I've a problem when I use openmp in combination with cpp files. I made a small example file which shows my problem

/* testOpenMP.c */
#include <omp.h>
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{ int nthreads, tid;
omp_set_num_threads(4);
printf("Number of processors: %d\n", omp_get_num_procs());
#pragma omp parallel private(nthreads, tid) {
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", omp_get_thread_num() );
}
}

compiled with following comand using gcc 4.3.2 and Matlab R2009b
mex CC=gcc CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" testOpenMP.c

creates following, correct output:
Number of processors: 4
Hello World from thread = 3
Hello World from thread = 2
Hello World from thread = 1
Hello World from thread = 0

If I now take exactly the same file just as cpp (and not C) I cant get OpenMP to work properly. Everything compiles correctly but the output is just
Number of processors: 4
Hello World from thread = 0

so multithreading isn't working.

I also set the environment variable "OMP_NUM_THREADS" like
setenv OMP_NUM_THREADS 4
before running the program

any ideas?
From: Juergen on
Okay I solved it by myself. I used the wrong flags

compiling it with
mex CXXFLAGS="\$CXXFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" testOpenMP.cpp

solved the problem

"Juergen " <juergen.wiest(a)gmx.de> wrote in message <i1f63b$6ot$1(a)fred.mathworks.com>...
> Hi,
>
> I've a problem when I use openmp in combination with cpp files. I made a small example file which shows my problem
>
> /* testOpenMP.c */
> #include <omp.h>
> #include "mex.h"
> void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
> { int nthreads, tid;
> omp_set_num_threads(4);
> printf("Number of processors: %d\n", omp_get_num_procs());
> #pragma omp parallel private(nthreads, tid) {
> tid = omp_get_thread_num();
> printf("Hello World from thread = %d\n", omp_get_thread_num() );
> }
> }
>
> compiled with following comand using gcc 4.3.2 and Matlab R2009b
> mex CC=gcc CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" testOpenMP.c
>
> creates following, correct output:
> Number of processors: 4
> Hello World from thread = 3
> Hello World from thread = 2
> Hello World from thread = 1
> Hello World from thread = 0
>
> If I now take exactly the same file just as cpp (and not C) I cant get OpenMP to work properly. Everything compiles correctly but the output is just
> Number of processors: 4
> Hello World from thread = 0
>
> so multithreading isn't working.
>
> I also set the environment variable "OMP_NUM_THREADS" like
> setenv OMP_NUM_THREADS 4
> before running the program
>
> any ideas?
From: Steve Amphlett on
"Juergen " <juergen.wiest(a)gmx.de> wrote in message <i1fduj$l4c$1(a)fred.mathworks.com>...
> Okay I solved it by myself. I used the wrong flags
>
> compiling it with
> mex CXXFLAGS="\$CXXFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" testOpenMP.cpp
>
> solved the problem

I tend to use "mex -v" so I see what exact compile and link commands are issued. It's saved my bacon many times.