Пример #1
0
 def _context_values(self):
     """Determine values depending on the context"""
     # computational node
     self.add_entry(Entry("node", local_host))
     self.add_entry(EntryAlias("noeud", self.get_entry("node")))
     # platform
     if on_windows():
         if on_64bits():
             platform = "WIN64"
         else:
             platform = "WIN32"
     else:
         platform = "LINUX"
         if on_64bits():
             platform = "LINUX64"
     self.add_entry(Entry("platform", platform))
     self.add_entry(EntryAlias("plate-forme", self.get_entry("platform")))
     # editor
     #TODO add gedit, kate.../gnome-terminal, konsole + display
     editor = _test_alternatives("EDITOR", [
         CommandLine("/usr/bin/editor"),
         CommandLine("/usr/bin/nedit"),
     ])
     self.add_entry(Entry("editor", editor))
     # terminal
     terminal = _test_alternatives("TERM", [
         CommandLine("/usr/bin/x-terminal-emulator"),
         CommandLine("/usr/bin/xterm"),
         CommandLine("gnome-terminal", "--execute", "@E"),
     ])
     self.add_entry(Entry("terminal", terminal))
Пример #2
0
 def GetMemInfo(self, what, mach='', user=''):
     """Return memory information.
     """
     if self._meminfo_cache.get(what + mach + user) is not None:
         return self._meminfo_cache[what + mach + user]
     if what in ('memtotal', ):
         num = None
         if on_linux():
             iret, out = self.Shell('cat /proc/meminfo', mach, user)
             mat = re.search('^%s *: *([0-9]+) *kb' % what, out,
                             re.MULTILINE | re.IGNORECASE)
             if mat != None:  # else: it should not !
                 num = int(mat.group(1)) / 1024
                 if not on_64bits():
                     num = min(num, 2047)
         self._meminfo_cache[what + mach + user] = num
         self._dbg("GetMemInfo '%s' returns : %s" % (what, num))
         return num
     else:
         return None
