-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathvector_add_codegen.py
More file actions
43 lines (31 loc) · 1021 Bytes
/
vector_add_codegen.py
File metadata and controls
43 lines (31 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
"""This is the minimal example of using a code generator with the Kernel Tuner"""
import json
import numpy
from kernel_tuner import tune_kernel
def my_fancy_generator(params):
kernel_string = """
__global__ void vector_add(float *c, float *a, float *b, int n) {
int i = blockIdx.x * %s + threadIdx.x;
if (i<n) {
c[i] = a[i] + b[i];
}
}
""" % params["block_size_x"]
return kernel_string
def tune():
size = 10000000
a = numpy.random.randn(size).astype(numpy.float32)
b = numpy.random.randn(size).astype(numpy.float32)
c = numpy.zeros_like(b)
n = numpy.int32(size)
args = [c, a, b, n]
tune_params = dict()
tune_params["block_size_x"] = [128+64*i for i in range(15)]
result = tune_kernel("vector_add", my_fancy_generator, size, args,
tune_params, lang="CUDA")
with open("vector_add.json", 'w') as fp:
json.dump(result, fp)
return result
if __name__ == "__main__":
tune()