diff --git a/lec5/cuda_example.py b/lec5/cuda_example.py new file mode 100644 index 0000000..3783c5f --- /dev/null +++ b/lec5/cuda_example.py @@ -0,0 +1,17 @@ +import cupy as cp + +source = r""" +extern "C" __global__ +void saxpy(int n, float a, const float* x, float* y) { + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < n) { + y[i] = a * x[i] + y[i]; + } +} +""" + +saxpy = cp.RawKernel(source, "saxpy") +n = x.size # x, y — массивы float32 на устройстве +threads = 256 +blocks = (n + threads - 1) // threads +saxpy((blocks,), (threads,), (cp.int32(n), cp.float32(2.0), x, y)) \ No newline at end of file