pavel
pavel

Reputation: 377

R mcparallel mapReduce

I am looking for parallel version of aggregate() function and looks like http://cran.r-project.org/web/packages/mapReduce/mapReduce.pdf together with http://cran.r-project.org/web/packages/multicore/multicore.pdf is exactly what I am looking for.

So as a test I've created a dataset with 10m records

blockSize <- 5000
records <- blockSize * 2000
df <- data.frame(id=1:records, value=rnorm(records))
df$period <- round(df$id/blockSize)
# now I want to aggregate by period and return mean of every block:
x <- aggregate(value ~ period, data=df, function(x) { mean(x) })
# with mapReduce it can be done
library(multicore)
library(mapReduce)
jobId <- mcparallel(mapReduce(map=period, mean(value), data=df))
y <- collect(jobId)

but still somehow it doesn't utilise all 4 CPU cores on my laptop:

$ top
02:00:35 up 3 days, 18:14,  3 users,  load average: 1,61, 1,20, 0,79
Tasks: 237 total,   2 running, 235 sleeping,   0 stopped,   0 zombie
%Cpu0  : 17,4 us,  5,1 sy,  0,0 ni, 74,3 id,  0,0 wa,  0,0 hi,  3,2 si,  0,0 st
%Cpu1  : 13,4 us,  6,9 sy,  0,0 ni, 79,7 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
%Cpu2  : 21,3 us, 32,3 sy,  0,0 ni, 46,3 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
%Cpu3  : 17,0 us, 36,0 sy,  0,0 ni, 47,0 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem:   3989664 total,  3298340 used,   691324 free,    27248 buffers
KiB Swap:  7580668 total,  1154164 used,  6426504 free,   320360 cached

PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
459 myuser    20   0 1850m 1,8g 1120 R  **99,1** 46,4   0:37.43 R

I use R 2.15.1:

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i686-pc-linux-gnu (32-bit)

What am I doing wrong and how to aggregate huge datasets in R utilising multicore?

Thanks.

Upvotes: 3

Views: 1003

Answers (1)

mnel
mnel

Reputation: 115390

How do you aggregate huge data sets in R?

Use data.table

library(data.table)


DT <- data.table(df)
setkey(DT, period)

DT[, list(value = mean(value)), by = period]

This will not use multiple cores, but will be extremely fast and memory efficient.

Upvotes: 5

Related Questions