Matrix Multiplication using threads

This is a discussion on Matrix Multiplication using threads within the C Programming forums, part of the General Programming Boards category; Hi, This code has been written to multiply 2 matrices using threads. The computation of each element of the matrix ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Matrix Multiplication using threads

    Hi,
    This code has been written to multiply 2 matrices using threads. The computation of each element of the matrix happens in the thread. As you can see at the end of the code, the sample outputs are not only wrong but different for 2 separate runs. I am not sure where the code is wrong. I am guessing it is the way the CPU schedules the threads.
    Any help would be appreciated.
    Thanks
    V

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    //GLOBAL CONSTANTS
    #define M 3
    #define K 2
    #define N 3 //Matrix sizes
    
    int A [M][K] = { {1,4}, {2,5}, {3,6} }; //global matrices
    int B [K][N] = { {8,7,6}, {5,4,3} };
    
    //GLOBAL VARIABLES
    int C [M][N]; //resultant matrix
    
    void *calc_Cell(void * num1); //calculates each cell of matrix
    
    int main(int argc, char *argv[]) //
    {
      pthread_t tid[M][N];//declare pthread id matrix
      //pthread_attr_t attr;
      int cellPosition[2];//declare position array to pass to threads
    
      //pthread_attr_init(&attr);
      
      int i,j; //placeholders for double-loop to traverse matrix
      //printf("Part 1\n");
      for(i = 0; i < M; i++)
        for(j = 0; j < N; j++)
          {
    	cellPosition[0] = i;//assign i and j locations to array to pass to thread
    	cellPosition[1] = j;
    	//printf("Inside first loop:\n");
            pthread_create(&tid[i][j], NULL, *calc_Cell, (void *) cellPosition);//create a new thread to calculate sum of cell [i][j]
          }
      //printf("Part 2\n");
      for(i = 0; i < M; i++)
        for(j = 0; j < N; j++)
          pthread_join(tid[i][j], NULL);//wait until each cell is finished calculating
      //printf("Part 3\n");
      for(i = 0; i < M; i++)
        { printf("\n");
          for(j = 0; j < N; j++)
    	{ printf("%d\t", C[i][j]);//print resultant matrix
          }//line 40
        }
    
      return 0;
    }
    
    void *calc_Cell(void * num1){
      //int cell = 0;
      //printf("%d", atoi(num1), "\n");
      int *line; //int pointer to receive void pass
      line = (int *) num1; //assign void array to int array
    
      int z;
      for(z = 0; z < K; z++)
        C[line[0]][line[1]] += (A[line[0]][z] * B[z][line[1]]);//calculate cell line[0]line[1] of matrix C
      //printf("%d\t", C[line[0]][line[1]]);
      pthread_exit(0);
    
      //C[line[0]][line[1]] = cell;
    }
    
    /*SAMPLE RUN #1
    
    0       0       0
    0       0       0
    0       45      288 
    
    SAMPLE RUN #2
    
    0       0       0
    0       0       0
    0       0       324 
    
    */

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
      for(i = 0; i < M; i++)
        for(j = 0; j < N; j++)
          {
        cellPosition[0] = i;//assign i and j locations to array to pass to thread
        cellPosition[1] = j;
        //printf("Inside first loop:\n");
            pthread_create(&tid[i][j], NULL, *calc_Cell, (void *) cellPosition);//create a new thread to calculate sum of cell [i][j]
          }
    I was kinda interested in your problem, which is why I spent a while looking at it, and it makes sense why its behaving in an unpredictable manner. Basically, as you can see above, the problem is with the highlighted code. For the first thread, it will use the function, initially, for row i=0 and column j=0. The next thread that is created, it changes the same variable that the first thread is using, so that both threads are working on the same values now, row i=o, column j=1. This continues, and eventually, any threads that are still running, have the same value for i and j. Of course theres a lot of variables that make it unpredictable, the big one being the CPU time given to and scheduling of the different threads.

    The solution is use different variables for each thread. In each iteration of this inner for loop, I would "malloc" an array of 2 ints, so that each thread you make you know will use variables (because its different memory). Since you "malloc", of course you will have to "free" the same number of times. If you "free" within the for loop, then you will loose the information that the previous thread is working on. Because of this, you should probably "free" it in the calc function itself, when its done calculating.

    Let us know if theres problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21