-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsineOfTheTimes1d.py
48 lines (41 loc) · 1.54 KB
/
sineOfTheTimes1d.py
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
44
45
46
47
48
import pyopencl as cl
import pyopencl.array as pycl_array
import numpy as np
import time
import cv2
class SineOfTheTimes1d(object):
def __init__(self):
self.context = cl.create_some_context()
self.queue = cl.CommandQueue(self.context)
self.WIDTH = 720
self.HEIGHT = 720
self.program = cl.Program(self.context, f"""
__kernel void sinOfTheTime(__global float *dest, __global const float *t)
{{
// it's 1d so we only get the x component
int x = get_global_id(0);
dest[x] = (sin(*t) * .5) + .5;
}}
""").build()
self.startTime = time.time()
self.frames = 0
def pySinOfTheTime(self):
t = pycl_array.to_device(self.queue, np.float32((time.time()) %6.2831))
# create the output array as 1d vector and reshape later
out = pycl_array.to_device(self.queue,
np.ones((self.WIDTH*self.HEIGHT*3,),
np.float32))
self.program.sinOfTheTime(self.queue, out.shape, None, out.data, t.data)
self.frames += 1
return out.get().reshape((self.WIDTH, self.HEIGHT, 3))
if __name__ == "__main__":
sot = SineOfTheTimes1d()
try:
while True:
for _ in range(30):
arr = sot.pySinOfTheTime()
cv2.imshow("foo", arr)
cv2.waitKey(1)
print(f"fps: {sot.frames / (time.time() - sot.startTime)}")
finally:
print(f"fps: {sot.frames/(time.time() - sot.startTime)}")