I would like your help to do this work, I have little practice in programming and I need to do in Python that I am still learning.
The work is as follows:
This work consists of dividing the matrix multiplication work into small tasks that will be distributed to SD workers.
Each task will consist of a row and column multiplication, which will be placed in the task bag.
Each entity performs the following activities:
Client: Read input matrices A and B, divide into small T tasks (row x column) and inserts each of the tasks into the Bag. It then collects result to result (as it gets ready) and assembles the resulting matrix C, which will be printed on the screen at the end of processing.
Tote Bag: Receives the Client's tasks and queues them in a list, from where the workers will remove the tasks for processing. In addition, the bag also receives the results of the workers and queues them in another result list, from where the customer will retrieve the results for assembly of the resulting matrix C.
Worker: Take tasks out of the Tote Bag and insert partial results back.
What I've done so far:
Customer Class = reads a line from matrix A and a column from Matrix B and sends it to the task bag.
import geraMatriz
import sacolaTarefas
class Cliente(object):
def main():
a=[[1,2],
[3,4]]
b = [[2,3],
[1,4]]
print('Matriz Inicial')
print 'A = ',a, '\nB = ', b
nLinhaA = len(a) #Ler quantdade linha matriz A
nColunaA = len(a[0])# Ler quantidade coluna A
nColunaB = len(b[0])# Ler quantidade coluna B
nLinhaB = len(b)#Ler quantdade linha matriz B
listaResultado = []
soma = []
for m in range(nColunaB):
somaLinha = []
for i in range(nLinhaA):
conteudoLinhaA = []
conteudoColunaB =[]
for j in range(nLinhaB):
conteudoLinhaA.append(a[i][j])#lendo linha de A
conteudoColunaB.append(b[j][m])# Lendo coluna B
objSacola = sacolaTarefas.Sacola(conteudoLinhaA, conteudoColunaB)
objSacola.listaTarefas()
main()
Bag Class = receives a row and column from the array through the constructor and adds it to the list.
import workers2
import Queue
class Sacola(object):
def __init__(self, conteudoLinhaA, conteudoColunaB):
self.conteudoLinhaA = conteudoLinhaA
self.conteudoColunaB = conteudoColunaB
lista = Queue.Queue()
listaResp = Queue.Queue()
def listaTarefas(self):
global lista
tarefa = self.conteudoLinhaA, self.conteudoColunaB
lista.put(tarefa)
return lista
resp = workers2.Worker(listaTarefas())
Worker class = receives the task list and distributes it to 4 processes to perform the multiplication of a row by a column.
from multiprocessing import Pool
TRABALHADORES = 4
class Worker(object):
def __init__(self, lista):
self.lista = lista
def multMatriz(self, lista):
for x in lista:#Para cada tarefa da lista que consiste em uma linha e uma coluna da matriz
for i in range(len(x)):#Quantidades de elementos de uma linha da matriz salva na lista
result = 0
for j in range(len(x[0])):# j varia de 0 ate a quantidade de elementos de uma linha/coluna a ser multiplicada
result += x[0][j] * x[1][j] # multiplica os elementos da linha da matriz pelos elementos da coluna a outra matriz
return result#resultado da multiplicacao de somente uma linha por uma coluna
pool = Pool(processes=TRABALHADORES)
pool.map(multMatriz, self.lista)
I do not know if I'm doing it in the best way but what I could think of.
A question, how do I pass "list" as parameter in:
pool.map(multMatriz, self.lista)
Returns the following error:
pool.map(multMatriz, self.lista)
NameError: name 'self' is not defined