Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
788d9328
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
788d9328
编写于
11月 09, 2022
作者:
L
Leo Chen
提交者:
GitHub
11月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean unused code: locked allocator (#47789)
* remove locked allocator * fix ut * add heafer file
上级
b1fb2360
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
18 addition
and
143 deletion
+18
-143
paddle/fluid/memory/allocation/CMakeLists.txt
paddle/fluid/memory/allocation/CMakeLists.txt
+0
-1
paddle/fluid/memory/allocation/best_fit_allocator.cc
paddle/fluid/memory/allocation/best_fit_allocator.cc
+3
-0
paddle/fluid/memory/allocation/best_fit_allocator.h
paddle/fluid/memory/allocation/best_fit_allocator.h
+4
-0
paddle/fluid/memory/allocation/best_fit_allocator_test.cc
paddle/fluid/memory/allocation/best_fit_allocator_test.cc
+2
-6
paddle/fluid/memory/allocation/best_fit_allocator_test.cu
paddle/fluid/memory/allocation/best_fit_allocator_test.cu
+1
-3
paddle/fluid/memory/allocation/buffered_allocator_test.cc
paddle/fluid/memory/allocation/buffered_allocator_test.cc
+7
-30
paddle/fluid/memory/allocation/locked_allocator.cc
paddle/fluid/memory/allocation/locked_allocator.cc
+0
-52
paddle/fluid/memory/allocation/locked_allocator.h
paddle/fluid/memory/allocation/locked_allocator.h
+0
-42
paddle/fluid/memory/allocation/retry_allocator_test.cc
paddle/fluid/memory/allocation/retry_allocator_test.cc
+1
-9
未找到文件。
paddle/fluid/memory/allocation/CMakeLists.txt
浏览文件 @
788d9328
...
...
@@ -4,7 +4,6 @@ set(ALLOCATOR_DEPS place stats profiler phi_backends device_context)
set
(
ALLOCATOR_SRCS
allocator.cc
cpu_allocator.cc
locked_allocator.cc
aligned_allocator.cc
buffered_allocator.cc
best_fit_allocator.cc
...
...
paddle/fluid/memory/allocation/best_fit_allocator.cc
浏览文件 @
788d9328
...
...
@@ -15,6 +15,7 @@
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
#include <cmath>
#include <mutex>
#include "paddle/fluid/platform/enforce.h"
...
...
@@ -120,6 +121,7 @@ size_t BestFitAllocator::NumFreeChunks() const {
return
num
;
}
void
BestFitAllocator
::
FreeImpl
(
phi
::
Allocation
*
allocation
)
{
std
::
lock_guard
<
SpinLock
>
guard
(
spinlock_
);
auto
*
bf_allocation
=
dynamic_cast
<
BestFitAllocation
*>
(
allocation
);
PADDLE_ENFORCE_NOT_NULL
(
bf_allocation
,
...
...
@@ -156,6 +158,7 @@ void BestFitAllocator::FreeImpl(phi::Allocation* allocation) {
delete
allocation
;
}
phi
::
Allocation
*
BestFitAllocator
::
AllocateImpl
(
size_t
size
)
{
std
::
lock_guard
<
SpinLock
>
guard
(
spinlock_
);
auto
highest_set_bit
=
static_cast
<
size_t
>
(
HighestBitPos
(
size
));
MapIt
map_it
;
for
(;
highest_set_bit
<
free_chunks_
.
size
();
++
highest_set_bit
)
{
...
...
paddle/fluid/memory/allocation/best_fit_allocator.h
浏览文件 @
788d9328
...
...
@@ -20,6 +20,7 @@
#include <map>
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/allocation/spin_lock.h"
#include "paddle/fluid/platform//place.h"
namespace
paddle
{
...
...
@@ -112,6 +113,8 @@ class BestFitAllocator : public Allocator {
size_t
NumFreeChunks
()
const
;
bool
IsAllocThreadSafe
()
const
override
{
return
true
;
}
private:
size_t
FreeSize
()
const
;
using
MapIt
=
typename
details
::
FreeChunkBin
::
value_type
::
iterator
;
...
...
@@ -131,6 +134,7 @@ class BestFitAllocator : public Allocator {
phi
::
Allocation
*
allocation_
;
// not owned
details
::
ChunkList
chunks_
;
details
::
FreeChunkBin
free_chunks_
;
SpinLock
spinlock_
;
};
}
// namespace allocation
}
// namespace memory
...
...
paddle/fluid/memory/allocation/best_fit_allocator_test.cc
浏览文件 @
788d9328
...
...
@@ -22,7 +22,6 @@
#include "gtest/gtest.h"
#include "gtest/gtest_pred_impl.h"
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
#include "paddle/fluid/memory/allocation/locked_allocator.h"
namespace
paddle
{
namespace
memory
{
...
...
@@ -100,10 +99,7 @@ TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
CPUAllocator
allocator
;
auto
global_allocation
=
allocator
.
Allocate
(
256UL
*
1024
*
1024
);
std
::
unique_ptr
<
Allocator
>
best_fit_allocator
(
new
BestFitAllocator
(
global_allocation
.
get
()));
LockedAllocator
locked_allocator
(
std
::
move
(
best_fit_allocator
));
BestFitAllocator
best_fit_allocator
(
global_allocation
.
get
());
auto
th_main
=
[
&
](
std
::
random_device
::
result_type
seed
)
{
std
::
default_random_engine
engine
(
seed
);
...
...
@@ -113,7 +109,7 @@ TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
size_t
allocate_size
=
dist
(
engine
);
auto
allocation
=
locked
_allocator
.
Allocate
(
sizeof
(
size_t
)
*
allocate_size
);
best_fit
_allocator
.
Allocate
(
sizeof
(
size_t
)
*
allocate_size
);
size_t
*
data
=
reinterpret_cast
<
size_t
*>
(
allocation
->
ptr
());
...
...
paddle/fluid/memory/allocation/best_fit_allocator_test.cu
浏览文件 @
788d9328
...
...
@@ -21,7 +21,6 @@
#include "paddle/fluid/memory/allocation/allocator_facade.h"
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
#include "paddle/fluid/memory/allocation/cuda_allocator.h"
#include "paddle/fluid/memory/allocation/locked_allocator.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/for_range.h"
namespace
paddle
{
...
...
@@ -40,8 +39,7 @@ TEST(BestFitAllocator, concurrent_cuda) {
CUDAAllocator
allocator
(
platform
::
CUDAPlace
(
0
));
// 256 MB
auto
cuda_allocation
=
allocator
.
Allocate
(
256U
*
1024
*
1024
);
LockedAllocator
concurrent_allocator
(
std
::
unique_ptr
<
Allocator
>
(
new
BestFitAllocator
(
cuda_allocation
.
get
())));
BestFitAllocator
concurrent_allocator
(
cuda_allocation
.
get
());
platform
::
CUDAPlace
gpu
(
0
);
phi
::
GPUContext
dev_ctx
(
gpu
);
...
...
paddle/fluid/memory/allocation/buffered_allocator_test.cc
浏览文件 @
788d9328
...
...
@@ -20,37 +20,11 @@
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
#include "paddle/fluid/memory/allocation/locked_allocator.h"
namespace
paddle
{
namespace
memory
{
namespace
allocation
{
inline
std
::
unique_ptr
<
BufferedAllocator
>
GetBufferedAllocator
(
phi
::
Allocation
*
allocation
,
bool
thread_safe
)
{
std
::
unique_ptr
<
Allocator
>
allocator
(
new
BestFitAllocator
(
allocation
));
if
(
thread_safe
)
{
allocator
.
reset
(
new
LockedAllocator
(
std
::
move
(
allocator
)));
}
return
std
::
unique_ptr
<
BufferedAllocator
>
(
new
BufferedAllocator
(
std
::
move
(
allocator
)));
}
TEST
(
buffered_allocator
,
thread_safety
)
{
std
::
unique_ptr
<
CPUAllocator
>
allocator
(
new
CPUAllocator
());
auto
chunk
=
allocator
->
Allocate
(
1
<<
20
);
{
auto
buf_allocator
=
GetBufferedAllocator
(
chunk
.
get
(),
true
);
ASSERT_EQ
(
buf_allocator
->
IsAllocThreadSafe
(),
true
);
}
{
auto
buf_allocator
=
GetBufferedAllocator
(
chunk
.
get
(),
false
);
ASSERT_EQ
(
buf_allocator
->
IsAllocThreadSafe
(),
false
);
}
}
class
StubAllocation
:
public
Allocation
{
public:
using
Allocation
::
Allocation
;
...
...
@@ -136,12 +110,15 @@ TEST(buffered_allocator, lazy_free) {
TEST
(
buffered_allocator
,
garbage_collection
)
{
std
::
unique_ptr
<
CPUAllocator
>
cpu_allocator
(
new
CPUAllocator
());
auto
chunk
=
cpu_allocator
->
Allocate
(
2048
);
auto
allocator
=
GetBufferedAllocator
(
chunk
.
get
(),
false
);
auto
x1
=
allocator
->
Allocate
(
1600
);
auto
x2
=
allocator
->
Allocate
(
400
);
std
::
unique_ptr
<
Allocator
>
allocator
(
new
BestFitAllocator
(
chunk
.
get
()));
auto
buffered_allocator
=
std
::
unique_ptr
<
BufferedAllocator
>
(
new
BufferedAllocator
(
std
::
move
(
allocator
)));
auto
x1
=
buffered_allocator
->
Allocate
(
1600
);
auto
x2
=
buffered_allocator
->
Allocate
(
400
);
x1
=
nullptr
;
x2
=
nullptr
;
auto
x3
=
allocator
->
Allocate
(
1600
);
auto
x3
=
buffered_
allocator
->
Allocate
(
1600
);
ASSERT_NE
(
x3
,
nullptr
);
ASSERT_NE
(
x3
->
ptr
(),
nullptr
);
}
...
...
paddle/fluid/memory/allocation/locked_allocator.cc
已删除
100644 → 0
浏览文件 @
b1fb2360
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/memory/allocation/locked_allocator.h"
#include <mutex> // NOLINT
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/lock_guard_ptr.h"
namespace
paddle
{
namespace
memory
{
namespace
allocation
{
bool
LockedAllocator
::
IsAllocThreadSafe
()
const
{
return
true
;
}
LockedAllocator
::
LockedAllocator
(
std
::
shared_ptr
<
Allocator
>
underlying_allocator
)
:
underlying_allocator_
(
std
::
move
(
underlying_allocator
))
{
PADDLE_ENFORCE_NOT_NULL
(
underlying_allocator_
,
platform
::
errors
::
InvalidArgument
(
"Underlying allocator of LockedAllocator is NULL"
));
if
(
!
underlying_allocator_
->
IsAllocThreadSafe
())
{
mtx_
.
reset
(
new
std
::
mutex
());
}
}
void
LockedAllocator
::
FreeImpl
(
phi
::
Allocation
*
allocation
)
{
platform
::
LockGuardPtr
<
std
::
mutex
>
guard
(
mtx_
);
underlying_allocator_
->
Free
(
allocation
);
}
phi
::
Allocation
*
LockedAllocator
::
AllocateImpl
(
size_t
size
)
{
platform
::
LockGuardPtr
<
std
::
mutex
>
guard
(
mtx_
);
return
underlying_allocator_
->
Allocate
(
size
).
release
();
}
}
// namespace allocation
}
// namespace memory
}
// namespace paddle
paddle/fluid/memory/allocation/locked_allocator.h
已删除
100644 → 0
浏览文件 @
b1fb2360
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <memory>
#include <mutex> // NOLINT
#include <thread> // NOLINT
#include "paddle/fluid/memory/allocation/allocator.h"
namespace
paddle
{
namespace
memory
{
namespace
allocation
{
// A allocator to make underlying allocator thread safe.
class
LockedAllocator
:
public
Allocator
{
public:
explicit
LockedAllocator
(
std
::
shared_ptr
<
Allocator
>
underlying_allocator
);
bool
IsAllocThreadSafe
()
const
override
;
protected:
void
FreeImpl
(
phi
::
Allocation
*
allocation
)
override
;
phi
::
Allocation
*
AllocateImpl
(
size_t
size
)
override
;
private:
std
::
shared_ptr
<
Allocator
>
underlying_allocator_
;
std
::
unique_ptr
<
std
::
mutex
>
mtx_
;
};
}
// namespace allocation
}
// namespace memory
}
// namespace paddle
paddle/fluid/memory/allocation/retry_allocator_test.cc
浏览文件 @
788d9328
...
...
@@ -19,7 +19,6 @@
#include "gtest/gtest.h"
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
#include "paddle/fluid/memory/allocation/locked_allocator.h"
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#include "paddle/fluid/memory/allocation/cuda_allocator.h"
#endif
...
...
@@ -34,11 +33,6 @@ TEST(RetryAllocator, RetryAllocator) {
size_t
size
=
(
1
<<
20
);
auto
cpu_allocation
=
cpu_allocator
.
Allocate
(
size
);
std
::
unique_ptr
<
BestFitAllocator
>
best_fit_allocator
(
new
BestFitAllocator
(
cpu_allocation
.
get
()));
std
::
unique_ptr
<
LockedAllocator
>
locked_allocator
(
new
LockedAllocator
(
std
::
move
(
best_fit_allocator
)));
size_t
thread_num
=
4
;
size_t
sleep_time
=
40
;
size_t
extra_time
=
20
;
...
...
@@ -48,10 +42,8 @@ TEST(RetryAllocator, RetryAllocator) {
{
std
::
unique_ptr
<
BestFitAllocator
>
best_fit_allocator
(
new
BestFitAllocator
(
cpu_allocation
.
get
()));
std
::
unique_ptr
<
LockedAllocator
>
locked_allocator
(
new
LockedAllocator
(
std
::
move
(
best_fit_allocator
)));
allocators
.
push_back
(
std
::
make_shared
<
RetryAllocator
>
(
std
::
move
(
locked
_allocator
),
std
::
move
(
best_fit
_allocator
),
(
thread_num
-
1
)
*
(
sleep_time
+
extra_time
)));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录