1 Importthreading, time2 ImportNumPy as NP3res = []4 classMyThread (Threading. Thread):5 def __init__(self,i,j,m1,m2):6Threading. Thread.__init__(self)7self.x, self.y =i,j8SELF.M1, self.m2 =M1, M29 defRun (self):Ten GlobalRes, lock One ifLock.acquire (): AM1 = self.m1[self.m1[:,0]==self.x] -M2 = self.m2[self.m2[:,1]==Self.y] -Value =0. the forItem1inchM1: - forItem2inchm2: - ifITEM1[1] = =Item2[0]: -Value + = Item1[2]*item2[2] + res.append ([Self.x,self.y,value]) - lock.release () + if "__main__"==__name__: Am1 = [[2,2],[0,0,1],[0,1,2],[1,0,3],[1,1,4]] atM2 = [[2,3],[0,0,2],[0,2,1],[1,2,3],[1,1,4]] -S1, s2 =m1[0], m2[0] - assertS1[1]==s2[0],'mismatch' -M1_value = Np.array (m1[1:]) -M2_value = Np.array (m2[1:]) -rows, cols = s1[0], s2[1] in res.append ([Rows, cols]) -Threadlist = [] toLock =Threading. Lock () + forIinchrange (rows): - forJinchRange (cols): thet =MyThread (I,j,m1_value,m2_value) * threadlist.append (t) $ forTinchthreadlist:Panax Notoginseng T.start () - forTinchthreadlist: the T.join () + Print(RES)
Python Multithreading sparse matrix multiplication