Skip to content

Commit b4e038e

Browse files
committed
adding a method to add your own cpp functions to be executed over pytaskr
1 parent eca1824 commit b4e038e

16 files changed

Lines changed: 308 additions & 19 deletions

File tree

examples/local/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ subdir('pendingOperation')
1010
subdir('workerSpecific')
1111
subdir('manyParallel')
1212
subdir('suspend')
13-
subdir('simple')
13+
subdir('simple')
14+
subdir('mmm')

examples/local/mmm/README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ABC Tasks
2+
============
3+
4+
A simple example for creating a task dependency-based runtime system using HiCR
5+
6+
It creates and adds tasks in an arbitrary order. However, given the dependencies should force the output to always be an ordered sequence of A->B->C. This example makes sure TaskR is handling these dependencies correctly.
7+

examples/local/mmm/meson.build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
testSuite = [ 'examples', 'local', 'mmmm' ]
2+
3+
if get_option('buildPyTaskR') and get_option('buildTests')
4+
test('pyTaskR',
5+
py,
6+
args : [ 'py_source/main.py' ],
7+
is_parallel : false,
8+
env: ['PYTHONPATH=' + meson.project_build_root() + '/include/pytaskr/'],
9+
suite: testSuite,
10+
workdir: meson.current_source_dir())
11+
endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Copyright 2025 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import taskr
18+
# from mmm import mmmDriver
19+
from mmm_cpp import mmm_cpp_Driver
20+
21+
def main():
22+
# Initialize taskr with the wanted compute manager backend and number of PUs
23+
t = taskr.taskr(taskr.HiCRBackend.nosv, 2)
24+
25+
# Get the runtime
26+
runtime = t.get_runtime()
27+
28+
# Running mmm example
29+
# mmmDriver(runtime)
30+
mmm_cpp_Driver(runtime)
31+
32+
if __name__ == "__main__":
33+
main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Copyright 2025 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import time
18+
import numpy as np
19+
import taskr
20+
21+
NTASKS = 2
22+
23+
N = 1000
24+
25+
def mmm(task):
26+
A = np.zeros((N,N))
27+
B = np.empty((N,N))
28+
C = np.empty((N,N))
29+
30+
for i in range(N):
31+
for j in range(N):
32+
B[i, j] = i
33+
C[i, j] = j
34+
35+
B += task.getLabel()+1
36+
C += task.getLabel()+1
37+
38+
A = B @ C
39+
print(f"Hello, I am task {task.getLabel()}")
40+
41+
42+
def mmmDriver(runtime):
43+
# Initializing taskr
44+
runtime.initialize()
45+
46+
taskfc = taskr.Function(mmm)
47+
48+
# Adding to tasks to taskr
49+
for i in range(NTASKS):
50+
runtime.addTask(taskr.Task(i, taskfc, i))
51+
52+
# Running taskr for the current repetition
53+
t_start = time.time()
54+
runtime.run()
55+
56+
# Waiting current repetition to end
57+
runtime.await_()
58+
t_duration = time.time() - t_start
59+
60+
print(f"total time: {t_duration}s")
61+
62+
# Finalizing taskr
63+
runtime.finalize()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Copyright 2025 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import time
18+
import taskr
19+
20+
NTASKS = 2
21+
22+
def mmm_cpp_Driver(runtime):
23+
# Initializing taskr
24+
runtime.initialize()
25+
26+
taskfc = taskr.get_cpp_function("cpp_mmm")
27+
28+
# Adding to tasks to taskr
29+
for i in range(NTASKS):
30+
runtime.addTask(taskr.Task(i, taskfc))
31+
32+
# Running taskr for the current repetition
33+
t_start = time.time()
34+
runtime.run()
35+
36+
# Waiting current repetition to end
37+
runtime.await_()
38+
print(f"total time: {time.time() - t_start}")
39+
40+
# Finalizing taskr
41+
runtime.finalize()

examples/local/simple/py_source/simple.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
import ctypes
17-
18-
libc = ctypes.CDLL("libc.so.6")
19-
libc.sched_getcpu.restype = ctypes.c_int
2016

2117
import taskr
2218

include/pytaskr/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ py = import('python').find_installation(pure: false)
66
pybind11_dep = dependency('pybind11', required: true)
77

88
py.extension_module('taskr',
9-
'pytaskr.cpp',
9+
['pytaskr.cpp', 'mmm.cpp'],
1010
install: true,
1111
dependencies : [TaskRBuildDep, pybind11_dep],
1212
)

include/pytaskr/mmm.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2025 Huawei Technologies Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <chrono>
18+
19+
#include <taskr/taskr.hpp>
20+
#include <pytaskr/pytaskr.hpp>
21+
22+
#define mytype float
23+
24+
#define NTASKS 2
25+
26+
/**
27+
* Compute mmm locally
28+
*/
29+
void mmm(taskr::Task *) {
30+
const size_t N = 1000;
31+
32+
// Allocate memory
33+
volatile mytype *A = (mytype *)calloc(1, N * N * sizeof(mytype));
34+
volatile mytype *B = (mytype *)malloc(N * N * sizeof(mytype));
35+
volatile mytype *C = (mytype *)malloc(N * N * sizeof(mytype));
36+
37+
// Filling matrices B and C
38+
for (size_t i = 0; i < N; ++i) {
39+
for (size_t j = 0; j < N; ++j) {
40+
B[i * N + j] = 1.0/(mytype(i + 1));
41+
C[i * N + j] = 1.0/(mytype(j + 1));
42+
}
43+
}
44+
45+
// mmm
46+
for (size_t i = 0; i < N; ++i) {
47+
for (size_t j = 0; j < N; ++j) {
48+
for (size_t k = 0; k < N; ++k) {
49+
A[i * N + j] += B[i * N + k] * C[k * N + j];
50+
}
51+
}
52+
}
53+
54+
// free memory
55+
free((mytype*)A);
56+
free((mytype*)B);
57+
free((mytype*)C);
58+
}
59+
60+
namespace taskr {
61+
62+
__attribute__((constructor)) // GCC/Clang: Run before main/init
63+
void register_my_func() {
64+
register_function("cpp_mmm", mmm);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)