在单插槽,多核CPU上的Windows中与R并行处理

时间:2018-06-27 22:34:40

标签: r parallel-processing

我一直在尝试在Windows中进行并行处理。 doSNOW声明它制作了“ SOCK”类型的集群,也称为“套接字集群”;我有一个具有8个物理和8个逻辑核心的cpu,我经常在linux中使用它。我一直在尝试运行下面的代码,但是每当尝试%dopar%选项时,它都会为NULL。所以我想知道Windows是否只能与多插槽计算机(我没有)并行工作

library(jsonlite)
library(doSNOW)
library(foreach)
library(dplyr)

claves <- c("I11D67", "I11D66", "I11D65", "I11D64", "I11D63", "I11D62", 
"I11D61", "I11C69", "I11D79", "I11D78", "I11D77", "I11D76", "I11D75", 
"I11D74", "I11D73", "I11D72", "I11D71", "I11C79", "I12C82", "I12C81", 
"I11D89", "I11D88", "I11D87", "I11D86", "I11D85")

cl <- makeCluster(8)
registerDoSNOW(cl)

foreach(i = claves, .combine=rbind, .errorhandling = "remove") %do% { 
url <- sprintf("http://www.beta.inegi.org.mx/app/api/buscador/busquedaTodos/%s_A/RANKING/es", i)
ea <- fromJSON(url)
ea  %>% select(TITULO, CVE_UNICA, EDICION)
} -> results

stopCluster(cl)

1 个答案:

答案 0 :(得分:1)

这不是您的硬件问题。这是以下两件事之一或全部:

  1. SNOW在Windows上运行时遇到问题。您想用doParallel或类似名称代替。
  2. 使用::确保您的代码未命中dplyr::select,例如MASS:select

这有效(带有一些不相关的警告):

if(!require(doParallel)) install.packages("doParallel")
library(foreach)
library(doParallel)
workers <- makeCluster(2) # start small to test
registerDoParallel(workers)

foreach(i = claves, .combine=rbind, .errorhandling = "remove") %do% { 
  url <- sprintf("http://www.beta.inegi.org.mx/app/api/buscador/busquedaTodos/%s_A/RANKING/es", i)
  ea <- fromJSON(url)
  ea  %>% dplyr::select(TITULO, CVE_UNICA, EDICION)
} -> results

stopCluster(cl)



dim(results)
[1] 50 39


results$CVE_UNICA
 [1] "889463489610" "702825634490" "702825271480" "702825637149" "702825267483" "889463489603" "702825213497" "702825709792"
"702825277420" "702825711115"
[11] "702825267476" "702825637101" "702825272128" "889463489597" "702825266967" "702825709327" "702825215576" "702825634582"
"702825214098" "702825709754"
[21] "702825271510" "702825003959" "702825271503" "702825208332" "702825213510" "702825709747" "702825213503" "702825709815"
     

“ 702825267506”“ 889463489627”       [31]“ 702825267490”“ 702825637170”“ 702825271497”“ 702825711269”“ 702825268114”“ 889463489580”“ 702825219352”“ 702825708375”   “ 702825215590”“ 702825708382”       [41]“ 702825215583”“ 702825637330”“ 702825214104”“ 702825649418”“ 702825203894”“ 702825003962”“ 702825203887”“ 702825712617”   “ 702825212421”“ 702825637293”

请参见此source,以获取几个很好的示例。