Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wk1234123
Paddle
提交
ce70df86
P
Paddle
项目概览
wk1234123
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ce70df86
编写于
6月 27, 2017
作者:
L
liaogang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add gpu_allocator
上级
ce938ae5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
131 addition
and
3 deletion
+131
-3
paddle/memory/.clang-format
paddle/memory/.clang-format
+5
-0
paddle/memory/detail/CMakeLists.txt
paddle/memory/detail/CMakeLists.txt
+1
-0
paddle/memory/detail/cpu_allocator.h
paddle/memory/detail/cpu_allocator.h
+3
-3
paddle/memory/detail/gpu_allocator.h
paddle/memory/detail/gpu_allocator.h
+92
-0
paddle/memory/detail/gpu_allocator_test.cc
paddle/memory/detail/gpu_allocator_test.cc
+30
-0
未找到文件。
paddle/memory/.clang-format
0 → 100644
浏览文件 @
ce70df86
---
Language: Cpp
BasedOnStyle: Google
Standard: Cpp11
...
paddle/memory/detail/CMakeLists.txt
浏览文件 @
ce70df86
cc_test
(
cpu_allocator_test SRCS cpu_allocator_test.cc
)
nv_test
(
gpu_allocator_test SRCS gpu_allocator_test.cc
)
paddle/memory/detail/cpu_allocator.h
浏览文件 @
ce70df86
...
...
@@ -32,21 +32,21 @@ namespace detail {
// default, we should use CPUAllocator<staging=false>.
template
<
bool
staging
>
class
CPUAllocator
{
public:
public:
void
*
Alloc
(
size_t
size
);
void
Free
(
void
*
p
,
size_t
size
);
};
template
<
>
class
CPUAllocator
<
false
>
{
public:
public:
void
*
Alloc
(
size_t
size
)
{
return
std
::
malloc
(
size
);
}
void
Free
(
void
*
p
,
size_t
size
)
{
std
::
free
(
p
);
}
};
template
<
>
class
CPUAllocator
<
true
>
{
public:
public:
void
*
Alloc
(
size_t
size
)
{
void
*
p
=
std
::
malloc
(
size
);
if
(
p
==
nullptr
)
{
...
...
paddle/memory/detail/gpu_allocator.h
0 → 100644
浏览文件 @
ce70df86
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 <stddef.h> // for size_t
#include <thrust/system_error.h>
#include <thrust/system/cuda/error.h>
namespace
paddle
{
namespace
memory
{
namespace
detail
{
inline
void
throw_on_error
(
cudaError_t
e
,
const
char
*
message
)
{
if
(
e
)
{
throw
thrust
::
system_error
(
e
,
thrust
::
cuda_category
(),
message
);
}
}
// GPUAllocator<staging=true> calls cudaHostMalloc, which returns
// pinned and locked memory as staging areas for data exchange
// between host and device. Allocates too much would reduce the
// amount of memory available to the system for paging. So, by
// default, we should use GPUAllocator<staging=false>.
template
<
bool
staging
>
class
GPUAllocator
{
public:
void
*
Alloc
(
size_t
size
);
void
Free
(
void
*
p
,
size_t
size
);
};
template
<
>
class
GPUAllocator
<
false
>
{
public:
void
*
Alloc
(
size_t
size
)
{
void
*
p
=
0
;
cudaError_t
result
=
cudaMalloc
(
&
p
,
size
);
if
(
result
==
cudaSuccess
)
{
return
p
;
}
// clear last error
cudaGetLastError
();
return
nullptr
;
}
void
Free
(
void
*
p
,
size_t
size
)
{
// 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.
auto
err
=
cudaFree
(
p
);
if
(
err
!=
cudaErrorCudartUnloading
)
{
throw_on_error
(
err
,
"cudaFree failed"
);
}
}
};
template
<
>
class
GPUAllocator
<
true
>
{
public:
void
*
Alloc
(
size_t
size
)
{
void
*
p
=
0
;
cudaError_t
result
=
cudaMallocHost
(
&
p
,
size
);
if
(
result
==
cudaSuccess
)
{
return
p
;
}
// clear last error
cudaGetLastError
();
return
nullptr
;
}
void
Free
(
void
*
p
,
size_t
size
)
{
throw_on_error
(
cudaFreeHost
(
p
),
"cudaFreeHost failed"
);
}
};
}
// namespace detail
}
// namespace memory
}
// namespace paddle
paddle/memory/detail/gpu_allocator_test.cc
0 → 100644
浏览文件 @
ce70df86
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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/memory/detail/gpu_allocator.h"
#include "gtest/gtest.h"
TEST
(
GPUAllocator
,
NonStaging
)
{
paddle
::
memory
::
detail
::
GPUAllocator
<
false
>
a
;
void
*
p
=
a
.
Alloc
(
4096
);
EXPECT_NE
(
p
,
nullptr
);
a
.
Free
(
p
,
4096
);
}
TEST
(
GPUAllocator
,
Staging
)
{
paddle
::
memory
::
detail
::
GPUAllocator
<
true
>
a
;
void
*
p
=
a
.
Alloc
(
4096
);
EXPECT_NE
(
p
,
nullptr
);
a
.
Free
(
p
,
4096
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录