Skip to content

Commit

Permalink
Option --restrictScope added
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienRietteMTO committed Oct 15, 2024
1 parent cf6bf85 commit bba54a4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 38 deletions.
2 changes: 1 addition & 1 deletion bin/pyft_parallel_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def init(cls):
epilog="The argument order matters.")

updateParser(parser, withInput=False, withOutput=False, withXml=False, withPlotCentralFile=True,
treeIsOptional=False, nbPar=True)
treeIsOptional=False, nbPar=True, restrictScope=False)
commonArgs, getFileArgs = getArgs(parser)

# Manager to share the Tree instance
Expand Down
10 changes: 6 additions & 4 deletions bin/pyft_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
epilog="The argument order matters.")

updateParser(parser, withInput=True, withOutput=True, withXml=True, withPlotCentralFile=False,
treeIsOptional=True, nbPar=False)
treeIsOptional=True, nbPar=False, restrictScope=True)
args, orderedOptions = getArgs(parser)[1]()

parserOptions = getParserOptions(args)
Expand All @@ -28,6 +28,8 @@
pft = PYFT(args.INPUT, args.OUTPUT, parser=args.parser, parserOptions=parserOptions,
verbosity=args.logLevel, wrapH=args.wrapH, tree=descTree,
enableCache=args.enableCache)
if args.restrictScope != '':
pft = pft.getScopeNode(args.restrictScope)

# apply the transformation in the order they were specified
for arg in orderedOptions:
Expand All @@ -39,12 +41,12 @@
if descTree is not None:
descTree.toJson(args.descTree)
if args.xml is not None:
pft.writeXML(args.xml)
pft.mainScope.writeXML(args.xml)
if not args.dryRun:
pft.write()
pft.mainScope.write()

# Closing
pft.close()
pft.mainScope.close()

except: # noqa E722
# 'exept' everything and re-raise error systematically
Expand Down
43 changes: 26 additions & 17 deletions doc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ applicable line is used. A line can take one of these two forms:
* "OPTIONS": if the line doesn\'t contain the FILE\_DESCRIPTOR part, it applies to
all source code.

**--restrictScope SCOPEPATH** Limit the action to this scope path (as described in
[Concepts](#concepts)).

### Dealing with variables

**\--showVariables** displays a list of all the declared variables
Expand Down Expand Up @@ -130,15 +133,11 @@ the third one is the variable name.
**\--attachArraySpecToEntity** move the array declaration attributes to the right
part of the declaration statement (e.g. "REAL, DIMENSION(5) :: X" becomes "REAL :: X(5)")

**\--showUnusedVariables [SCOPEPATH]** lists the unused varibales. Without argument all the
unused variables are shown. If one argument is given it is the scope path (as described
in [Concepts](#concepts)) where unused variables are searched for.
**\--showUnusedVariables** lists the unused varibales.

**\--removeUnusedLocalVariables SCOPEPATH EXCLUDE** remove unused local variables. Without argument all the
unused variables are suppressed. If one argument is given it is the scope path (as described
in [Concepts](#concepts)) where unused variables are searched for.
**\--removeUnusedLocalVariables EXCLUDE** remove unused local variables.

**\--removePHYEXUnusedLocalVariables SCOPEPATH EXCLUDE** variation aroud the \--removeUnusedLocalVariables
**\--removePHYEXUnusedLocalVariables EXCLUDE** variation aroud the \--removeUnusedLocalVariables
to deal with the variables declared in the mnh\_expand directives.

**\--addExplicitArrayBounds** Adds explicit bounds to arrays that already have parentheses.
Expand All @@ -156,11 +155,12 @@ replaces automatic arrays by allocatables.

**\--replaceAutomaticWithAllocatable** replace all automatic arrays with allocatables

**\--addArgInTree** Add an argument variable recursively. First argument is the scope (as
described in [Concepts](#concepts), the second is the variable name, the third is the
declarative statement to insert, the fourth is the position (python indexing) the new
variable will have in the calling statment of the routine. The recursive inclusion of
the argument variable stop at the scopes defined by the --stopScopes option.
**\--addArgInTree** Add an argument variable recursively begining at the scope path
defined with the --restrictScope option. The first argument is the variable name,
the second one is the declarative statement to insert, the third one fourth is the position
(python indexing) the new variable will have in the calling statment of the routine.
The recursive inclusion of the argument variable stops at the scopes defined by the
--stopScopes option.

### Cosmetics

Expand Down Expand Up @@ -214,9 +214,7 @@ if option is 'Warn'; otherwise issue an error message and raise an exception.

### Dealing with statements

**\--removeCall SCOPEPATH CALLNAME** removes call statements. The first argument describes from where the
call statements must be suppressed (it is a scope path as described in [Concepts](#concepts)).
The second argument is the subprogram name.
**\--removeCall CALLNAME** removes call statements. The argument is the subprogram name.

**\--removePrints** removes print statements. The argument describes from where the
call statements must be suppressed (it is a scope path as described in [Concepts](#concepts)).
Expand All @@ -239,6 +237,9 @@ call statements must be suppressed (it is a scope path as described in [Concepts

**\--deleteBudgetDDH** delete Budget/DDH use.

**--deleteRoutineCallsMesoNHGPU** Delete parts of the code not compatible with MesoNH-OpenACC
such as OCND2 blocks.

**\--deleteNonColumnCallsPHYEX** delete call to PHYEX routines that needs information on horizontal
points (multiple column dependency).

Expand Down Expand Up @@ -274,17 +275,25 @@ inout arrays in subroutines.
**\--mathFunctoBRFunc** Convert intrinsic math functions **, LOG, ATAN, **2, **3, **4, EXP,
COS, SIN, ATAN2 into a self defined function BR_ for MesoNH bit-repro.

**--convertTypesInCompute** Use single variable instead of variable contained in structure
in compute statement for optimization issue.

### OpenACC

**\--addACC_data** add !$acc data present and !$acc end data directives
**\--addACCData** add !$acc data present and !$acc end data directives

**\--addACC_routine_seq** add "!$acc routine seq" to routines called directly or indirectly
**\--addACCRoutineSeq** add "!$acc routine seq" to routines called directly or indirectly
by scopes defined by the --stopScopes option.

**\--buildACCTypeHelpers** build module files containing helpers to copy user type structures.

**--craybyPassDOCONCURRENT** remove acc loop independant collapse for BR_ fonctions and
mnh\_undef(OPENACC) macro use DO CONCURRENT with mnh\_undef(LOOP)

**\--removeACC** remove ACC directives

**--removebyPassDOCONCURRENT** remove macro !$mnh\_(un)def(OPENACC) and !$mnh\_(un)def(LOOP) directives

### Preprocessor

**\--applyCPPifdef** This option is followed by the list of defined or undefined CPP keys.
Expand Down
19 changes: 19 additions & 0 deletions examples/addArgInTree_after.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!#PYFT transfo: --addArgInTree XX REAL::XX 0 --restrictScope module:ADDARGINTREE/sub:FOO1/sub:FOO3 --stopScopes module:ADDARGINTREE/sub:FOO1/sub:FOO2

MODULE ADDARGINTREE
CONTAINS
SUBROUTINE FOO1()
CALL FOO2()
CONTAINS
SUBROUTINE FOO2(XX)
REAL::XX
CALL FOO3(XX)
CALL FOO4()
END SUBROUTINE FOO2
SUBROUTINE FOO3(XX)
REAL::XX
END SUBROUTINE FOO3
SUBROUTINE FOO4()
END SUBROUTINE FOO4
END SUBROUTINE FOO1
END MODULE ADDARGINTREE
17 changes: 17 additions & 0 deletions examples/addArgInTree_before.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
!#PYFT transfo: --addArgInTree XX REAL::XX 0 --restrictScope module:ADDARGINTREE/sub:FOO1/sub:FOO3 --stopScopes module:ADDARGINTREE/sub:FOO1/sub:FOO2

MODULE ADDARGINTREE
CONTAINS
SUBROUTINE FOO1()
CALL FOO2()
CONTAINS
SUBROUTINE FOO2()
CALL FOO3()
CALL FOO4()
END SUBROUTINE FOO2
SUBROUTINE FOO3()
END SUBROUTINE FOO3
SUBROUTINE FOO4()
END SUBROUTINE FOO4
END SUBROUTINE FOO1
END MODULE ADDARGINTREE
40 changes: 24 additions & 16 deletions src/pyft/scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def getDescTree(args, cls=Tree):


def updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, treeIsOptional,
nbPar):
nbPar, restrictScope):
"""
Updates an argparse parser with arguments common to all the different tools
:param parser: parser in which arguments are added
Expand All @@ -108,6 +108,7 @@ def updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, tr
:param withPlotCentralFile: to add the --plotCentralFile argument
:param treeIsOptional: is the --tree argument optional?
:param nbPar: number of parallel processes
:param restrictScope: can we specify the scope path
"""

# ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
Expand Down Expand Up @@ -145,6 +146,14 @@ def updateParser(parser, withInput, withOutput, withXml, withPlotCentralFile, tr
'2) "OPTIONS" (if the line doesn\'t contain the FILE_DESCRIPTOR ' +
'part, it applies to all source code).')

if restrictScope:
parser.add_argument('--restrictScope', default=None, type=str, metavar='SCOPEPATH',
help="Limit the action to this scope path (SUBROUTINE/FUNCTION/" +
"MODULE/TYPE). It is '/'-separated path with each element " +
"having the form 'module:<name of the module>', " +
"'sub:<name of the subroutine>', " +
"'func:<name of the function>' or 'type:<name of the type>'.")

# Inputs and outputs
updateParserInputsOutputs(parser, withInput, withOutput, withXml)

Expand Down Expand Up @@ -282,13 +291,12 @@ def updateParserVariables(parser):
'will replace automatic arrays by allocatables.')
gVariables.add_argument('--replaceAutomaticWithAllocatable', action='store_true',
help='Replace all automatic arrays with allocatable arrays.')
gVariables.add_argument('--addArgInTree', default=None, action='append', nargs=4,
metavar=('SCOPEPATH', 'VARNAME', 'DECLARATION', 'POSITION'),
help='Add an argument variable. The argument is the scope path (as ' +
'for the --removeVariable option. The second is the variable ' +
'name, the third is the declarative statement to insert, ' +
'the fourth is the position (python indexing) the new ' +
'variable will have in the calling statment of the ' +
gVariables.add_argument('--addArgInTree', default=None, action='append', nargs=3,
metavar=('VARNAME', 'DECLARATION', 'POSITION'),
help='Add an argument variable. The first argument is the variable ' +
'name, the second one is the declarative statement to insert, ' +
'the third one is the position (python indexing) the new ' +
'variable will have in the calling statement of the ' +
'routine. Needs the --stopScopes argument')


Expand Down Expand Up @@ -384,9 +392,6 @@ def updateParserApplications(parser):
'all in and inout arrays in subroutines')
gApplications.add_argument('--shumanFUNCtoCALL', default=False, action='store_true',
help='Transform shuman functions to call statements')
gApplications.add_argument('--buildACCTypeHelpers', default=False, action='store_true',
help='build module files containing helpers to copy user ' +
'type structures')
gApplications.add_argument('--mathFunctoBRFunc', default=False, action='store_true',
help='Convert intrinsic math functions **, LOG, ATAN, **2, **3, ' +
'**4, EXP, COS, SIN, ATAN2 into a self defined function BR_ ' +
Expand Down Expand Up @@ -414,6 +419,9 @@ def updateParserOpenACC(parser):
gOpenACC.add_argument('--removebyPassDOCONCURRENT', default=False, action='store_true',
help='remove macro !$mnh_(un)def(OPENACC) and !$mnh_(un)def(LOOP) ' +
'directives')
gOpenACC.add_argument('--buildACCTypeHelpers', default=False, action='store_true',
help='build module files containing helpers to copy user ' +
'type structures')


def updateParserChecks(parser):
Expand Down Expand Up @@ -601,11 +609,11 @@ def applyTransfoVariables(pft, arg, args, simplify, parserOptions, stopScopes):
"{type}, DIMENSION({doubledotshape}), ALLOCATABLE :: {name}",
"ALLOCATE({name}({shape}))", "DEALLOCATE({name})")
elif arg == '--addArgInTree':
for scopePath, varName, declStmt, pos in args.addArgInTree:
pft.getScopeNode(scopePath).addArgInTree(varName, declStmt, int(pos), stopScopes,
parser=args.parser,
parserOptions=parserOptions,
wrapH=args.wrapH)
for varName, declStmt, pos in args.addArgInTree:
pft.addArgInTree(varName, declStmt, int(pos), stopScopes,
parser=args.parser,
parserOptions=parserOptions,
wrapH=args.wrapH)


def applyTransfoApplications(pft, arg, args, simplify, parserOptions, stopScopes):
Expand Down

0 comments on commit bba54a4

Please sign in to comment.