|
import numpy as np |
|
import tensorflow.compat.v1 as tf |
|
tf.disable_v2_behavior() |
|
from datetime import datetime |
|
|
|
|
|
devices = ['cpu', 'gpu'] |
|
|
|
|
|
|
|
shapes = [(50, 50), (100, 100), (500, 500), (1000, 1000)] |
|
|
|
|
|
def compute_operations(device, shape): |
|
"""Run a simple set of operations on a matrix of given shape on given device |
|
|
|
Parameters |
|
---------- |
|
device : the type of device to use, either 'cpu' or 'gpu' |
|
shape : a tuple for the shape of a 2d tensor, e.g. (10, 10) |
|
|
|
Returns |
|
------- |
|
out : results of the operations as the time taken |
|
""" |
|
|
|
|
|
with tf.device(device): |
|
random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1) |
|
dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix)) |
|
sum_operation = tf.reduce_sum(dot_operation) |
|
|
|
|
|
start_time = datetime.now() |
|
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session: |
|
result = session.run(sum_operation) |
|
elapsed_time = datetime.now() - start_time |
|
|
|
return result, elapsed_time |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
for device in devices: |
|
print("--" * 20) |
|
|
|
for shape in shapes: |
|
_, time_taken = compute_operations(device, shape) |
|
|
|
|
|
print("Input shape:", shape, "using Device:", device, "took: {:.2f}".format(time_taken.seconds + time_taken.microseconds/1e6)) |
|
|
|
|
|
print("--" * 20) |