-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.py
58 lines (48 loc) · 2.09 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import tensorflow as tf
print("matrix matmul in cpu")
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
with tf.device('/cpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')
c = tf.matmul(a,b)
print(sess.run(c))
print("matrix matmul in gpu with 70% gpu memrory")
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
with tf.Session(config=tf.ConfigProto(log_device_placement=True, gpu_options=gpu_options)) as sess:
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')
c = tf.matmul(a,b)
print(sess.run(c))
print("matrix matmul in gpu that not exit, but use allow_soft_placement flag")
print()
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
with tf.device('/gpu:4'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')
c = tf.matmul(a,b)
print(sess.run(c))
print("Matrix matmul with tower gpus")
# multi-gpu
c = []
for d in ['/gpu:0', '/gpu:1', '/gpu:2']:
with tf.device(d):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')
c.append(tf.matmul(a,b))
with tf.device('/cpu:0'):
sum = tf.add_n(c)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(c)
print(sess.run(sum))
print("matrix matmul in gpu that not exit, with catch tf.erros.InvalidArgumentError exception ")
# must exception
try:
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
with tf.device('/gpu:4'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')
c = tf.matmul(a,b)
print(sess.run(c))
except tf.errors.InvalidArgumentError as e:
print(e)