Advertisement
Guest User

maxmin

a guest
Nov 26th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <signal.h>
  6.  
  7. #define MYTAG 1
  8. int rank, j;
  9. double startwtime = 0.0, endwtime;
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. int total, n, size, i, dest;
  14. double result;
  15. MPI_Status status;
  16.  
  17. MPI_Init(&argc, &argv);
  18. MPI_Comm_size(MPI_COMM_WORLD, &size);
  19. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  20.  
  21. double a[10][10];
  22. total = 10;
  23.  
  24. if (rank == 0)
  25. n = total / size + 1;
  26. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  27.  
  28. //a = new double[total];
  29.  
  30.  
  31. if (rank == 0) {
  32. for (dest=1; dest < size; dest++) {
  33. for (i = 0; i < n; i++) {
  34. for(int j = 0; j < total;j++) {
  35. a[i][j] = 5;
  36. }
  37.  
  38. }
  39. MPI_Send(a, n, MPI_DOUBLE, dest, MYTAG, MPI_COMM_WORLD);
  40. }
  41. n = total - n*(size-1);
  42.  
  43. } else {
  44. MPI_Recv(a, n, MPI_DOUBLE, 0, MYTAG, MPI_COMM_WORLD, &status);
  45. }
  46.  
  47. double max = a[0][0];
  48. double min = a[0][0];
  49. for (i = 0; i < n; i++) {
  50. for(int j = 0; j < total;j++) {
  51. if (a[i][j] < min)
  52. min = a[i][j];
  53. }
  54. if (min > max)
  55. max = min;
  56. }
  57.  
  58. MPI_Reduce(&max, &result, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  59.  
  60. if (rank == 0) {
  61. printf("Answer is %.4f\n", result);
  62. }
  63.  
  64. MPI_Finalize();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement