<jupyter_start><jupyter_text>0. Prepare<jupyter_code>!nvidia-smi
!pip uninstall numpy
!pip install numpy==1.14.6
!pip install mxnet-cu100
# !pip install d2l
import mxnet as mx
import matplotlib.pyplot as plt
import numpy as np

from time import time

# Testing that GPU works.
a = mx.nd.ones((2, 3), mx.gpu())
b = a * 2 + 1
print(b)<jupyter_output>[[3. 3. 3.]
 [3. 3. 3.]]
<NDArray 2x3 @gpu(0)><jupyter_text>1. Computing C=AB using CPU<jupyter_code># construct two matrices A and B
tic = time()

A = mx.nd.random.normal(shape=(4096,4096))
A.wait_to_read()
B = mx.nd.random.normal(shape=(4096,4096))
B.wait_to_read()

cpu_t0 = time()-tic
print("Construct two 4096*4096 matrices using CPU takes {0}s.".format(format(cpu_t0, '0.2f')))
# case1. compute C=AB using CPU
tic = time()

res = mx.nd.dot(A, B)
res.wait_to_read()

cpu_t1 = time()-tic
print("Compute C=AB using CPU takes {0}s.".format(format(cpu_t1, '0.2f')))<jupyter_output>Compute C=AB using CPU takes 2.19s.<jupyter_text>2. Computing C=AB using GPU<jupyter_code># construct two matrices A and B
tic = time()

A = mx.nd.random.normal(shape=(4096,4096), ctx=mx.gpu())
A.wait_to_read()
B = mx.nd.random.normal(shape=(4096,4096), ctx=mx.gpu())
B.wait_to_read()

gpu_t0 = time()-tic
print("Construct two 4096*4096 matrices using CPU takes {0}s.".format(format(gpu_t0, '0.2f')))
# case2. compute C=AB using GPU
tic = time()

res = mx.nd.dot(A, B)
res.wait_to_read()

gpu_t1 = time()-tic
print("Construct two 4096*4096 matrices using CPU takes {0}s.".format(format(gpu_t1, '0.2f')))<jupyter_output>Construct two 4096*4096 matrices using CPU takes 0.05s.<jupyter_text>3.Time Comparision<jupyter_code>x = range(2)
y = [cpu_t0, gpu_t0]

fig, ax = plt.subplots()
ax.set_title('Time for constructing A&B')
ax.annotate
plt.bar(x, y, width=0.3)
plt.xticks(x, ('CPU', 'GPU'))
plt.ylabel('time/s')

for a,b in zip(x,y):
  plt.text(a, b+0.01, '%.02f' % b, ha='center', va= 'bottom',fontsize=10)

plt.ylim(0,max(y)+0.12)
plt.show()
x = range(2)
y = [cpu_t1, gpu_t1]

fig, ax = plt.subplots()
ax.set_title('Time for computing C=AB')
plt.bar(x, y, width=0.3)
plt.xticks(x, ('CPU', 'GPU'))
plt.ylabel('time/s')
for a,b in zip(x,y):
  plt.text(a, b+0.01, '%.02f' % b, ha='center', va= 'bottom',fontsize=10)
plt.ylim(0,max(y)+0.2)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18820468343451865, 'fraction_numerical': 0.06418039895923677, 'mean_word_length': 5.218328840970351, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '550882', 'license': 'Apache-2.0', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1034, 'n_words': 230, 'path': 'compare-cpu-gpu.ipynb', 'repo_name': 'gray0018/Compare-CPU-GPU', 'size': '2306'}