-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GPU] Global Buffer manager and optimization #2816
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 Debadri Samaddar <[email protected]> | ||
* | ||
* @file cl_buffer_manager.cpp | ||
* @date 01 Dec 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author Debadri Samaddar <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This file contains global Buffer objects and manages them | ||
*/ | ||
|
||
#include <cl_buffer_manager.h> | ||
|
||
namespace nntrainer { | ||
|
||
ClBufferManager &ClBufferManager::getInstance() { | ||
static ClBufferManager instance; | ||
return instance; | ||
} | ||
|
||
// to-do: Implementation to be updated with array of Buffer objects if required | ||
// fp16 Buffer objects to be added in future | ||
void ClBufferManager::initBuffers() { | ||
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 inBufferA; | ||
delete inBufferB; | ||
delete inBufferC; | ||
delete outBufferA; | ||
delete outBufferB; | ||
ml_logi("ClBufferManager: Buffers destroyed"); | ||
} | ||
|
||
} // namespace nntrainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 Debadri Samaddar <[email protected]> | ||
* | ||
* @file cl_buffer_manager.h | ||
* @date 01 Dec 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author Debadri Samaddar <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This file contains global Buffer objects and manages them | ||
*/ | ||
|
||
#ifndef __CL_BUFFER_MANAGER_H__ | ||
#define __CL_BUFFER_MANAGER_H__ | ||
|
||
#include <string> | ||
|
||
#include <opencl_buffer.h> | ||
#include <opencl_context_manager.h> | ||
|
||
#include <nntrainer_log.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class ClBufferManager contains Buffer object management | ||
* @brief Support for Buffer management | ||
*/ | ||
|
||
class ClBufferManager { | ||
|
||
private: | ||
/** | ||
* @brief Private constructor to prevent object creation | ||
* | ||
*/ | ||
ClBufferManager() : | ||
inBufferA(nullptr), | ||
inBufferB(nullptr), | ||
inBufferC(nullptr), | ||
outBufferA(nullptr), | ||
outBufferB(nullptr){}; | ||
|
||
/** | ||
* @brief OpenCl context global instance | ||
* | ||
*/ | ||
opencl::ContextManager &context_inst_ = opencl::ContextManager::GetInstance(); | ||
|
||
/** | ||
* @brief Buffer size in bytes preset (256 mebibytes) | ||
*/ | ||
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: | ||
/** | ||
* @brief Get Global ClBufferManager. | ||
* | ||
* @return ClBufferManager& | ||
*/ | ||
static ClBufferManager &getInstance(); | ||
|
||
/** | ||
* @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. | ||
* | ||
*/ | ||
~ClBufferManager(); | ||
}; | ||
} // namespace nntrainer | ||
|
||
#endif /* __CL_BUFFER_MANAGER_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you gurantee that initBuffers() is called before any possible call to ClBufferManager::some-access-funtion?
Is there a reason not to do it at the constructor of ClBufferManager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
initBuffers()
will be called at the beginning when context is initialized before anyClBufferManager
member is accessed.The main reason of not doing it inside the constructor is to enforce lazy loading of the buffers. Otherwise buffers might get initialized before OpenCL context and command queue is initialized which might result in undefined behaviour when trying to read/write into the buffers. To avoid such ambiguity, it was removed from the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To guarantee the calling sequence (other memer functions are called after
initBuffers()
), it should not depend on human programmer's plans, comments, documents, or promises. It is still prone to human errors (especially with open source contributors). It should be guaranteed by the code so that another developer who doesn't know about this in the open source community won't be able to reverse the sequence by mistake, thinking that this object is initialized already with getInstance or constructor.At least, set the buffer pointers NULL at the constructor so that the caller is guaranteed to know that something's wrong if init wasn't called. And state (doxygen for potentially harmful member functions) that it will return NULL if init is not called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your insights. I have added initializers on the constructor and added relevant doc for the member functions.