Giter Club home page Giter Club logo

lauraeparallel's Introduction

LauraeParallel: Laurae's R package for Parallel Load Balancing

This R package is meant to be used with the parallel package in order to speed up long and dynamic optimization computations. It attempts to simulate guided/dynamic OpenMP scheduling, which makes it a very good scheduler for slow and dynamic functions, but very bad for very fast and constant (in time) functions.

It can be used with LauraeCE R optimization package.

Useful also if you need timeout on functions.

Installation:

devtools::install_github("Laurae2/LauraeParallel")

Example

This is how it currently looks:

> library(parallel)
> cl <- makeCluster(2)
> system.time({parLapply(cl, 1:6, function(x) {
+     Sys.sleep(x)
+     return(x)
+ })})
   user  system elapsed 
   0.43    0.11   15.10 
> system.time({LauraeLapply(cl, 1:6, function(x) {
+     Sys.sleep(x)
+     return(x)
+ })})
   user  system elapsed 
   0.00    0.00   12.02 

With "ERROR" on timeout but keep running:

> library(LauraeParallel)
> library(parallel)
> library(R.utils)
> 
> cl <- makeCluster(2)
> 
> my_fun <- function(x) {
+   Sys.sleep(x)
+   return(x)
+ }
> invisible(clusterEvalQ(cl = cl, expr = {
+   library(R.utils)
+ }))
> clusterExport(cl = cl, "my_fun")
> 
> system.time({data <- LauraeLapply(cl, (1:6) * 0.1, function(x) {my_fun(x)})})
   user  system elapsed 
   0.00    0.02    1.26 
> data
[[1]]
[1] 0.1

[[2]]
[1] 0.2

[[3]]
[1] 0.3

[[4]]
[1] 0.4

[[5]]
[1] 0.5

[[6]]
[1] 0.6

> 
> system.time({data <- LauraeLapply(cl, (1:6) * 0.1, function(x) {
+   err <- try(withTimeout(my_fun(x), timeout = 0.3, onTimeout = "error"))
+   if (class(err) == "try-error") {
+     return("ERROR")
+   } else {
+     return(err)
+   }
+ })})
   user  system elapsed 
   0.00    0.00    1.15 
> data
[[1]]
[1] 0.1

[[2]]
[1] 0.2

[[3]]
[1] "ERROR"

[[4]]
[1] "ERROR"

[[5]]
[1] "ERROR"

[[6]]
[1] "ERROR"

> 
> stopCluster(cl)

lauraeparallel's People

Contributors

laurae2 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.