Giter Club home page Giter Club logo

parallel-speed-test-r's Introduction

parallel-speed-test-R

Test the speed of your machine using a different number of cores for parallel processing

=====================

Libraries:

  • doParallel (from Revolution Analytics for parallelization)
  • tcltk2 (to build a progress bar to monitor the progress)

This code was inspired by the "Hyperthreading FTW? Testing parallelization performance in R" post from the Rcrastinate blog.

My primary additions were the progress bar from the tcltk2 package and two ggplots at the end.

=====================

Progress Bar: Primary Components

Progress Bar

Initialize with the progress bar with a title and parameters for the smallest and largest values to expect

# Test Parameters
runs <- 5 # number of times to repeat experiment
obs_list <- c(50000, 100000, 150000, 200000) # num obs to generate
cores_list <-c(1,2,3,4,5,6,7,8) # number of cores to use

# total iterations to make
total <- runs * length(obs_list) * length(cores_list)

# Launch Progress Bar
pb <- tkProgressBar(title = "Parallel Processing Test", min = 0,max = total, width = 1000)

In the middle of the for loop, update the progress bar

setTkProgressBar(pb, grand_iterator, 
                   label=paste("Elapsed Time:",round(elapsed_time,1),
                               "minutes",
                               round(grand_iterator/total*100, 1),
                               "% Complete --- ",
                               "Currently generating",
                               prettyNum(cases,big.mark=",",scientific=F),
                               "random numbers using", cores, "cores")

Notice how overzelous you can get on the text you want to display in the progress bar. I choose to fill it full of info for initial debugging purposes to see where the algoritm gets stuck.

The last step is to close the progress bar when you are done.

close(pb)

=====================

Parallelization: Primary Components

Determine the number of clusters to use. The cores variable is a user defined choice. My machine has four cores, with hyperthreading, so it continue to get gains up to 8 cores.

  cluster <- makeCluster(cores)
  registerDoParallel(cluster)

Execute your function with a foreach loop from the doParallel package, and put the %DOPAR% before the brackets.

  result.vec <- foreach(i = 1:100, .combine=c) %dopar% {
    rnorm(cases, mean = 10, sd = 30)
  }

Before running another iteration of the loop with a different value for the number of cores, reset the cores option.

stopCluster(cluster)

=====================

Results of the experiment:

One way to view the data, via a scatter of boxplots that don't add a ton of clarity, but they're pretty.

Box Plot

Another way to view the same results, which comes out a bit clearer

Line Chart

parallel-speed-test-r's People

Contributors

benporter avatar

Stargazers

Liling Xu avatar Ben avatar Jan Stanstrup avatar

Watchers

James Cloos avatar  avatar

Forkers

kx11jkjk

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.