-
Notifications
You must be signed in to change notification settings - Fork 21
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
18
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 7 commits
Commits
Show all changes
18 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 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,75 @@ | ||
| import pykokkos as pk | ||
|
|
||
|
|
||
| @pk.workunit | ||
| def init_data(i: int, view: pk.View1D[int]): | ||
| view[i] = i + 1 | ||
|
|
||
|
|
||
| # Test lower_bound with scratch memory | ||
| @pk.workunit | ||
| def team_lower_bound(team_member: pk.TeamMember, view: pk.View1D[int], result_view: pk.View1D[int]): | ||
| 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() | ||
|
|
||
| # Allocate scratch memory for sorted data | ||
| scratch: pk.ScratchView1D[int] = pk.ScratchView1D( | ||
| team_member.team_scratch(0), team_size | ||
| ) | ||
|
|
||
| # Copy data to scratch and make it sorted within the team | ||
| scratch[team_rank] = view[globalIdx] | ||
| team_member.team_barrier() | ||
|
|
||
| # Now use lower_bound to find position in scratch | ||
| # For example, find lower bound for the value at team_rank position | ||
| search_value: int = team_rank * 2 # Search for a value | ||
IvanGrigorik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Find lower bound in scratch memory | ||
| bound_idx: int = pk.lower_bound(scratch, team_size, search_value) | ||
|
|
||
| # Store result | ||
| result_view[globalIdx] = bound_idx | ||
|
|
||
|
|
||
| # Test lower_bound with regular view | ||
| @pk.workunit | ||
| def lower_bound_view(i: int, view: pk.View1D[int], result_view: pk.View1D[int]): | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Find lower bound for value i in the first 10 elements | ||
| 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) | ||
|
|
||
| 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) | ||
|
|
||
| print("\nRunning lower_bound with scratch memory...") | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pk.parallel_for(team_policy, team_lower_bound, view=view, result_view=result_view) | ||
| print(f"Result (scratch lower_bound): {result_view}") | ||
|
|
||
| # Test with RangePolicy (regular view) | ||
| print("\nRunning lower_bound with 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.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| 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,75 @@ | ||
| import pykokkos as pk | ||
|
|
||
|
|
||
| @pk.workunit | ||
| def init_data(i: int, view: pk.View1D[int]): | ||
| view[i] = i + 1 | ||
|
|
||
|
|
||
| # Test upper_bound with scratch memory | ||
| @pk.workunit | ||
| def team_upper_bound(team_member: pk.TeamMember, view: pk.View1D[int], result_view: pk.View1D[int]): | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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() | ||
|
|
||
| # Allocate scratch memory for sorted data | ||
| scratch: pk.ScratchView1D[int] = pk.ScratchView1D( | ||
| team_member.team_scratch(0), team_size | ||
| ) | ||
|
|
||
| # Copy data to scratch and make it sorted within the team | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scratch[team_rank] = view[globalIdx] | ||
| team_member.team_barrier() | ||
|
|
||
| # Now use upper_bound to find position in scratch | ||
| # For example, find upper bound for the value at team_rank position | ||
| search_value: int = team_rank * 2 # Search for a value | ||
IvanGrigorik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Find upper bound in scratch memory | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bound_idx: int = pk.upper_bound(scratch, team_size, search_value) | ||
|
|
||
| # Store result | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| result_view[globalIdx] = bound_idx | ||
|
|
||
|
|
||
| # Test upper_bound with regular view | ||
| @pk.workunit | ||
| def upper_bound_view(i: int, view: pk.View1D[int], result_view: pk.View1D[int]): | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Find upper bound for value i in the first 10 elements | ||
IvanGrigorik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
|
|
||
| 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.
Show resolved
Hide resolved
|
||
| team_policy = pk.TeamPolicy(pk.ExecutionSpace.Cuda, num_teams, team_size) | ||
|
|
||
| print("\nRunning upper_bound with scratch memory...") | ||
| 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.
Show resolved
Hide resolved
|
||
|
|
||
| # Test with RangePolicy (regular view) | ||
IvanGrigorik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print("\nRunning upper_bound with regular view...") | ||
| 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.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| 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"] |
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,91 @@ | ||
| from pykokkos.interface.views import ViewType | ||
| from pykokkos.core import cppast | ||
|
|
||
| def lower_bound(view: ViewType, size: int, value) -> int: | ||
| """ | ||
| Perform a lower bound search on a view | ||
|
|
||
| Returns the index of the first element not less than (i.e. greater or equal to) value, | ||
| similar to std::lower_bound or thrust::lower_bound. | ||
|
|
||
| :param view: the view to search (must be sorted) | ||
| :param size: the number of elements to search | ||
| :param value: the value to search for | ||
| :returns: the index of the first element >= value | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| def generate_lower_bound_binary_search( | ||
| view_expr: cppast.Expr, size_expr: cppast.Expr, value_expr: cppast.Expr | ||
| ) -> cppast.CompoundStmt: | ||
| """ | ||
| Generate binary search implementation for lower_bound. | ||
| Returns a CompoundStmt that implements: | ||
|
|
||
| int left = 0; | ||
| int right = size; | ||
| int mid; | ||
| while (left < right) { | ||
| mid = left + (right - left) / 2; | ||
| if (view[mid] < value) { | ||
| left = mid + 1; | ||
| } else { | ||
| right = mid; | ||
| } | ||
| } | ||
| return left; | ||
| """ | ||
|
|
||
| # Variable declarations | ||
| int_type = cppast.PrimitiveType("int32_t") | ||
|
|
||
| # int left = 0; | ||
| left_var = cppast.DeclRefExpr("left") | ||
| left_init = cppast.IntegerLiteral(0) | ||
| left_decl = cppast.VarDecl(int_type, left_var, left_init) | ||
| left_stmt = cppast.DeclStmt(left_decl) | ||
|
|
||
| # int right = size; | ||
| right_var = cppast.DeclRefExpr("right") | ||
| right_decl = cppast.VarDecl(int_type, right_var, size_expr) | ||
| right_stmt = cppast.DeclStmt(right_decl) | ||
|
|
||
| # int mid; | ||
| mid_var = cppast.DeclRefExpr("mid") | ||
| mid_decl = cppast.VarDecl(int_type, mid_var, None) | ||
| mid_stmt = cppast.DeclStmt(mid_decl) | ||
|
|
||
| # while (left < right) | ||
| while_cond = cppast.BinaryOperator(left_var, right_var, cppast.BinaryOperatorKind.LT) | ||
|
|
||
| # mid = left + (right - left) / 2; | ||
| right_minus_left = cppast.BinaryOperator(right_var, left_var, cppast.BinaryOperatorKind.Sub) | ||
| div_expr = cppast.BinaryOperator(right_minus_left, cppast.IntegerLiteral(2), cppast.BinaryOperatorKind.Div) | ||
| mid_calc = cppast.BinaryOperator(left_var, div_expr, cppast.BinaryOperatorKind.Add) | ||
| mid_assign = cppast.AssignOperator([mid_var], mid_calc, cppast.BinaryOperatorKind.Assign) | ||
|
|
||
| # if (view[mid] < value) | ||
| view_ref = view_expr if isinstance(view_expr, cppast.DeclRefExpr) else cppast.DeclRefExpr("view") | ||
| view_access = cppast.ArraySubscriptExpr(view_ref, [mid_var]) | ||
| if_cond = cppast.BinaryOperator(view_access, value_expr, cppast.BinaryOperatorKind.LT) | ||
|
|
||
| # left = mid + 1; | ||
| mid_plus_one = cppast.BinaryOperator(mid_var, cppast.IntegerLiteral(1), cppast.BinaryOperatorKind.Add) | ||
| left_assign = cppast.AssignOperator([left_var], mid_plus_one, cppast.BinaryOperatorKind.Assign) | ||
|
|
||
| # right = mid; | ||
| right_assign = cppast.AssignOperator([right_var], mid_var, cppast.BinaryOperatorKind.Assign) | ||
|
|
||
| # if-else statement | ||
| if_stmt = cppast.IfStmt(if_cond, left_assign, right_assign) | ||
|
|
||
| # while body | ||
| while_body = cppast.CompoundStmt([mid_assign, if_stmt]) | ||
| while_stmt = cppast.WhileStmt(while_cond, while_body) | ||
|
|
||
| # return left; | ||
| return_stmt = cppast.ReturnStmt(left_var) | ||
|
|
||
| # Complete function body | ||
| return cppast.CompoundStmt([left_stmt, right_stmt, mid_stmt, while_stmt, return_stmt]) |
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.