[mlpack-svn] r15661 - in mlpack/conf/jenkins-conf/benchmark: methods/mlpy util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Aug 26 05:44:54 EDT 2013


Author: marcus
Date: Mon Aug 26 05:44:52 2013
New Revision: 15661

Log:
Add new timeout function to handel threads.

Modified:
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/allknn.py
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kernel_pca.py
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kmeans.py
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/lars.py
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/linear_regression.py
   mlpack/conf/jenkins-conf/benchmark/methods/mlpy/pca.py
   mlpack/conf/jenkins-conf/benchmark/util/timer.py

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/allknn.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/allknn.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/allknn.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def AllKnnMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunAllKnnMlpy():
+    def RunAllKnnMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -70,12 +68,14 @@
         k = re.search("-k (\d+)", options)
         if not k:
           Log.Fatal("Required option: Number of furthest neighbors to find.")
+          q.put(-1)
           return -1
         else:
           k = int(k.group(1))
           if (k < 1 or k > referenceData.shape[0]):
             Log.Fatal("Invalid k: " + k.group(1) + "; must be greater than 0 and "
               + "less ")
+            q.put(-1)
             return -1
 
         # Perform All K-Nearest-Neighbors.
@@ -87,13 +87,12 @@
         else:
           out = model.pred(referenceData)
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunAllKnnMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunAllKnnMlpy, self.timeout)
+    
 
   '''
   Perform All K-Nearest-Neighbors. If the method has been successfully completed 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kernel_pca.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kernel_pca.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kernel_pca.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def KPCAMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunKPCAMlpy():
+    def RunKPCAMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -65,6 +63,7 @@
           if (d > data.shape[1]):
             Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater "
               + "than existing dimensionality (" + str(data.shape[1]) + ")!")
+            q.put(-1)
             return -1    
 
         # Get the kernel type and make sure it is valid.
@@ -72,6 +71,7 @@
         if not kernel:
             Log.Fatal("Choose kernel type, valid choices are 'polynomial', " + 
                   "'gaussian', 'linear' and 'hyptan'.")
+            q.put(-1)
             return -1
         elif kernel.group(1) == "polynomial":
           degree = re.search('-D (\d+)', options)
@@ -87,6 +87,7 @@
         else:
           Log.Fatal("Invalid kernel type (" + kernel.group(1) + "); valid " +
                   "choices are 'polynomial', 'gaussian', 'linear' and 'hyptan'.")
+          q.put(-1)
           return -1
 
         # Perform Kernel Principal Components Analysis.
@@ -94,13 +95,11 @@
         model.learn(kernel)
         out = model.transform(kernel, k=d)
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunKPCAMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunKPCAMlpy, self.timeout)
 
   '''
   Perform Kernel Principal Components Analysis. If the method has been 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kmeans.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kmeans.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/kmeans.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def KMeansMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunKMeansMlpy():
+    def RunKMeansMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -62,10 +60,12 @@
       # Now do validation of options.
       if not clusters:
         Log.Fatal("Required option: Number of clusters or cluster locations.")
+        q.put(-1)
         return -1
       elif int(clusters.group(1)) < 1:
         Log.Fatal("Invalid number of clusters requested! Must be greater than or "
             + "equal to 1.")
+        q.put(-1)
         return -1
 
       with totalTimer:
@@ -75,13 +75,11 @@
         else:
           kmeans = mlpy.kmeans(data, int(clusters.group(1)))
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunKMeansMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunKMeansMlpy, self.timeout)
 
   '''
   Perform K-Means Clustering. If the method has been successfully completed 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/lars.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/lars.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/lars.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def LARSMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunLARSMlpy():
+    def RunLARSMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -62,13 +60,11 @@
         model.learn(inputData, responsesData)
         out = model.beta()
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunLARSMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunLARSMlpy, self.timeout)
 
   '''
   Perform Least Angle Regression. If the method has been successfully completed 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/linear_regression.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/linear_regression.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/linear_regression.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def LinearRegressionMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunLinearRegressionMlpy():
