Gaudi Framework, version v23r0

Home   Generated: Mon Jan 30 2012

Main.py

Go to the documentation of this file.
00001 import sys, os
00002 from time import time
00003 from Gaudi import Configuration
00004 import logging
00005 
00006 log = logging.getLogger(__name__)
00007 
00008 class gaudimain(object) :
00009     # main loop implementation, None stands for the default
00010     mainLoop = None
00011 
00012     def __init__(self) :
00013         from Configurables import ApplicationMgr
00014         appMgr = ApplicationMgr()
00015         if "GAUDIAPPNAME" in os.environ:
00016             appMgr.AppName = str(os.environ["GAUDIAPPNAME"])
00017         if "GAUDIAPPVERSION" in os.environ:
00018             appMgr.AppVersion = str(os.environ["GAUDIAPPVERSION"])
00019         self.log = logging.getLogger(__name__)
00020 
00021     def setupParallelLogging( self ) :
00022         # ---------------------------------------------------
00023         # set up Logging
00024         # ----------------
00025         # from multiprocessing import enableLogging, getLogger
00026         import multiprocessing
00027         # preliminaries for handlers/output files, etc.
00028         from time import ctime
00029         datetime = ctime()
00030         datetime = datetime.replace(' ', '_')
00031         outfile = open( 'gaudirun-%s.log'%(datetime), 'w' )
00032         # two handlers, one for a log file, one for terminal
00033         streamhandler = logging.StreamHandler(strm=outfile)
00034         console       = logging.StreamHandler()
00035         # create formatter : the params in parentheses are variable names available via logging
00036         formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
00037         # add formatter to Handler
00038         streamhandler.setFormatter(formatter)
00039         console.setFormatter(formatter)
00040         # now, configure the logger
00041         # enableLogging( level=0 )
00042         # self.log = getLogger()
00043         self.log = multiprocessing.log_to_stderr()
00044         self.log.setLevel( logging.INFO )
00045         self.log.name = 'Gaudi/Main.py Logger'
00046         self.log.handlers = []
00047         # add handlers to logger : one for output to a file, one for console output
00048         self.log.addHandler(streamhandler)
00049         self.log.addHandler(console)
00050         self.log.removeHandler(console)
00051         # set level!!
00052         self.log.setLevel = logging.INFO
00053         # ---------------------------------------------------
00054 
00055     def generatePyOutput(self, all = False):
00056         from pprint import pformat
00057         conf_dict = Configuration.configurationDict(all)
00058         return pformat(conf_dict)
00059 
00060     def generateOptsOutput(self, all = False):
00061         from pprint import pformat
00062         conf_dict = Configuration.configurationDict(all)
00063         out = []
00064         names = conf_dict.keys()
00065         names.sort()
00066         for n in names:
00067             props = conf_dict[n].keys()
00068             props.sort()
00069             for p in props:
00070                 out.append('%s.%s = %s;' % (n,p, repr(conf_dict[n][p])))
00071         return "\n".join(out)
00072 
00073     def _writepickle(self, filename) :
00074         #--- Lets take the first file input file as the name of the pickle file
00075         import pickle
00076         output = open(filename, 'wb')
00077         # Dump only the the configurables that make sense to dump (not User ones)
00078         from GaudiKernel.Proxy.Configurable import getNeededConfigurables
00079         to_dump = {}
00080         for n in getNeededConfigurables():
00081             to_dump[n] = Configuration.allConfigurables[n]
00082         pickle.dump(to_dump, output, -1)
00083         output.close()
00084 
00085     def printconfig(self, old_format = False, all = False) :
00086         msg = 'Dumping all configurables and properties'
00087         if not all:
00088             msg += ' (different from default)'
00089         log.info(msg)
00090         conf_dict = Configuration.configurationDict(all)
00091         if old_format:
00092             print self.generateOptsOutput(all)
00093         else:
00094             print self.generatePyOutput(all)
00095 
00096     def writeconfig(self, filename, all = False):
00097         write = { ".pkl" : lambda filename, all: self._writepickle(filename),
00098                   ".py"  : lambda filename, all: open(filename,"w").write(self.generatePyOutput(all) + "\n"),
00099                   ".opts": lambda filename, all: open(filename,"w").write(self.generateOptsOutput(all) + "\n"),
00100                 }
00101         from os.path import splitext
00102         ext = splitext(filename)[1]
00103         if ext in write:
00104             write[ext](filename, all)
00105         else:
00106             log.error("Unknown file type '%s'. Must be any of %r.", ext, write.keys())
00107             sys.exit(1)
00108 
00109     def printsequence(self):
00110         def printAlgo( algName, appMgr, prefix = ' ') :
00111             print prefix + algName
00112             alg = appMgr.algorithm( algName.split( "/" )[ -1 ] )
00113             prop = alg.properties()
00114             if prop.has_key( "Members" ) :
00115                 subs = prop[ "Members" ].value()
00116                 for i in subs : printAlgo( i.strip( '"' ), appMgr, prefix + "     " )
00117             elif prop.has_key( "DetectorList" ) :
00118                 subs = prop[ "DetectorList" ].value()
00119                 for i in subs : printAlgo( algName.split( "/" )[ -1 ] + i.strip( '"' ) + "Seq", appMgr, prefix + "     ")
00120         import GaudiPython
00121         self.g = GaudiPython.AppMgr()
00122         mp = self.g.properties()
00123         print "\n ****************************** Algorithm Sequence **************************** \n"
00124         for i in mp["TopAlg"].value(): printAlgo( i, self.g )
00125         print "\n ****************************************************************************** \n"
00126 
00127     ## Instantiate and run the application.
00128     #  Depending on the number of CPUs (ncpus) specified, it start
00129     def run(self, ncpus = None):
00130         if not ncpus:
00131             # Standard sequential mode
00132             result = self.runSerial()
00133         else:
00134             # Otherwise, run with the specified number of cpus
00135             result = self.runParallel(ncpus)
00136         return result
00137 
00138 
00139     def runSerial(self) :
00140         #--- Instantiate the ApplicationMgr------------------------------
00141         import GaudiPython
00142         self.log.debug('-'*80)
00143         self.log.debug('%s: running in serial mode', __name__)
00144         self.log.debug('-'*80)
00145         sysStart = time()
00146         self.g = GaudiPython.AppMgr()
00147         runner = self.mainLoop or (lambda app, nevt: app.run(nevt))
00148         statuscode = runner(self.g, self.g.EvtMax)
00149         if hasattr(statuscode, "isSuccess"):
00150             success = statuscode.isSuccess()
00151         else:
00152             success = statuscode
00153         success = self.g.exit().isSuccess() and success
00154         if not success and self.g.ReturnCode == 0:
00155             # ensure that the return code is correctly set
00156             self.g.ReturnCode = 1
00157         sysTime = time()-sysStart
00158         self.log.debug('-'*80)
00159         self.log.debug('%s: serial system finished, time taken: %5.4fs', __name__, sysTime)
00160         self.log.debug('-'*80)
00161         return self.g.ReturnCode
00162 
00163     def runParallel(self, ncpus) :
00164         if self.mainLoop:
00165             self.log.fatal("Cannot use custom main loop in multi-process mode, check your options")
00166             return 1
00167         self.setupParallelLogging( )
00168         from Gaudi.Configuration import Configurable
00169         import GaudiMP.GMPBase as gpp
00170         c = Configurable.allConfigurables
00171         self.log.info('-'*80)
00172         self.log.info('%s: Parallel Mode : %i '%(__name__, ncpus))
00173         from commands import getstatusoutput as gso
00174         metadataCommands = [ 'uname -a',
00175                              'echo $CMTCONFIG',
00176                              'echo $GAUDIAPPNAME',
00177                              'echo $GAUDIAPPVERSION']
00178         for comm in metadataCommands :
00179             s, o = gso( comm )
00180             if s :
00181                 o = "Undetermined"
00182             string = '%s: %30s : %s '%(__name__, comm, o)
00183             self.log.info( string )
00184         try :
00185             events = str(c['ApplicationMgr'].EvtMax)
00186         except :
00187             events = "Undetermined"
00188         self.log.info('%s: Events Specified : %s '%(__name__,events))
00189         self.log.info('-'*80)
00190         # Parall = gpp.Coordinator(ncpus, shared, c, self.log)
00191         Parall = gpp.Coord( ncpus, c, self.log )
00192         sysStart = time()
00193         sc = Parall.Go()
00194         self.log.info('MAIN.PY : received %s from Coordinator'%(sc))
00195         if sc.isFailure() :
00196             return 1
00197         sysTime = time()-sysStart
00198         self.log.name = 'Gaudi/Main.py Logger'
00199         self.log.info('-'*80)
00200         self.log.info('%s: parallel system finished, time taken: %5.4fs', __name__, sysTime)
00201         self.log.info('-'*80)
00202         return 0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Jan 30 2012 13:52:45 for Gaudi Framework, version v23r0 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004