4

I have both matrices containing only ones and each array has 500 rows and columns. So, the resulting matrix should be a matrix of all elements having value 500. But, I am getting res_mat[0][0]=5000. Even other elements are also 5000. Why?

#include<stdio.h> #include<pthread.h> #include<unistd.h> #include<stdlib.h> #define ROWS 500 #define COLUMNS 500 #define N_THREADS 10 int mat1[ROWS][COLUMNS],mat2[ROWS][COLUMNS],res_mat[ROWS][COLUMNS]; void *mult_thread(void *t) { /*This function calculates 50 ROWS of the matrix*/ int starting_row; starting_row = *((int *)t); starting_row = 50 * starting_row; int i,j,k; for (i = starting_row;i<starting_row+50;i++) for (j=0;j<COLUMNS;j++) for (k=0;k<ROWS;k++) res_mat[i][j] += (mat1[i][k] * mat2[k][j]); return; } void fill_matrix(int mat[ROWS][COLUMNS]) { int i,j; for(i=0;i<ROWS;i++) for(j=0;j<COLUMNS;j++) mat[i][j] = 1; } int main() { int n_threads = 10; //10 threads created bcos we have 500 rows and one thread calculates 50 rows int j=0; pthread_t p[n_threads]; fill_matrix(mat1); fill_matrix(mat2); for (j=0;j<10;j++) pthread_create(&p[j],NULL,mult_thread,&j); for (j=0;j<10;j++) pthread_join(p[j],NULL); printf("%d\n",res_mat[0][0]); return 0; } 
1
  • Basically, in my program each thread calculates 50 rows.. And I pass the value of rows to be calculated while doing function call pthread_create() Commented Mar 15, 2010 at 9:41

2 Answers 2

4

I don't know if this is the cause of your problem but:

 pthread_create(&p[j],NULL,mult_thread,&j); 

is definitely broken. You are passing in the address of j &j. So each thread will get a random value 0 <= starting_row <= 9, when it actually starts. Probably better to just pass in (void*)j and get it out with (int)j.

You're also never initialising res_mat, but IIRC it'll get initialised anyway.

EDIT: The reason that the value of starting_row is random, is the j goes through all the numbers between 0 and 9 between the thread being started, and it being joined. So the thread will be started at some random point between those two, and will pick up the value of j at that point.

Sign up to request clarification or add additional context in comments.

3 Comments

Why in each thread the value of starting_row would be a random between 0 & 9?
Actaully, what I think is that ALL THREADS will have the same value of j.. Am I right?
No because once a thread is created, you don't know when or in which order they wille be executed. After a call to pthread_create, which thread is executing, the main one, or the one just created ? so you may be right (ie all thread are created and then begin execution), but this is just a possibility
3

You are passing a pointer to j to the thread function. The value of j may have changed when the thread accesses it. Just pass (void *)j and change the cast in the thread function to starting_row = (int)t; .

2 Comments

So, what do I actually do? Only changing &j -> (void *)j will create SEG Faults..
Thanx. I got it. Easy & short.. Cheers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.