[Swift-commit] r3999 - in SwiftApps/SwiftR/Swift: R exec

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Wed Jan 19 13:32:48 CST 2011


Author: tga
Date: 2011-01-19 13:32:47 -0600 (Wed, 19 Jan 2011)
New Revision: 3999

Added:
   SwiftApps/SwiftR/Swift/R/Workers.R
   SwiftApps/SwiftR/Swift/exec/start-swift-daemon
Modified:
   SwiftApps/SwiftR/Swift/R/Swift.R
Log:
Checking in work on launching start-swift within R.
All logic to launch and track processes is present, but TERM signal is not
correctly taking down the worker processes at this stage: need to fix.


Modified: SwiftApps/SwiftR/Swift/R/Swift.R
===================================================================
--- SwiftApps/SwiftR/Swift/R/Swift.R	2011-01-18 23:59:39 UTC (rev 3998)
+++ SwiftApps/SwiftR/Swift/R/Swift.R	2011-01-19 19:32:47 UTC (rev 3999)
@@ -193,6 +193,9 @@
   options(swift.initialexpr=initcmds) # Set here; used in test group 4
 }
 
+
+
+
 swiftTest_1.1 <- function()
 {
 

Added: SwiftApps/SwiftR/Swift/R/Workers.R
===================================================================
--- SwiftApps/SwiftR/Swift/R/Workers.R	                        (rev 0)
+++ SwiftApps/SwiftR/Swift/R/Workers.R	2011-01-19 19:32:47 UTC (rev 3999)
@@ -0,0 +1,147 @@
+
+
+
+swiftInit <- function( cores=NULL, server=NULL, 
+                    hosts=NULL, nodes=NULL, project=NULL, 
+                    parEnv=NULL, workmode=NULL,
+                    throttle=NULL, queue=NULL,
+                    rcmd=NULL, time=NULL,
+                    workerLogging=NULL )
+{
+    #TODO: document function
+    # server: which server backend to use to acquire workers
+    #           for example, local runs tasks on the local machine
+    #           pbs, uses the PBS scheduler to obtain nodes on a cluster,
+    #           etc
+    # hosts: list of hosts to use (for ssh backend)
+    # nodes: number of hosts to use (for cluster-based backends)
+    # cores: number of cores per host to use #TODO: check
+    # time: (pbs and sge servers only) walltime in hh:mm:ss
+    #       Default is 30 minutes on these servers, unlimited
+    #       elsewhere
+    # wkloglvl: logging level.  Settings are NONE, ERROR, WARn, INFO,
+    #               DEBUG, TRACE
+    # Options which are server and site-specific:
+    #   project, queue
+
+
+    # In case it was somehow deleted
+    if (is.null(.swift.workers)) {
+        .swift.workers <<- list()
+    }
+
+    # Find out where start-swift script lives in this
+    # R installation
+    # Presume UNIX path names - start-swift script
+    cmdString <- file.path(.find.package("Swift"), "exec/start-swift-daemon")
+
+    if(! is.null(cores) )  {
+        cmdString <- paste(cmdString, "-c", cores)
+    }
+    if(! is.null(server) )  {
+        cmdString <- paste(cmdString, "-s", server) 
+    }
+    if(! is.null(hosts) )  {
+        cmdString <- paste(cmdString, "-h", hosts) 
+    }
+    if(! is.null(parEnv) )  {
+        cmdString <- paste(cmdString, "-e", parEnv) 
+    }
+    if(! is.null(workmode) )  {
+        cmdString <- paste(cmdString, "-m", workmode) 
+    }
+    if(! is.null(nodes) )  {
+        cmdString <- paste(cmdString, "-n", nodes) 
+    }
+    if(! is.null(throttle) )  {
+        cmdString <- paste(cmdString, "-p", throttle) 
+    }
+    if(! is.null(queue) )  {
+        cmdString <- paste(cmdString, "-q", queue) 
+    }
+    if(! is.null(rcmd) )  {
+        cmdString <- paste(cmdString, "-r", rcmd) 
+    }
+    if(! is.null(time) )  {
+        cmdString <- paste(cmdString, "-t", time) 
+    }
+    if(! is.null(workerLogging) )  {
+        cmdString <- paste(cmdString, "-w", workerLogging) 
+    }
+    
+
+
+    # launch asynchronously
+    # for now, we will rely on the shell script's output to inform
+    #  the user if there was a problem with the workers
+    output <- system(cmdString, intern=TRUE)
+    cat("Started worker manager with pid ", output, "\n")
+
+    # store pid
+    .swift.workers[[length(.swift.workers) + 1]] <<- output
+
+    # add hook to ensure child process will be killed when 
+    # this process exits
+    addHook()
+}
+
+swiftShutdown <- function() 
+{
+    if (is.null(.swift.workers)) {
+        return
+    }
+    cat("Shutting down Swift worker processes\n")
+    # shut down all worker processes using kill
+    for (pid in .swift.workers) {
+        cat("Killing ", pid, "\n")
+        killCmd <- paste("kill","-1", pid, " &> /dev/null")
+        cat(killCmd, "\n")
+        system(killCmd, wait=FALSE)
+    }
+
+    .swift.workers <<- list()
+
+}
+
+.First.lib <- function(libname, packagename) {
+    # When the library is loaded, set up the
+    # list of workers
+    .swift.workers <<- list()
+}
+
+.Last.lib <- function(p) 
+{
+    # If the library is unloaded we need to do cleanup
+    swiftShutdown()
+}
+
+# Hook to perform cleanup of workers upon shutting down an R
+# session
+addHook <- function() {
+    # Replace the user's last function with ours
+    # If .UserLast already exists don't worry about it
+    # as we've already added our hook
+    if (!exists(".UserLast")) {
+        if (!exists(".Last")) {
+            # Create a dummy function
+            .UserLast <<- function () {}
+    
+        }
+        else {
+            .UserLast <<- .Last
+        }
+        
+        .Last <<- function () {
+            swiftShutdown()
+            .UserLast()
+            removeHook()
+        }
+    }
+}
+
+removeHook <- function() {
+    if (exists(".UserLast", where=".GlobalEnv")) {
+        .Last <<- .UserLast
+        rm(".UserLast", pos=".GlobalEnv")
+    }
+}

Added: SwiftApps/SwiftR/Swift/exec/start-swift-daemon
===================================================================
--- SwiftApps/SwiftR/Swift/exec/start-swift-daemon	                        (rev 0)
+++ SwiftApps/SwiftR/Swift/exec/start-swift-daemon	2011-01-19 19:32:47 UTC (rev 3999)
@@ -0,0 +1,14 @@
+#!/bin/sh
+# This script is intended as a helper script to let
+# R launch start-swift.  It works around the limitations
+# of the R system command, which cannot retrieve
+# the process id of the child process.
+# This script forks off a child process, detaches it
+# and then, as the only thing written to stdout, echoes
+# the pid of start-swift
+ssscript=`dirname $0`/start-swift
+# Start as detached daemon, with output going to stdout
+$ssscript "$@" 1>&2 &
+childpid=$!
+echo ${childpid}
+disown $childpid


Property changes on: SwiftApps/SwiftR/Swift/exec/start-swift-daemon
___________________________________________________________________
Name: svn:executable
   + *




More information about the Swift-commit mailing list