Thread: pthread programming question

  1. #1
    Registered User
    Join Date
    May 2010

    Question pthread programming question

    Hi Guys,

    I've got a multi process program as below. I'm using proc 0 as a passive receiver which used non-blocking receiver to receive messages sent from the other procs. However what I'm currently seeing is it can't receive from the last proc

    for example if I called the program like this:

    mpirun -np 4 test

    the output I get is:

    receive some into position 0
    receive some into position 1
    receive some into position 2
    before waitsome
    how many done:1
    Msg (sbuf = 1) from 1 with tag 1
    receive some second time into position 1
    before waitsome
    how many done:1
    Msg (sbuf = 2) from 2 with tag 2
    receive some second time into position 2
    before waitsome

    <<--------- from here the program just stalls waiting for the message from proc 3 to come

    Any help on this would be must appreciated thanks

    #include "mpi.h"
    #include <stdio.h>
    typedef struct {
       int answer;
       int read_size;
    int main(int argc, char *argv[])
        int rank, size, i, sbuf, cnt;
        MPI_Init( &argc, &argv );
        MPI_Comm_rank( MPI_COMM_WORLD, &rank );
        MPI_Comm_size( MPI_COMM_WORLD, &size );
        sbuf = rank;
        if (rank == 0) {
            MPI_Request* requests = (MPI_Request*)malloc((size-1)*sizeof(MPI_Request));
            MPI_Status* statuses = (MPI_Status*)malloc(sizeof(MPI_Status)*(size-1));
            int* indices = (int *)malloc((size-1)*sizeof(int));
            int* buf = (int *)malloc((size-1)*sizeof(int));
            int j, ndone;
            cnt = size-1;
            for (i=0; i<size-1; i++) {
                printf("receive some into position %d\n",i);
                MPI_Irecv(buf+i, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &requests[i]);
            while(cnt > 0) {
                printf("before waitsome\n");
                MPI_Waitsome(size-1, requests, &ndone, indices, statuses);
                printf("how many done:%d\n",ndone);
                for (i=0; i<ndone; i++) {
                    j = indices[i];
                    printf("Msg (sbuf = %d) from %d with tag %d\n",
                    printf("receive some second time into position %d\n",j);
                    MPI_Irecv(buf+j, 1, MPI_INT, j, MPI_ANY_TAG, MPI_COMM_WORLD, &requests[j]);
                cnt -= ndone;
        else {
            printf("proc %d sending...\n",rank);
            MPI_Send( &sbuf, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
        return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Why are you subtracting one from all of your loops? I assume that 'MPI_Init' somehow sets 'rank' and 'size' to actually have a value? Anyway, let's say size is 4. Let's also assume that means you want four children.
    cnt = 4-1;
            for (i=0; i<4-1; i++) {
    0 1 2

    There's your answer.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about the pthread.
    By thungmail in forum C Programming
    Replies: 4
    Last Post: 10-31-2009, 09:38 PM
  2. Quick pthread question - starting paused/suspended
    By NightWolf8800 in forum C Programming
    Replies: 3
    Last Post: 10-24-2009, 08:15 AM
  3. Pthread question
    By gundamz2001 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 04:04 AM
  4. pthread question
    By rotis23 in forum Linux Programming
    Replies: 10
    Last Post: 04-07-2004, 02:57 AM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

Tags for this Thread