Skip to content

Commit de8563c

Browse files
committed
lint
1 parent 6cce506 commit de8563c

9 files changed

+176
-96
lines changed

R/cpp_opts.R

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ run_info_cli <- function(exe_file) {
2121

2222
# new parser
2323
# Parse the string output of <model> `info` into an R object (list)
24+
2425
parse_exe_info_string <- function(ret_stdout) {
2526
info <- list()
2627
info_raw <- strsplit(strsplit(ret_stdout, "\n")[[1]], "=")
@@ -35,7 +36,12 @@ parse_exe_info_string <- function(ret_stdout) {
3536
}
3637
}
3738

38-
info[["stan_version"]] <- paste0(info[["stan_version_major"]], ".", info[["stan_version_minor"]], ".", info[["stan_version_patch"]])
39+
info[["stan_version"]] <- paste0(
40+
info[["stan_version_major"]],
41+
".",
42+
info[["stan_version_minor"]],
43+
".", info[["stan_version_patch"]]
44+
)
3945
info[["stan_version_major"]] <- NULL
4046
info[["stan_version_minor"]] <- NULL
4147
info[["stan_version_patch"]] <- NULL
@@ -110,9 +116,10 @@ validate_cpp_options <- function(cpp_options) {
110116
) {
111117
warning(
112118
"User header specified both via cpp_options[[\"USER_HEADER\"]] ",
113-
"and cpp_options[[\"user_header\"]]. Please only specify your user header in one location",
119+
"and cpp_options[[\"user_header\"]].",
114120
call. = FALSE
115121
)
122+
cpp_options[["user_header"]] <- NULL
116123
}
117124

118125
names(cpp_options) <- tolower(names(cpp_options))
@@ -141,18 +148,24 @@ validate_cpp_options <- function(cpp_options) {
141148
assert_valid_opencl <- function(
142149
opencl_ids,
143150
exe_info,
144-
fallback_exe_info = list('stan_version' = '2.0.0', 'stan_opencl' = FALSE)
151+
fallback_exe_info = list("stan_version" = "2.0.0", "stan_opencl" = FALSE)
145152
) {
146153
if (is.null(opencl_ids)) return(invisible(opencl_ids))
147-
148-
fallback <- length(exe_info) == 0
149-
if(fallback) exe_info <- fallback_exe_info
150-
# If we're unsure if this info is accurate, we shouldn't stop the user from attempting on that basis
151-
# the user should have been warned about this in initialize(), so no need to re-warn here.
152-
if(fallback) stop <- warning
154+
155+
fallback <- length(exe_info) == 0
156+
if (fallback) exe_info <- fallback_exe_info
157+
# If we're unsure if this info is accurate,
158+
# we shouldn't stop the user from attempting on that basis
159+
# the user should have been warned about this in initialize(),
160+
# so no need to re-warn here.
161+
if (fallback) stop <- warning
153162

154163
if (exe_info[['stan_version']] < "2.26.0") {
155-
stop("Runtime selection of OpenCL devices is only supported with CmdStan version 2.26 or newer.", call. = FALSE)
164+
stop(
165+
"Runtime selection of OpenCL devices is only supported ",
166+
"with CmdStan version 2.26 or newer.",
167+
call. = FALSE
168+
)
156169
}
157170

158171
if (isFALSE(exe_info[["stan_opencl"]])) {
@@ -165,12 +178,19 @@ assert_valid_opencl <- function(
165178
}
166179

167180
# cpp_options must be a list
168-
assert_valid_threads <- function(threads, exe_info, fallback_exe_info, multiple_chains = FALSE) {
169-
fallback <- length(exe_info) == 0
170-
if(fallback) exe_info <- fallback_exe_info
171-
# If we're unsure if this info is accurate, we shouldn't stop the user from attempting on that basis
172-
# the user should have been warned about this in initialize(), so no need to re-warn here.
173-
if(fallback) stop <- warning
181+
assert_valid_threads <- function(
182+
threads,
183+
exe_info,
184+
fallback_exe_info,
185+
multiple_chains = FALSE
186+
) {
187+
fallback <- length(exe_info) == 0
188+
if (fallback) exe_info <- fallback_exe_info
189+
# If we're unsure if this info is accurate,
190+
# we shouldn't stop the user from attempting on that basis
191+
# the user should have been warned about this in initialize(),
192+
# so no need to re-warn here.
193+
if (fallback) stop <- warning
174194

175195
threads_arg <- if (multiple_chains) "threads_per_chain" else "threads"
176196
checkmate::assert_integerish(threads, .var.name = threads_arg,
@@ -193,59 +213,39 @@ assert_valid_threads <- function(threads, exe_info, fallback_exe_info, multiple_
193213
invisible(threads)
194214
}
195215

196-
validate_precompile_cpp_options <- function(cpp_options) {
197-
if(is.null(cpp_options) || length(cpp_options) == 0) return(list())
198-
199-
if (!is.null(cpp_options[["user_header"]]) && !is.null(cpp_options[['USER_HEADER']])) {
200-
warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE)
201-
cpp_options[["user_header"]] <- NULL
202-
}
203-
204-
names(cpp_options) <- tolower(names(cpp_options))
205-
flags_set_if_defined <- c(
206-
# cmdstan
207-
"stan_threads", "stan_mpi", "stan_opencl", "stan_no_range_checks", "stan_cpp_optims",
208-
# stan math
209-
"integrated_opencl", "tbb_lib", "tbb_inc", "tbb_interface_new"
210-
)
211-
for (flag in flags_set_if_defined) {
212-
if (isFALSE(cpp_options[[flag]])) warning(
213-
toupper(flag), " set to ", cpp_options[flag], " Since this is a non-empty value, ",
214-
"it will result in the corresponding ccp option being turned ON. To turn this",
215-
" option off, use cpp_options = list(", flag, " = NULL)."
216-
)
217-
}
218-
cpp_options
219-
}
220216

221217

222218
# For two functions below
223219
# both styles are lists which should have flag names in lower case as names of the list
224220
# cpp_options style means is NULL or empty string
225221
# exe_info style means off is FALSE
222+
226223
exe_info_style_cpp_options <- function(cpp_options) {
227224
if(is.null(cpp_options)) cpp_options <- list()
228225
names(cpp_options) <- tolower(names(cpp_options))
229226
flags_reported_in_exe_info <- c(
230-
"stan_threads", "stan_mpi", "stan_opencl", "stan_no_range_checks", "stan_cpp_optims"
227+
"stan_threads", "stan_mpi", "stan_opencl",
228+
"stan_no_range_checks", "stan_cpp_optims"
231229
)
232230
for (flag in flags_reported_in_exe_info) {
233-
cpp_options[[flag]] <- !(is.null(cpp_options[[flag]]) || cpp_options[[flag]] == '')
231+
cpp_options[[flag]] <- !(
232+
is.null(cpp_options[[flag]]) || cpp_options[[flag]] == ""
233+
)
234234
}
235235
cpp_options
236236
}
237237

238238
exe_info_reflects_cpp_options <- function(exe_info, cpp_options) {
239-
if(length(exe_info) == 0) {
240-
warning('Recompiling is recommended due to missing exe_info.')
239+
if (length(exe_info) == 0) {
240+
warning("Recompiling is recommended due to missing exe_info.")
241241
return(TRUE)
242242
}
243-
if(is.null(cpp_options)) return(TRUE)
243+
if (is.null(cpp_options)) return(TRUE)
244244

245245
cpp_options <- exe_info_style_cpp_options(cpp_options)[tolower(names(cpp_options))]
246246
overlap <- names(cpp_options)[names(cpp_options) %in% names(exe_info)]
247247

248-
if(length(overlap) == 0) TRUE else all.equal(
248+
if (length(overlap) == 0) TRUE else all.equal(
249249
exe_info[overlap],
250250
cpp_options[overlap]
251251
)

R/model.R

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' Create a new CmdStanModel object
32
#'
43
#' @description \if{html}{\figure{logo.png}{options: width=25}}
@@ -252,7 +251,7 @@ CmdStanModel <- R6::R6Class(
252251
private$stan_file_ <- absolute_path(stan_file)
253252
private$stan_code_ <- readLines(stan_file)
254253
private$model_name_ <- sub(" ", "_", strip_ext(basename(private$stan_file_)))
255-
private$precompile_cpp_options_ <- validate_precompile_cpp_options(args$cpp_options) %||% list()
254+
private$precompile_cpp_options_ <- validate_cpp_options(args$cpp_options) %||% list()
256255
private$precompile_stanc_options_ <- assert_valid_stanc_options(args$stanc_options) %||% list()
257256
if (!is.null(args$user_header) || !is.null(args$cpp_options[["USER_HEADER"]]) ||
258257
!is.null(args$cpp_options[["user_header"]])) {
@@ -392,18 +391,20 @@ CmdStanModel <- R6::R6Class(
392391
# because that value is only set if model has been recomplied
393392
# since CmdStanModel instantiation
394393
if (!fallback) {
395-
return(self$exe_info()[['stan_version']])
394+
return(self$exe_info()[["stan_version"]])
396395
}
397396
for (candidate in c(
398-
self$exe_info()[['stan_version']],
399-
self$exe_info_fallback()[['stan_version']]
400-
)) if (!is.null(candidate)) return (candidate)
397+
self$exe_info()[["stan_version"]],
398+
self$exe_info_fallback()[["stan_version"]]
399+
)) if (!is.null(candidate)) return(candidate)
401400
},
402401
cpp_options = function() {
403402
warning(
404-
'mod$cpp_options() will be deprecated in the next major version of cmdstanr. ',
405-
'Use mod$exe_info() to see options from last compilation. ',
406-
'Use mod$precompile_cpp_options() to see default options for next compilation.'
403+
"mod$cpp_options() will be deprecated ",
404+
"in the next major version of cmdstanr. ",
405+
"Use mod$exe_info() to see options from last compilation. ",
406+
"Use mod$precompile_cpp_options() ",
407+
"to see default options for next compilation."
407408
)
408409
private$cpp_options_
409410
},
@@ -565,13 +566,16 @@ compile <- function(quiet = TRUE,
565566

566567
if (!is.null(user_header) && (
567568
!is.null(cpp_options[["USER_HEADER"]]) || !is.null(cpp_options[["user_header"]])
568-
)) warning("User header specified both via user_header argument and via cpp_options arguments")
569+
)) warning(
570+
"User header specified both via user_header argument ",
571+
"and via cpp_options arguments"
572+
)
569573

570574
if (length(cpp_options) == 0 && !is.null(private$precompile_cpp_options_)) {
571575
cpp_options <- private$precompile_cpp_options_
572576
}
573577
cpp_options_legacy <- cpp_options
574-
cpp_options <- validate_precompile_cpp_options(cpp_options)
578+
cpp_options <- validate_cpp_options(cpp_options)
575579

576580
if (length(stanc_options) == 0 && !is.null(private$precompile_stanc_options_)) {
577581
stanc_options <- private$precompile_stanc_options_
@@ -810,7 +814,10 @@ compile <- function(quiet = TRUE,
810814
private$precompile_stanc_options_ <- NULL
811815
private$precompile_include_paths_ <- NULL
812816

813-
# Must be run after private$cmdstan_version_, private$exe_file_, and private$precompiled_cpp_options_
817+
# Must be run after
818+
# - private$cmdstan_version_
819+
# - private$exe_file_
820+
# - private$precompiled_cpp_options_
814821
# are all up to date
815822
self$exe_info(update=TRUE)
816823

@@ -1340,7 +1347,11 @@ sample <- function(data = NULL,
13401347
output_dir = output_dir,
13411348
output_basename = output_basename,
13421349
sig_figs = sig_figs,
1343-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
1350+
opencl_ids = assert_valid_opencl(
1351+
opencl_ids,
1352+
self$exe_info(),
1353+
self$exe_info_fallback()
1354+
),
13441355
model_variables = model_variables,
13451356
save_cmdstan_config = save_cmdstan_config
13461357
)
@@ -1597,7 +1608,11 @@ optimize <- function(data = NULL,
15971608
num_procs = 1,
15981609
show_stderr_messages = show_exceptions,
15991610
show_stdout_messages = show_messages,
1600-
threads_per_proc = assert_valid_threads(threads, self$exe_info(), self$exe_info_fallback())
1611+
threads_per_proc = assert_valid_threads(
1612+
threads,
1613+
self$exe_info(),
1614+
self$exe_info_fallback()
1615+
)
16011616
)
16021617
model_variables <- NULL
16031618
if (is_variables_method_supported(self)) {
@@ -1632,7 +1647,11 @@ optimize <- function(data = NULL,
16321647
output_dir = output_dir,
16331648
output_basename = output_basename,
16341649
sig_figs = sig_figs,
1635-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
1650+
opencl_ids = assert_valid_opencl(
1651+
opencl_ids,
1652+
self$exe_info(),
1653+
self$exe_info_fallback()
1654+
),
16361655
model_variables = model_variables,
16371656
save_cmdstan_config = save_cmdstan_config
16381657
)
@@ -1737,7 +1756,11 @@ laplace <- function(data = NULL,
17371756
num_procs = 1,
17381757
show_stderr_messages = show_exceptions,
17391758
show_stdout_messages = show_messages,
1740-
threads_per_proc = assert_valid_threads(threads, self$exe_info(), self$exe_info_fallback())
1759+
threads_per_proc = assert_valid_threads(
1760+
threads,
1761+
self$exe_info(),
1762+
self$exe_info_fallback()
1763+
)
17411764
)
17421765
model_variables <- NULL
17431766
if (is_variables_method_supported(self)) {
@@ -1799,7 +1822,11 @@ laplace <- function(data = NULL,
17991822
output_dir = output_dir,
18001823
output_basename = output_basename,
18011824
sig_figs = sig_figs,
1802-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
1825+
opencl_ids = assert_valid_opencl(
1826+
opencl_ids,
1827+
self$exe_info(),
1828+
self$exe_info_fallback()
1829+
),
18031830
model_variables = model_variables,
18041831
save_cmdstan_config = save_cmdstan_config
18051832
)
@@ -1887,7 +1914,11 @@ variational <- function(data = NULL,
18871914
num_procs = 1,
18881915
show_stderr_messages = show_exceptions,
18891916
show_stdout_messages = show_messages,
1890-
threads_per_proc = assert_valid_threads(threads, self$exe_info(), self$exe_info_fallback())
1917+
threads_per_proc = assert_valid_threads(
1918+
threads,
1919+
self$exe_info(),
1920+
self$exe_info_fallback()
1921+
)
18911922
)
18921923
model_variables <- NULL
18931924
if (is_variables_method_supported(self)) {
@@ -1922,7 +1953,11 @@ variational <- function(data = NULL,
19221953
output_dir = output_dir,
19231954
output_basename = output_basename,
19241955
sig_figs = sig_figs,
1925-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
1956+
opencl_ids = assert_valid_opencl(
1957+
opencl_ids,
1958+
self$exe_info(),
1959+
self$exe_info_fallback()
1960+
),
19261961
model_variables = model_variables,
19271962
save_cmdstan_config = save_cmdstan_config
19281963
)
@@ -2032,7 +2067,11 @@ pathfinder <- function(data = NULL,
20322067
num_procs = 1,
20332068
show_stderr_messages = show_exceptions,
20342069
show_stdout_messages = show_messages,
2035-
threads_per_proc = assert_valid_threads(num_threads, self$exe_info(), self$exe_info_fallback())
2070+
threads_per_proc = assert_valid_threads(
2071+
num_threads,
2072+
self$exe_info(),
2073+
self$exe_info_fallback()
2074+
)
20362075
)
20372076
model_variables <- NULL
20382077
if (is_variables_method_supported(self)) {
@@ -2072,7 +2111,11 @@ pathfinder <- function(data = NULL,
20722111
output_dir = output_dir,
20732112
output_basename = output_basename,
20742113
sig_figs = sig_figs,
2075-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
2114+
opencl_ids = assert_valid_opencl(
2115+
opencl_ids,
2116+
self$exe_info(),
2117+
self$exe_info_fallback()
2118+
),
20762119
model_variables = model_variables,
20772120
num_threads = num_threads,
20782121
save_cmdstan_config = save_cmdstan_config
@@ -2169,7 +2212,12 @@ generate_quantities <- function(fitted_params,
21692212
procs <- CmdStanGQProcs$new(
21702213
num_procs = length(fitted_params_files),
21712214
parallel_procs = checkmate::assert_integerish(parallel_chains, lower = 1, null.ok = TRUE),
2172-
threads_per_proc = assert_valid_threads(threads_per_chain, self$exe_info(), self$exe_info_fallback(), multiple_chains = TRUE)
2215+
threads_per_proc = assert_valid_threads(
2216+
threads_per_chain,
2217+
self$exe_info(),
2218+
self$exe_info_fallback(),
2219+
multiple_chains = TRUE
2220+
)
21732221
)
21742222
model_variables <- NULL
21752223
if (is_variables_method_supported(self)) {
@@ -2190,7 +2238,11 @@ generate_quantities <- function(fitted_params,
21902238
output_dir = output_dir,
21912239
output_basename = output_basename,
21922240
sig_figs = sig_figs,
2193-
opencl_ids = assert_valid_opencl(opencl_ids, self$exe_info(), self$exe_info_fallback()),
2241+
opencl_ids = assert_valid_opencl(
2242+
opencl_ids,
2243+
self$exe_info(),
2244+
self$exe_info_fallback()
2245+
),
21942246
model_variables = model_variables
21952247
)
21962248
runset <- CmdStanRun$new(args, procs)
@@ -2407,7 +2459,9 @@ model_variables <- function(stan_file, include_paths = NULL, allow_undefined = F
24072459
}
24082460

24092461
is_variables_method_supported <- function(mod) {
2410-
cmdstan_version() >= "2.27.0" && mod$has_stan_file() && file.exists(mod$stan_file())
2462+
cmdstan_version() >= "2.27.0" &&
2463+
mod$has_stan_file() &&
2464+
file.exists(mod$stan_file())
24112465
}
24122466

24132467
resolve_exe_path <- function(

0 commit comments

Comments
 (0)