Created
February 15, 2011 00:03
-
-
Save dsparks/826841 to your computer and use it in GitHub Desktop.
Example of parallel vectorized functions using snow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(snow) | |
VariabletoExport <- 5 | |
TheFunction <- function(x){ | |
TimeWaster <- mean(rnorm(100000, 0, 1)) | |
x^2 + TimeWaster + VariabletoExport | |
} | |
FunctionInput <- 1:1000 | |
### Unparallelized ### | |
StartTime <- Sys.time() | |
FunctionOutput <- lapply(FunctionInput, TheFunction) | |
ProcessTime <- Sys.time() - StartTime | |
ProcessTime | |
### Parallelized ### | |
StartTime <- Sys.time() | |
TheCluster <- makeCluster(8, type = "SOCK") | |
clusterSetupRNG(TheCluster, type = "RNGstream") | |
clusterExport(TheCluster, c("VariabletoExport")) | |
FunctionOutput <- parSapply(TheCluster, FunctionInput, TheFunction) | |
stopCluster(TheCluster) | |
ProcessTime <- Sys.time() - StartTime | |
ProcessTime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also: http://mockquant.blogspot.com/2011/02/simple-examplehow-to-use-foreach-and.html