Go to the documentation of this file.00001 #include <time.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <mpi.h>
00005
00006 int main(int argc, char *argv[]) {
00007 int numprocs, rank, namelen;
00008 char processor_name[MPI_MAX_PROCESSOR_NAME];
00009
00010 MPI::Init(argc, argv);
00011
00012
00013 rank = MPI::COMM_WORLD.Get_rank();
00014 numprocs = MPI::COMM_WORLD.Get_size();
00015 MPI::Get_processor_name(processor_name, namelen);
00016
00017
00018
00019
00020 srand(time(NULL)+rank);
00021
00022 int array_len = 100;
00023
00024 if (rank == 0)
00025 {
00026 double* buf = new double[array_len];
00027 double sum = 0;
00028 MPI::Status status;
00029 for (int i=1; i < MPI::COMM_WORLD.Get_size(); i++)
00030 {
00031 MPI::COMM_WORLD.Recv(buf, array_len, MPI_DOUBLE , i, 100, status);
00032 for(int j=0;j<array_len;j++)
00033 sum += buf[j];
00034 }
00035 printf("sum: %f\n",sum);
00036 }
00037 else
00038 {
00039 double* x = new double[array_len];
00040 for(int i=0;i<array_len;i++)
00041 x[i] = (double)rand()/(double)RAND_MAX;
00042 MPI::COMM_WORLD.Send(x, array_len, MPI_DOUBLE , 0, 100);
00043 printf("node: %d x: %f send complete\n",rank,x[0]);
00044 }
00045
00046 MPI::Finalize();
00047 }
00048