Created
September 7, 2017 19:34
-
-
Save marw/bb7315533c6ab462edc38680f2bd506c to your computer and use it in GitHub Desktop.
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
import time | |
import argparse | |
import multiprocessing | |
import random | |
import numpy as np | |
M = 250 | |
# size ~ 0.5MB | |
X = np.random.randn(M, M) | |
# to allow CPU with 6 cores to end tasks at once | |
TASK_N = 180 | |
# size ~ 90MB | |
MATRICES = 180 | |
def small_mat_test(i): | |
for i in range(TASK_N): | |
X.dot(X) | |
def small_mat(args): | |
""" | |
Testing small matrices that should fit in CPU cache. | |
""" | |
pool = multiprocessing.Pool() | |
start_time = time.time() | |
pool.map(small_mat_test, list(range(args.n))) | |
elapsed_time = time.time() - start_time | |
return elapsed_time | |
def list_mat(args): | |
""" | |
Testing list of bigger matrices that probably not fit in CPU cache. | |
""" | |
arrays = [] | |
for i in range(MATRICES): | |
arrays.append(np.random.randn(M, M)) | |
def list_mat_test(i): | |
idx1 = list(range(MATRICES)) | |
idx2 = list(range(MATRICES)) | |
random.shuffle(idx1) | |
random.shuffle(idx2) | |
for i in range(MATRICES): | |
arrays[idx1[i]].dot(arrays[idx2[i]]) | |
pool = multiprocessing.Pool() | |
start_time = time.time() | |
pool.map(small_mat_test, list(range(args.n))) | |
elapsed_time = time.time() - start_time | |
return elapsed_time | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--suite', choices=('small_mat', 'list_mat'), default='small_mat') | |
parser.add_argument('--n', help='Number of iterations', default=3*16) | |
args = parser.parse_args() | |
if args.suite == 'small_mat': | |
elapsed_time = small_mat(args) | |
print('[Small Matrix Test] Elapsed: {:0.3f} seconds Matrix: {}x{} Multiplications: {} MulPerSec: {:.2f}'.format( | |
elapsed_time, M, M, args.n * TASK_N, (args.n * TASK_N)/elapsed_time)) | |
elif args.suite == 'list_mat': | |
elapsed_time = list_mat(args) | |
print('[List Matrix Test] Elapsed: {:0.3f} seconds Matrix: {}x{} x {} Multiplications: {} MulPerSec: {:.2f}'.format( | |
elapsed_time, M, M, MATRICES, args.n * MATRICES, (args.n * MATRICES) / elapsed_time)) | |
else: | |
raise ValueError('Nobody expects invalid parameter!') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My various results, more or less correlate with https://www.cpubenchmark.net/