Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple GPU and CPU models #302

Merged
merged 77 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
6c3941f
Remove all gpu code from c++ models
richfitz Oct 18, 2021
445c7f2
Much simpler gpu toggle for compiled code
richfitz Oct 18, 2021
219eeaa
Skeleton for gpu version
richfitz Oct 18, 2021
6ef3104
Construction of device object
richfitz Oct 18, 2021
c0eed41
WIP
richfitz Oct 18, 2021
0026644
More WIP
richfitz Oct 21, 2021
328c21a
Try by removing CPU(DA) code
johnlees Oct 21, 2021
81f31ac
Put CPUDA code back
johnlees Oct 22, 2021
5d79057
Add device filter back
johnlees Oct 22, 2021
469cdc8
In dust_device obj, use set_x methods rather than initialisation
johnlees Oct 22, 2021
6ff6b90
Write remaining state and RNG interface fns
johnlees Oct 26, 2021
ac63cb4
Remove tmp particle from constructor
johnlees Oct 26, 2021
7e1cefc
Temporary dust interface for compiling
johnlees Oct 26, 2021
d2ded66
Add dustdevice header
johnlees Oct 26, 2021
36014ac
Fix some compile errors
johnlees Oct 26, 2021
583c465
Add namespace to filter state
johnlees Oct 26, 2021
ff757ef
Remove unnamed const bools
johnlees Oct 26, 2021
6337387
Go back to shared_ptr types
johnlees Oct 27, 2021
58bcce1
Add host interface to device resample
johnlees Oct 27, 2021
e1cf961
Change header order again
johnlees Oct 27, 2021
b22f716
Add filter state header
johnlees Oct 27, 2021
e07a0d3
Interface compiler errors
johnlees Oct 27, 2021
a2e332f
Change filter interface calls to device version
johnlees Oct 27, 2021
b7276c5
Start on new approach to compilation
richfitz Oct 29, 2021
b7bde85
Return device info
richfitz Oct 29, 2021
53d3c7d
Better error messages
richfitz Oct 29, 2021
9dd10c1
Partially working CPUDA code
richfitz Nov 1, 2021
05eb0e9
Fix device select
richfitz Nov 1, 2021
414cb76
Fix multiple parameter init
richfitz Nov 1, 2021
3b89406
Conditionally enable gpu
richfitz Nov 1, 2021
81258a1
Rebuild examples
richfitz Nov 1, 2021
bcbf66a
Disable more of the filter
richfitz Nov 1, 2021
0c188eb
Start patching tests
richfitz Nov 1, 2021
8c7e6ac
Rengenerate examples again
richfitz Nov 1, 2021
53e03eb
Enable compare/data gpu tests
richfitz Nov 1, 2021
730d976
All basic gpu tests working
richfitz Nov 1, 2021
09d788e
Resupport vector of steps
richfitz Nov 1, 2021
ecdf9f1
Drop has_gpu_support template
richfitz Nov 1, 2021
bdec01b
Method to report if we have gpu support
richfitz Nov 1, 2021
4665e48
Regenerate docs
richfitz Nov 1, 2021
5c27c76
Re-enable test
richfitz Nov 1, 2021
bcede65
Get model compiling
richfitz Nov 1, 2021
20e8d13
Merge remote-tracking branch 'origin/master' into i292-simpler-gpu-al…
richfitz Nov 1, 2021
020b48b
Bump version
richfitz Nov 1, 2021
4ba6704
Drop blank line
richfitz Nov 1, 2021
01b957d
Delint bash script
richfitz Nov 1, 2021
b4f7515
Patch easy misses in r code
richfitz Nov 1, 2021
1987bd2
Refactor handling of negative device_id
richfitz Nov 1, 2021
43d593c
Correctly save host rng state
richfitz Nov 1, 2021
5455c07
Expand tests
richfitz Nov 1, 2021
4b04957
Delete obsolete test
richfitz Nov 1, 2021
7fe89dc
Fix for partial state bug
richfitz Nov 1, 2021
7add58c
Add additional state update test
richfitz Nov 1, 2021
9d78bd0
Add validation checks
richfitz Nov 2, 2021
86914c2
Disable omp error support
richfitz Nov 2, 2021
4bd088c
Reformat
richfitz Nov 2, 2021
28ed467
Fix for parameter setting
richfitz Nov 2, 2021
addc1da
Fix resample
richfitz Nov 2, 2021
43e27b1
Test of parameter setting into multipar object
richfitz Nov 2, 2021
63e7f20
Drop assertions
richfitz Nov 2, 2021
7d46093
Simplify rng state fetch
richfitz Nov 2, 2021
ec26f09
Adjust condition for error reset
richfitz Nov 2, 2021
756fbdf
Simplify multiparameter setting
richfitz Nov 2, 2021
270b0a1
Simplify device state construction
richfitz Nov 2, 2021
697236e
Start removing redundant code
richfitz Nov 2, 2021
2417756
Simplify further
richfitz Nov 2, 2021
42e997d
Simplify rng set
richfitz Nov 2, 2021
d69396f
Make to/from device simpler
richfitz Nov 2, 2021
9ef7f1c
General tidyup
richfitz Nov 2, 2021
5a3f32b
Make function less mysterious
richfitz Nov 2, 2021
cec8f94
Combine similar package and dust code
richfitz Nov 2, 2021
1636a0e
Small tidyups noticed in PR review
richfitz Nov 2, 2021
0f2d439
Bump version number
richfitz Nov 2, 2021
7b362c2
Regenerate/redocument
richfitz Nov 2, 2021
378bf9b
Eliminate unused code
richfitz Nov 2, 2021
b819f7c
Comments from review
richfitz Nov 3, 2021
6d43bd3
Add another issue link
richfitz Nov 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dust
Title: Iterate Multiple Realisations of Stochastic Models
Version: 0.10.1
Version: 0.11.0
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("John", "Lees", role = "aut"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dust 0.11.0

* Rationalised the GPU interface, now once created models can only be used on either the GPU or CPU which simplifies the internal bookkeeping (#292, #302)

# dust 0.10.0

* Improved and generalised RNG interface, with more algorithms and more control
Expand Down
84 changes: 61 additions & 23 deletions R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,55 @@ generate_dust <- function(filename, quiet, workdir, cuda, skip_cache, mangle) {
file.path(path, "DESCRIPTION"))
substitute_dust_template(data, "NAMESPACE",
file.path(path, "NAMESPACE"))
substitute_dust_template(data, "dust.R.template",
file.path(path, "R/dust.R"))
substitute_dust_template(data, "dust.hpp",
file.path(path, "src", "dust.hpp"))

if (is.null(cuda)) {
path_dust_cpp <- file.path(path, "src", "dust.cpp")
cpp_ext <- ".cpp"
substitute_dust_template(data, "Makevars",
file.path(path, "src", "Makevars"))
} else {
path_dust_cpp <- file.path(path, "src", "dust.cu")
cpp_ext <- ".cu"
substitute_dust_template(data, "Makevars.cuda",
file.path(path, "src", "Makevars"))
}
substitute_dust_template(data, "dust.cpp", path_dust_cpp)

## Keep the generated dust files simple by dropping roxygen docs
## which are used in making the interface docs (?dust_generator) and
## internal comments which remind developers about next steps after
## modifying files.
dust_r <- drop_internal_comments(readLines(file.path(path, "R/dust.R")))
writeLines(drop_roxygen(dust_r), file.path(path, "R/dust.R"))

dust_cpp <- drop_internal_comments(readLines(path_dust_cpp))
writeLines(dust_cpp, path_dust_cpp)

dust_hpp <- drop_internal_comments(readLines(file.path(path, "src/dust.hpp")))
writeLines(dust_hpp, file.path(path, "src/dust.hpp"))
code <- dust_code(data, config)
writeLines(code$r, file.path(path, "R/dust.R"))
writeLines(code$cpp, file.path(path, paste0("src/dust", cpp_ext)))
writeLines(code$hpp, file.path(path, "src/dust.hpp"))

res <- list(key = base, gpu = gpu, data = data, path = path)
cache$models$set(base, res, skip_cache)
res
}


dust_code <- function(data, config) {
dust_r <- drop_roxygen(
substitute_dust_template(data, "dust.R.template", NULL))

dust_cpp <- c(substitute_dust_template(data, "dust.cpp", NULL),
substitute_dust_template(data, "dust_methods.cpp", NULL))
dust_hpp <- c(substitute_dust_template(data, "dust.hpp", NULL),
substitute_dust_template(data, "dust_methods.hpp", NULL))

if (config$has_gpu_support) {
data_gpu <- data
data_gpu$target <- "gpu"
data_gpu$container <- "DustDevice"
dust_cpp <- c(dust_cpp,
substitute_dust_template(data_gpu, "dust_methods.cpp", NULL))
dust_hpp <- c(dust_hpp,
substitute_dust_template(data_gpu, "dust_methods.hpp", NULL))
}

ret <- list(r = dust_r,
hpp = dust_hpp,
cpp = dust_cpp)

lapply(ret, drop_internal_comments)
}


compile_and_load <- function(filename, quiet = FALSE, workdir = NULL,
cuda = NULL, skip_cache = FALSE) {
res <- generate_dust(filename, quiet, workdir, cuda, skip_cache, TRUE)
Expand Down Expand Up @@ -96,6 +110,9 @@ compile_and_load <- function(filename, quiet = FALSE, workdir = NULL,
substitute_template <- function(data, src, dest) {
template <- read_lines(src)
txt <- glue_whisker(template, data)
if (is.null(dest)) {
return(txt)
}
writelines_if_changed(txt, dest)
}

Expand All @@ -106,18 +123,39 @@ substitute_dust_template <- function(data, src, dest) {


glue_whisker <- function(template, data) {
if (length(template) > 1L) {
template <- paste(template, collapse = "\n")
}
stopifnot(length(template) == 1L)
glue::glue(template, .envir = data, .open = "{{", .close = "}}",
.trim = FALSE)
}


dust_template_data <- function(model, config, cuda) {
methods <- function(target) {
nms <- c("alloc", "run", "simulate", "set_index", "n_state",
"update_state", "state", "step", "reorder", "resample",
"rng_state", "set_rng_state", "set_n_threads",
"set_data", "compare_data", "filter")
m <- sprintf("%s = dust_%s_%s_%s", nms, target, config$name, nms)
sprintf("list(\n%s)", paste(" ", m, collapse = ",\n"))
}
methods_cpu <- methods("cpu")

if (config$has_gpu_support) {
methods_gpu <- methods("gpu")
} else {
methods_gpu <- paste(
"list(alloc = function(...) {",
' stop("GPU support not enabled for this object")',
" })", sep = "\n")
}

list(model = model,
name = config$name,
class = config$class,
param = deparse_param(config$param),
cuda = cuda$flags)
cuda = cuda$flags,
target = "cpu",
container = "Dust",
methods_cpu = methods_cpu,
methods_gpu = methods_gpu)
}
Loading