错误:C 堆栈使用量太接近限制 [英] Error: C stack usage is too close to the limit

查看:55
本文介绍了错误:C 堆栈使用量太接近限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 R 中运行一些相当深的递归代码,但它一直给我这个错误:

I'm attempting to run some fairly deep recursive code in R and it keeps giving me this error:

错误:C 堆栈使用量太接近限制

Error: C stack usage is too close to the limit

我从 CStack_info() 的输出是:

Cstack_info()
    size    current  direction eval_depth 
67108864       8120          1          2 

我的机器上有足够的内存,我只是想弄清楚如何增加 R 的 CStack.

I have plenty of memory on my machine, I'm just trying to figure out how I can increase the CStack for R.

有人要求提供一个可重现的示例.这是导致问题的一些基本示例代码.运行 f(1,1) 几次你会得到错误.请注意,我已经设置了 --max-ppsize = 500000 和 options(expressions=500000) 所以如果你不设置它们,你可能会收到关于这两件事之一的错误.如您所见,这里的递归可能非常深入,我不知道如何让它始终如一地工作.谢谢.

Someone asked for a reproducible example. Here's some basic sample code that causes the problem. Running f(1,1) a few times you'll get the error. Note that I've already set --max-ppsize = 500000 and options(expressions=500000) so if you don't set those you might get an error about one of those two things instead. As you can see, the recursion can go pretty deep here and I've got no idea how to get it to work consistently. Thanks.

f <- function(root=1,lambda=1) {
    x <- c(0,1);
    prob <- c(1/(lambda+1),lambda/(lambda+1));
        repeat {
      if(root == 0) {
        break;
      }
      else {
        child <- sample(x,2,replace=TRUE,prob);
        if(child[1] == 0 && child[2] == 0) {
          break;
        }
        if(child[1] == 1) {
          child[1] <- f(root=child[1],lambda);
        }
        if(child[2] == 1 && child[1] == 0) {
          child[2] <- f(root=child[2],lambda);
        }
      }
      if(child[1] == 0 && child[2] == 0) {
        break;
      }
      if(child[1] == 1 || child[2] == 1) {
        root <- sample(x,1,replace=TRUE,prob);
      }
        }
    return(root)
}

推荐答案

堆栈大小是一个操作系统参数,可按进程调整(参见 setrlimit(2)).据我所知,您无法在 R 中调整它,但是您可以在启动 R 之前使用 ulimit 命令从 shell 中调整它.它是这样工作的:

The stack size is an operating system parameter, adjustable per-process (see setrlimit(2)). You can't adjust it from within R as far as I can tell, but you can adjust it from the shell before starting R, with the ulimit command. It works like this:

$ ulimit -s # print default
8192
$ R --slave -e 'Cstack_info()["size"]'
   size 
8388608

8388608 = 1024 * 8192;R 打印与 ulimit -s 相同的值,但以字节而不是千字节为单位.

8388608 = 1024 * 8192; R is printing the same value as ulimit -s, but in bytes instead of kilobytes.

$ ulimit -s 16384 # enlarge stack limit to 16 megs
$ R --slave -e 'Cstack_info()["size"]'
    size 
16777216 

要对此设置进行永久调整,请将 ulimit 命令添加到您的 shell 启动文件中,以便在您每次登录时执行.我无法给出比这更具体的说明,因为这完全取决于您拥有哪种外壳和东西.我也不知道如何登录图形环境(如果您不在终端窗口中运行 R,这将是相关的).

To make a permanent adjustment to this setting, add the ulimit command to your shell startup file, so it's executed every time you log in. I can't give more specific directions than that, because it depends on exactly which shell you have and stuff. I also don't know how to do it for logging into a graphical environment (which will be relevant if you're not running R inside a terminal window).

这篇关于错误:C 堆栈使用量太接近限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