Skip to content

Commit

Permalink
Merge pull request #536 from climbfuji/feature/dummy_allocate_inactive
Browse files Browse the repository at this point in the history
Dummy-allocate inactive local variables
  • Loading branch information
grantfirl authored Mar 5, 2024
2 parents 219f2e9 + d837479 commit b945b07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scripts/mkcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def actions(self):
def actions(self, values):
if isinstance(values, dict):
for key in values.keys():
if key in ['in', 'out'] and isstring(values[key]):
if key in ['in', 'out', 'in_inactive', 'out_inactive'] and isstring(values[key]):
self._actions[key] = values[key]
else:
raise Exception('Invalid values for variable attribute actions.')
Expand Down
18 changes: 14 additions & 4 deletions scripts/mkstatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,8 @@ def write(self, metadata_request, metadata_define, arguments, debug):
tmpvar = tmpvars[local_vars[var_standard_name]['name']]
actions_in = tmpvar.actions['in']
actions_out = tmpvar.actions['out']
actions_in_inactive = tmpvar.actions['in_inactive']
actions_out_inactive = tmpvar.actions['out_inactive']
else:
# Add a local variable (tmpvar) for this variable
tmpvar_cnt += 1
Expand Down Expand Up @@ -1388,6 +1390,9 @@ def write(self, metadata_request, metadata_define, arguments, debug):
var_defs_manual.append('integer :: ib, nb')

# Define actions before. Always copy data in, independent of intent.
dims_inactive = ','.join('0' for dim in tmpvar.dimensions)
actions_in_inactive = ''' ! Dummy-allocate local variable to length 0
allocate({tmpvar}({dims_inactive}))'''.format(tmpvar=tmpvar.local_name, dims_inactive=dims_inactive)
actions_in = ''' ! Allocate local variable to copy blocked data {var} into a contiguous array
allocate({tmpvar}({dims}))
ib = 1
Expand Down Expand Up @@ -1421,9 +1426,10 @@ def write(self, metadata_request, metadata_define, arguments, debug):
else:
actions_out = ''' deallocate({tmpvar})
'''.format(tmpvar=tmpvar.local_name)

actions_out_inactive = ''' deallocate({tmpvar})
'''.format(tmpvar=tmpvar.local_name)
# Set/update actions for this temporary variable
tmpvar.actions = {'in' : actions_in, 'out' : actions_out}
tmpvar.actions = {'in' : actions_in, 'out' : actions_out, 'in_inactive' : actions_in_inactive, 'out_inactive' : actions_out_inactive}
tmpvars[local_vars[var_standard_name]['name']] = tmpvar

# Add unit conversions, if necessary
Expand All @@ -1447,16 +1453,20 @@ def write(self, metadata_request, metadata_define, arguments, debug):
actions_before += '''
if ({conditional}) then
{actions_in}
else
{actions_in_inactive}
end if
'''.format(conditional=conditionals[var_standard_name],
actions_in=actions_in.rstrip('\n'))
actions_in=actions_in.rstrip('\n'), actions_in_inactive=actions_in_inactive.rstrip('\n'))
# Add the conditionals for the "after" operations
actions_after += '''
if ({conditional}) then
{actions_out}
else
{actions_out_inactive}
end if
'''.format(conditional=conditionals[var_standard_name],
actions_out=actions_out.rstrip('\n'))
actions_out=actions_out.rstrip('\n'), actions_out_inactive=actions_out_inactive.rstrip('\n'))

# Add to argument list
arg = '{local_name}={var_name},'.format(local_name=var.local_name, var_name=tmpvar.local_name)
Expand Down

0 comments on commit b945b07

Please sign in to comment.