Skip to content

Instantly share code, notes, and snippets.

@4OH4
Created November 13, 2021 20:29
Show Gist options
  • Save 4OH4/d8c34db0a2326b5d9a84a38ef722fdbf to your computer and use it in GitHub Desktop.
Save 4OH4/d8c34db0a2326b5d9a84a38ef722fdbf to your computer and use it in GitHub Desktop.
Creating a CUDA function with PyCUDA (pt. 1)
import numpy as np
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
# Define our function using CUDA code
cuda_func_def = """
__global__ void multiply(float *result, float *a, float *b)
{
const int i = threadIdx.x;
result[i] = a[i] * b[i];
}
"""
# Create CUDA module and import into Python
mod = SourceModule(cuda_func_def)
multiply_func = mod.get_function("multiply")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment