I got lots of error messages when trying to use openmp in a c++ code for building my R package on windows 7:
c:/rtools/mingw/bin/../lib/gcc/mingw32/4.5.0/libgomp.a(parallel.o):(.text+0x19): undefined reference to `_imp__pthread_getspecific'
c:/rtools/mingw/bin/../lib/gcc/mingw32/4.5.0/libgomp.a(parallel.o):(.text+0x7a): undefined reference to `_imp__pthread_mutex_lock'
c:/rtools/mingw/bin/../lib/gcc/mingw32/4.5.0/libgomp.a(env.o):(.text+0x510): undefined reference to `_imp__pthread_mutex_init'
...
Is Rtools not supporting openmp? Does anyone know how to use openmp in windows R packages please?
Compile with -fopenmp to enable OpenMP. GCC binary builds are provided by Linux distributions, often with offloading support provided by additional packages, and by multiple entities for other platforms – and you can build it from source. Windows, Linux, and MacOSX.
Enable OpenMP Right-click on your project in Solution Explorer, and select properties. Select C/C++ -> Language, and change OpenMP Support to Yes.
OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating systems, including Solaris, AIX, FreeBSD, HP-UX, Linux, macOS, and Windows.
In 2015, Rtools
has openmp
support under Windows, which plays nicely with Rcppp
. Here is a simple example creating a squares
function for numeric vectors:
// src/example.cpp
#include <Rcpp.h>
#include <omp.h>
// [[Rcpp::plugins(openmp)]]]
// [[Rcpp::export]]
Rcpp::NumericVector squares (Rcpp::NumericVector data)
{
Rcpp::NumericVector result(data.size());
#pragma omp parallel
{
Rcpp::Rcout << omp_get_num_threads() << std::endl;
for (int i = 0; i < data.size(); i++) {
result[i] = data[i] * data[i];
}
}
return result;
}
We also need to create src/Makevars.win
with the openmp
compilation flags. In this example, the sample src/Makevars
will work on linux:
# src/Makevars.win
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
No, per discussions on the R-devel mailing list. It also came up on the Rcpp-devel list.
R itself does not use OpenMP on Windows, so there is not support in Rtools. On other OSs R does of course have OpenMP support.
By reference to these posts (R-devel mailing list), I tried to use OpenMP in windows R packages by using TDM-GCC. It seems to run right.
But I recommend to use officially supported OSs. I don't know what problems will happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With