Guest User

Untitled

a guest
Nov 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #define TRIALS 20
  7. #define ARRAY_SIZE 10000
  8.  
  9. int main (int argc, char *argv[])
  10. {
  11. int myid, numprocs;
  12. double startwtime, endwtime;
  13. int namelen;
  14. int* numbers = new int[ARRAY_SIZE];
  15. int i, j, max;
  16. int s, s0, startIndex, endIndex;
  17. double totalTime;
  18. MPI_Status stat;
  19.  
  20. char processor_name[MPI_MAX_PROCESSOR_NAME];
  21.  
  22. MPI_Init(&argc, &argv);
  23. MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  24. MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  25. MPI_Get_processor_name(processor_name, &namelen);
  26.  
  27. fprintf(stderr, "Process %d on $s\n", myid, processor_name);
  28. fflush(stderr);
  29.  
  30. if (myid == 0)
  31. {
  32. s = (int) floor(ARRAY_SIZE/numprocs);
  33. s0 = s + ARRAY_SIZE%numprocs;
  34. max = 0;
  35.  
  36. for (i = 0; i < ARRAY_SIZE; i++)
  37. {
  38. numbers[i] = i;
  39. }
  40. }
  41.  
  42. MPI_Send(&numbers, ARRAY_SIZE, MPI_INT, 0, 1, MPI_COMM_WORLD);
  43. MPI_Send(&s, ARRAY_SIZE, MPI_INT, 0, 2, MPI_COMM_WORLD);
  44. MPI_Send(&s0, ARRAY_SIZE, MPI_INT, 0, 3, MPI_COMM_WORLD);
  45. MPI_Send(&max, ARRAY_SIZE, MPI_INT, 0, 4, MPI_COMM_WORLD);
  46.  
  47. startIndex = s0 + (myid - 1)*s;
  48. endIndex = startIndex + s;
  49.  
  50. totalTime = 0;
  51.  
  52. for (j = 1; j <= TRIALS; j++)
  53. {
  54. if (myid == 0)
  55. {
  56. for (i = 0; i < s0; i++)
  57. {
  58. if (max < numbers[i])
  59. {
  60. max = numbers[i];
  61. }
  62. }
  63. }
  64. else
  65. {
  66. for (i = startIndex; i < endIndex; i++)
  67. {
  68. if (max < numbers[i])
  69. {
  70. max = numbers[i];
  71. }
  72. }
  73. }
  74.  
  75. MPI_Recv(&numbers, ARRAY_SIZE, MPI_INT, i, 1, MPI_COMM_WORLD, &stat);
  76. MPI_Recv(&s, ARRAY_SIZE, MPI_INT, i, 2, MPI_COMM_WORLD, &stat);
  77. MPI_Recv(&s0, ARRAY_SIZE, MPI_INT, i, 3, MPI_COMM_WORLD, &stat);
  78. MPI_Recv(&max, ARRAY_SIZE, MPI_INT, i, 4, MPI_COMM_WORLD, &stat);
  79.  
  80. if (myid == 0)
  81. {
  82. double runTime;
  83. endwtime = MPI_Wtime();
  84. runTime = endwtime - startwtime;
  85.  
  86. printf("Trial %d : Execution time (sec) = %f\n", j, runTime);
  87. printf("Max = %d \n", max);
  88. totalTime += runTime;
  89. }
  90. }
  91.  
  92. MPI_Finalize();
  93.  
  94. if (myid == 0)
  95. printf("Average time efor %d trials = %f\n", TRIALS, totalTime/TRIALS);
  96. }
Add Comment
Please, Sign In to add comment