mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 09:44:28 +00:00
Initialize and use cuBLAS properly
This commit is contained in:
@@ -15,7 +15,8 @@ namespace Layers {
|
|||||||
~Dense();
|
~Dense();
|
||||||
|
|
||||||
void forward(const float* input, float* output);
|
void forward(const float* input, float* output);
|
||||||
void to_cuda();
|
virtual void setWeights(const std::vector<std::vector<float>>& weights) = 0;
|
||||||
|
virtual void setBiases(const std::vector<float>& biases) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int inputSize;
|
int inputSize;
|
||||||
@@ -31,6 +32,7 @@ namespace Layers {
|
|||||||
|
|
||||||
void initializeWeights();
|
void initializeWeights();
|
||||||
void initializeBiases();
|
void initializeBiases();
|
||||||
|
void toCuda();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Layers
|
} // namespace Layers
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace Layers {
|
|||||||
virtual ~ILayer() {}
|
virtual ~ILayer() {}
|
||||||
|
|
||||||
virtual void forward(const float* input, float* output) = 0;
|
virtual void forward(const float* input, float* output) = 0;
|
||||||
virtual void to_cuda() = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Layers
|
} // namespace Layers
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define CUDA_HELPER_H
|
#define CUDA_HELPER_H
|
||||||
|
|
||||||
#include <cuda_runtime.h>
|
#include <cuda_runtime.h>
|
||||||
|
#include <cublas_v2.h>
|
||||||
|
|
||||||
#define IDX2C(i,j,ld) (((j)*(ld))+(i))
|
#define IDX2C(i,j,ld) (((j)*(ld))+(i))
|
||||||
|
|
||||||
@@ -17,7 +18,15 @@ do { \
|
|||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
// Initialize CUDA and return the device properties
|
// cuBLAS error checking macro
|
||||||
cudaDeviceProp initializeCUDA();
|
#define CUBLAS_CHECK(call) \
|
||||||
|
do { \
|
||||||
|
cublasStatus_t result = call; \
|
||||||
|
if (result != CUBLAS_STATUS_SUCCESS) { \
|
||||||
|
fprintf(stderr, "cuBLAS error at %s:%d code=%d\n", \
|
||||||
|
__FILE__, __LINE__, static_cast<int>(result)); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#endif // CUDA_HELPER_H
|
#endif // CUDA_HELPER_H
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ Layers::Dense::Dense(int inputSize, int outputSize, cublasHandle_t cublasHandle)
|
|||||||
CUDA_CHECK(cudaMalloc((void**)&d_weights, sizeof(float) * inputSize * outputSize));
|
CUDA_CHECK(cudaMalloc((void**)&d_weights, sizeof(float) * inputSize * outputSize));
|
||||||
CUDA_CHECK(cudaMalloc((void**)&d_biases, sizeof(float) * biases.size()));
|
CUDA_CHECK(cudaMalloc((void**)&d_biases, sizeof(float) * biases.size()));
|
||||||
|
|
||||||
to_cuda();
|
toCuda();
|
||||||
}
|
}
|
||||||
|
|
||||||
Layers::Dense::~Dense() {
|
Layers::Dense::~Dense() {
|
||||||
@@ -54,7 +54,17 @@ void Layers::Dense::forward(const float* input, float* output) {
|
|||||||
cublasSaxpy(cublasHandle, outputSize, &alpha, d_biases, 1, output, 1);
|
cublasSaxpy(cublasHandle, outputSize, &alpha, d_biases, 1, output, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::to_cuda() {
|
void Layers::Dense::toCuda() {
|
||||||
CUDA_CHECK(cudaMemcpy(d_weights, weights.data(), sizeof(float) * inputSize * outputSize, cudaMemcpyHostToDevice));
|
CUBLAS_CHECK(cublasSetMatrix(outputSize, inputSize, sizeof(float), weights.data(), inputSize, d_weights, outputSize));
|
||||||
CUDA_CHECK(cudaMemcpy(d_biases, biases.data(), sizeof(float) * biases.size(), cudaMemcpyHostToDevice));
|
CUBLAS_CHECK(cublasSetVector(biases.size(), sizeof(float), biases.data(), 1, d_biases, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layers::Dense::setWeights(const std::vector<std::vector<float>>& weights) {
|
||||||
|
this->weights = weights;
|
||||||
|
toCuda();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layers::Dense::setBiases(const std::vector<float>& biases) {
|
||||||
|
this->biases = biases;
|
||||||
|
toCuda();
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include "cuda_helper.h"
|
#include "cuda_helper.h"
|
||||||
|
#include <cublas_v2.h>
|
||||||
|
|
||||||
// Initialize CUDA and return the device properties
|
cudaDeviceProp initializeCUDA(cublasHandle_t& cublasHandle) {
|
||||||
cudaDeviceProp initializeCUDA() {
|
|
||||||
int deviceCount;
|
int deviceCount;
|
||||||
CUDA_CHECK(cudaGetDeviceCount(&deviceCount));
|
CUDA_CHECK(cudaGetDeviceCount(&deviceCount));
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ cudaDeviceProp initializeCUDA() {
|
|||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int device = 0; // You can modify this to choose a different GPU
|
int device = 0;
|
||||||
CUDA_CHECK(cudaSetDevice(device));
|
CUDA_CHECK(cudaSetDevice(device));
|
||||||
|
|
||||||
cudaDeviceProp deviceProp;
|
cudaDeviceProp deviceProp;
|
||||||
@@ -20,5 +20,8 @@ cudaDeviceProp initializeCUDA() {
|
|||||||
|
|
||||||
std::printf("Using CUDA device %d: %s\n", device, deviceProp.name);
|
std::printf("Using CUDA device %d: %s\n", device, deviceProp.name);
|
||||||
|
|
||||||
|
// Initialize cuBLAS
|
||||||
|
CUBLAS_CHECK(cublasCreate(&cublasHandle));
|
||||||
|
|
||||||
return deviceProp;
|
return deviceProp;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user