Guest User

Untitled

a guest
Feb 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. as <- runif(30) # limit for
  2. bs <- runif(30)
  3. cs <- runif(30) # distribution of food sources ('random'/'clustered')
  4.  
  5. p_space <- list ()
  6. for (a in as){
  7. for (b in bs){
  8. for (c in cs){
  9. p_space[[length(p_space)+1]] <- c(a,b,c)
  10. }
  11. }
  12. }
  13. made_up <- function(pvec){
  14. a <- pvec[1]
  15. b <- pvec[2]
  16. c <- pvec[3]
  17. for(i in 1:10000){
  18. d <- a*b*c
  19. }
  20. return(list(a,b,c,d))
  21. }
  22. system.time({
  23. z <- lapply(p_space,made_up)
  24. results1 <- data.frame(matrix(unlist(z),ncol = 4, byrow = T))
  25. colnames(results1) <- c('a','b','c','product')
  26.  
  27. })
  28.  
  29. user system elapsed
  30. 21.23 0.01 21.26
  31.  
  32. library(parallel)
  33. no_cores <- detectCores()
  34. cl <- makeCluster(no_cores)
  35. clusterExport(cl, c("p_space"))
  36. # clusterEvalQ() #for libraries on all nodes
  37. system.time ({
  38. z <- parLapply(cl, p_space, made_up)
  39. results <- data.frame(matrix(unlist(z),ncol = 4, byrow = T))
  40. colnames(results) <- c('a','b','c','product')
  41. })
  42.  
  43. user system elapsed
  44. 0.02 0.02 10.86
Add Comment
Please, Sign In to add comment