title: Python Multithreaded Matrix Multiplication
body: <p>I am doing a project for the end of the semester and I need to be able to take a matrix to a power and I need to make the problem multithreaded.</p>
  
  <p>This code works in some situations and does not in other situations. I believe that it has to do with the logic in the nested loops in the process_data function but I am not sure what I am doing wrong! I have been working on this for a couple weeks and I am absolutely stumped. It seems like it has something to do with my threads going out of bounds but even then I am not very sure because there are some situations where the threads go out of bounds but then still calculates the matrices properly.</p>
  
  <p>Please help!</p>
  
  <pre><code>import copy
  import numpy
  import Queue
  import random
  import threading
  import time
  import timeit
  
  # Create variable that determines the number of columns and
  # rows in the matrix.
  n = 4
  
  # Create variable that determines the power we are taking the
  # matrix to.
  p = 2
  
  # Create variable that determines the number of threads we are
  # using.
  t = 2
  
  # Create an exit flag.
  exitFlag = 0
  
  # Create threading class.
  class myThread (threading.Thread):
      def __init__(self, threadID, name, q):
          threading.Thread.__init__(self)
          self.threadID = threadID
          self.name = name
          self.q = q
      def run(self):
          print "Starting " + self.name
          process_data(self.name, self.q)
          print "Exiting " + self.name
  
  # Create a function that will split our data into multiple threads
  # and do the matrix multiplication.
  def process_data(threadName, q):
      numCalc = ((n^3)/t)
      for a in range(p-1):
          for b in range((numCalc*(q-1)),(numCalc*(q))):
              for c in range(n):
                  for d in range(n):
                      matrix[a+1][b][c] += matrix[a][b][d] * matrix[0][d][c]
  
  # Create a three dimensional matrix that will store the ouput for
  # each power of the matrix multiplication.
  matrix = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(p)]
  print matrix
  
  # This part fills our initial n by n matrix with random numbers
  # ranging from 0 to 9 and then prints it!
  print "Populating Matrix!"
  for i in range(n):
      for j in range(n): 
          matrix[0][i][j] = random.randint(0,9)
  
  # Tells the user that we are multiplying matrices and starts the
  # timer.
  print "Taking our matrix to the next level!"
  start = timeit.default_timer()
  threadLock = threading.Lock()
  
  threads = []
  threadID = 1
  
  # Create new threads
  for tName in range(t):
      thread = myThread(threadID, "Thread-0"+str(tName), threadID)
      thread.start()
      threads.append(thread)
      threadID += 1
  # Wait for all threads to complete
  for x in threads:
      x.join()
  
  stop = timeit.default_timer()
  print stop - start 
  print "Exiting main thread!"
  print matrix
  </code></pre>
  
  <p>Taking the matrix squared seems to work in every case but if I try to calculate beyond that the remaining powers come out with matrices that are filled with zeroes! The case that I have posted works.</p>
  
  <p>When I change the n, p and t variables is when I run into problems where it does not calculate properly.</p>
  
  <p>Thank you for your time.</p>