use*_*931 6 c++ cpu memory-management gpu tensorflow
TensorFlow正在使用分配我的所有GPU内存并忽略我的命令来使用CPU,我该如何解决这个问题呢?
这是我的代码摘录 testprog
Session *session;
SessionOptions opts = SessionOptions();
//force to allocate 0 memory on gpu
opts.config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(0);
opts.config.mutable_gpu_options()->set_allow_growth(false);
//create session with these settings
TF_CHECK_OK(NewSession(opts, &session));
TF_CHECK_OK(session->Create(graph_def));
//set device to cpu
graph::SetDefaultDevice("/cpu:0", &graph_def);
//run arbitrary model
Status status = session->Run(classifierInput, {output_layer},{},&outputs);
TF_CHECK_OK(session->Close());
Run Code Online (Sandbox Code Playgroud)
打电话nvidi-smi告诉我:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.66 Driver Version: 375.66 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Quadro P4000 Off | 0000:01:00.0 Off | N/A |
| N/A 50C P0 28W / N/A | 7756MiB / 8114MiB | 42% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1784 G /usr/bin/X 139MiB |
| 0 3828 G qtcreator 28MiB |
| 0 7721 C ...testprog/build/testprog 7585MiB |
+-----------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
由于这个问题用C++标记.解决方案是
tensorflow::Session *sess;
tensorflow::SessionOptions options;
tensorflow::ConfigProto* config = &options.config;
// disabled GPU entirely
(*config->mutable_device_count())["GPU"] = 0;
// place nodes somewhere
config->set_allow_soft_placement(true);
Run Code Online (Sandbox Code Playgroud)
请参阅此处的示例.我的另一篇文章,TensorFlow如何放置节点.
编辑:有GitHub问题.你可以试试:
#include <stdlib.h>
setenv("CUDA_VISIBLE_DEVICES", "", 1);
Run Code Online (Sandbox Code Playgroud)
要么
auto gpu_options = config->gpu_options();
gpu_options.set_visible_device_list("");
Run Code Online (Sandbox Code Playgroud)
但这可能会给你failed call to cuInit: CUDA_ERROR_NO_DEVICE.