Skip to content

Commit

Permalink
properly adjusting the starting index for Fortran
Browse files Browse the repository at this point in the history
  • Loading branch information
ylee88 committed Aug 26, 2024
1 parent 87d58f5 commit f4ec4bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
19 changes: 11 additions & 8 deletions tools/milhoja_pypkg/src/milhoja/FortranTemplateUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def iterate_tilemetadata(

construct_host = ""
if pure_source != LBOUND_ARGUMENT:
# adjusting index base
fix_index = '+1' if pure_source in bounds_data else ''
info.dtype = F2C_TYPE_MAPPING.get(info.dtype, info.dtype)

Expand All @@ -176,15 +177,17 @@ def iterate_tilemetadata(
# not have knowledge of the other
if source == TILE_INTERIOR_ARGUMENT:
construct_host = "[MILHOJA_MDIM * 2] = {" \
"tileDesc_h->lo().I()+1,tileDesc_h->hi().I()+1, " \
"tileDesc_h->lo().J()+1,tileDesc_h->hi().J()+1, " \
"tileDesc_h->lo().K()+1,tileDesc_h->hi().K()+1 }"
f"tileDesc_h->lo().I(){fix_index}, tileDesc_h->hi().I(){fix_index}, " \
f"tileDesc_h->lo().J(){fix_index}, tileDesc_h->hi().J(){fix_index}, " \
f"tileDesc_h->lo().K(){fix_index}, tileDesc_h->hi().K(){fix_index} " \
"}"

elif source == TILE_ARRAY_BOUNDS_ARGUMENT:
construct_host = "[MILHOJA_MDIM * 2] = {" \
"tileDesc_h->loGC().I()+1,tileDesc_h->hiGC().I()+1, "\
"tileDesc_h->loGC().J()+1,tileDesc_h->hiGC().J()+1, "\
"tileDesc_h->loGC().K()+1,tileDesc_h->hiGC().K()+1 }"
f"tileDesc_h->loGC().I(){fix_index}, tileDesc_h->hiGC().I(){fix_index}, "\
f"tileDesc_h->loGC().J(){fix_index}, tileDesc_h->hiGC().J(){fix_index}, "\
f"tileDesc_h->loGC().K(){fix_index}, tileDesc_h->hiGC().K(){fix_index} " \
"}"

elif source == TILE_LEVEL_ARGUMENT:
one_time_mdata[item] = data
Expand All @@ -202,8 +205,8 @@ def iterate_tilemetadata(
else:
# info.dtype = VECTOR_ARRAY_EQUIVALENT[info.dtype]
# intvect i,j,k start at 0 so we need to add 1 to the
# index. However, anything that's just integers needs to be
# untouched.
# index as it is a Fortran.
# However, anything that's just integers needs to be untouched.
for idx, value in enumerate(lbound):
found = re.search('[a-zA-z]', value)
if found:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
TILE_UBOUND_ARGUMENT, SCRATCH_ARGUMENT, F2C_TYPE_MAPPING,
THREAD_INDEX_VAR_NAME, GRID_DATA_ARGUMENT, TILE_INTERIOR_ARGUMENT,
TILE_ARRAY_BOUNDS_ARGUMENT, GRID_DATA_PTRS, SOURCE_DATATYPES,
VECTOR_ARRAY_EQUIVALENT, TILE_ARGUMENTS_ALL, GRID_DATA_LBOUNDS
VECTOR_ARRAY_EQUIVALENT, TILE_ARGUMENTS_ALL, GRID_DATA_LBOUNDS,
TILE_INDEX_DATA,
)


Expand Down Expand Up @@ -149,14 +150,16 @@ def _fill_mdata_connectors(self, arg, spec, connectors: dict, saved):
if src == TILE_INTERIOR_ARGUMENT or src == TILE_ARRAY_BOUNDS_ARGUMENT:
lo = 'tile_lo' if src == TILE_INTERIOR_ARGUMENT else "tile_loGC"
hi = 'tile_hi' if src == TILE_INTERIOR_ARGUMENT else "tile_hiGC"
# Fortran assumes 1-based index
offs = "+1"
connectors[self.C2F_ARG_LIST].append(f"const void* {src}")
connectors[self.REAL_ARGS].append(f"static_cast<void*>({src})")
lo_data = lo.replace("tile_", "") + "()"
hi_data = hi.replace("tile_", "") + "()"
combined = f"int {src}[] = {{\n{self.INDENT}"
combined += f',\n{self.INDENT}'.join(
'{0}->{1}.{3}(),{0}->{2}.{3}()'.format(
self.tile_desc_name, lo_data, hi_data, char
'{0}->{1}.{3}(){4}, {0}->{2}.{3}(){4}'.format(
self.tile_desc_name, lo_data, hi_data, char, offs
)
for char in ['I', 'J', 'K']
) + "\n}"
Expand Down Expand Up @@ -186,11 +189,15 @@ def _fill_mdata_connectors(self, arg, spec, connectors: dict, saved):

dtype = SOURCE_DATATYPES[src]
if dtype in VECTOR_ARRAY_EQUIVALENT:
offs = ""
if src in TILE_INDEX_DATA:
# Fortran assumes 1-based index
offs = "+1"
raw = VECTOR_ARRAY_EQUIVALENT[dtype]
connectors[self.CONSOLIDATE_TILE_DATA].append(
f"{raw} {arg}_array[] = {{\n{self.INDENT}{arg}.I(),\n"
f"{self.INDENT}{arg}.J(),\n"
f"{self.INDENT}{arg}.K()\n}}"
f"{raw} {arg}_array[] = {{\n{self.INDENT}{arg}.I(){offs},\n"
f"{self.INDENT}{arg}.J(){offs},\n"
f"{self.INDENT}{arg}.K(){offs}\n}}"
)

else:
Expand Down Expand Up @@ -241,6 +248,13 @@ def _fill_lbound_connectors(self, arg, spec, connectors, saved):
)
saved.add(word)

# adjusting the base index
# because it is a Fortran
for i, bound in enumerate(lb):
for keyword in TILE_INDEX_DATA:
if keyword in bound:
lb[i] = bound + "+1"

lb = f"{{\n{self.INDENT}" + f',\n{self.INDENT}'.join(lb) + "\n}"
lb = lb.replace(" ", "")
connectors[self.CONSOLIDATE_TILE_DATA].append(
Expand Down
1 change: 1 addition & 0 deletions tools/milhoja_pypkg/src/milhoja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TILE_FACE_AREAS_ARGUMENT,
TILE_CELL_VOLUMES_ARGUMENT,
TILE_ARGUMENTS_ALL,
TILE_INDEX_DATA,
GRID_DATA_LBOUNDS, GRID_DATA_EXTENTS,
C2F_TYPE_MAPPING, VECTOR_ARRAY_EQUIVALENT,
GRID_DATA_PTRS, SOURCE_DATATYPES, F2C_TYPE_MAPPING,
Expand Down
10 changes: 10 additions & 0 deletions tools/milhoja_pypkg/src/milhoja/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
TILE_CELL_VOLUMES_ARGUMENT
}

# The following index data must be handled carefully
# with a proper starting index, e.g., 1-based for Fortran language,
# during the code generations.
TILE_INDEX_DATA = [
TILE_LO_ARGUMENT,
TILE_HI_ARGUMENT,
TILE_LBOUND_ARGUMENT,
TILE_UBOUND_ARGUMENT,
]

# ----- DATA ACCESS KEYWORDS
READ = "r"
WRITE = "w"
Expand Down

0 comments on commit f4ec4bd

Please sign in to comment.