Skip to content

Commit e4acc6f

Browse files
committed
remove inclusive_scan from other branch
1 parent dad8aae commit e4acc6f

File tree

8 files changed

+153
-122
lines changed

8 files changed

+153
-122
lines changed

examples/kokkos/inclusive_scan_team.py

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pykokkos as pk
2+
3+
4+
@pk.workunit
5+
def init_data(i: int, view: pk.View1D[int]):
6+
view[i] = i + 1
7+
8+
9+
# Test lower_bound with scratch memory
10+
@pk.workunit
11+
def team_lower_bound(team_member: pk.TeamMember, view: pk.View1D[int], result_view: pk.View1D[int]):
12+
team_size: int = team_member.team_size()
13+
offset: int = team_member.league_rank() * team_size
14+
localIdx: int = team_member.team_rank()
15+
globalIdx: int = offset + localIdx
16+
team_rank: int = team_member.team_rank()
17+
18+
# Allocate scratch memory for sorted data
19+
scratch: pk.ScratchView1D[int] = pk.ScratchView1D(
20+
team_member.team_scratch(0), team_size
21+
)
22+
23+
# Copy data to scratch and make it sorted within the team
24+
scratch[team_rank] = view[globalIdx]
25+
team_member.team_barrier()
26+
27+
# Now use lower_bound to find position in scratch
28+
# For example, find lower bound for the value at team_rank position
29+
search_value: int = team_rank * 2 # Search for a value
30+
31+
# Find lower bound in scratch memory
32+
bound_idx: int = pk.lower_bound(scratch, team_size, search_value)
33+
34+
# Store result
35+
result_view[globalIdx] = bound_idx
36+
37+
38+
# Test lower_bound with regular view
39+
@pk.workunit
40+
def test_lower_bound_view(i: int, view: pk.View1D[int], result_view: pk.View1D[int]):
41+
# Find lower bound for value i in the first 10 elements
42+
search_value: int = i
43+
bound_idx: int = pk.lower_bound(view, 10, search_value)
44+
result_view[i] = bound_idx
45+
46+
47+
def main():
48+
N = 64
49+
team_size = 32
50+
num_teams = (N + team_size - 1) // team_size
51+
52+
view: pk.View1D[int] = pk.View([N], int)
53+
result_view: pk.View1D[int] = pk.View([N], int)
54+
55+
p_init = pk.RangePolicy(pk.ExecutionSpace.OpenMP, 0, N)
56+
pk.parallel_for(p_init, init_data, view=view)
57+
58+
print(f"Total elements: {N}, Team size: {team_size}, Number of teams: {num_teams}")
59+
print(f"Initial view: {view}")
60+
61+
# Test with TeamPolicy (scratch memory)
62+
team_policy = pk.TeamPolicy(pk.ExecutionSpace.OpenMP, num_teams, team_size)
63+
64+
print("\nRunning lower_bound with scratch memory...")
65+
pk.parallel_for(team_policy, team_lower_bound, view=view, result_view=result_view)
66+
print(f"Result (scratch lower_bound): {result_view}")
67+
68+
# Test with RangePolicy (regular view)
69+
print("\nRunning lower_bound with regular view...")
70+
pk.parallel_for(p_init, test_lower_bound_view, view=view, result_view=result_view)
71+
print(f"Result (view lower_bound): {result_view}")
72+
73+
74+
if __name__ == "__main__":
75+
main()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pykokkos as pk
2+
3+
4+
@pk.workunit
5+
def init_data(i: int, view: pk.View1D[int]):
6+
view[i] = i + 1
7+
8+
9+
# Test upper_bound with scratch memory
10+
@pk.workunit
11+
def team_upper_bound(team_member: pk.TeamMember, view: pk.View1D[int], result_view: pk.View1D[int]):
12+
team_size: int = team_member.team_size()
13+
offset: int = team_member.league_rank() * team_size
14+
localIdx: int = team_member.team_rank()
15+
globalIdx: int = offset + localIdx
16+
team_rank: int = team_member.team_rank()
17+
18+
# Allocate scratch memory for sorted data
19+
scratch: pk.ScratchView1D[int] = pk.ScratchView1D(
20+
team_member.team_scratch(0), team_size
21+
)
22+
23+
# Copy data to scratch and make it sorted within the team
24+
scratch[team_rank] = view[globalIdx]
25+
team_member.team_barrier()
26+
27+
# Now use upper_bound to find position in scratch
28+
# For example, find upper bound for the value at team_rank position
29+
search_value: int = team_rank * 2 # Search for a value
30+
31+
# Find upper bound in scratch memory
32+
bound_idx: int = pk.upper_bound(scratch, team_size, search_value)
33+
34+
# Store result
35+
result_view[globalIdx] = bound_idx
36+
37+
38+
# Test upper_bound with regular view
39+
@pk.workunit
40+
def test_upper_bound_view(i: int, view: pk.View1D[int], result_view: pk.View1D[int]):
41+
# Find upper bound for value i in the first 10 elements
42+
search_value: int = i
43+
bound_idx: int = pk.upper_bound(view, 10, search_value)
44+
result_view[i] = bound_idx
45+
46+
47+
def main():
48+
N = 64
49+
team_size = 32
50+
num_teams = (N + team_size - 1) // team_size
51+
52+
view: pk.View1D[int] = pk.View([N], int)
53+
result_view: pk.View1D[int] = pk.View([N], int)
54+
55+
p_init = pk.RangePolicy(pk.ExecutionSpace.Cuda, 0, N)
56+
pk.parallel_for(p_init, init_data, view=view)
57+
58+
print(f"Total elements: {N}, Team size: {team_size}, Number of teams: {num_teams}")
59+
print(f"Initial view: {view}")
60+
61+
# Test with TeamPolicy (scratch memory)
62+
team_policy = pk.TeamPolicy(pk.ExecutionSpace.Cuda, num_teams, team_size)
63+
64+
print("\nRunning upper_bound with scratch memory...")
65+
pk.parallel_for(team_policy, team_upper_bound, view=view, result_view=result_view)
66+
print(f"Result (scratch upper_bound): {result_view}")
67+
68+
# Test with RangePolicy (regular view)
69+
print("\nRunning upper_bound with regular view...")
70+
pk.parallel_for(p_init, test_upper_bound_view, view=view, result_view=result_view)
71+
print(f"Result (view upper_bound): {result_view}")
72+
73+
74+
if __name__ == "__main__":
75+
main()

