From: Sanchit on 22 Mar 2010 08:36 Hi, Following program is to parallelize Matrix Multiplication using Threads. My problem is that , my proogram is not printing anything in function "thread_function". Kindly Help. #include<signal.h> #include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<pthread.h> #include<time.h> // decleared globally so can be used by all threads through resorce shareing and dont get destroyed on exit of threads............ int PMatrix[500] [500]; //final matrix product of other marices int Matrix1[500][500],Matrix2[500][500]; // matrices to be multiplied // theread func has arg of type void* and return type of void * void *thread_function(void *arg) // here argument recieved is pointer to set of row no.s to do multiplication { int i,j,k,a; int* b = (int*) arg; // typecasting to int to use further a = (*b)*50; // determines range printf("%d\n",a); printf("thread %d\n",*b); fflush(stdout) ; for(i=a;i<a+50;i++) // Each thread computes for 50 rows for(j=0;j<500;j++) for(k=0;k<500;k++) PMatrix[i][j]+=Matrix1[i] [k]*Matrix2[k][j]; return NULL; } int main() { int i,j; void * exit_status; pthread_t tid[10]; // thread ids 10 as ten therads are created clock_t t1,t2; //to mesure no of ticks double t; t1=clock(); for(i=0;i<500;i++) for(j=0;j<500;j++) Matrix1[i][j] = rand()%10; for(i=0;i<500;i++) for(j=0;j<500;j++) Matrix2[i][j] = rand()%10; for(i=0;i<10;i++)// thread creation total 10 therads each computes product for 50 rows .. pthread_create(&tid[i],NULL,thread_function, (void*)&i); for(i=0;i++;i<10) // checking wether all threads have terminated pthread_join(tid[i],&exit_status); printf("Matrix 1 is \n"); for(i=0;i<5;i++) // prints Matrix1 up to 5 rows and colums { for(j=0;j<5;j++) { printf(" %d",Matrix1[i][j]); } printf("\n"); } printf("\n Matrix2 is \n"); for(i=0;i<5;i++) // prints Matrix2 up to 5 rows and colums { for(j=0;j<5;j++) { printf(" %d",Matrix2[i][j]); } printf("\n"); } //for(i=0;i++;i<10) // checking wether all threads have terminated // pthread_join(tid[i],&exit_status); printf("\n PMatrix is \n"); for(i=0;i<5;i++) // prints PMatrix up to 5 rows and colums { for(j=0;j<5;j++) { printf(" %d",PMatrix[i][j]); } printf("\n"); } t2 = clock(); t = ((double)(t2 - t1))/CLOCKS_PER_SEC ; // cpu time = NO of tics / No of tics per sec printf("\nTime taken by multi threading is -> %lf \n",t); return 0; }
From: Ben Bacarisse on 22 Mar 2010 09:13 Sanchit <sanchitgupta.1(a)gmail.com> writes: > Following program is to parallelize Matrix Multiplication using > Threads. My problem is that , my proogram is not printing anything in > function "thread_function". Kindly Help. It may be because you are not joining with any of the threads. The use of // comments that have wrapped is a problem for people who want to try your example. <snip> > int* b = (int*)arg; // typecasting to int... The word "cast" is preferable because "typecast" means something else in English. But you are casting to int *, not int. This sort of comment is often best avoided. It is not that different from: x = x + 1; // increment x <snip> > for(i=0;i<10;i++) // thread creation [...] > pthread_create(&tid[i],NULL,thread_function,(void*)&i); Passing a pointer to the same variable is a problem here. 'i' may have changed by the time the thread looks at it. You need a separate object per thread. > for(i=0;i++;i<10) // checking wether all threads have terminated Look closely at the condition! > pthread_join(tid[i],&exit_status); A tip: if you choose matrices whose product you know, you can verify the answer. -- Ben.
From: David Schwartz on 22 Mar 2010 10:28 On Mar 22, 5:36 am, Sanchit <sanchitgupt...(a)gmail.com> wrote: > for(i=0;i<10;i++)// thread creation total 10 therads each > computes product for 50 > rows .. > pthread_create(&tid[i],NULL,thread_function, > (void*)&i); This code is horribly broken. You pass a thread the address of 'i', and then you change the value of 'i'. Meanwhile, the thread tries to access 'i' through that pointer. You must not modify a variable in one thread while another thread is or might be reading it. DS
From: Sanchit on 23 Mar 2010 09:30 I agree with Ben and David regarding changing value of i. But at this point I am only worried because nothing is being printed in my thread function.
From: David Schwartz on 23 Mar 2010 10:00
On Mar 23, 6:30 am, Sanchit <sanchitgupt...(a)gmail.com> wrote: > I agree with Ben and David regarding changing value of i. But at this > point I am only worried because nothing is being printed in my thread > function. That's likely because you 'return' from 'main' before it has a chance to output. Returning from 'main' terminates the process. You can call 'pthread_exit' or join the other threads if you want. But I wouldn't recommend trying to debug toy code that contains known race conditions, except to fix those race conditions. (Or to study them specifically.) It can get confusing when you mix things and you may learn things that are not so (because you incorrectly judge their cause). DS |