-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
12 deletions.
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,84 @@ | ||
/* | ||
* TinyTorch | ||
* @author : [email protected] | ||
* | ||
*/ | ||
|
||
#include "Allocator.h" | ||
|
||
#include <cassert> | ||
#include <cstdlib> | ||
|
||
#include "Logger.h" | ||
|
||
namespace TinyTorch { | ||
|
||
CachedAllocator::CachedAllocator(size_t maxCacheSize) | ||
: maxCacheSize_(maxCacheSize), currentCacheSize_(0) {} | ||
|
||
CachedAllocator::~CachedAllocator() { | ||
CachedAllocator::clear(); | ||
for (auto& pair : allocatedList_) { | ||
std::free(pair.first); | ||
} | ||
} | ||
|
||
void* CachedAllocator::malloc(size_t size) { | ||
auto it = freedList_.find(size); | ||
if (it != freedList_.end() && !it->second.empty()) { | ||
void* ptr = it->second.front(); | ||
it->second.pop_front(); | ||
allocatedList_[ptr] = size; | ||
currentCacheSize_ -= size; | ||
return ptr; | ||
} | ||
|
||
void* ptr = std::malloc(size); | ||
if (ptr) { | ||
allocatedList_[ptr] = size; | ||
} else { | ||
LOGE("std::malloc failed with size: %lld", size); | ||
} | ||
return ptr; | ||
} | ||
|
||
void CachedAllocator::free(void* ptr) { | ||
auto it = allocatedList_.find(ptr); | ||
if (it != allocatedList_.end()) { | ||
size_t size = it->second; | ||
freedList_[size].push_back(ptr); | ||
allocatedList_.erase(it); | ||
currentCacheSize_ += size; | ||
|
||
shrink(); | ||
} else { | ||
LOGE("error: ptr not valid: %p", ptr); | ||
} | ||
} | ||
|
||
void CachedAllocator::clear() { | ||
assert(allocatedList_.empty()); | ||
for (auto& pair : freedList_) { | ||
for (void* ptr : pair.second) { | ||
std::free(ptr); | ||
} | ||
} | ||
} | ||
|
||
void CachedAllocator::shrink() { | ||
while (!freedList_.empty() && currentCacheSize_ > maxCacheSize_) { | ||
auto it = freedList_.begin(); | ||
if (!it->second.empty()) { | ||
void* ptr = it->second.front(); | ||
it->second.pop_front(); | ||
std::free(ptr); | ||
currentCacheSize_ -= it->first; | ||
|
||
if (it->second.empty()) { | ||
freedList_.erase(it); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace TinyTorch |
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,34 @@ | ||
/* | ||
* TinyTorch | ||
* @author : [email protected] | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <list> | ||
#include <unordered_map> | ||
|
||
#include "TensorImpl.h" | ||
|
||
namespace TinyTorch { | ||
|
||
class CachedAllocator : public Allocator { | ||
public: | ||
explicit CachedAllocator(size_t maxCacheSize = 256 * 1024 * 1024); // 256 MB | ||
~CachedAllocator() override; | ||
|
||
void* malloc(size_t size) override; | ||
void free(void* ptr) override; | ||
void clear() override; | ||
|
||
private: | ||
void shrink(); | ||
|
||
size_t maxCacheSize_; | ||
size_t currentCacheSize_; | ||
std::unordered_map<void*, size_t> allocatedList_; | ||
std::unordered_map<size_t, std::list<void*>> freedList_; | ||
}; | ||
|
||
} // namespace TinyTorch |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
#include <fstream> | ||
|
||
#include "Allocator.h" | ||
#include "Data.h" | ||
#include "Function.h" | ||
#include "Logger.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