【问题标题】:What are the caveats for calling other packages inside doMC's foreach and dopar?在 doMC 的 foreach 和 dopar 中调用其他包有什么注意事项?
【发布时间】:2017-05-03 21:49:07
【问题描述】:

此代码按预期工作:

library(dplyr)
data <- list(t1 = "hello world.", t2 = "bye world")

library(doMC)
registerDoMC(3)

res <- foreach(t = data) %dopar% {

    print(sprintf("processing %s", t))

    data.frame(text = t) %>%
    dplyr::count(text)

}

print(res)

但是,此代码仅打印“处理 hello world”。和“处理再见世界”,然后挂起(不抛出异常)。

library(dplyr)
coreNLP::initCoreNLP()

data <- list(t1 = "hello world.", t2 = "bye world")

library(doMC)
registerDoMC(3)

res <- foreach(t = data) %dopar% {

    print(sprintf("processing %s", t))

    coreNLP::annotateString(t)$token

}

print(res)

如果我将%dopar% 更改为%do%,上面的代码将按预期工作。

我不明白是什么导致了这种行为。为什么在 %dopar% 中调用 coreNLP 函数会导致 R 挂起,但可以与其他包一起正常工作?这和coreNLP对Java的依赖有关系吗?

这是sessionInfo()的输出:

R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.0

【问题讨论】:

    标签: r parallel-processing stanford-nlp domc


    【解决方案1】:

    您的第一个示例在看起来类似的设置上对我来说效果很好。运行示例后我的会话信息如下;确保使用新的 R 会话重试 (R --vanilla)。我有四个核心(来自parallel::detectCores())。

    sessionInfo()
    R version 3.4.0 (2017-04-21)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 16.04.2 LTS
    
    Matrix products: default
    BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
    LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0
    
    locale:
     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
     [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] parallel  stats     graphics  grDevices utils     datasets  methods  
    [8] base     
    
    other attached packages:
    [1] doMC_1.3.4      iterators_1.0.8 foreach_1.4.3   dplyr_0.5.0    
    
    loaded via a namespace (and not attached):
    [1] compiler_3.4.0   magrittr_1.5     R6_2.2.0         assertthat_0.2.0
    [5] DBI_0.6-1        tibble_1.3.0     Rcpp_0.12.10     codetools_0.2-15
    

    你的第二个例子对我也有用。输出如下。我的猜测是,分叉的进程可能共享 coreNLP 所依赖的相同底层 Java 进程/服务;不太了解 coreNLP。

    > res <- foreach(t = data) %dopar% {
    + 
    +     print(sprintf("processing %s", t))
    + 
    +     coreNLP::annotateString(t)$token
    + 
    + }
    [1] "processing hello world."
    [1] "processing bye world"
    
    
    ^CError in selectChildren(ac, 1) : 
      Java called System.exit(130) requesting R to quit - trying to recover
    Error during wrapup: C stack usage  591577121812 is too close to the limit
    
     *** caught segfault ***
    address 0x2, cause 'memory not mapped'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多