Thread: specify the start and end of the parallel part using MPI

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    specify the start and end of the parallel part using MPI

    I like to specify the start and end of the parallel part using MPI. In the following toy code, I want to limit the parallel part within function f():

    Code:
        #include "mpi.h"  
        #include <stdio.h>  
        #include <string.h>  
    
        void f();
    
        int main(int argc, char **argv)  
        {  
        printf("%s\n", "Start running!");  
        f();  
        printf("%s\n", "End running!");  
        return 0;  
        }  
        
          
        void f()  
        {  
        char idstr[32]; char buff[128];  
        int numprocs; int myid; int i;  
        MPI_Status stat;  
          
        printf("Entering function f().\n");
    
        MPI_Init(NULL, NULL);  
        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);  
        MPI_Comm_rank(MPI_COMM_WORLD,&myid);  
          
        if(myid == 0)  
        {  
          printf("WE have %d processors\n", numprocs);  
          for(i=1;i<numprocs;i++)  
          {  
            sprintf(buff, "Hello %d", i);  
            MPI_Send(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD); }  
            for(i=1;i<numprocs;i++)  
            {  
              MPI_Recv(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD, &stat);  
              printf("%s\n", buff);  
            }  
        }  
        else  
        {  
          MPI_Recv(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &stat);  
          sprintf(idstr, " Processor %d ", myid);  
          strcat(buff, idstr);  
          strcat(buff, "reporting for duty\n");  
          MPI_Send(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD);  
        }  
        MPI_Finalize();  
          
        printf("Leaving function f().\n");  
        }
    However, the running output is not as what I expected. The printf parts before and after the parallel part have been executed by every process, not just the main process:
    $ mpirun -np 3 ex2
    Start running!
    Entering function f().
    Start running!
    Entering function f().
    Start running!
    Entering function f().
    WE have 3 processors
    Hello 1 Processor 1 reporting for duty

    Hello 2 Processor 2 reporting for duty

    Leaving function f().
    End running!
    Leaving function f().
    End running!
    Leaving function f().
    End running!
    So it seems to me the parallel part is not limited between MPI_Init() and MPI_Finalize(). How can I specify the start and end of the parallel part?

    Thanks and regards!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You are seeing the output from all 3 instances of your process. If you want to limit which should perform output, then you need "if (myid == x) printf(...)".

    If you only want a single process but multiple threads, then use a multi-threading library.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks Codeplug!

    1. Someone told me the beginning and end of the part to be paralleled is MPI_Init() and MPI_Finalize(), and other parts before and after it are just serialized. So it is not correct. Instead the whole program will be parallel whether or not MPI_Init() and MPI_Finalize() are just in the middle of the program and even inside a function called by main()?

    2. In my case, myid is just defined as a local variable inside function f(). Before and after f() is called by main(), myid does not exist. How can I limit those printouts in main() to the main process only?

    Thanks!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    1. Think of it as: Each process instance is running independently of the other - or "at the same time". MPI provides the synchronization and communication between each instance.

    2. Move the Init call out into main and get your rank firs think. Then only rank 0 can perform any output (or however you want to do it).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. Overwriting all in array, why?
    By guesst in forum C Programming
    Replies: 7
    Last Post: 10-09-2008, 05:56 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM