26 Remove macro !$mnh_(un)def(OPENACC) and !$mnh_(un)def(LOOP) directives
27 for other compiler than Cray
30 self.removeComments(exclDirectives=[],
31 pattern=re.compile(
r'^\!\$mnh_undef\(LOOP\)'))
32 self.removeComments(exclDirectives=[],
33 pattern=re.compile(
r'^\!\$mnh_undef\(OPENACC\)'))
34 self.removeComments(exclDirectives=[],
35 pattern=re.compile(
r'^\!\$mnh_define\(LOOP\)'))
36 self.removeComments(exclDirectives=[],
37 pattern=re.compile(
r'^\!\$mnh_define\(OPENACC\)'))
42 By pass a bug of the CRAY compiler in which the vectorisation is not done with
43 BR_ fonctions use or locally.
44 On all expanded compute kernels with !$acc loop independent collapse(X) placed:
45 - if BR_ fonction is used : !$acc loop independent collapse(X) is removed and
46 the nested DO loops are factorised into DO CONCURRENT
47 - if a mnh_undef(OPENACC) macro is in place, !$acc loop collapse independant(X)
49 - if a mnh_undef(LOOP) macro is in place the nested DO loops are factorised into
52 def checkPresenceofBR(node):
53 """Return True if a BR_ (math BIT-REPRODUCTIBILITY) function is present in the node"""
54 mathBRList = [
'ALOG',
'LOG',
'EXP',
'COS',
'SIN',
'ASIN',
'ATAN',
'ATAN2',
56 namedE = node.findall(
'.//{*}named-E/{*}N/{*}n')
58 if alltext(el)
in [
'BR_' + e
for e
in mathBRList]:
62 def getStatementsInDoConstruct(node, savelist):
64 if 'do-construct' in tag(sNode):
65 getStatementsInDoConstruct(sNode, savelist)
66 elif 'do-stmt' not in tag(sNode):
67 savelist.append(sNode)
70 useAccLoopIndependent =
True
72 comments = self.findall(
'.//{*}C')
74 for comment
in comments:
75 if comment.text.startswith(
'!$mnh_undef(LOOP)'):
76 useNestedLoops =
False
77 if comment.text.startswith(
'!$mnh_undef(OPENACC)'):
78 useAccLoopIndependent =
False
80 if comment.text.startswith(
'!$mnh_define(LOOP)'):
82 if comment.text.startswith(
'!$mnh_define(OPENACC)'):
83 useAccLoopIndependent =
True
85 if comment.text.startswith(
'!$acc loop independent collapse('):
87 par = self.getParent(comment)
88 ind = list(par).index(comment)
89 nestedLoop = par[ind + 1]
91 getStatementsInDoConstruct(nestedLoop, statements)
95 for stmt
in statements:
96 if checkPresenceofBR(stmt):
102 if not useAccLoopIndependent
or isBRPresent:
103 toremove.append((self, comment))
106 if not useNestedLoops
or isBRPresent:
108 doStmt = nestedLoop.findall(
'.//{*}do-stmt')
110 for do
in reversed(doStmt):
111 table[do.find(
'.//{*}do-V/{*}named-E/{*}N/{*}n').text] = \
112 [alltext(do.find(
'.//{*}lower-bound')),
113 alltext(do.find(
'.//{*}upper-bound'))]
116 inner, outer, _ = self.createDoConstruct(table,
117 indent=len(nestedLoop.tail),
121 for stmt
in statements:
122 inner.insert(-1, stmt)
125 par.insert(ind, outer)
126 toremove.append((par, nestedLoop))
129 for parent, elem
in toremove:
135 Convert (DE)ALLOCATE to (DE)ALLOCATE_HIP on variables only sent to the GPU via
136 !$acc enter data copyin
138 !$acc enter data create
139 This is necessary for using the managed memory with GPU AMD MI250X (on Adastra)
141 scopes = self.getScopes()
144 comments = scope.findall(
'.//{*}C')
145 pointers = scope.findall(
'.//{*}pointer-a-stmt')
147 for coms
in comments:
148 if (
'!$acc enter data' in coms.text
or '!$acc exit data' in coms.text) \
149 and coms.text.count(
'!') == 1:
150 if coms.text.count(
'(') == 1:
152 varsToChange.extend(coms.text.split(
')')[0].split(
'(')[1:][0].split(
','))
155 variableName = re.search(
r'\(.*\)', coms.text).group(0)[1:-1]
156 varsToChange.insert(0, variableName)
158 if len(pointers) > 0:
159 for i, var
in enumerate(varsToChange):
160 for pointer
in pointers:
161 if alltext(pointer.find(
'.//{*}E-1/{*}named-E')) == var:
162 varsToChange[i] = alltext(pointer.find(
'.//{*}E-2/{*}named-E'))
164 for i, var
in enumerate(varsToChange):
165 varsToChange[i] = var.replace(
' ',
'')
167 if len(varsToChange) > 0:
168 scope.addModuleVar([(scope.path,
'MODE_MNH_HIPFORT',
None)])
170 allocateStmts = scope.findall(
'.//{*}allocate-stmt')
171 allocateStmts.extend(scope.findall(
'.//{*}deallocate-stmt'))
173 for stmt
in allocateStmts:
175 allocateArg = stmt.find(
'.//{*}arg-spec')
176 arrayR = allocateArg.find(
'.//{*}array-R')
179 parensR = allocateArg.findall(
'.//{*}parens-R')
180 if len(parensR) == 2:
182 varsChecking = alltext(allocateArg).split(
'%')[0]+
'%' + \
183 alltext(allocateArg).split(
'%')[1].split(
'(')[0]
184 elif len(parensR) == 1:
186 if allocateArg.find(
'.//{*}component-R'):
187 varsChecking = alltext(allocateArg)
189 varsChecking = alltext(allocateArg).split(
'(')[0]
190 elif len(parensR) == 0:
191 varsChecking = alltext(allocateArg).split(
'(')[0]
193 varsChecking = alltext(allocateArg).split(
'(')[0]
195 if varsChecking
in varsToChange:
196 removeLastComma =
True
197 stmt.text =
"CALL MNH_HIP" + stmt.text
198 if tag(stmt) ==
'allocate-stmt':
200 lowerBounds = allocateArg.findall(
'.//{*}lower-bound')
201 if lowerBounds
is not None:
202 for bounds
in lowerBounds:
204 arrayR = allocateArg.find(
'.//{*}array-R')
207 parensR = allocateArg.findall(
'.//{*}parens-R')
209 parensR[-1].text =
','
211 if allocateArg.find(
'.//{*}component-R'):
212 removeLastComma =
False
214 parensR[0].text =
','
218 allocateArg.tail =
''
223 1) Add after declaration:
224 !$acc data present ( list of intent arrays)
225 2) Add at the end of the routine
228 scopes = self.getScopes()
229 if scopes[0].path.split(
'/')[-1].split(
':')[1][:4] ==
'MODD':
236 if 'sub:' in scope.path
and 'func' not in scope.path
and 'interface' not in scope.path:
239 for var
in scope.varList:
241 if var[
'arg']
and var[
'as']
and 'TYPE' not in var[
't']
and \
242 var[
'scopePath'] == scope.path:
243 arraysIntent.append(var[
'n'])
245 if len(arraysIntent) == 0:
249 listVar =
"!$acc data present ( "
251 for var
in arraysIntent:
253 listVar = listVar +
'\n!$acc & '
255 listVar = listVar + var +
", &"
257 listVarEnd = listVar[:-3]
258 accAddMultipleLines = createExpr(listVarEnd +
')')
259 idx = scope.insertStatement(scope.indent(accAddMultipleLines[0]), first=
True)
262 for iLine, line
in enumerate(accAddMultipleLines[1:]):
263 scope.insert(idx + 1 + iLine, line)
266 comment = createElem(
'C', text=
'!$acc end data', tail=
'\n')
267 scope.insertStatement(scope.indent(comment), first=
False)