Thread: For those Who know MPI

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    For those Who know MPI

    In the code below. I splitt the comm WORLD_COMM in 6 subgroups and Allreduce the rank before splitting.

    Code:
      #include "mpi.h" 
    #include <stdio.h>
    int main(argc,argv) 
    int argc; 
    char *argv[]; { 
      int rank,rank2,group_size,group_size2,comm_size,comm_size2,new_rank, sendbuf, recvbuf, server=0;
     int ranks[1];
     int array2, array1;
     int color=5;
     MPI_Group workers_group, world_group; 
     MPI_Comm world,workers_comm; 
     MPI_Init(&argc,&argv); 
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
     MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
     array1=rank;
     printf("rank= %d comm_size=%d \n",rank, comm_size);
    
     world=MPI_COMM_WORLD;
    
     MPI_Barrier(world);
    
     if (rank == 0){color=0;}
     if ((rank>0) && (rank<4)){color=1;}
     if ((rank>3)&& (rank<7)){color=2;}
     if ((rank>6)&& (rank<10)){color=3;}
     if ((rank>9)&& (rank<21)){color=4;}
    
    
     MPI_Comm_split(world,color,rank,&workers_comm);
    
     MPI_Comm_rank(workers_comm,&rank2);
     MPI_Comm_size(workers_comm,&comm_size2);
    
    
     printf("size=%d  size2= %d comm_size2=%d new rank= %d\n",group_size,group_size2, comm_size2,rank2);
     
     
     MPI_Allreduce(&array1,&array2,comm_size2,MPI_INT,MPI_SUM,workers_comm);
     
    
     printf("array1=%d  array2= %d comm_size2=%d new rank= %d\n",array1,array2, comm_size2,rank2);
     
     MPI_Barrier(workers_comm);
     MPI_Comm_free(&workers_comm);
     MPI_Finalize();
    }
    when I am using 10 processes the outcome seems ok:

    Code:
    rank= 1 comm_size=10 
    rank= 2 comm_size=10 
    rank= 3 comm_size=10 
    rank= 5 comm_size=10 
    rank= 4 comm_size=10 
    rank= 0 comm_size=10 
    rank= 6 comm_size=10 
    rank= 7 comm_size=10 
    rank= 9 comm_size=10 
    rank= 8 comm_size=10 
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    array1=2  array2= 6 comm_size2=3 new rank= 1
    array1=3  array2= 6 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    array1=5  array2= 15 comm_size2=3 new rank= 1
    array1=4  array2= 15 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=1 new rank= 0
    array1=1  array2= 6 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    array1=6  array2= 15 comm_size2=3 new rank= 2
    array1=0  array2= 0 comm_size2=1 new rank= 0
    array1=8  array2= 24 comm_size2=3 new rank= 1
    array1=7  array2= 24 comm_size2=3 new rank= 0
    array1=9  array2= 24 comm_size2=3 new rank= 2
    But when i am using 14 or above I am getting strange numbers for the size and rank of the new comm (comm_size2)

    Code:
    rank= 1 comm_size=20 
    rank= 2 comm_size=20 
    rank= 3 comm_size=20 
    rank= 4 comm_size=20 
    rank= 5 comm_size=20 
    rank= 6 comm_size=20 
    rank= 8 comm_size=20 
    rank= 9 comm_size=20 
    rank= 11 comm_size=20 
    rank= 7 comm_size=20 
    rank= 10 comm_size=20 
    rank= 14 comm_size=20 
    rank= 15 comm_size=20 
    rank= 0 comm_size=20 
    rank= 16 comm_size=20 
    rank= 12 comm_size=20 
    rank= 17 comm_size=20 
    rank= 18 comm_size=20 
    rank= 19 comm_size=20 
    rank= 13 comm_size=20 
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=10 new rank= 5
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=10 new rank= 2
    size=4414736  size2= 0 comm_size2=10 new rank= 7
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    array1=8  array2= 24 comm_size2=3 new rank= 1
    array1=9  array2= 24 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=10 new rank= 6
    array1=7  array2= 24 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    size=4414736  size2= 0 comm_size2=10 new rank= 3
    array1=2  array2= 6 comm_size2=3 new rank= 1
    array1=1  array2= 6 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=1 new rank= 0
    array1=3  array2= 6 comm_size2=3 new rank= 2
    size=4414736  size2= 0 comm_size2=10 new rank= 1
    array1=0  array2= 0 comm_size2=1 new rank= 0
    size=4414736  size2= 0 comm_size2=3 new rank= 1
    size=4414736  size2= 0 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=10 new rank= 0
    size=4414736  size2= 0 comm_size2=10 new rank= 8
    array1=5  array2= 15 comm_size2=3 new rank= 1
    array1=6  array2= 15 comm_size2=3 new rank= 2
    array1=4  array2= 15 comm_size2=3 new rank= 0
    size=4414736  size2= 0 comm_size2=10 new rank= 4
    size=4414736  size2= 0 comm_size2=10 new rank= 9
    array1=19  array2= 145 comm_size2=327670 new rank= 200
    array1=15  array2= 145 comm_size2=327670 new rank= 200
    array1=17  array2= 145 comm_size2=327670 new rank= 200
    array1=18  array2= 145 comm_size2=327670 new rank= 200
    array1=14  array2= 145 comm_size2=327670 new rank= 200
    array1=13  array2= 145 comm_size2=327670 new rank= 200
    array1=12  array2= 145 comm_size2=327670 new rank= 200
    array1=16  array2= 145 comm_size2=327670 new rank= 200
    array1=11  array2= 145 comm_size2=327670 new rank= 200
    array1=10  array2= 145 comm_size2=327670 new rank= 200
    Any ideas of why it is happening?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    'group_size' and 'group_size2' are uninitialized variables, so their values mean nothing.

    You are splitting your processes into "buckets":
    Code:
    [rank] = bucket X
    --------------------
    [0]     = bucket 0
    [1-3]   = bucket 1
    [4-6]   = bucket 2
    [7-9]   = bucket 3
    [10-20] = bucket 4
    [21-N]  = bucket 5
    With 10 processes, you'll have 4 groups (buckets) containing 1, 3, 3, and 3 processes.
    With 20 processes, you'll have 5 groups containing 1, 3, 3, 3, and 10 processes.

    The call to MPI_Allreduce is incorrect. The third parameter is the number of elements in the send buffer (first parameter), which should be 1 in this case.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    Dear codeplug,

    Many thanks for your reply. You helped me a lot. I couldnt spot this

    Best,
    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallelize for-loop by MPI?
    By lehe in forum C Programming
    Replies: 1
    Last Post: 02-05-2010, 09:13 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