Find the answer to your Linux question:
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) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.
    Attached Files Attached Files

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    If I'm not wrong,I think Its related to thread synchronization. May be try adding mutex and see how it works.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Location
    Montgomery, AL
    Posts
    27
    Quote Originally Posted by kitpgm View Post
    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.
    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.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,639
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •