-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement SplitPytatoArrayContext that attempts a trivial parallelization strategy #216
Open
kaushikcfd
wants to merge
3
commits into
main
Choose a base branch
from
split_pytato_actx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
""" | ||
.. autoclass:: SplitPytatoPyOpenCLArrayContext | ||
|
||
""" | ||
|
||
__copyright__ = """ | ||
Copyright (C) 2023 Kaushik Kulkarni | ||
Copyright (C) 2023 Andreas Kloeckner | ||
Copyright (C) 2022 Matthias Diener | ||
Copyright (C) 2022 Matt Smith | ||
""" | ||
|
||
__license__ = """ | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
""" | ||
|
||
import sys | ||
from typing import TYPE_CHECKING | ||
|
||
import loopy as lp | ||
|
||
from arraycontext.impl.pytato import PytatoPyOpenCLArrayContext | ||
|
||
|
||
if TYPE_CHECKING or getattr(sys, "_BUILDING_SPHINX_DOCS", False): | ||
import pytato | ||
|
||
|
||
class SplitPytatoPyOpenCLArrayContext(PytatoPyOpenCLArrayContext): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this thing is named "split". Maybe "generic parallelizing"/"basic parallelizing"? |
||
""" | ||
.. note:: | ||
|
||
Refer to :meth:`transform_dag` and :meth:`transform_loopy_program` for | ||
details on the transformation algorithm provided by this array context. | ||
|
||
.. warning:: | ||
|
||
For expression graphs with large number of nodes high compile times are | ||
expected. | ||
""" | ||
def transform_dag(self, | ||
dag: "pytato.DictOfNamedArrays") -> "pytato.DictOfNamedArrays": | ||
r""" | ||
Returns a transformed version of *dag*, where the applied transform is: | ||
|
||
#. Materialize as per MPMS materialization heuristic. | ||
#. materialize every :class:`pytato.array.Einsum`\ 's inputs and outputs. | ||
""" | ||
import pytato as pt | ||
|
||
# Step 1. Collapse equivalent nodes in DAG. | ||
# ----------------------------------------- | ||
# type-ignore-reason: mypy is right pytato provides imprecise types. | ||
dag = pt.transform.deduplicate_data_wrappers(dag) # type: ignore[assignment] | ||
|
||
# Step 2. Materialize reduction inputs/outputs. | ||
# ------------------------------------------ | ||
from .utils import ( | ||
get_inputs_and_outputs_of_einsum, | ||
get_inputs_and_outputs_of_reduction_nodes) | ||
|
||
reduction_inputs_outputs = frozenset.union( | ||
*get_inputs_and_outputs_of_einsum(dag), | ||
*get_inputs_and_outputs_of_reduction_nodes(dag) | ||
) | ||
|
||
def materialize_einsum(expr: pt.transform.ArrayOrNames | ||
) -> pt.transform.ArrayOrNames: | ||
if expr in reduction_inputs_outputs: | ||
if isinstance(expr, pt.InputArgumentBase): | ||
return expr | ||
else: | ||
return expr.tagged(pt.tags.ImplStored()) | ||
else: | ||
return expr | ||
|
||
# type-ignore-reason: mypy is right pytato provides imprecise types. | ||
dag = pt.transform.map_and_copy(dag, # type: ignore[assignment] | ||
materialize_einsum) | ||
|
||
# Step 3. MPMS materialize | ||
# ------------------------ | ||
dag = pt.transform.materialize_with_mpms(dag) | ||
|
||
return dag | ||
|
||
def transform_loopy_program(self, | ||
t_unit: lp.TranslationUnit) -> lp.TranslationUnit: | ||
r""" | ||
Returns a transformed version of *t_unit*, where the applied transform is: | ||
|
||
#. An execution grid size :math:`G` is selected based on *self*'s | ||
OpenCL-device. | ||
#. The iteration domain for each statement in the *t_unit* is divided to | ||
equally among the work-items in :math:`G`. | ||
#. Kernel boundaries are drawn between every statement in the instruction. | ||
Although one can relax this constraint by letting :mod:`loopy` compute | ||
where to insert the global barriers, but it is not guaranteed to be | ||
performance profitable since we do not attempt any further loop-fusion | ||
and/or array contraction. | ||
#. Once the kernel boundaries are inferred, :func:`alias_global_temporaries` | ||
is invoked to reduce the memory peak memory used by the transformed | ||
program. | ||
""" | ||
# Step 1. Split the iteration across work-items | ||
# --------------------------------------------- | ||
from .utils import split_iteration_domain_across_work_items | ||
t_unit = split_iteration_domain_across_work_items(t_unit, self.queue.device) | ||
|
||
# Step 2. Add a global barrier between individual loop nests. | ||
# ------------------------------------------------------ | ||
from .utils import add_gbarrier_between_disjoint_loop_nests | ||
t_unit = add_gbarrier_between_disjoint_loop_nests(t_unit) | ||
|
||
# Step 3. Transform reduce to scalar statements | ||
# --------------------------------------------- | ||
from .utils import parallelize_reduce_to_scalars | ||
t_unit = parallelize_reduce_to_scalars(t_unit, self.queue.device) | ||
|
||
# Step 4. Alias global temporaries with disjoint live intervals | ||
# ------------------------------------------------------------- | ||
from .utils import alias_global_temporaries | ||
t_unit = alias_global_temporaries(t_unit) | ||
|
||
return t_unit | ||
|
||
# vim: fdm=marker |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to docs somewhere?