From cc94badd01e1a1ca7e1641633d86d2c89a3d9050 Mon Sep 17 00:00:00 2001 From: Debadri Samaddar Date: Tue, 10 Dec 2024 09:37:04 +0530 Subject: [PATCH] [GPU] Abstraction of cl_buffer_manager Adding abstraction in cl_buffer_manager using const for opencl::Buffer methods Signed-off-by: Debadri Samaddar --- nntrainer/cl_buffer_manager.cpp | 20 ++++----- nntrainer/cl_buffer_manager.h | 44 ++++++++++++++++--- nntrainer/opencl/opencl_buffer.cpp | 9 ++-- nntrainer/opencl/opencl_buffer.h | 9 ++-- .../tensor/cl_operations/blas_kernels.cpp | 18 ++++---- 5 files changed, 66 insertions(+), 34 deletions(-) diff --git a/nntrainer/cl_buffer_manager.cpp b/nntrainer/cl_buffer_manager.cpp index df5009dcc8..cf61e80100 100644 --- a/nntrainer/cl_buffer_manager.cpp +++ b/nntrainer/cl_buffer_manager.cpp @@ -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"); } diff --git a/nntrainer/cl_buffer_manager.h b/nntrainer/cl_buffer_manager.h index 99b79ef4ca..730d3c5b10 100644 --- a/nntrainer/cl_buffer_manager.h +++ b/nntrainer/cl_buffer_manager.h @@ -45,7 +45,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); + + const opencl::Buffer *inBufferA; + const opencl::Buffer *inBufferB; + const opencl::Buffer *inBufferC; + const opencl::Buffer *outBufferA; + const opencl::Buffer *outBufferB; public: /** @@ -55,17 +61,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 const opencl::Buffer* + */ + const opencl::Buffer *getInBufferA() { return inBufferA; } + + /** + * @brief Get read only inBufferB. + * @return const opencl::Buffer* + */ + const opencl::Buffer *getInBufferB() { return inBufferB; } + + /** + * @brief Get read only inBufferC. + * @return const opencl::Buffer* + */ + const opencl::Buffer *getInBufferC() { return inBufferC; } + + /** + * @brief Get read-write outBufferA. + * @return const opencl::Buffer* + */ + const opencl::Buffer *getOutBufferA() { return outBufferA; } + + /** + * @brief Get read-write outBufferB. + * @return const opencl::Buffer* + */ + const opencl::Buffer *getOutBufferB() { return outBufferB; } + /** * @brief Destroy Buffer pointers. * diff --git a/nntrainer/opencl/opencl_buffer.cpp b/nntrainer/opencl/opencl_buffer.cpp index 07ae213a94..e6b00d61a9 100644 --- a/nntrainer/opencl/opencl_buffer.cpp +++ b/nntrainer/opencl/opencl_buffer.cpp @@ -90,14 +90,14 @@ cl_mem &Buffer::GetBuffer() { return mem_buf_; } * @return true if successful write or false otherwise */ bool Buffer::WriteData(CommandQueueManager &command_queue_inst, - const void *data) { + const void *data) const { return command_queue_inst.EnqueueWriteBuffer(mem_buf_, size_, data); } bool Buffer::WriteDataRegion(CommandQueueManager &command_queue_inst, size_t size_in_bytes, const void *data, size_t host_origin_offset, - size_t buffer_origin_offset) { + size_t buffer_origin_offset) const { if (size_in_bytes > size_) { ml_loge("Failed to write buffer region. Region size(%lu bytes) greater " "than buffer size(%lu bytes).", @@ -115,14 +115,15 @@ bool Buffer::WriteDataRegion(CommandQueueManager &command_queue_inst, * @param data * @return true if successful read or false otherwise */ -bool Buffer::ReadData(CommandQueueManager &command_queue_inst, void *data) { +bool Buffer::ReadData(CommandQueueManager &command_queue_inst, + void *data) const { return command_queue_inst.EnqueueReadBuffer(mem_buf_, size_, data); } bool Buffer::ReadDataRegion(CommandQueueManager &command_queue_inst, size_t size_in_bytes, void *data, size_t host_origin_offset, - size_t buffer_origin_offset) { + size_t buffer_origin_offset) const { if (size_in_bytes > size_) { ml_loge("Failed to read from buffer region. Region size(%lu bytes) greater " "than buffer size(%lu bytes).", diff --git a/nntrainer/opencl/opencl_buffer.h b/nntrainer/opencl/opencl_buffer.h index beddb30339..8b35baeee4 100644 --- a/nntrainer/opencl/opencl_buffer.h +++ b/nntrainer/opencl/opencl_buffer.h @@ -104,7 +104,8 @@ class Buffer { * @param data * @return true if successful write or false otherwise */ - bool WriteData(CommandQueueManager &command_queue_inst, const void *data); + bool WriteData(CommandQueueManager &command_queue_inst, + const void *data) const; /** * @brief writing data to a buffer region @@ -119,7 +120,7 @@ class Buffer { bool WriteDataRegion(CommandQueueManager &command_queue_inst, size_t size_in_bytes, const void *data, size_t host_origin_offset = 0, - size_t buffer_origin_offset = 0); + size_t buffer_origin_offset = 0) const; /** * @brief reading data from the buffer @@ -128,7 +129,7 @@ class Buffer { * @param data * @return true if successful read or false otherwise */ - bool ReadData(CommandQueueManager &command_queue_inst, void *data); + bool ReadData(CommandQueueManager &command_queue_inst, void *data) const; /** * @brief Reading data from a buffer region @@ -143,7 +144,7 @@ class Buffer { bool ReadDataRegion(CommandQueueManager &command_queue_inst, size_t size_in_bytes, void *data, size_t host_origin_offset = 0, - size_t buffer_origin_offset = 0); + size_t buffer_origin_offset = 0) const; /** * @brief Mapping buffer to host memory diff --git a/nntrainer/tensor/cl_operations/blas_kernels.cpp b/nntrainer/tensor/cl_operations/blas_kernels.cpp index 3ceccbdcd3..6c7751b8b0 100644 --- a/nntrainer/tensor/cl_operations/blas_kernels.cpp +++ b/nntrainer/tensor/cl_operations/blas_kernels.cpp @@ -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; } @@ -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;