Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
236b7dd2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
236b7dd2
编写于
3月 20, 2018
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pinned memory
上级
484cff6e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
107 addition
and
11 deletion
+107
-11
paddle/fluid/memory/detail/system_allocator.cc
paddle/fluid/memory/detail/system_allocator.cc
+41
-0
paddle/fluid/memory/detail/system_allocator.h
paddle/fluid/memory/detail/system_allocator.h
+12
-0
paddle/fluid/memory/memory.cc
paddle/fluid/memory/memory.cc
+46
-7
paddle/fluid/memory/memory.h
paddle/fluid/memory/memory.h
+8
-4
未找到文件。
paddle/fluid/memory/detail/system_allocator.cc
浏览文件 @
236b7dd2
...
@@ -119,6 +119,47 @@ void GPUAllocator::Free(void* p, size_t size, size_t index) {
...
@@ -119,6 +119,47 @@ void GPUAllocator::Free(void* p, size_t size, size_t index) {
bool
GPUAllocator
::
UseGpu
()
const
{
return
true
;
}
bool
GPUAllocator
::
UseGpu
()
const
{
return
true
;
}
void
*
CUDAPinnedAllocator
::
Alloc
(
size_t
&
index
,
size_t
size
)
{
if
(
size
<=
0
)
return
nullptr
;
void
*
p
;
// NOTE: here, we use GpuMaxAllocSize() as the maximum memory size
// of host fallback allocation. Allocates too much would reduce
// the amount of memory available to the underlying system for paging.
size_t
usable
=
paddle
::
platform
::
GpuMaxAllocSize
()
-
fallback_alloc_size_
;
if
(
size
>
usable
)
return
nullptr
;
cudaError_t
result
=
cudaMallocHost
(
&
p
,
size
);
if
(
result
==
cudaSuccess
)
{
index
=
1
;
fallback_alloc_size_
+=
size
;
return
p
;
}
return
nullptr
;
}
void
CUDAPinnedAllocator
::
Free
(
void
*
p
,
size_t
size
,
size_t
index
)
{
cudaError_t
err
;
PADDLE_ASSERT
(
index
==
1
);
PADDLE_ASSERT
(
fallback_alloc_size_
>=
size
);
fallback_alloc_size_
-=
size
;
err
=
cudaFreeHost
(
p
);
// Purposefully allow cudaErrorCudartUnloading, because
// that is returned if you ever call cudaFree after the
// driver has already shutdown. This happens only if the
// process is terminating, in which case we don't care if
// cudaFree succeeds.
if
(
err
!=
cudaErrorCudartUnloading
)
{
PADDLE_ENFORCE
(
err
,
"cudaFreeHost failed in GPUPinnedAllocator::Free."
);
}
}
bool
CUDAPinnedAllocator
::
UseGpu
()
const
{
return
true
;
}
#endif
#endif
}
// namespace detail
}
// namespace detail
...
...
paddle/fluid/memory/detail/system_allocator.h
浏览文件 @
236b7dd2
...
@@ -51,6 +51,18 @@ class GPUAllocator : public SystemAllocator {
...
@@ -51,6 +51,18 @@ class GPUAllocator : public SystemAllocator {
size_t
gpu_alloc_size_
=
0
;
size_t
gpu_alloc_size_
=
0
;
size_t
fallback_alloc_size_
=
0
;
size_t
fallback_alloc_size_
=
0
;
};
};
class
CUDAPinnedAllocator
:
public
SystemAllocator
{
public:
virtual
void
*
Alloc
(
size_t
&
index
,
size_t
size
);
virtual
void
Free
(
void
*
p
,
size_t
size
,
size_t
index
);
virtual
bool
UseGpu
()
const
;
private:
size_t
gpu_alloc_size_
=
0
;
// TODO(zcd): how to define the upper limit of CUDAPinnedMemory?
size_t
fallback_alloc_size_
=
0
;
};
#endif
#endif
}
// namespace detail
}
// namespace detail
...
...
paddle/fluid/memory/memory.cc
浏览文件 @
236b7dd2
...
@@ -38,7 +38,8 @@ BuddyAllocator* GetCPUBuddyAllocator() {
...
@@ -38,7 +38,8 @@ BuddyAllocator* GetCPUBuddyAllocator() {
}
}
template
<
>
template
<
>
void
*
Alloc
<
platform
::
CPUPlace
>
(
platform
::
CPUPlace
place
,
size_t
size
)
{
void
*
Alloc
<
platform
::
CPUPlace
>
(
platform
::
CPUPlace
place
,
size_t
size
,
bool
use_pinned
)
{
VLOG
(
10
)
<<
"Allocate "
<<
size
<<
" bytes on "
<<
platform
::
Place
(
place
);
VLOG
(
10
)
<<
"Allocate "
<<
size
<<
" bytes on "
<<
platform
::
Place
(
place
);
void
*
p
=
GetCPUBuddyAllocator
()
->
Alloc
(
size
);
void
*
p
=
GetCPUBuddyAllocator
()
->
Alloc
(
size
);
VLOG
(
10
)
<<
" pointer="
<<
p
;
VLOG
(
10
)
<<
" pointer="
<<
p
;
...
@@ -46,7 +47,8 @@ void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size) {
...
@@ -46,7 +47,8 @@ void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size) {
}
}
template
<
>
template
<
>
void
Free
<
platform
::
CPUPlace
>
(
platform
::
CPUPlace
place
,
void
*
p
)
{
void
Free
<
platform
::
CPUPlace
>
(
platform
::
CPUPlace
place
,
void
*
p
,
bool
use_pinned
)
{
VLOG
(
10
)
<<
"Free pointer="
<<
p
<<
" on "
<<
platform
::
Place
(
place
);
VLOG
(
10
)
<<
"Free pointer="
<<
p
<<
" on "
<<
platform
::
Place
(
place
);
GetCPUBuddyAllocator
()
->
Free
(
p
);
GetCPUBuddyAllocator
()
->
Free
(
p
);
}
}
...
@@ -82,15 +84,47 @@ BuddyAllocator* GetGPUBuddyAllocator(int gpu_id) {
...
@@ -82,15 +84,47 @@ BuddyAllocator* GetGPUBuddyAllocator(int gpu_id) {
return
as
[
gpu_id
];
return
as
[
gpu_id
];
}
}
BuddyAllocator
*
GetCUDAPinnedBuddyAllocator
(
int
gpu_id
)
{
static
BuddyAllocator
**
as
=
NULL
;
if
(
as
==
NULL
)
{
int
gpu_num
=
platform
::
GetCUDADeviceCount
();
as
=
new
BuddyAllocator
*
[
gpu_num
];
for
(
int
gpu
=
0
;
gpu
<
gpu_num
;
gpu
++
)
{
as
[
gpu
]
=
nullptr
;
}
}
platform
::
SetDeviceId
(
gpu_id
);
if
(
!
as
[
gpu_id
])
{
as
[
gpu_id
]
=
new
BuddyAllocator
(
new
detail
::
CUDAPinnedAllocator
,
platform
::
GpuMinChunkSize
(),
platform
::
GpuMaxChunkSize
());
VLOG
(
10
)
<<
"
\n\n
NOTE: each GPU device use "
<<
FLAGS_fraction_of_gpu_memory_to_use
*
100
<<
"% of GPU memory.
\n
"
<<
"You can set GFlags environment variable '"
<<
"FLAGS_fraction_of_gpu_memory_to_use"
<<
"' to change the fraction of GPU usage.
\n\n
"
;
}
return
as
[
gpu_id
];
}
template
<
>
template
<
>
size_t
Used
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
)
{
size_t
Used
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
)
{
return
GetGPUBuddyAllocator
(
place
.
device
)
->
Used
();
return
GetGPUBuddyAllocator
(
place
.
device
)
->
Used
();
}
}
template
<
>
template
<
>
void
*
Alloc
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
,
size_t
size
)
{
void
*
Alloc
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
,
size_t
size
,
auto
*
buddy_allocator
=
GetGPUBuddyAllocator
(
place
.
device
);
bool
use_pinned
)
{
auto
*
ptr
=
buddy_allocator
->
Alloc
(
size
);
void
*
ptr
;
if
(
use_pinned
)
{
auto
*
buddy_allocator
=
GetCUDAPinnedBuddyAllocator
(
place
.
device
);
ptr
=
buddy_allocator
->
Alloc
(
size
);
}
else
{
auto
*
buddy_allocator
=
GetGPUBuddyAllocator
(
place
.
device
);
ptr
=
buddy_allocator
->
Alloc
(
size
);
}
if
(
ptr
==
nullptr
)
{
if
(
ptr
==
nullptr
)
{
int
cur_dev
=
platform
::
GetCurrentDeviceId
();
int
cur_dev
=
platform
::
GetCurrentDeviceId
();
platform
::
SetDeviceId
(
place
.
device
);
platform
::
SetDeviceId
(
place
.
device
);
...
@@ -108,8 +142,13 @@ void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size) {
...
@@ -108,8 +142,13 @@ void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size) {
}
}
template
<
>
template
<
>
void
Free
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
,
void
*
p
)
{
void
Free
<
platform
::
CUDAPlace
>
(
platform
::
CUDAPlace
place
,
void
*
p
,
GetGPUBuddyAllocator
(
place
.
device
)
->
Free
(
p
);
bool
use_pinned
)
{
if
(
use_pinned
)
{
GetCUDAPinnedBuddyAllocator
(
place
.
device
)
->
Free
(
p
);
}
else
{
GetGPUBuddyAllocator
(
place
.
device
)
->
Free
(
p
);
}
}
}
#endif
#endif
...
...
paddle/fluid/memory/memory.h
浏览文件 @
236b7dd2
...
@@ -33,7 +33,7 @@ namespace memory {
...
@@ -33,7 +33,7 @@ namespace memory {
* address is valid or not.
* address is valid or not.
*/
*/
template
<
typename
Place
>
template
<
typename
Place
>
void
*
Alloc
(
Place
place
,
size_t
size
);
void
*
Alloc
(
Place
place
,
size_t
size
,
bool
use_pinned
=
false
);
/**
/**
* \brief Free memory block in one place.
* \brief Free memory block in one place.
...
@@ -43,7 +43,7 @@ void* Alloc(Place place, size_t size);
...
@@ -43,7 +43,7 @@ void* Alloc(Place place, size_t size);
*
*
*/
*/
template
<
typename
Place
>
template
<
typename
Place
>
void
Free
(
Place
place
,
void
*
ptr
);
void
Free
(
Place
place
,
void
*
ptr
,
bool
use_pinned
=
false
);
/**
/**
* \brief Total size of used memory in one place.
* \brief Total size of used memory in one place.
...
@@ -74,11 +74,15 @@ class PODDeleter {
...
@@ -74,11 +74,15 @@ class PODDeleter {
static_assert
(
std
::
is_pod
<
T
>::
value
,
"T must be POD"
);
static_assert
(
std
::
is_pod
<
T
>::
value
,
"T must be POD"
);
public:
public:
explicit
PODDeleter
(
Place
place
)
:
place_
(
place
)
{}
explicit
PODDeleter
(
Place
place
,
bool
use_pinned
=
false
)
void
operator
()(
T
*
ptr
)
{
Free
(
place_
,
static_cast
<
void
*>
(
ptr
));
}
:
place_
(
place
),
use_pinned_
(
use_pinned
)
{}
void
operator
()(
T
*
ptr
)
{
Free
(
place_
,
static_cast
<
void
*>
(
ptr
),
use_pinned_
);
}
private:
private:
Place
place_
;
Place
place_
;
bool
use_pinned_
;
};
};
/**
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录