High-performance neural network implementation with CUDA acceleration
#include "tensor.h"
#include <cuda_runtime.h>
__global__ void matmul_kernel(float* A, float* B, float* C,
int M, int N, int K) {
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
if (row < M && col < N) {
float sum = 0.0f;
for (int k = 0; k < K; k++) {
sum += A[row * K + k] * B[k * N + col];
}
C[row * N + col] = sum;
}
}
Tensor Tensor::matmul(const Tensor& other) {
Tensor result(rows_, other.cols_);
matmul_kernel<<<grid, block>>>(
data_, other.data_, result.data_,
rows_, other.cols_, shared_dim_
);
return result;
}