inst/doc/RBOINC-RU.R

## ----eval=FALSE---------------------------------------------------------------
#  install.packages(c('R.utils', 'askpass', 'foreach', 'httr', 'ssh', 'xml2',
#                     'stats', 'utils', 'doParallel', 'parallel', 'BiocManager'))
#  install.packages("RBOINC.cl", repos="http://R-Forge.R-project.org")

## ----eval=FALSE---------------------------------------------------------------
#  install.packages(c('R.utils', 'askpass', 'foreach', 'httr', 'ssh', 'xml2',
#                     'stats', 'utils', 'doParallel', 'parallel', 'BiocManager'))
#  install.packages("RBOINC.cl", repos="http://R-Forge.R-project.org")

## ----eval = FALSE-------------------------------------------------------------
#  test_jobs = function(work_func,
#                       data = NULL,
#                       n = NULL,
#                       init_func = NULL,
#                       global_vars = NULL,
#                       packages = c(),
#                       files = c(),
#                       callback_function = NULL,
#                       install_func = NULL)

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  data = 1:9
#  
#  res = test_jobs(fun, data)

## ----eval=FALSE---------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  data = 1:9
#  
#  res = test_jobs(fun, data, 2)

## ----eval=FALSE---------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fac = function(val)
#  {
#    if(val == 0 || val == 1){
#      return(val)
#    } else {
#      return(val*fac(val- 1))
#    }
#  }
#  
#  data = 1:9
#  
#  res = test_jobs(fac, data, 2)

## ----eval=FALSE---------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  N = 10000
#  
#  pi_approx = function()
#  {
#    a = runif(N,0,1)
#    b = runif(N,0,1)
#    res = sum(a*a+b*b < 1)/N
#    return(res)
#  }
#  
#  res = test_jobs(pi_approx, n = 10, global_vars = list(N = N))
#  
#  prob = 0
#  count = 0
#  for(val in res){
#    prob = prob + val$result
#    count = count + 1
#  }

## ----eval=FALSE---------------------------------------------------------------
#  print(prob/count*4)

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  d = function(val)
#  {
#    return(val/2)
#  }
#  
#  m = function(val)
#  {
#    return(val*3)
#  }
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  data = 1:10
#  
#  # Здесь мы передаём функции как глобальные переменные:
#  res = test_jobs(fun, data, 3, global_vars = list(d = d, m = m), packages = "numbers")

## ----eval=FALSE---------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  res = test_jobs(fun, data, 3, global_vars = list(d = d, m = m), packages = c("package1", "package2"))

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function()
#  {
#    mat = matrix(c(0, 0, 1, 1,
#                    0, 0, 1, 1,
#                    1, 1, 0, 1,
#                    1, 1, 1, 0),
#                  byrow=TRUE, ncol=4)
#     rownames(mat) <- letters[1:4]
#     colnames(mat) <- letters[1:4]
#     return(graphAM(adjMat=mat))
#  }
#  
#  res = test_jobs(fun, n = 1, packages = c("graph"))
#  
#  BiocManager::install("Rgraphviz", ask=FALSE)
#  library('Rgraphviz')
#  
#  plot(res[[1]]$result)

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  callback = function(val)
#  {
#    print(val)
#    return(val/2)
#  }
#  
#  data = 1:9
#  
#  res = test_jobs(fun, data, 2, callback_function = callback)

## ----eval=FALSE---------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  code_m = paste(
#  "#include <Rcpp.h>",
#  "using namespace Rcpp;",
#  "",
#  "// [[Rcpp::export]]",
#  "NumericVector m(NumericVector val)",
#  "{",
#  "    return val*3;",
#  "}",
#  sep = '\n'
#  )
#  
#  code_d = paste(
#  "#include <Rcpp.h>",
#  "using namespace Rcpp;",
#  "",
#  "// [[Rcpp::export]]",
#  "NumericVector d(NumericVector val)",
#  "{",
#  "    return val/2;",
#  "}",
#  sep = '\n'
#  )
#  
#  dirname = tempfile()
#  dir.create(dirname)
#  out = file(paste0(dirname, "/m.cpp"))
#  writeLines(code_m, out)
#  close(out)
#  out = file(paste0(dirname, "/d.cpp"))
#  writeLines(code_d, out)
#  close(out)

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = val/2
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp("m.cpp")
#  }
#  
#  data = 1:10
#  
#  res = test_jobs(fun, data, 1, packages = c("numbers", "Rcpp"),
#                  files = paste0(dirname, "/m.cpp"), init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp("m.cpp")
#    sourceCpp("d.cpp")
#  }
#  
#  data = 1:10
#  
#  res = test_jobs(fun, data, 1, packages = c("numbers", "Rcpp"),
#                  files = c(paste0(dirname, "/m.cpp"), paste0(dirname, "/d.cpp")),
#                  init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp(paste0(dname, "/m.cpp"))
#    sourceCpp(paste0(dname, "/d.cpp"))
#  }
#  
#  data = 1:3
#  
#  res = test_jobs(fun, data, packages = c("numbers", "Rcpp"),
#                  global_vars = list(dname = basename(dirname)),
#                  files = dirname, init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  for(val in res){
#    print(val$result)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  inst = function(pkgs){
#    for(val in pkgs){
#      if(val == "MJMrss"){
#        remotes::install_github("ProgGrey/MJMrss")
#      }
#    }
#  }
#  
#  
#  fun = function()
#  {
#    lambda = 1
#    N = 4
#    f = c(1,2.2)
#    P_a = matrix(c(0.1,0.9,
#                   0.0, 1.0), nrow = 2, byrow = TRUE)
#    P_d = matrix(c(1.0, 0.0,
#                   0.2, 0.8), nrow = 2, byrow = TRUE)
#    classes = matrix(c(1,      2,    3, 4, # servers
#                       0.5, 0.25, 0.15, 0.1, # probability
#                       1,    1.9,  2.5, 3), # class speed (1/mean work)
#                     nrow = 3, byrow = TRUE)
#    m = build_model(lambda, N, classes, f, P_a, P_d)
#    return(m$mean_clients)
#  }
#  
#  res = test_jobs(fun, n = 1, packages = c("remotes", "MJMrss"), install_func = inst)

