Matrix Addition
94 字
1 分钟
Matrix Addition
Matrix Addition
题面

答案
#include <cuda_runtime.h>
__global__ void matrix_add(const float* A, const float* B, float* C, int N) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N * N) { C[i] = A[i] + B[i]; }}
// A, B, C are device pointers (i.e. pointers to memory on the GPU)extern "C" void solve(const float* A, const float* B, float* C, int N) { int threadsPerBlock = 256; int blocksPerGrid = (N * N + threadsPerBlock - 1) / threadsPerBlock;
matrix_add<<<blocksPerGrid, threadsPerBlock>>>(A, B, C, N); cudaDeviceSynchronize();}Matrix Addition
https://dongyanzhang.com/posts/leetgpu/matrix-addition/


