Skip to content

Instantly share code, notes, and snippets.

@4OH4
Created November 13, 2021 20:39
Show Gist options
  • Save 4OH4/1e881372a3b6b5dd468f5ded99633357 to your computer and use it in GitHub Desktop.
Save 4OH4/1e881372a3b6b5dd468f5ded99633357 to your computer and use it in GitHub Desktop.
Allocating memory on the GPU and copying data with PyCUDA (pt. 2)
# create Python variables
a = np.random.randn(100).astype(np.float32)
b = np.random.randn(100).astype(np.float32)
result = np.random.randn(100).astype(np.float32)
# allocate memory on GPU
a_gpu = cuda.mem_alloc(a.nbytes)
b_gpu = cuda.mem_alloc(b.nbytes)
result_gpu = cuda.mem_alloc(b.nbytes)
# copy data to GPU
cuda.memcpy_htod(a_gpu, a)
cuda.memcpy_htod(b_gpu, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment