[Rcpp-devel] Stack imbalance warning when using Rcpp and OpenMP

Michael Braun braunm at MIT.EDU
Tue Jul 26 23:08:51 CEST 2011


Hi.

I have an R list that I want to pass to C++ code through Rcpp, and then process each of the list elements with another function, in parallel through OpenMP.  Here is a simplified example in which I compute the square root of a number in each list element:

// file omp.cpp

#include <Rcpp.h>
#include <omp.h>

double func(SEXP Y_) {
  using namespace Rcpp;
  double Y = as<double>(Y_);
  double res = sqrt(Y);
  return(res);
}

RcppExport SEXP omp3 (SEXP R_) {

  BEGIN_RCPP

 using namespace Rcpp;

 List R = R_;
 int n = R.size();
 int i;

 NumericVector X(n);
 
#pragma omp parallel shared(R, n, X) private(i)
 {
#pragma omp for
 for (i=0; i<n; i++) {
   X(i) = func(R(i));
 }
 }
 
 return(wrap(X));

 END_RCPP

}


The R code to call the omp3 C++ function is:

dyn.load("omp.so")

R <- vector("list",5)
for(i in 1:5) R[[i]] = i  

res <- .Call("omp3",R)

dyn.unload("omp.so")


When I run this program, I almost always get stack imbalance warnings like:

Warning: stack imbalance in '.Call', 33 then 32
Warning: stack imbalance in '<-', 31 then 30
Warning: stack imbalance in 'eval.with.vis', 27 then 26
Warning: stack imbalance in '.Internal', 26 then 25
Warning: stack imbalance in '<-', 20 then 19
Warning: stack imbalance in '{', 18 then 17
Warning: stack imbalance in 'if', 16 then 15
Warning: stack imbalance in '{', 14 then 13
Warning: stack imbalance in 'for', 8 then 7

I say almost because it does not happen all the time, but if I run the same script a few more times, I will invariably get a stack imbalance warning. (Also, the numbers are not always the same).

But there's more.  If I run the program a bunch of times, eventually I get:

Lost warning messages
> Error in eval.with.vis(expr, envir, enclos) : 
  REAL() can only be applied to a 'numeric', not a 'NULL'
Error during wrapup: C stack usage is too close to the limit


Once I get this error, I will no longer get stack imbalance warnings, but I cannot predict how many attempts it would take before the C stack usage error comes up.

And if I am really lucky, I'll get memory not mapped segfaults, but not every time.

I have run this on my Mac Pro with both g++ 4.6 and Intel C++ compiler 12.0.4.  I have also run it on a Red Hat Linux machine with g++ 4.4 (the segfaults happen much more frequently under Linux than under Mac OSX).  The stack imbalance warnings come up on both machines, with all compilers.

I also wonder if this is related to another problem with Rcpp and OpenMP that I posted back in late March.  In that case, the problem was using an old compiler; updating the compiler solved the compilation problem.   The difference between that case and this example is calling the second function with a SEXP as an argument. This example compiles, but does not run.

I know that Rcpp is rigorously tested, so the problem is probably something that I am doing wrong.  In particular, I wonder if each element of the list should be passed as a SEXP, or if there is a better way to do it.  In any case, I greatly appreciate your assistance.

(Also, I deeply apologize for not using the inline package with the example.  I could not figure out how to include the two different functions.)

Thanks,

Michael








-------------------------------------------
Michael Braun
Associate Professor of Management Science (Marketing Group)
MIT Sloan School of Management
100 Main St.., E62-535
Cambridge, MA 02139
braunm at mit.edu
617-253-3436




More information about the Rcpp-devel mailing list