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

Untitled

By: a guest on Jun 21st, 2012  |  syntax: C++  |  size: 2.06 KB  |  hits: 17  |  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 buffer[10];
  14.     int proc,headproc, msglen=64;
  15.     MPI_Status status;
  16.         MPI_Status *Statuses;
  17.     char *sendbuf;
  18.     char *recvbuf;
  19.     int TIMES=5;
  20.     int TAG1=1;
  21.         MPI_Request *Requests;
  22.     double R,s,Duration,Finish,Start;
  23.  
  24.  
  25.     MPI_Init(&argc, &argv);
  26.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  27.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  28.     if (size < 2)
  29.     {
  30.         printf("Please run with two processes.\n");
  31.         fflush(stdout);
  32.         MPI_Finalize();
  33.         return 0;
  34.     }
  35.     sendbuf =(char *) malloc(sizeof(char)*8388608);
  36.     recvbuf =(char *) malloc(sizeof(char)*8388608);
  37.     if (rank == 0)
  38.     {
  39.         for(int j = 1; j <= 18; j++)
  40.         {
  41.  
  42.             Start = MPI_Wtime();
  43.             for(int i = 0; i < TIMES; i ++)
  44.             {
  45.                 MPI_Isend(sendbuf,msglen,MPI_CHAR,proc,TAG1, MPI_COMM_WORLD,&Requests[0]);
  46.                 MPI_Irecv(recvbuf,msglen,MPI_CHAR,proc,TAG1, MPI_COMM_WORLD,&Requests[1]);
  47.                                 MPI_Waitall(2,Requests,Statuses);
  48.             }
  49.             Finish = MPI_Wtime();
  50.             Duration = Finish-Start;
  51.             R=2*TIMES*msglen/Duration;
  52.             s=msglen/R-Duration;
  53.             printf("Num=%d, Time is %f, R=%f, lat=%f \r\n",j,Duration,R,s);
  54.             msglen+=msglen;
  55.  
  56.         }
  57.     }
  58.  
  59.     if (rank == 1)
  60.     {
  61.         for(int j = 1; j <= 18; j++)
  62.         {
  63.             for(int i = 0; i < TIMES; i ++)
  64.             {
  65.                 MPI_Irecv(sendbuf,msglen,MPI_CHAR,proc,TAG1, MPI_COMM_WORLD,&Requests[1]);
  66.                 MPI_Isend(recvbuf,msglen,MPI_CHAR,proc,TAG1, MPI_COMM_WORLD,&Requests[0]);
  67.                                 MPI_Waitall(2,Requests,Statuses);
  68.             }
  69.             msglen+=msglen;
  70.         }
  71.         fflush(stdout);
  72.     }
  73.     MPI_Finalize();
  74.     free(sendbuf);
  75.     free(recvbuf);
  76.     return 0;
  77. }