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

add auto_grown_mutex for selected rows #11012

Merged
merged 4 commits into from
May 30, 2018

Conversation

jacquesqiao
Copy link
Member

@jacquesqiao jacquesqiao commented May 29, 2018

project: #10868

为什么mutex要放到 unique_ptr中?
https://stackoverflow.com/questions/30340029/copy-class-with-stdmutex
https://stackoverflow.com/questions/37172050/copy-or-move-constructor-for-a-class-with-a-member-stdmutex-or-other-non-copy

template <typename DeviceContext, typename T>
struct Add {
framework::SelectedRows operator()(const DeviceContext& context,
const framework::SelectedRows& input1,
const framework::SelectedRows& input2) {
framework::SelectedRows out;
out.set_rows(input1.rows());
out.set_height(input1.height());
out.mutable_value()->mutable_data<T>(input1.value().dims(),
context.GetPlace());
auto e_out = framework::EigenVector<T>::Flatten(*(out.mutable_value()));
auto e_in1 = framework::EigenVector<T>::Flatten(input1.value());
auto e_in2 = framework::EigenVector<T>::Flatten(input2.value());
e_out.device(*context.eigen_device()) = e_in1 + e_in2;
return out;
}
};

上面的代码,会在函数内部构造一个selected_rows, 然后return回来,如果selected rows里面有个成员是mutex,他不会生成一个默认的移动构造函数,编译上述functor的时候会挂,如果放到一个unique_prt里边,他会默认调用移动构造函数,把unique_ptr中的指针移动出来。

Yancey1989
Yancey1989 previously approved these changes May 29, 2018
Copy link
Contributor

@Yancey1989 Yancey1989 left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -46,11 +48,13 @@ class SelectedRows {
SelectedRows(const std::vector<int64_t>& rows, const int64_t& height)
: rows_(rows), height_(height) {
value_.reset(new Tensor());
auto_grown_mutex_.reset(new std::mutex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we need a TODO here, make variable thread-safe completely.

Copy link
Member Author

Choose a reason for hiding this comment

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

will give a detailed explanation later.

Copy link
Contributor

@Yancey1989 Yancey1989 left a comment

Choose a reason for hiding this comment

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

LGTM

@jacquesqiao jacquesqiao merged commit 654f5d3 into PaddlePaddle:develop May 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants