Task Bag (Bag of Tasks) for Matrix Multiplication

Asked

Viewed 140 times

1

I would like the help of you 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 work of multiplication of matrices into small tasks that will be distributed to SD workers.

Each task will be composed by a multiplication row x column, which will be placed in the task bag.

Each entity carries out the following activities:

  1. Client: Reads input matrices A and B, splits into small tasks T (row x column) and insert into the Bag each of the tasks. Then, it collects the result (as they are ready) and assembles the resulting matrix C, which will be printed on the screen at the end of the processing.

  2. Bag: Receives the tasks of the Customer 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 list of results, from where the customer will draw the results for assembly of the resulting matrix C.

  3. Worker: Removes tasks from the bag and inserts back partial results.

What I’ve done so far:

Client class = reads a row of matrix A and a column of 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 = takes a row and column of the matrix through the constructor and adds 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())   

Class Worker = receives the task list and distributes to 4 processes 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 don’t know if I’m doing it the best way.

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  
  • Friend must be why you called the functions without being within another Worker class function.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.