La multiplication de matrice à l'aide de plusieurs threads?

Je suis censé multiplier de 2 matrices à l'aide de threads. Deux choses: je reçois 0 quand je lance le programme. Je reçois aussi des messages d'erreurs(pour chacun d'eux, il dit: "warning: passing argument 1 of 'printMatrix' from incompatible pointer type" sur les lignes en gras(où j'essaie d'imprimer la sortie). Également à noter, le premier bloc qui est en gras, je que c'était ma tentative de résoudre le problème. Je pense que je suis proche, mais je ne peut pas l'être. Quelqu'un peut-il aider? Merci 🙂
La sortie ressemble à ceci:
A=
1 4
2 5
3 6
B=
8 7 6
5 4 3
A*B=
0 0 0
0 0 0
0 0 0

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define M 3
#define K 2
#define N 3
struct v
{
int i; //row
int j; //column
};
int A[M][K] = {{1,4},{2,5},{3,6}};
int B[K][N] = {{8,7,6},{5,4,3}};
int C[M][N];
void *workerThread(void *data)
{
int i=((struct v*)data)->i;
int j=((struct v*)data)->j;
int accumulator = 0;
/*this is where you should calculate the assigned Cell. You will need to use the row(i) of
A and column[j] of B. Accumulate the result in accumulator */
**int k;
for(k=0; k<k; k++)
{
accumulator = A[i][k]*B[k][j];
}
C[i][j]=accumulator;
pthread_exit(NULL);**
}
void printMatrix(int *matrixIn, int rows, int columns)
{
int *matrix = matrixIn;
int i,j;
for (i=0;i<rows;i++)
{
}
int main (int argc, char *argv[])
{
pthread_t threads[M*N];
int i,j;
int counter = 0;
int numThreadsCreated = 0;
/*the following 5 lines demonstrates how to create 1 thread to calculate C[0][0], you
will need to create a loop for all of C's cells*/
struct v *data = (struct v *)malloc(sizeof(struct v));
data->i = 0; //assign the row of C for thread to calculate
data->j = 0; //assign the column of C for thread to calculate
pthread_create(&threads[0], NULL, workerThread, data);
numThreadsCreated++;
/*wait for all the threads to finish before printing out the matrices*/
for(j=0; j < numThreadsCreated; j++)
{
pthread_join( threads[j], NULL);
}
printf("A=\n");
**printMatrix(A,3,2);**
printf("B=\n");
**printMatrix(B,2,3);**
printf("A*B=\n");
**printMatrix(C,M,N);**
pthread_exit(NULL);
}
Cette ligne est erronée,--->for(k=0; k<k; k++)!
Comment alors? C'était la ligne j'ai été recommandé par quelqu'un d'autre. @shekharsuman
Vérifier ma réponse pour plus de précisions et d'essayer la même chose et puis d'autres commentaires au sujet de votre problème!!!!!!
À moins que votre M et N sont énormes, cela ne va pas être beaucoup d'un rendement gagner. Vous devriez la recherche pour "parallèle de multiplication de matrice" pour voir les différentes approches alternatives au problème lorsque vous traitez avec des matrices de grande taille.

OriginalL'auteur Jay | 2014-10-21