2017-05-03 34 views
0

此代碼按預期工作:在doMC的foreach和dopar中調用其他包的注意事項是什麼?

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) 

然而,這個代碼只是打印「處理世界你好」。和「處理再見世界」,然後掛起(沒有拋出異常)。上述

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 

回答

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' 
相關問題