<jupyter_start><jupyter_text>PyCUDA: CUDA kernels Required imports.<jupyter_code>import numpy  as np
import pycuda.autoinit
from pycuda import gpuarray
from pycuda.compiler import SourceModule<jupyter_output><empty_output><jupyter_text>Using PyCUDA's `SourceModule`, it is possible to write kernels in pure CUDA.  The following kernel takes an array as input, and scales with the given factor.<jupyter_code>kernel = SourceModule(
    '''
    __global__ void scale(float *out_vec, float *in_vec, const float factor) {
        int i = threadIdx.x + blockDim.x*blockIdx.x;
        out_vec[i] = factor*in_vec[i];
    }
    '''
)
factor = 3.14<jupyter_output><empty_output><jupyter_text>Prepare the data on host and device.<jupyter_code>host_vec = np.arange(0.0, 1024.0**2, 1.0, dtype=np.float32)
gpu_vec = gpuarray.to_gpu(host_vec)
gpu_out_vec = gpuarray.empty_like(gpu_vec)<jupyter_output><empty_output><jupyter_text>Compile the kernel function.<jupyter_code>scale_func = kernel.get_function('scale')<jupyter_output><empty_output><jupyter_text>Compute and verify the result.<jupyter_code>scale_func(gpu_out_vec, gpu_vec, np.float32(factor), block=(1024, 1, 1), grid=(host_vec.shape[0]//1024, 1, 1))
np.sum(gpu_out_vec.get())
factor*host_vec.sum()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup