root/examples/connectivity_c.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * Copyright (c) 2007      Sun Microsystems, Inc.  All rights reserved.
   3  */
   4 
   5 /*
   6  * Test the connectivity between all processes.
   7  */
   8 
   9 #include <errno.h>
  10 #include <stdio.h>
  11 #include <stdlib.h>
  12 #include <string.h>
  13 #include <netdb.h>
  14 #include <unistd.h>
  15 #include <mpi.h>
  16 
  17 int
  18 main(int argc, char **argv)
  19 {
  20     MPI_Status  status;
  21     int         verbose = 0;
  22     int         rank;
  23     int         np;        /* number of processes in job */
  24     int         peer;
  25     int         i;
  26     int         j;
  27     int         length;
  28     char        name[MPI_MAX_PROCESSOR_NAME+1];
  29 
  30     MPI_Init(&argc, &argv);
  31     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  32     MPI_Comm_size(MPI_COMM_WORLD, &np);
  33 
  34     /*
  35      * If we cannot get the name for whatever reason, just
  36      * set it to unknown. */
  37     if (MPI_SUCCESS != MPI_Get_processor_name(name, &length)) {
  38         strcpy(name, "unknown");
  39     }
  40 
  41     if (argc>1 && strcmp(argv[1], "-v")==0)
  42         verbose = 1;
  43 
  44     for (i=0; i<np; i++) {
  45         if (rank==i) {
  46             /* rank i sends to and receives from each higher rank */
  47             for(j=i+1; j<np; j++) {
  48                 if (verbose)
  49                     printf("checking connection between rank %d on %s and rank %-4d\n",
  50                            i, name, j);
  51                 MPI_Send(&rank, 1, MPI_INT, j, rank, MPI_COMM_WORLD);
  52                 MPI_Recv(&peer, 1, MPI_INT, j, j, MPI_COMM_WORLD, &status);
  53             }
  54         } else if (rank>i) {
  55             /* receive from and reply to rank i */
  56             MPI_Recv(&peer, 1, MPI_INT, i, i, MPI_COMM_WORLD, &status);
  57             MPI_Send(&rank, 1, MPI_INT, i, rank, MPI_COMM_WORLD);
  58         }
  59     }
  60 
  61     MPI_Barrier(MPI_COMM_WORLD);
  62     if (rank==0)
  63         printf("Connectivity test on %d processes PASSED.\n", np);
  64 
  65     MPI_Finalize();
  66     return 0;
  67 }

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