# run a simple model on Xgrid using a single job:
# Ensure the required environmental variables are set:
Sys.setenv(XGRID_CONTROLLER_HOSTNAME="<hostname>")
Sys.setenv(XGRID_CONTROLLER_PASSWORD="<password>")
# Simulate the data
X <- 1:100
Y <- rnorm(length(X), 2*X + 10, 1)
# Model in the JAGS format
model <- "model {
for(i in 1 : N){
Y[i] ~ dnorm(true.y[i], precision);
true.y[i] <- (m * X[i]) + c;
}
m ~ dunif(-1000,1000);
c ~ dunif(-1000,1000);
precision ~ dexp(1);
}"
# Run the model synchronously using the 'simple' method
# and a wait interval of 1 minute:
results <- xgrid.run.jags(xgrid.method='simple',
wait.interval='1 min', model=model, monitor=c("m", "c",
"precision"), data=list(N=length(X), X=X, Y=Y), n.chains=2,
plots = FALSE)
# Analyse the results:
results$summary
# Submit a job to xgrid and (later) retrieve the results. Use an
# ART script to ensure the job is only sent to nodes with JAGS installed:
# Ensure the required environmental variables are set:
Sys.setenv(XGRID_CONTROLLER_HOSTNAME="<hostname>")
Sys.setenv(XGRID_CONTROLLER_PASSWORD="<password>")
# Create the ART script we need to ensure JAGS is installed:
cat('#!/bin/bash
if [ -f /usr/local/bin/jags ]; then
echo 1
else
echo 0
fi
', file='jagsART.sh')
# Simulate the data
X <- 1:100
Y <- rnorm(length(X), 2*X + 10, 1)
# Model in the JAGS format
model <- "model {
for(i in 1 : N){
Y[i] ~ dnorm(true.y[i], precision);
true.y[i] <- (m * X[i]) + c;
}
m ~ dunif(-1000,1000);
c ~ dunif(-1000,1000);
precision ~ dexp(1);
}"
# Run the model asynchronously (the ART script path must
# be specified as an absolute link as xgrid won't be called
# in the current working directory, and all paths must be
# enclosed in quotes to preserve spaces):
name <- xgrid.submit.jags(xgrid.method='separatejobs',
sub.options=if(!file.exists(Sys.which('mgrid'))) paste('-art
"', getwd(), '/jagsART.sh"', sep='') else paste('-a "', getwd(),
'/jagsART.sh"', sep=''), model=model, monitor=c("m", "c", "precision"),
data=list(N=length(X), X=X, Y=Y), n.chains=2, plots = FALSE,
inits=list(list(.RNG.name='base::Wichmann-Hill'),
list(.RNG.name='base::Marsaglia-Multicarry')))
# Cleanup (remove jagsART file):
unlink('jagsART.sh')
# Retrieve the results:
results <- xgrid.results.jags(name)
# Autorun a model to convergence using separate tasks on xgrid.
# Ensure the tasks are sent to the 2 fastest nodes (called 'Bugati'
# and 'McLaren') in our (fictional) cluster using arguments to mgrid.
# Ensure the required environmental variables are set:
Sys.setenv(XGRID_CONTROLLER_HOSTNAME="<hostname>")
Sys.setenv(XGRID_CONTROLLER_PASSWORD="<password>")
# Ensure mgrid is installed:
if(!file.exists(Sys.which('mgrid'))) install.mgrid()
# Simulate the data
X <- 1:100
Y <- rnorm(length(X), 2*X + 10, 1)
# Model in the JAGS format
model <- "model {
for(i in 1 : N){
Y[i] ~ dnorm(true.y[i], precision);
true.y[i] <- (m * X[i]) + c;
}
m ~ dunif(-1000,1000);
c ~ dunif(-1000,1000);
precision ~ dexp(1);
}"
# Run the model synchronously using the 'separatetasks' method and
# a wait interval of 1 minute:
results <- xgrid.autorun.jags(xgrid.method='separatetasks',
wait.interval='1 min', sub.options='-h "Bugati:McLaren"',
model=model, monitor=c("m", "c", "precision"),
data=list(N=length(X), X=X, Y=Y), n.chains=2,
inits=list(list(.RNG.name='base::Wichmann-Hill'),
list(.RNG.name='base::Marsaglia-Multicarry')), plots = FALSE)Run the code above in your browser using DataLab