[Swift-commit] r4054 - SwiftApps/SwiftR/Swift/R
noreply at svn.ci.uchicago.edu
noreply at svn.ci.uchicago.edu
Thu Feb 3 12:37:27 CST 2011
Author: tga
Date: 2011-02-03 12:37:26 -0600 (Thu, 03 Feb 2011)
New Revision: 4054
Modified:
SwiftApps/SwiftR/Swift/R/Swift.R
Log:
Working versions of swiftLibrary and swiftDetach functions which work
analagously to built-in library and detach fucntions (but with fewer options).
These functions are implemented in such a way that the sequence of library
and detach calls is stored in R and added to the initialExpr for all the swift
workers, so new workers can be brought online and have the correct libraries
loaded without intervention from the user.
Modified: SwiftApps/SwiftR/Swift/R/Swift.R
===================================================================
--- SwiftApps/SwiftR/Swift/R/Swift.R 2011-02-03 17:56:19 UTC (rev 4053)
+++ SwiftApps/SwiftR/Swift/R/Swift.R 2011-02-03 18:37:26 UTC (rev 4054)
@@ -61,14 +61,26 @@
# Build a list of "Library" statements for all of the libraries
# already specified through swiftLibrary commands.
- lib <- getOption("swift.libraries")
+ lib <- getOption(".swift.packages")
if (!is.null(lib)) {
# library statements
stmts <- lapply(lib,
- function (l) {
- return (sprintf("library(%s);", l));
+ function (libcmd) {
+ verb <- libcmd[1];
+ if (verb == "library") {
+ return (sprintf("library(%s);", libcmd[2]));
+ }
+ else if (verb == "detach") {
+ return (sprintf("try(detach(package:%s),silent=T);",
+ libcmd[2]));
+ }
+ else {
+ error(paste("invalid verb ", verb, " in ",
+ ".swift.packages option"))
+ }
})
libstr = paste(stmts, collapse=" ")
+ cat("libstr: ", libstr)
}
else {
libstr = ""
@@ -270,31 +282,93 @@
swiftapply(func, arglists)
}
-swiftLibrary <- function (newlib, reset=FALSE)
-{
+swiftRemoveAll <- function () {
+ # Cleans up all data in global namespace on workers
+ options(.swift.exports=list(c("removeAll")))
+}
+
+
+swiftLibrary <- function (packname) {
# Add a library to be included on all workers
# The package can be provided directly or alternatively
# the package name can be provided.
- if (reset) {
- libs = list()
+ # If reset is true, then previously added libraries won't be
+ # reimported on new workers.
+
+ # Check to see if it is a string, if it is not a string
+ # then we will deparse it to get the expression text
+ if (!is.character(substitute(packname))) {
+ # Maybe library was provided unquoted
+ packname <- deparse(substitute(packname))
}
+ if (! packname %in% installed.packages()) {
+ # Warn users in case they made a typo
+ warning(paste("Package", packname,
+ "was not a installed package in this instance of R,",
+ "but may be installed on SwiftR workers"))
+ }
+ plist <- getOption(".swift.packages")
+ if (is.null(plist)) {
+ plist <- list()
+ }
else {
- libs <- getOption("swift.libraries")
- if (is.null(libs))
- libs <- list()
+ # scan libs for previous detaches and libraries of
+ # this package. Delete all of them
+ plist = Filter(function(packcmd)
+ { return (packcmd[2] != packname);},
+ plist)
}
- for (l in newlib) {
- if (!is.character(l)) {
- # Maybe library was provided unquoted
- #FIXME: doesn't work because l is evaluated above
- l <- deparse(substitute(l))
- }
- libs[[length(libs) + 1]] <- l
+ # Add the library command to the end of the list of commands to be
+ # executed in slave R instances
+ plist[[length(plist) + 1]] <- c("library", packname)
+
+ options(.swift.packages=plist)
+}
+
+
+swiftDetach <- function (name) {
+ # name is an string or identifier such as "package:OpenMx",
+ # following the same pattern as the built in R detach() function
+ # Detaches a library from workers and ensures that it will no longer be
+ # imported. Note that the effect on dependent imports is a little
+ # messy and behaves differently on existing and new workers.
+ # If package A requires package B, and we import A on a worker,
+ # package B will also be imported on the worker. By detaching package A
+ # we don't also detach B. This contrasts to a fresh worker, where
+ # package B will not be imported.
+ if (!is.character(substitute(name))) {
+ # Maybe package was provided unquoted
+ name <- deparse(substitute(name))
}
- options(swift.libraries=libs)
+
+ #TODO: remove from options(".swift.packages")
+ # Scan through list and remove any attach or detach of this name
+ # add a detach cmd to the end
+ if (substr(name, 1, 8) != "package:")
+ stop("Can only detach packages. Package must be specified as package:PackageName")
+ # Get bit after "package:"
+ packname = substr(name, 9, nchar(name))
+ plist = getOption(".swift.packages")
+ if (is.null(plist)) {
+ plist = list()
+ }
+ else {
+ # Previous export/detach commands are now redundant
+ plist = Filter(function(packcmd)
+ { return (packcmd[2] != packname);},
+ plist)
+ }
+ plist[[length(plist) + 1]] = c("detach", packname)
+
+ options(.swift.packages=plist)
}
+
+swiftExportAll <- function () {
+ return (swiftExport(list=ls(globalenv())))
+}
+
#TODO: not implemented
-.swiftExport <- function (...) {
+.swiftExport <- function (..., list=NULL) {
# List of object names (as R symbols or as strings)
# These will be passed directly to save() to be serialized
@@ -332,4 +406,16 @@
# swiftRemove()
# Implementation of this function is somewhat problematic, as it only
# partially undoes previous work
+
+
+ #Pseudocode:
+ # TODO: choose file location
+ # expFile <- ???
+ save(..., list=list, file=expFile)
+ exportList <- getOption(".swift.exports")
+ if (is.null(exportList))
+ exportList = list() #TODO: start with removeAll command?
+ exportList[[length(exportList) + 1]] = c("export", expFile)
+ options(.swift.exports=exportList)
+
}
More information about the Swift-commit
mailing list