root/orte/test/mpi/spawn_tree.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 #include "orte_config.h"
   2 
   3 #include <stdio.h>
   4 #include <stdlib.h>
   5 #include <string.h>
   6 #include <math.h>
   7 #include <unistd.h>
   8 #include <mpi.h>
   9 
  10 int main(int argc, char ** argv){
  11 
  12     int i;
  13     int rank, size, child_rank;
  14     char nomehost[OPAL_MAXHOSTNAMELEN];
  15     MPI_Comm parent, intercomm1, intercomm2;
  16     int erro;
  17     int level, curr_level;
  18 
  19 
  20     if (argc < 2) {
  21         fprintf(stderr, "Usage: spawn_tree <#levels>\n");
  22         exit(1);
  23     }
  24     level = atoi(argv[1]);
  25 
  26     MPI_Init(&argc, &argv);
  27 
  28 
  29     MPI_Comm_get_parent(&parent);
  30 
  31     if(parent == MPI_COMM_NULL){
  32         rank=0;
  33     }
  34     else{
  35         MPI_Recv(&rank, 1, MPI_INT, 0, 0, parent, MPI_STATUS_IGNORE);
  36     }
  37 
  38     curr_level = (int) log2(rank+1);
  39 
  40     printf(" --> rank: %d and curr_level: %d\n", rank, curr_level);
  41 
  42     // Node propagation
  43     if(curr_level < level){
  44         // 2^(curr_level+1) - 1 + 2*(rank - 2^curr_level - 1) = 2*rank + 1
  45         child_rank = 2*rank + 1;
  46         printf("(%d) Before create rank %d\n", rank, child_rank);
  47         MPI_Comm_spawn(argv[0], &argv[1], 1, MPI_INFO_NULL, 0,
  48                        MPI_COMM_SELF, &intercomm1, &erro);
  49         printf("(%d) After create rank %d\n", rank, child_rank);
  50 
  51         MPI_Send(&child_rank, 1, MPI_INT, 0, 0, intercomm1);
  52 
  53         //sleep(1);
  54 
  55         child_rank = child_rank + 1;
  56         printf("(%d) Before create rank %d\n", rank, child_rank);
  57         MPI_Comm_spawn(argv[0], &argv[1], 1, MPI_INFO_NULL, 0,
  58                        MPI_COMM_SELF, &intercomm2, &erro);
  59         printf("(%d) After create rank %d\n", rank, child_rank);
  60 
  61         MPI_Send(&child_rank, 1, MPI_INT, 0, 0, intercomm2);
  62 
  63     }
  64 
  65     gethostname(nomehost, sizeof(nomehost));
  66     printf("(%d) in %s\n", rank, nomehost);
  67 
  68     MPI_Finalize();
  69     return(0);
  70 
  71 }

/* [<][>][^][v][top][bottom][index][help] */