2

I'm doing geostatistical interpolations in R on a 5600 x 5700 matrix and, despite having available memory, I'm getting the error "C stack usage is too close to the limit."

There are a few SO questions related to this issue including this one and this one. These sources and others I've seen online suggest changing the stack size often resolves this issue. Some suggested this change: R_CStackLimit = (uintptr_t)-1 in the file "Rinterface.h". However I am on Windows 7 (x64), using R 2.15.3 (x64) via the Rpy2 module (v 2.3.6 x64 by way of Christoph Gohlke) in Python 2.7, and "Rinterface.h" isn't to be found. How can I otherwise change my effective stack limit for R?

The code that I am running for the interpolation is as follows (except I have it wrapped in a function):

  d <- read.table(wd,header=TRUE,sep=',')
  d <- na.omit(d)
  coordinates(d) <- ~ longdd+latdd ## convert simple data frame into a spatial data frame object
  proj4string(d) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
  stations <- spTransform(d, CRS(utm19))
  names(stations@data)[1] <- "stations"

  grids <- readGDAL("dem.asc")
  names(grids) <- "dem"
  grids$dsea <- readGDAL("dsea.asc")$band1
  proj4string(grids) <- CRS(utm19)
  ov <- overlay(grids, stations)
  stations$dem = ov$dem
  stations$dsea = ov$dsea

  stations <- stations[!is.na(stations$dsea),]

  vgm <- vgm(model="Sph",range=25000)

  v <- variogram(air_temp_max_c~dem+dsea,stations) 
  vgm_max_r<-fit.variogram(v, model=vgm)
  temp_uk <- krige(air_temp_max_c~dem+dsea, locations=stations, newdata=grids, model=vgm_max_r)
  write.asciigrid(temp_uk[1],outmax)
  max_cv <- krige.cv(air_temp_max_c~dem+dsea, locations=stations, vgm_max_r)

  max_cv <-data.frame(max_cv)
  max_cv["date"] <- dt
  max_cv["gs"] <- gs
  max_cv["parameter"] <- "air_temp_max_c"
  write.table(max_cv,file=<outFile>,sep=",",row.names=F)
Community
  • 1
  • 1
metasequoia
  • 7,014
  • 5
  • 41
  • 54
  • 1
    There might be something slightly inefficient in your code: it may be easier to fix than to recompile R (especially on Windows). How are you doing those interpolations? – Vincent Zoonekynd Jun 02 '13 at 08:30
  • This value is already the default ( https://bitbucket.org/lgautier/rpy2/src/2f51ee90bb3fce452d09ebe435efd9ccfc06c1ac/rpy/rinterface/_rinterface.c?at=default#cl-1260 ). Which version of rpy2 are you using by the way ? (`import rpy2; print(rpy2.__version__)`) – lgautier Jun 02 '13 at 12:52
  • @lgautier - does this imply that code optimization is where I should focus for a work around? I've updated my answer to include the rpy2 version: v2.3.6 x64 by way of Christoph Gohlke – metasequoia Jun 02 '13 at 14:01
  • @metasequoia - You could also check with Christoph whether the stack limit is what I think it is on Windows (its setting is conditional in the C code for rpy2). Otherwise, yes. It would mean that the limit would also be hit while running this in R only, I think. – lgautier Jun 02 '13 at 16:58

1 Answers1

0

you can use #include "Rinterface.h" and put file Rinterface.h in the same file as your C or R code.

The "Rinterface.h" is available at: https://svn.r-project.org/R/trunk/src/include/Rinterface.h

JJsmith
  • 41
  • 1
  • 4