From 182490d92d2dd5da7b56aa1f51687229a382a43e Mon Sep 17 00:00:00 2001 From: boennecd Date: Sat, 19 Jan 2019 10:48:46 +0100 Subject: [PATCH 1/2] fix issue 2 --- R/parglm.R | 2 +- tests/testthat/test_misc.R | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test_misc.R diff --git a/R/parglm.R b/R/parglm.R index 5847ac7..03efd0a 100644 --- a/R/parglm.R +++ b/R/parglm.R @@ -66,7 +66,7 @@ parglm <- function( contrasts = NULL, model = TRUE, x = FALSE, y = TRUE, ...){ cl <- match.call() cl[[1L]] <- quote(glm) - cl[c("method", "singular.ok")] <- list(quote(parglm.fit), FALSE) + cl[c("method", "singular.ok")] <- list(quote(parglm::parglm.fit), FALSE) eval(cl, parent.frame()) } diff --git a/tests/testthat/test_misc.R b/tests/testthat/test_misc.R new file mode 100644 index 0000000..4dc4e33 --- /dev/null +++ b/tests/testthat/test_misc.R @@ -0,0 +1,14 @@ +context("Miscellaneous tests") + +test_that("'parglm' works when package is not attached",{ + # Issue: https://github.com/boennecd/parglm/issues/2#issue-397286510 + # See https://github.com/r-lib/devtools/issues/1797#issuecomment-423288947 + + expect_silent( + local({ + detach("package:parglm", unload = TRUE, force = TRUE) + parglm::parglm(mpg ~ gear , data = datasets::mtcars) + library(parglm) + }, + envir= new.env(parent = environment(glm)))) +}) From bd22d86c400d3b06b6f877ea2ad672835af7748f Mon Sep 17 00:00:00 2001 From: boennecd Date: Sat, 19 Jan 2019 11:13:25 +0100 Subject: [PATCH 2/2] fix issue 3 --- R/parglm.R | 15 +++++++++++++++ tests/testthat/test_misc.R | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/R/parglm.R b/R/parglm.R index 03efd0a..089c736 100644 --- a/R/parglm.R +++ b/R/parglm.R @@ -170,6 +170,21 @@ parglm.fit <- function( if (is.null(offset)) offset <- rep.int(0, nobs) + n_min_per_thread <- 10L + n_per_thread <- nrow(x) / control$nthreads + if(n_per_thread < n_min_per_thread){ + nthreads_new <- nrow(x) %/% n_min_per_thread + if(nthreads_new < 1L) + nthreads_new <- 1L + + warning( + "Too few observation compared to the number of threads. ", + nthreads_new, " thread(s) will be used instead of ", + control$nthreads, ".") + + control$nthreads <- nthreads_new + } + block_size <- if(!is.null(control$block_size)) control$block_size else if(control$nthreads > 1L) diff --git a/tests/testthat/test_misc.R b/tests/testthat/test_misc.R index 4dc4e33..9680424 100644 --- a/tests/testthat/test_misc.R +++ b/tests/testthat/test_misc.R @@ -12,3 +12,21 @@ test_that("'parglm' works when package is not attached",{ }, envir= new.env(parent = environment(glm)))) }) + +test_that("Using more threads then rows yields a warning", { + # Issue: https://github.com/boennecd/parglm/issues/3#issue-399052270 + + this_df <- data.frame( a = sample( 1:1000000 , 20 ) / 100 , b = 1 ) + expect_warning( + parglm( a ~ b - 1, data = this_df , nthreads = 64), + regexp = "Too few observation compared to the number of threads. 2 thread(s) will be used instead of 64.", + fixed = TRUE) + + # should yield one thread (the number of rows is less than the number required + # per thread) + this_df <- data.frame( a = sample( 1:1000000 , 5 ) / 100 , b = 1 ) + expect_warning( + parglm( a ~ b - 1, data = this_df , nthreads = 64), + regexp = "Too few observation compared to the number of threads. 1 thread(s) will be used instead of 64.", + fixed = TRUE) +})