Skip to content

Commit

Permalink
[GPU] Abstraction of cl_buffer_manager
Browse files Browse the repository at this point in the history
Adding abstraction in cl_buffer_manager
using const for data size

Signed-off-by: Debadri Samaddar <[email protected]>
  • Loading branch information
s-debadri authored and myungjoo committed Dec 23, 2024
1 parent e958157 commit 3ac9404
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
20 changes: 10 additions & 10 deletions nntrainer/cl_buffer_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ ClBufferManager &ClBufferManager::getInstance() {
// to-do: Implementation to be updated with array of Buffer objects if required
// fp16 Buffer objects to be added in future
void ClBufferManager::initBuffers() {
readBufferA = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
readBufferB = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
readBufferC = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
writeBufferA = new opencl::Buffer(context_inst_, buffer_size_bytes, false);
writeBufferB = new opencl::Buffer(context_inst_, buffer_size_bytes, false);
inBufferA = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
inBufferB = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
inBufferC = new opencl::Buffer(context_inst_, buffer_size_bytes, true);
outBufferA = new opencl::Buffer(context_inst_, buffer_size_bytes, false);
outBufferB = new opencl::Buffer(context_inst_, buffer_size_bytes, false);
ml_logi("ClBufferManager: Buffers initialized");
}

ClBufferManager::~ClBufferManager() {
delete readBufferA;
delete readBufferB;
delete readBufferC;
delete writeBufferA;
delete writeBufferB;
delete inBufferA;
delete inBufferB;
delete inBufferC;
delete outBufferA;
delete outBufferB;
ml_logi("ClBufferManager: Buffers destroyed");
}

Expand Down
51 changes: 43 additions & 8 deletions nntrainer/cl_buffer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class ClBufferManager {
* @brief Private constructor to prevent object creation
*
*/
ClBufferManager(){};
ClBufferManager() :
inBufferA(nullptr),
inBufferB(nullptr),
inBufferC(nullptr),
outBufferA(nullptr),
outBufferB(nullptr){};

/**
* @brief OpenCl context global instance
Expand All @@ -45,7 +50,13 @@ class ClBufferManager {
/**
* @brief Buffer size in bytes preset (256 mebibytes)
*/
size_t buffer_size_bytes = 8192 * 8192 * sizeof(float);
const size_t buffer_size_bytes = 8192 * 8192 * sizeof(float);

opencl::Buffer *inBufferA;
opencl::Buffer *inBufferB;
opencl::Buffer *inBufferC;
opencl::Buffer *outBufferA;
opencl::Buffer *outBufferB;

public:
/**
Expand All @@ -55,17 +66,41 @@ class ClBufferManager {
*/
static ClBufferManager &getInstance();

opencl::Buffer *readBufferA;
opencl::Buffer *readBufferB;
opencl::Buffer *readBufferC;
opencl::Buffer *writeBufferA;
opencl::Buffer *writeBufferB;

/**
* @brief Initialize Buffer objects.
*/
void initBuffers();

/**
* @brief Get read only inBufferA.
* @return opencl::Buffer* or nullptr if initBuffers() is not called
*/
opencl::Buffer *getInBufferA() { return inBufferA; }

/**
* @brief Get read only inBufferB.
* @return opencl::Buffer* or nullptr if initBuffers() is not called
*/
opencl::Buffer *getInBufferB() { return inBufferB; }

/**
* @brief Get read only inBufferC.
* @return opencl::Buffer* or nullptr if initBuffers() is not called
*/
opencl::Buffer *getInBufferC() { return inBufferC; }

/**
* @brief Get read-write outBufferA.
* @return opencl::Buffer* or nullptr if initBuffers() is not called
*/
opencl::Buffer *getOutBufferA() { return outBufferA; }

/**
* @brief Get read-write outBufferB.
* @return opencl::Buffer* or nullptr if initBuffers() is not called
*/
opencl::Buffer *getOutBufferB() { return outBufferB; }

/**
* @brief Destroy Buffer pointers.
*
Expand Down
18 changes: 9 additions & 9 deletions nntrainer/tensor/cl_operations/blas_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,39 @@ void sgemv_cl(const float *matAdata, const float *vecXdata, float *vecYdata,
size_t dim1_size = sizeof(float) * dim1;
size_t dim2_size = sizeof(float) * dim2;

result = clbuffInstance.readBufferA->WriteDataRegion(
result = clbuffInstance.getInBufferA()->WriteDataRegion(
cl_context_ref.command_queue_inst_, dim1 * dim2 * sizeof(float),
matAdata);
if (!result) {
break;
}

result = clbuffInstance.readBufferB->WriteDataRegion(
result = clbuffInstance.getInBufferB()->WriteDataRegion(
cl_context_ref.command_queue_inst_, dim2_size, vecXdata);
if (!result) {
break;
}

result = clbuffInstance.writeBufferA->WriteDataRegion(
result = clbuffInstance.getOutBufferA()->WriteDataRegion(
cl_context_ref.command_queue_inst_, dim1_size, vecYdata);
if (!result) {
break;
}

result = kernel_sgemv_ptr->SetKernelArguments(0, clbuffInstance.readBufferA,
sizeof(cl_mem));
result = kernel_sgemv_ptr->SetKernelArguments(
0, clbuffInstance.getInBufferA(), sizeof(cl_mem));
if (!result) {
break;
}

result = kernel_sgemv_ptr->SetKernelArguments(1, clbuffInstance.readBufferB,
sizeof(cl_mem));
result = kernel_sgemv_ptr->SetKernelArguments(
1, clbuffInstance.getInBufferB(), sizeof(cl_mem));
if (!result) {
break;
}

result = kernel_sgemv_ptr->SetKernelArguments(
2, clbuffInstance.writeBufferA, sizeof(cl_mem));
2, clbuffInstance.getOutBufferA(), sizeof(cl_mem));
if (!result) {
break;
}
Expand All @@ -96,7 +96,7 @@ void sgemv_cl(const float *matAdata, const float *vecXdata, float *vecYdata,
break;
}

result = clbuffInstance.writeBufferA->ReadDataRegion(
result = clbuffInstance.getOutBufferA()->ReadDataRegion(
cl_context_ref.command_queue_inst_, dim1_size, vecYdata);
if (!result) {
break;
Expand Down

0 comments on commit 3ac9404

Please sign in to comment.