Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3b0afae3
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3b0afae3
编写于
1月 08, 2018
作者:
Y
Yang Yu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add more comments
上级
bdc82956
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
19 addition
and
19 deletion
+19
-19
paddle/framework/details/cow_ptr.h
paddle/framework/details/cow_ptr.h
+19
-15
paddle/framework/details/cow_ptr_test.cc
paddle/framework/details/cow_ptr_test.cc
+0
-4
未找到文件。
paddle/framework/details/cow_ptr.h
浏览文件 @
3b0afae3
...
@@ -25,13 +25,14 @@ class ThreadUnsafeOwnershipFlags {
...
@@ -25,13 +25,14 @@ class ThreadUnsafeOwnershipFlags {
public:
public:
ThreadUnsafeOwnershipFlags
(
bool
flag
)
:
flag_
(
flag
)
{}
ThreadUnsafeOwnershipFlags
(
bool
flag
)
:
flag_
(
flag
)
{}
ThreadUnsafeOwnershipFlags
(
const
ThreadUnsafeOwnershipFlags
&
o
)
=
delete
;
ThreadUnsafeOwnershipFlags
(
const
ThreadUnsafeOwnershipFlags
&
o
ther
)
=
delete
;
ThreadUnsafeOwnershipFlags
&
operator
=
(
const
ThreadUnsafeOwnershipFlags
&
o
)
=
ThreadUnsafeOwnershipFlags
&
operator
=
(
delete
;
const
ThreadUnsafeOwnershipFlags
&
other
)
=
delete
;
ThreadUnsafeOwnershipFlags
(
ThreadUnsafeOwnershipFlags
&&
o
)
=
default
;
ThreadUnsafeOwnershipFlags
(
ThreadUnsafeOwnershipFlags
&&
o
ther
)
=
default
;
void
SetOwnership
(
bool
flag
)
{
flag_
=
flag
;
}
void
SetOwnership
(
bool
flag
)
{
flag_
=
flag
;
}
// Invoke the callback if it is not owned.
template
<
typename
Callback
>
template
<
typename
Callback
>
void
AcquireOwnershipOnce
(
Callback
acquire
)
{
void
AcquireOwnershipOnce
(
Callback
acquire
)
{
if
(
!
flag_
)
{
if
(
!
flag_
)
{
...
@@ -44,7 +45,7 @@ class ThreadUnsafeOwnershipFlags {
...
@@ -44,7 +45,7 @@ class ThreadUnsafeOwnershipFlags {
bool
flag_
;
bool
flag_
;
};
};
// Copy
On
Write pointer.
// Copy
-On-
Write pointer.
// It will hold a T* pointer, and only copy once when `MutableData` is invoked.
// It will hold a T* pointer, and only copy once when `MutableData` is invoked.
//
//
// The template parameter OwnershipFlags should have:
// The template parameter OwnershipFlags should have:
...
@@ -52,6 +53,8 @@ class ThreadUnsafeOwnershipFlags {
...
@@ -52,6 +53,8 @@ class ThreadUnsafeOwnershipFlags {
// * SetOwnership(bool flag).
// * SetOwnership(bool flag).
// * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not
// * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not
// owned.
// owned.
//
// https://en.wikipedia.org/wiki/Copy-on-write
template
<
typename
T
,
typename
OwnershipFlags
=
ThreadUnsafeOwnershipFlags
>
template
<
typename
T
,
typename
OwnershipFlags
=
ThreadUnsafeOwnershipFlags
>
class
COWPtr
{
class
COWPtr
{
public:
public:
...
@@ -59,33 +62,34 @@ class COWPtr {
...
@@ -59,33 +62,34 @@ class COWPtr {
explicit
COWPtr
(
T
*
ptr
)
:
payload_
(
ptr
),
ownership_
{
true
}
{}
explicit
COWPtr
(
T
*
ptr
)
:
payload_
(
ptr
),
ownership_
{
true
}
{}
// Move methods. Steal ownership from origin
// Move methods. Steal ownership from origin
COWPtr
(
COWPtr
&&
o
)
COWPtr
(
COWPtr
&&
o
ther
)
:
payload_
(
o
.
payload_
),
ownership_
{
std
::
move
(
o
.
ownership_
)}
{}
:
payload_
(
o
ther
.
payload_
),
ownership_
{
std
::
move
(
other
.
ownership_
)}
{}
COWPtr
&
operator
=
(
COWPtr
&&
origin
)
=
default
;
COWPtr
&
operator
=
(
COWPtr
&&
origin
)
=
default
;
// Copy methods. Not own payload
// Copy methods. Not own payload
COWPtr
(
const
COWPtr
&
o
)
:
payload_
(
o
.
payload_
),
ownership_
{
false
}
{}
COWPtr
(
const
COWPtr
&
o
ther
)
:
payload_
(
other
.
payload_
),
ownership_
{
false
}
{}
COWPtr
&
operator
=
(
const
COWPtr
&
o
)
{
COWPtr
&
operator
=
(
const
COWPtr
&
o
ther
)
{
payload_
=
o
.
payload_
;
payload_
=
o
ther
.
payload_
;
ownership_
.
SetOwnership
(
false
);
ownership_
.
SetOwnership
(
false
);
return
*
this
;
return
*
this
;
}
}
// Access read only data.
const
T
&
Data
()
const
{
return
*
payload_
;
}
const
T
&
Data
()
const
{
return
*
payload_
;
}
// Access mutable data. If the data is not owned, the data will be copied
// before.
T
*
MutableData
()
{
T
*
MutableData
()
{
ownership_
.
AcquireOwnershipOnce
(
ownership_
.
AcquireOwnershipOnce
(
[
this
]
{
payload_
.
reset
(
new
T
(
*
payload_
));
});
[
this
]
{
payload_
.
reset
(
new
T
(
*
payload_
));
});
return
payload_
.
get
();
return
payload_
.
get
();
}
}
void
Reset
()
{
ownership_
.
AcquireOwnershipOnce
([
this
]
{
payload_
.
reset
();
});
payload_
.
reset
(
new
T
());
}
private:
private:
// Actual data pointer.
std
::
shared_ptr
<
T
>
payload_
;
std
::
shared_ptr
<
T
>
payload_
;
// Ownership flag.
OwnershipFlags
ownership_
;
OwnershipFlags
ownership_
;
};
};
...
...
paddle/framework/details/cow_ptr_test.cc
浏览文件 @
3b0afae3
...
@@ -28,10 +28,6 @@ TEST(COWPtr, all) {
...
@@ -28,10 +28,6 @@ TEST(COWPtr, all) {
*
ptr2
.
MutableData
()
=
10
;
*
ptr2
.
MutableData
()
=
10
;
ASSERT_EQ
(
ptr
.
Data
(),
0
);
ASSERT_EQ
(
ptr
.
Data
(),
0
);
ASSERT_EQ
(
ptr2
.
Data
(),
10
);
ASSERT_EQ
(
ptr2
.
Data
(),
10
);
auto
ptr_before
=
ptr2
.
MutableData
();
ptr2
.
Reset
();
ASSERT_NE
(
ptr2
.
MutableData
(),
ptr_before
);
}
}
}
// namespace details
}
// namespace details
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录