This file is indexed.

/usr/share/pyshared/asrun/parametric.py is in code-aster-run 1.13.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-

# ==============================================================================
# COPYRIGHT (C) 1991 - 2003  EDF R&D                  WWW.CODE-ASTER.ORG
# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
# (AT YOUR OPTION) ANY LATER VERSION.
#
# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
# GENERAL PUBLIC LICENSE FOR MORE DETAILS.
#
# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
# ==============================================================================

"""
Functions for parametric executions
"""

import os
import os.path as osp
from math import log10

from asrun.common.i18n  import _
from asrun.mystring     import print3, ufmt
from asrun.thread       import Dispatcher
from asrun.distrib      import DistribParametricTask
from asrun.common_func  import get_tmpname
from asrun.repart       import get_hostrc
from asrun.common.utils import now



fmt_head = '%s %s %s %s %s %s' % (_(u"job").center(12), _(u"result").center(18),
        _(u"cpu").rjust(8), _(u"sys").rjust(8), _(u"cpu+sys").rjust(8), _(u"elapsed").rjust(8) )
fmt_resu = '%-12s %-18s %8.2f %8.2f %8.2f %8.2f'
fmt_res2 = '%%4d %s %%4d %s    %%8.2f %%8.2f %%8.2f %%8.2f' \
    % (_("jobs").ljust(7), _("errors").ljust(10))
fmt_tot = '-'*12+' '+'-'*18+' '+'-'*8+' '+'-'*8+' '+'-'*8+' '+'-'*8


def Parametric(run, prof, mpi_info, numthread='auto', **kargs):
    """Run a parametric study.
    """
    run.print_timer = True

    # 1. ----- initializations
    jn = run['num_job']

    # 1.2. rep_trav from profile or from run[...]
    reptrav = mpi_info.reptrav()

    run.Mess(_(u'Code_Aster parametric execution'), 'TITLE')
    mpi_info.set_cpuinfo(1, 1)

    # ----- how many threads ?
    try:
        numthread = int(numthread)
    except (TypeError, ValueError):
        numthread = run.GetCpuInfo('numthread')

    # 1.3. content of the profile
    if not prof.Get('D', typ='distr'):
        run.Mess(_(u'"distr" file is necessary'), '<F>_FILE_NOT_FOUND')
    else:
        fdist = prof.Get('D', typ='distr')[0]['path']
        if run.IsRemote(fdist):
            tmpdist = get_tmpname(run, run['tmp_user'], basename='distr')
            run.ToDelete(tmpdist)
            kret = run.Copy(tmpdist, fdist)
            fdist = tmpdist
        else:
            fdist = run.PathOnly(fdist)
    dist_cnt = open(fdist, 'r').read()
    try:
        keywords = get_distribution_data(dist_cnt)
    except Exception, msg:
        run.Mess(str(msg), '<F>_ERROR')
    list_val = keywords['VALE']
    nbval = len(list_val)
    # it may be very big
    del keywords['VALE']

    if not prof.Get('R', typ='repe'):
        run.Mess(_(u'no result directory found (type "repe")'), '<F>_NO_RESU_DIR')
    else:
        resudir = prof.Get('R', typ='repe')[0]['path']
        if run.IsRemote(resudir):
            run.Mess(_(u'the result directory must not be on a remote host'), '<F>_ERROR')
        resudir = run.PathOnly(resudir)
        run.MkDir(resudir)
        run.Delete(osp.join(resudir, 'RESULTAT'))
        run.Delete(osp.join(resudir, 'NOOK'))
        flashdir = osp.join(resudir, 'flash')
        prfl = prof.Get('R', typ='flash')
        if prfl:
            if prfl[0]['path'] == "None":
                flashdir = None
            else:
                flashdir = prfl[0]['path']

    # check
    type_base, compress = prof.get_base('R')
    if type_base:
        path = prof.Get('R', typ=type_base)[0]['path']
        if path != resudir:
            run.Mess(ufmt(_(u"'repe' and '%s' must be identical. '%s' is set to %s"),
                          type_base, type_base, resudir), "<A>_ALARM")

    # get hostrc object
    tit = _(u"Checking hosts")
    hostrc = get_hostrc(run, prof)
    run.timer.Start(tit)
    n_avail, n_tot = hostrc.CheckHosts(run, numthread=numthread)
    run.timer.Stop(tit)
    run.Mess(_(u'Number of available hosts : %d/%d') % (n_avail, n_tot), "SILENT")
    if n_avail < 1:
        run.Mess(_(u"No available host. Run cancelled."), "<F>_INVALID_PARAMETER")

    # timeout before rejected a job = tpsjob
    try:
        timeout = prof.get_timeout() * 2.
    except Exception, reason:
        run.Mess(_(u"incorrect value for tpsjob : %s") % reason, '<F>_INVALID_PARAMETER')

    # print a summary
    summary = _(u"""
--- %d calculations to run
--- Run started at %s
--- Parameters used for this run :
    Directory to copy results       : %s
    Directory for job files         : %s
    Code_Aster version              : %s
    Executable filename             : %s
    Commands catalogue directory    : %s
    Elements catalogue filename     : %s
    Working directory               : %s
    Submission timeout (seconds)    : %.0f""")

    sum_thread = _(u"""    Number of threads               : %d""")
    sum_rc     = _(u"""    Available hosts (number of cpu) : %s""")
    sum_end = _(u"""
--- All executions finished at %s

--- Results :
""")
    fmt_spup = _(u"""
--- Speed-up is %.2f
""")
    info_start = ( nbval, now(), resudir, flashdir,
        prof.get_version_path(), kargs['exe'], kargs['cmde'], kargs['ele'],
        reptrav, timeout )
    txt_summary = [ ufmt(summary, *info_start), ]
    if numthread > 1:
        txt_summary.append( sum_thread % numthread )
    if hostrc:
        host_str = ', '.join(['%(host)s (%(cpu)d)' % hostrc.GetConfig(h) \
                            for h in hostrc.get_all_connected_hosts()])
        txt_summary.append( sum_rc % host_str )
    txt_summary.append('')
    text_summary = os.linesep.join(txt_summary)
    print3(text_summary)
    if nbval < 1:
        run.Mess(_(u"There is no value for the parameters."), '<F>_ERROR')

    # change directory before running sub-execution (reptrav will be deleted !)
    os.chdir(run['rep_trav'])

    # ----- Execute calculations in parallel using a Dispatcher object
    # elementary task...
    task = DistribParametricTask( # IN
                          run=run, prof=prof,
                          hostrc=hostrc,
                          keywords=keywords,
                          nbmaxitem=0, timeout=timeout,
                          resudir=resudir, flashdir=flashdir,
                          reptrav=reptrav,
                          info=1,
                          # OUT
                          nbnook=[0,]*numthread, exec_result=[])
    # ... and dispatch task on 'list_tests'
    tit = _(u'Parametric execution')
    run.timer.Start(tit)
    etiq = 'calc_%%0%dd' % (int(log10(nbval)) + 1)
    labels = [etiq % (i+1) for i in range(nbval)]
    couples = zip(labels, list_val)
    execution = Dispatcher(couples, task, numthread=numthread)
    cpu_dt, sys_dt, tot_dt = run.timer.StopAndGet(tit)

    # Summary
    run.Mess(_(u'Summary of the run'), 'TITLE')
    print3(text_summary)
    print3(sum_end % now())
    t = [0., 0., 0., 0.]
    print3(fmt_head)
    print3(fmt_tot)
    task.exec_result.sort()
    for result in task.exec_result:
        lin = result[:-1]
        del lin[1] # job, opts, diag, tcpu, tsys, ttot, telap, output
        t[0] += lin[2]
        t[1] += lin[3]
        t[2] += lin[4]
        t[3] += lin[5]
        print3(fmt_resu % tuple(lin))
    print3(fmt_tot)
    print3(fmt_res2 % (len(task.exec_result), sum(task.nbnook), t[0], t[1], t[2], t[3]))

    # check the number of calculations really run
    if len(task.exec_result) != len(list_val):
        run.Mess(_(u'%d studies to run, %d really run !') \
            % (len(list_val), len(task.exec_result)), '<E>_ERROR')

    # force global diagnostic to <F>_ERROR if errors occured
    if sum(task.nbnook) > 0:
        run.diag = '<E>_ERROR'

    spup = 0.
    if tot_dt != 0.:
        spup = t[3]/tot_dt
    print3(fmt_spup % spup)
    if hostrc is not None:
        print3(os.linesep + hostrc.repr_history())

    run.CheckOK()
    run.Mess(_(u'All calculations run successfully'), 'OK')


def get_distribution_data(txt):
    """Return a dict of the distribution datas :
        (required)  VALE : list of dicts of the parameters values
        (optional) PRE_CALCUL (resp. UNITE_PRE_CALCUL) : list of commands to insert
            just after the DEBUT/POURSUITE command (respectively an integer defining
            the logical unit provided to INCLUDE)
        (optional) POST_CALCUL (resp. UNITE_POST_CALCUL) : list of commands to
            insert just before the FIN command (respectively an integer defining the
            logical unit provided to INCLUDE)
    """
    from asrun.N__F import _F
    dico = {}
    d = locals().copy()
    key = set(d.keys())
    exec txt in d
    if d.get('VALE') != None:
        dico['VALE'] = d['VALE']
    else:
        # sinon unique objet présent
        del d['__builtins__']
        key.symmetric_difference_update(d.keys())
        if len(key) != 1:
            dico['VALE'] = []
        else:
            dico['VALE'] = d[key.pop()]
    if not is_list_of_dict(dico['VALE']):
        raise TypeError, _(u'a list of dicts is required, not %s') % dico['VALE']
    for key in ('PRE_CALCUL', 'UNITE_PRE_CALCUL', 'POST_CALCUL', 'UNITE_POST_CALCUL'):
        dico[key] = d.get(key)
    return dico


def is_list_of_dict(obj):
    """Return True if obj is a list (iterable) of dictionnaries (with items method).
    """
    try:
        for sobj in obj:
            for k, v in sobj.items():
                pass
        res = True
    except:
        res = False
    return res