# 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=data, 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\nif [ -f /usr/local/bin/jags ]; then echo 1; else echo 0; fi\n', 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:
name <- xgrid.submit.jags(xgrid.method='separatejobs', sub.options=paste('-a ' , getwd(), '/jagsART.sh', sep=''), model=model, monitor=c("m", "c", "precision"), data=data, n.chains=2, plots = FALSE)
# Cleanup (remove jagsART file):
unlink('jagsART.sh')
# Retrieve the results:
results <- xgrid.results(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:
stopifnot(Sys.which('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" -f', model=model, monitor=c("m", "c", "precision"), data=data, n.chains=2, plots = FALSE)Run the code above in your browser using DataLab