From edd at debian.org  Tue Sep 14 19:42:08 2010
From: edd at debian.org (Dirk Eddelbuettel)
Date: Tue, 14 Sep 2010 12:42:08 -0500
Subject: [Rd] Best way to manage configuration for openMP support
In-Reply-To: <B295D4D3-C4CA-41D9-AC57-9DD0EC391BE6@r-project.org>
References: <AANLkTikdeTG2dEQYfTnh5jDTASnCBiSkis+xKsVUBOpu@mail.gmail.com>
	<19599.24388.430713.722583@max.nulle.part>
	<B295D4D3-C4CA-41D9-AC57-9DD0EC391BE6@r-project.org>
Message-ID: <19599.46064.609453.301965@max.nulle.part>


On 14 September 2010 at 13:01, Simon Urbanek wrote:
| 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:

Seconded. This is the proper way. I had meant to add the hint for a
autoconf-wrapped test program but had to dash out earlier.

Thanks for posting a full example, Simon. Much appreciated.

Dirk
 
| # 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:
| 
| > 
| > On 14 September 2010 at 11:06, Karl Forner wrote:
| > | I've written a package that may use OpenMP to speed up computations. OpenMP
| > | is supported in recent Gcc versions by using the -fopenmp flag.
| > | The problem is that flag crashed gcc versions that do not support OpenMP.
| > | So what is the best way for a package to handle this issue. Has someone a
| > | configure script that deals with this ?
| > 
| > I don't know off-hand of any CRAN packages that do that, but you could look
| > at Luke Tierney's pnmath package which uses Open MP. It may have a test.
| > 
| > Else, you can query gcc for minimum versions. I have some configure code from
| > way back when then tested for a minimum version of 3.0 (!!):
| > 
| > # We are using C++
| > AC_LANG(C++)
| > AC_REQUIRE_CPP
| > 
| > AC_PROG_CXX
| > if test "${GXX}" = yes; then
| >    gxx_version=`${CXX} -v 2>&1 | grep "^.*g.. version" | \\
| > 		       sed -e 's/^.*g.. version *//'`
| >    case ${gxx_version} in
| >        1.*|2.*)
| > 	     AC_MSG_WARN([Only g++ version 3.0 or greater can be used with RQuantib.])
| > 	     AC_MSG_ERROR([Please use a different compiler.])   
| >        ;;
| >    esac
| > fi
| > 
| > You could do the same for gcc and strip out major version (4) and minor (0 or
| > 1) and then complain.  With 4.2 you should be fine.
| > 
| > Dirk
| > 
| > -- 
| > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| > 
| > ______________________________________________
| > R-devel at r-project.org mailing list
| > https://stat.ethz.ch/mailman/listinfo/r-devel
| > 
| > 
| 

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



[Rd] Best way to manage configuration for openMP support
Dirk Eddelbuettel