Skip to content

Instantly share code, notes, and snippets.

@python273
Created March 24, 2023 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save python273/7fb5a4bece3b3b1d609f4bb237588703 to your computer and use it in GitHub Desktop.
Save python273/7fb5a4bece3b3b1d609f4bb237588703 to your computer and use it in GitHub Desktop.
import time
import numpy as np
import pyopencl as cl
def main():
bytes_size = 10*1024*1024*1024
num_of_floats = bytes_size // 4
devices = sum([x.get_devices(device_type=cl.device_type.GPU) for x in cl.get_platforms()], [])
device = devices[0]
print(device)
ctx = cl.Context(devices=[device])
buf = cl.Buffer(ctx, cl.mem_flags.READ_WRITE, bytes_size)
arr = np.zeros(num_of_floats, dtype=np.float32)
arr[:] = 0.1337
print(f"size of arr: {arr.nbytes / 1024 / 1024 / 1024:.2f} GB")
time.sleep(1)
print('start copying', flush=True)
start = time.perf_counter_ns()
with cl.CommandQueue(ctx) as queue:
cl.enqueue_copy(queue, buf, arr)
cl.enqueue_barrier(queue)
end = time.perf_counter_ns()
print('end copying', flush=True)
print(f"copy from numpy to pyopencl: {end - start} ns")
b = (bytes_size / 1024 / 1024 / 1024) / ((end - start) / 1e9)
print(f"bandwith: {b:.2f} GB/s")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment