Matrix multiplication using multiple threads?

13,946

Your program seems to have implemented a wrong coding algorithm for matrix multiplication.

The following piece of code seems absurd :-

for(k=0; k<k; k++)
{
        accumulator = A[i][k]*B[k][j];
}
C[i][j]=accumulator;    //   Not a code for matrix multiplication...

You should implement something like :-

for(i=0;i<M;i++){
 for(j=0;j<N;j++){
  accumulator=0;
   for(int something=0;something<K;something++){
    accumulator=accumulator+A[i][something]*B[something][j];
   }
  C[i][j]=accumulator;
  accumulator=0;
 }
}
Share:
13,946

Related videos on Youtube

Jay
Author by

Jay

Updated on July 04, 2022

Comments

  • Jay
    Jay almost 2 years

    I am supposed to multiply 2 matrices using threads. Two things: I keep getting 0's when I run the program. I also get message errors(for each, it says "warning: passing argument 1 of 'printMatrix' from incompatible pointer type" on the bolded lines(where I try to print the output). Also to note, the first block that is bolded, I that was my attempt at solving the problem. I think I am close, but I may not be. Can anyone help? Thanks :) Output looks like this: 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);
    }
    
    • Am_I_Helpful
      Am_I_Helpful
      This line is flawed--->for(k=0; k<k; k++)!
    • Chuck Walbourn
      Chuck Walbourn
      Unless your M and N are huge, this is not going to be much of a performance win. You should search for "parallel matrix multiplication" to see various alternative approaches to the problem when dealing with large matrices.
  • Jay
    Jay over 9 years
    I just tried this. I replaced my previous code with the one you recommended. It is giving me these error messages ("error: 'k' undeclared (first use in this function)" & ("error: (each undeclared identifier is reported only once for each function it appears in") @shekharsuman
  • Am_I_Helpful
    Am_I_Helpful over 9 years
    You need to use the capital OR block alphabet K instead of lower-case k!!! YOu've #define K=3. How can it give error? Please check your cases!
  • Jay
    Jay over 9 years
    The error messages refer to this line: for (int something = 0; something<k; something++)
  • Jay
    Jay over 9 years
    Well, it is still giving me the same 3 error messages for printing the matrices, and also another error message for the last line(end bracket) that says: error: expected declaration or statement at end of input
  • Jay
    Jay over 9 years
    It worked! ... I think, it still gives me that error message but it multiplied.