Skip to content

Commit b254862

Browse files
committed
Add vector_add_observers_pmt example
1 parent cae377f commit b254862

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
"""This is the minimal example from the README"""
3+
4+
import json
5+
from collections import OrderedDict
6+
7+
import numpy
8+
from kernel_tuner import tune_kernel
9+
from kernel_tuner.observers.pmt import PMTObserver
10+
11+
def tune():
12+
13+
kernel_string = """
14+
__global__ void vector_add(float *c, float *a, float *b, int n) {
15+
int i = blockIdx.x * block_size_x + threadIdx.x;
16+
if (i<n) {
17+
c[i] = a[i] + b[i];
18+
}
19+
}
20+
"""
21+
22+
size = 80000000
23+
24+
a = numpy.random.randn(size).astype(numpy.float32)
25+
b = numpy.random.randn(size).astype(numpy.float32)
26+
c = numpy.zeros_like(b)
27+
n = numpy.int32(size)
28+
29+
args = [c, a, b, n]
30+
31+
tune_params = dict()
32+
tune_params["block_size_x"] = [128+64*i for i in range(15)]
33+
34+
pmtobserver = PMTObserver(["nvml", "rapl"])
35+
36+
metrics = OrderedDict()
37+
metrics["GPU W"] = lambda p: p["nvml_power"]
38+
metrics["CPU W"] = lambda p: p["rapl_power"]
39+
40+
results, env = tune_kernel("vector_add", kernel_string, size, args, tune_params, observers=[pmtobserver], metrics=metrics, iterations=32)
41+
42+
with open("vector_add.json", 'w') as fp:
43+
json.dump(results, fp)
44+
45+
return results
46+
47+
48+
if __name__ == "__main__":
49+
tune()

0 commit comments

Comments
 (0)