-
Notifications
You must be signed in to change notification settings - Fork 78
Opencl allocation #938
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
Open
dsding2
wants to merge
30
commits into
inducer:main
Choose a base branch
from
dsding2:opencl_allocation
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
Opencl allocation #938
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
cbdb757
add localized allocation and deallocation
dsding2 2fee158
delete commented out code
dsding2 8ace895
deal with base storage
dsding2 c4e635c
ruff check fixes
dsding2 24b1a47
rework to push allocations outside of loops
dsding2 be78797
add types, fix ruff
dsding2 461558d
Merge remote-tracking branch 'upstream/main' into opencl_allocation
dsding2 0bcf4df
Merge branch 'main' into opencl_allocation
dsding2 0b6abdd
refactor to make more target-generic
dsding2 4f95a6b
resolve lingering merge issues
dsding2 bd98636
fix to only allocate global temporaries
dsding2 47dda68
move temp declarations to ASTBuilder
dsding2 e494a3b
Merge branch 'main' into opencl_allocation
dsding2 88c436f
fix typing
dsding2 dae91e2
fix typing hopefully
dsding2 1cfe83a
add basic test
dsding2 3c3bb78
Merge branch 'main' into opencl_allocation
dsding2 3ef324c
more typing/ruff fixes
dsding2 f708b66
fix tutorial.rst and add to baseline
dsding2 a0a8365
Merge branch 'main' into opencl_allocation
inducer f12ce9f
Merge branch 'main' into opencl_allocation
inducer 452be6b
Merge branch 'main' into opencl_allocation
inducer 3985576
Update loopy/schedule/tools.py
dsding2 95e119e
Apply suggested test changes
dsding2 5cbfbf1
implement rename and documentation suggestions
dsding2 612b238
ruff fixes, revert broken change
dsding2 4b0d754
Merge branch 'main' into opencl_allocation
inducer e591ae6
Merge branch 'main' into opencl_allocation
inducer 1290c64
Merge branch 'main' into opencl_allocation
inducer b24fe99
Improvements
inducer 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -177,6 +177,137 @@ def supporting_temporary_names( | |
|
|
||
| return frozenset(result) | ||
|
|
||
|
|
||
| def _get_temporaries_accessed_in_schedule( | ||
| kernel: LoopKernel, | ||
| sched_idx_lower_bound: int, | ||
| sched_idx_upper_bound: int | ||
| ) -> frozenset[str]: | ||
| from loopy.schedule import CallKernel, EnterLoop, LeaveLoop | ||
|
|
||
| linearization = kernel.linearization | ||
| assert linearization is not None | ||
|
|
||
| temporaries: frozenset[str] = frozenset() | ||
| for sched_index in range(sched_idx_lower_bound, sched_idx_upper_bound): | ||
| sched_item = linearization[sched_index] | ||
| if isinstance(sched_item, CallKernel): | ||
| temporaries = ( | ||
| temporaries_written_in_subkernel(kernel, sched_item.kernel_name) | ||
| | temporaries_read_in_subkernel( | ||
| kernel, sched_item.kernel_name | ||
| ) | ||
| | (temporaries) | ||
| ) | ||
| elif isinstance(sched_item, (EnterLoop, LeaveLoop)): | ||
| # ignore further outside-kernel loops | ||
| pass | ||
|
|
||
| else: | ||
| raise NotImplementedError("kernel with non-CallKernel outermost") | ||
|
|
||
| return temporaries | ||
|
|
||
|
|
||
| def _map_to_base_storage(kernel: LoopKernel, tv_names: Set[str]) -> Set[str]: | ||
| result: set[str] = set() | ||
| for tv_name in tv_names: | ||
| while True: | ||
| tv = kernel.temporary_variables[tv_name] | ||
| if tv.base_storage is not None: | ||
| tv_name = tv.base_storage | ||
| else: | ||
| break | ||
|
|
||
| result.add(tv_name) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| @memoize_on_first_arg | ||
| def get_sched_index_to_first_and_last_used( | ||
|
Owner
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. Rather than "implicitly" attaching allocations to schedule items, maybe realize them explicitly as a new kind of schedule item? |
||
| kernel: LoopKernel | ||
| ) -> tuple[Mapping[int, Set[str]], Mapping[int, Set[str]]]: | ||
| """ | ||
| Returns the tuple (first_used, last_used), where first_used is | ||
| a dict such that first_used[sched_index] is the set of all global temporary | ||
| variable names first used at sched_index. | ||
|
|
||
| Likewise, last_used[sched_index] is the set of all global temporary | ||
| variable names last used at sched_index. | ||
| """ | ||
| from loopy.kernel.data import AddressSpace | ||
| from loopy.schedule import CallKernel, EnterLoop, Barrier | ||
|
|
||
| assert kernel.linearization is not None | ||
|
|
||
| global_temporaries = frozenset( | ||
| tv.name for tv in kernel.temporary_variables.values() | ||
| if tv.address_space == AddressSpace.GLOBAL | ||
| ) | ||
|
|
||
| # Collapse into blocks | ||
| block_boundaries = get_block_boundaries(kernel.linearization) | ||
|
|
||
| tvs_accessed_at: dict[int, frozenset[str]] = {} | ||
| sched_index = 0 | ||
| while sched_index < len(kernel.linearization): | ||
| sched_item = kernel.linearization[sched_index] | ||
| if isinstance(sched_item, CallKernel): | ||
| block_end = block_boundaries[sched_index] | ||
| tvs_accessed_at[sched_index] = ( | ||
| temporaries_written_in_subkernel(kernel, sched_item.kernel_name) | ||
| | temporaries_read_in_subkernel( | ||
| kernel, sched_item.kernel_name | ||
| ) | ||
| ) & global_temporaries | ||
|
|
||
| sched_index = block_end + 1 | ||
|
|
||
| elif isinstance(sched_item, EnterLoop): | ||
| block_end = block_boundaries[sched_index] | ||
| tvs_accessed_at[sched_index] = _get_temporaries_accessed_in_schedule( | ||
| kernel, sched_index, block_end+1 | ||
| ) & global_temporaries | ||
|
|
||
| sched_index = block_end + 1 | ||
|
|
||
| elif isinstance(sched_item, Barrier): | ||
| sched_index += 1 | ||
| else: | ||
| raise ValueError( | ||
| f"unexpected schedule item at outermost level: {type(sched_item)}") | ||
|
|
||
| storage_vars_accessed_at = { | ||
| sched_index: _map_to_base_storage(kernel, accessed) | ||
| for sched_index, accessed in tvs_accessed_at.items() | ||
| } | ||
| del tvs_accessed_at | ||
|
|
||
| # forward pass for first_accesses | ||
| first_accesses: dict[int, Set[str]] = {} | ||
| seen_storage_vars: set[str] = set() | ||
| for sched_index in range(0, len(kernel.linearization)): | ||
| accessed = storage_vars_accessed_at.get(sched_index, set()) | ||
| new_storage_vars = accessed - seen_storage_vars | ||
| seen_storage_vars.update(accessed) | ||
|
|
||
| if new_storage_vars: | ||
| first_accesses[sched_index] = new_storage_vars | ||
|
|
||
| # backward pass for last_accesses | ||
| last_accesses: dict[int, Set[str]] = {} | ||
| seen_storage_vars = set() | ||
| for sched_index in range(len(kernel.linearization)-1, -1, -1): | ||
| accessed = storage_vars_accessed_at.get(sched_index, set()) | ||
| new_storage_vars = accessed - seen_storage_vars | ||
| seen_storage_vars.update(accessed) | ||
|
|
||
| if new_storage_vars: | ||
| last_accesses[sched_index] = new_storage_vars | ||
|
|
||
| return (first_accesses, last_accesses) | ||
|
|
||
| # }}} | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Can this handle
RunInstructionoutermost?