C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2009, 02:10 PM   #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 

*/
vbdave78 is offline   Reply With Quote
Old 10-27-2009, 07:08 PM   #2
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
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.
nadroj is offline   Reply With Quote
Reply

Tags
code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C - access violation uber C Programming 2 07-08-2009 01:30 PM
*operator overloading: scalar matrix multiplication gemini_shooter C++ Programming 4 06-08-2009 01:14 PM
Matrix Help HelpmeMark C++ Programming 27 03-06-2008 05:57 PM
What is a matrix's purpose in OpenGL jimboob Game Programming 5 11-14-2004 12:19 AM


All times are GMT -6. The time now is 01:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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