Пример #3
0
def execute(reptrav, multiple=False, with_dbg=False, only_env=False,
            follow_output=True, fpara=None, facmtps=1.,
            runner=None, **kargs):
    """Run a Code_Aster execution in 'reptrav'.
    Arguments :
        multiple : False if only one execution is run (so stop if it fails),
            True if several executions are run (don't stop when error occurs)
        with_dbg : start debugger or not,
        fpara    : deprecated,
        follow_output : print output to follow the execution,
    kargs give "run, conf, prof, build" instances + exec name
    Return a tuple (diag, tcpu, tsys, ttot, validbase).
    """
    # 1. ----- initializations
    run    = kargs['run']
    conf   = kargs['conf']
    prof   = kargs['prof']
    build  = kargs['build']
    exetmp = kargs['exe']
    ctest  = prof['parent'][0] == "astout"
    waf_inst = build.support('waf')
    waf_nosupv = build.support('nosuperv')
    waf_orb = build.support('orbinitref')
    use_numthreads = build.support('use_numthreads')
    run.DBG("version supports: waf ({0}), nosuperv ({1}), "
            "orbinitref ({2}), numthreads ({3})".format(waf_inst, waf_nosupv,
            waf_orb, use_numthreads))
    if not waf_inst:
        exetmp = osp.join('.', osp.basename(exetmp))
    tcpu = 0.
    tsys = 0.
    ttot = 0.
    validbase = True
    if runner is None:
        runner = Runner()
        runner.set_rep_trav(reptrav)

    interact = prof.args.has_key('interact') or \
              prof.args.get('args', '').find('-interact') > -1

    os.chdir(reptrav)

    # 2. ----- list of command files
    list_comm = glob('fort.1.*')
    list_comm.sort()
    if osp.exists('fort.1'):
        list_comm.insert(0, 'fort.1')

    # 3. ----- arguments list
    drep = { 'REPOUT' : 'rep_outils', 'REPMAT' : 'rep_mat', 'REPDEX' : 'rep_dex' }
    cmd = []
    if waf_nosupv:
        cmd.append('fort.1')
    else:
        if waf_inst:
            cmd.append(osp.join(conf['SRCPY'][0], conf['ARGPYT'][0]))
        else:
            cmd.append(osp.join(conf['REPPY'][0], conf['ARGPYT'][0]))
            cmd.extend(conf['ARGEXE'])
        # warning: using --commandes will turn off backward compatibility
        cmd.append('-commandes fort.1')
        # cmd.append(_fmtoption('command', 'fort.1'))

    # remove deprecated options
    long_opts_rm = ['rep', 'mem', 'mxmemdy', 'memory_stat', 'memjeveux_stat',
                    'type_alloc', 'taille', 'partition',
                    'origine', 'eficas_path']
    # for version < 12.6/13.2 that does not support --ORBInitRef=, ignore it
    if not waf_orb:
        long_opts_rm.append('ORBInitRef')
    cmd_memtps = {}
    for k, v in prof.args.items():
        if k == 'args':
            cmd.append(prof.args[k])
        elif k in long_opts_rm:
            warn("this command line option is deprecated : --%s" % k,
                 DeprecationWarning, stacklevel=3)
        elif k in ('memjeveux', 'memjeveux_stat', 'tpmax'):
            cmd_memtps[k] = v
        elif v.strip() == '' and k in drep.values():
            run.Mess(_(u'empty value not allowed for "%s"') % k, '<A>_INVALID_PARAMETER')
        else:
            cmd.append(_fmtoption(k, v))

    # add arguments to find the process (for as_actu/as_del)
    if not 'astout' in prof['actions'] and not 'distribution' in prof['actions']:
        cmd.append(_fmtoption('num_job', run['num_job']))
        cmd.append(_fmtoption('mode', prof['mode'][0]))

    # arguments which can be set in file 'config.txt'
    for kconf, karg in drep.items():
        if conf[kconf][0] != '' and not karg in prof.args.keys():
            cmd.append(_fmtoption(karg, conf[kconf][0]))

    ncpus = prof['ncpus'][0]
    if use_numthreads:
        if ncpus == '':
            ncpus = max([run[prof['mode'][0] + '_nbpmax'] / 2, 1])
        cmd.append(_fmtoption('numthreads', ncpus))
    elif ncpus == '':
        ncpus = '1'

    # 4. ----- add parameters from prof
    if on_64bits():
        facW = 8
    else:
        facW = 4
    tps   = 0
    memj  = 0
    memjs = 0
    nbp   = 0
    try:
        tps = int(float(prof.args['tpmax']))
    except KeyError:
        run.Mess(_(u'tpmax not provided in profile'), '<E>_INCORRECT_PARA')
    except ValueError:
        run.Mess(_(u'incorrect value for tpmax (%s) in profile') % \
                prof.args['tpmax'], '<E>_INCORRECT_PARA')
    try:
        memj = float(prof.args['memjeveux'])
    except KeyError:
        run.Mess(_(u'memjeveux not provided in profile'), '<E>_INCORRECT_PARA')
    except ValueError:
        run.Mess(_(u'incorrect value for memjeveux (%s) in profile') % \
                prof.args['memjeveux'], '<E>_INCORRECT_PARA')

    try:
        memjs = float(prof.args.get('memjeveux_stat', 0))
    except ValueError:
        run.Mess(_(u'incorrect value for memjeveux_stat (%s) in profile') % \
                prof.args['memjeveux_stat'], '<E>_INCORRECT_PARA')

    try:
        nbp = int(ncpus)
    except ValueError:
        run.Mess(_(u'incorrect value for ncpus (%s) in profile') % \
                prof['ncpus'][0], '<E>_INCORRECT_PARA')

    # 4.1. check for memory, time and procs limits
    run.Mess(_(u'Parameters : memory %d MB - time limit %d s') % (memj*facW, tps))
    check_limits(run, prof['mode'][0], tps, memj*facW, nbp, runner.nbnode(), runner.nbcpu())
    # check for previous errors (parameters)
    if not multiple:
        run.CheckOK()
    elif run.GetGrav(run.diag) > run.GetGrav('<A>'):
        run.Mess(_(u'error in parameters : %s') % run.diag)
        return run.diag, tcpu, tsys, ttot, validbase

    # 5. ----- only environment, print command lines to execute
    if only_env:
        run.Mess(ufmt(_(u'Code_Aster environment prepared in %s'), reptrav), 'OK')
        run.Mess(_(u'To start execution copy/paste following lines in a ksh/bash shell :'))
        run.Mess('   cd %s' % reptrav, 'SILENT')
        run.Mess('   . %s' % osp.join(confdir, 'profile.sh'), 'SILENT')
        tmp_profile = "profile_tmp.sh"
        open(tmp_profile, 'w').write("""
export PYTHONPATH=$PYTHONPATH:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
""")
        # set per version environment
        for f in conf.get_with_absolute_path('ENV_SH') + [tmp_profile]:
            run.Mess('   . %s' % f, 'SILENT')

        cmd.insert(0, exetmp)
        # add memjeveux and tpmax
        cmd.extend([_fmtoption(k, v) for k, v in cmd_memtps.items()])
        cmdline = ' '.join(cmd)

        # get pdb.py path
        pdbpy_cmd = "import os, sys ; " \
            "pdbpy = os.path.join(sys.prefix, 'lib', 'python' + sys.version[:3], 'pdb.py')"
        d = {}
        exec pdbpy_cmd in d
        pdbpy = d['pdbpy']

        if runner.really():  #XXX and if not ? perhaps because of exit_code
            cmdline = runner.get_exec_command(cmdline,
                add_tee=False,
                env=conf.get_with_absolute_path('ENV_SH'))

        # print command lines
        k = 0
        for fcomm in list_comm:
            k += 1
            run.Mess(_(u"Command line %d :") % k)
            run.Mess(u"cp %s fort.1" % fcomm, 'SILENT')
            run.Mess(cmdline, 'SILENT')
            # how to start the Python debugger
            if not runner.really():
                run.Mess(_(u'To start execution in the Python debugger you could type :'), 'SILENT')
                pdb_cmd = cmdline.replace(exetmp,
                    '%s %s' % (exetmp, ' '.join(pdbpy.splitlines())))
                run.Mess(u"cp %s fort.1" % fcomm, 'SILENT')
                run.Mess(pdb_cmd, 'SILENT')

        diag = 'OK'

    # 6. ----- really execute
    else:
        # 6.1. content of reptrav
        if not ctest:
            run.Mess(ufmt(_(u'Content of %s before execution'), reptrav), 'TITLE')
            kret, out = run.Shell(cmd='ls -la')
            print3(out)

        if len(list_comm) == 0:
            run.Mess(_(u'no .comm file found'), '<E>_NO_COMM_FILE')
        # check for previous errors (copy datas)
        if not multiple:
            run.CheckOK()
        elif run.GetGrav(run.diag) > run.GetGrav('<A>'):
            run.Mess(_(u'error occurs during preparing data : %s') % run.diag)
            return run.diag, tcpu, tsys, ttot, validbase

        # 6.2. complete command line
        cmd.append('--suivi_batch')
        add_tee = False
        if with_dbg:
            # how to run the debugger
            cmd_dbg = run.config.get('cmd_dbg', '')
            if not cmd_dbg:
                run.Mess(_(u'command line to run the debugger not defined'),
                        '<F>_DEBUG_ERROR')
            if cmd_dbg.find('gdb') > -1:
                ldbg = ['break main', ]
            else:
                ldbg = ['stop in main', ]
            # add memjeveux and tpmax
            update_cmd_memtps(cmd_memtps)
            cmd.extend([_fmtoption(k, v) for k, v in cmd_memtps.items()])
            pos_memtps = -1

            cmd_args = ' '.join(cmd)
            ldbg.append('run ' + cmd_args)
            cmd = getdbgcmd(cmd_dbg, exetmp, '', ldbg, cmd_args)
        else:
            add_tee = True
            cmd.insert(0, exetmp)
            # position where insert memjeveux+tpmax
            pos_memtps = len(cmd)
            # keep for compatibility with version < 13.1
            os.environ['OMP_NUM_THREADS'] = str(ncpus)
            # unlimit coredump size
            try:
                corefilesize = int(prof['corefilesize'][0]) * 1024*1024
            except ValueError:
                corefilesize = 'unlimited'
            run.SetLimit('core', corefilesize)

        # 6.3. if multiple .comm files, keep previous bases
        if len(list_comm) > 1:
            run.Mess(_(u'%d command files') % len(list_comm))
            validbase = False
            BASE_PREC = osp.join(reptrav, 'BASE_PREC')
            run.MkDir(BASE_PREC)

        # 6.4. for each .comm file
        diag = '?'
        diag_ok = None
        k = 0
        for fcomm in list_comm:
            k += 1
            os.chdir(runner.reptrav())
            run.Copy('fort.1', fcomm)

            # start execution
            tit = _(u'Code_Aster run')
            run.timer.Start(tit)

            # add memjeveux and tpmax at the right position
            cmd_i = cmd[:]
            update_cmd_memtps(cmd_memtps)
            if pos_memtps > -1:
                for key, value in cmd_memtps.items():
                    cmd_i.insert(pos_memtps, _fmtoption(key, value))

            if True or not ctest:
                run.Mess(tit, 'TITLE')
                run.Mess(_(u'Command line %d :') % k)
                if not run['verbose']:
                    run.Mess(' '.join(cmd_i))
                if waf_nosupv:
                    dash = "# " + "-" * 90
                    content =[_(u"Content of the file to execute"),
                              dash,
                              to_unicode(open('fort.1', 'rb').read()),
                              dash]
                    run.Mess(os.linesep.join(content))

            cmd_exec = runner.get_exec_command(' '.join(cmd_i),
                add_tee=add_tee,
                env=conf.get_with_absolute_path('ENV_SH'))

            # go
            iret, exec_output = run.Shell(cmd_exec, follow_output=follow_output, interact=interact)
            if iret != 0:
                for f in ('fort.6', 'fort.8', 'fort.9'):
                    run.FileCat(text="""\n <I>_EXIT_CODE = %s""" % iret, dest=f)
            if not follow_output and not ctest:
                print3(exec_output)

            # mpirun does not include cpu/sys time of childrens, add it in timer
            runner.add_to_timer(exec_output, tit)

            run.timer.Stop(tit)
            if k < len(list_comm):
                for b in glob('vola.*')+glob('loca.*'):
                    run.Delete(b)

            if len(list_comm) > 1:
                ldiag = getDiag(run, cas_test=ctest)
                diag_k  = ldiag[0]
                tcpu += ldiag[1]
                tsys += ldiag[2]
                ttot += ldiag[3]
                run.FileCat('fort.6', 'fort_bis.6')
                run.FileCat('fort.8', 'fort_bis.8')
                run.FileCat('fort.9', 'fort_bis.9')
                run.Delete('fort.6')
                run.Delete('fort.8')
                run.Delete('fort.9')
                if re.search('<[ESF]{1}>', diag_k):
                    # switch <F> to <E> if multiple .comm
                    if diag_k.find('<F>') > -1:
                        diag_k = diag_k.replace('<F>', '<E>')
                        # ...and try to restore previous bases
                        run.Mess(ufmt(_(u'restore bases from %s'), BASE_PREC))
                        lbas = glob(osp.join(BASE_PREC, 'glob.*')) + \
                         glob(osp.join(BASE_PREC, 'bhdf.*')) + \
                         glob(osp.join(BASE_PREC, 'pick.*'))
                        if len(lbas) > 0:
                            run.Copy(os.getcwdu(), niverr='INFO', verbose=follow_output, *lbas)
                        else:
                            run.Mess(_(u'no glob/bhdf base to restore'), '<A>_ALARM')
                    run.Mess(_(u'execution aborted (comm file #%d)') % k, diag_k)
                    diag = diag_k
                    break
                else:
                    # save bases in BASE_PREC if next execution fails
                    validbase = True
                    if k < len(list_comm):
                        if not ctest:
                            run.Mess(ufmt(_(u'save bases into %s'), BASE_PREC))
                        lbas = glob('glob.*') + \
                         glob('bhdf.*') + \
                         glob('pick.*')
                        run.Copy(BASE_PREC, niverr='INFO', verbose=follow_output, *lbas)
                    run.Mess(_(u'execution ended (comm file #%d)') % k, diag_k)
                # at least one is ok/alarm ? keep the "worse good" status!
                if run.GetGrav(diag_k) in (0, 1):
                    diag_ok = diag_ok or 'OK'
                    if run.GetGrav(diag_ok) < run.GetGrav(diag_k):
                        diag_ok = diag_k
                # the worst diagnostic
                if run.GetGrav(diag) < run.GetGrav(diag_k):
                    diag = diag_k

        # 6.5. global diagnostic
        if len(list_comm) > 1:
            run.Rename('fort_bis.6', 'fort.6')
            run.Rename('fort_bis.8', 'fort.8')
            run.Rename('fort_bis.9', 'fort.9')
        else:
            diag, tcpu, tsys, ttot, telap = getDiag(run, cas_test=ctest)
            validbase = run.GetGrav(diag) <= run.GetGrav('<S>')
        if ctest and run.GetGrav(diag) < 0:
            diag = '<F>_' + diag
        if ctest and diag == 'NO_TEST_RESU' and diag_ok:
            diag = diag_ok
            run.ReinitDiag(diag)
        # expected diagnostic ?
        if prof['expected_diag'][0]:
            expect = prof['expected_diag'][0]
            if run.GetGrav(diag) >= run.GetGrav('<E>'):
                diag = '<F>_ERROR'
            if run.GetGrav(diag) == run.GetGrav(expect):
                run.Mess(_(u'Diagnostic is as expected.'))
                diag = 'OK'
            else:
                run.Mess(_(u"Diagnostic is not as expected (got '%s').") % diag)
                diag = 'NOOK_TEST_RESU'
            run.ReinitDiag(diag)
        run.Mess(_(u'Code_Aster run ended, diagnostic : %s') % diag)

        # 6.6. post-mortem analysis of the core file
        if not with_dbg:
            cmd_dbg = run.config.get('cmd_post', '')
            lcor = glob('core*')
            if cmd_dbg and lcor:
                run.Mess(_(u'Code_Aster run created a coredump'),
                        '<E>_CORE_FILE')
                if not multiple:
                    # take the first one if several core files
                    core = lcor[0]
                    run.Mess(ufmt(_(u'core file name : %s'), core))
                    cmd = getdbgcmd(cmd_dbg, exetmp, core,
                            ('where', 'quit'), '')
                    tit = _(u'Coredump analysis')
                    run.Mess(tit, 'TITLE')
                    run.timer.Start(tit)
                    iret, output = run.Shell(' '.join(cmd),
                            alt_comment='coredump analysis...', verbose=True)
                    if iret == 0 and not ctest:
                        print3(output)
                    run.timer.Stop(tit)

        if not ctest:
            # 6.7. content of reptrav
            run.Mess(ufmt(_(u'Content of %s after execution'), os.getcwdu()), 'TITLE')
            kret, out = run.Shell(cmd='ls -la . REPE_OUT')
            print3(out)

            # 6.8. print some informations
            run.Mess(_(u'Size of bases'), 'TITLE')
            lf = glob('vola.*')
            lf.extend(glob('loca.*'))
            lf.extend(glob('glob.*'))
            lf.extend(glob('bhdf.*'))
            lf.extend(glob('pick.*'))
            for f in lf:
                run.Mess(_(u'size of %s : %12d bytes') % (f, os.stat(f).st_size))

    return diag, tcpu, tsys, ttot, validbase
