Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/examples_kokkos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: examples/kokkos

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test_pykokkos:
strategy:
matrix:
platform: [ubuntu-latest]
python-version: ["3.13"]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade numpy mypy==1.0.1 cmake pytest pybind11 scikit-build patchelf
- name: Install pykokkos-base
run: |
python install_base.py install -- -DENABLE_LAYOUTS=ON -DENABLE_MEMORY_TRAITS=OFF -DENABLE_VIEW_RANKS=2
- name: Install pykokkos
run: |
python -m pip install .
- name: mypy check
run: |
mypy pykokkos
- name: Run all kokkos examples
run: |
for f in examples/kokkos/*.py; do
echo "Running $f..."
if [[ "$f" == *_lambda.py ]]; then
output=$(python "$f" 2>&1) || {
if ! echo "$output" | grep -q "TypeError"; then
echo "$output"
exit 1
fi
echo "Ignoring TypeError in $f"
}
echo "$output"
else
python "$f"
fi
done
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Documentation](https://github.com/kokkos/pykokkos/actions/workflows/documentation.yml/badge.svg)](https://github.com/kokkos/pykokkos/actions/workflows/documentation.yml)
[![Linux](https://github.com/kokkos/pykokkos/actions/workflows/build_linux.yml/badge.svg)](https://github.com/kokkos/pykokkos/actions/workflows/build_linux.yml)
[![MacOS](https://github.com/kokkos/pykokkos/actions/workflows/build_macos.yml/badge.svg)](https://github.com/kokkos/pykokkos/actions/workflows/build_macos.yml)
[![examples/kokkos](https://github.com/kokkos/pykokkos/actions/workflows/examples_kokkos.yml/badge.svg)](https://github.com/kokkos/pykokkos/actions/workflows/examples_kokkos.yml)

PyKokkos is a framework for writing high-performance Python code
similar to Numba. In contrast to Numba, PyKokkos kernels are primarily
Expand Down
1 change: 0 additions & 1 deletion examples/kokkos/01_hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def hello(i: int):

def main():
N: int = 10
pk.set_default_space(pk.ExecutionSpace.OpenMP)
pk.parallel_for(N, hello)


Expand Down
13 changes: 4 additions & 9 deletions examples/kokkos/01_hello_world_lambda.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import pykokkos as pk


@pk.workload
class HelloWorld:
def __init__(self, n):
self.N: int = n

@pk.main
def run(self):
pk.parallel_for(self.N, lambda i: pk.printf("Hello from i = %i\n", i))
def main():
N: int = 10
pk.parallel_for(N, lambda i: pk.printf("Hello from i = %d\n", i))


if __name__ == "__main__":
pk.execute(pk.ExecutionSpace.OpenMP, HelloWorld(10))
main()
1 change: 0 additions & 1 deletion examples/kokkos/02_simple_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def squaresum(i: int, acc, values):

def main():
N: int = 10
pk.set_default_space(pk.ExecutionSpace.OpenMP)

# Create array with squares
values = np.array([i * i for i in range(N)], dtype=np.int32)
Expand Down
17 changes: 5 additions & 12 deletions examples/kokkos/02_simple_reduce_lambda.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import pykokkos as pk


@pk.workload
class SquareSum:
def __init__(self, n):
self.N: int = n
self.total: int = 0
def main():
N: int = 10

@pk.main
def run(self):
self.total = pk.parallel_reduce(self.N, lambda i, acc: acc + i * i)
total = pk.parallel_reduce(N, lambda i, acc: acc + i * i)

@pk.callback
def results(self):
print("Sum:", self.total)
print("Sum:", total)


if __name__ == "__main__":
pk.execute(pk.ExecutionSpace.OpenMP, SquareSum(10))
main()
1 change: 0 additions & 1 deletion examples/kokkos/03_simple_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def my_reduction(i: int, accumulator: pk.Acc[pk.double], a: pk.View2D[pk.int32])

def main():
N: int = 10
pk.set_default_space(pk.ExecutionSpace.OpenMP)

a: pk.View2D[pk.int32] = pk.View([N, 3], pk.int32)

Expand Down
38 changes: 14 additions & 24 deletions examples/kokkos/03_simple_view_lambda.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import numpy as np
import pykokkos as pk


@pk.workload
class SimpleView:
def __init__(self, n):
self.N: int = n
self.total: int = 0
self.a: pk.View2D[pk.int32] = pk.View([self.N, 3], pk.int32)
def main():
N = 10

@pk.callback
def results(self):
for row in self.a:
print(row)
print("\nResult is", self.total)
i = np.arange(1, N + 1, dtype=np.int32)
j = np.arange(1, 3 + 1, dtype=np.int32)
a = i.reshape(-1, 1) ** j.reshape(1, -1)
print(a)

@pk.main
def run(self):
pk.parallel_for(self.N, self.initialize_view)
self.total = pk.parallel_reduce(
self.N,
lambda i, accumulator: accumulator
+ self.a[i][0] * self.a[i][1] / (self.a[i][2]),
)
total: int = pk.parallel_reduce(
N, lambda u, acc: acc + a[i][0] * a[i][1] / a[i][2], a=a
)

@pk.workunit
def initialize_view(self, i: int):
for j in range(3):
self.a[i][j] = (i + 1) ** (j + 1)
for row in a:
print(row)
print("\nResult is", total)


if __name__ == "__main__":
pk.execute(pk.ExecutionSpace.OpenMP, SimpleView(10))
main()
1 change: 0 additions & 1 deletion examples/kokkos/04_simple_memoryspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def reduction(i: int, acc: pk.Acc[pk.double], a: pk.View2D[pk.int32]):

def main():
N: int = 10
pk.set_default_space(pk.ExecutionSpace.OpenMP)

a: pk.View2D[pk.int32] = pk.View([N, 3], pk.int32)

Expand Down
35 changes: 13 additions & 22 deletions examples/kokkos/04_simple_memoryspaces_lambda.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import numpy as np
import pykokkos as pk


@pk.workload
class SimpleSpaces:
def __init__(self, n):
self.N: int = n
self.sum: int = 0
self.a: pk.View2D[pk.int32] = pk.View([n, 3], pk.int32)
for i in range(n):
for j in range(3):
self.a[i][j] = i * n + j
def main():
N = 10

@pk.main
def run(self):
self.sum = pk.parallel_reduce(
self.N,
lambda i, accumulator: accumulator
+ self.a[i][0]
- self.a[i][1]
+ self.a[i][2],
)
# Initialize the array
i = np.arange(N, dtype=np.int32)
j = np.arange(3, dtype=np.int32)
a = i.reshape(-1, 1) * N + j.reshape(1, -1)

@pk.callback
def use_results(self):
print(self.sum)
sum_result = pk.parallel_reduce(
N, lambda i, acc: acc + a[i][0] - a[i][1] + a[i][2], a=a
)

print(sum_result)


if __name__ == "__main__":
pk.execute(pk.ExecutionSpace.OpenMP, SimpleSpaces(10))
main()
1 change: 0 additions & 1 deletion examples/kokkos/05_simple_atomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def findprimes(

def simple_atomics():
N: int = 100
pk.set_default_space(pk.ExecutionSpace.OpenMP)

data: pk.View1D[pk.int32] = pk.View([N], pk.int32)
result: pk.View1D[pk.int32] = pk.View([N], pk.int32)
Expand Down
2 changes: 2 additions & 0 deletions examples/kokkos/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
This directory contains examples translated from the main Kokkos repository:
https://github.com/kokkos/kokkos/tree/develop/example/tutorial

Except for `inclusive_scan_team_cuda.py`, each example executes on the default Kokkos execution space specified in the Kokkos build
1 change: 0 additions & 1 deletion examples/kokkos/add1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def add1(i: int, a: pk.View1D[pk.int32]):
def main():
n: int = 100 * 1000
N: int = n
pk.set_default_space(pk.ExecutionSpace.OpenMP)

a: pk.View1D[pk.int32] = pk.View([N], pk.int32)

Expand Down
32 changes: 15 additions & 17 deletions examples/kokkos/add1_lambda.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import numpy as np
import pykokkos as pk


@pk.workload
class AddOne:
def __init__(self, n):
self.N: int = n
self.a: pk.View1D[pk.int32] = pk.View([n], pk.int32)
@pk.workunit
def add1(i: int, a: pk.View1D[pk.int32]):
a[i] += 1

for i in range(self.N):
self.a[i] = 2
print(f"Initialized view: [{self.a[0]}, ... repeats {n-1} times]")

@pk.main
def run(self):
y: int = 1
pk.parallel_for(self.N, lambda i: self.a[i] + y, self.a)
def main():
n = 100 * 1000
N = n

# Initialize the array
a = 2 * np.ones(N, dtype=np.int32)
print(f"Initialized view: [{a[0]}, ... repeats {n-1} times]")

@pk.callback
def results(self):
print(f"Results: [{self.a[0]}, ... repeats {n-1} times]")
pk.parallel_for(N, lambda i: a[i] + 1, a=a)

print(f"Results: [{a[0]}, ... repeats {n-1} times]")


if __name__ == "__main__":
n = 100 * 1000
pk.execute(pk.ExecutionSpace.OpenMP, AddOne(n))
main()
4 changes: 2 additions & 2 deletions examples/kokkos/inclusive_scan_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def main():
num_teams = (N + team_size - 1) // team_size

view = np.zeros([N], dtype=np.int32)
p_init = pk.RangePolicy(pk.ExecutionSpace.OpenMP, 0, N)
p_init = pk.RangePolicy(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}")

team_policy = pk.TeamPolicy(pk.ExecutionSpace.OpenMP, num_teams, team_size)
team_policy = pk.TeamPolicy(num_teams, team_size)

print("Running kernel...")
pk.parallel_for(team_policy, team_scan, view=view, team_size=team_size)
Expand Down
1 change: 0 additions & 1 deletion examples/kokkos/math_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def my_calculation(i: int, a: pk.View1D[pk.int32], N: int):
def main():
n: int = 10
N: int = n
pk.set_default_space(pk.ExecutionSpace.OpenMP)

a: pk.View1D[pk.int32] = pk.View([N], pk.int32)

Expand Down
1 change: 0 additions & 1 deletion examples/kokkos/matrix_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def final_sum(i: int, accumulator: pk.Acc[pk.double], mat: pk.View2D[pk.int32]):
def main():
r: int = 5
c: int = 10
pk.set_default_space(pk.ExecutionSpace.OpenMP)

mat: pk.View2D[pk.int32] = pk.View([r, c], pk.int32)

Expand Down
1 change: 0 additions & 1 deletion examples/kokkos/random_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def my_reduction(i: int, accumulator: pk.Acc[pk.int32], a: pk.View1D[pk.int32]):
def main():
n: int = 10
N: int = n
pk.set_default_space(pk.ExecutionSpace.OpenMP)

a: pk.View1D[pk.int32] = pk.View([N], pk.int32)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run() -> None:
N = 10

A = np.zeros([N], dtype=np.int32)
p = pk.RangePolicy(pk.ExecutionSpace.OpenMP, 0, N)
p = pk.RangePolicy(0, N)
pk.parallel_for(p, init, view=A)

timer = pk.Timer()
Expand Down
34 changes: 0 additions & 34 deletions examples/kokkos/scan_functor.py

This file was deleted.

Loading
Loading