Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: C++  |  size: 2.02 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // mpi_5_1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <mpi.h>
  8. #include <malloc.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     int rank, size, i;
  13.     int msglen=64;
  14.     //MPI_Status status;
  15.         MPI_Status Statuses[1];
  16.     char *sendbuf;
  17.     char *recvbuf;
  18.     int TIMES=5;
  19.     int TAG1=1;
  20.         MPI_Request Requests[1];
  21.     double R,s,Duration,Finish,Start;
  22.  
  23.  
  24.     MPI_Init(&argc, &argv);
  25.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  26.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27.     if (size < 2)
  28.     {
  29.         printf("Please run with two processes.\n");
  30.         fflush(stdout);
  31.         MPI_Finalize();
  32.         return 0;
  33.     }
  34.     sendbuf =(char *) malloc(sizeof(char)*8388608);
  35.     recvbuf =(char *) malloc(sizeof(char)*8388608);
  36.     if (rank == 0)
  37.     {
  38.         for(int j = 1; j <= 18; j++)
  39.         {
  40.  
  41.             Start = MPI_Wtime();
  42.             for(int i = 0; i < TIMES; i ++)
  43.             {
  44.                 MPI_Isend(sendbuf,msglen,MPI_CHAR,1,TAG1, MPI_COMM_WORLD,&Requests[0]);
  45.                 MPI_Irecv(recvbuf,msglen,MPI_CHAR,1,TAG1, MPI_COMM_WORLD,&Requests[1]);
  46.                                 MPI_Waitall(2,Requests,Statuses);
  47.             }
  48.             Finish = MPI_Wtime();
  49.             Duration = Finish-Start;
  50.             R=2*TIMES*msglen/Duration;
  51.             s=msglen/R-Duration;
  52.             printf("Num=%d, Time is %f, R=%f, lat=%f \r\n",j,Duration,R,s);
  53.             msglen+=msglen;
  54.  
  55.         }
  56.     }
  57.  
  58.     if (rank == 1)
  59.     {
  60.         for(int j = 1; j <= 18; j++)
  61.         {
  62.             for(int i = 0; i < TIMES; i ++)
  63.             {
  64.                 MPI_Irecv(sendbuf,msglen,MPI_CHAR,0,TAG1, MPI_COMM_WORLD,&Requests[1]);
  65.                 MPI_Isend(recvbuf,msglen,MPI_CHAR,0,TAG1, MPI_COMM_WORLD,&Requests[0]);
  66.                                 MPI_Waitall(2,Requests,Statuses);
  67.             }
  68.             msglen+=msglen;
  69.         }
  70.         fflush(stdout);
  71.     }
  72.     MPI_Finalize();
  73.     free(sendbuf);
  74.     free(recvbuf);
  75.     return 0;
  76. }