Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. where p = # of processes
  2. n = number of intervals per process
  3. a = lower limit of integration
  4. b = upper limit of integration
  5. h = (b-a)/(n*p)
  6. aij = a +[ i*n +j]h
  7.  
  8. /* C Example */
  9. #include <mpi.h>
  10. #include <math.h>
  11. #include <stdio.h>
  12. float fct(float x)
  13. {
  14. return cos(x);
  15. }
  16. /* Prototype */
  17. float integral(float a, int n, float h);
  18. void main(argc,argv)
  19. int argc;
  20. char *argv[];
  21. {
  22. /***********************************************************************
  23. * *
  24. * This is one of the MPI versions on the integration example *
  25. * It demonstrates the use of : *
  26. * *
  27. * 1) MPI_Init *
  28. * 2) MPI_Comm_rank *
  29. * 3) MPI_Comm_size *
  30. * 4) MPI_Recv *
  31. * 5) MPI_Send *
  32. * 6) MPI_Finalize *
  33. * *
  34. ***********************************************************************/
  35. int n, p, i, j, ierr,num;
  36. float h, result, a, b, pi;
  37. float my_a, my_range;
  38.  
  39. int myid, source, dest, tag;
  40. MPI_Status status;
  41. float my_result;
  42.  
  43. pi = acos(-1.0); /* = 3.14159... */
  44. a = 0.; /* lower limit of integration */
  45. b = pi*1./2.; /* upper limit of integration */
  46. n = 100000; /* number of increment within each process */
  47.  
  48. dest = 0; /* define the process that computes the final result */
  49. tag = 123; /* set the tag to identify this particular job */
  50.  
  51. /* Starts MPI processes ... */
  52.  
  53. MPI_Init(&argc,&argv); /* starts MPI */
  54. MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* get current process id */
  55. MPI_Comm_size(MPI_COMM_WORLD, &p); /* get number of processes */
  56.  
  57. h = (b-a)/n; /* length of increment */
  58. num = n/p; /* number of intervals calculated by each process*/
  59. my_range = (b-a)/p;
  60. my_a = a + myid*my_range;
  61. my_result = integral(my_a,num,h);
  62.  
  63. printf("Process %d has the partial result of %f\n", myid,my_result);
  64.  
  65. if(myid == 0) {
  66. result = my_result;
  67. for (i=1;i<p;i++) {
  68. source = i; /* MPI process number range is [0,p-1] */
  69. MPI_Recv(&my_result, 1, MPI_REAL, source, tag,
  70. MPI_COMM_WORLD, &status);
  71. result += my_result;
  72. }
  73. printf("The result =%f\n",result);
  74. }
  75. else
  76. MPI_Send(&my_result, 1, MPI_REAL, dest, tag,
  77. MPI_COMM_WORLD); /* send my_result to intended dest.
  78. */
  79. MPI_Finalize(); /* let MPI finish up ... */
  80. }
  81. float integral(float a, int n, float h)
  82. {
  83. int j;
  84. float h2, aij, integ;
  85.  
  86. integ = 0.0; /* initialize integral */
  87. h2 = h/2.;
  88. for (j=0;j<n;j++) { /* sum over all "j" integrals */
  89. aij = a + j*h; /* lower limit of "j" integral */
  90. integ += fct(aij+h2)*h;
  91. }
  92. return (integ);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement