Answer the question
In order to leave comments, you need to log in
How to speed up matrix multiplication in numpy?
hello! please help
me I have 2 matrices of size 2500*2500 filled with random numbers from 0 to 500. First I divide matrices A and B into submatrices using numpy.array_slit. Then I multiply the submatrices using dot in one thread. If you do that, it takes a lot of time.
I want to do submatrix multiplication in separate threads. That is, for example, I divide the matrix A into two parts into a1 and a2. I also divide the B matrix into b1 and b1. I want to multiply a1 and b1 in tread1, and multiply a2 and b2 in tread2. How to implement it? Is there any method in numpy to multiply submatrices separately?
here is the code:
import threading
import numpy as np
import timeit
import time
thread_lock = threading.Lock()
class MyThread(threading.Thread):
def __init__(self, name, delay, a, b):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
self.a= a
self.b = b
def run(self):
print('Starting thread %s.' % self.name)
thread_lock.acquire()
matrix_multiplctn(self.a,self.b, self.delay)
thread_lock.release()
print('Finished thread %s.' % self.name )
def matrix_multiplctn(a, b, delay):
a1=np.array_split(a, 2, axis=1) #разделяю по столбцам
b1=np.array_split(b, 2, axis=0) #разделяю по строкам
start_time = timeit.default_timer()
c = np.dot(a1,b1)
print(timeit.default_timer() - start_time)
time.sleep(delay)
print(c)
a=np.random.randint(0,500, size=(2500,2500))
b=np.random.randint(0,500, size=(2500,2500))
thread = MyThread("A", 0.1, a, b)
start_time1 = timeit.default_timer()
thread.start()
thread.join()
print("Время выполнения A: ", timeit.default_timer() - start_time1)
print('Finished.')
Answer the question
In order to leave comments, you need to log in
Do not do this.
First, the importance of row-major and column-major order in this operation should be taken into account:
Secondly, the transition from int32 to float32 (or float64) gives a radical speedup due to BLAS :
BLAS is already used in numpy "under the hood" (by at least in the Anaconda distribution), so you should not explicitly call these functions manually - as shown above, this will be slower.
PS Theory in a nutshell:
Performance Tips of NumPy ndarray
Understanding the internals of NumPy to avoid unne...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question