mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 09:44:28 +00:00
Fix matmul and max reduce memcheck errors
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
using namespace CUDANet;
|
using namespace CUDANet;
|
||||||
|
|
||||||
|
|
||||||
__global__ void Kernels::mat_vec_mul(
|
__global__ void Kernels::mat_vec_mul(
|
||||||
const float* __restrict__ d_matrix,
|
const float* __restrict__ d_matrix,
|
||||||
const float* __restrict__ d_vector,
|
const float* __restrict__ d_vector,
|
||||||
@@ -13,32 +12,17 @@ __global__ void Kernels::mat_vec_mul(
|
|||||||
) {
|
) {
|
||||||
int tid = blockDim.x * blockIdx.x + threadIdx.x;
|
int tid = blockDim.x * blockIdx.x + threadIdx.x;
|
||||||
|
|
||||||
__shared__ float shared[BLOCK_SIZE];
|
if (tid < h) {
|
||||||
|
|
||||||
float temp = 0.0f;
|
float temp = 0.0f;
|
||||||
|
|
||||||
#pragma unroll
|
for (unsigned int j = 0; j < w; j++) {
|
||||||
for (unsigned int i = 0; i < (w + BLOCK_SIZE - 1) / BLOCK_SIZE; i++) {
|
temp += d_matrix[tid * w + j] * d_vector[j];
|
||||||
if (i * BLOCK_SIZE + threadIdx.x < w) {
|
|
||||||
shared[threadIdx.x] = d_vector[i * BLOCK_SIZE + threadIdx.x];
|
|
||||||
} else {
|
|
||||||
shared[threadIdx.x] = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (unsigned int j = 0; j < BLOCK_SIZE; j++) {
|
|
||||||
temp += d_matrix[tid * w + i * BLOCK_SIZE + j] * shared[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
__syncthreads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d_output[tid] = temp;
|
d_output[tid] = temp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
__global__ void Kernels::vec_vec_add(
|
__global__ void Kernels::vec_vec_add(
|
||||||
const float* __restrict__ d_vector1,
|
const float* __restrict__ d_vector1,
|
||||||
const float* __restrict__ d_vector2,
|
const float* __restrict__ d_vector2,
|
||||||
@@ -52,7 +36,6 @@ __global__ void Kernels::vec_vec_add(
|
|||||||
d_output[tid] = d_vector1[tid] + d_vector2[tid];
|
d_output[tid] = d_vector1[tid] + d_vector2[tid];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
__global__ void Kernels::vec_scalar_sub(
|
__global__ void Kernels::vec_scalar_sub(
|
||||||
const float* __restrict__ d_src,
|
const float* __restrict__ d_src,
|
||||||
float* __restrict__ d_out,
|
float* __restrict__ d_out,
|
||||||
@@ -66,7 +49,6 @@ __global__ void Kernels::vec_scalar_sub(
|
|||||||
d_out[tid] = d_src[tid] - d_scalar[0];
|
d_out[tid] = d_src[tid] - d_scalar[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
__global__ void Kernels::vec_scalar_div(
|
__global__ void Kernels::vec_scalar_div(
|
||||||
const float* __restrict__ d_src,
|
const float* __restrict__ d_src,
|
||||||
float* __restrict__ d_out,
|
float* __restrict__ d_out,
|
||||||
@@ -80,7 +62,6 @@ __global__ void Kernels::vec_scalar_div(
|
|||||||
d_out[tid] = d_src[tid] / d_scalar[0];
|
d_out[tid] = d_src[tid] / d_scalar[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
__global__ void Kernels::vec_exp(
|
__global__ void Kernels::vec_exp(
|
||||||
const float* __restrict__ src,
|
const float* __restrict__ src,
|
||||||
float* __restrict__ dst,
|
float* __restrict__ dst,
|
||||||
|
|||||||
@@ -27,17 +27,7 @@ void Utils::clear(float* d_vec, const unsigned int length) {
|
|||||||
void Utils::max(float* d_vec, float* d_max, const unsigned int length) {
|
void Utils::max(float* d_vec, float* d_max, const unsigned int length) {
|
||||||
|
|
||||||
const int grid_size = (length + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
const int grid_size = (length + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||||
|
|
||||||
std::cout << "grid_size: " << grid_size << ", length: " << length << std::endl;
|
|
||||||
CUDA_CHECK(cudaGetLastError());
|
|
||||||
|
|
||||||
Kernels::max_reduce<<<grid_size, BLOCK_SIZE>>>(d_vec, d_max, length);
|
Kernels::max_reduce<<<grid_size, BLOCK_SIZE>>>(d_vec, d_max, length);
|
||||||
|
|
||||||
std::cout << "input: " << std::endl;
|
|
||||||
print_vec(d_vec, length);
|
|
||||||
std::cout << "max: " << std::endl;
|
|
||||||
print_vec(d_max, length);
|
|
||||||
|
|
||||||
CUDA_CHECK(cudaGetLastError());
|
CUDA_CHECK(cudaGetLastError());
|
||||||
|
|
||||||
int remaining = grid_size;
|
int remaining = grid_size;
|
||||||
|
|||||||
@@ -43,12 +43,12 @@ TEST(MatMulTest, MatVecMulTest) {
|
|||||||
cudaStatus = cudaMemcpy(d_vector, vector.data(), sizeof(float) * w, cudaMemcpyHostToDevice);
|
cudaStatus = cudaMemcpy(d_vector, vector.data(), sizeof(float) * w, cudaMemcpyHostToDevice);
|
||||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||||
|
|
||||||
int THREADS_PER_BLOCK = std::max(w, h);
|
int grid_size = (std::max(w, h) + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||||
int BLOCKS = 1;
|
|
||||||
|
|
||||||
CUDANet::Utils::clear(d_output, h);
|
CUDANet::Utils::clear(d_output, h);
|
||||||
|
|
||||||
CUDANet::Kernels::mat_vec_mul<<<BLOCKS, THREADS_PER_BLOCK, sizeof(float) * w>>>(d_matrix, d_vector, d_output, w, h);
|
CUDANet::Kernels::mat_vec_mul<<<grid_size, BLOCK_SIZE>>>(d_matrix, d_vector, d_output, w, h);
|
||||||
cudaStatus = cudaDeviceSynchronize();
|
cudaStatus = cudaDeviceSynchronize();
|
||||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ TEST(MatMulTest, MaxReduceTest) {
|
|||||||
cudaStatus = cudaMalloc((void**)&d_input, sizeof(float) * n);
|
cudaStatus = cudaMalloc((void**)&d_input, sizeof(float) * n);
|
||||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||||
|
|
||||||
cudaStatus = cudaMalloc((void**)&d_output, sizeof(float));
|
cudaStatus = cudaMalloc((void**)&d_output, sizeof(float) * n);
|
||||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||||
|
|
||||||
cudaStatus = cudaMemcpy(d_input, input.data(), sizeof(float) * n, cudaMemcpyHostToDevice);
|
cudaStatus = cudaMemcpy(d_input, input.data(), sizeof(float) * n, cudaMemcpyHostToDevice);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <cuda_runtime.h>
|
#include <cuda_runtime.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
TEST(ActivationTest, SoftmaxTest1) {
|
TEST(ActivationTest, SoftmaxTest1) {
|
||||||
const int inputSize = 5;
|
const int inputSize = 5;
|
||||||
cudaError_t cudaStatus;
|
cudaError_t cudaStatus;
|
||||||
@@ -39,6 +40,9 @@ TEST(ActivationTest, SoftmaxTest1) {
|
|||||||
|
|
||||||
cudaFree(d_input);
|
cudaFree(d_input);
|
||||||
cudaDeviceReset();
|
cudaDeviceReset();
|
||||||
|
|
||||||
|
cudaStatus = cudaGetLastError();
|
||||||
|
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ActivationTest, SoftmaxTest2) {
|
TEST(ActivationTest, SoftmaxTest2) {
|
||||||
|
|||||||
Reference in New Issue
Block a user