+    def RunLinearRegressionMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -69,13 +67,11 @@
         model.learn(X, y)
         b =  model.beta()
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunLinearRegressionMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunLinearRegressionMlpy, self.timeout)
 
   '''
   Perform Linear Regression. If the method has been successfully completed 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/mlpy/pca.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/mlpy/pca.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/mlpy/pca.py	Mon Aug 26 05:44:52 2013
@@ -46,9 +46,7 @@
   @return - Elapsed time in seconds or -1 if the method was not successful.
   '''
   def PCAMlpy(self, options):
-
-    @timeout(self.timeout, os.strerror(errno.ETIMEDOUT))
-    def RunPCAMlpy():
+    def RunPCAMlpy(q):
       totalTimer = Timer()
 
       # Load input dataset.
@@ -66,6 +64,7 @@
           if (k > data.shape[1]):
             Log.Fatal("New dimensionality (" + str(k) + ") cannot be greater "
                 + "than existing dimensionality (" + str(data.shape[1]) + ")!")
+            q.put(-1)
             return -1
 
         # Get the options for running PCA.
@@ -76,13 +75,11 @@
         prep.learn(data)
         out = prep.transform(data, k)      
 
-      return totalTimer.ElapsedTime()
+      time = totalTimer.ElapsedTime()
+      q.put(time)
+      return time
 
-    try:
-      return RunPCAMlpy()
-    except TimeoutError as e:
-      Log.Warn("Script timed out after " + str(self.timeout) + " seconds")
-      return -2
+    return timeout(RunPCAMlpy, self.timeout)
 
   '''
   Perform Principal Components Analysis. If the method has been successfully 

Modified: mlpack/conf/jenkins-conf/benchmark/util/timer.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/timer.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/timer.py	Mon Aug 26 05:44:52 2013
@@ -6,11 +6,21 @@
 '''
 
 from __future__ import with_statement
-import time
-from functools import wraps
-import errno
 import os
-import signal
+import sys
+import inspect
+
+# Import the util path, this method even works if the path contains symlinks to 
+# modules.
+cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(
+  os.path.split(inspect.getfile(inspect.currentframe()))[0], "util")))
+if cmd_subfolder not in sys.path:
+  sys.path.insert(0, cmd_subfolder)
+
+from log import *
+
+import time
+from multiprocessing import Process, Queue
 
 '''
 This class implements three functions to measure the time.
@@ -35,32 +45,24 @@
     return self.__finish - self.__start
 
 '''
-This Class provides a timeout error.
-'''
-class TimeoutError(Exception):
-    errno = 23
-
-'''
 This function implements a timeout for a function call.
 
- at param seconds - The time until the timeout. Default 5000 seconds.
- at param errorMessage - Message for the Error when the timeout is invoked.
- at return Timeout error.
-'''
-def timeout(seconds=5000, errorMessage=os.strerror(errno.ETIME)):
-    def decorator(func):
-        def handleTimeout(signum, frame):
-            raise TimeoutError(errorMessage)
-
-        def wrapper(*args, **kwargs):
-            signal.signal(signal.SIGALRM, handleTimeout)
-            signal.setitimer(signal.ITIMER_REAL,seconds)
-            try:
-                result = func(*args, **kwargs)
-            finally:
-                signal.alarm(0)
-            return result
-
-        return wraps(func)(wrapper)
-
-    return decorator
+ at param fun - Start the process with the given function.
+ at param timeout - The time until the timeout. Default 9000 seconds.
+ at return The return value of the process.
+'''
+def timeout(fun, timeout=9000):
+  q = Queue()
+  p = Process(target=fun, args=(q,))
+  p.start()
+  p.join(timeout)
+
+  if p.is_alive():
+    # Terminate the process.
+    p.terminate()
+    p.join()
+
+    Log.Warn("Script timed out after " + str(timeout) + " seconds")
+    return -2
+  else:
+    return q.get()



More information about the mlpack-svn mailing list