Thread: MPI Help

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    23

    MPI Help

    Hi,

    I'm having trouble using MPI. I have a nested for loop and I only want the nested for loop to be worked on by the multiple processors and only one processor (the root) to be responsible for the outer for loop. Normally if I wanted to assign a chunk of code to just one processor I would do:
    Code:
    if (rank == ROOT)
    // Work done here
    but I can't do this in this case as all the work would be assigned to just the root processor (nested for loop included). Does anyone know any way to get around this?
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean something like:

    Code:
    if( rank != root )
        /* do stuff... */

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    23
    nooo.. i wish it was that simple...
    here is the problem:
    Code:
    if (rank == 0)
    for (i=0;i<n;i++)
    {
    // Stuff done just by rank 0 here
    
    MPI_Scatter(); // send parts of the matrix to all the processes
    for (j=0;j<x;j++) // I WANT ALL PROCESSORS TO SEE THIS CODE (Each do their own for loop)
     // Each processor does work on its part of the matrix
    MPI_Gather();
    }
    So is there a way all the processors see the second for loop?
    Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (rank != 0)
        goto nest;
    else
    {
        for (i=0;i<n;i++)
        {
            // Stuff done just by rank 0 here
    
            MPI_Scatter(); // send parts of the matrix to all the processes
            nest:
            for (j=0;j<x;j++) // I WANT ALL PROCESSORS TO SEE THIS CODE (Each do their own for loop)
                 // Each processor does work on its part of the matrix
            if( rank != 0 )
                 break;
            MPI_Gather();
        }
    }
    Something like that should work. You'll just need to get a bit creative. (IE: x isn't initialized there, so you'll need to supply sufficient wrappings to get all of that done. Just slap "if( rank == 0)" right before each statement you want to only execute by root, and then provide an "else" for it.


    Quzah.
    Last edited by quzah; 04-28-2007 at 09:03 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    23
    I'm not sure that will work... I want each processor to work on the inner for loop on each iteration of the outer for loop... this code looks like all the processors will work only once on the inner for loop...
    Also the other processes need to wait for the stuff done by the first processor in the outer for loop (especially the scatter function as it sends the data to all the processors)

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( rank == root )
        i = 0;
    while( i < n )
    {
        if( rank == root )
        {
            /* ...do stuff... */
            MPI_Scatter( ); /* Move this line out of IF, if everyone does MPI_ */
        }
        
        /* For everyone, or if for !root, add in an IF to handle it. */
        for (j=0;j<x;j++) // I WANT ALL PROCESSORS TO SEE THIS CODE (Each do their own for loop)
            /* ...do stuff... */
    
        if( rank == root )
        {
            MPI_Gather( ); /* Same as above... */
            i++; /* Move so it's visible to whoever is supposed to increment it. */
        }
    }
    You just need to think about who needs to do what, and block the appropriate people from doing it. Don't expect me to figure the whole thing out for you, you need to think about it and decide what lines are needed by who, and then IF them appropriately.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Communication using MPI
    By Cell in forum Linux Programming
    Replies: 9
    Last Post: 08-13-2009, 02:28 AM
  2. MPI in C
    By ltee in forum C Programming
    Replies: 5
    Last Post: 03-26-2009, 06:10 AM
  3. Sorting whit MPI
    By isato in forum C Programming
    Replies: 0
    Last Post: 03-03-2009, 10:38 AM
  4. Malloc and MPI
    By moddinati in forum C Programming
    Replies: 17
    Last Post: 03-07-2008, 07:55 PM
  5. MPI programming
    By kris.c in forum Tech Board
    Replies: 1
    Last Post: 12-08-2006, 12:25 PM