Пример #4
0
def macr_recal(self, UNITE_ESCL, RESU_EXP, POIDS, LIST_PARA, RESU_CALC,
               ITER_MAXI, ITER_FONC_MAXI, RESI_GLOB_RELA, UNITE_RESU,
               PARA_DIFF_FINI, GRAPHIQUE, METHODE, INFO, **args):

    ASTER_ROOT = os.environ['ASTER_ROOT']

    try:
        sys.path.append(os.path.join(ASTER_ROOT, 'ASTK', 'ASTK_SERV', 'lib'))
        sys.path.append(
            os.path.join(
                ASTER_ROOT, 'lib',
                'python%s.%s' % (sys.version_info[0], sys.version_info[1]),
                'site-packages'))
    except:
        pass

    #_____________________________________________
    #
    # RECUPERATION DU PROFIL DU CALCUL MAITRE
    #_____________________________________________
    # Lecture du fichier .export dans le repertoire temporaire d'execution
    list_export = glob.glob('*.export')
    if len(list_export) == 0:
        UTMESS('F', 'RECAL0_4')
    elif len(list_export) > 1:
        UTMESS('F', 'RECAL0_5')
    prof = AsterProfil(list_export[0])

    #_____________________________________________
    #
    # PARAMETRES
    #_____________________________________________
    TOLE_PARA = args['TOLE_PARA']
    TOLE_FONC = args['TOLE_FONC']

    # Pour les calculs esclaves
    CALCUL_ESCLAVE = {}.fromkeys([
        'LANCEMENT',
        'MODE',
        'UNITE_SUIVI',
        'CLASSE',
        'ACTUALISATION',
        'memjeveux',
        'memjob',
        'mem_aster',
        'tpmax',
        'tpsjob',
        'mpi_nbnoeud',
        'mpi_nbcpu',
        'NMAX_SIMULT',
    ])

    dESCLAVE = args['CALCUL_ESCLAVE'][0].cree_dict_valeurs(
        args['CALCUL_ESCLAVE'][0].mc_liste)
    for i in list(dESCLAVE.keys()):
        if dESCLAVE[i] is None:
            del dESCLAVE[i]

    CALCUL_ESCLAVE['LANCEMENT'] = dESCLAVE['LANCEMENT']
    if 'UNITE_SUIVI' in dESCLAVE:
        CALCUL_ESCLAVE['UNITE_SUIVI'] = dESCLAVE['UNITE_SUIVI']
    else:
        CALCUL_ESCLAVE['UNITE_SUIVI'] = None
    if 'MODE' in dESCLAVE:
        CALCUL_ESCLAVE['MODE'] = dESCLAVE['MODE']
    else:
        CALCUL_ESCLAVE['MODE'] = prof['mode'][0].upper()

    LANCEMENT = CALCUL_ESCLAVE['LANCEMENT']

    # Parametres de l'algorithme genetique
    if 'NB_PARENTS' in args:
        NB_PARENTS = args['NB_PARENTS']
    if 'NB_FILS' in args:
        NB_FILS = args['NB_FILS']
    if 'ECART_TYPE' in args:
        ECART_TYPE = args['ECART_TYPE']
    if 'ITER_ALGO_GENE' in args:
        ITER_ALGO_GENE = args['ITER_ALGO_GENE']
    if 'RESI_ALGO_GENE' in args:
        RESI_ALGO_GENE = args['RESI_ALGO_GENE']

    if 'GRAINE' in args:
        UTMESS('A', 'RECAL0_43')
        GRAINE = args['GRAINE']
    else:
        GRAINE = None

    # Parametres concernant le recalage d'un modele dynamique
    if 'DYNAMIQUE' in args:
        DYNAMIQUE = args['DYNAMIQUE']
    else:
        DYNAMIQUE = None

    #_____________________________________________
    #
    # VERIFICATION PREALABLE SUR GNUPLOT
    #_____________________________________________

    if GRAPHIQUE:
        dGRAPHIQUE = GRAPHIQUE[0].cree_dict_valeurs(GRAPHIQUE[0].mc_liste)
        if 'FORMAT' in dGRAPHIQUE and dGRAPHIQUE['FORMAT'] == 'GNUPLOT':
            # On essaie d'importer Gnuplot -> PAS DE GRAPHIQUE
            if not HAS_GNUPLOT:
                GRAPHIQUE = None
                UTMESS('A', 'RECAL0_3')

    #_____________________________________________
    #
    # PARAMETRES DU MODE DISTRIBUTION
    #_____________________________________________
    if LANCEMENT == 'DISTRIBUTION':

        if debug:
            print(prof.param['tpsjob'][0])
            print(prof.args['tpmax'])
            print(prof.param['mem_aster'][0])
            print(prof.args['memjeveux'])
            print(prof.param['memjob'][0])

        # Pour la conversion mega-mots / mega-octets
        if on_64bits():
            facw = 8
        else:
            facw = 4

        # Recuperation du parametre mem_aster
        try:
            mem_aster = int(prof['mem_aster'][0])
        except ValueError:
            mem_aster = 100
        if mem_aster in (0, 100):
            if CALCUL_ESCLAVE['MODE'] == 'INTERACTIF':
                UTMESS('A', 'RECAL0_6')
            mem_aster = 100
        CALCUL_ESCLAVE['mem_aster'] = mem_aster

        # Utilisation du mot-cle TEMPS
        if 'TEMPS' in dESCLAVE:
            CALCUL_ESCLAVE['tpsjob'] = int(dESCLAVE['TEMPS'] / 60)
            CALCUL_ESCLAVE['tpmax'] = int(dESCLAVE['TEMPS'])
        else:
            # Recuperation depuis le calcul maitre
            CALCUL_ESCLAVE['tpsjob'] = prof.param['tpsjob'][0]
            CALCUL_ESCLAVE['tpmax'] = prof.args['tpmax']

        # Utilisation du mot-cle MEMOIRE
        if 'MEMOIRE' in dESCLAVE:
            CALCUL_ESCLAVE['memjob'] = int(dESCLAVE['MEMOIRE'] * 1024)
            # Calcul du parametre memjeveux esclave
            memjeveux = int(dESCLAVE['MEMOIRE'] / facw)
            try:
                if mem_aster == 100:
                    CALCUL_ESCLAVE['memjeveux'] = memjeveux
                else:
                    CALCUL_ESCLAVE['memjeveux'] = float(
                        int((float(mem_aster) / 100.) * float(memjeveux)))
            except:
                UTMESS('F', 'RECAL0_8')
        else:
            # Recuperation depuis le calcul maitre
            CALCUL_ESCLAVE['memjob'] = int(prof.param['memjob'][0])
            CALCUL_ESCLAVE['memjeveux'] = prof.args['memjeveux']

        # Utilisation du mot-cle MPI_NBCPU
        if 'MPI_NBCPU' in dESCLAVE:

            # Verifie que le calcul maitre est bien en MPI sur 1 cpu
            mpi_nbcpu = str(prof['mpi_nbcpu'][0])
            if mpi_nbcpu != '1':
                UTMESS('A', 'RECAL0_7')

            CALCUL_ESCLAVE['mpi_nbcpu'] = int(dESCLAVE['MPI_NBCPU'])

        # Utilisation du mot-cle MPI_NBNOEUD
        if 'MPI_NBNOEUD' in dESCLAVE:
            CALCUL_ESCLAVE['mpi_nbnoeud'] = int(dESCLAVE['MPI_NBNOEUD'])

        # Parametres batch
        if CALCUL_ESCLAVE['MODE'] == 'BATCH':
            if 'CLASSE' in dESCLAVE:
                CALCUL_ESCLAVE['CLASSE'] = dESCLAVE['CLASSE']
            if 'ACTUALISATION' in dESCLAVE:
                CALCUL_ESCLAVE['ACTUALISATION'] = dESCLAVE['ACTUALISATION']

            # Affichage parametres batch
            if CALCUL_ESCLAVE['CLASSE']:
                classe = CALCUL_ESCLAVE['CLASSE']
            else:
                classe = ' -auto- '
            UTMESS('I',
                   'RECAL0_69',
                   valk=(str(CALCUL_ESCLAVE['tpmax']),
                         str(int(CALCUL_ESCLAVE['memjob']) / 1024),
                         str(int(float(CALCUL_ESCLAVE['memjeveux']) * facw)),
                         classe))

    #_____________________________________________
    #
    # VERIFICATIONS
    #_____________________________________________

    if float(PARA_DIFF_FINI) > 0.1:
        UTMESS('A', 'RECAL0_76', valk=(str(PARA_DIFF_FINI)))

    #_____________________________________________
    #
    # INITIALISATIONS
    #_____________________________________________
    # Stocke l'ordre initial des parametres pour restituer dans le bon ordre
    # les valeurs en sortie de la macro
    LIST_NOM_PARA = [para[0] for para in LIST_PARA]

    # On classe les parametres
    LIST_PARA.sort()

    # Pour les algorithmes d'optimize.py, on a des limitations
    if METHODE in ['FMIN', 'FMINBFGS', 'FMINNCG']:
        # On ne peut tracer qu'a la derniere iteration
        if GRAPHIQUE:
            if GRAPHIQUE['AFFICHAGE'] == 'TOUTE_ITERATION':
                UTMESS('I', 'RECAL0_10', valk=METHODE)
        # Les bornes ne sont pas gerees
        UTMESS('I', 'RECAL0_11', valk=METHODE)

    #_______________________________________________
    #
    # GESTION DE L'OPTION FACULTATIVE POUR LES POIDS
    #_______________________________________________
    if (POIDS is None):
        POIDS = NP.ones(len(RESU_EXP))

    #_____________________________________________
    #
    # GESTION DES ERREURS DE SYNTAXE
    #_____________________________________________
    texte_erreur, texte_alarme = gestion(UNITE_ESCL, LIST_PARA, RESU_CALC,
                                         RESU_EXP, POIDS, GRAPHIQUE,
                                         UNITE_RESU, METHODE)
    if (texte_erreur != ""):
        UTMESS('F', "RECAL0_12", valk=texte_erreur)
    if (texte_alarme != ""):
        UTMESS('A', "RECAL0_12", valk=texte_alarme)

    #_____________________________________________
    #
    # INITIALISATIONS
    #_____________________________________________
    iter = 0
    restant, temps_iter = 0., 0.
    restant, temps_iter, err = reca_utilitaires.temps_CPU(restant, temps_iter)
    para, val, borne_inf, borne_sup = reca_utilitaires.transforme_list_Num(
        LIST_PARA, RESU_EXP)
    val_init = copy.copy(val)

    # Fonctionnelle en sortie (vectorielle ou scalaire)
    if METHODE in ['FMIN', 'FMINBFGS', 'FMINNCG', 'GENETIQUE', 'HYBRIDE']:
        vector_output = False
    else:
        vector_output = True

    # OBJET "CALCUL"
    CALCUL_ASTER = reca_calcul_aster.CALCUL_ASTER(
        jdc=self,
        METHODE=METHODE,
        UNITE_ESCL=UNITE_ESCL,
        UNITE_RESU=UNITE_RESU,
        para=para,
        reponses=RESU_CALC,
        PARA_DIFF_FINI=PARA_DIFF_FINI,
        vector_output=vector_output,
        DYNAMIQUE=DYNAMIQUE,
        # LANCEMENT       = LANCEMENT,
        CALCUL_ESCLAVE=CALCUL_ESCLAVE,
        INFO=INFO,
    )

    CALCUL_ASTER.RESU_EXP = RESU_EXP
    CALCUL_ASTER.RESU_CALC = RESU_CALC
    CALCUL_ASTER.LIST_PARA = LIST_PARA

    if CALCUL_ESCLAVE['UNITE_SUIVI']:
        CALCUL_ASTER.unity_follow = CALCUL_ESCLAVE['UNITE_SUIVI']

    # Instances des classes pour le calcul de l'erreur et le
    # dimensionnemnt/adim
    Dim = reca_algo.Dimension(copy.copy(val_init))
    CALCUL_ASTER.Simul = reca_interp.Sim_exp(RESU_EXP, POIDS)
    CALCUL_ASTER.Dim = Dim
    CALCUL_ASTER.reca_algo = reca_algo

    if (GRAPHIQUE):
        CALCUL_ASTER.UNITE_GRAPHIQUE = GRAPHIQUE['UNITE']

    # Dans le cas de la dynamique avec appariement manual des MAC, on passe la
    # flag correspondant a True
    if METHODE in [
            'HYBRIDE', 'LEVENBERG', 'GENETIQUE'
    ]:  # AAC --> j'ai modifie et donne la possibilite d'afficher la fenetre mac pour levenb et gene
        if (DYNAMIQUE is not None
                and DYNAMIQUE['APPARIEMENT_MANUEL'] == 'OUI'):
            CALCUL_ASTER.graph_mac = True

    # Instance de la classe gérant l'affichage des resultats du calcul de
    # l'optimisation
    Mess = reca_message.Message(para, RESU_EXP, copy.copy(val_init),
                                UNITE_RESU)
    Mess.initialise()

    # Calcul de F
    #    erreur = CALCUL_ASTER.calcul_F(val)
    # Calcul de F et G
    #    erreur, residu, A_nodim, A = CALCUL_ASTER.calcul_FG(val)
    #    sys.exit()

    # Mode INCLUDE : on doit executer les commandes PRE ici
    if LANCEMENT == 'INCLUSION':
        UNITE_INCLUDE = UNITE_ESCL
        recal.make_include_files(UNITE_INCLUDE=UNITE_INCLUDE,
                                 calcul=RESU_CALC,
                                 parametres=LIST_PARA)

    #-------------------------------------------------------------------------------
    # Pas d'optimisation (juste une evaluation de la fonctionnelle pour le point courant)
    #-------------------------------------------------------------------------------
    #
    if ITER_MAXI <= 0:
        erreur = CALCUL_ASTER.calcul_F(val)
        residu = 0
        iter = 0
        L_F = CALCUL_ASTER.Lcalc[0]
        CALCUL_ASTER.evaluation_fonction = 1

    #-------------------------------------------------------------------------------
    # Algorithme FMIN (pas d'adimensionnement car n'utilise pas de gradient)
    #-------------------------------------------------------------------------------
    #
    elif (METHODE == 'FMIN'):
        UTMESS('I', 'RECAL0_13', valk=METHODE, files=Mess.get_filename())
        val, fval, warnflag = fmin(CALCUL_ASTER.calcul_F,
                                   val,
                                   maxiter=ITER_MAXI,
                                   maxfun=ITER_FONC_MAXI,
                                   fulloutput=1)

        iter_fonc = CALCUL_ASTER.evaluation_fonction
        if warnflag == 1:
            UTMESS('I', 'RECAL0_54', files=Mess.get_filename())
        if warnflag == 2:
            UTMESS('I', 'RECAL0_55', files=Mess.get_filename())
        Mess.affiche_etat_final_convergence(iter,
                                            ITER_MAXI,
                                            iter_fonc,
                                            ITER_FONC_MAXI,
                                            RESI_GLOB_RELA,
                                            residu=0,
                                            Act=[])
        Mess.affiche_fonctionnelle(fval)
        Mess.affiche_valeurs(val)
        nomres = Sortie(LIST_NOM_PARA, LIST_PARA, val, CALCUL_ASTER, Mess)
        return nomres

    #-------------------------------------------------------------------------------
    # Algorithme GENETIQUE (pas d'adimensionnement car n'utilise pas de gradient)
    #-------------------------------------------------------------------------------
    #
    elif (METHODE == 'GENETIQUE'):
        UTMESS('I', 'RECAL0_13', valk=METHODE, files=Mess.get_filename())
        nb_parents = NB_PARENTS
        nb_fils = NB_FILS
        nb_iter = ITER_ALGO_GENE
        sigma = ECART_TYPE
        err_min = RESI_ALGO_GENE
        graine = GRAINE
        val = evolutivo(CALCUL_ASTER, val, nb_iter, err_min, nb_parents,
                        nb_fils, sigma, borne_inf, borne_sup, graine)
        nomres = Sortie(LIST_NOM_PARA, LIST_PARA, val, CALCUL_ASTER, Mess)
        return nomres

    #-------------------------------------------------------------------------------
    # Pour tous les autres methodes, on adimensionne
    #-------------------------------------------------------------------------------
    #
    else:

        #-------------------------------------------------------------------------------
        # Si METHODE=='HYBRIDE', on lance d'abord l'algo genetique et ensuite celui de
        # Levenberg-Marquardt qui demarre avec le jeu de parametres issu de
        # genetique
        if (METHODE == 'HYBRIDE'):
            nb_parents = NB_PARENTS
            nb_fils = NB_FILS
            nb_iter = ITER_ALGO_GENE
            sigma = ECART_TYPE
            err_min = RESI_ALGO_GENE
            graine = GRAINE
            val_gene = evolutivo(CALCUL_ASTER, val, nb_iter, err_min,
                                 nb_parents, nb_fils, sigma, borne_inf,
                                 borne_sup, graine)
            val = copy.copy(val_gene)
            val_init = copy.copy(val)
            # AA ? CALCUL_ASTER.graph_mac = True

        # Calcul de F et G
        erreur, residu, A_nodim, A = CALCUL_ASTER.calcul_FG(val)
        E = recal.CALC_ERROR(experience=RESU_EXP,
                             X0=val,
                             calcul=RESU_CALC,
                             poids=POIDS)
        E.CalcError(CALCUL_ASTER.Lcalc)
        E.CalcSensibilityMatrix(CALCUL_ASTER.Lcalc,
                                val,
                                dX=None,
                                pas=PARA_DIFF_FINI)

        L_init = E.L_init
        L_J_init = E.L_J_init
        J_init = E.J_init
        J = E.J
        A = E.A
        A_nodim = E.A_nodim
        erreur = E.erreur
        residu = E.residu
        gradient_init = E.gradient_init

        # Calcul du lambda_init
        l = reca_algo.lambda_init(NP.dot(NP.transpose(A), A))

        Mess.affiche_result_iter(iter, J, val, residu, NP.array([]))

        CALCUL_ASTER.L_init = L_init
        CALCUL_ASTER.L_J_init = L_J_init
        CALCUL_ASTER.J_init = J_init
        CALCUL_ASTER.A_init = A
        CALCUL_ASTER.gradient_init = gradient_init
        CALCUL_ASTER.residu_init = residu

        # On teste un manque de temps CPU
        restant, temps_iter, err = reca_utilitaires.temps_CPU(
            restant, temps_iter)
        if (err == 1):
            ier = ier + 1
            return ier

        #-------------------------------------------------------------------------------
        # Methode FMINBFGS et FMINNCG
        #-------------------------------------------------------------------------------
        #
        if METHODE in ['FMINBFGS', 'FMINNCG']:

            UTMESS('I', 'RECAL0_13', valk=METHODE, files=Mess.get_filename())

            # Derivees
            f = CALCUL_ASTER.calcul_F2
            fprime = CALCUL_ASTER.calcul_G
            warnflag = 0

            if 'GRADIENT' in args and args['GRADIENT'] == 'NON_CALCULE':
                f = CALCUL_ASTER.calcul_F
                fprime = None

            if fprime:
                UTMESS('I', 'RECAL0_14')
            else:
                UTMESS('I', 'RECAL0_15')

            # Lancement de l'optimisation
            if METHODE == 'FMINBFGS':
                val, fval, func_calls, grad_calls, warnflag = fminBFGS(
                    f=f,
                    x0=val,
                    fprime=fprime,
                    maxiter=ITER_MAXI,
                    avegtol=RESI_GLOB_RELA,
                    fulloutput=1)

            elif METHODE == 'FMINNCG':
                val, fval, func_calls, grad_calls, hcalls, warnflag = fminNCG(
                    f=f,
                    x0=val,
                    fprime=fprime,
                    fhess_p=None,
                    fhess=None,
                    maxiter=ITER_MAXI,
                    avextol=RESI_GLOB_RELA,
                    fulloutput=1)

            # Affichage des messages de sortie
            iter_fonc = CALCUL_ASTER.evaluation_fonction
            if warnflag:
                UTMESS('I', 'RECAL0_55', files=Mess.get_filename())
            Mess.affiche_etat_final_convergence(iter,
                                                ITER_MAXI,
                                                iter_fonc,
                                                ITER_FONC_MAXI,
                                                RESI_GLOB_RELA,
                                                residu=0,
                                                Act=[])
            Mess.affiche_fonctionnelle(fval)
            Mess.affiche_valeurs(val)

            # Permet d'avoir un diagnostic NOOK pour le job
            if warnflag:
                iter = ITER_MAXI

            L_F = CALCUL_ASTER.L
            residu = fval
            ecart_fonc = 0  # non calcule avec ces methodes
            ecart_para = 0  # non calcule avec ces methodes

        #-------------------------------------------------------------------------------
        # Methode Levenberg-Marquardt
        #----------------------------------------------------------------------
        elif METHODE in ['LEVENBERG', 'HYBRIDE']:

            #___________________________________________________________
            #
            # BOUCLE PRINCIPALE DE L'ALGORITHME de Levenberg-Marquardt
            #___________________________________________________________

            UTMESS('I', 'RECAL0_13', valk=METHODE, files=Mess.get_filename())
            while (iter < ITER_MAXI):
                iter = iter + 1
                new_val, s, l, Act = reca_algo.Levenberg_bornes(
                    val, Dim, val_init, borne_inf, borne_sup, A, erreur, l,
                    UNITE_RESU)

                # On teste la variation sur les parametres
                ecart_para = reca_algo.calcul_norme2(
                    NP.array(new_val) - NP.array(val))
                if debug:
                    print("AA0/ecart para=%s\nAA0/oldpara/newpara=%s %s" %
                          (ecart_para, val, new_val))
                if ecart_para < TOLE_PARA:
                    UTMESS('I',
                           'RECAL0_51',
                           valr=ecart_para,
                           files=Mess.get_filename())
                    break

                # Calculs au point courant val et toutes les perturbations par
                # differences finies (N+1 calculs distribues ou inclus)
                CALCUL_ASTER.calcul_FG(new_val)

                # Calcul de l'erreur et de la matrice des sensibilites
                old_J = copy.copy(J)
                E.CalcError(CALCUL_ASTER.Lcalc)
                new_J = E.J

                l = reca_algo.actualise_lambda(l, Dim.adim(val),
                                               Dim.adim(new_val), A, erreur,
                                               new_J, J)
                E.CalcSensibilityMatrix(CALCUL_ASTER.Lcalc,
                                        new_val,
                                        dX=None,
                                        pas=PARA_DIFF_FINI)

                L_F = CALCUL_ASTER.Lcalc[0]
                A = E.A_nodim
                val = copy.copy(new_val)
                erreur = copy.copy(E.erreur)
                J = E.J

                if debug:
                    print("AA0/L_F=", L_F)
                    print("AA0/l=", l)
                    print("AA0/erreur=", erreur)
                    print("AA0/J=", J)
                    print("AA0/A_nodim=", A)

                # Calcul de la matrice des sensibilites
                A = Dim.adim_sensi(A)

                # Calcul du residu
                residu = reca_algo.test_convergence(gradient_init, erreur, A,
                                                    s)

                if debug:
                    print("AA0/residu=", residu)
                    print("AA0/new_val=", new_val)
                    print("AA0/A=", A)

                # On calcule la variation sur la fonctionnelle
                ecart_fonc = abs(new_J - old_J)

                # Affichage iteration
                Mess.affiche_result_iter(iter, J, val, residu, Act, ecart_para,
                                         ecart_fonc)

                # On teste la variation sur la fonctionnelle
                if ecart_fonc < TOLE_FONC:
                    UTMESS('I',
                           'RECAL0_52',
                           valr=ecart_fonc,
                           files=Mess.get_filename())
                    break

                if (GRAPHIQUE):
                    if GRAPHIQUE['AFFICHAGE'] == 'TOUTE_ITERATION':
                        GRAPHE_UL_OUT = GRAPHIQUE['UNITE']
                        if 'FORMAT' in dGRAPHIQUE and dGRAPHIQUE[
                                'FORMAT'] == 'XMGRACE':
                            pilote = GRAPHIQUE['PILOTE']
                        else:
                            pilote = 'INTERACTIF'
                        reca_utilitaires.graphique(GRAPHIQUE['FORMAT'], L_F,
                                                   RESU_EXP, RESU_CALC, iter,
                                                   GRAPHE_UL_OUT, pilote)

                # On teste le residu
                if residu <= RESI_GLOB_RELA:
                    UTMESS('I',
                           'RECAL0_50',
                           valr=residu,
                           files=Mess.get_filename())
                    break

                # On teste un manque de temps CPU
                restant, temps_iter, err = reca_utilitaires.temps_CPU(
                    restant, temps_iter)
                if (err == 1):
                    UTMESS('I', 'RECAL0_53', files=Mess.get_filename())
                    break

            #_____________________________________________
            #
            # FIN DES ITERATIONS
            # CONVERGENCE OU ECHEC
            #_____________________________________________
            iter_fonc = CALCUL_ASTER.evaluation_fonction
            Mess.affiche_etat_final_convergence(iter, ITER_MAXI, iter_fonc,
                                                ITER_FONC_MAXI, RESI_GLOB_RELA,
                                                residu, Act)
            reca_algo.calcul_etat_final(para, A, iter, ITER_MAXI,
                                        RESI_GLOB_RELA, residu, Mess)

        #----------------------------------------------------------------------
    #_____________________________________________
    #
    # FIN DES ITERATIONS POUR TOUS LES ALGOS
    #_____________________________________________
    if (GRAPHIQUE):
        fichier = None
        # Pour les algorithmes d'optimize.py, on ne peut tracer qu'a la
        # derniere iteration
        if (GRAPHIQUE['AFFICHAGE'] == 'ITERATION_FINALE') or (METHODE in [
                'FMIN', 'FMINBFGS', 'FMINNCG'
        ]) or (ITER_MAXI <= 0):
            UTMESS('I', 'RECAL0_17')
            GRAPHE_UL_OUT = GRAPHIQUE['UNITE']
            pilote = GRAPHIQUE['PILOTE']
            reca_utilitaires.graphique(GRAPHIQUE['FORMAT'], L_F, RESU_EXP,
                                       RESU_CALC, iter, GRAPHE_UL_OUT, pilote,
                                       fichier)

    # Si pas de convergence alors diagnostic NOOK_TEST_RESU