## ----eval = FALSE-------------------------------------------------------------
#  res[[1]]$result

## ----eval = FALSE-------------------------------------------------------------
#  create_connection = function(server,
#                               dir,
#                               username,
#                               password = NULL,
#                               keyfile = NULL)

## ----eval=FALSE---------------------------------------------------------------
#  con = create_connection("ssh://server.local", "~/projects/rboinc_dev", "boincadm", "Emooka9u")
#  con = create_connection("http://server.local", "rboinc_dev", "submitter@example.com","ahth3Eze")
#  con = create_connection("https://server.local", "rboinc_dev", "submitter@example.com")

## ----eval=FALSE---------------------------------------------------------------
#  create_jobs = function(connection,
#                         work_func,
#                         data = NULL,
#                         n = NULL,
#                         init_func = NULL,
#                         global_vars = NULL,
#                         packages = c(),
#                         files = c(),
#                         install_func = NULL)

## ----eval=FALSE---------------------------------------------------------------
#  update_jobs_status = function(connection, jobs_status, callback_function = NULL)

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  data = 1:2
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  status = create_jobs(con, fun, data)
#  

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  
#  if(status$status == "done"){
#    print(status$results)
#    close_connection(con)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  data = 1:9
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  status = create_jobs(con, fun, data, 2)

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  
#  print(status$results)

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  
#  if(status$status == "done"){
#    print(status$results)
#    close_connection(con)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fac = function(val)
#  {
#    if(val == 0 || val == 1){
#      return(val)
#    } else {
#      return(val*fac(val - 1))
#    }
#  }
#  
#  data = 1:9
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  status = create_jobs(con, fac, data, 1)

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  for(val in status$results){
#    print(val)
#  }
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  N = 100000
#  
#  pi_approx = function()
#  {
#    a = runif(N,0,1)
#    b = runif(N,0,1)
#    res = sum(a*a+b*b < 1)/N
#    return(res)
#  }
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  status = create_jobs(con, pi_approx, n = 3, global_vars = list(N = N))
#  
#  

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  if(status$status == "done"){
#    close_connection(con)
#  }
#  
#  prob = 0
#  count = 0
#  for(val in status$results){
#    prob = prob + val
#    count = count + 1
#  }
#  print(prob/count*4)

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  d = function(val)
#  {
#    return(val/2)
#  }
#  
#  m = function(val)
#  {
#    return(val*3)
#  }
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  data = 1:10
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  # Здесь мы передаём функции как глобальные переменные:
#  status = create_jobs(con, fun, data, 1, global_vars = list(d = d, m = m), packages = "numbers")

## ----eval=FALSE---------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  for(val in status$results){
#    print(val)
#  }
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  res = create_jobs(con, fun, data, 3, global_vars = list(d = d, m = m),
#                    packages = c("package1", "package2"))

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function()
#  {
#    mat = matrix(c(0, 0, 1, 1,
#                    0, 0, 1, 1,
#                    1, 1, 0, 1,
#                    1, 1, 1, 0),
#                  byrow=TRUE, ncol=4)
#     rownames(mat) <- letters[1:4]
#     colnames(mat) <- letters[1:4]
#     return(graphAM(adjMat=mat))
#  }
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  status = create_jobs(con, fun, n = 1, packages = c("graph"))

## ----eval = FALSE-------------------------------------------------------------
#  install.packages("BiocManager")
#  BiocManager::install(ask=FALSE)
#  BiocManager::install(c("graph", "Rgraphviz"), ask=FALSE)
#  library('Rgraphviz')
#  
#  status = update_jobs_status(con, status)
#  if(status$status == "done"){
#    close_connection(con)
#  }
#  plot(status$results[[1]])

## ----eval=FALSE---------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    return(val*5)
#  }
#  
#  callback = function(val)
#  {
#    print(val)
#    return(1)
#  }
#  
#  data = 1:9
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  
#  status = create_jobs(con, fun, data, 2)
#  
#  status = update_jobs_status(con, status, callback)
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval=FALSE---------------------------------------------------------------
#  for(val in status$results){
#    print(val)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  code_m = paste(
#  "#include <Rcpp.h>",
#  "using namespace Rcpp;",
#  "",
#  "// [[Rcpp::export]]",
#  "NumericVector m(NumericVector val)",
#  "{",
#  "    return val*3;",
#  "}",
#  sep = '\n'
#  )
#  
#  code_d = paste(
#  "#include <Rcpp.h>",
#  "using namespace Rcpp;",
#  "",
#  "// [[Rcpp::export]]",
#  "NumericVector d(NumericVector val)",
#  "{",
#  "    return val/2;",
#  "}",
#  sep = '\n'
#  )
#  
#  dirname = tempfile()
#  dir.create(dirname)
#  out = file(paste0(dirname, "/m.cpp"))
#  writeLines(code_m, out)
#  close(out)
#  out = file(paste0(dirname, "/d.cpp"))
#  writeLines(code_d, out)
#  close(out)

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = val/2
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp("m.cpp")
#  }
#  
#  data = 1:10
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  status = create_jobs(con, fun, data, 1, packages = c("numbers", "Rcpp"),
#                       files = paste0(dirname, "/m.cpp"), init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  for(val in status$results){
#    print(val)
#  }
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp("m.cpp")
#    sourceCpp("d.cpp")
#  }
#  
#  data = 1:10
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  status = create_jobs(con, fun, data, 1, packages = c("numbers", "Rcpp"),
#                       files = c(paste0(dirname, "/m.cpp"), paste0(dirname, "/d.cpp")),
#                       init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  for(val in status$results){
#    print(val)
#  }
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  fun = function(val)
#  {
#    if(mod(val,2) == 0){
#      res = d(val)
#    } else {
#      res = m(val)
#    }
#    return(res)
#  }
#  
#  init = function()
#  {
#    sourceCpp(paste0(dname, "/m.cpp"))
#    sourceCpp(paste0(dname, "/d.cpp"))
#  }
#  
#  data = 1:3
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  status = create_jobs(con, fun, data, packages = c("numbers", "Rcpp"),
#                       global_vars = list(dname = basename(dirname)),
#                       files = dirname, init_func = init)

## ----eval = FALSE-------------------------------------------------------------
#  status = update_jobs_status(con, status)
#  for(val in status$results){
#    print(val)
#  }
#  if(status$status == "done"){
#    close_connection(con)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  library(RBOINC.cl)
#  
#  inst = function(pkgs){
#    for(val in pkgs){
#      if(val == "MJMrss"){
#        remotes::install_github("ProgGrey/MJMrss")
#      }
#    }
#  }
#  
#  
#  fun = function()
#  {
#    lambda = 1
#    N = 4
#    f = c(1,2.2)
#    P_a = matrix(c(0.1,0.9,
#                   0.0, 1.0), nrow = 2, byrow = TRUE)
#    P_d = matrix(c(1.0, 0.0,
#                   0.2, 0.8), nrow = 2, byrow = TRUE)
#    classes = matrix(c(1,      2,    3, 4, # servers
#                       0.5, 0.25, 0.15, 0.1, # probability
#                       1,    1.9,  2.5, 3), # class speed (1/mean work)
#                     nrow = 3, byrow = TRUE)
#    m = build_model(lambda, N, classes, f, P_a, P_d)
#    return(m$mean_clients)
#  }
#  
#  con = create_connection("http://server.local/", "rboinc_alpha", "submitter@example.com", "ahth3Eze")
#  status = create_jobs(con, fun, n = 1, packages = c("remotes", "MJMrss"),
#                       install_func = inst)
#  

## ----eval = FALSE-------------------------------------------------------------
#  status$results[[1]]

## ----eval=FALSE---------------------------------------------------------------
#  ...
#  con = create_connection(...)
#  status = create_jobs(...)
#  close_connection(con)
#  save(status, file = "jobs.rda")
#  quit()

## ----eval=FALSE---------------------------------------------------------------
#  con = create_connection(...)
#  load("jobs.rda")
#  status = update_jobs_status(con, status)
#  close_connection(con)
#  ...

## ----eval=FALSE---------------------------------------------------------------
#  status = cancel_jobs(con, status)

Try the RBOINC.cl package in your browser

Any scripts or data that you put into this service are public.

RBOINC.cl documentation built on Nov. 15, 2022, 3 a.m.