forked from t-kalinowski/quickr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-openmp-utils.R
More file actions
257 lines (214 loc) · 6.57 KB
/
Copy pathtest-openmp-utils.R
File metadata and controls
257 lines (214 loc) · 6.57 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
test_that("openmp_makevars_lines uses explicit env flags", {
withr::local_envvar(c(
QUICKR_OPENMP_FFLAGS = "-fopenmp",
QUICKR_OPENMP_LIBS = "-fopenmp"
))
expect_equal(
quickr:::openmp_makevars_lines(),
c("PKG_FFLAGS += -fopenmp", "PKG_LIBS += -fopenmp")
)
})
test_that("openmp_makevars_lines errors when flags are missing", {
local_mocked_bindings(
openmp_fflags = function() "",
.package = "quickr"
)
expect_error(
quickr:::openmp_makevars_lines(),
"OpenMP was requested but no OpenMP flags",
class = "quickr_openmp_unavailable"
)
})
test_that("openmp_makevars_lines errors when linker flags are missing", {
local_mocked_bindings(
openmp_fflags = function() "-fopenmp",
openmp_link_flags = function(...) "",
.package = "quickr"
)
expect_error(
quickr:::openmp_makevars_lines(),
"OpenMP was requested but no OpenMP linker flags",
class = "quickr_openmp_unavailable"
)
})
test_that("openmp_config_value caches toolchain lookups", {
cache_env <- environment(quickr:::openmp_config_value)
old_cache <- cache_env$cached
old_config <- cache_env$quickr_r_cmd_config_value
withr::defer(cache_env$cached <- old_cache)
withr::defer({
if (is.null(old_config)) {
rm(quickr_r_cmd_config_value, envir = cache_env)
} else {
cache_env$quickr_r_cmd_config_value <- old_config
}
})
cache_env$cached <- NULL
calls <- 0
cache_env$quickr_r_cmd_config_value <- function(...) {
calls <<- calls + 1
"value"
}
expect_equal(quickr:::openmp_config_value("QUICKR_TEST_CACHE"), "value")
expect_equal(quickr:::openmp_config_value("QUICKR_TEST_CACHE"), "value")
expect_equal(calls, 1)
})
test_that("get_pending_parallel returns NULL for NULL or non-scope", {
expect_null(quickr:::get_pending_parallel(NULL))
expect_null(quickr:::get_pending_parallel(list()))
})
test_that("take_pending_parallel returns NULL for NULL or non-scope", {
expect_null(quickr:::take_pending_parallel(NULL))
expect_null(quickr:::take_pending_parallel(list()))
})
test_that("parse_parallel_decl errors on unknown arguments", {
e <- quote(parallel(foo))
expect_error(
quickr:::parse_parallel_decl(e),
"unknown argument to parallel"
)
})
test_that("parse_parallel_decl parses private argument", {
e <- quote(parallel(private = c(x, y)))
result <- quickr:::parse_parallel_decl(e)
expect_equal(result$backend, "omp")
expect_equal(result$private, c("x", "y"))
# Single symbol
e2 <- quote(parallel(private = z))
result2 <- quickr:::parse_parallel_decl(e2)
expect_equal(result2$private, "z")
})
test_that("parse_parallel_decl errors on invalid private values", {
expect_error(
quickr:::parse_parallel_decl(quote(parallel(private = 1))),
"private must be a symbol or c\\(\\) of symbols"
)
expect_error(
quickr:::parse_parallel_decl(quote(parallel(private = c(x, 1)))),
"private must be a symbol or c\\(\\) of symbols"
)
})
test_that("parallel private validates declared symbols (rolling mean)", {
fn <- function(x, window) {
declare(
type(x = double(n)),
type(window = double(j))
)
out <- double(length(x) - length(window) + 1)
window_size <- as.double(length(window))
declare(parallel(private = c(acc, j)))
for (i in seq_along(out)) {
acc <- 0
for (j in seq_along(window)) {
acc <- acc + x[(i + j - 1)] * window[j]
}
out[i] <- acc * window_size
}
out
}
skip_if_no_openmp()
expect_quick_identical(
fn,
list(x = as.double(1:10), window = as.double(1:3))
)
})
test_that("parallel private errors on undeclared symbols (rolling mean)", {
fn <- function(x, window) {
declare(
type(x = double(n)),
type(window = double(j))
)
out <- double(length(x) - length(window) + 1)
window_size <- as.double(length(window))
declare(parallel(private = c(qwerty, j)))
for (i in seq_along(out)) {
acc <- 0
for (j in seq_along(window)) {
acc <- acc + x[(i + j - 1)] * window[j]
}
out[i] <- acc * window_size
}
out
}
expect_error(
r2f(fn),
"could not resolve private symbol"
)
})
test_that("parallel private ignores scalars assigned inside loop", {
fn <- function(x, window) {
declare(
type(x = double(n)),
type(window = double(j))
)
out <- double(length(x) - length(window) + 1)
window_size <- as.double(length(window))
declare(parallel(private = j))
for (i in seq_along(out)) {
acc <- 0
for (j in seq_along(window)) {
acc <- acc + x[(i + j - 1)] * window[j]
}
out[i] <- acc * window_size
}
out
}
expect_no_error(r2f(fn))
})
test_that("is_parallel_target_stmt handles edge cases", {
# non-call
expect_false(quickr:::is_parallel_target_stmt(1L))
# assignment with less than 3 elements
bad_assign <- quote(`<-`(x))
expect_false(quickr:::is_parallel_target_stmt(bad_assign))
# other call types
expect_false(quickr:::is_parallel_target_stmt(quote(foo(x))))
})
test_that("openmp_directives errors for unsupported backend", {
parallel <- list(backend = "cuda")
expect_error(
quickr:::openmp_directives(parallel),
"unsupported parallel backend"
)
})
test_that("openmp_fflags uses env variable first", {
withr::local_envvar(QUICKR_OPENMP_FFLAGS = "-custom-flags")
result <- quickr:::openmp_fflags()
expect_equal(result, "-custom-flags")
})
test_that("openmp_link_flags uses env variable first", {
withr::local_envvar(QUICKR_OPENMP_LIBS = "-lcustom")
result <- quickr:::openmp_link_flags()
expect_equal(result, "-lcustom")
})
test_that("openmp_fflags returns empty when no config found", {
local_mocked_bindings(
openmp_config_value = function(...) "",
.package = "quickr"
)
withr::local_envvar(QUICKR_OPENMP_FFLAGS = "")
result <- quickr:::openmp_fflags()
expect_equal(result, "")
})
test_that("openmp_link_flags returns fflags for non-clang CC", {
local_mocked_bindings(
openmp_config_value = function(name, ...) {
switch(name, SHLIB_OPENMP_CFLAGS = "", CC = "gcc", "")
},
.package = "quickr"
)
withr::local_envvar(QUICKR_OPENMP_LIBS = "")
result <- quickr:::openmp_link_flags(fflags = "-fopenmp")
expect_equal(result, "-fopenmp")
})
test_that("openmp_link_flags returns empty for clang CC", {
local_mocked_bindings(
openmp_config_value = function(name, ...) {
switch(name, SHLIB_OPENMP_CFLAGS = "", CC = "clang", FC = "flang", "")
},
.package = "quickr"
)
withr::local_envvar(QUICKR_OPENMP_LIBS = "")
result <- quickr:::openmp_link_flags(fflags = "-fopenmp")
expect_equal(result, "")
})