pykokkos/core/translators/symbols_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, members: PyKokkosMembers, pk_import: str, path: str):
5454
self.global_symbols.update(math_functions)
5555
self.global_symbols.update(allowed_types)
5656
self.global_symbols.update(view_dtypes)
57-
self.global_symbols.update(["self", "range", "math", "List", "abs", "inclusive_scan", "upper_bound"])
57+
self.global_symbols.update(["self", "range", "math", "List", "abs", "upper_bound", "lower_bound"])
5858
self.global_symbols.add(pk_import)
5959

6060
self.global_symbols.update([field.declname for field in members.fields])

pykokkos/core/visitors/workunit_visitor.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -318,51 +318,6 @@ def visit_Call(self, node: ast.Call) -> cppast.CallExpr:
318318

319319
return real_number_call
320320

321-
# Kokkos `inclusive_scan`
322-
if name == "inclusive_scan":
323-
# Check if it's called via pk.inclusive_scan
324-
is_pk_call = (
325-
isinstance(node.func, ast.Attribute)
326-
and isinstance(node.func.value, ast.Name)
327-
and node.func.value.id == self.pk_import
328-
)
329-
330-
if not is_pk_call:
331-
return super().visit_Call(node)
332-
333-
# Expected signature: pk.inclusive_scan(team_member, view, length)
334-
# or: pk.inclusive_scan(team_member, view) - uses view size
335-
if len(args) < 2 or len(args) > 3:
336-
self.error(
337-
node,
338-
"pk.inclusive_scan() takes 2 or 3 arguments: team_member, view, [length]",
339-
)
340-
341-
team_member_expr = args[0]
342-
view_expr = args[1]
343-
344-
# Create Kokkos::Experimental::begin() and end() calls
345-
begin_function = cppast.DeclRefExpr("Kokkos::Experimental::begin")
346-
end_function = cppast.DeclRefExpr("Kokkos::Experimental::end")
347-
view_begin = cppast.CallExpr(begin_function, [view_expr])
348-
349-
if len(args) == 3:
350-
# Use provided length: begin + length
351-
length_expr = args[2]
352-
view_begin_for_end = cppast.CallExpr(begin_function, [view_expr])
353-
view_end = cppast.BinaryOperator(
354-
view_begin_for_end, length_expr, cppast.BinaryOperatorKind.Add
355-
)
356-
else:
357-
# Use end() when no length is provided
358-
view_end = cppast.CallExpr(end_function, [view_expr])
359-
360-
# Create Kokkos::Experimental::inclusive_scan call
361-
function = cppast.DeclRefExpr("Kokkos::Experimental::inclusive_scan")
362-
scan_args = [team_member_expr, view_begin, view_end, view_begin]
363-
364-
return cppast.CallExpr(function, scan_args)
365-
366321
# Custom `upper_bound` implementation using binary search
367322
if name == "upper_bound":
368323
# Check if it's called via pk.upper_bound

pykokkos/interface/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .accumulator import Acc
2-
from .algorithms import inclusive_scan, lower_bound, upper_bound
2+
from .algorithms import lower_bound, upper_bound
33
from .atomic.atomic_fetch_op import (
44
atomic_fetch_add, atomic_fetch_and, atomic_fetch_div,
55
atomic_fetch_lshift, atomic_fetch_max, atomic_fetch_min,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from .inclusive_scan import inclusive_scan
21
from .lower_bound import lower_bound
32
from .upper_bound import upper_bound
43

5-
__all__ = ["inclusive_scan", "lower_bound", "upper_bound"]
4+
__all__ = ["lower_bound", "upper_bound"]

pykokkos/interface/algorithms/inclusive_scan.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)