tensorflow 1.8 測試gpu程式碼
阿新 • • 發佈:2019-01-01
檢視日誌資訊若包含gpu資訊,就是使用了gpu。
下面的程式碼檢視日誌資訊,如果包含gpu資訊就是使用了gpu
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
輸出
2018-08-30 13:06:50.335491: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2018-08-30 13:06:50.431993: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2018-08-30 13:06:50.432381: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.645 pciBusID: 0000:01:00.0 totalMemory: 10.91GiB freeMemory: 10.45GiB 2018-08-30 13:06:50.432412: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0 2018-08-30 13:06:50.580801: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-08-30 13:06:50.580827: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0 2018-08-30 13:06:50.580832: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N 2018-08-30 13:06:50.581014: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10114 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1) Device mapping: /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1 2018-08-30 13:06:50.657554: I tensorflow/core/common_runtime/direct_session.cc:284] Device mapping: /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1
下面的程式碼
import tensorflow as tf with tf.device('/cpu:0'): a = tf.constant([1.0,2.0,3.0],shape=[3],name='a') b = tf.constant([1.0,2.0,3.0],shape=[3],name='b') with tf.device('/gpu:1'): c = a+b #注意:allow_soft_placement=True表明:計算裝置可自行選擇,如果沒有這個引數,會報錯。 #因為不是所有的操作都可以被放在GPU上,如果強行將無法放在GPU上的操作指定到GPU上,將會報錯。 sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) #sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) sess.run(tf.global_variables_initializer()) print(sess.run(c))
輸出
2018-08-30 13:08:39.847329: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2018-08-30 13:08:39.941591: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2018-08-30 13:08:39.941946: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.645 pciBusID: 0000:01:00.0 totalMemory: 10.91GiB freeMemory: 10.45GiB 2018-08-30 13:08:39.941958: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0 2018-08-30 13:08:40.091625: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-08-30 13:08:40.091653: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0 2018-08-30 13:08:40.091657: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N 2018-08-30 13:08:40.091841: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10113 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1) Device mapping: /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1 2018-08-30 13:08:40.168198: I tensorflow/core/common_runtime/direct_session.cc:284] Device mapping: /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1 init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0 2018-08-30 13:08:40.168906: I tensorflow/core/common_runtime/placer.cc:886] init: (NoOp)/job:localhost/replica:0/task:0/device:GPU:0 add: (Add): /job:localhost/replica:0/task:0/device:GPU:0 2018-08-30 13:08:40.168952: I tensorflow/core/common_runtime/placer.cc:886] add: (Add)/job:localhost/replica:0/task:0/device:GPU:0 b: (Const): /job:localhost/replica:0/task:0/device:CPU:0 2018-08-30 13:08:40.168959: I tensorflow/core/common_runtime/placer.cc:886] b: (Const)/job:localhost/replica:0/task:0/device:CPU:0 a: (Const): /job:localhost/replica:0/task:0/device:CPU:0 2018-08-30 13:08:40.168964: I tensorflow/core/common_runtime/placer.cc:886] a: (Const)/job:localhost/replica:0/task:0/device:CPU:0 [2. 4. 6.]