tests/testthat/test-parallel.R

context("Parallelization")
library(parallel)
library(microbenchmark)

test_that("parLapply is at least as efficient as boot.strength", {
  boot_iteration <- function(i, data){
    require(bnlearn)
    resampling <- sample(nrow(data), nrow(data), replace = TRUE)
    hc(data)
  }
  data(learning.test)
  nearest10 <- function(x) 10^ceiling(log10(x))
  cl <- makeCluster(4)
  time1 <- summary(microbenchmark(boot.strength(learning.test, cluster = cl,
                                        R = 200, algorithm = "hc"),
                          times = 30, unit = "s"))$mean
  cl <- makeCluster(4)
  time2 <- summary(microbenchmark(parLapply(cl, 1:200, boot_iteration, data = learning.test),
                          times = 30, unit = "s"))$mean
  expect_true(nearest10(time2) <= nearest10(time1))
})

test_that("parLapply can be used with starting nets"{
  boot_iteration <- function(net, data){
    require(bnlearn)
    resampling <- sample(nrow(data), nrow(data), replace = TRUE)
    hc(data, start = net)
  }
  nets <- random.graph(names(learning.test), num = 200,
                       method = "ic-dag", every = 100)
  cl <- makeCluster(4)
  out <- parLapply(cl, nets, boot_iteration, data = learning.test)
})
robertness/bninfo documentation built on May 27, 2019, 10:32 a.m.