Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/boennecd/parglm
Browse files Browse the repository at this point in the history
  • Loading branch information
boennecd committed Jan 19, 2019
2 parents 3d2c6cc + bd22d86 commit c4a282a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
17 changes: 16 additions & 1 deletion R/parglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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))))
})

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)
})

0 comments on commit c4a282a

Please sign in to comment.