How can fix NameError: global name is not defined error in python? -
i wrote python code multiplying 2 matrices using threads.but gave error:
raise self._value nameerror: global name 'a' not defined.
i know it's because of defining global a,b matrices i'm new in python , don't know how fix it. how can fix problem?
import numpy import random import numpy np random import randint import multiprocessing, numpy, ctypes def linemult(start): #global a, b, c, part n = len(a) in xrange(start, start+part): k in xrange(n): j in xrange(n): c[i][j] += a[i][k] * b[k][j] def creationthreads(a, b, threadnumber): n = len(a) pool = multiprocessing.pool(threadnumber) pool.map(linemult, range(0,n, part)) return c if __name__ == "__main__": import argparse, sys argparse import argumentparser temp=[] #initializing matrices------------------ size = int(raw_input('enter n: ')) print('initializing a...') = [] in range (0, size): new = [] j in range (0, size): new.append(randint(-1000,1000)) a.append(new) print ('matrice initialized with: ',a) print('initializing b...') b = [] in range (0, size): new = [] j in range (0, size): new.append(randint(-1000,1000)) b.append(new) print ('matrice b initialized with: ',b) # #finish initializing matrices threadnumber = 2 part=size / threadnumber if part < 1: part = 1 temp= creationthreads(a, b, threadnumber)
Comments
Post a Comment