Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Linux gotcha: Building with -lgomp

kfsone
New Contributor I
674 Views
FYI for Linux Users. Linking binaries with Intel Compiler and -lgomp produces different behavior than linking with GCC and -lgomp (I believe this is expected behavior, however).

omptest.cpp
[cpp]#include 
#include 

int main(int argc, char* argv[]) 
{
  static const signed int THREADS_TO_USE = 8 ;
  static const signed int MAX_THREAD_NO  = THREADS_TO_USE - 1 ; // 0-based index for thread number

  signed int maxThreadNo = -1 ; // Will log the highest thread we see here.

  #pragma omp parallel for schedule(static, 1) num_threads(THREADS_TO_USE) shared(maxThreadNo)
  for ( signed int i = 0 ; i < THREADS_TO_USE * THREADS_TO_USE ; ++i ) // Ensure enough work to get some threads going
  {
    signed int myThread = omp_get_thread_num() ;
    #pragma omp critical
    {
      if ( myThread > maxThreadNo )
        maxThreadNo = myThread ;
    }
  }

  if ( maxThreadNo == MAX_THREAD_NO )
    printf("SUCCESS\n") ;
  else
    printf("FAILED: maxThreadNo == %d\n", maxThreadNo) ;
}
[/cpp]
omptest.sh script to build/run 4 variants.
[bash]#! /bin/bash

test_compile () {
 ldflag="${1}"
 exe="./omptest.exe"
 if [ "${CC}" = "g++" ] ; then openmp="-fopenmp" ; else openmp="-openmp" ; fi
 ${CC} -O3 -Wall -Werror ${openmp} ${ldflag} -o ${exe} omptest.cpp && ${exe}
 rm -f ${exe}
}

build_omptest_nogomp () {
 echo "Testing ${CC} without gomp:"
 test_compile ""
}

build_omptest_withgomp () {
 echo "Testing ${CC} with gomp:"
 test_compile -lgomp
}

CC="g++" build_omptest_nogomp
CC="icpc" build_omptest_nogomp
CC="g++" build_omptest_withgomp
CC="icpc" build_omptest_withgomp
[/bash]
Produces:
osmith@lucid32:~$ uname -a && bash omptest.sh
Linux lucid32 2.6.32-19-generic #28-Ubuntu SMP Wed Mar 31 17:46:20 UTC 2010 i686 GNU/Linux
Testing g++ without gomp:
SUCCESS
Testing icpc without gomp:
SUCCESS
Testing g++ with gomp:
SUCCESS
Testing icpc with gomp:
FAILED: maxThreadNo == 0
I.e. if you have left-over -lgomp flags from building your project with GCC/G++, you will only get one thread in OpenMP pragmas.
Please note that this code is not intended to be a good example of how to code with openmp :)
0 Kudos
1 Reply
TimP
Honored Contributor III
674 Views
-liomp5 should replace lgomp. That library handles both gomp and Intel OpenMP function calls consistently (on linux).
0 Kudos
Reply