繁体   English   中英

使用降雪包,具有C-in-R功能的R中的并行计算。 问题:Mac显示加载轮并几乎死机

[英]Parallel computing in R with C-within-R functions, using snowfall package. Issue: Mac shows loading wheels and almost freezes

我有一个包含C-within-R函数的R包,名为myFun。 我想在并行计算中将此称为myFun。 此myFun本身在Mac上可以正常运行,但是通过并行计算功能sfClusterApplyLB调用时,它表现出奇怪的行为:我的Mac显示装载轮,R几乎冻结。 稍后,R停止冻结,并且sfClusterApplyLB返回并行化的结果。 我真的想避免这种冻结情况,因为冻结时我什至无法上下滚动R Console!

为了说明这一点,我有一个小的示例代码。

我有一个小的C代码,它会循环100次,同时每20秒打印出一次迭代次数,并在每次迭代时休眠1秒:

 # include <R.h>
 # include <Rinternals.h>
 # include <Rmath.h>
 # include <R_ext/Linpack.h>
 # include <unistd.h>

 SEXP myC (SEXP j)
  {
   for (int i = 0; i < 100; i++)
      {
        R_FlushConsole(); 
        R_ProcessEvents();
        R_CheckUserInterrupt(); 
        sleep(1); // sleep one second at each iteration. this sleep is
        // replaced by something in my code
        if (i%20==0) Rprintf("\v%d iterations are done...",i);
    }
  return (R_NilValue);
  }

我创建了一个临时R包“ myRpack”,其中包含此myC函数及其R包装函数myFun,其定义为:

 myFun <- function(x)
   {
    .Call("myC",
       as.integer(x),
       "myRpack")
    }

通过终端中的命令R CMD INSTALL myRpack将“ myRpack”安装到了我的Mac。

独立运行myCfun函数可以正常工作。 查看,

 library(myRpack)
 myFun(1)

现在,我想使用降雪包并行计算此myFun。 这是用于此目的的并行计算的R代码:

 library("snowfall")
 sfInit(parallel=TRUE,cpus=3,type="SOCK")
 x <- 1 : 100
 res.list <- sfClusterApplyLB(x,myFun)

然后R冻结!

PS我执行C-in-R函数(不使用并行计算)时遇到了这个问题。 我向Rcpp询问了这个问题:Mac显示加载轮并且几乎冻结 那时候的解决方案是添加行

    R_FlushConsole(); 
    R_ProcessEvents();
    R_CheckUserInterrupt();  

在我的C文件中(我也在代码中做了)。 但是,此解决方案在并行计算环境中无济于事。

我将不胜感激任何帮助。

PSS即使我将myC函数定义为:

# include <R.h>
# include <Rinternals.h>
# include <Rmath.h>
# include <R_ext/Linpack.h>
# include <unistd.h>

SEXP myC (SEXP j)
{
  const int input=INTEGER(j)[0];
  Rprintf("\n input %d",input);
  for (int i = 0; i < 100; i++)
    {
  R_FlushConsole(); 
  R_ProcessEvents();
      R_CheckUserInterrupt(); 
  sleep(1); // sleep one second at each iteration. this sleep is
      // replaced by something in my code
  if (i%20==0) Rprintf("\v%d iterations are done...",i);
}
  return (R_NilValue);
}

存在问题。

我遇到了同样的问题,偶然发现了一种可能无法解决您的问题的解决方法。

tl; dr:我意识到我正在运行的脚本会使用较低级别的C代码并行调用这些函数,这些脚本将挂在R GUI中,并会在终端的R实例中正确执行(每个实例一次,以后的source() ing会挂起)。


更多背景信息,对于面临相同问题的其他人:我碰到了它,它并行地调用函数gbm.step()dismo包。 我正在使用doParallel和foreach包对其进行并行化。 gbm.step()函数使用C函数来拟合增强的回归树模型,当我并行运行它时,它会冻结(经检查,CPU的大部分使用率是System,而不是User)。

我只是在Mavericks上开始遇到此问题,所以我想知道这是否与Mavericks的压缩内存或类似问题有关。

(我的最终解决方法是开始在Linux服务器上运行此代码,因此,请考虑它的价值。)

在您的myC函数中

SEXP myC(SEXP j){

难道不应该将“ j”转换为C变量吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM