Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include<mpi.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. /**
  6. * @author cristian.chilipirea
  7. *
  8. */
  9.  
  10. int main(int argc, char * argv[]) {
  11. int rank;
  12. int nProcesses;
  13. MPI_Init(&argc, &argv);
  14.  
  15. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  16. MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
  17. printf("Hello from %i/%i\n", rank, nProcesses);
  18.  
  19. int dest, src;
  20.  
  21. MPI_Status Stat;
  22. //if(rank==0) { // This code is run by a single process
  23. int polynomialSize;
  24. int n;
  25. int i;
  26. int j;
  27. float x;
  28.  
  29.  
  30. FILE * polFunctionFile = fopen("a1.txt","rt");
  31. fscanf(polFunctionFile, "%i", &polynomialSize);
  32. float * a = malloc(sizeof(float)*polynomialSize);
  33. for(i=0;i<polynomialSize;i++) {
  34. fscanf(polFunctionFile, "%f", a+i);
  35. }
  36. fclose(polFunctionFile);
  37.  
  38.  
  39. for(x=0; x<3; x+=1) {
  40. float sum = 0;
  41. float xPowI = 1;
  42. float out_pow, in_pow;
  43. float out_sum = 0;
  44. float in_sum;
  45. /*for(i=0;i<polynomialSize;i++) {
  46. sum += xPowI * a[i];
  47. xPowI *= x; // more efficient than pow(x,i);
  48. }
  49. printf("%f ", sum);*/
  50. //printf("%f\n", x);
  51. //for(i=0;i<polynomialSize;i++) {
  52. if(rank==0){
  53. dest = rank+1;
  54. out_sum += xPowI * a[rank];
  55. out_pow = xPowI;
  56. out_pow *= x;
  57. MPI_Send(&out_pow, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
  58. MPI_Send(&out_sum, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
  59.  
  60.  
  61.  
  62. }
  63. else if(rank == nProcesses-1){
  64. src = rank-1;
  65. //dest = rank+1;
  66. MPI_Recv(&in_pow, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
  67. MPI_Recv(&in_sum, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
  68.  
  69. //printf("%f\n", in_pow);
  70. out_sum = in_sum + in_pow * a[rank];
  71. //out_pow = in_pow * x;
  72. printf("%f \n", out_sum);
  73.  
  74. }
  75. else{
  76. src = rank - 1;
  77. dest = rank+1;
  78. MPI_Recv(&in_pow, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
  79.  
  80. sum += in_pow * a[rank];
  81. out_pow = in_pow* x;
  82. MPI_Send(&out_pow, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
  83.  
  84. }
  85. //}
  86. }
  87.  
  88. //}
  89.  
  90. /*for(x = 0;x<3;x++){
  91. float sum = 0;
  92. float xPowI = 1;
  93.  
  94.  
  95. }*/
  96.  
  97. MPI_Finalize();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement