Created
December 9, 2020 20:21
-
-
Save mpvasilis/817ac3901a876507d367e54a1d481444 to your computer and use it in GitHub Desktop.
TensorFlow 2 - CPU vs GPU times calculation on matrix multiplications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import tensorflow as tf | |
import time | |
import os | |
tf.compat.v1.disable_eager_execution() | |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' | |
def get_times(maximum_time): | |
device_times = { | |
"/gpu:0":[], | |
"/cpu:0":[] | |
} | |
matrix_sizes = range(500,50000,100) | |
for size in matrix_sizes: | |
print("####### Matrix size: " + str(size) + " #######") | |
for device_name in device_times.keys(): | |
print("####### Calculating on the " + device_name + " #######") | |
shape = (size,size) | |
data_type = tf.float16 | |
with tf.device(device_name): | |
r1 = tf.random.uniform(shape=shape, minval=0, maxval=1, dtype=data_type) | |
r2 = tf.random.uniform(shape=shape, minval=0, maxval=1, dtype=data_type) | |
dot_operation = tf.matmul(r2, r1) | |
with tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=False)) as session: | |
start_time = time.time() | |
result = session.run(dot_operation) | |
time_taken = time.time() - start_time | |
#print(result) | |
device_times[device_name].append(time_taken) | |
print("Time taken:", time_taken) | |
if time_taken > maximum_time: | |
return device_times, matrix_sizes | |
device_times, matrix_sizes = get_times(10) # Change the number to specify maximum cut-off compute time (in seconds) after which the comparison script is terminated and results displayed. | |
print(device_times) | |
gpu_times = device_times["/gpu:0"] | |
cpu_times = device_times["/cpu:0"] | |
plt.plot(matrix_sizes[:len(gpu_times)], gpu_times, 'o-') | |
plt.plot(matrix_sizes[:len(cpu_times)], cpu_times, 'o-') | |
plt.ylabel('Time') | |
plt.xlabel('Matrix size') | |
plt.show() | |
plt.plot(matrix_sizes[:len(cpu_times)], [a/b for a,b in zip(cpu_times,gpu_times)], 'o-') | |
plt.ylabel('CPU Time / GPU Time') | |
plt.xlabel('Matrix size') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment