How to fix NameError: global name undefined error in python?

I wrote this python code to multiply 2 matrices using threads.but, I gave this error:

    raise self._value
NameError: global name 'A' is not defined.

      

I know this due to the definition of global A, B matrices, but I am new to python and I don’t know how to fix this. how can i fix this problem?

import numpy
import random
import numpy as np
from random import randint
import multiprocessing, numpy, ctypes

def lineMult(start):
    #global A, B, C, part
    n = len(A)
    for i in xrange(start, start+part):
        for k in xrange(n):
            for 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
    from argparse import ArgumentParser

    temp=[]
    #initializing matrices------------------
    size = int(raw_input('Enter n: '))
    print('initializing A...')
    A = []
    for i in range (0, size):
        new = []
        for j in range (0, size):
            new.append(randint(-1000,1000))
        A.append(new)
    print ('matrice A initialized with:    ',A)

    print('initializing B...')
    B = []
    for i in range (0, size):
        new = []
        for 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)

      

+3


source to share





All Articles