PyForTool
Python-fortran-tool
Loading...
Searching...
No Matches
scripting.py
1"""
2This module contains functions usefull to build scripts around the pyfortool library
3"""
4
5import sys
6from multiprocessing import cpu_count, Pool
7from multiprocessing.managers import BaseManager
8import re
9import shlex
10import os
11import argparse
12import logging
13import traceback
14
15from pyfortool.pyfortool import PYFT
16from pyfortool.tree import Tree
17from pyfortool.util import isint, PYFTError
18from pyfortool import __version__
19
20
21def task(filename):
22 """
23 Function to use on each file
24 :param clsPYFT: PYFT class to use
25 :param filename: file name
26 """
27 global PYFT # noqa: F824
28 global allFileArgs # noqa: F824
29 allArgs, orderedOptions = allFileArgs[filename]
30 try:
31 # Opening and reading of the FORTRAN file
32 with PYFT(filename, filename,
33 parserOptions=getParserOptions(allArgs), verbosity=allArgs.logLevel,
34 wrapH=allArgs.wrapH,
35 enableCache=allArgs.enableCache) as pft:
36
37 # apply the transformation in the order they were specified
38 for arg in orderedOptions:
39 logging.debug('Applying %s on %s', arg, filename)
40 applyTransfo(pft, arg, allArgs,
41 filename if filename == allArgs.plotCentralFile else None)
42 logging.debug(' -> Done')
43
44 # Writing
45 if not allArgs.dryRun:
46 pft.write()
47
48 # Reporting
49 return (0, filename)
50
51 except Exception as exc: # pylint: disable=broad-except
52 logging.error("The following error has occurred in the file %s", filename)
53 traceback.print_exception(exc, file=sys.stdout)
54 sys.stdout.flush()
55 return (1, filename)
56
57
58def mainParallel(argv=None):
59 """
60 Core of the pyfortool_parallel.py command
61 :param argv: list of arguments (including the 'programm' name in first position)
62 as would be obtained by sys.argv. Or None to use sys.argv
63 """
64
65 class MyManager(BaseManager):
66 """
67 Custom manager to deal with Tree instances
68 """
69
70 MyManager.register('Tree', Tree)
71
72 def init(cls, afa):
73 """
74 Pool initializer
75 """
76 # After many, many attempts, it seems very difficult (if not impossible)
77 # to do without global variables
78 global PYFT # pylint: disable=global-statement
79 global allFileArgs # pylint: disable=global-statement
80 PYFT = cls
81 allFileArgs = afa
82
83 parser = argparse.ArgumentParser(description='Python FORTRAN tool', allow_abbrev=False,
84 epilog="The argument order matters.")
85
86 updateParser(parser, withInput=False, withOutput=False, withXml=False, withPlotCentralFile=True,
87 treeIsOptional=False, nbPar=True, restrictScope=False)
88 commonArgs, getFileArgs = getArgs(parser, argv)
89
90 # Manager to share the Tree instance
91 with MyManager() as manager:
92 # Set-up the Tree instance
93 sharedTree = getDescTree(commonArgs, manager.Tree)
94
95 # Prepare PYFT to be used in parallel
96 PYFT.setParallel(sharedTree)
97
98 # Set-up the processes
99 allFileArgs = {file: getFileArgs(file) for file in sharedTree.getFiles()}
100 logging.info('Executing in parallel on %i files with a maximum of %i processes',
101 len(allFileArgs), commonArgs.nbPar)
102 with Pool(commonArgs.nbPar, initializer=init, initargs=(PYFT, allFileArgs)) as pool:
103 result = pool.map(task, sharedTree.getFiles())
104
105 # Writting the descTree object
106 sharedTree.toJson(commonArgs.descTree)
107
108 # General error
109 errors = [item[1] for item in result if item[0] != 0]
110 status = len(errors)
111 if status != 0:
112 logging.error('List of files with error:')
113 for error in errors:
114 logging.error(' - %s', error)
115 raise PYFTError(f"Errors have been reported in {status} file(s).")
116
117 # Unset parallel processing
118 PYFT.unsetParallel()
119
120
121def main(argv=None):
122 """
123 Core of the pyfortool.py command
124 :param argv: list of arguments (including the 'programm' name in first position)
125 as would be obtained by sys.argv. Or None to use sys.argv
126 """
127 parser = argparse.ArgumentParser(description='Python FORTRAN tool', allow_abbrev=False,
128 epilog="The argument order matters.")
129
130 updateParser(parser, withInput=True, withOutput=True, withXml=True, withPlotCentralFile=False,
131 treeIsOptional=True, nbPar=False, restrictScope=True)
132 args, orderedOptions = getArgs(parser, argv)[1]()
133
134 parserOptions = getParserOptions(args)
135 descTree = getDescTree(args)
136
137 try:
138 # Opening and reading of the FORTRAN file
139 pft = PYFT(args.INPUT, args.OUTPUT, parserOptions=parserOptions,
140 verbosity=args.logLevel, wrapH=args.wrapH, tree=descTree,
141 enableCache=args.enableCache)
142 if args.restrictScope != '':
143 pft = pft.getScopeNode(args.restrictScope)
144
145 # apply the transformation in the order they were specified
146 for arg in orderedOptions:
147 logging.debug('Applying %s on %s', arg, args.INPUT)
148 applyTransfo(pft, arg, args, plotCentralFile=args.INPUT)
149 logging.debug(' -> Done')
150
151 # Writing
152 if descTree is not None:
153 descTree.toJson(args.descTree)
154 if args.xml is not None:
155 pft.mainScope.writeXML(args.xml)
156 if not args.dryRun:
157 pft.mainScope.write()
158
159 # Closing
160 pft.mainScope.close()
161
162 except: # noqa E722
163 # 'exept' everything and re-raise error systematically
164 logging.error("The following error has occurred in the file %s", args.INPUT)
165 raise
166
167
168ARG_UPDATE_CNT = ('--alignContinuation', '--addBeginContinuation',
169 '--removeBeginContinuation',
170 '--emoveALLContinuation')
171
172
173def getArgs(parser, argv):
174 """
175 Parse arguments and interpret the --optsByEnv option
176 :param parser: argparse parser
177 :param argv: list of arguments (including the 'programm' name in first position)
178 as would be obtained by sys.argv. Or None to use sys.argv.
179 :return: a tuple with
180 - an argparse namespace containing common arguments (not using the --optsEnv option)
181 - a function taking a filename as input and returning
182 - an argparse namespace with the common arguments and the ones added by
183 interpreting the --optsEnv option
184 - an ordered list of arguments
185 """
186 if argv is None:
187 argv = sys.argv
188 args = parser.parse_args(argv[1:])
189
190 def getFileArgs(filename=args.INPUT if hasattr(args, 'INPUT') else None):
191 """
192 :param filename: name of source code file
193 :return: argparse namespace to use with this file and
194 a list given the order in which the arguments were provided
195 """
196 # Decode the --optsByEnv option
197 arguments = argv[1:]
198 if args.optsByEnv is not None:
199 extra = ''
200 for line in [] if args.optsByEnv is None else os.environ[args.optsByEnv].split('\n'):
201 if ':=:' in line:
202 if re.match(line.split(':=:')[0], filename):
203 extra = line.split(':=:')[1]
204 else:
205 extra = line
206 index = arguments.index('--optsByEnv')
207 arguments = arguments[:index] + shlex.split(extra) + arguments[index + 2:] # keep order
208
209 # Compute the ordered list
210 updateCnt = False
211 optList = []
212 for arg in arguments:
213 if arg.startswith('--') and arg not in optList:
214 if arg in ARG_UPDATE_CNT:
215 if not updateCnt:
216 updateCnt = True
217 optList.append(arg)
218 else:
219 optList.append(arg)
220
221 return parser.parse_args(arguments), optList
222
223 return args, getFileArgs
224
225
227 """
228 Get the options to use for the fxtran parser
229 :param args: arguments parsed by the argparse parser
230 """
231 if args.parserOption is None:
232 parserOptions = PYFT.DEFAULT_FXTRAN_OPTIONS.copy()
233 else:
234 parserOptions = [el for elements in args.parserOption for el in elements]
235 if args.addIncludes:
236 parserOptions = [opt for opt in parserOptions if opt not in ('-no-include', '-noinclude')]
237 return parserOptions
238
239
240def getDescTree(args, cls=Tree):
241 """
242 get the Tree object built with the parsed arguments
243 :param args: arguments parsed by the argparse parser
244 :param cls: class to use (usefull for manager)
245 :return: a Tree instance
246 """
247 parserOptions = getParserOptions(args)
248 if args.descTree:
249 descTree = cls(tree=args.tree, descTreeFile=args.descTree,
250 parserOptions=parserOptions,
251 wrapH=args.wrapH, verbosity=args.logLevel)
252 else:
253 descTree = None
254 return descTree
255
256
257def updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, treeIsOptional,
258 nbPar, restrictScope):
259 """
260 Updates an argparse parser with arguments common to all the different tools
261 :param parser: parser in which arguments are added
262 :param withOutput: do we need the INPUT argument
263 :param withOutput: do we need the OUTPUT argument
264 :param withXml: do we need to be able to define an XML output file
265 :param withPlotCentralFile: to add the --plotCentralFile argument
266 :param treeIsOptional: is the --tree argument optional?
267 :param nbPar: number of parallel processes
268 :param restrictScope: can we specify the scope path
269 """
270
271 # ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
272 # IMPORTANT NOTE
273 # Argument order matters but argparse is not able to give the order
274 # Therefore, arguments are processed twice. The first time by argparse to fully decode them.
275 # Then a second pass is made direcly on sys.argv. This mechanism has two implications:
276 # allow_abbrev must be set to False in ArgumentParser
277 # only long argument options are allowed (begining with two dashes)
278 # ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
279 assert not parser.allow_abbrev, 'parser must be created with allow_abbrev=False'
280
281 parser.add_argument('--version', action='version',
282 version='%(prog)s {version}'.format(version=__version__))
283 parser.add_argument('--simplify', default=False, action='store_true',
284 help='After a deletion, recursively deletes the code ' +
285 'and variables that have become useless')
286 parser.add_argument('--logLevel', default='warning',
287 help='Provide logging level. Example --logLevel debug (default is warning)')
288 parser.add_argument('--enableCache', default=False, action='store_true',
289 help='Precompute parent of each xml node and store the result')
290 if nbPar:
291 parser.add_argument('--nbPar', default=cpu_count(), type=int,
292 help='Number of parallel processes, 0 to get as many processes ' +
293 'as the number of cores (default=0)')
294 parser.add_argument('--optsByEnv', default=None, type=str,
295 help='Name of the environment variable containing additional arguments ' +
296 'to use. These arguments are processed after all other arguments. ' +
297 'The variable can contain a multi-lines string. The ' +
298 'variable is read line by line and the last applicable line is ' +
299 'used. A line can take one of these two forms: ' +
300 '1) "FILE_DESCRIPTOR:=:OPTIONS" (where FILE_DESCRIPTOR is a ' +
301 'regular expression to test against the filename. If there ' +
302 'is a match, the OPTIONS can be used for the file) and ' +
303 '2) "OPTIONS" (if the line doesn\'t contain the FILE_DESCRIPTOR ' +
304 'part, it applies to all source code).')
305
306 if restrictScope:
307 parser.add_argument('--restrictScope', default='', type=str, metavar='SCOPEPATH',
308 help="Limit the action to this scope path (SUBROUTINE/FUNCTION/" +
309 "MODULE/TYPE). It is '/'-separated path with each element " +
310 "having the form 'module:<name of the module>', " +
311 "'sub:<name of the subroutine>', " +
312 "'func:<name of the function>' or 'type:<name of the type>'.")
313
314 # Inputs and outputs
315 updateParserInputsOutputs(parser, withInput, withOutput, withXml)
316
317 # fxtran
318 updateParserFxtran(parser)
319
320 # Variables
322
323 # Cosmetics
325
326 # Applications
328
329 # openACC
330 updateParserOpenACC(parser)
331
332 # Checks
333 updateParserChecks(parser)
334
335 # Statements
337
338 # Misc
339 updateParserMisc(parser)
340
341 # Tree
342 updateParserTree(parser, withPlotCentralFile, treeIsOptional)
343
344 # Preprocessot
346
347
348def updateParserInputsOutputs(parser, withInput, withOutput, withXml):
349 """
350 Updates an argparse parser with input/output arguments
351 :param parser: parser in which arguments are added
352 :param withOutput: do we need the INPUT argument
353 :param withOutput: do we need the OUTPUT argument
354 :param withXml: do we need to be able to define an XML output file
355 """
356 gInOut = parser.add_argument_group('Input and output')
357 if withInput:
358 gInOut.add_argument('INPUT', help='FORTRAN input file')
359 if withOutput:
360 gInOut.add_argument('OUTPUT', default=None, help='FORTRAN output file', nargs='?')
361 gInOut.add_argument('--renamefF', default=False, action='store_true',
362 help='Put file extension in upper case')
363 gInOut.add_argument('--renameFf', default=False, action='store_true',
364 help='Put file extension in lower case')
365 if withXml:
366 gInOut.add_argument('--xml', default=None, type=str,
367 help='Output file for xml')
368 gInOut.add_argument('--dryRun', default=False, action='store_true',
369 help='Dry run without writing the FORTRAN file (the xml ' +
370 'is still written')
371
372
374 """
375 Updates an argparse parser with fxtran arguments
376 """
377 gParser = parser.add_argument_group('fxtran parser relative options')
378 gParser.add_argument('--parserOption', nargs='*', action='append',
379 help='Option to pass to fxtran, defaults ' +
380 f'to {PYFT.DEFAULT_FXTRAN_OPTIONS}')
381 gParser.add_argument('--wrapH', default=False, action='store_true',
382 help='Wrap .h file content into a MODULE to enable the reading')
383
384
386 """
387 Updates an argparse parser with variables arguments
388 """
389 gVariables = parser.add_argument_group('Options to deal with variables')
390 gVariables.add_argument('--showVariables', default=False, action='store_true',
391 help='Show the declared variables')
392 gVariables.add_argument('--removeVariable', nargs=2, action='append',
393 metavar=('SCOPEPATH', 'VARNAME'),
394 help="Variable to remove from declaration. The first argument " +
395 "is the SUBROUTINE/FUNCTION/MODULE/TYPE where the variable " +
396 "is declared. It is '/'-separated path with each element having " +
397 "the form 'module:<name of the module>', " +
398 "'sub:<name of the subroutine>', " +
399 "'func:<name of the function>' or 'type:<name of the type>'. " +
400 "The second argument is the variable name")
401 gVariables.add_argument('--attachArraySpecToEntity', default=False, action='store_true',
402 help='Find all T-decl-stmt elements that have a child element ' +
403 'attribute with attribute-N=DIMENSION and move the attribute ' +
404 'into EN-N elements')
405 gVariables.add_argument('--addVariable', nargs=4, action='append',
406 metavar=('SCOPEPATH', 'VARNAME', 'DECLARATION', 'POSITION'),
407 help='Add a variable. First argument is the scope path (as for ' +
408 'the --removeVariable option. The second is the variable ' +
409 'name, the third is the declarative statement to insert, ' +
410 'the fourth is the position (python indexing) the new ' +
411 'variable will have in the calling statment of the ' +
412 'routine (non-integer value for a local variable).')
413 gVariables.add_argument('--addModuleVariable', nargs=3, action='append',
414 metavar=('SCOPEPATH', 'MODULENAME', 'VARNAME'),
415 help='Add a USE statement. The first argument is the scope path ' +
416 '(as for the --removeVariable option). The second is the module ' +
417 'name; the third is the variable name.')
418 gVariables.add_argument('--showUnusedVariables', default=False, action='store_true',
419 help='Show a list of unused variables.')
420 gVariables.add_argument('--removeUnusedLocalVariables',
421 help='Remove unused local variables, excluding some variables (comma-' +
422 'separated list or NONE to exclude nothing).')
423 gVariables.add_argument('--removePHYEXUnusedLocalVariables',
424 help='Remove unused local variables, excluding some variables (comma-' +
425 'separated list or NONE to exclude nothing). This option takes ' +
426 'into account the mnh_expand directives to prevent from ' +
427 'removing useful variables.')
428 gVariables.add_argument('--addExplicitArrayBounds', action='store_true',
429 help='Adds explicit bounds to arrays that already have parentheses.')
430 gVariables.add_argument('--addArrayParentheses', action='store_true',
431 help='Adds parentheses to arrays (A => A(:))')
432 gVariables.add_argument('--modifyAutomaticArrays', metavar="DECL#START#END",
433 help='Transform all automatic arrays declaration using the templates.' +
434 ' The DECL part of the template will replace the declaration ' +
435 'statement, the START part will be inserted as the first ' +
436 'executable statement while the END part will be inserted as ' +
437 'the last executable statement. Each part ' +
438 'of the template can use the following place holders: ' +
439 '"{doubledotshape}", "{shape}", "{lowUpList}", "{name}" and ' +
440 '"{type}" which are, respectively modified into ' +
441 '":, :, :", "I, I:J, 0:I", "1, I, I, J, 0, I", "A", "REAL" ' +
442 'if the original declaration statement ' +
443 'was "A(I, I:J, 0:I)". For example, the template ' +
444 '"{type}, DIMENSION({doubledotshape}), ALLOCATABLE :: ' +
445 '{name}#ALLOCATE({name}({shape}))#DEALLOCATE({name})"' +
446 'will replace automatic arrays by allocatables.')
447 gVariables.add_argument('--replaceAutomaticWithAllocatable', action='store_true',
448 help='Replace all automatic arrays with allocatable arrays.')
449 gVariables.add_argument('--addArgInTree', default=None, action='append', nargs=3,
450 metavar=('VARNAME', 'DECLARATION', 'POSITION'),
451 help='Add an argument variable. The first argument is the variable ' +
452 'name, the second one is the declarative statement to insert, ' +
453 'the third one is the position (python indexing) the new ' +
454 'variable will have in the calling statement of the ' +
455 'routine. Needs the --stopScopes argument')
456 gVariables.add_argument('--addONLY', default=False, action='store_true',
457 help='Add missing ONLY clause to USE statements.')
458
459
461 """
462 Updates an argparse parser with cosmetics arguments
463 """
464 gCosmetics = parser.add_argument_group('Cosmetics options')
465 gCosmetics.add_argument('--upperCase', default=False, action='store_true',
466 help='Put FORTRAN code in upper case letters')
467 gCosmetics.add_argument('--lowerCase', default=False, action='store_true',
468 help='Put FORTRAN code in lower case letters')
469 gCosmetics.add_argument('--changeIfStatementsInIfConstructs', default=False,
470 action='store_true',
471 help='Find all if-statement and convert it to if-then-statement')
472 gCosmetics.add_argument('--indent', default=False, action='store_true',
473 help='Correct indentation')
474 gCosmetics.add_argument('--removeIndent', default=False, action='store_true',
475 help='Remove indentation')
476 gCosmetics.add_argument('--removeEmptyLines', default=False, action='store_true',
477 help='Remove empty lines')
478 gCosmetics.add_argument('--removeComments', default=False, action='store_true',
479 help='Remove comments')
480 gCosmetics.add_argument('--updateSpaces', default=False, action='store_true',
481 help='Updates spaces around operators, commas, parenthesis and ' +
482 'at the end of line')
483 gCosmetics.add_argument('--alignContinuation', default=False, action='store_true',
484 help='Align the beginings of continued lines')
485 gCosmetics.add_argument('--addBeginContinuation', default=False, action='store_true',
486 help='Add missing continuation characters (\'&\') at the ' +
487 'begining of lines')
488 gCosmetics.add_argument('--removeBeginContinuation', default=False, action='store_true',
489 help='Remove continuation characters (\'&\') at the begining of lines')
490 gCosmetics.add_argument('--removeALLContinuation', default=False, action='store_true',
491 help='Remove all continuation characters(\'&\')')
492 gCosmetics.add_argument('--prettify', default=False, action='store_true',
493 help='Prettify the source code (indentation, spaces...)')
494 gCosmetics.add_argument('--minify', default=False, action='store_true',
495 help='Simplify the source code (indentation, spaces...)')
496 gCosmetics.add_argument('--removeEmptyCONTAINS', default=False, action='store_true',
497 help='Remove useless CONTAINS statements')
498 gCosmetics.add_argument('--formatModuleUse', default=False, action='store_true',
499 help='Order USE declarations by module type (MODD_, MODE_, ' +
500 'MODI_, MODN_) and alphabetically within each group')
501
502
504 """
505 Updates an argparse parser with applications arguments
506 """
507 gApplications = parser.add_argument_group('Options to apply upper level transformation')
508 gApplications.add_argument('--deleteDrHook', default=False, action='store_true',
509 help='Delete DR HOOK use')
510 gApplications.add_argument('--addDrHook', default=False, action='store_true',
511 help='Add DR HOOK')
512 gApplications.add_argument('--deleteBudgetDDH', default=False, action='store_true',
513 help='Delete Budget/DDH use')
514 gApplications.add_argument('--deleteRoutineCallsMesoNHGPU', default=False, action='store_true',
515 help='Delete parts of the code not compatible with MesoNH-OpenACC' +
516 'such as OCND2 blocks')
517 gApplications.add_argument('--splitModuleRoutineFile', default=False, action='store_true',
518 help='Split a file')
519 gApplications.add_argument('--deleteNonColumnCallsPHYEX', default=False, action='store_true',
520 help='Delete call to PHYEX routines that needs information on ' +
521 'horizontal points (multiple column dependency')
522 gApplications.add_argument('--removeIJDim', default=False, action='store_true',
523 help='Remove I and J dimensions (1, KLON). ' +
524 'Needs the --stopScopes argument.')
525 gApplications.add_argument('--expandAllArraysPHYEX', default=False, action='store_true',
526 help='Expand all array syntax (computing and where block) ' +
527 'using PHYEX conventions')
528 gApplications.add_argument('--expandAllArraysPHYEXConcurrent', default=False,
529 action='store_true',
530 help='Expand all array syntax with DO CONCURRENT loops ' +
531 '(computing and where block) using PHYEX conventions')
532 gApplications.add_argument('--expandAllArrays', default=False, action='store_true',
533 help='Expand all array syntax (computing and where block) ' +
534 'using mnh directives if present')
535 gApplications.add_argument('--expandAllArraysConcurrent', default=False, action='store_true',
536 help='Expand all array syntax with DO CONCURRENT loops ' +
537 '(computing and where block) using mnh directives if present')
538 gApplications.add_argument('--inlineContainedSubroutinesPHYEX', default=False,
539 action='store_true',
540 help='Inline containted subroutines in main routine, using ' +
541 'PHYEX conventions')
542 gApplications.add_argument('--addStack', metavar='MODEL', type=str,
543 help='Add local arrays to the stack. The argument is the ' +
544 'the model name in which stack must be added ("AROME" ' +
545 'or "MESONH"). Needs the --stopScopes argument for AROME.')
546 gApplications.add_argument('--addIncludes', default=False, action='store_true',
547 help='Add .h includes in the file and remove the INCLUDE statement')
548 gApplications.add_argument('--addSubmodulePHYEX', default=False, action='store_true',
549 help='Add SUBMODULE and INTERFACE of subroutines in PHYEX')
550 gApplications.add_argument('--mnhExpand', default=False, action='store_true',
551 help='Apply the mnh_expand directives with DO loops')
552 gApplications.add_argument('--mnhExpandConcurrent', default=False, action='store_true',
553 help='Apply the mnh_expand directives with DO CONCURRENT loops')
554 gApplications.add_argument('--addMPPDB_CHECKS', default=False, action='store_true',
555 help='Add MPPDB_CHEKS bit-repro checking routines of MesoNH for ' +
556 'all in and inout arrays in subroutines')
557 gApplications.add_argument('--addPrints', default=False, action='store_true',
558 help='Add Prints of min/maxval and shape of all in, out, inout ' +
559 'arguments of all scopes')
560 gApplications.add_argument('--shumanFUNCtoCALL', default=False, action='store_true',
561 help='Transform shuman functions to call statements')
562 gApplications.add_argument('--mathFunctoBRFunc', default=False, action='store_true',
563 help='Convert intrinsic math functions **, LOG, ATAN, **2, **3, ' +
564 '**4, EXP, COS, SIN, ATAN2 into a self defined function BR_ ' +
565 'for MesoNH bit-repro.')
566 gApplications.add_argument('--convertTypesInCompute', default=False, action='store_true',
567 help='Use single variable instead of variable contained in ' +
568 'structure in compute statement for optimization issue ')
569 gApplications.add_argument('--buildModi', default=False, action='store_true',
570 help='Builds the corresponding modi_ file')
571 gApplications.add_argument('--removeExtraDOinMnhDoConcurrent', default=False,
572 action='store_true',
573 help='Remove DO and ENDDO instructions inside !$mnh_do_concurrent')
574 gApplications.add_argument('--convertuseModuleToIncludes', default=False,
575 action='store_true',
576 help='Convert USE MODULE, ONLY: ROUTINE to #include routine.intfb.h')
577
578
580 """
581 Updates an argparse parser with openACC arguments
582 """
583 gOpenACC = parser.add_argument_group('OpenACC')
584 gOpenACC.add_argument('--addACCData', default=False, action='store_true',
585 help='Add !$acc data present and !$acc end data directives')
586 gOpenACC.add_argument('--addACCRoutineSeq', default=False, action='store_true',
587 help='Add "!$acc routine seq" to routines under stopScopes')
588 gOpenACC.add_argument('--craybyPassDOCONCURRENT', default=False, action='store_true',
589 help='remove acc loop independant collapse for BR_ fonctions and ' +
590 'mnh_undef(OPENACC) macro' +
591 ' use DO CONCURRENT with mnh_undef(LOOP)')
592 gOpenACC.add_argument('--removeACC', default=False, action='store_true',
593 help='remove all ACC directives')
594 gOpenACC.add_argument('--removebyPassDOCONCURRENT', default=False, action='store_true',
595 help='remove macro !$mnh_(un)def(OPENACC) and !$mnh_(un)def(LOOP) ' +
596 'directives')
597 gOpenACC.add_argument('--buildACCTypeHelpers', default=False, action='store_true',
598 help='build module files containing helpers to copy user ' +
599 'type structures')
600 gOpenACC.add_argument('--allocatetoHIP', default=False, action='store_true',
601 help='convert (DE)ALLOCATE to (DE)ALLOCATE_HIP on variables only sent ' +
602 'to the GPU via acc enter data copyin/create (for GPU AMD MI250X)')
603
604
606 """
607 Updates an argparse parser with checks arguments
608 """
609
610 gChecks = parser.add_argument_group('Check options')
611 gChecks.add_argument('--checkIMPLICIT', choices={'Warn', 'Err'}, default=None,
612 help='Send a warning or raise an error if the "IMPLICIT NONE" ' +
613 'is missing')
614 gChecks.add_argument('--checkINTENT', choices={'Warn', 'Err'}, default=None,
615 help='Send a warning or raise an error if the "INTENT" ' +
616 'attribute is missing for a dummy argument')
617 gChecks.add_argument('--checkOpInCall', choices={'Warn', 'Err'}, default=None,
618 help='Send a warning or raise an error if a call argument is an '
619 'operation.')
620 gChecks.add_argument('--checkUnusedLocalVar', choices={'Warn', 'Err'}, default=None,
621 help='Send a warning or raise an error if some local '
622 'variables are unused.')
623 gChecks.add_argument('--checkPHYEXUnusedLocalVar', choices={'Warn', 'Err'}, default=None,
624 help='Send a warning or raise an error if some local '
625 'variables are unused (excluding variables needed '
626 'for mnh_expand directives).')
627 gChecks.add_argument('--checkEmptyParensInCall', choices={'Warn', 'Err'}, default=None,
628 help='Send a warning or raise an error if a call argument is an '
629 'array with empty parens.')
630 gChecks.add_argument('--checkONLY', choices={'Warn', 'Err'}, default=None,
631 help='Send a warning or raise an error if a USE statement is not '
632 'followed by an ONLY clause.')
633 gChecks.add_argument('--checkKeyDim', choices={'Warn', 'Err'}, default=None,
634 help='Send a warning or raise an error if MERGE-based array '
635 'dimensions are inconsistent across scopes.')
636
637
639 """
640 Updates an argparse parser with statements arguments
641 """
642
643 gStatement = parser.add_argument_group('Statements options')
644 gStatement.add_argument('--removeCall', action='append',
645 help="Call to remove from the source code. The argument " +
646 "is the subprogram name")
647 gStatement.add_argument('--removePrints', default=False, action='store_true',
648 help="Remove print statements from the source code.")
649 gStatement.add_argument('--inlineContainedSubroutines', default=False, action='store_true',
650 help='Inline containted subroutines in main routine')
651 gStatement.add_argument('--setFalseIfStmt', default=None,
652 help='Replace this value by .FALSE. in if statements')
653 gStatement.add_argument('--checkEmptyParensInMnhExpand', choices={'Warn', 'Err'}, default=None,
654 help='Send a warning or raise an error if an empty parens ' +
655 'is found in an mnh_expand directive.')
656
657
659 """
660 Updates an argparse parser with misc arguments
661 """
662 gMisc = parser.add_argument_group('Miscellaneous')
663 gMisc.add_argument('--showScopes', default=False, action='store_true',
664 help='Show the different scopes found in the source code')
665 gMisc.add_argument('--empty', default=False, action='store_true',
666 help='Empty the different scopes')
667
668
669def updateParserTree(parser, withPlotCentralFile, treeIsOptional):
670 """
671 Updates an argparse parser with statements arguments
672 :param withPlotCentralFile: to add the --plotCentralFile argumen
673 :param treeIsOptional: is the --tree argument optional?
674 """
675 gTree = parser.add_argument_group('Tree')
676 gTree.add_argument('--tree', default=None, action='append', required=not treeIsOptional,
677 help='Directories where source code must be searched for')
678 gTree.add_argument('--descTree', default=None, required=not treeIsOptional,
679 help='File to write and/or read the description of the tree.')
680 if withPlotCentralFile:
681 gTree.add_argument('--plotCentralFile', default=None, type=str,
682 help='Central file of the plot')
683 gTree.add_argument('--plotCompilTree', default=None,
684 help='File name for compilation dependency graph (.dot or image extension)')
685 gTree.add_argument('--plotExecTree', default=None,
686 help='File name for execution dependency graph (.dot or image extension)')
687 gTree.add_argument('--plotMaxUpper', default=None, type=int,
688 help='Maximum number of upper elements in the plot tree')
689 gTree.add_argument('--plotMaxLower', default=None, type=int,
690 help='Maximum number of lower elements in the plot tree')
691 gTree.add_argument('--stopScopes', default=None, type=str,
692 help='#-separated list of scopes ' +
693 'where the recursive inclusion of an argument variable ' +
694 'must stop (needed for some transformations).')
695
696
698 """
699 Updates an argparse parser with statements arguments
700 """
701 gCpp = parser.add_argument_group('Preprocessor')
702 gCpp.add_argument('--applyCPPifdef', nargs='*', action='append',
703 help="This option is followed by the list of defined or undefined " +
704 "CPP keys. " +
705 "All #ifdef and #ifndef concerning these keys are evaluated. " +
706 "Undefined keys are preceded by a percentage sign.")
707
708
709def applyTransfo(pft, arg, args, plotCentralFile):
710 """
711 Apply transformation on a PYFT instance
712 :param pft: PYFT instance
713 :param arg: argument to deal with
714 :param args: parsed argparsed arguments
715 :param plotCentralFile: central file for plots
716 """
717 simplify = {'simplify': args.simplify}
718 parserOptions = getParserOptions(args)
719 stopScopes = args.stopScopes.split('#') if args.stopScopes is not None else None
720
721 # File name manipulations
722 applyTransfoFileName(pft, arg)
723
724 # Variables
725 applyTransfoVariables(pft, arg, args, simplify, parserOptions, stopScopes)
726
727 # Applications
728 applyTransfoApplications(pft, arg, args, simplify, parserOptions, stopScopes)
729
730 # OpenACC
731 applyTransfoOpenACC(pft, arg, args, stopScopes)
732
733 # Cosmetics
734 applyTransfoCosmetics(pft, arg, args)
735
736 # Checks
737 applyTransfoChecks(pft, arg, args, stopScopes)
738
739 # Statements
740 applyTransfoStatements(pft, arg, args, simplify)
741
742 # Misc
743 applyTransfoMisc(pft, arg, args, simplify)
744
745 # Tree
746 applyTransfoTree(pft, arg, args, plotCentralFile)
747
748 # Preprocessor
749 applyTransfoPreprocessor(pft, arg, args)
750
751
753 """
754 Apply file name transformations on a PYFT instance
755 :param pft: PYFT instance
756 :param arg: argument to deal with
757 :param args: parsed argparsed arguments
758 """
759
760 # File name manipulations
761 if arg == '--renamefF':
762 pft.renameUpper()
763 elif arg == '--renameFf':
764 pft.renameLower()
765
766
767def applyTransfoVariables(pft, arg, args, simplify, parserOptions, stopScopes):
768 """
769 Apply variables transformations on a PYFT instance
770 :param pft: PYFT instance
771 :param arg: argument to deal with
772 :param args: parsed argparsed arguments
773 :param simplify: kwargs to simplify
774 :param parserOptions: fxtran parser options
775 :param stopScopes: upper limit in call tree for some transformations
776 """
777 if arg == '--showVariables':
778 pft.varList.showVarList()
779 elif arg == '--attachArraySpecToEntity':
780 pft.attachArraySpecToEntity()
781 elif arg == '--removeVariable':
782 pft.removeVar(args.removeVariable, **simplify)
783 elif arg == '--addVariable':
784 pft.addVar([[v[0], v[1], v[2], (int(v[3]) if isint(v[3]) else None)]
785 for v in args.addVariable])
786 elif arg == '--addModuleVariable':
787 pft.addModuleVar([[v[0], v[1], v[2]] for v in args.addModuleVariable])
788 elif arg == '--showUnusedVariables':
789 pft.showUnusedVar()
790 elif arg == '--removeUnusedLocalVariables':
791 pft.removeUnusedLocalVar(
792 [item.strip() for item in args.removeUnusedLocalVariables.split(',')]
793 if args.removeUnusedLocalVariables != 'NONE' else None, **simplify)
794 elif arg == '--removePHYEXUnusedLocalVariables':
795 pft.removePHYEXUnusedLocalVar(
796 [item.strip() for item in args.removePHYEXUnusedLocalVariables.split(',')]
797 if args.removePHYEXUnusedLocalVariables != 'NONE' else None, **simplify)
798 elif arg == '--addExplicitArrayBounds':
799 pft.addExplicitArrayBounds()
800 elif arg == '--addArrayParentheses':
801 pft.addArrayParentheses()
802 elif arg == '--modifyAutomaticArrays':
803 pft.modifyAutomaticArrays(*(args.modifyAutomaticArrays.split('#')))
804 elif arg == '--replaceAutomaticWithAllocatable':
805 pft.modifyAutomaticArrays(
806 "{type}, DIMENSION({doubledotshape}), ALLOCATABLE :: {name}",
807 "ALLOCATE({name}({shape}))", "DEALLOCATE({name})")
808 elif arg == '--addArgInTree':
809 for varName, declStmt, pos in args.addArgInTree:
810 pft.addArgInTree(varName, declStmt, int(pos), stopScopes,
811 parserOptions=parserOptions,
812 wrapH=args.wrapH)
813 elif arg == '--addONLY':
814 pft.addONLY(parserOptions=parserOptions, wrapH=args.wrapH)
815
816
817def applyTransfoApplications(pft, arg, args, simplify, parserOptions, stopScopes):
818 """
819 Apply applications transformations on a PYFT instance
820 :param pft: PYFT instance
821 :param arg: argument to deal with
822 :param args: parsed argparsed arguments
823 :param simplify: kwargs to simplify
824 :param parserOptions: fxtran parser options
825 :param stopScopes: upper limit in call tree for some transformations
826 """
827 if arg == '--addStack':
828 pft.addStack(args.addStack, stopScopes,
829 parserOptions=parserOptions,
830 wrapH=args.wrapH)
831 elif arg == '--deleteDrHook':
832 pft.deleteDrHook(**simplify)
833 elif arg == '--addDrHook':
834 pft.addDrHook()
835 elif arg == '--deleteBudgetDDH':
836 pft.deleteBudgetDDH(**simplify)
837 elif arg == '--deleteRoutineCallsMesoNHGPU':
838 pft.deleteRoutineCallsMesoNHGPU(**simplify)
839 elif arg == '--deleteNonColumnCallsPHYEX':
840 pft.deleteNonColumnCallsPHYEX(**simplify)
841 elif arg == '--addMPPDB_CHECKS':
842 pft.addMPPDB_CHECKS()
843 elif arg == '--addPrints':
844 pft.addMPPDB_CHECKS(printsMode=True)
845 elif arg == '--addSubmodulePHYEX':
846 pft.addSubmodulePHYEX()
847 # mnhExpand must be before inlineContainedSubroutines as inlineContainedSubroutines
848 # can change variable names used by mnh_expand directives
849 assert not (args.mnhExpand and args.mnhExpandConcurrent), \
850 "Only one of --mnhExpand and --mnhExpandConcurrent"
851 if arg == '--mnhExpand':
852 pft.removeArraySyntax(everywhere=False, addAccIndependentCollapse=False)
853 elif arg == '--mnhExpandConcurrent':
854 pft.removeArraySyntax(concurrent=True, everywhere=False)
855 elif arg == '--inlineContainedSubroutines':
856 pft.inlineContainedSubroutines(**simplify)
857 elif arg == '--inlineContainedSubroutinesPHYEX':
858 pft.inlineContainedSubroutinesPHYEX(**simplify)
859 elif arg == '--expandAllArrays':
860 pft.removeArraySyntax()
861 elif arg == '--expandAllArraysConcurrent':
862 pft.removeArraySyntax(concurrent=True)
863 elif arg == '--expandAllArraysPHYEX':
864 pft.expandAllArraysPHYEX()
865 elif arg == '--expandAllArraysPHYEXConcurrent':
866 pft.expandAllArraysPHYEX(concurrent=True)
867 elif arg == '--removeIJDim':
868 pft.removeIJDim(stopScopes,
869 parserOptions=parserOptions,
870 wrapH=args.wrapH, **simplify)
871 elif arg == '--shumanFUNCtoCALL':
872 pft.shumanFUNCtoCALL()
873 elif arg == '--buildACCTypeHelpers':
874 pft.buildACCTypeHelpers()
875 elif arg == '--mathFunctoBRFunc':
876 pft.mathFunctoBRFunc()
877 elif arg == '--convertTypesInCompute':
878 pft.convertTypesInCompute()
879 elif arg == '--buildModi':
880 pft.buildModi()
881 elif arg == '--splitModuleRoutineFile':
882 pft.splitModuleRoutineFile()
883 elif arg == '--removeExtraDOinMnhDoConcurrent':
884 pft.removeExtraDOinMnhDoConcurrent()
885 elif arg == '--convertuseModuleToIncludes':
886 pft.convertuseModuleToIncludes()
887
888
889def applyTransfoOpenACC(pft, arg, args, stopScopes): # pylint: disable=unused-argument
890 """
891 Apply openACC transformations on a PYFT instance
892 :param pft: PYFT instance
893 :param arg: argument to deal with
894 :param args: parsed argparsed arguments
895 :param stopScopes: upper limit in call tree for some transformations
896 """
897 if arg == '--addACCData':
898 pft.addACCData()
899 elif arg == '--craybyPassDOCONCURRENT':
900 pft.craybyPassDOCONCURRENT()
901 elif arg == '--removebyPassDOCONCURRENT':
902 pft.removebyPassDOCONCURRENT()
903 elif arg == '--addACCRoutineSeq':
904 pft.addACCRoutineSeq(stopScopes)
905 elif arg == '--removeACC':
906 pft.removeACC()
907 elif arg == '--allocatetoHIP':
908 pft.allocatetoHIP()
909
910
911def applyTransfoCosmetics(pft, arg, args):
912 """
913 Apply cosmetics transformations on a PYFT instance
914 :param pft: PYFT instance
915 :param arg: argument to deal with
916 :param args: parsed argparsed arguments
917 """
918 if arg == '--upperCase':
919 pft.upperCase()
920 elif arg == '--lowerCase':
921 pft.lowerCase()
922 elif arg == '--changeIfStatementsInIfConstructs':
923 pft.changeIfStatementsInIfConstructs()
924 elif arg == '--indent':
925 pft.indent()
926 elif arg == '--removeIndent':
927 pft.indent(indentProgramunit=0, indentBranch=0)
928 elif arg == '--removeEmptyLines':
929 pft.removeEmptyLines()
930 elif arg == '--removeComments':
931 pft.removeComments()
932 elif arg == '--updateSpaces':
933 pft.updateSpaces()
934 elif arg in ARG_UPDATE_CNT:
935 pft.updateContinuation(align=args.alignContinuation,
936 addBegin=args.addBeginContinuation,
937 removeBegin=args.removeBeginContinuation,
938 removeALL=args.removeALLContinuation)
939 elif arg == '--prettify':
940 pft.indent()
941 pft.upperCase()
942 pft.removeEmptyLines()
943 pft.updateSpaces()
944 pft.updateContinuation()
945 elif arg == '--minify':
946 pft.indent(indentProgramunit=0, indentBranch=0)
947 pft.upperCase()
948 pft.removeComments()
949 pft.removeEmptyLines()
950 pft.updateSpaces()
951 pft.updateContinuation(align=False, removeALL=True, addBegin=False)
952 elif arg == '--removeEmptyCONTAINS':
953 pft.removeEmptyCONTAINS()
954 elif arg == '--formatModuleUse':
955 pft.formatModuleUse()
956
957
958def applyTransfoChecks(pft, arg, args, stopScopes):
959 """
960 Apply checks transformations on a PYFT instance
961 :param pft: PYFT instance
962 :param arg: argument to deal with
963 :param args: parsed argparsed arguments
964 :param stopScopes: list of scope paths or None
965 """
966 if arg == '--checkIMPLICIT':
967 pft.checkImplicitNone(args.checkIMPLICIT == 'Err')
968 elif arg == '--checkINTENT':
969 pft.checkIntent(args.checkINTENT == 'Err')
970 elif arg == '--checkOpInCall':
971 pft.checkOpInCall(args.checkOpInCall == 'Err')
972 elif arg == '--checkUnusedLocalVar':
973 pft.checkUnusedLocalVar(args.checkUnusedLocalVar == 'Err')
974 elif arg == '--checkPHYEXUnusedLocalVar':
975 pft.checkPHYEXUnusedLocalVar(args.checkPHYEXUnusedLocalVar == 'Err')
976 elif arg == '--checkEmptyParensInCall':
977 pft.checkEmptyParensInCall(args.checkEmptyParensInCall == 'Err')
978 elif arg == '--checkONLY':
979 pft.checkONLY(args.checkONLY == 'Err')
980 elif arg == '--checkKeyDim':
981 pft.checkKeyDimConsistency(args.checkKeyDim == 'Err',
982 stopScopes=stopScopes)
983 elif arg == '--checkEmptyParensInMnhExpand':
984 pft.checkEmptyParensInMnhExpand(args.checkEmptyParensInMnhExpand == 'Err')
985
986
987def applyTransfoStatements(pft, arg, args, simplify):
988 """
989 Apply statements transformations on a PYFT instance
990 :param pft: PYFT instance
991 :param arg: argument to deal with
992 :param args: parsed argparsed arguments
993 :param simplify: kwargs to simplify
994 """
995 if arg == '--removeCall':
996 for rc in args.removeCall:
997 pft.removeCall(rc, **simplify)
998 elif arg == '--removePrints':
999 pft.removePrints(**simplify)
1000 elif arg == '--setFalseIfStmt':
1001 pft.setFalseIfStmt(args.setFalseIfStmt, **simplify)
1002
1003
1004def applyTransfoMisc(pft, arg, args, simplify): # pylint: disable=unused-argument
1005 """
1006 Apply misc transformations on a PYFT instance
1007 :param pft: PYFT instance
1008 :param arg: argument to deal with
1009 :param args: parsed argparsed arguments
1010 :param simplify: kwargs to simplify
1011 """
1012 if arg == '--showScopes':
1013 pft.showScopesList()
1014 elif arg == '--empty':
1015 pft.empty(**simplify)
1016
1017
1018def applyTransfoTree(pft, arg, args, plotCentralFile):
1019 """
1020 Apply tree transformations on a PYFT instance
1021 :param pft: PYFT instance
1022 :param arg: argument to deal with
1023 :param args: parsed argparsed arguments
1024 :param plotCentralFile: central file for plots
1025 """
1026 if arg == '--plotCompilTree' and plotCentralFile is not None:
1027 pft.tree.plotCompilTreeFromFile(plotCentralFile, args.plotCompilTree,
1028 args.plotMaxUpper, args.plotMaxLower)
1029 elif arg == '--plotExecTree' and plotCentralFile is not None:
1030 pft.tree.plotExecTreeFromFile(plotCentralFile, args.plotExecTree,
1031 args.plotMaxUpper, args.plotMaxLower)
1032
1033
1034def applyTransfoPreprocessor(pft, arg, args):
1035 """
1036 Apply preprocessor transformations on a PYFT instance
1037 :param pft: PYFT instance
1038 :param arg: argument to deal with
1039 :param args: parsed argparsed arguments
1040 """
1041 if arg == '--applyCPPifdef':
1042 pft.applyCPPifdef([k for aList in args.applyCPPifdef for k in aList])
1043
1044
1045if __name__ == "__main__":
1046 main()
applyTransfoApplications(pft, arg, args, simplify, parserOptions, stopScopes)
Definition scripting.py:817
updateParserVariables(parser)
Definition scripting.py:385
applyTransfoStatements(pft, arg, args, simplify)
Definition scripting.py:987
applyTransfoTree(pft, arg, args, plotCentralFile)
applyTransfoPreprocessor(pft, arg, args)
updateParserCosmetics(parser)
Definition scripting.py:460
applyTransfoVariables(pft, arg, args, simplify, parserOptions, stopScopes)
Definition scripting.py:767
updateParserInputsOutputs(parser, withInput, withOutput, withXml)
Definition scripting.py:348
updateParserApplications(parser)
Definition scripting.py:503
updateParserFxtran(parser)
Definition scripting.py:373
updateParserChecks(parser)
Definition scripting.py:605
updateParserOpenACC(parser)
Definition scripting.py:579
updateParserPreprocessor(parser)
Definition scripting.py:697
mainParallel(argv=None)
Definition scripting.py:58
applyTransfoOpenACC(pft, arg, args, stopScopes)
Definition scripting.py:889
updateParserStatements(parser)
Definition scripting.py:638
getArgs(parser, argv)
Definition scripting.py:173
applyTransfo(pft, arg, args, plotCentralFile)
Definition scripting.py:709
getDescTree(args, cls=Tree)
Definition scripting.py:240
applyTransfoMisc(pft, arg, args, simplify)
updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, treeIsOptional, nbPar, restrictScope)
Definition scripting.py:258
applyTransfoCosmetics(pft, arg, args)
Definition scripting.py:911
applyTransfoFileName(pft, arg)
Definition scripting.py:752
updateParserTree(parser, withPlotCentralFile, treeIsOptional)
Definition scripting.py:669
applyTransfoChecks(pft, arg, args, stopScopes)
Definition scripting.py:958
updateParserMisc(parser)
Definition scripting.py:658