Skip to content
Prev 37729 / 63424 Next

Best way to manage configuration for openMP support

Please do NOT use version checks on compilers and other tools - those are the wrong way to go! You want to use actual functionality check as that is the only reliable way to find out that something works or not*. For example there are issues on certain Linux systems with the gomp library that prevents it from loading into packages so regardless of the compiler version it won't work. Also other compilers also support OMP so checking specific compiler's version will be simply wrong.

In fact autoconf has AC_OPENMP macro that does all the heavy-lifting for you. This is a sample configure.ac that does the job:

# Process this file with autoconf to produce a configure script.
AC_INIT(OpenMPpackage, 0.8, Simon.Urbanek at r-project.org)
AC_CONFIG_SRCDIR([src/test.c])

# find R home and set CC/CFLAGS
: ${R_HOME=`R RHOME`}
if test -z "${R_HOME}"; then
  echo "could not determine R_HOME"
  exit 1
fi
RBIN="${R_HOME}/bin/R"
CC=`"${RBIN}" CMD config CC`;
CFLAGS=`"${RBIN}" CMD config CFLAGS`
LIBS="${PKG_LIBS}"

# Checks for programs.
AC_PROG_CC
# Check for OpenMP
AC_OPENMP

# since some systems have broken OMP libraries
# we also check that the actual package will work
ac_pkg_openmp=no
if test -n "${OPENMP_CFLAGS}"; then
  AC_MSG_CHECKING([whether OpenMP will work in a package])
  AC_LANG_CONFTEST(
  [AC_LANG_PROGRAM([[#include <omp.h>]], [[ return omp_get_num_threads (); ]])])
  PKG_CFLAGS="${OPENMP_CFLAGS}" PKG_LIBS="${OPENMP_CFLAGS}" "$RBIN" CMD SHLIB conftest.c 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD && "$RBIN" --vanilla -q -e "dyn.load(paste('conftest',.Platform\$dynlib.ext,sep=''))" 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD && ac_pkg_openmp=yes
  AC_MSG_RESULT([${ac_pkg_openmp}])
fi

# if ${ac_pkg_openmp} = "yes" then we have OMP, otherwise it will be "no"
if test "${ac_pkg_openmp}" = no; then
  OPENMP_CFLAGS=''
  # you could put AC_MSG_ERROR here is OpenMP is required
fi

AC_SUBST(OPENMP_CFLAGS)

AC_CONFIG_FILES([src/Makevars])
AC_OUTPUT

And your Makevars.in will probably look something like:

PKG_CPPFLAGS=@OPENMP_CFLAGS@
PKG_LIBS=@OPENMP_CFLAGS@ @LIBS@

Cheers,
Simon


* - compiler version checks are sometime used directly in the source files as a work-around if autoconf cannot be used. But since autoconf provides an easy way to test functionality you should do that instead if available.
On Sep 14, 2010, at 7:40 AM, Dirk Eddelbuettel wrote: