如何在Tensorflow c ++中为sesion配置设置inter_op_parallelism_threads和intra_op_parallelism_threads [英] how to set inter_op_parallelism_threads and intra_op_parallelism_threads for seesion configuration in Tensorflow c++

查看:39
本文介绍了如何在Tensorflow c ++中为sesion配置设置inter_op_parallelism_threads和intra_op_parallelism_threads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在python tensorflow中设置内部和内部线程,如下所示

I know how to set intra and inter threads in python tensorflow which is as shown below

n_cpus=1
sess = tf.Session(config=tf.ConfigProto(
     device_count={ "CPU": n_cpus },
    inter_op_parallelism_threads=n_cpus,
    intra_op_parallelism_threads=8
));

但我需要在 C++ 中实现它.所以在 tensorflow c++ 中设置内部和内部线程的任何等效代码??

But I need to implement this in c++. SO any equivalent code to set inter and intra threads in tensorflow c++ ??

推荐答案

您可以使用以下代码在 C++ 中实现.

You can implement this in C++ by using the following code.

Status LoadGraph(string graph_file_name, std::unique_ptr<tensorflow::Session>* session) {
  tensorflow::GraphDef graph_def;
  Status load_graph_status =
      ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &graph_def);
  if (!load_graph_status.ok()) {
    return tensorflow::errors::NotFound("Failed to load compute graph at '",
                                        graph_file_name, "'");
  }
  
  // initialize the number of worker threads
    tensorflow::SessionOptions options;
    tensorflow::ConfigProto & config = options.config;
    config.set_inter_op_parallelism_threads(1);
    config.set_intra_op_parallelism_threads(1);
    config.set_use_per_session_threads(false); 

  session->reset(tensorflow::NewSession(options));
  Status session_create_status = (*session)->Create(graph_def);
  if (!session_create_status.ok()) {
    return session_create_status;
  }

  return Status::OK();
}

这篇关于如何在Tensorflow c ++中为sesion配置设置inter_op_parallelism_threads和intra_op_parallelism_threads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