import ocaml ocaml.mpi_status() ocaml.mpi_hello()
def setup(argv=None, do_features=True, do_logging=True, do_welcome=None, log_to_console_only=None, warn_about_py_ext=True): """Carry out the various stages of the setup of Nsim. do_cmdline: the command line is parsed using the OptionParser object stored inside cmdline_parser variable. If it has not been set by the user, then it is automatically generated in this function. """ # If the setup has been marked as completed then exit immediately if task_done['completed']: return # Check once for all what really needs to be done do_features = do_features and not task_done['features'] do_cmdline = (argv != None) and not task_done['cmdline'] do_logging = do_logging and not task_done['logging'] do_welcome = (do_welcome == True or (do_welcome == None and not task_done['welcome'])) # We first parse the command line (options, arguments) = (None, None) if do_cmdline: global cmdline_parser if cmdline_parser == None: cmdline_parser = generate_cmdline_parser() (options, arguments) = cmdline_parser.parse_args(argv) is_interactive = (len(arguments) == 0) # Deal here with some of the command line args if options.forcelog == None: log_to_console_only = is_interactive elif options.forcelog == True: log_to_console_only = False else: log_to_console_only = True task_done['cmdline'] = True # We would like now to setup the ocaml and python feature objects. # First, however, we need to determine the name of the logging file, since # it is required in order to construct correctly the pyfeature object. savedir = '.' runid = get_nmag_runid(arguments) if options and options.logfilename: logfilename = options.logfilename log_to_console_only = False else: logfilename = runid + '_log.log' logfilepath = os.path.join(savedir, logfilename) # We now find out where we should read the configuration for logging logconfigfile = None if options != None and options.logconfigfile != None: logconfigfile = options.logconfigfile # We can now construct the feature objects global pyfeatures, ocamlfeatures if do_features: pyfeatures = \ nsim.features.Features(defaults={'logfilepath':logfilepath}) ocamlfeatures = nsim.features.OcamlFeatures() # We now determine the exact name of the config file which we should # load in order to setup the logger nmaglibrarypath = nsim.snippets.get_absolute_librarypath(__file__)[0] if logconfigfile == None: # The user has not provided his own configuration file for logging # we then have to use one of the default files. if log_to_console_only: logconfigfile = "logging-console.conf" else: logconfigfile = "logging.conf" # We need to prepend the path where this files can be found # (which doesn't make sense if the user has provided his own file) logconfigfile = os.path.join(nmaglibrarypath, logconfigfile) else: # The option log_to_console_only does not make sense in this case. # We may 'assert log_to_console_only == None', but we don't. # We just ignore the issue, this is the best thing to do! pass # We finally fill the pyfeatures object with default values (to be # overridden by command line options) ocamlconfigfile = os.path.join(nmaglibrarypath, 'ocaml.conf') pyfeatures.set('nmag', 'ocamlconfigfile', ocamlconfigfile) pyfeatures.set('nmag', 'logconfigfile', logconfigfile) pyfeatures.set('nmag', 'loglevel', 'info') pyfeatures.set('etc', 'runid', runid) pyfeatures.set('etc', 'mainprogram', 'nmag') pyfeatures.set('etc', 'savedir', savedir) # Location of output files # Move any settings from pyfeatures to OCaml's features living in # 'snippets' pyfeatures.to_ocaml_features() task_done['features'] = True # Interpret the command line arguments and override settings in pyfeatures if do_cmdline: cmdline_to_pyfeatures(options, arguments) # We rename the files here for two reasons: # - we have to do it before the log file is created # (before the 'do_logging' section) # - we have to do it after the command line has been parsed # (and only if the command line has been parsed?), after the config # file has been read and we hence know what is the name of the log # file. if pyfeatures.get('nmag', 'clean', raw=True): nsim.snippets.rename_old_files([logfilename]) # We are now ready to setup the logger global log log = logging.getLogger('nsim') if do_logging: # Setup logging from default logging configuration file nsim.logtools.setup_loggers(logconfigfile, logfilepath) # Last, we need to set the global log level nsim.logtools.set_global_loglevel(pyfeatures.get('nmag', 'loglevel')) # All loggers are set up. setup_ocaml_loggers() log.debug('current logging status is \n%s' % nsim.logtools.logging_status_str()) task_done['logging'] = True # Just a warning message about the extension of the file if warn_about_py_ext and do_cmdline and len(argv) > 0: _, script_extension = os.path.splitext(os.path.split(argv[0])[1]) if script_extension.lower() != ".py": msg = ("Nmag scripts need to have the .py extension. Will wait " "2 seconds before continuing...") log.warn(msg) nsim.snippets.funky_wait(2) if do_cmdline: setup_things(options, arguments) if do_welcome: import ocaml log.debug("Sundials library path ='%s'" % ocaml.get_nsim_sundials_library_path()) nr_cpus = ocaml.petsc_mpi_nr_nodes() log.info("Nsim %s" % get_version_string()) log.info("Runid is '%s'" % (pyfeatures.get('etc', 'runid'))) log.info("Using %d CPUs" % (nr_cpus)) if nr_cpus > 1: log.info("Waiting 1 seconds for messages from slaves to arrive " "(experimental)") ocaml.mpi_hello() time.sleep(1) task_done['welcome'] = True return (options, arguments)