mpi_test.cpp
Go to the documentation of this file.
00001 #include <time.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <mpi.h>
00005 #include <math.h>
00006 
00007 int main(int argc, char *argv[]) {
00008   int numprocs, rank, namelen;
00009   char processor_name[MPI_MAX_PROCESSOR_NAME];
00010 
00011   MPI::Init(argc, argv);
00012   //MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
00013   //MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00014   rank = MPI::COMM_WORLD.Get_rank();
00015   numprocs = MPI::COMM_WORLD.Get_size();
00016   MPI::Get_processor_name(processor_name, namelen);
00017 
00018   //printf("Process %d on %s out of %d\n", rank, processor_name, numprocs);
00019 
00020   // init random seed
00021   srand(time(NULL)+rank);
00022 
00023   double start_time = MPI::Wtime();
00024   // simple summing test
00025   for (unsigned int k = 1; k < 8; k++)
00026   {
00027     unsigned int nx = pow(10,k);
00028     for (int target=0; target < MPI::COMM_WORLD.Get_size(); target++)
00029     {
00030       if (rank == target)
00031       {
00032         double sum = 0;
00033         for (int i=0; i < MPI::COMM_WORLD.Get_size(); i++)
00034         {
00035           // receive from all other nodes
00036           if (i != target)
00037           {
00038             double comm_time = MPI::Wtime();
00039             MPI::Status status;
00040             MPI::COMM_WORLD.Probe (i, target, status);      // for test purposes only
00041             // Gets the number of "top level" elements
00042             unsigned int count = status.Get_count (MPI_BYTE);
00043             double *buf = (double*)malloc (count * sizeof (double));
00044             // Performs a standard-mode blocking receive
00045             //printf("debug %d count %d\n",i,count);
00046             MPI::COMM_WORLD.Recv (buf, count, MPI_DOUBLE, i, target, status);
00047             printf("receive: from %d to %d on %s szie %d complete at %f duration %f\n",i,rank,processor_name,nx,MPI::Wtime()-start_time,MPI::Wtime()-comm_time);
00048             for (unsigned int j=0;j<count;j++) sum += buf[j];
00049             free(buf);
00050           }
00051         }
00052         printf("sum: %f\n",sum);
00053       }
00054       else
00055       {
00056         // send to target
00057         double *x = (double*)malloc (nx * sizeof (double));
00058         for (unsigned int j=0;j<nx;j++) x[j] = (double)rand()/(double)RAND_MAX;
00059         double comm_time = MPI::Wtime();
00060         MPI::COMM_WORLD.Send(x, nx, MPI_DOUBLE , target, target);
00061         printf("send: from %d to %d on %s size %d complete at %f duration %f\n",rank,target,processor_name,nx,MPI::Wtime()-start_time,MPI::Wtime()-comm_time);
00062         free(x);
00063       }
00064     }
00065   }
00066 
00067   MPI::Finalize();
00068 }
00069 


mpi_test
Author(s): John Hsu
autogenerated on Mon Jan 6 2014 11:27:22