# if (residu > RESI_GLOB_RELA) and  (ecart_fonc > TOLE_FONC) and
# (ecart_para < TOLE_PARA):

    if debug:
        print("residu, RESI_GLOB_RELA=", residu, RESI_GLOB_RELA,
              (residu > RESI_GLOB_RELA))
        print("ecart_fonc, TOLE_FONC=", ecart_fonc, TOLE_FONC,
              (ecart_fonc > TOLE_FONC))
        print("ecart_para, TOLE_PARA=", ecart_para, TOLE_PARA,
              (ecart_para > TOLE_PARA))

    if (residu > RESI_GLOB_RELA):
        _tmp = []
        _tmp.append({
            'PARA': 'ITER_MAXI',
            'LISTE_R': 0.0,
        })

    #_____________________________________________
    #
    # CREATIONS DE LA LISTE DE REELS CONTENANT
    # LES VALEURS DES PARAMETRES A CONVERGENCE
    #_____________________________________________
    nomres = Sortie(LIST_NOM_PARA, LIST_PARA, val, CALCUL_ASTER, Mess)
    return nomres
Пример #5
0
    #_____________________________________________
    #
    # PARAMETRES DU MODE DISTRIBUTION
    #_____________________________________________
    if LANCEMENT == 'DISTRIBUTION':

        if debug:
            print prof.param['tpsjob'][0]
            print prof.args['tpmax']
            print prof.param['mem_aster'][0]
            print prof.args['memjeveux']
            print prof.param['memjob'][0]

        # Pour la conversion mega-mots / mega-octets
        from asrun.common.sysutils import on_64bits
        if on_64bits():
            facw = 8
        else:
            facw = 4

        # Recuperation du parametre mem_aster
        try:
            mem_aster = int(prof['mem_aster'][0])
        except ValueError:
            mem_aster = 100
        if mem_aster in (0, 100):
            if CALCUL_ESCLAVE['MODE'] == 'INTERACTIF':
                UTMESS('A', 'RECAL0_6')
            mem_aster = 100
        CALCUL_ESCLAVE['mem_aster'] = mem_aster