在Windows XP上的R中进行多核处理-通过doMC和foreach [英] multi-core processing in R on windows XP - via doMC and foreach

查看:80
本文介绍了在Windows XP上的R中进行多核处理-通过doMC和foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发布此问题,以寻求有关如何优化Windows XP计算机上R中多个处理器使用的建议.

I'm posting this question to ask for advice on how to optimize the use of multiple processors from R on a Windows XP machine.

目前,我正在创建4个脚本(每个脚本都带有例如(1:100中的i)和(i:101:200中的i)等),它们同时在4个不同的R会话中运行.这似乎会使用所有可用的cpu.

At the moment I'm creating 4 scripts (each script with e.g. for (i in 1:100) and (i in 101:200), etc) which I run in 4 different R sessions at the same time. This seems to use all the available cpu.

但是,我想这样做更有效率.一种解决方案是使用"doMC"和"foreach"软件包,但这在Windows计算机上的R中是不可能的.

I however would like to do this a bit more efficient. One solution could be to use the "doMC" and the "foreach" package but this is not possible in R on a Windows machine.

例如

library("foreach")
library("strucchange")
library("doMC") # would this be possible on a windows machine?
registerDoMC(2)  # for a computer with two cores (processors)
## Nile data with one breakpoint: the annual flows drop in 1898
## because the first Ashwan dam was built
data("Nile")
plot(Nile)

## F statistics indicate one breakpoint
fs.nile <- Fstats(Nile ~ 1)
plot(fs.nile)
breakpoints(fs.nile)     # , hpc = "foreach" --> It would be great to test this.
lines(breakpoints(fs.nile))

有解决方案或建议吗?

推荐答案

为完整起见,以下是对Tal评论的要求答案,它提供了一种简单且可移植的替代方法.答案包括跑步

For completeness, here is the requested answer to Tal's comment which provides a simple and portable alternative. The answer consists of running

 > library(snow)
 > help(makeCluster)

并从示例顶部开始运行前三行代码:

and running the first three lines of code from the top of the Examples: section:

> cl <- makeCluster(c("localhost","localhost"), type = "SOCK")
> clusterApply(cl, 1:2, get("+"), 3)
[[1]]
[1] 4

[[2]]
[1] 5

> stopCluster(cl)
> .Platform$OS.type
[1] "windows"
> 

真的那么难吗?

Was that really that hard?

诸如 doSNOW 之类的附加软件包 foreach 可以通过便携式方式使用.

Add-on packages like doSNOW and thereafter foreach can make use of this in a portable way.

这篇关于在Windows XP上的R中进行多核处理-通过doMC和foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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