fork download
  1. # your code goes here
  2. import pyopencl as cl
  3. import numpy as np
  4. import time
  5.  
  6. kernsrc = """
  7. __kernel void foo(__global float* a){
  8. size_t gid = get_global_id(0);
  9. float s = a[gid];
  10. a[gid] = s*(sin(s)+cos(s));
  11. }
  12. """
  13.  
  14. mf = cl.mem_flags
  15. ctx = cl.create_some_context()
  16. queue = cl.CommandQueue(ctx)
  17. prg = cl.Program(ctx, kernsrc).build()
  18. sz = 134217728
  19. arr = np.random.rand(sz).astype(np.float32)
  20. res = np.zeros(sz, dtype=arr.dtype)
  21. t1a = time.time()
  22. clarr = cl.Buffer(ctx, mf.READ_WRITE| mf.COPY_HOST_PTR, hostbuf=arr)
  23. t1b = time.time()
  24. evt = prg.foo(queue, (sz,), None, clarr)
  25. evt.wait()
  26. t3a = time.time()
  27. cl.enqueue_copy(queue, res, clarr)
  28. t3b = time.time()
  29.  
  30. print("Copy to device: {0:.3f}s\nRun: {1:.3f}s\nCopy from device: {2:.3f}s".format(t1b-t1a, t3a-t1b, t3b-t3a))
Runtime error #stdin #stdout #stderr 0.02s 9984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 2, in <module>
ImportError: No module named 'pyopencl'