Mike IT Expert
Mike IT Expert

Reputation: 353

Intel c++ compiler: how to compiler and link openmp pragma in macro definition/define?

I have a simple code which uses pragma in macro definitions. The code compiles and runs fine using QtMinGW just fine.

#include <omp.h>
#include <stdio.h>

#define NUMEL 1024

#define OMP_PARALLEL _Pragma("omp parallel")
#define OMP_FOR _Pragma("omp for")

#define main_func(par)                                      \
int main(){                                                 \
    int a[NUMEL];                                           \
    OMP_PARALLEL                                            \
    {                                                       \
        int i;                                              \
        OMP_FOR                                             \
        for (i=0; i<NUMEL; i++){                            \
            printf("THRD : %d \n", omp_get_thread_num());   \
            a[i] = tan(i*2);                                \
        }                                                   \
    }                                                       \
    return 0;                                               \
}

main_func(par)

However, if I use Intel C++ Compiler (ICC) (icl.exe on windows) it outputs the below error:

 error: expected a ";"
  main_func(par)

By try and error, I noticed that if add ; at the of line like below the error changes to a linke error:

#define OMP_PARALLEL _Pragma("omp parallel");
#define OMP_FOR _Pragma("omp for");

Link ERROR

test_omp.obj : error LNK2019: unresolved external symbol _Pragma referenced in function main
test_omp.exe : fatal error LNK1120: 1 unresolved externals
make: *** [Makefile:16: test_omp] Error 1120

Any comment is much appreciate it.

Upvotes: 0

Views: 347

Answers (1)

Mike IT Expert
Mike IT Expert

Reputation: 353

After tones of try and error some Q/A on icc forum it turns out

  1. _Pragma should be replaced with __pragma
  2. no need for quotes

so the intel/icc syntax for using omp pragmas in a macro definition should be corrected as below:

#define OMP_PARALLEL __pragma(omp parallel)
#define OMP_FOR __pragma(omp for)

I hope it help others ... I leave the test code and makefile so you try yourself too. It took me 3 days to sort this out.

Feel free to vote my Q/A if you find it helpful.

#include <omp.h>
#include <stdio.h>

#define NUMEL 1024

#define OMP_PARALLEL __pragma(omp parallel)
#define OMP_FOR __pragma(omp for)

#define main_func(par)                                      \
int main(){                                                 \
    int a[NUMEL];                                           \
    OMP_PARALLEL                                            \
    {                                                       \
        int i;                                              \
        OMP_FOR                                             \
        for (i=0; i<NUMEL; i++){                            \
            printf("THRD : %d \n", omp_get_thread_num());   \
            a[i] = tan(i*2);                                \
        }                                                   \
    }                                                       \
    return 0;                                               \
}

main_func(par)


int main2(){
    int a[NUMEL];
    #pragma omp parallel
    {
        int i;
        #pragma omp for
        for (i=0; i<NUMEL; i++){
            printf("THRD : %d \n", omp_get_thread_num());
            a[i] = tan(i*2);
        }
    }
    return 0;
}

PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CXX=icl
CC=icl
LD =xilink

LIBDIR=Debug/lib/
OBJDIR=Debug/obj/

CFLAGS=/Qopenmp
LDFLAGS= /nodefaultlib:vcomp libiomp5md.lib
OBJS = $(OBJDIR)test_omp.obj 

all: test_omp

test_omp: $(OBJS)  
    $(LD) $(LDFLAGS) $(OBJS) -Fe:test_omp.exe


$(OBJDIR)%.obj: $(PROJECT_ROOT)%.cpp
    $(CXX) -c $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -Fo:$@ $<

$(OBJDIR)%.obj: $(PROJECT_ROOT)%.c
    $(CC) $(CFLAGS) $(CPPFLAGS) -Fo:$@ -c $<


clean:
    rm -fr test_icl $(OBJS) test_omp.exe

Upvotes: 1

Related Questions