Results 1 to 4 of 4
Hi,
I wrote a C program using Pthreads to compute the product of 2 matrices. Each element in the product matrix is computed in a separate thread. Eg: Thread (i,j) ...
- 03-26-2011 #1Just Joined!
- Join Date
- Mar 2011
- Posts
- 1
Pthreads: Threads not getting arguments properly (OS: Ubuntu)
Hi,
I wrote a C program using Pthreads to compute the product of 2 matrices. Each element in the product matrix is computed in a separate thread. Eg: Thread (i,j) computes the element C[i][j] of the matrix C, where C=A*B. A is m*n, B is n*p, C is m*p. m,n,p are given as command-line arguments. A and B are initialized to random values from 1 to 10, while all elements of C are initialized to -1.
But some threads do not get their arguments (i,j) correctly. So some elements C[i][j] still remain as -1, even after the program is over. My OS is Ubuntu 10.10 (Maverick Meerkat) 32-bit. A sample run of the program on my machine is:
$>./mult 2 2 2
Thread 1,0 started.
Thread 1,0 finished.
Thread 1,0 started.
Thread 1,0 finished.
Thread 1,1 started.
Thread 1,1 finished.
Thread 1,1 started.
Thread 1,1 finished.
Matrix A:
5 8
9 8
Matrix B:
7 7
8 1
Matrix C:
-1 -1
127 71
I ran the program on another computer and it worked correctly. Is it due to a problem in the Pthreads library in my OS? Please help me. I have attached the source code.
- 03-28-2011 #2
If I'm not wrong,I think Its related to thread synchronization. May be try adding mutex and see how it works.
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 04-06-2011 #3Just Joined!
- Join Date
- Sep 2010
- Location
- Montgomery, AL
- Posts
- 27
Be aware, that the pthread_create function does not block until the thread is created. It is started later by the O.S. There is no guarantee that the threads will be started in the order that you call pthread_create. Since you are passing a pointer to the args structure to each thread, if the object is being altered before the thread executes (i.e. in the next iteration of your loop) then you will mess up your calculation.
That is my guess.
- 04-08-2011 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
For a problem like this, to get proper synchronization and avoid the need for mutexes, or other locking mechanisms that degrade the performance of multi-threaded algorithms, you should break your problem into a main/controller thread that starts the worker threads. Each worker thread will allocate, or has allocated for it, its own memory to place results when done with its computations, and then pass those results back to the controller, or inform the controller they are done, who can then aggregate the results as they come in. There are a number of robust non-locking ways to accomplish this. Which, it comes to mind, is why you are taking this class? Ok. I've pointed you in the right (or a right) direction. It's up to you now. No more help from me/us...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
