Advertisement
Guest User

Untitled

a guest
May 6th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<cmath>
  4. #include "mpi.h"
  5. #define N 100
  6. typedef struct {
  7. double real, imag;
  8. } Complex;
  9.  
  10. void MIN(Complex *in, Complex *inout, int *len, MPI_Datatype *dptr)
  11. {
  12. int i;
  13. for (i = 0; i < *len; i++) {
  14. if(sqrt(inout[i].real*inout[i].real + inout[i].imag*inout[i].imag) > sqrt(in[i].real * in[i].real + in[i].imag * in[i].imag)){
  15. inout[i] = in[i];
  16. }
  17. }
  18. }
  19. void MAX(Complex *in, Complex *inout, int *len, MPI_Datatype *dptr)
  20. {
  21. int i;
  22.  
  23. for (i = 0; i < *len; i++) {
  24. if(sqrt(inout[i].real*inout[i].real + inout[i].imag*inout[i].imag) < sqrt(in[i].real * in[i].real + in[i].imag * in[i].imag)){
  25. inout[i] = in[i];
  26. }
  27. }
  28. }
  29. Complex localMax(Complex *locArr, int len, MPI_Datatype dtp){
  30. Complex locMAX = locArr[0];
  31. for(int i=1; i<len; i++){
  32. if(sqrt(locArr[i].real*locArr[i].real + locArr[i].imag*locArr[i].imag) > sqrt(locMAX.real * locMAX.real + locMAX.imag * locMAX.imag)){
  33. locMAX = locArr[i];
  34. }
  35. }
  36. return locMAX;
  37. }
  38. Complex localMin(Complex *locArr, int len, MPI_Datatype dtp){
  39. Complex locMIN = locArr[0];
  40. for(int i=1; i<len; i++){
  41. if(sqrt(locArr[i].real*locArr[i].real + locArr[i].imag*locArr[i].imag) < sqrt(locMIN.real * locMIN.real + locMIN.imag * locMIN.imag)){
  42. locMIN = locArr[i];
  43. }
  44. }
  45. return locMIN;
  46. }
  47. int main(int argc, char **argv )
  48. {
  49. int rank, size;
  50. double time;
  51. MPI_Op myMAX, myMIN;
  52. MPI_Datatype mytype;
  53. int blocklens[1];
  54. MPI_Aint displs[1];
  55. MPI_Datatype old_types[1];
  56.  
  57. MPI_Init( &argc, &argv );
  58. MPI_Comm_rank(MPI_COMM_WORLD, &rank );
  59. MPI_Comm_size(MPI_COMM_WORLD, &size);
  60.  
  61. int num = N/size;
  62. if(N%size !=0){
  63. num++;
  64. }
  65.  
  66. int len = num*size;
  67. Complex *com = new Complex[len];
  68. Complex max;
  69. Complex *buff = new Complex[num];
  70. Complex local_MAX, resultMAX, local_MIN, resultMIN;
  71.  
  72. blocklens[0] = 2;
  73. displs[0] = 0;
  74. old_types[0] = MPI_DOUBLE;
  75.  
  76. MPI_Type_create_struct(1, blocklens, displs, old_types, &mytype);
  77. MPI_Type_commit(&mytype);
  78.  
  79.  
  80. if(rank == 0)
  81. {
  82. for(int i =0; i<len; i++){
  83. if(i >= N){
  84. com[i].real = com[0].real;
  85. com[i].imag = com[0].imag;
  86. }
  87. else{
  88. com[i].real = (double)(rand()%1000) / (double)(rand()%1000 + 1);
  89. com[i].imag = (double)(rand()%1000) / (double)(rand()%1000 + 1);
  90.  
  91. }
  92. }
  93.  
  94. }
  95.  
  96. time = MPI_Wtime();
  97.  
  98. MPI_Scatter(com, num, mytype, buff, num, mytype, 0, MPI_COMM_WORLD);
  99.  
  100. local_MAX = localMax(buff, num, mytype);
  101.  
  102.  
  103. local_MIN = localMin(buff, num, mytype);
  104.  
  105.  
  106. MPI_Op_create( (MPI_User_function *)MAX, true, &myMAX );
  107. MPI_Reduce(&local_MAX, &resultMAX, 1, mytype, myMAX, 0, MPI_COMM_WORLD);
  108. MPI_Op_free(&myMAX);
  109.  
  110. MPI_Op_create( (MPI_User_function *)MIN, true, &myMIN );
  111. MPI_Reduce(&local_MIN, &resultMIN, 1, mytype, myMIN, 0, MPI_COMM_WORLD);
  112. MPI_Op_free(&myMIN);
  113.  
  114. if(rank == 0){
  115. printf(" Time = %.8f\n", MPI_Wtime() - time);
  116. printf("N = %d\nNum processors = %d\n", N, size);
  117. printf("COM_MAX = %.4f + %.4f*i\n", resultMAX.real, resultMAX.imag);
  118. printf("COM_MIN = %.4f + %.4f*i\n", resultMIN.real, resultMIN.imag);
  119. }
  120. delete[] com;
  121. delete[] buff;
  122. MPI_Finalize();
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement