multi-threading of a matrix multiplication in python
Question: I want to create n threads and that each thread computes an entire row of the
result matrix. I have tried the following code,

    
    
    import numpy
    import random
    import threading
    
    class MatrixMult(threading.Thread):
        """A thread which computes the i,j entry of A * B"""
        def __init__(self, A, B, i):
            super(MatrixMult, self).__init__()
            self.A = A
            self.B = B
            self.i = i
            #self.j = j                                                             
        def run(self):
            print "Computing %i, %i" % (self.i, self.i)
            x = 0
            result=[]
            for k in range(self.A.shape[0])
                x += self.A[self.i,k] * self.B[k,self.i  
            self.result=x
            print "Completed %i, %i" % (self.i, self.j)
    
    def mult(n):
        """A function to randomly create two n x n matrices and multiply them"""
        # Create two random matrices                                                
        A = numpy.zeros((n,n))
        B = numpy.zeros((n,n))
        for i in range(n):
            for j in range(n):
                A[i,j] = random.randrange(0, 100)
                B[i,j] = random.randrange(0, 100)
        # Create and start the threads                                              
        threads = []
        for i in range(n):
           # for j in range(n):                                                     
            t = MatrixMult(A, B, i)
                threads.append(t)
                t.start()
        for t in threads: t.join()
        C = numpy.zeros((n,n))
        for t in threads:
            C[t.i] = t.result
        return C
    print multi(30)
    

however it prints out many weird matrices:

    
    
    [ 66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.
       66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.
       66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.  66695.
       66695.  66695.  66695.]
     [ 88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.
       88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.
       88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.  88468.
       88468.  88468.  88468.]]
    

anyone see a problem in my code? I don't see what i am doing wrong.


Answer: Your code sets

    
    
    C[t.i] = t.result
    

which sets an entire row of `C` to the value `t.result`, which is a scalar. I
see some commented stuff about `j` in there; you presumably want to account
for that, and also fix

    
    
    x += self.A[self.i,k] * self.B[k,self.i
    

to use `j` (and also not be a syntax error). As-is, it seems like you're
computing `C[i, i]` and then assigning that value to the whole row.

Also: you know this code is guaranteed to be much, much, much slower than
`np.dot`, right? Between doing tight loops in python, distributing
computational work across threads despite the
[GIL](http://wiki.python.org/moin/GlobalInterpreterLock), and also being [an
inefficient algorithm for matrix
multiplication](http://en.wikipedia.org/wiki/Matrix_multiplication#Algorithms_for_efficient_matrix_multiplication)
in the first place. If your goal is actually to speed up matrix multiplies
using multiple cores, link your numpy to MKL, OpenBLAS, or ACML, use `np.dot`,
and call it a day.

