-
Notifications
You must be signed in to change notification settings - Fork 23
algorithms: bounds #320
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
IvanGrigorik
wants to merge
21
commits into
main
Choose a base branch
from
grigorik/upper_bound
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
algorithms: bounds #320
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0dae1c5
add inclusive scan translation along with Kokkos `begin()` and `end()`
IvanGrigorik 8704ef5
add inclusive scan example inside of the kernel
IvanGrigorik 99ac006
add init file
IvanGrigorik dad8aae
add lower and upper bound functions, using binary search
IvanGrigorik e4acc6f
remove `inclusive_scan` from other branch
IvanGrigorik d666913
fix mypy issues
IvanGrigorik 5b32aba
rename `test` to `example`
IvanGrigorik c389845
remove unnecessary comments and annotations
IvanGrigorik a908a0f
update examples, remove annotations and comments
IvanGrigorik 08177c7
add support of multiple ranges
IvanGrigorik 92ad949
Merge branch 'grigorik/upper_bound' of https://github.com/kokkos/pyko…
IvanGrigorik 7c8f17a
add ignore flags to runtests
IvanGrigorik 5fe1bb6
Revert "add support of multiple ranges"
IvanGrigorik 866a0a5
Revert "add ignore flags to runtests"
IvanGrigorik a122cd3
trying to figure out the issue with CIs
IvanGrigorik 312d86e
Revert "Revert "add ignore flags to runtests""
IvanGrigorik c2a9225
Revert "Revert "add support of multiple ranges""
IvanGrigorik e50875c
Merge branch 'main' into grigorik/upper_bound
IvanGrigorik 297a4d9
Merge branch 'main' into grigorik/upper_bound
IvanGrigorik e142d10
try to fix test CI
IvanGrigorik c88da9b
add class method annotation
IvanGrigorik 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import pykokkos as pk | ||
|
|
||
|
|
||
| @pk.workunit | ||
| def init_data(i, view): | ||
| view[i] = i + 1 | ||
|
|
||
|
|
||
| # Test lower_bound with scratch memory | ||
| @pk.workunit | ||
| def team_lower_bound(team_member, view, result_view): | ||
| team_size: int = team_member.team_size() | ||
| offset: int = team_member.league_rank() * team_size | ||
| localIdx: int = team_member.team_rank() | ||
| globalIdx: int = offset + localIdx | ||
| team_rank: int = team_member.team_rank() | ||
|
|
||
| scratch: pk.ScratchView1D[int] = pk.ScratchView1D( | ||
| team_member.team_scratch(0), team_size | ||
| ) | ||
|
|
||
| scratch[team_rank] = view[globalIdx] | ||
| team_member.team_barrier() | ||
| search_value: int = team_rank * 2 # Search for a value | ||
| bound_idx: int = pk.lower_bound(scratch, team_size, search_value) | ||
| result_view[globalIdx] = bound_idx | ||
|
|
||
|
|
||
| # Test lower_bound with regular view | ||
| # Find lower bound for value i in the first 10 elements | ||
| @pk.workunit | ||
| def lower_bound_view(i, view, result_view): | ||
| search_value: int = i | ||
| bound_idx: int = pk.lower_bound(view, 10, search_value) | ||
| result_view[i] = bound_idx | ||
|
|
||
|
|
||
| def main(): | ||
| N = 64 | ||
| team_size = 32 | ||
| num_teams = (N + team_size - 1) // team_size | ||
|
|
||
| view: pk.View1D[int] = pk.View([N], int) | ||
| result_view: pk.View1D[int] = pk.View([N], int) | ||
|
|
||
| # Expected results | ||
| expected_scratch = pk.View([64], int) | ||
| expected_scratch_data = [ | ||
| 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, | ||
| 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | ||
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 | ||
| ] | ||
| for i in range(64): | ||
| expected_scratch[i] = expected_scratch_data[i] | ||
|
|
||
| expected_view = pk.View([64], int) | ||
| expected_view_data = [ | ||
| 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10 | ||
| ] | ||
| for i in range(64): | ||
| expected_view[i] = expected_view_data[i] | ||
|
|
||
| p_init = pk.RangePolicy(pk.ExecutionSpace.OpenMP, 0, N) | ||
| pk.parallel_for(p_init, init_data, view=view) | ||
|
|
||
| print(f"Total elements: {N}, Team size: {team_size}, Number of teams: {num_teams}") | ||
| print(f"Initial view: {view}") | ||
|
|
||
| # Test with TeamPolicy (scratch memory) | ||
| team_policy = pk.TeamPolicy(pk.ExecutionSpace.OpenMP, num_teams, team_size) | ||
|
|
||
| pk.parallel_for(team_policy, team_lower_bound, view=view, result_view=result_view) | ||
| print(f"Result (scratch lower_bound): {result_view}") | ||
|
|
||
| # Assert scratch lower_bound results | ||
| for i in range(N): | ||
| assert ( | ||
| result_view[i] == expected_scratch[i] | ||
| ), f"Mismatch at index {i}: got {result_view[i]}, expected {expected_scratch[i]}" | ||
|
|
||
| # Test with RangePolicy (regular view) | ||
| pk.parallel_for(p_init, lower_bound_view, view=view, result_view=result_view) | ||
| print(f"Result (view lower_bound): {result_view}") | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
|
|
||
| for i in range(N): | ||
| assert ( | ||
| result_view[i] == expected_view[i] | ||
| ), f"Mismatch at index {i}: got {result_view[i]}, expected {expected_view[i]}" | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import pykokkos as pk | ||
|
|
||
|
|
||
| @pk.workunit | ||
| def init_data(i, view): | ||
| view[i] = i + 1 | ||
|
|
||
|
|
||
| # Test upper_bound with scratch memory | ||
| @pk.workunit | ||
| def team_upper_bound(team_member, view, result_view): | ||
| team_size: int = team_member.team_size() | ||
| offset: int = team_member.league_rank() * team_size | ||
| localIdx: int = team_member.team_rank() | ||
| globalIdx: int = offset + localIdx | ||
| team_rank: int = team_member.team_rank() | ||
|
|
||
| scratch: pk.ScratchView1D[int] = pk.ScratchView1D( | ||
| team_member.team_scratch(0), team_size | ||
| ) | ||
|
|
||
| scratch[team_rank] = view[globalIdx] | ||
| team_member.team_barrier() | ||
| search_value: int = team_rank * 2 # Search for a value | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
| bound_idx: int = pk.upper_bound(scratch, team_size, search_value) | ||
| result_view[globalIdx] = bound_idx | ||
|
|
||
|
|
||
| # Test upper_bound with regular view | ||
| # Find upper bound for value i in the first 10 elements | ||
| @pk.workunit | ||
| def upper_bound_view(i, view, result_view): | ||
| search_value: int = i | ||
| bound_idx: int = pk.upper_bound(view, 10, search_value) | ||
| result_view[i] = bound_idx | ||
|
|
||
|
|
||
| def main(): | ||
| N = 64 | ||
| team_size = 32 | ||
| num_teams = (N + team_size - 1) // team_size | ||
|
|
||
| view: pk.View1D[int] = pk.View([N], int) | ||
| result_view: pk.View1D[int] = pk.View([N], int) | ||
|
|
||
| # Expected results | ||
| expected_scratch = pk.View([64], int) | ||
| expected_scratch_data = [ | ||
| 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, | ||
| 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | ||
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 | ||
| ] | ||
| for i in range(64): | ||
| expected_scratch[i] = expected_scratch_data[i] | ||
|
|
||
| expected_view = pk.View([64], int) | ||
| expected_view_data = [ | ||
| 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | ||
| 10, 10, 10, 10, 10, 10, 10, 10 | ||
| ] | ||
| for i in range(64): | ||
| expected_view[i] = expected_view_data[i] | ||
|
|
||
| p_init = pk.RangePolicy(pk.ExecutionSpace.Cuda, 0, N) | ||
| pk.parallel_for(p_init, init_data, view=view) | ||
|
|
||
| print(f"Total elements: {N}, Team size: {team_size}, Number of teams: {num_teams}") | ||
| print(f"Initial view: {view}") | ||
|
|
||
| # Test with TeamPolicy (scratch memory) | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
| team_policy = pk.TeamPolicy(pk.ExecutionSpace.Cuda, num_teams, team_size) | ||
|
|
||
| pk.parallel_for(team_policy, team_upper_bound, view=view, result_view=result_view) | ||
| print(f"Result (scratch upper_bound): {result_view}") | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
|
|
||
| # Assert scratch upper_bound results | ||
| for i in range(N): | ||
| assert ( | ||
| result_view[i] == expected_scratch[i] | ||
| ), f"Mismatch at index {i}: got {result_view[i]}, expected {expected_scratch[i]}" | ||
| print("Scratch upper_bound test passed") | ||
|
|
||
| # Test with RangePolicy (regular view) | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
| pk.parallel_for(p_init, upper_bound_view, view=view, result_view=result_view) | ||
| print(f"Result (view upper_bound): {result_view}") | ||
|
IvanGrigorik marked this conversation as resolved.
|
||
|
|
||
| # Assert view upper_bound results | ||
| for i in range(N): | ||
| assert ( | ||
| result_view[i] == expected_view[i] | ||
| ), f"Mismatch at index {i}: got {result_view[i]}, expected {expected_view[i]}" | ||
| print("View upper_bound test passed") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from .lower_bound import lower_bound | ||
| from .upper_bound import upper_bound | ||
|
|
||
| __all__ = ["lower_bound", "upper_bound"] |
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.
Uh oh!
There was an error while loading. Please reload this page.