Make each thread have its own default stream (#3281)

This commit is contained in:
Cheng
2026-03-25 15:48:49 +09:00
committed by GitHub
parent 57c813f042
commit df7f7db943
14 changed files with 175 additions and 123 deletions
+29
View File
@@ -36,6 +36,35 @@ TEST_CASE("test stream management") {
}
}
TEST_CASE("test default stream in threads") {
std::set<Stream> thread_streams;
std::mutex mtx;
std::vector<std::thread> threads;
auto all_streams = get_streams();
size_t num_streams = all_streams.size();
size_t num_threads = 4;
for (size_t i = 0; i < num_threads; ++i) {
threads.emplace_back([&thread_streams, &mtx]() {
auto s = default_stream(gpu::is_available() ? Device::gpu : Device::cpu);
std::lock_guard lock(mtx);
thread_streams.insert(s);
});
}
for (auto& t : threads) {
t.join();
}
CHECK_EQ(thread_streams.size(), num_threads);
all_streams = get_streams();
CHECK_EQ(all_streams.size() - num_streams, num_threads);
std::set new_streams(all_streams.begin() + num_streams, all_streams.end());
CHECK_EQ(new_streams, thread_streams);
}
TEST_CASE("test get streams") {
auto streams = get_streams();