Parrallel R Note 2
2015-03-10
Motivation
I want to use a Linux cluster to run an R script faster. I am sick of waiting many hours or days for the Monte Carlo simulation to finish.
Monte Carlo simulation
: a computerized mathematical technique that allows people to account for risk in quantitative analysis and decision making. This technique furnishes the decision-maker with a range of possible outcomes and the probabilities they will occur for any choice of action. It shows the extreme possibilities—the outcomes of going for broke and for the most conservative decision—along with all possible consequences for middle-of-the-road decisions.
Solution
Use snow
to run my R code on the company or university’s Linux cluster. snow
fits well with tradtional cluster environment, and is able to take advantage of high-speed communication networks using MPI (if you don’t understand this term, refer to Parallel R note 1).
How it works
snow
provides parallel execution functions. These functions are variations of the standard lapply()
function. snow
uses a master/worker
architecture.
- architecture: master/worker
- master: sends tasks to the workers
- worker: execute the tasks and return the results to the master
- communication mechanism : between master and worker
Important feature : communication mechanism
Different transport mechanisms include socket connection
, MPI
, PVM
, or NetWorkSpaces
. This allows snow
to be portable, but still take advantage of high-performance communication mechanisms if available.
socket
: most portable, no additional packages requiredMPI
: supported via theRmpi
package, popular on Linux clustersPVM
: supported via therpvm
package, popular on multicore, e.g., Windows PCNWS
: supported via thenws
package
Suitable applications
Monte Carlo simulations, bootstrapping, cross validation, ensemble machine learning algorithms, and K-means clustering.
Tip 1: using the rsprng
and rlecuyer
packages for parallel random number generation. This is important for simulations, bootstrapping, and machine learning.
Tip 2: snow
is not designed for dealing with large data, such as distributing data files to the workers. The input arguments must fit into memory and all of the task results are kept in memory on the master until they are returned to the caller in a list.
Tip 3 snow
can be used with high-performance dfs (distributed file system) in order to operate on large data files, but the user has to arrange this.
Setting up
snow
is available on CRAN
(Comprehensive R Archive Network). It is pure R code and installed like any other CRAN
package.
library("snow")
To use snow
with MPI
, I will also need to install the Rmpi
package. However, this is usually quite problematic, because it has an external dependency on MPI. The socket
transport can be used without installing any packages. For the reason that I am new to snow
, I would just play with this communication mechinsim first.
I will first of all check if i have successfully installed the snow
package by
library()
And I do find snow
in the file R packages available
.
Creating clusters
The first thing to do is to create a cluster object. It is used to interact with the cluster workers, and is passed as the first argument to many of the snow
functions.
Tip: I can create different types of cluster objects, depending on the transport mechanism.
library("snow")
cl <- makeCluster(4,type="SOCK")
4
is the cluster specification, and the SOCK
is the cluster type.
The socket
transport lauches each of these workers via the ssh
command unless the name
is localhost
, in which case makeCluster()
starts the worker itself. Tip: For remote execution, I should configure ssh
to use password-less login. This can be done using public-key authentication and SSH agents.
Demo: Parallel K-Means
K-Means
is a unsupervised clustering algorithm that classifies the rows of observations (a dataset) into k clusters1. It starts with a guess of the location for each of the cluster centers, and gradually improves the center locations until it finishes the clustering. This algorithm is included in the stats
package as kmenas()
function.
To load the package MASS
,
library(MASS)
Before I use it for parallel, I try using the lapply()
function to make sure it works. Once this is done, it should be fairly easy to convert to one of the snow
parallel execution functions.
I use a vector of four 25s to specify the nstart
argument in order to get equivalent results to using a 100 in a single call to kmeans(). The length of the vector x
should be equal to the number of workers in the cluster when running in parallel.
x <- rep(25,4)
x
## [1] 25 25 25 25
results <-lapply(x,function(nstart) kmeans(Boston,4,nstart=nstart))
i <- sapply(results, function(result) result$ tot.withinss)
result <-results[[which.min(i)]]
print(result)
## K-means clustering with 4 clusters of sizes 102, 268, 38, 98
##
## Cluster means:
## crim zn indus chas nox rm age
## 1 10.9105113 0.00000 18.572549 0.07843137 0.6712255 5.982265 89.91373
## 2 0.2410479 17.81716 6.668582 0.07462687 0.4833981 6.465448 55.70522
## 3 15.2190382 0.00000 17.926842 0.02631579 0.6737105 6.065500 89.90526
## 4 0.7412906 9.94898 12.983776 0.06122449 0.5822347 6.189847 73.28878
## dis rad tax ptratio black lstat medv
## 1 2.077164 23.019608 668.2059 20.19510 371.80304 17.874020 17.42941
## 2 4.873560 4.313433 276.5485 17.87313 387.81407 9.538022 25.86530
## 3 1.994429 22.500000 644.7368 19.92895 57.78632 20.448684 13.12632
## 4 3.331821 4.826531 406.0816 17.66633 371.66429 12.714898 22.37857
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 2 2 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 2 2 2 2 2 2 2 2 2 2 4 4 3 4 4 4 4 4
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 4 4 4 4 4 4 4 4 4 4 4 3 3 4 4 4 4 4
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 2 2 2 2 2 2 2 4 4 4 4 4 4 2 2 2 2 2
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 2 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 2 2 2 2 2 2 2 2 2 2 4 4 4 2 2 2 2 2
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 2 2 2 2 4 4 4 2 2 2 2 2 2 2 2 2 2 2
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 4 4 4 4 4 2 2 2 2 4 4 2 2 2 1 1 1 1
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 3 3 3 3 3 3 1 1 1 3 3 3 3 3 3 3 3 3
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## 3 3 3 3 3 3 3 1 1 1 1 1 1 3 1 1 1 1
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## 3 1 1 1 3 3 3 3 1 1 1 1 1 1 1 1 3 1
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 2 2 2
## 505 506
## 2 2
##
## Within cluster sum of squares by cluster:
## [1] 181891.7 924118.8 313208.7 395218.3
## (between_SS / total_SS = 90.6 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss"
## [5] "tot.withinss" "betweenss" "size" "iter"
## [9] "ifault"
Now I will parallel this algorithm. let’s look at which functions in snow
can be used.
- clusterApply()
- clusterApplyLB()
- parLapply()
I will use clusterApply()
for this demo. The first argument of this function must be a snow
cluster object. I also need to load MASS
on the workers, rather than on the master, since it is the workers that use the boston
dataset.
library(snow)
cl <- makeCluster(4,type="SOCK")
ignore <- clusterEvalQ(cl,{library(MASS);NULL})
results <-clusterApply(cl,rep(25,4),function(nstart) kmeans(Boston,4,nstart=nstart))
i <- sapply(results, function(result) result$tot.withinss)
result <-results[[which.min(i)]]
print(result)
## K-means clustering with 4 clusters of sizes 98, 268, 102, 38
##
## Cluster means:
## crim zn indus chas nox rm age
## 1 0.7412906 9.94898 12.983776 0.06122449 0.5822347 6.189847 73.28878
## 2 0.2410479 17.81716 6.668582 0.07462687 0.4833981 6.465448 55.70522
## 3 10.9105113 0.00000 18.572549 0.07843137 0.6712255 5.982265 89.91373
## 4 15.2190382 0.00000 17.926842 0.02631579 0.6737105 6.065500 89.90526
## dis rad tax ptratio black lstat medv
## 1 3.331821 4.826531 406.0816 17.66633 371.66429 12.714898 22.37857
## 2 4.873560 4.313433 276.5485 17.87313 387.81407 9.538022 25.86530
## 3 2.077164 23.019608 668.2059 20.19510 371.80304 17.874020 17.42941
## 4 1.994429 22.500000 644.7368 19.92895 57.78632 20.448684 13.12632
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 2 2 2 2 2 2 2 2 2 2 1 1 4 1 1 1 1 1
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 1 1 1 1 1 1 1 1 1 1 1 4 4 1 1 1 1 1
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 2 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 2 2 2 2 2 2 2 2 2 2 1 1 1 2 2 2 2 2
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 1 1 1 1 1 2 2 2 2 1 1 2 2 2 3 3 3 3
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 3 3 3 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 4 4 4 4 4 4 3 3 3 4 4 4 4 4 4 4 4 4
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## 4 4 4 4 4 4 4 3 3 3 3 3 3 4 3 3 3 3
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## 4 3 3 3 4 4 4 4 3 3 3 3 3 3 3 3 4 3
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 2 2 2
## 505 506
## 2 2
##
## Within cluster sum of squares by cluster:
## [1] 395218.3 924118.8 181891.7 313208.7
## (between_SS / total_SS = 90.6 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss"
## [5] "tot.withinss" "betweenss" "size" "iter"
## [9] "ifault"
Now I compare the results above and as I can see, the snow
version isn’t that much different than the lapply
version. The biggest problem in converting from lapply
to one of the parallel operations is handling the data properly and efficently. In this example, it is easy. The dataset was in a package, so all I need to do was to load the package on all workers.
I will quote some coments on the function clusterEvalQ
from the book where I am learning paralle R
“clusterEvalQ() takes two arguments: the cluster object, and an expression that is evaluated on each of the workers. It returns the result from each of the workers in a list, which we don’t use here. I use a compound expression to load MASS and return NULL to avoid sending unnecessary data back to the master process. That isn’t a serious issue in this case, but it can be, so I often return NULL to be safe.” (Parallel R by Q. Ethan McCallum and Stephen Weston (O’Reilly). Copyright 2012 Q. Ethan McCallum and Stephen Weston, 978-1-449-30992-3.)
Don’t confuse these clusters with cluster objects and cluster workers↩