File: test_reproducibility.R

package info (click to toggle)
r-cran-parallelmap 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: sh: 13; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
context("reproducibility")

test_that("reproducibility with standard RNG kind works for 'socket' mode", {
  set.seed(42)
  parallelStartSocket(cpus = 2)
  foo1 <- parallelMap(runif, rep(3, 2))
  parallelStop()

  set.seed(42)
  parallelStartSocket(cpus = 2)
  foo2 <- parallelMap(runif, rep(3, 2))
  parallelStop()

  expect_equal(foo1, foo2)
})

test_that("socket: reproducibility can be turned off", {
  set.seed(42)
  parallelStartSocket(cpus = 2, reproducible = FALSE)
  foo1 <- parallelMap(runif, rep(3, 2))
  parallelStop()

  set.seed(42)
  parallelStartSocket(cpus = 2, reproducible = FALSE)
  foo2 <- parallelMap(runif, rep(3, 2))
  parallelStop()

  # would be class "logical" if TRUE
  expect_is(all.equal(foo1, foo2), "character")
})

test_that("reproducibility with standard RNG kind works for 'multicore' mode", {
  skip_on_os("windows")

  set.seed(42)
  parallelStartMulticore(2)
  foo3 = parallelMap(rnorm, 1:2)
  parallelStop()

  set.seed(42)
  parallelStartMulticore(2)
  foo4 = parallelMap(rnorm, 1:2)
  parallelStop()

  expect_equal(foo3, foo4)
})

# This is how it was before v1.5 -> set.seed() with standard RNG kind had no
# effect
test_that("multicore: reproducibility with standard RNG kind can be turned off", {
  skip_on_os("windows")

  set.seed(42)
  parallelStartMulticore(2, reproducible = FALSE)
  foo3 = parallelMap(rnorm, 1:2)
  parallelStop()

  set.seed(42)
  parallelStartMulticore(2, reproducible = FALSE)
  foo4 = parallelMap(rnorm, 1:2)
  parallelStop()

  # would be class "logical" if TRUE
  expect_is(all.equal(foo3, foo4), "character")
})

test_that("reproducibility with L'Ecuyer-CMRG RNG kind works for 'multicore' mode", {
  skip_on_os("windows")

  # NB: this RNG kind should always work, even without reproducible = TRUE
  set.seed(42, "L'Ecuyer-CMRG")
  parallelStartMulticore(2)
  foo5 = parallelMap(rnorm, 1:2)
  parallelStop()

  set.seed(42, "L'Ecuyer-CMRG")
  parallelStartMulticore(2)
  foo6 = parallelMap(rnorm, 1:2)
  parallelStop()

  expect_equal(foo5, foo6)
})

# I do not think we can do anything here:
# 1. This RNG kind always forces reproducibility in parallel processes,
# even when mc.set.seed = FALSE is FALSE
test_that("reproducibility with L'Ecuyer-CMRG RNG kind works even when 'reproducible = FALSE'", {
  skip_on_os("windows")

  # NB: this RNG kind should always work, even without reproducible = TRUE
  set.seed(42, "L'Ecuyer-CMRG")
  parallelStartMulticore(2, reproducible = FALSE)
  foo5 = parallelMap(rnorm, 1:2)
  parallelStop()

  set.seed(42, "L'Ecuyer-CMRG")
  parallelStartMulticore(2, reproducible = FALSE)
  foo6 = parallelMap(rnorm, 1:2)
  parallelStop()

  expect_equal(foo5, foo6)
})