2This module contains functions usefull to build scripts around the pyfortool library
6from multiprocessing
import cpu_count, get_context
7from multiprocessing.managers
import BaseManager
18from pyfortool
import __version__
23 Function to use on each file
24 :param clsPYFT: PYFT class to use
25 :param filename: file name
29 allArgs, orderedOptions = allFileArgs[filename]
32 with PYFT(filename, filename,
35 enableCache=allArgs.enableCache)
as pft:
38 for arg
in orderedOptions:
39 logging.debug(
'Applying %s on %s', arg, filename)
41 filename
if filename == allArgs.plotCentralFile
else None)
42 logging.debug(
' -> Done')
45 if not allArgs.dryRun:
51 except Exception
as exc:
52 logging.error(
"The following error has occurred in the file %s", filename)
54 traceback.print_exception(type(exc), exc, exc.__traceback__, file=sys.stdout)
61 Manager sharing a single Tree instance between the pyfortool_parallel processes
65TreeManager.register(
'Tree', Tree)
82 Core of the pyfortool_parallel.py command
83 :param argv: list of arguments (including the 'programm' name in first position)
84 as would be obtained by sys.argv. Or None to use sys.argv
86 parser = argparse.ArgumentParser(description=
'Python FORTRAN tool', allow_abbrev=
False,
87 epilog=
"The argument order matters.")
89 updateParser(parser, withInput=
False, withOutput=
False, withXml=
False, withPlotCentralFile=
True,
90 treeIsOptional=
False, nbPar=
True, restrictScope=
False)
91 commonArgs, getFileArgs =
getArgs(parser, argv)
98 ctx = get_context(
'fork')
106 PYFT.setParallel(sharedTree, clsLock=ctx.Lock, clsRLock=ctx.RLock)
109 allFileArgs = {file: getFileArgs(file)
for file
in sharedTree.getFiles()}
110 logging.info(
'Executing in parallel on %i files with a maximum of %i processes',
111 len(allFileArgs), commonArgs.nbPar)
112 with ctx.Pool(commonArgs.nbPar, initializer=poolInit,
113 initargs=(PYFT, allFileArgs))
as pool:
114 result = pool.map(task, sharedTree.getFiles())
117 sharedTree.toJson(commonArgs.descTree)
120 errors = [item[1]
for item
in result
if item[0] != 0]
123 logging.error(
'List of files with error:')
125 logging.error(
' - %s', error)
126 raise PYFTError(f
"Errors have been reported in {status} file(s).")
135 Core of the pyfortool.py command
136 :param argv: list of arguments (including the 'programm' name in first position)
137 as would be obtained by sys.argv. Or None to use sys.argv
139 parser = argparse.ArgumentParser(description=
'Python FORTRAN tool', allow_abbrev=
False,
140 epilog=
"The argument order matters.")
142 updateParser(parser, withInput=
True, withOutput=
True, withXml=
True, withPlotCentralFile=
False,
143 treeIsOptional=
True, nbPar=
False, restrictScope=
True)
144 args, orderedOptions =
getArgs(parser, argv)[1]()
151 pft =
PYFT(args.INPUT, args.OUTPUT, parserOptions=parserOptions,
152 verbosity=args.logLevel, wrapH=args.wrapH, tree=descTree,
153 enableCache=args.enableCache)
154 if args.restrictScope !=
'':
155 pft = pft.getScopeNode(args.restrictScope)
158 for arg
in orderedOptions:
159 logging.debug(
'Applying %s on %s', arg, args.INPUT)
160 applyTransfo(pft, arg, args, plotCentralFile=args.INPUT)
161 logging.debug(
' -> Done')
164 if descTree
is not None:
165 descTree.toJson(args.descTree)
166 if args.xml
is not None:
167 pft.mainScope.writeXML(args.xml)
169 pft.mainScope.write()
172 pft.mainScope.close()
176 logging.error(
"The following error has occurred in the file %s", args.INPUT)
180ARG_UPDATE_CNT = (
'--alignContinuation',
'--addBeginContinuation',
181 '--removeBeginContinuation',
182 '--emoveALLContinuation')
187 Parse arguments and interpret the --optsByEnv option
188 :param parser: argparse parser
189 :param argv: list of arguments (including the 'programm' name in first position)
190 as would be obtained by sys.argv. Or None to use sys.argv.
191 :return: a tuple with
192 - an argparse namespace containing common arguments (not using the --optsEnv option)
193 - a function taking a filename as input and returning
194 - an argparse namespace with the common arguments and the ones added by
195 interpreting the --optsEnv option
196 - an ordered list of arguments
200 args = parser.parse_args(argv[1:])
202 def getFileArgs(filename=args.INPUT if hasattr(args,
'INPUT')
else None):
204 :param filename: name of source code file
205 :return: argparse namespace to use with this file and
206 a list given the order in which the arguments were provided
210 if args.optsByEnv
is not None:
212 for line
in []
if args.optsByEnv
is None else os.environ[args.optsByEnv].split(
'\n'):
214 if re.match(line.split(
':=:')[0], filename):
215 extra = line.split(
':=:')[1]
218 index = arguments.index(
'--optsByEnv')
219 arguments = arguments[:index] + shlex.split(extra) + arguments[index + 2:]
224 for arg
in arguments:
225 if arg.startswith(
'--')
and arg
not in optList:
226 if arg
in ARG_UPDATE_CNT:
233 return parser.parse_args(arguments), optList
235 return args, getFileArgs
240 Get the options to use for the fxtran parser
241 :param args: arguments parsed by the argparse parser
243 if args.parserOption
is None:
244 parserOptions = PYFT.DEFAULT_FXTRAN_OPTIONS.copy()
246 parserOptions = [el
for elements
in args.parserOption
for el
in elements]
248 parserOptions = [opt
for opt
in parserOptions
if opt
not in (
'-no-include',
'-noinclude')]
254 get the Tree object built with the parsed arguments
255 :param args: arguments parsed by the argparse parser
256 :param cls: class to use (usefull for manager)
257 :return: a Tree instance
261 descTree = cls(tree=args.tree, descTreeFile=args.descTree,
262 parserOptions=parserOptions,
263 wrapH=args.wrapH, verbosity=args.logLevel)
269def updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, treeIsOptional,
270 nbPar, restrictScope):
272 Updates an argparse parser with arguments common to all the different tools
273 :param parser: parser in which arguments are added
274 :param withOutput: do we need the INPUT argument
275 :param withOutput: do we need the OUTPUT argument
276 :param withXml: do we need to be able to define an XML output file
277 :param withPlotCentralFile: to add the --plotCentralFile argument
278 :param treeIsOptional: is the --tree argument optional?
279 :param nbPar: number of parallel processes
280 :param restrictScope: can we specify the scope path
291 assert not parser.allow_abbrev,
'parser must be created with allow_abbrev=False'
293 parser.add_argument(
'--version', action=
'version',
294 version=
'%(prog)s {version}'.format(version=__version__))
295 parser.add_argument(
'--simplify', default=
False, action=
'store_true',
296 help=
'After a deletion, recursively deletes the code ' +
297 'and variables that have become useless')
298 parser.add_argument(
'--logLevel', default=
'warning',
299 help=
'Provide logging level. Example --logLevel debug (default is warning)')
300 parser.add_argument(
'--enableCache', default=
False, action=
'store_true',
301 help=
'Precompute parent of each xml node and store the result')
303 parser.add_argument(
'--nbPar', default=cpu_count(), type=int,
304 help=
'Number of parallel processes, 0 to get as many processes ' +
305 'as the number of cores (default=0)')
306 parser.add_argument(
'--optsByEnv', default=
None, type=str,
307 help=
'Name of the environment variable containing additional arguments ' +
308 'to use. These arguments are processed after all other arguments. ' +
309 'The variable can contain a multi-lines string. The ' +
310 'variable is read line by line and the last applicable line is ' +
311 'used. A line can take one of these two forms: ' +
312 '1) "FILE_DESCRIPTOR:=:OPTIONS" (where FILE_DESCRIPTOR is a ' +
313 'regular expression to test against the filename. If there ' +
314 'is a match, the OPTIONS can be used for the file) and ' +
315 '2) "OPTIONS" (if the line doesn\'t contain the FILE_DESCRIPTOR ' +
316 'part, it applies to all source code).')
319 parser.add_argument(
'--restrictScope', default=
'', type=str, metavar=
'SCOPEPATH',
320 help=
"Limit the action to this scope path (SUBROUTINE/FUNCTION/" +
321 "MODULE/TYPE). It is '/'-separated path with each element " +
322 "having the form 'module:<name of the module>', " +
323 "'sub:<name of the subroutine>', " +
324 "'func:<name of the function>' or 'type:<name of the type>'.")
362 Updates an argparse parser with input/output arguments
363 :param parser: parser in which arguments are added
364 :param withOutput: do we need the INPUT argument
365 :param withOutput: do we need the OUTPUT argument
366 :param withXml: do we need to be able to define an XML output file
368 gInOut = parser.add_argument_group(
'Input and output')
370 gInOut.add_argument(
'INPUT', help=
'FORTRAN input file')
372 gInOut.add_argument(
'OUTPUT', default=
None, help=
'FORTRAN output file', nargs=
'?')
373 gInOut.add_argument(
'--renamefF', default=
False, action=
'store_true',
374 help=
'Put file extension in upper case')
375 gInOut.add_argument(
'--renameFf', default=
False, action=
'store_true',
376 help=
'Put file extension in lower case')
378 gInOut.add_argument(
'--xml', default=
None, type=str,
379 help=
'Output file for xml')
380 gInOut.add_argument(
'--dryRun', default=
False, action=
'store_true',
381 help=
'Dry run without writing the FORTRAN file (the xml ' +
387 Updates an argparse parser with fxtran arguments
389 gParser = parser.add_argument_group(
'fxtran parser relative options')
390 gParser.add_argument(
'--parserOption', nargs=
'*', action=
'append',
391 help=
'Option to pass to fxtran, defaults ' +
392 f
'to {PYFT.DEFAULT_FXTRAN_OPTIONS}')
393 gParser.add_argument(
'--wrapH', default=
False, action=
'store_true',
394 help=
'Wrap .h file content into a MODULE to enable the reading')
399 Updates an argparse parser with variables arguments
401 gVariables = parser.add_argument_group(
'Options to deal with variables')
402 gVariables.add_argument(
'--showVariables', default=
False, action=
'store_true',
403 help=
'Show the declared variables')
404 gVariables.add_argument(
'--removeVariable', nargs=2, action=
'append',
405 metavar=(
'SCOPEPATH',
'VARNAME'),
406 help=
"Variable to remove from declaration. The first argument " +
407 "is the SUBROUTINE/FUNCTION/MODULE/TYPE where the variable " +
408 "is declared. It is '/'-separated path with each element having " +
409 "the form 'module:<name of the module>', " +
410 "'sub:<name of the subroutine>', " +
411 "'func:<name of the function>' or 'type:<name of the type>'. " +
412 "The second argument is the variable name")
413 gVariables.add_argument(
'--attachArraySpecToEntity', default=
False, action=
'store_true',
414 help=
'Find all T-decl-stmt elements that have a child element ' +
415 'attribute with attribute-N=DIMENSION and move the attribute ' +
416 'into EN-N elements')
417 gVariables.add_argument(
'--addVariable', nargs=4, action=
'append',
418 metavar=(
'SCOPEPATH',
'VARNAME',
'DECLARATION',
'POSITION'),
419 help=
'Add a variable. First argument is the scope path (as for ' +
420 'the --removeVariable option. The second is the variable ' +
421 'name, the third is the declarative statement to insert, ' +
422 'the fourth is the position (python indexing) the new ' +
423 'variable will have in the calling statment of the ' +
424 'routine (non-integer value for a local variable).')
425 gVariables.add_argument(
'--addModuleVariable', nargs=3, action=
'append',
426 metavar=(
'SCOPEPATH',
'MODULENAME',
'VARNAME'),
427 help=
'Add a USE statement. The first argument is the scope path ' +
428 '(as for the --removeVariable option). The second is the module ' +
429 'name; the third is the variable name.')
430 gVariables.add_argument(
'--showUnusedVariables', default=
False, action=
'store_true',
431 help=
'Show a list of unused variables.')
432 gVariables.add_argument(
'--removeUnusedLocalVariables',
433 help=
'Remove unused local variables, excluding some variables (comma-' +
434 'separated list or NONE to exclude nothing).')
435 gVariables.add_argument(
'--removePHYEXUnusedLocalVariables',
436 help=
'Remove unused local variables, excluding some variables (comma-' +
437 'separated list or NONE to exclude nothing). This option takes ' +
438 'into account the mnh_expand directives to prevent from ' +
439 'removing useful variables.')
440 gVariables.add_argument(
'--addExplicitArrayBounds', action=
'store_true',
441 help=
'Adds explicit bounds to arrays that already have parentheses.')
442 gVariables.add_argument(
'--addArrayParentheses', action=
'store_true',
443 help=
'Adds parentheses to arrays (A => A(:))')
444 gVariables.add_argument(
'--modifyAutomaticArrays', metavar=
"DECL#START#END",
445 help=
'Transform all automatic arrays declaration using the templates.' +
446 ' The DECL part of the template will replace the declaration ' +
447 'statement, the START part will be inserted as the first ' +
448 'executable statement while the END part will be inserted as ' +
449 'the last executable statement. Each part ' +
450 'of the template can use the following place holders: ' +
451 '"{doubledotshape}", "{shape}", "{lowUpList}", "{name}" and ' +
452 '"{type}" which are, respectively modified into ' +
453 '":, :, :", "I, I:J, 0:I", "1, I, I, J, 0, I", "A", "REAL" ' +
454 'if the original declaration statement ' +
455 'was "A(I, I:J, 0:I)". For example, the template ' +
456 '"{type}, DIMENSION({doubledotshape}), ALLOCATABLE :: ' +
457 '{name}#ALLOCATE({name}({shape}))#DEALLOCATE({name})"' +
458 'will replace automatic arrays by allocatables.')
459 gVariables.add_argument(
'--replaceAutomaticWithAllocatable', action=
'store_true',
460 help=
'Replace all automatic arrays with allocatable arrays.')
461 gVariables.add_argument(
'--addArgInTree', default=
None, action=
'append', nargs=3,
462 metavar=(
'VARNAME',
'DECLARATION',
'POSITION'),
463 help=
'Add an argument variable. The first argument is the variable ' +
464 'name, the second one is the declarative statement to insert, ' +
465 'the third one is the position (python indexing) the new ' +
466 'variable will have in the calling statement of the ' +
467 'routine. Needs the --stopScopes argument')
468 gVariables.add_argument(
'--addONLY', default=
False, action=
'store_true',
469 help=
'Add missing ONLY clause to USE statements.')
474 Updates an argparse parser with cosmetics arguments
476 gCosmetics = parser.add_argument_group(
'Cosmetics options')
477 gCosmetics.add_argument(
'--splitLongLines', default=
False, action=
'store_true',
478 help=
'Split lines longer than the limit given by --maxLineLength')
479 gCosmetics.add_argument(
'--upperCase', default=
False, action=
'store_true',
480 help=
'Put FORTRAN code in upper case letters')
481 gCosmetics.add_argument(
'--lowerCase', default=
False, action=
'store_true',
482 help=
'Put FORTRAN code in lower case letters')
483 gCosmetics.add_argument(
'--changeIfStatementsInIfConstructs', default=
False,
485 help=
'Find all if-statement and convert it to if-then-statement')
486 gCosmetics.add_argument(
'--indent', default=
False, action=
'store_true',
487 help=
'Correct indentation')
488 gCosmetics.add_argument(
'--removeIndent', default=
False, action=
'store_true',
489 help=
'Remove indentation')
490 gCosmetics.add_argument(
'--removeEmptyLines', default=
False, action=
'store_true',
491 help=
'Remove empty lines')
492 gCosmetics.add_argument(
'--removeComments', default=
False, action=
'store_true',
493 help=
'Remove comments')
494 gCosmetics.add_argument(
'--updateSpaces', default=
False, action=
'store_true',
495 help=
'Updates spaces around operators, commas, parenthesis and ' +
496 'at the end of line')
497 gCosmetics.add_argument(
'--alignContinuation', default=
False, action=
'store_true',
498 help=
'Align the beginings of continued lines')
499 gCosmetics.add_argument(
'--addBeginContinuation', default=
False, action=
'store_true',
500 help=
'Add missing continuation characters (\'&\') at the ' +
502 gCosmetics.add_argument(
'--removeBeginContinuation', default=
False, action=
'store_true',
503 help=
'Remove continuation characters (\'&\') at the begining of lines')
504 gCosmetics.add_argument(
'--removeALLContinuation', default=
False, action=
'store_true',
505 help=
'Remove all continuation characters(\'&\')')
506 gCosmetics.add_argument(
'--prettify', default=
False, action=
'store_true',
507 help=
'Prettify the source code (indentation, spaces...)')
508 gCosmetics.add_argument(
'--minify', default=
False, action=
'store_true',
509 help=
'Simplify the source code (indentation, spaces...)')
510 gCosmetics.add_argument(
'--removeEmptyCONTAINS', default=
False, action=
'store_true',
511 help=
'Remove useless CONTAINS statements')
512 gCosmetics.add_argument(
'--formatModuleUse', default=
False, action=
'store_true',
513 help=
'Order USE declarations by module type (MODD_, MODE_, ' +
514 'MODI_, MODN_) and alphabetically within each group')
519 Updates an argparse parser with applications arguments
521 gApplications = parser.add_argument_group(
'Options to apply upper level transformation')
522 gApplications.add_argument(
'--deleteDrHook', default=
False, action=
'store_true',
523 help=
'Delete DR HOOK use')
524 gApplications.add_argument(
'--addDrHook', default=
False, action=
'store_true',
526 gApplications.add_argument(
'--deleteBudgetDDH', default=
False, action=
'store_true',
527 help=
'Delete Budget/DDH use')
528 gApplications.add_argument(
'--deleteRoutineCallsMesoNHGPU', default=
False, action=
'store_true',
529 help=
'Delete parts of the code not compatible with MesoNH-OpenACC' +
530 'such as OCND2 blocks')
531 gApplications.add_argument(
'--splitModuleRoutineFile', default=
False, action=
'store_true',
533 gApplications.add_argument(
'--deleteNonColumnCallsPHYEX', default=
False, action=
'store_true',
534 help=
'Delete call to PHYEX routines that needs information on ' +
535 'horizontal points (multiple column dependency')
536 gApplications.add_argument(
'--removeIJDim', default=
False, action=
'store_true',
537 help=
'Remove I and J dimensions (1, KLON). ' +
538 'Needs the --stopScopes argument.')
539 gApplications.add_argument(
'--expandAllArraysPHYEX', default=
False, action=
'store_true',
540 help=
'Expand all array syntax (computing and where block) ' +
541 'using PHYEX conventions')
542 gApplications.add_argument(
'--expandAllArraysPHYEXConcurrent', default=
False,
544 help=
'Expand all array syntax with DO CONCURRENT loops ' +
545 '(computing and where block) using PHYEX conventions')
546 gApplications.add_argument(
'--expandAllArrays', default=
False, action=
'store_true',
547 help=
'Expand all array syntax (computing and where block) ' +
548 'using mnh directives if present')
549 gApplications.add_argument(
'--expandAllArraysConcurrent', default=
False, action=
'store_true',
550 help=
'Expand all array syntax with DO CONCURRENT loops ' +
551 '(computing and where block) using mnh directives if present')
552 gApplications.add_argument(
'--inlineContainedSubroutinesPHYEX', default=
False,
554 help=
'Inline containted subroutines in main routine, using ' +
556 gApplications.add_argument(
'--addStack', metavar=
'MODEL', type=str,
557 help=
'Add local arrays to the stack. The argument is the ' +
558 'the model name in which stack must be added ("AROME" ' +
559 'or "MESONH"). Needs the --stopScopes argument for AROME.')
560 gApplications.add_argument(
'--addIncludes', default=
False, action=
'store_true',
561 help=
'Add .h includes in the file and remove the INCLUDE statement')
562 gApplications.add_argument(
'--addSubmodulePHYEX', default=
False, action=
'store_true',
563 help=
'Add SUBMODULE and INTERFACE of subroutines in PHYEX')
564 gApplications.add_argument(
'--mnhExpand', default=
False, action=
'store_true',
565 help=
'Apply the mnh_expand directives with DO loops')
566 gApplications.add_argument(
'--mnhExpandConcurrent', default=
False, action=
'store_true',
567 help=
'Apply the mnh_expand directives with DO CONCURRENT loops')
568 gApplications.add_argument(
'--addMPPDB_CHECKS', default=
False, action=
'store_true',
569 help=
'Add MPPDB_CHEKS bit-repro checking routines of MesoNH for ' +
570 'all in and inout arrays in subroutines')
571 gApplications.add_argument(
'--addPrints', default=
False, action=
'store_true',
572 help=
'Add Prints of min/maxval and shape of all in, out, inout ' +
573 'arguments of all scopes')
574 gApplications.add_argument(
'--shumanFUNCtoCALL', default=
False, action=
'store_true',
575 help=
'Transform shuman functions to call statements ' +
577 gApplications.add_argument(
'--shugradTransfo', default=
False, action=
'store_true',
578 help=
'Transform shuman functions to call statements')
579 gApplications.add_argument(
'--mathFunctoBRFunc', default=
False, action=
'store_true',
580 help=
'Convert intrinsic math functions **, LOG, ATAN, **2, **3, ' +
581 '**4, EXP, COS, SIN, ATAN2 into a self defined function BR_ ' +
582 'for MesoNH bit-repro.')
583 gApplications.add_argument(
'--convertTypesInCompute', default=
False, action=
'store_true',
584 help=
'Use single variable instead of variable contained in ' +
585 'structure in compute statement for optimization issue ')
586 gApplications.add_argument(
'--buildModi', default=
False, action=
'store_true',
587 help=
'Builds the corresponding modi_ file')
588 gApplications.add_argument(
'--removeExtraDOinMnhDoConcurrent', default=
False,
590 help=
'Remove DO and ENDDO instructions inside !$mnh_do_concurrent')
591 gApplications.add_argument(
'--convertuseModuleToIncludes', default=
False,
593 help=
'Convert USE MODULE, ONLY: ROUTINE to #include routine.intfb.h')
598 Updates an argparse parser with openACC arguments
600 gOpenACC = parser.add_argument_group(
'OpenACC')
601 gOpenACC.add_argument(
'--addACCData', default=
False, action=
'store_true',
602 help=
'Add !$acc data present and !$acc end data directives')
603 gOpenACC.add_argument(
'--addACCRoutineSeq', default=
False, action=
'store_true',
604 help=
'Add "!$acc routine seq" to routines under stopScopes')
605 gOpenACC.add_argument(
'--craybyPassDOCONCURRENT', default=
False, action=
'store_true',
606 help=
'remove acc loop independant collapse for BR_ fonctions and ' +
607 'mnh_undef(OPENACC) macro' +
608 ' use DO CONCURRENT with mnh_undef(LOOP)')
609 gOpenACC.add_argument(
'--removeACC', default=
False, action=
'store_true',
610 help=
'remove all ACC directives')
611 gOpenACC.add_argument(
'--removebyPassDOCONCURRENT', default=
False, action=
'store_true',
612 help=
'remove macro !$mnh_(un)def(OPENACC) and !$mnh_(un)def(LOOP) ' +
614 gOpenACC.add_argument(
'--buildACCTypeHelpers', default=
False, action=
'store_true',
615 help=
'build module files containing helpers to copy user ' +
617 gOpenACC.add_argument(
'--allocatetoHIP', default=
False, action=
'store_true',
618 help=
'convert (DE)ALLOCATE to (DE)ALLOCATE_HIP on variables only sent ' +
619 'to the GPU via acc enter data copyin/create (for GPU AMD MI250X)')
624 Updates an argparse parser with checks arguments
627 gChecks = parser.add_argument_group(
'Check options')
628 gChecks.add_argument(
'--checkIMPLICIT', choices={
'Warn',
'Err'}, default=
None,
629 help=
'Send a warning or raise an error if the "IMPLICIT NONE" ' +
631 gChecks.add_argument(
'--checkINTENT', choices={
'Warn',
'Err'}, default=
None,
632 help=
'Send a warning or raise an error if the "INTENT" ' +
633 'attribute is missing for a dummy argument')
634 gChecks.add_argument(
'--checkOpInCall', choices={
'Warn',
'Err'}, default=
None,
635 help=
'Send a warning or raise an error if a call argument is an '
637 gChecks.add_argument(
'--checkUnusedLocalVar', choices={
'Warn',
'Err'}, default=
None,
638 help=
'Send a warning or raise an error if some local '
639 'variables are unused.')
640 gChecks.add_argument(
'--checkPHYEXUnusedLocalVar', choices={
'Warn',
'Err'}, default=
None,
641 help=
'Send a warning or raise an error if some local '
642 'variables are unused (excluding variables needed '
643 'for mnh_expand directives).')
644 gChecks.add_argument(
'--checkEmptyParensInCall', choices={
'Warn',
'Err'}, default=
None,
645 help=
'Send a warning or raise an error if a call argument is an '
646 'array with empty parens.')
647 gChecks.add_argument(
'--checkContinuationInArraySection', choices={
'Warn',
'Err'},
649 help=
'Send a warning or raise an error if an array index '
650 'selection (containing a \':\' or applied to a variable '
651 'declared as an array) is split over several lines.')
652 gChecks.add_argument(
'--checkLineLength', choices={
'Warn',
'Err'}, default=
None,
653 help=
'Send a warning or raise an error if a line is longer than '
654 'the limit given by --maxLineLength.')
655 gChecks.add_argument(
'--maxLineLength', type=int, default=132,
656 help=
'Maximum line length used by --checkLineLength and '
657 '--splitLongLines (default: 132).')
658 gChecks.add_argument(
'--checkONLY', choices={
'Warn',
'Err'}, default=
None,
659 help=
'Send a warning or raise an error if a USE statement is not '
660 'followed by an ONLY clause.')
661 gChecks.add_argument(
'--checkKeyDim', choices={
'Warn',
'Err'}, default=
None,
662 help=
'Send a warning or raise an error if MERGE-based array '
663 'dimensions are inconsistent across scopes.')
664 gChecks.add_argument(
'--checkTopLevelUSE', choices={
'Warn',
'Err'}, default=
None,
665 help=
'Send a warning or raise an error if a USE statement in '
666 'top-level module is present')
671 Updates an argparse parser with statements arguments
674 gStatement = parser.add_argument_group(
'Statements options')
675 gStatement.add_argument(
'--removeCall', action=
'append',
676 help=
"Call to remove from the source code. The argument " +
677 "is the subprogram name")
678 gStatement.add_argument(
'--removePrints', default=
False, action=
'store_true',
679 help=
"Remove print statements from the source code.")
680 gStatement.add_argument(
'--inlineContainedSubroutines', default=
False, action=
'store_true',
681 help=
'Inline containted subroutines in main routine')
682 gStatement.add_argument(
'--setFalseIfStmt', default=
None,
683 help=
'Replace this value by .FALSE. in if statements')
684 gStatement.add_argument(
'--checkEmptyParensInMnhExpand', choices={
'Warn',
'Err'}, default=
None,
685 help=
'Send a warning or raise an error if an empty parens ' +
686 'is found in an mnh_expand directive.')
691 Updates an argparse parser with misc arguments
693 gMisc = parser.add_argument_group(
'Miscellaneous')
694 gMisc.add_argument(
'--showScopes', default=
False, action=
'store_true',
695 help=
'Show the different scopes found in the source code')
696 gMisc.add_argument(
'--empty', default=
False, action=
'store_true',
697 help=
'Empty the different scopes')
702 Updates an argparse parser with statements arguments
703 :param withPlotCentralFile: to add the --plotCentralFile argumen
704 :param treeIsOptional: is the --tree argument optional?
706 gTree = parser.add_argument_group(
'Tree')
707 gTree.add_argument(
'--tree', default=
None, action=
'append', required=
not treeIsOptional,
708 help=
'Directories where source code must be searched for')
709 gTree.add_argument(
'--descTree', default=
None, required=
not treeIsOptional,
710 help=
'File to write and/or read the description of the tree.')
711 if withPlotCentralFile:
712 gTree.add_argument(
'--plotCentralFile', default=
None, type=str,
713 help=
'Central file of the plot')
714 gTree.add_argument(
'--plotCompilTree', default=
None,
715 help=
'File name for compilation dependency graph (.dot or image extension)')
716 gTree.add_argument(
'--plotExecTree', default=
None,
717 help=
'File name for execution dependency graph (.dot or image extension)')
718 gTree.add_argument(
'--plotMaxUpper', default=
None, type=int,
719 help=
'Maximum number of upper elements in the plot tree')
720 gTree.add_argument(
'--plotMaxLower', default=
None, type=int,
721 help=
'Maximum number of lower elements in the plot tree')
722 gTree.add_argument(
'--stopScopes', default=
None, type=str,
723 help=
'#-separated list of scopes ' +
724 'where the recursive inclusion of an argument variable ' +
725 'must stop (needed for some transformations).')
730 Updates an argparse parser with statements arguments
732 gCpp = parser.add_argument_group(
'Preprocessor')
733 gCpp.add_argument(
'--applyCPPifdef', nargs=
'*', action=
'append',
734 help=
"This option is followed by the list of defined or undefined " +
736 "All #ifdef and #ifndef concerning these keys are evaluated. " +
737 "Undefined keys are preceded by a percentage sign.")
742 Apply transformation on a PYFT instance
743 :param pft: PYFT instance
744 :param arg: argument to deal with
745 :param args: parsed argparsed arguments
746 :param plotCentralFile: central file for plots
748 simplify = {
'simplify': args.simplify}
750 stopScopes = args.stopScopes.split(
'#')
if args.stopScopes
is not None else None
785 Apply file name transformations on a PYFT instance
786 :param pft: PYFT instance
787 :param arg: argument to deal with
788 :param args: parsed argparsed arguments
792 if arg ==
'--renamefF':
794 elif arg ==
'--renameFf':
800 Apply variables transformations on a PYFT instance
801 :param pft: PYFT instance
802 :param arg: argument to deal with
803 :param args: parsed argparsed arguments
804 :param simplify: kwargs to simplify
805 :param parserOptions: fxtran parser options
806 :param stopScopes: upper limit in call tree for some transformations
808 if arg ==
'--showVariables':
809 pft.varList.showVarList()
810 elif arg ==
'--attachArraySpecToEntity':
811 pft.attachArraySpecToEntity()
812 elif arg ==
'--removeVariable':
813 for scopePath, varName
in args.removeVariable:
814 pft.getScopeNode(pft.normalizeScope(scopePath)).removeVar([varName], **simplify)
815 elif arg ==
'--addVariable':
816 for scopePath, varName, declStmt, pos
in args.addVariable:
817 pft.getScopeNode(pft.normalizeScope(scopePath)).addVar(
818 [[varName, declStmt, (int(pos)
if isint(pos)
else None)]])
819 elif arg ==
'--addModuleVariable':
820 for scopePath, moduleName, varName
in args.addModuleVariable:
821 pft.getScopeNode(pft.normalizeScope(scopePath)).addModuleVar([[moduleName, varName]])
822 elif arg ==
'--showUnusedVariables':
824 elif arg ==
'--removeUnusedLocalVariables':
825 pft.removeUnusedLocalVar(
826 [item.strip()
for item
in args.removeUnusedLocalVariables.split(
',')]
827 if args.removeUnusedLocalVariables !=
'NONE' else None, **simplify)
828 elif arg ==
'--removePHYEXUnusedLocalVariables':
829 pft.removePHYEXUnusedLocalVar(
830 [item.strip()
for item
in args.removePHYEXUnusedLocalVariables.split(
',')]
831 if args.removePHYEXUnusedLocalVariables !=
'NONE' else None, **simplify)
832 elif arg ==
'--addExplicitArrayBounds':
833 pft.addExplicitArrayBounds()
834 elif arg ==
'--addArrayParentheses':
835 pft.addArrayParentheses()
836 elif arg ==
'--modifyAutomaticArrays':
837 pft.modifyAutomaticArrays(*(args.modifyAutomaticArrays.split(
'#')))
838 elif arg ==
'--replaceAutomaticWithAllocatable':
839 pft.modifyAutomaticArrays(
840 "{type}, DIMENSION({doubledotshape}), ALLOCATABLE :: {name}",
841 "ALLOCATE({name}({shape}))",
"DEALLOCATE({name})")
842 elif arg ==
'--addArgInTree':
843 for varName, declStmt, pos
in args.addArgInTree:
844 pft.addArgInTree(varName, declStmt, int(pos), stopScopes,
845 parserOptions=parserOptions,
847 elif arg ==
'--addONLY':
848 pft.addONLY(parserOptions=parserOptions, wrapH=args.wrapH)
853 Apply applications transformations on a PYFT instance
854 :param pft: PYFT instance
855 :param arg: argument to deal with
856 :param args: parsed argparsed arguments
857 :param simplify: kwargs to simplify
858 :param parserOptions: fxtran parser options
859 :param stopScopes: upper limit in call tree for some transformations
861 if arg ==
'--addStack':
862 pft.addStack(args.addStack, stopScopes,
863 parserOptions=parserOptions,
865 elif arg ==
'--deleteDrHook':
866 pft.deleteDrHook(**simplify)
867 elif arg ==
'--addDrHook':
869 elif arg ==
'--deleteBudgetDDH':
870 pft.deleteBudgetDDH(**simplify)
871 elif arg ==
'--deleteRoutineCallsMesoNHGPU':
872 pft.deleteRoutineCallsMesoNHGPU(**simplify)
873 elif arg ==
'--deleteNonColumnCallsPHYEX':
874 pft.deleteNonColumnCallsPHYEX(**simplify)
875 elif arg ==
'--addMPPDB_CHECKS':
876 pft.addMPPDB_CHECKS()
877 elif arg ==
'--addPrints':
878 pft.addMPPDB_CHECKS(printsMode=
True)
879 elif arg ==
'--addSubmodulePHYEX':
880 pft.addSubmodulePHYEX()
883 assert not (args.mnhExpand
and args.mnhExpandConcurrent), \
884 "Only one of --mnhExpand and --mnhExpandConcurrent"
885 if arg ==
'--mnhExpand':
886 pft.removeArraySyntax(everywhere=
False, addAccIndependentCollapse=
False)
887 elif arg ==
'--mnhExpandConcurrent':
888 pft.removeArraySyntax(concurrent=
True, everywhere=
False)
889 elif arg ==
'--inlineContainedSubroutines':
890 pft.inlineContainedSubroutines(**simplify)
891 elif arg ==
'--inlineContainedSubroutinesPHYEX':
892 pft.inlineContainedSubroutinesPHYEX(**simplify)
893 elif arg ==
'--expandAllArrays':
894 pft.removeArraySyntax()
895 elif arg ==
'--expandAllArraysConcurrent':
896 pft.removeArraySyntax(concurrent=
True)
897 elif arg ==
'--expandAllArraysPHYEX':
898 pft.expandAllArraysPHYEX()
899 elif arg ==
'--expandAllArraysPHYEXConcurrent':
900 pft.expandAllArraysPHYEX(concurrent=
True)
901 elif arg ==
'--removeIJDim':
902 pft.removeIJDim(stopScopes,
903 parserOptions=parserOptions,
904 wrapH=args.wrapH, **simplify)
905 elif arg ==
'--shumanFUNCtoCALL':
906 pft.shumanFUNCtoCALL()
907 elif arg ==
'--shugradTransfo':
909 elif arg ==
'--buildACCTypeHelpers':
910 pft.buildACCTypeHelpers()
911 elif arg ==
'--mathFunctoBRFunc':
912 pft.mathFunctoBRFunc()
913 elif arg ==
'--convertTypesInCompute':
914 pft.convertTypesInCompute()
915 elif arg ==
'--buildModi':
917 elif arg ==
'--splitModuleRoutineFile':
918 pft.splitModuleRoutineFile()
919 elif arg ==
'--removeExtraDOinMnhDoConcurrent':
920 pft.removeExtraDOinMnhDoConcurrent()
921 elif arg ==
'--convertuseModuleToIncludes':
922 pft.convertuseModuleToIncludes()
927 Apply openACC transformations on a PYFT instance
928 :param pft: PYFT instance
929 :param arg: argument to deal with
930 :param args: parsed argparsed arguments
931 :param stopScopes: upper limit in call tree for some transformations
933 if arg ==
'--addACCData':
935 elif arg ==
'--craybyPassDOCONCURRENT':
936 pft.craybyPassDOCONCURRENT()
937 elif arg ==
'--removebyPassDOCONCURRENT':
938 pft.removebyPassDOCONCURRENT()
939 elif arg ==
'--addACCRoutineSeq':
940 pft.addACCRoutineSeq(stopScopes)
941 elif arg ==
'--removeACC':
943 elif arg ==
'--allocatetoHIP':
949 Apply cosmetics transformations on a PYFT instance
950 :param pft: PYFT instance
951 :param arg: argument to deal with
952 :param args: parsed argparsed arguments
954 if arg ==
'--splitLongLines':
955 pft.splitLongLines(args.maxLineLength)
956 elif arg ==
'--upperCase':
958 elif arg ==
'--lowerCase':
960 elif arg ==
'--changeIfStatementsInIfConstructs':
961 pft.changeIfStatementsInIfConstructs()
962 elif arg ==
'--indent':
964 elif arg ==
'--removeIndent':
965 pft.indent(indentProgramunit=0, indentBranch=0)
966 elif arg ==
'--removeEmptyLines':
967 pft.removeEmptyLines()
968 elif arg ==
'--removeComments':
970 elif arg ==
'--updateSpaces':
972 elif arg
in ARG_UPDATE_CNT:
973 pft.updateContinuation(align=args.alignContinuation,
974 addBegin=args.addBeginContinuation,
975 removeBegin=args.removeBeginContinuation,
976 removeALL=args.removeALLContinuation)
977 elif arg ==
'--prettify':
980 pft.removeEmptyLines()
982 pft.updateContinuation()
983 elif arg ==
'--minify':
984 pft.indent(indentProgramunit=0, indentBranch=0)
987 pft.removeEmptyLines()
989 pft.updateContinuation(align=
False, removeALL=
True, addBegin=
False)
990 elif arg ==
'--removeEmptyCONTAINS':
991 pft.removeEmptyCONTAINS()
992 elif arg ==
'--formatModuleUse':
993 pft.formatModuleUse()
998 Apply checks transformations on a PYFT instance
999 :param pft: PYFT instance
1000 :param arg: argument to deal with
1001 :param args: parsed argparsed arguments
1002 :param stopScopes: list of scope paths or None
1004 if arg ==
'--checkIMPLICIT':
1005 pft.checkImplicitNone(args.checkIMPLICIT ==
'Err')
1006 elif arg ==
'--checkINTENT':
1007 pft.checkIntent(args.checkINTENT ==
'Err')
1008 elif arg ==
'--checkOpInCall':
1009 pft.checkOpInCall(args.checkOpInCall ==
'Err')
1010 elif arg ==
'--checkUnusedLocalVar':
1011 pft.checkUnusedLocalVar(args.checkUnusedLocalVar ==
'Err')
1012 elif arg ==
'--checkPHYEXUnusedLocalVar':
1013 pft.checkPHYEXUnusedLocalVar(args.checkPHYEXUnusedLocalVar ==
'Err')
1014 elif arg ==
'--checkEmptyParensInCall':
1015 pft.checkEmptyParensInCall(args.checkEmptyParensInCall ==
'Err')
1016 elif arg ==
'--checkContinuationInArraySection':
1017 pft.checkContinuationInArraySection(args.checkContinuationInArraySection ==
'Err')
1018 elif arg ==
'--checkLineLength':
1019 pft.checkLineLength(args.maxLineLength, args.checkLineLength ==
'Err')
1020 elif arg ==
'--checkONLY':
1021 pft.checkONLY(args.checkONLY ==
'Err')
1022 elif arg ==
'--checkKeyDim':
1023 pft.checkKeyDimConsistency(args.checkKeyDim ==
'Err',
1024 stopScopes=stopScopes)
1025 elif arg ==
'--checkTopLevelUSE':
1026 pft.checkTopLevelUSE(args.checkTopLevelUSE ==
'Err')
1027 elif arg ==
'--checkEmptyParensInMnhExpand':
1028 pft.checkEmptyParensInMnhExpand(args.checkEmptyParensInMnhExpand ==
'Err')
1033 Apply statements transformations on a PYFT instance
1034 :param pft: PYFT instance
1035 :param arg: argument to deal with
1036 :param args: parsed argparsed arguments
1037 :param simplify: kwargs to simplify
1039 if arg ==
'--removeCall':
1040 for rc
in args.removeCall:
1041 pft.removeCall(rc, **simplify)
1042 elif arg ==
'--removePrints':
1043 pft.removePrints(**simplify)
1044 elif arg ==
'--setFalseIfStmt':
1045 pft.setFalseIfStmt(args.setFalseIfStmt, **simplify)
1050 Apply misc transformations on a PYFT instance
1051 :param pft: PYFT instance
1052 :param arg: argument to deal with
1053 :param args: parsed argparsed arguments
1054 :param simplify: kwargs to simplify
1056 if arg ==
'--showScopes':
1057 pft.showScopesList()
1058 elif arg ==
'--empty':
1059 pft.empty(**simplify)
1064 Apply tree transformations on a PYFT instance
1065 :param pft: PYFT instance
1066 :param arg: argument to deal with
1067 :param args: parsed argparsed arguments
1068 :param plotCentralFile: central file for plots
1070 if arg ==
'--plotCompilTree' and plotCentralFile
is not None:
1071 pft.tree.plotCompilTreeFromFile(plotCentralFile, args.plotCompilTree,
1072 args.plotMaxUpper, args.plotMaxLower)
1073 elif arg ==
'--plotExecTree' and plotCentralFile
is not None:
1074 pft.tree.plotExecTreeFromFile(plotCentralFile, args.plotExecTree,
1075 args.plotMaxUpper, args.plotMaxLower)
1080 Apply preprocessor transformations on a PYFT instance
1081 :param pft: PYFT instance
1082 :param arg: argument to deal with
1083 :param args: parsed argparsed arguments
1085 if arg ==
'--applyCPPifdef':
1086 pft.applyCPPifdef([k
for aList
in args.applyCPPifdef
for k
in aList])
1089if __name__ ==
"__main__":