R 多线程和多节点并行计算

一:R本身是单线程的,如何让其多线程跑起来,提高运算速度?

用Parallel和foreach包玩转并行计算

看完上面这篇文章就会了。说白了,要加载parallel包,再改写一下自己的代码就ok了。

#-----用一个实力来演示 R 如何多线程计算
func <- function(x) {
n = 1
raw <- x
while (x > 1) {
x <- ifelse(x%%2==0,x/2,3*x+1)
n = n + 1
}
return(c(raw,n))
}

#----
library(parallel)
# 用system.time来返回计算所需时间
system.time({
x <- 1:1e5
cl <- makeCluster(4) # 初始化四核心集群
results <- parLapply(cl,x,func) # lapply的并行版本
res.df <- do.call('rbind',results) # 整合结果
stopCluster(cl) # 关闭集群
})

用户 系统 流逝 
0.431 0.062 18.954 

对1:100万执行func函数,只用18.954秒就完成了。

#---我把结果用图形展示(见图一),图还挺奇怪的,,,
library(ggplot2)
df=as.data.frame(res.df)
qplot(data=df,x=V1,y=V2)

------------

图一

-----------

图二:看一下CPU使用率,可以看到有四个 R  的线程在跑,CPU使用率瞬间飙升到近100%,心疼我电脑,,,

---------

把parallel用在爬虫程序,下面拿一个爬虫程序测试一下parallel性能如何

需要注意的是需要把加载包这个过程写进函数里,因为每个线程都需要加载包。

getdata <- function(i){
library(magrittr)
library(proto)
library(gsubfn)
library(bitops)
library(rvest)
library(stringr)
library(DBI)
library(RSQLite)
#library(sqldf)
library(RCurl)
#library(ggplot2)
library(sp)
library(raster)
url <- paste0("http://www.cnblogs.com/pick/",i,"/")##generate url
combined_info <- url%>%html_session()%>%html_nodes("div.post_item div.post_item_foot")%>%html_text()%>%strsplit(split="\r\n")
post_date <- sapply(combined_info, function(v) return(v[3]))%>%str_sub(9,24)%>%as.POSIXlt()##get the date
post_year <- post_date$year+1900
post_month <- post_date$mon+1
post_day <- post_date$mday
post_hour <- post_date$hour
post_weekday <- weekdays(post_date)
title <- url%>%html_session()%>%html_nodes("div.post_item h3")%>%html_text()%>%as.character()%>%trim()
link <- url%>%html_session()%>%html_nodes("div.post_item a.titlelnk")%>%html_attr("href")%>%as.character()
author <- url%>%html_session()%>%html_nodes("div.post_item a.lightblue")%>%html_text()%>%as.character()%>%trim()
author_hp <- url%>%html_session()%>%html_nodes("div.post_item a.lightblue")%>%html_attr("href")%>%as.character()
recommendation <- url%>%html_session()%>%html_nodes("div.post_item span.diggnum")%>%html_text()%>%trim()%>%as.numeric()
article_view <- url%>%html_session()%>%html_nodes("div.post_item span.article_view")%>%html_text()%>%str_sub(4,20)
article_view <- gsub(")","",article_view)%>%trim()%>%as.numeric()
article_comment <- url%>%html_session()%>%html_nodes("div.post_item span.article_comment")%>%html_text()%>%str_sub(14,100)
article_comment <- gsub(")","",article_comment)%>%trim()%>%as.numeric()
data.frame(title,recommendation,article_view,article_comment,post_date,post_weekday,post_year,post_month,post_day,post_hour,link,author,author_hp)

}

#--------方法1 循环

df <- data.frame()

system.time({
for(i in 1:73){
df <- rbind(df,getdata(i))
}
})


用户 系统 流逝
21.605 0.938 95.918

#--------方法 2 多线程并行计算
library(parallel)
system.time({
x <- 1:73
cl <- makeCluster(4) # 初始化四核心集群
results <- parLapply(cl,x,getdata) # lapply的并行版本
jinghua <- do.call('rbind',results) # 整合结果
stopCluster(cl) # 关闭集群
})

用户 系统 流逝
0.155 0.122 32.674

显然用parallel快很多,,,

---

爬下来的数据长这样,,,是博客园精华帖的一些信息,,,

------我是分割线-------------------------

二:部署R在linux服务器上

等部署完再写遇到过哪些坑,,,,不过 肖楠:WEB SCRAPING WITH R 这篇文章介绍了种种linux上R的各种好处

Why Linux?

• Network performance & mem. management → Faster 

• Better parallelization support → Faster

• Unied encoding & locale → Faster (for coders)

• More recent third party libs → Faster (less bugs)

 很期待我们的分析环境搭建起来,,,

三:总结

------

要想提高R的运算速度,可从以下几点着手解决

1.抛弃data.frame,拥抱data.table,优化code,,,

2.利用R本身的parallel,多线程计算,提高CPU利用率,,

3.上一个强大的服务器,16核128G啊,这种暴强的超级计算机,,

4.上多个巨型机进行集群,Rhadoop,SparkR,,,

-------

SparkR最新进展,备查,。

Announcing SparkR: R on Spark

SparkR github

SparkR (R on Spark)

Documentation for package ‘SparkR’ version 1.4.1

SparkR 技术,听起来很炫,其实还有很多路要走,,,曾配合Transwrap的工程师 对SparkR环境进行功能测试,结果是:要想把本地的R代码正常的运行在SparkR环境下,需大量改动代码,因为R code和sparkR环境的R code是不一样的,spark的数据结构是RDD(RDD 全称为Resilient Distributed Datasets,是一个容错的、并行的数据结构,可以让用户显式地将数据存储到磁盘和内存中,并能控制数据的分区。)Announcing SparkR: R on Spark  刚发布说1.41版本,要支持data.frame了,期待SparkR变得更好用,,,

-------

下面放一些R会的deck备查。

第七届中国R语言会议(北京)纪要【含演讲资料】

第七届中国R语言会议(广州)纪要【含演讲资料】

第六届中国R语言会议(北京)纪要

第六届中国R语言会议(上海)纪要

第五届中国R语言会议(上海会场)纪要

原文地址:https://www.cnblogs.com/litao1105/p/4605165.html