C++ Tensorflow,如何使用多线程制作session-> Run(),或者花费更少的时间

标签 c++ multithreading session tensorflow

我在 CPU 上运行以下所有内容。 我运行示例 ensorflow/examples/label_image 花费 7~8 秒。据我所知,python 花费大约 0.5 秒来处理相同的样本,这是因为“TensorFlow Session 对象是多线程的,因此多个线程可以轻松地使用相同的 session 并并行运行操作。”。 但是,如何使用 C++ 在 Session 上设置多线程。

我尝试... 我在“tensorflow/tensorflow/core/common_runtime/direct_session.cc”中硬编码第 81 行: “const int32 num_threads = 16;” 但是,它不起作用。

我如何设置一些配置或我应该做什么?

最佳答案

使用多线程怎么样?

std::vector<std::thread> threads;

for (std::size_t i = 0; i < 10; ++i) {
   threads.push_back(std::thread([&]{ session->Run(); })); 
}


for (std::size_t i = 0; i < 10; ++i) {
   threads[i].join();
}

关于C++ Tensorflow,如何使用多线程制作session-> Run(),或者花费更少的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714533/

相关文章:

c++ - Printf 以不熟悉的方式使用

python - 在 Python 中使用 UEye API

java - 在 Java 中停止线程

java - 将 Java applet 参数保存到 session 或 cookie 中

php - 如何使用 redis 库在 codeigniter 中通过 sessionid 更改 session 数据?

c++ - 使用 String c_str() 返回 char*

C++ : Calling inherited static method from static method

java - Web 模块之间是否共享线程?

multithreading - 使用 Clojure 线程化长时间运行的进程并比较它们的返回

php - session_set_save_handler 导致登录循环