Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #define TRIALS 20
- #define ARRAY_SIZE 10000
- int main (int argc, char *argv[])
- {
- int myid, numprocs;
- double startwtime, endwtime;
- int namelen;
- int* numbers = new int[ARRAY_SIZE];
- int i, j, max;
- int s, s0, startIndex, endIndex;
- double totalTime;
- MPI_Status stat;
- char processor_name[MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Get_processor_name(processor_name, &namelen);
- fprintf(stderr, "Process %d on $s\n", myid, processor_name);
- fflush(stderr);
- if (myid == 0)
- {
- s = (int) floor(ARRAY_SIZE/numprocs);
- s0 = s + ARRAY_SIZE%numprocs;
- max = 0;
- for (i = 0; i < ARRAY_SIZE; i++)
- {
- numbers[i] = i;
- }
- }
- MPI_Send(&numbers, ARRAY_SIZE, MPI_INT, 0, 1, MPI_COMM_WORLD);
- MPI_Send(&s, ARRAY_SIZE, MPI_INT, 0, 2, MPI_COMM_WORLD);
- MPI_Send(&s0, ARRAY_SIZE, MPI_INT, 0, 3, MPI_COMM_WORLD);
- MPI_Send(&max, ARRAY_SIZE, MPI_INT, 0, 4, MPI_COMM_WORLD);
- startIndex = s0 + (myid - 1)*s;
- endIndex = startIndex + s;
- totalTime = 0;
- for (j = 1; j <= TRIALS; j++)
- {
- if (myid == 0)
- {
- for (i = 0; i < s0; i++)
- {
- if (max < numbers[i])
- {
- max = numbers[i];
- }
- }
- }
- else
- {
- for (i = startIndex; i < endIndex; i++)
- {
- if (max < numbers[i])
- {
- max = numbers[i];
- }
- }
- }
- MPI_Recv(&numbers, ARRAY_SIZE, MPI_INT, i, 1, MPI_COMM_WORLD, &stat);
- MPI_Recv(&s, ARRAY_SIZE, MPI_INT, i, 2, MPI_COMM_WORLD, &stat);
- MPI_Recv(&s0, ARRAY_SIZE, MPI_INT, i, 3, MPI_COMM_WORLD, &stat);
- MPI_Recv(&max, ARRAY_SIZE, MPI_INT, i, 4, MPI_COMM_WORLD, &stat);
- if (myid == 0)
- {
- double runTime;
- endwtime = MPI_Wtime();
- runTime = endwtime - startwtime;
- printf("Trial %d : Execution time (sec) = %f\n", j, runTime);
- printf("Max = %d \n", max);
- totalTime += runTime;
- }
- }
- MPI_Finalize();
- if (myid == 0)
- printf("Average time efor %d trials = %f\n", TRIALS, totalTime/TRIALS);
- }
Add Comment
Please, Sign In to add comment