Last active
September 5, 2022 04:08
-
-
Save DhruvilKarani/a1261edea61406348924232895c36493 to your computer and use it in GitHub Desktop.
Matrix multiplication using multiprocessing without shared memory
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 numpy as np | |
from typing import List | |
from time import time | |
from multiprocessing import Pool | |
print("--No shared memory---") | |
M = 100000 | |
d = 32 | |
N = 1000 | |
matrix = np.random.randn(M, d) | |
query_vectors = [np.random.randn(1, d) for i in range(N)] | |
def get_numpy_cosine_for_query(query, np_matrix=matrix): | |
return np.matmul(np_matrix, query.T) | |
def numpy_cosine_online_multiprocessing_nn(np_matrix: np.array, queries: List[np.array]): | |
with Pool(4) as pool: | |
result = list(pool.starmap(get_numpy_cosine_for_query, queries)) | |
time_taken_list = [] | |
for _ in range(10): | |
start = time() | |
numpy_cosine_online_multiprocessing_nn(matrix, query_vectors) | |
end = time() | |
time_taken_list.append(end-start) | |
print("Time taken:", np.mean(time_taken_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment