学 GPU kernel 编程

在浏览器里写 CUDA kernel,跑在真 GPU 上。

内测中 · 仅限受邀用户

#include <cuda_runtime.h>

__global__ void vector_add(const float* a, const float* b, float* c, int N) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < N) c[i] = a[i] + b[i];
}

// a, b, c are device pointers
extern "C" void solve(const float* a, const float* b, float* c, int N) {
    int threads = 256;
    int blocks = (N + threads - 1) / threads;
    vector_add<<<blocks, threads>>>(a, b, c, N);
}