我使用threads.编写了这个用于乘以2个矩阵的python代码。但是我给出了这个错误:
raise self._value
NameError: global name 'A' is not defined.
我知道这是因为定义了全局A,B矩阵,但我是python中的新手,我不知道如何修复它。 我该如何解决这个问题?
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)