Matrix Copy
91 字
1 分钟
Matrix Copy
题面

答案
#include <cuda_runtime.h>
__global__ void copy_matrix_kernel(const float* A, float* B, int total) { int idx = blockDim.x * blockIdx.x + threadIdx.x; if (idx > total) { return; } if (idx < total) { B[idx] = A[idx]; }}
// A, B are device pointers (i.e. pointers to memory on the GPU)extern "C" void solve(const float* A, float* B, int N) { int total = N * N; int threadsPerBlock = 256; int blocksPerGrid = (total + threadsPerBlock - 1) / threadsPerBlock; copy_matrix_kernel<<<blocksPerGrid, threadsPerBlock>>>(A, B, total); cudaDeviceSynchronize();}


