操作系统问题三、多线程矩阵乘法(这个挺有意思,跟向量处理机原理相关)

普通算法,后面还有更快的strassen算法,原理不讲了,讲原理的汗牛充栋了都。代码参考一下。

from threading import Thread
import time
import random


'''
:function SP_Matrix: 作用是把矩阵A分解成四个4个n/2×n/2的子矩阵。
:function Merge_Matrix: 作用是把四个4个n/2×n/2的子矩阵合并为一个n×n的矩阵。
:function Add_Matrix: 作用是计算矩阵A和B的加。
'''

class BasicThread(Thread):
    def __init__(self, func, args):
        '''
        :param func: 调用的对象
        :param args: 调用对象的参数
        '''
        Thread.__init__(self)
        self.func = func
        self.args = args
        self.result = None

    def run(self):
        self.result = self.func(*self.args)

def Dot_Matrix(A, B):
    n_row = len(A)
    n_column = len(A[0])
    n = min(n_column, n_row)
    C = [[0 for col in range(n)] for row in range(n)]
    if n_row == 1:
        for i in range(n_column):
            C[0][0] += A[0][i]*B[0][i]
    elif n_column == 1:
        for i in range(n_row):
            C[0][0] += A[i][0]*B[i][0]
    else:
        (A11, A12, A21, A22) = SP_matrix(A)
        (B11, B12, B21, B22) = SP_matrix(B)
        T1 = BasicThread(func=Dot_Matrix, args=(A12, B21))
        T2 = BasicThread(func=Dot_Matrix, args=(A12, B22))
        T3 = BasicThread(func=Dot_Matrix, args=(A22, B21))
        T4 = BasicThread(func=Dot_Matrix, args=(A22, B22))
        T5 = BasicThread(func=Dot_Matrix, args=(A11, B11))
        T6 = BasicThread(func=Dot_Matrix, args=(A11, B12))
        T7 = BasicThread(func=Dot_Matrix, args=(A21, B11))
        T8 = BasicThread(func=Dot_Matrix, args=(A21, B12))
        # start threading
        T1.start()
        T2.start()
        T3.start()
        T4.start()
        T5.start()
        T6.start()
        T7.start()
        T8.start()
        # wait to be done
        T1.join()
        T2.join()
        T3.join()
        T4.join()
        T5.join()
        T6.join()
        T7.join()
        T8.join()
        C11A = T1.result
        C12A = T2.result
        C21A = T3.result
        C22A = T4.result
        C11B = T5.result
        C12B = T6.result
        C21B = T7.result
        C22B = T8.result
        C = Merge_Matrix(Add_Matrix(C11A,C11B), Add_Matrix(C12A,C12B), Add_Matrix(C21A,C21B), Add_Matrix(C22A,C22B))
    return C

def SP_matrix(A):
    n_row = len(A)
    n_column = len(A[0])
    n2_row = int(n_row / 2)
    n2_column = int(n_column / 2)
    A11 = [[0 for col in range(n2_column)] for row in range(n2_row)]
    A12 = [[0 for col in range(n2_column)] for row in range(n2_row)]
    A21 = [[0 for col in range(n2_column)] for row in range(n2_row)]
    A22 = [[0 for col in range(n2_column)] for row in range(n2_row)]
    for i in range(0, n2_row):
        for j in range(0, n2_column):
            A11[i][j] = A[i][j]
            A12[i][j] = A[i][j + n2_column]
            A21[i][j] = A[i + n2_row][j]
            A22[i][j] = A[i + n2_row][j + n2_column]
    return (A11, A12, A21, A22)


def Merge_Matrix(A11, A12, A21, A22):
    n2 = len(A11)
    n = 2 * n2
    A = [[0 for col in range(n)] for row in range(n)]
    for i in range(0, n):
        for j in range(0, n):
            if i <= (n2 - 1) and j <= (n2 - 1):
                A[i][j] = A11[i][j]

            elif i <= (n2 - 1) and j > (n2 - 1):
                A[i][j] = A12[i][j - n2]
            elif i > (n2 - 1) and j <= (n2 - 1):
                A[i][j] = A21[i - n2][j]
            else:
                A[i][j] = A22[i - n2][j - n2]
    return A

def Add_Matrix(A, B):
    n_row = len(A)
    n_column = len(A[0])
    C = [[0 for col in range(n_column)] for row in range(n_row)]
    for i in range(0, n_row):
        for j in range(0, n_column):
            C[i][j] = A[i][j] + B[i][j]
    return C

if __name__ == "__main__":
    start = time.clock()
    A = [[random.random() for i in range(32)] for j in range(64)]
    B = [[random.random() for i in range(32)] for j in range(64)]
    T_main = BasicThread(func=Dot_Matrix, args=(A, B))
    T_main.start()
    T_main.join()
    end = time.clock()
    print("数组A:")
    print(A)
    print("数组B:")
    print(B)
    print("结果:")
    print(T_main.result)
    print("运行时间:")
    print(end-start)

你可能感兴趣的:(python,多线程,矩阵)