root/orte/test/mpi/sendrecv_blaster.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  */
   3 
   4 #include <stdio.h>
   5 #include <stdlib.h>
   6 #include <unistd.h>
   7 #include <string.h>
   8 #include <time.h>
   9 #include <sys/time.h>
  10 
  11 #include "mpi.h"
  12 
  13 int main(int argc, char *argv[])
  14 {
  15     MPI_Status     status;               /* MPI status                          */
  16     int            mpierr;               /* MPI function return code            */
  17     int            rank;                 /* Process rank within MPI_COMM_WORLD  */
  18     int            size;
  19     int            dest, src;
  20     int            tag0=41;              /* MPI message tag                     */
  21 
  22     int            inject;
  23     int            report;
  24     int            iterations;
  25     int            n_bytes;
  26     unsigned char* send_buff;
  27     unsigned char* recv_buff;
  28     char*          tmp;
  29 
  30     int            i, j, count;
  31 
  32     float fraction, randval;
  33     struct timeval tp;
  34 
  35     if (1 < argc) {
  36         if (0 == strncmp(argv[1], "-h", 2) ||
  37             0 == strncmp(argv[1], "--h", 3)) {
  38             printf("Usage: mpirun --options-- ./sendrecv_blaster <options> where options are:\n"
  39                    "\tpattern=[self | pair | ring] where\n"
  40                    "\t\tself => sendrecv with self\n"
  41                    "\t\tpair => sendrecv with a complementary partner [0 <-> N-1, 1 <-> N-2...]\n"
  42                    "\t\tring [default] => sendrecv around a ring [0 recvs from N-1 and sends to 1]\n"
  43                    "\tsize=[value < 0 => max message size in kbytes, value > 0 => max message size in Mbytes (default=1MByte)]\n"
  44                    "\tinject=[value = #iterations before injecting MPI_Sendrecv to self (default: never)]\n"
  45                    "\treport=[value = #iterations/reporting point (default: 1000)\n"
  46                    "\titerations=[value = #iterations before stopping (default: 1000000)\n");
  47             return 0;
  48         }
  49     }
  50 
  51     mpierr = MPI_Init(&argc, &argv);
  52     if (mpierr != MPI_SUCCESS)
  53     {
  54         fprintf(stderr, "MPI Error %d (MPI_Init)\n",mpierr);
  55         fflush(stderr);
  56         MPI_Abort(MPI_COMM_WORLD, -1);
  57     }
  58 
  59     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
  60 
  61     mpierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  62     if (mpierr != MPI_SUCCESS || rank < 0)
  63     {
  64         fprintf(stderr, "MPI Error %d (MPI_Comm_rank)\n",mpierr);
  65         fflush(stderr);
  66         MPI_Abort(MPI_COMM_WORLD, -1);
  67     }
  68 
  69     mpierr = MPI_Comm_size(MPI_COMM_WORLD, &size);
  70     if (mpierr != MPI_SUCCESS || size < 0)
  71     {
  72         fprintf(stderr, "MPI Error %d (MPI_Comm_size)\n",mpierr);
  73         fflush(stderr);
  74         MPI_Abort(MPI_COMM_WORLD, -1);
  75     }
  76 
  77     /* setup defaults in lieu of args */
  78     n_bytes = 1024*1024;
  79     inject = -1;
  80     report = 1000;
  81     iterations = 1000000;
  82     /* do a ring */
  83     src = rank - 1;
  84     if (src < 0) {
  85         src = size - 1;
  86     }
  87     dest = rank + 1;
  88     if (dest > size-1) {
  89         dest = 0;
  90     }
  91 
  92     for (i=1; i < argc; i++) {
  93         fprintf(stderr, "got %s\n", argv[i]);
  94         if (0 == strncmp(argv[i], "pattern", strlen("pattern"))) {
  95             tmp = strchr(argv[i], '=');
  96             tmp++;
  97             if (0 == strcmp(tmp, "self")) {
  98                 /* just do it with myself */
  99                 src = rank;
 100                 dest = rank;
 101             } else if (0 == strcmp(tmp, "pair")) {
 102                 /* do it pair-wise */
 103                 src = (size-1) - rank;
 104                 dest = src;
 105             } else {
 106                 /* do a ring */
 107                 src = rank - 1;
 108                 if (src < 0) {
 109                     src = size - 1;
 110                 }
 111                 dest = rank + 1;
 112                 if (dest > size-1) {
 113                     dest = 0;
 114                 }
 115             }
 116         } else if (0 == strncmp(argv[i], "size", strlen("size"))) {
 117             tmp = strchr(argv[i], '=');
 118             tmp++;
 119             n_bytes = atoi(tmp);
 120             if (n_bytes < 0) {
 121                 n_bytes = -1 * n_bytes * 1024;
 122             } else {
 123                 n_bytes = n_bytes * 1024*1024;
 124             }
 125         } else if (0 == strncmp(argv[i], "inject", strlen("inject"))) {
 126             tmp = strchr(argv[i], '=');
 127             tmp++;
 128             inject = atoi(tmp);
 129         } else if (0 == strncmp(argv[i], "report", strlen("report"))) {
 130             tmp = strchr(argv[i], '=');
 131             tmp++;
 132             report = atoi(tmp);
 133         } else if (0 == strncmp(argv[i], "iter", strlen("iter"))) {
 134             tmp = strchr(argv[i], '=');
 135             tmp++;
 136             iterations = atoi(tmp);
 137         }
 138     }
 139 
 140     send_buff = (unsigned char *) valloc(n_bytes);
 141     recv_buff = (unsigned char *) valloc(n_bytes);
 142 
 143     /* seed the random number generator */
 144     gettimeofday (&tp, NULL);
 145     srand (tp.tv_usec);
 146 
 147     for ( i=0; i<n_bytes; i++ )
 148     {
 149         send_buff[i] = i%128;
 150     }
 151 
 152     fprintf(stderr, "Rank %d: recving from src %d sending to dest %d with max buff size %dKbytes\n",
 153             rank, src, dest, n_bytes/1024);
 154 
 155     i=0;
 156     while (i < iterations)
 157     {
 158         randval = rand();
 159         fraction = randval/RAND_MAX;
 160         count = fraction * n_bytes;
 161         mpierr = MPI_Sendrecv(send_buff, count, MPI_CHAR, dest, tag0,
 162                               recv_buff, n_bytes, MPI_CHAR, src, tag0, MPI_COMM_WORLD, &status);
 163         if (mpierr != MPI_SUCCESS)
 164         {
 165             fprintf(stderr,"MPI Error %d (MPI_Sendrecv) [%d,%d] at iteration %d\n",mpierr,src,dest,i);
 166             fflush(stderr);
 167             MPI_Abort(MPI_COMM_WORLD, -1);
 168         }
 169         i++;
 170         if (0 == (i % report)) {
 171             fprintf(stderr, "Rank %d has completed %dk iterations\n", rank, i/1000);
 172         }
 173         if (0 < inject && 0 == (i % inject)) {
 174             mpierr = MPI_Sendrecv(send_buff, count, MPI_CHAR, rank, tag0,
 175                                   recv_buff, n_bytes, MPI_CHAR, rank, tag0, MPI_COMM_WORLD, &status);
 176             if (mpierr != MPI_SUCCESS)
 177             {
 178                 fprintf(stderr,"MPI Error %d (MPI_Sendrecv) [%d,%d] at iteration %d\n",mpierr,rank,rank,i);
 179                 fflush(stderr);
 180                 MPI_Abort(MPI_COMM_WORLD, -1);
 181             } else {
 182                 fprintf(stderr, "Rank %d has completed MPI_Sendrecv with myself\n", rank);
 183             }
 184         }
 185     }
 186 
 187     fprintf(stderr, "Rank %d completed test\n", rank);
 188     MPI_Finalize();
 189 }

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