Skip to content

Remove in-place modification of arrays in preparation for lazy evaluation#180

Closed
thomasgibson wants to merge 17 commits into
mainfrom
thg/remove-inplace-mod
Closed

Remove in-place modification of arrays in preparation for lazy evaluation#180
thomasgibson wants to merge 17 commits into
mainfrom
thg/remove-inplace-mod

Conversation

@thomasgibson

Copy link
Copy Markdown
Collaborator

With @kaushikcfd's blessing, I am reviving #58 (which is a pre-requisite for #116). Still a WIP.

@thomasgibson thomasgibson added help wanted lazy evaluation Anything related to lazy evaluation labels May 6, 2021
Comment thread meshmode/dof_array.py
Comment on lines +485 to +502
# def flatten(ary: Union[DOFArray, np.ndarray]) -> Any:
# r"""Convert a :class:`DOFArray` into a "flat" array of degrees of freedom,
# where the resulting type of the array is given by the
# :attr:`DOFArray.array_context`.

# Array elements are laid out contiguously, with the element group
# index varying slowest, element index next, and intra-element DOF
# index fastest.

# Vectorizes over object arrays of :class:`DOFArray`\ s.
# """
# if isinstance(ary, np.ndarray):
# return obj_array_vectorize(flatten, ary)

# actx = ary.array_context

# return actx.np.concatenate(actx.np.reshape(grp_ary, (-1,))
# for grp_ary in ary)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaushikcfd Unfortunately, this does not work. Every time an array goes through this, an array with junk is returned. Haven't taken a close look into why, but making a note here.

Comment on lines +170 to +214
kernels = [] # get kernels for each batch; to be fused eventually
kwargs = {} # kwargs to the fused kernel
for ibatch, batch in enumerate(cgrp.batches):
sgrp = self.from_discr.groups[batch.from_group_index]

knl = lp.rename_argument(
kproj(), "basis_tabulation",
f"basis_tabulation_batch{ibatch}"
)
knl = lp.rename_argument(knl, "ary", f"ary_batch{ibatch}")
knl = lp.rename_argument(knl, "weights", f"weights_batch{ibatch}")
knl = lp.rename_argument(knl, "from_element_indices",
f"from_element_indices_{ibatch}")
knl = lp.rename_argument(knl, "to_element_indices",
f"to_element_indices_{ibatch}")
knl = lp.rename_argument(knl, "nelements",
f"nelements_{ibatch}")

for iname in knl.all_inames():
knl = lp.rename_iname(knl, iname, f"{iname}_batch{ibatch}")

# Generate the basis tabulation matrix
tabulations = []
for ibasis, basis_fn in enumerate(sgrp.basis_obj().functions):
basis = actx.from_numpy(
basis_fn(batch.result_unit_nodes).flatten())

# NOTE: batch.*_element_indices are reversed here because
# they are from the original forward connection, but
# we are going in reverse here. a bit confusing, but
# saves on recreating the connection groups and batches.
actx.call_loopy(kproj(),
ibasis=ibasis,
ary=ary[sgrp.index],
basis=basis,
weights=weights[igrp, ibatch],
result=c[igrp],
from_element_indices=batch.to_element_indices,
to_element_indices=batch.from_element_indices)
tabulations.append(basis_fn(batch.result_unit_nodes).flatten())
tabulations = actx.from_numpy(np.asarray(tabulations))

kwargs[f"basis_tabulation_batch{ibatch}"] = tabulations
kwargs[f"ary_batch{ibatch}"] = ary[sgrp.index]
kwargs[f"weights_batch{ibatch}"] = weights[igrp, ibatch]
kwargs[f"from_element_indices_{ibatch}"] = \
batch.from_element_indices
kwargs[f"to_element_indices_{ibatch}"] = \
batch.to_element_indices

kernels.append(knl)

fused_knl = lp.fuse_kernels(kernels)
fused_knl = fused_knl.copy(name="fused_kproj_program")
fused_knl = lp.add_nosync(fused_knl,
"global",
"writes:result",
"writes:result",
bidirectional=True,
force=True)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unfortunately at a loss for why this fails to work (just segfaults --- tested by running py.test test/test_chained.py -k 'test_reversed_chained_connection). Would appreciate someone with acute loopy-awareness to look at this and spot something "obviously wrong". Cough: @inducer :)

@thomasgibson

Copy link
Copy Markdown
Collaborator Author

Closing this PR since the recent updates made it a huge pain to rebase. Replaced with a much cleaner attempt: #192

@thomasgibson thomasgibson deleted the thg/remove-inplace-mod branch May 21, 2021 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted lazy evaluation Anything related to lazy evaluation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants