-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
test-threads.R
184 lines (163 loc) · 6.72 KB
/
test-threads.R
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
context("threading")
set_cmdstan_path()
stan_program <- testing_stan_file("bernoulli")
stan_gq_program <- testing_stan_file("bernoulli_ppc")
data_file_gq_json <- testing_data("bernoulli_ppc")
data_file_json <- test_path("resources", "data", "bernoulli.data.json")
test_that("using threads_per_chain without stan_threads set in compile() warns", {
mod <- cmdstan_model(stan_program)
expect_warning(
expect_output(
mod$sample(data = data_file_json, threads_per_chain = 4),
"Running MCMC with 4 sequential chains",
fixed = TRUE
),
"'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!",
fixed = TRUE)
})
test_that("threading works with sample()", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = TRUE), force_recompile = TRUE)
expect_error(
mod$sample(data = data_file_json),
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' but 'threads_per_chain' was not set!",
fixed = TRUE
)
expect_output(
f <- mod$sample(data = data_file_json, parallel_chains = 4, threads_per_chain = 1),
"Running MCMC with 4 parallel chains, with 1 thread(s) per chain..",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 1)
expect_equal(f$metadata()$threads_per_chain, 1)
expect_output(
f <- mod$sample(data = data_file_json, parallel_chains = 4, threads_per_chain = 2),
"Running MCMC with 4 parallel chains, with 2 thread(s) per chain..",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 2)
expect_equal(f$metadata()$threads_per_chain, 2)
expect_output(
f <- mod$sample(data = data_file_json, parallel_chains = 4, threads_per_chain = 4),
"Running MCMC with 4 parallel chains, with 4 thread(s) per chain..",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 4)
expect_equal(f$metadata()$threads_per_chain, 4)
})
test_that("threading works with optimize()", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = TRUE), force_recompile = TRUE)
expect_error(
mod$optimize(data = data_file_json),
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' but 'threads' was not set!",
fixed = TRUE
)
expect_output(
f <- mod$optimize(data = data_file_json, threads = 1, seed = 123),
"Optimization terminated normally",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 1)
expect_equal(f$metadata()$threads, 1)
expect_output(
f <- mod$optimize(data = data_file_json, threads = 2, seed = 123),
"Optimization terminated normally",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 2)
expect_equal(f$metadata()$threads, 2)
expect_output(
f <- mod$optimize(data = data_file_json, threads = 4, seed = 123),
"Optimization terminated normally",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 4)
expect_equal(f$metadata()$threads, 4)
})
test_that("threading works with variational()", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = TRUE), force_recompile = TRUE)
expect_error(
mod$variational(data = data_file_json),
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' but 'threads' was not set!",
fixed = TRUE
)
expect_output(
f <- mod$variational(data = data_file_json, threads = 1, seed = 123),
"EXPERIMENTAL ALGORITHM",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 1)
expect_equal(f$metadata()$threads, 1)
expect_output(
f <- mod$variational(data = data_file_json, threads = 2, seed = 123),
"EXPERIMENTAL ALGORITHM",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 2)
expect_equal(f$metadata()$threads, 2)
expect_output(
f <- mod$variational(data = data_file_json, threads = 4, seed = 123),
"EXPERIMENTAL ALGORITHM",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 4)
expect_equal(f$metadata()$threads, 4)
})
test_that("threading works with generate_quantities()", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = TRUE), force_recompile = TRUE)
mod_gq <- cmdstan_model(stan_gq_program, cpp_options = list(stan_threads = TRUE), force_recompile = TRUE)
expect_output(
f <- mod$sample(data = data_file_json, parallel_chains = 4, threads_per_chain = 1),
"Running MCMC with 4 parallel chains, with 1 thread(s) per chain..",
fixed = TRUE
)
expect_error(
mod_gq$generate_quantities(fitted_params = f, data = data_file_json),
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' but 'threads_per_chain' was not set!",
fixed = TRUE
)
expect_output(
f_gq <- mod_gq$generate_quantities(fitted_params = f, data = data_file_gq_json, threads_per_chain = 1, seed = 123),
"Running standalone generated quantities after 4 MCMC chains",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 1)
expect_equal(f_gq$metadata()$threads_per_chain, 1)
expect_output(
f_gq <- mod_gq$generate_quantities(fitted_params = f, data = data_file_gq_json, threads_per_chain = 2, seed = 123),
"Running standalone generated quantities after 4 MCMC chains",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 2)
expect_equal(f_gq$metadata()$threads_per_chain, 2)
expect_output(
f_gq <- mod_gq$generate_quantities(fitted_params = f, data = data_file_gq_json, threads_per_chain = 4, seed = 123),
"Running standalone generated quantities after 4 MCMC chains",
fixed = TRUE
)
expect_equal(as.integer(Sys.getenv("STAN_NUM_THREADS")), 4)
expect_equal(f_gq$metadata()$threads_per_chain, 4)
})
test_that("correct output when stan_threads not TRUE", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE)
expect_output(
mod$sample(data = data_file_json),
"Running MCMC with 4 sequential chains",
fixed = TRUE
)
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = "dummy string"), force_recompile = TRUE)
expect_output(
mod$sample(data = data_file_json),
"Running MCMC with 4 sequential chains",
fixed = TRUE
)
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE)
expect_output(
expect_warning(
mod$sample(data = data_file_json, threads_per_chain = 4),
"'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!",
fixed = TRUE
),
"Running MCMC with 4 sequential chains",
fixed = TRUE
)
})