#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <mpi.h>
#include "timer.h"
#define SIZE 100000000
int array[SIZE];
int number;
int counter;void fill ( )
{
srand (time (NULL));
for (int i=0; i<SIZE; i++)
{
array[i] = rand()%10;
}
}void count (int start, int end)
{
int i;
for (i=start; i<start+end; i++)
{
if (array[i] == 3)
{
counter ++;
}
}
}
int main (int argc, char** argv)
{
int i, j, k;
int overall;
int myID, numProcs, size, rem;
MPI_Status status;
timer watch;
float total=0.0f; MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &numProcs);
MPI_Comm_rank (MPI_COMM_WORLD, &myID);
number = numProcs; size = SIZE/numProcs;
rem = SIZE%numProcs; for (j=0; j<100; j++)
{
overall = 0; if (myID == 0)
{ fill ( ); for (i=1; i < numProcs; i++)
{
MPI_Send (array+(i*size), size, MPI_INT, i, i, MPI_COMM_WORLD);
}
}
else
{ MPI_Recv (array+(myID*size), size, MPI_INT, 0, myID, MPI_COMM_WORLD, &status);
} MPI_Barrier (MPI_COMM_WORLD);
counter = 0; if (myID == 0)
{ watch.start (); count (0, size);
count (numProcs*size, rem);
} else
{ count (myID*size, size);
} MPI_Reduce (&counter, &counter, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (myID == 0)
{ watch.stop (); if (j>10 && j<90)
{
total += watch.read ();
}
}
} if (myID == 0)
{
printf ("%i\t%.3f\n", number, total/80.0f);
} MPI_Finalize ();
return 0;
}