How use openmp in .cu file ?

Hi, everyone,

Below is my code ( main.cu ):

#include <cufft.h>
#include <cutil.h>

#include <omp.h>

int
main()
{

#pragma omp parallel sections
{

/ * thread#0*/
#pragma omp section
{
do_something_on_GPU(…);
}
/thread#1/
#pragma omp section
{
do_something_on_CPU(…);
}
}

}

I just want to cooperate CPU & GPU to do something.

My problem is that when I compile the above code these omp pragmas do not have any effect !

My system is SuSE10.1,
the C/C++ compiler is icpc&icc 10.0.023, and nvcc1.0,
and the option ’ -Xcompiler -openmp’ has added to ‘NVCCFLAG’ in ‘common.mk’.

Thanks for your helps!

To pass the flag use -Xcompiler " -openmp".

If this is your real code, the first pragma is mispelled.

remove the header of openmp then add -openmp when you compile.

On my OS:

SuSE2.6.16.13-4-smp

gcc : 4.1.0

intel c/c++: 10.0.023

nvcc : 1.1

in “common.mk”:

CXX = icpc 

CC  = icpc

LINK = icpc -fPIC

NVCCFLAGS += -Xcompiler -openmp 

I try to compile the following code:

// file: omp_test.cu

include <stdio.h>

include <omp.h>

int

main()

{

#pragma omp parallel

{

  printf("hello, world!");

}

}

the compiling error is:

cc1plus:error:output filename specified twice.

How to specifiy the option “-openmp” through nvcc to icpc ?

Thanks.

I just ported the Windows CUDA SDK project/cudaOpenMP/cudaOpenMP.cu from Windows to Linux RHEL 5.1 with gcc 4.1. My understanding is that although OpenMP isn’t generally available until gcc 4.2, some Linux ports, like RHEL 5.1 with gcc 4.1, do support OpenMP to some extent (a good share of the OpenMP Validation Suite passes, but not everything). cudaOpenMP reports “Test PASSED” on RHEL 5.1 (I don’t have OpenMP support on my Windows machine so I can’t compare outputs).

To enable:

  1. add the following line below the line containing “NVCCFLAGS :=” around line 90
    NVCCFLAGS += -Xcompiler -fopenmp
  2. search for “# Libs” and add the following line below the initial definition of LIB
    LIB += -lgomp
  3. in cudaOpenMP.cu place curly-brackets ({}) around the #pragma omp parallel clause (after doing so the { will be at line 120 and the } at line 149

The extra set of {}'s shouldn’t have been necessary, but it seems to have been necessary for me.

P.S. This is RHEL Server 5.1, not workstation.

Hi,

I’m getting the same error message when compiling with -openmp:

cc1plus: error: output filename specified twice

and I’m using the same OS – opensuse 10.2

here is the command–
nvcc -Xcompiler -openmp -I. -I/usr/local/NVIDIA_CUDA_SDK/common/inc -I/usr/local/cmkl/include -c int.cu

If I don’t use ‘-Xcompiler -openmp’, everything compiles correctly…

Could you solve the problem? Looks like CUDA SUSE 10.2 bug to me…

The extra {}'s are needed when the #pragma encapsulates more than one statement, thus forming a block. At least that’s what I read elsewhere…

Anyway, using laffer’s advice above I got the cudaOpenMP example from the Windoze SDK working. I also wrote another little test program and all seemed to be going well so I went ahead and integrated OpenMP into my main project. Now, when I compile the project I get a “warning: ignoring #pragma omp parallel”.

The difference between my project and the cudaOpenMP example is that I’m using OpenMP in .cpp file, as opposed to .cu. The curious thing is that all the other OpenMP calls I use compile and work correctly. For instance omp_get_num_procs() compiles and gives the right result if queried, but when the #pragma omp parallel block is entered there’s only one CPU thread etc etc…

As I mentioned, I made the changes suggested by laffer in the common.mk file which means I can now compile and run CUDA app’s with OpenMP pragmas in .cu files but not in .cpp files. I’ve tried all sorts of different combinations of the following general form:

[codebox]CUDACCFLAGS := -Xcompiler -fopenmp -lgomp

CUDACCFLAGS += -Xcompiler -fopenmp -lgomp

NVCCFLAGS := -Xcompiler -fopenmp

NVCCFLAGS += -Xcompiler -fopenmp

CCLFAGS := -Xcompiler -fopenmp

CCLFAGS += -Xcompiler -fopenmp

LIB := -lgomp

LIB += -lgomp[/codebox]

but in each case I get the same “warning: ignoring #pragma omp parallel” and otherwise everything compiles, and runs… The OpenMP library is being found, linked and OpenMP calls work but the #pragma is ignored! Why!!!

Can anybody help me out with this? I’m sure it’s something small. If someone does I promise I’ll write a little how-to and post it here so if this question is ever asked again, it won’t need to be answered again…

Dumbass! I just solved it and it was very easy :D

Basically you have to do the same as laffer’s solution above, that is: in the common.mk file add “-fopenmp” to the CXXFLAGS variable.

I think we can safely say that to compile OpenMP in any file type .c, .cpp, or otherwise go to the common.mk file and add a line of the form

[codebox][whatever compiler]FLAGS += -fopenmp[/codebox]

or where [whatever compiler] is not for C or C++

[codebox][whatever compiler]FLAGS -Xcompiler -fopenmp[/codebox]

Why do I always get stuck on the easy stuff? (Don’t answer that…)

Hi, I know this is nearly 2 years late, but I’ve come across this problem, too, and figured out that “what” and the “why.”

The problem is that nvcc assumes a Gnu C compiler, regardless of whether you are using ifc or pgcc as your regular compiler. It sounds like you use ifc/ifcpc, which would normally accept “-openmp”. Use the gcc syntax “-fopenmp” in your nvcc command line instead.

Now, this bring about a whole new series of problems—how to link in the OpenMP libraries when some of your code is compiled with “ifc -openmp” and the Cuda stuff with “nvcc --compiler-options -fopenmp”. Any ideas?