Skip to content
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

Memory Pool Improvement For Variadic Sized Inputs #4190

Merged
merged 34 commits into from
Oct 9, 2022
Merged
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f5783b5
A LayerNorm_x86 class mocking LayerNorm for tests;
LinHeLurking Jul 18, 2022
7a94b1a
All SIMD optimizations success wihout support_packing; Maybe there's …
LinHeLurking Jul 20, 2022
b126e0f
Located error about packed layout.
LinHeLurking Jul 21, 2022
1982605
All test passed; Now it supports packing layout
LinHeLurking Jul 21, 2022
0fa8689
Fix runtime cpu dispatch;
LinHeLurking Jul 21, 2022
6a683d3
Use fmadd wrapper in x86_usability.h;
LinHeLurking Jul 22, 2022
bf95312
Merge packed & unpacked code.
LinHeLurking Jul 22, 2022
af97b05
Func rename.
LinHeLurking Jul 22, 2022
a9be63a
Simplify and merge more branches about packed layout;
LinHeLurking Jul 22, 2022
976692a
Code format
LinHeLurking Jul 24, 2022
d7007c3
Replace some member functions with static inline functions.
LinHeLurking Jul 25, 2022
508d143
Add copyright header
LinHeLurking Jul 25, 2022
cf015d8
apply code-format changes
LinHeLurking Jul 25, 2022
5084955
Add more tests with 16 packed for AVX512
LinHeLurking Jul 25, 2022
48fb4ea
Code format
LinHeLurking Jul 25, 2022
8c4ed97
Merge branch 'master' of https://github.com/Tencent/ncnn
LinHeLurking Jul 25, 2022
3c2c1c8
Merge branch 'master' of https://github.com/LinHeLurking/ncnn
LinHeLurking Jul 25, 2022
487568d
Copyright statement year fixed
LinHeLurking Jul 26, 2022
23db5ab
Fix accidentally added corelation of mean/var and SIMD ISA
LinHeLurking Jul 26, 2022
72777b4
Fix accidentally added corelation of fmadd/affine_fmadd and SIMD ISA
LinHeLurking Jul 26, 2022
b20d298
Fix a wrong test param
LinHeLurking Jul 26, 2022
4fddf9e
Fix runtime dispatch
LinHeLurking Jul 26, 2022
2555b3e
apply code-format changes
LinHeLurking Jul 26, 2022
1b118f7
no store duplicates
nihui Jul 29, 2022
649a63b
Merge branch 'Tencent:master' into master
LinHeLurking Aug 1, 2022
49a5b7f
Simple miss count for better space efficiency
LinHeLurking Aug 11, 2022
2ecda04
Change miss count position;
LinHeLurking Aug 16, 2022
cee9d22
Simple double ended greedy;
LinHeLurking Sep 7, 2022
3f2731f
Merge branch 'Tencent:master' into master
LinHeLurking Sep 7, 2022
577d593
Merge branch 'master' into mem-pool
LinHeLurking Sep 7, 2022
ee7ca30
Fix santinizer bug;
LinHeLurking Sep 26, 2022
d4b6698
Change it_max/it_min initial value;
LinHeLurking Oct 6, 2022
4a0a1a9
Add size drop threshold setter;
LinHeLurking Oct 6, 2022
4066294
set workspace allocator cr to zero as we had some sort of recylcing c…
nihui Oct 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PoolAllocatorPrivate
Mutex budgets_lock;
Mutex payouts_lock;
unsigned int size_compare_ratio; // 0~256
static const size_t size_threshold = 10;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_threshold does not seem expressive enough
budget_count_threshold or budget_drop_threshold might be better?

How about adding a setter api that allows the user to control whether a more aggressive or conservative budget recycling strategy is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed & Added a setter.

std::list<std::pair<size_t, void*> > budgets;
std::list<std::pair<size_t, void*> > payouts;
};
Expand Down Expand Up @@ -104,11 +105,20 @@ void* PoolAllocator::fastMalloc(size_t size)
d->budgets_lock.lock();

// find free budget
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin(), it_max = d->budgets.end(), it_min = d->budgets.end();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these max/min iterators initialized to end() instead of begin() ?
If the budgets are empty, if (d->budgets.size() >= d->size_threshold) will always be false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it doesn't matter.

for (; it != d->budgets.end(); ++it)
{
size_t bs = it->first;

if (it_min == d->budgets.end() || it->first < it_min->first)
{
it_min = it;
}
if (it_max == d->budgets.end() || it->first > it_max->first)
{
it_max = it;
}

// size_compare_ratio ~ 100%
if (bs >= size && ((bs * d->size_compare_ratio) >> 8) <= size)
{
Expand All @@ -128,6 +138,26 @@ void* PoolAllocator::fastMalloc(size_t size)
}
}

if (d->budgets.size() >= d->size_threshold)
{
// All chunks in pool are not chosen. Then try to drop some outdated
// chunks and return them to OS.
if (it_max->first < size)
{
// Current query is asking for a chunk larger than any cached chunks.
// Then remove the smallest one.
ncnn::fastFree(it_min->second);
d->budgets.erase(it_min);
}
else if (it_min->first > size)
{
// Current query is asking for a chunk smaller than any cached chunks.
// Then remove the largest one.
ncnn::fastFree(it_max->second);
d->budgets.erase(it_max);
}
}

d->budgets_lock.unlock();

// new
Expand Down Expand Up @@ -178,6 +208,7 @@ class UnlockedPoolAllocatorPrivate
{
public:
unsigned int size_compare_ratio; // 0~256
static const size_t size_threshold = 10;
std::list<std::pair<size_t, void*> > budgets;
std::list<std::pair<size_t, void*> > payouts;
};
Expand Down Expand Up @@ -243,11 +274,20 @@ void UnlockedPoolAllocator::set_size_compare_ratio(float scr)
void* UnlockedPoolAllocator::fastMalloc(size_t size)
{
// find free budget
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin(), it_max = d->budgets.begin(), it_min = d->budgets.begin();
for (; it != d->budgets.end(); ++it)
{
size_t bs = it->first;

if (it->first > it_max->first)
{
it_max = it;
}
if (it->first < it_min->first)
{
it_min = it;
}

// size_compare_ratio ~ 100%
if (bs >= size && ((bs * d->size_compare_ratio) >> 8) <= size)
{
Expand All @@ -261,6 +301,20 @@ void* UnlockedPoolAllocator::fastMalloc(size_t size)
}
}

if (d->budgets.size() >= d->size_threshold)
{
if (it_max->first < size)
{
ncnn::fastFree(it_min->second);
d->budgets.erase(it_min);
}
else if (it_min->first > size)
{
ncnn::fastFree(it_max->second);
d->budgets.erase(it_max);
}
}

// new
void* ptr = ncnn::fastMalloc(size);

Expand Down