| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines