From 3ac94042113a7ee407902ff27c529f801a682c55 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 data size Signed-off-by: Debadri Samaddar --- nntrainer/cl_buffer_manager.cpp | 20 ++++---- nntrainer/cl_buffer_manager.h | 51 ++++++++++++++++--- .../tensor/cl_operations/blas_kernels.cpp | 18 +++---- 3 files changed, 62 insertions(+), 27 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..9e4ea2b9fa 100644 --- a/nntrainer/cl_buffer_manager.h +++ b/nntrainer/cl_buffer_manager.h @@ -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 @@ -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: /** @@ -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. * 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;