-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
SINGA-236 memory pool (update from an old branch) #254
Open
liyuchenmike
wants to merge
7
commits into
apache:master
Choose a base branch
from
liyuchenmike:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a792866
SINGA-236 memory pool \n Implemented the following features: \n 1. A…
liyuchenmike ac5c59f
updated the memory pool structure so that so fixed size pool is required
liyuchenmike 5ad387a
SINGA-244 Separating swig interface and python binding files
aaronwwf 07a71a8
SINGA-244 Separating swig interface and python binding files
aaronwwf 46fb1e3
SINGA-245 Float as the first operand can not multiply with a tensor o…
RUAN0007 67b338d
SINGA-245 Float as the first operand can not multiply with a tensor o…
nudles 646c804
SINGA-244 Separating swig interface and python binding files
xiezl 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
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
#include <atomic> | ||
#include "singa/proto/core.pb.h" | ||
#include "singa/singa_config.h" | ||
#include "singa/core/common.h" | ||
|
||
#ifdef USE_CUDA | ||
#include "cnmem.h" | ||
|
@@ -50,6 +51,57 @@ class DeviceMemPool { | |
// size_t init_size_ = 0, max_size_ = 0; | ||
}; | ||
|
||
class CppMemPool { | ||
public: | ||
// initial pool size (MB), and the size of each memory uint in the memory pool (KB) | ||
CppMemPool(size_t init_size_mb = 256, size_t uint_size_kb = 1); | ||
|
||
// return a new pool based on the current pool | ||
// once returned, the old pool will be invalid | ||
// re-initial with pool size (MB), and set the size of each memory uint in the memory pool (KB) | ||
void RsetMemPool(size_t init_size_mb = 256, size_t uint_size_kb = 1); | ||
|
||
// create the memory requested, if size is larger than memUintSize, malloc from system call | ||
// is_ptr_null indicate whether the pointer is null and if so we will initialize it in the malloc function, | ||
// otherwise we will use the ptr directly and access its data and functions. | ||
// after the malloc, the data pointer of the block will be changed and the orginal data pointer will be lost. | ||
void Malloc(Block** ptr, const size_t size, bool is_ptr_null = true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be simpler if the API is |
||
void Free(Block* ptr); | ||
|
||
std::pair<size_t, size_t> GetMemUsage(); | ||
size_t GetNumFreeUints(){return numUints - numAllocatedUintsInPool;}; | ||
|
||
// release all memory. | ||
// all pointers allocated in the pool must be freed before calling the descturctor. | ||
~CppMemPool(); | ||
|
||
protected: | ||
// each structure define a memory uint in the memory pool | ||
// the structure is a static double linked list | ||
struct _Uint { | ||
struct _Uint *pPrev, *pNext; | ||
Block* pBlk; | ||
}; | ||
|
||
// pointer to the memory pool | ||
void* pMemPool; | ||
|
||
// head pointer to allocated memory uint | ||
struct _Uint* pAllocatedMemUint; | ||
// head pointer to free memory uint | ||
struct _Uint* pFreeMemUint; | ||
|
||
// the size of each memory uint with/out the meta data of the uint | ||
size_t memUintSize, memUintSizeNoMeta; | ||
|
||
// the number of memory uints in the pool | ||
size_t numUints; | ||
// the number of allocated uints which are resided in the memory pool | ||
size_t numAllocatedUintsInPool; | ||
// the number of allocated uints including the ones resided outside the memory pool | ||
size_t numAllocatedUints; | ||
}; | ||
|
||
#ifdef USE_CUDA | ||
class CnMemPool : public DeviceMemPool { | ||
public: | ||
|
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.
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.
Are all memory units of the same size?
Can we do it in this way:
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.
optimizations could be added later, e.g.
OptimizeMemPool(): re-compute the total size blocks in use, and move them into contiguous memory space.