Skip to content

Commit

Permalink
[GPU] Lazy initialization of Buffers
Browse files Browse the repository at this point in the history
Initialize buffer objects after command queue creation

Signed-off-by: Debadri Samaddar <[email protected]>
  • Loading branch information
s-debadri authored and myungjoo committed Dec 23, 2024
1 parent 02493a1 commit e958157
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion nntrainer/cl_buffer_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ClBufferManager &ClBufferManager::getInstance() {

// to-do: Implementation to be updated with array of Buffer objects if required
// fp16 Buffer objects to be added in future
ClBufferManager::ClBufferManager() {
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);
Expand All @@ -36,6 +36,7 @@ ClBufferManager::~ClBufferManager() {
delete readBufferC;
delete writeBufferA;
delete writeBufferB;
ml_logi("ClBufferManager: Buffers destroyed");
}

} // namespace nntrainer
7 changes: 6 additions & 1 deletion nntrainer/cl_buffer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClBufferManager {
* @brief Private constructor to prevent object creation
*
*/
ClBufferManager();
ClBufferManager(){};

/**
* @brief OpenCl context global instance
Expand All @@ -61,6 +61,11 @@ class ClBufferManager {
opencl::Buffer *writeBufferA;
opencl::Buffer *writeBufferB;

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

/**
* @brief Destroy Buffer pointers.
*
Expand Down
8 changes: 7 additions & 1 deletion nntrainer/cl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <layer.h>
#include <layer_devel.h>

#include <cl_buffer_manager.h>
#include <opencl_command_queue_manager.h>
#include <opencl_context_manager.h>
#include <opencl_kernel.h>
Expand Down Expand Up @@ -79,12 +80,14 @@ class ClContext {

template <typename... Ts> using FactoryMap = std::tuple<IndexType<Ts>...>;

// getting static instance of commandqueue and opencl context
// getting static instance of commandqueue, opencl context and buffermanager
opencl::CommandQueueManager &command_queue_inst_ =
opencl::CommandQueueManager::GetInstance();

opencl::ContextManager &context_inst_ = opencl::ContextManager::GetInstance();

ClBufferManager &clbuffInstance = ClBufferManager::getInstance();

/**
* @brief Default constructor
*/
Expand Down Expand Up @@ -272,6 +275,9 @@ class ClContext {

// getContext() called inside createCommandQueue which creates clContext
bool result = command_queue_inst_.CreateCommandQueue();
// initialize device buffers
clbuffInstance.initBuffers();

cl_initialized = result;
return cl_initialized;
};
Expand Down

0 comments on commit e958157

Please sign in to comment.