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

Thread data registry #40912

Merged
merged 79 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
8744ba7
add align for WorkQueue
liutiexing Sep 22, 2021
4759bc8
Merge branch 'develop' of https://github.com/liutiexing/Paddle into d…
liutiexing Sep 22, 2021
6f00ace
add spinlock
liutiexing Sep 23, 2021
2d6f1cf
merge develop
liutiexing Sep 23, 2021
f5099be
Merge branch 'develop' of https://github.com/liutiexing/Paddle into d…
liutiexing Sep 26, 2021
54aa332
merge develop
liutiexing Oct 12, 2021
1d1bd82
merge
liutiexing Oct 12, 2021
dfbf3e4
Merge remote-tracking branch 'upstream/develop' into develop
liutiexing Oct 12, 2021
a5392b3
Merge remote-tracking branch 'upstream/develop' into develop
liutiexing Oct 14, 2021
e206173
Add EventsWaiter
liutiexing Oct 15, 2021
0a3dcd9
Revert "Add EventsWaiter"
liutiexing Oct 15, 2021
4689bb5
Merge remote-tracking branch 'upstream/develop' into develop
liutiexing Oct 15, 2021
0cec99a
Merge remote-tracking branch 'upstream/develop' into develop
liutiexing Oct 20, 2021
481c4fa
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Oct 27, 2021
83db84e
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Oct 29, 2021
7010e0d
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Nov 16, 2021
ec2a363
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Nov 23, 2021
90a59ec
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Nov 26, 2021
1445bbe
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Nov 29, 2021
a2c74ab
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 1, 2021
1c09b4e
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 2, 2021
cb8cf7d
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 8, 2021
cf0dcd6
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 8, 2021
2f95801
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 14, 2021
14bec1b
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 15, 2021
8a5f7af
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 16, 2021
f0a5915
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 20, 2021
0fe35aa
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 21, 2021
f65eef2
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 23, 2021
b37e42d
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 28, 2021
cf5e240
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 29, 2021
b31869a
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Dec 30, 2021
fab2911
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 4, 2022
16b0903
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 6, 2022
074fea5
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 7, 2022
8f4a51c
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 8, 2022
09036ff
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 10, 2022
0e6a94f
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 13, 2022
d2293fd
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 14, 2022
b529801
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 17, 2022
ff55840
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 17, 2022
52684e7
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 21, 2022
e806789
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 22, 2022
e59a3f8
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 24, 2022
8fa5e17
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 25, 2022
5c8ffbd
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 25, 2022
e5586e9
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 26, 2022
e5731a4
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Jan 28, 2022
67cd2a6
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 10, 2022
df6298b
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 11, 2022
7edaab6
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 11, 2022
bdec640
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 11, 2022
7c0736c
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 14, 2022
fc9f166
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 16, 2022
91a3729
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 17, 2022
dd1ad2c
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 19, 2022
c5afd93
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 19, 2022
0d45241
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 21, 2022
696f4c7
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 21, 2022
0b14de3
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 22, 2022
92e1b3d
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 22, 2022
db7a3c6
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 23, 2022
9f91993
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 23, 2022
116a1f3
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 24, 2022
88f6444
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 25, 2022
bdb927e
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 25, 2022
71e2bda
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 26, 2022
bc3edb6
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 27, 2022
3735f63
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Feb 28, 2022
9e2ffcf
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 1, 2022
581de82
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 2, 2022
5a3d862
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 3, 2022
01e6716
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 11, 2022
3e6a64c
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 16, 2022
b03f605
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 21, 2022
e4754e1
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 21, 2022
5b758c2
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 23, 2022
c35b816
Merge branch 'PaddlePaddle:develop' into develop
liutiexing Mar 24, 2022
f2dbcc2
Update ThreadDataRegistry
Mar 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,24 @@ std::string EventsWaiter::WaitEvent() {
}

trigger_event_.store(kEmptyEventId, std::memory_order_relaxed);
waiting_.store(false, std::memory_order_relaxed);
std::string evt_name =
triggered == kEmptyEventId ? "NoEventNotifier" : GetEventName(triggered);
VLOG(10) << "Consume event id:" << triggered << ", name:" << evt_name;
// lazy deletion
{
triggered = trigger_event_;
std::lock_guard<paddle::memory::SpinLock> guard(events_lock_);
if (deleted_events_.size() > 0) {
for (auto evt : deleted_events_) {
if (evt == triggered) {
continue;
}
events_.erase(evt);
}
deleted_events_.clear();
}
}
waiting_.store(false, std::memory_order_relaxed);
return evt_name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <type_traits>
Expand All @@ -23,13 +24,8 @@
namespace paddle {
namespace framework {

static uint64_t main_tid =
std::hash<std::thread::id>()(std::this_thread::get_id());

template <typename T>
class ThreadDataRegistry {
class ThreadDataHolder;

public:
// Singleton
static ThreadDataRegistry& GetInstance() {
Expand All @@ -52,73 +48,92 @@ class ThreadDataRegistry {
template <typename Alias = T, typename = std::enable_if_t<
std::is_copy_constructible<Alias>::value>>
std::unordered_map<uint64_t, T> GetAllThreadDataByValue() {
std::unordered_map<uint64_t, T> data_copy;
std::lock_guard<std::mutex> lock(lock_);
data_copy.reserve(tid_map_.size());
for (auto& kv : tid_map_) {
data_copy.emplace(kv.first, kv.second->GetData());
}
return data_copy;
return impl_->GetAllThreadDataByValue();
}

// Returns current snapshot of all threads. Make sure there is no thread
// create/destory when using it.
std::unordered_map<uint64_t, std::reference_wrapper<T>>
GetAllThreadDataByRef() {
std::unordered_map<uint64_t, std::reference_wrapper<T>> data_ref;
std::lock_guard<std::mutex> lock(lock_);
data_ref.reserve(tid_map_.size());
for (auto& kv : tid_map_) {
data_ref.emplace(kv.first, std::ref(kv.second->GetData()));
}
return data_ref;
return impl_->GetAllThreadDataByRef();
}

void RegisterData(uint64_t tid, ThreadDataHolder* tls_obj) {
std::lock_guard<std::mutex> lock(lock_);
tid_map_[tid] = tls_obj;
}
private:
// types
class ThreadDataHolder;
class ThreadDataRegistryImpl {
public:
void RegisterData(uint64_t tid, ThreadDataHolder* tls_obj) {
std::lock_guard<std::mutex> lock(lock_);
tid_map_[tid] = tls_obj;
}

void UnregisterData(uint64_t tid) {
if (tid == main_tid) {
return;
void UnregisterData(uint64_t tid) {
std::lock_guard<std::mutex> lock(lock_);
tid_map_.erase(tid);
}
std::lock_guard<std::mutex> lock(lock_);
tid_map_.erase(tid);
}

private:
template <typename Alias = T, typename = std::enable_if_t<
std::is_copy_constructible<Alias>::value>>
std::unordered_map<uint64_t, T> GetAllThreadDataByValue() {
std::unordered_map<uint64_t, T> data_copy;
std::lock_guard<std::mutex> lock(lock_);
data_copy.reserve(tid_map_.size());
for (auto& kv : tid_map_) {
data_copy.emplace(kv.first, kv.second->GetData());
}
return data_copy;
}

std::unordered_map<uint64_t, std::reference_wrapper<T>>
GetAllThreadDataByRef() {
std::unordered_map<uint64_t, std::reference_wrapper<T>> data_ref;
std::lock_guard<std::mutex> lock(lock_);
data_ref.reserve(tid_map_.size());
for (auto& kv : tid_map_) {
data_ref.emplace(kv.first, std::ref(kv.second->GetData()));
}
return data_ref;
}

private:
std::mutex lock_;
std::unordered_map<uint64_t, ThreadDataHolder*> tid_map_; // not owned
};

class ThreadDataHolder {
public:
ThreadDataHolder() {
explicit ThreadDataHolder(
std::shared_ptr<ThreadDataRegistryImpl> registry) {
registry_ = std::move(registry);
tid_ = std::hash<std::thread::id>()(std::this_thread::get_id());
ThreadDataRegistry::GetInstance().RegisterData(tid_, this);
registry_->RegisterData(tid_, this);
}

~ThreadDataHolder() {
ThreadDataRegistry::GetInstance().UnregisterData(tid_);
}
~ThreadDataHolder() { registry_->UnregisterData(tid_); }

T& GetData() { return data_; }

private:
std::shared_ptr<ThreadDataRegistryImpl> registry_;
uint64_t tid_;
T data_;
};

ThreadDataRegistry() = default;
// methods
ThreadDataRegistry() { impl_ = std::make_shared<ThreadDataRegistryImpl>(); }

ThreadDataRegistry(const ThreadDataRegistry&) = delete;

ThreadDataRegistry& operator=(const ThreadDataRegistry&) = delete;

T& CurrentThreadData() {
static thread_local ThreadDataHolder thread_data;
static thread_local ThreadDataHolder thread_data(impl_);
return thread_data.GetData();
}

std::mutex lock_;
std::unordered_map<uint64_t, ThreadDataHolder*> tid_map_; // not owned
// data
std::shared_ptr<ThreadDataRegistryImpl> impl_;
};

} // namespace framework
Expand Down