3333#include " paddle/phi/common/type_traits.h"
3434#include " paddle/phi/extension.h"
3535
36- // #define ENABLE_ASYNC_RUN
36+ #define ENABLE_ASYNC_RUN
3737
3838class HpuOperator {
3939 public:
@@ -88,78 +88,64 @@ class GlobalWorkStreamExecutor {
8888 }
8989
9090 template <typename R>
91- std::future<R> async (std::function<R()> func) {
91+ std::future<R> async (synStreamHandle stream, std::function<R()> func) {
9292 auto task = std::make_shared<std::packaged_task<R ()>>(std::move (func));
9393 std::future<R> res = task->get_future ();
94- add_task ([task]() { (*task)(); });
94+ add_task (stream, [task]() { (*task)(); });
9595 return res;
9696 }
9797
9898 template <typename F>
99- auto async (F&& func) -> std::future<decltype(func())> {
99+ auto async (synStreamHandle stream, F&& func)
100+ -> std::future<decltype(func())> {
100101 using R = decltype (func ());
101102 auto task =
102103 std::make_shared<std::packaged_task<R ()>>(std::forward<F>(func));
103104 std::future<R> res = task->get_future ();
104- add_task ([task]() { (*task)(); });
105+ add_task (stream, [task]() { (*task)(); });
105106 return res;
106107 }
107108
108109 template <typename R>
109- R sync (std::function<R()> func) {
110- return async (std::move (func)).get ();
110+ R sync (synStreamHandle stream, std::function<R()> func) {
111+ return async (stream, std::move (func)).get ();
111112 }
112113
113114 template <typename F>
114- auto sync (F&& func) -> decltype(func()) {
115- return async (std::forward<F>(func)).get ();
115+ auto sync (synStreamHandle stream, F&& func) -> decltype(func()) {
116+ return async (stream, std::forward<F>(func)).get ();
116117 }
117118
118119 private:
119- GlobalWorkStreamExecutor () {
120- worker_ = std::thread ([this ]() {
121- while (true ) {
122- std::function<void ()> task;
123- {
124- std::unique_lock<std::mutex> lock (queue_mutex_);
125- condition_.wait (lock, [this ] { return !tasks_.empty () || stop_; });
126-
127- if (stop_ && tasks_.empty ()) break ;
128-
129- task = std::move (tasks_.front ());
130- tasks_.pop ();
131- }
132- task (); // 执行任务
133- }
134- });
135- }
136-
120+ struct WorkerThread {
121+ std::thread thread;
122+ std::queue<std::function<void ()>> tasks;
123+ std::mutex mutex;
124+ std::condition_variable condition;
125+ bool stop = false ;
126+ };
127+
128+ GlobalWorkStreamExecutor () = default ;
137129 ~GlobalWorkStreamExecutor () {
138- {
139- std::lock_guard<std::mutex> lock (queue_mutex_);
140- stop_ = true ;
130+ for (auto & [stream, worker] : workers_) {
131+ {
132+ std::lock_guard<std::mutex> lock (worker->mutex );
133+ worker->stop = true ;
134+ }
135+ worker->condition .notify_all ();
136+ if (worker->thread .joinable ()) {
137+ worker->thread .join ();
138+ }
141139 }
142- condition_.notify_all ();
143- if (worker_.joinable ()) worker_.join ();
144140 }
145141
146- void add_task (std::function<void ()> task) {
147- {
148- std::lock_guard<std::mutex> lock (queue_mutex_);
149- tasks_.emplace (std::move (task));
150- }
151- condition_.notify_one ();
152- }
142+ void add_task (const synStreamHandle stream, std::function<void ()> task);
153143
154- // 删除拷贝构造和赋值
155144 GlobalWorkStreamExecutor (const GlobalWorkStreamExecutor&) = delete ;
156145 GlobalWorkStreamExecutor& operator =(const GlobalWorkStreamExecutor&) = delete ;
157146
158- std::thread worker_;
159- std::queue<std::function<void ()>> tasks_;
160- std::mutex queue_mutex_;
161- std::condition_variable condition_;
162- bool stop_ = false ;
147+ std::unordered_map<synStreamHandle, std::shared_ptr<WorkerThread>> workers_;
148+ std::mutex workers_mutex_;
163149};
164150#endif
165151
@@ -175,6 +161,7 @@ class RecipeRunner {
175161 void Run (C_Stream stream, std::map<std::string, uint64_t > tensors) {
176162 synRecipeHandle recipehandle = this ->recipeHandle_ ;
177163 auto future = GlobalWorkStreamExecutor::instance ().async (
164+ reinterpret_cast <synStreamHandle>(stream),
178165 [this , stream, tensors, recipehandle] {
179166 ExecuteRecipe (stream, tensors, recipehandle);
180167 });
0 commit comments