提交 c6175207 编写于 作者: Y Yi Wang

In response to comments from Liao Gang and Yu Yang

上级 8cfa48dc
...@@ -25,14 +25,16 @@ cout << memory::Used(pl); ...@@ -25,14 +25,16 @@ cout << memory::Used(pl);
memory::Free(pl, p); memory::Free(pl, p);
``` ```
### The API ### API
In `paddle/memory/memory.h` we have: In `paddle/memory/memory.h` we have:
```cpp ```cpp
template <typeanme Place> void* Alloc(Place, size_t); namespace memory {
template <typeanme Place> void Free(Place, void*); template <typename Place> void* Alloc(Place, size_t);
} template <typename Place> void Free(Place, void*);
template <typename Place> void Used(Place);
} // namespace memory
``` ```
These function templates have specializations on either `platform::CPUPlace` or `platform::GPUPlace`: These function templates have specializations on either `platform::CPUPlace` or `platform::GPUPlace`:
...@@ -48,12 +50,14 @@ and ...@@ -48,12 +50,14 @@ and
```cpp ```cpp
template<> template<>
void Alloc(GPUPlace)(GPUPlace p, size_t size) { void Alloc<GPUPlace>(GPUPlace p, size_t size) {
return GetGPUBuddyAllocator(p.id)->Alloc(size); return GetGPUBuddyAllocator(p.id)->Alloc(size);
} }
``` ```
### The Implementation Similar specializations exist for `Free` and `Used`.
### Implementation
`GetCPUBuddyAllocator` and `GetGPUBuddyAllocator` are singletions. `GetCPUBuddyAllocator` and `GetGPUBuddyAllocator` are singletions.
...@@ -94,7 +98,7 @@ class BuddyAllocator { ...@@ -94,7 +98,7 @@ class BuddyAllocator {
private: private:
struct Block { struct Block {
size_t size; size_t size;
Blobk* left, right; Block* left, right;
}; };
... ...
}; };
...@@ -102,15 +106,15 @@ class BuddyAllocator { ...@@ -102,15 +106,15 @@ class BuddyAllocator {
#### System Allocators #### System Allocators
The `GPUAllocator` and `CPUAllocator` are calls *system allocators*. They hold information about the device, including the amount of memory has been allocated. So that we can call The `GPUAllocator` and `CPUAllocator` are calls *system allocators*. They work as the fallback allocators of `BuddyAllocator`. A system allocator holds information about a device, including the amount of memory has been allocated, so we can call
- `GPUAllocator::Used` and - `GPUAllocator::Used()` and
- `CPUAllocator::Used` - `CPUAllocator::Used()`
to get the amount of memory that has been allocated so far. to get the amount of memory that has been allocated so far.
## Why Such a Design ## Justification
I got inspiration from Majel and Caffe2, though above design look different from both. I got inspiration from Majel and Caffe2, though above design look different from both.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册