## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = identical(Sys.getenv("IN_PKGDOWN"), "true") ||
    identical(Sys.getenv("CUDA_ML_GPU_VIGNETTES"), "true")
)

## ----save-file, results = "hide"----------------------------------------------
# library(cuda.ml)
# 
# cuda_ml_install()
# 
# model <- cuda_ml_linear_reg(
#   mpg ~ .,
#   data = mtcars,
#   penalty = 0.01,
#   mixture = 0
# )
# 
# state_path <- tempfile(fileext = ".cuda-ml-state")
# cuda_ml_serialize(model, state_path)

## ----restore-file-------------------------------------------------------------
# library(cuda.ml)
# 
# cuda_ml_install()
# 
# model <- cuda_ml_unserialize(state_path)
# 
# predictors <- subset(mtcars, select = -mpg)
# predict(model, predictors[1:5, ])

## ----raw-state----------------------------------------------------------------
# state <- cuda_ml_serialize(model)
# str(state)
# 
# model <- cuda_ml_unserialize(state)

## ----bundle-------------------------------------------------------------------
# library(bundle)
# 
# bundle_path <- tempfile(fileext = ".bundle.rds")
# bundled_model <- bundle(model)
# saveRDS(bundled_model, bundle_path)
# 
# bundled_model <- readRDS(bundle_path)
# model <- unbundle(bundled_model)

## ----bundle-nvforest----------------------------------------------------------
# set.seed(1)
# forest <- cuda_ml_rand_forest(
#   class ~ .,
#   data = modeldata::hpc_data,
#   trees = 100
# )
# 
# cpu_bundle <- bundle(forest, device = "cpu")
# forest_bundle_path <- tempfile(fileext = ".bundle.rds")
# saveRDS(cpu_bundle, forest_bundle_path)

## ----unbundle-nvforest--------------------------------------------------------
# library(cuda.ml)
# library(bundle)
# 
# cuda_ml_install(device = "cpu")
# forest <- unbundle(readRDS(forest_bundle_path))

## ----export-nvforest----------------------------------------------------------
# forest_directory <- tempfile("forest-artifact-")
# dir.create(forest_directory)
# cuda_ml_nvforest_export(
#   forest,
#   directory = forest_directory,
#   prefix = "model"
# )

## ----import-nvforest----------------------------------------------------------
# cuda_ml_install(device = "cpu")
# 
# forest <- cuda_ml_nvforest_import(
#   directory = forest_directory,
#   prefix = "model",
#   device = "cpu"
# )

## ----nvforest-devices---------------------------------------------------------
# forest_state <- cuda_ml_serialize(forest)
# cpu_forest <- cuda_ml_unserialize(forest_state, device = "cpu")
# gpu_forest <- cuda_ml_unserialize(
#   forest_state,
#   device = "gpu",
#   device_id = 0
# )

## ----cleanup, include = FALSE-------------------------------------------------
# unlink(
#   c(state_path, bundle_path, forest_bundle_path, forest_directory),
#   recursive = TRUE
# )

