Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /*
  2. * Polynomial Interpolation *
  3. * This program demonstrates a function that performs polynomial
  4. * interpolation. The function is taken from "Numerical Recipes
  5. * in C", Second Edition, by William H. Press, Saul A. Teukolsky,
  6. * William T. Vetterling, and Brian P. Flannery.
  7. *
  8. */
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <omp.h>
  13.  
  14. #define N 20
  15. #define X 14.5
  16.  
  17. #define NUM_THREADS 7
  18.  
  19. /* Number of function sample points */
  20. /* Interpolate at this value of x */
  21. /* Function 'vector' is used to allocate vectors with subscript
  22. range v[nl..nh] */
  23. double *vector (long nl, long nh)
  24. {
  25. double *v;
  26. v = (double *) malloc(((nh-nl+2)*sizeof(double)));
  27. return v-nl+1;
  28. }
  29. /* Function 'free_vector' is used to free up memory allocated
  30. with function 'vector' */
  31. void free_vector(double *v, long nl, long nh)
  32. {
  33. free ((char *) (v+nl-1));
  34. }
  35. /* Function 'polint' performs a polynomial interpolation */
  36. void polint (double xa[], double ya[], int n, double x, double *y, double *dy) {
  37. int i, m, ns=1;
  38. double den,dif,dift,ho,hp,w;
  39. double *c, *d;
  40. dif = fabs(x-xa[1]);
  41. c = vector(1,n);
  42. d = vector(1,n);
  43. #pragma omp parallel
  44.  
  45. #pragma omp parallel for private (i)
  46. for (i=1; i<= n; i++) {
  47. #pragma omp critical
  48.  
  49. dift = fabs (x - xa[i]);
  50. if (dift<dif) {
  51. ns = i;
  52. dif = dift;
  53. }
  54. c[i] = ya[i];
  55. d[i] = ya[i];
  56. }
  57. *y = ya[ns--];
  58.  
  59. #pragma omp parallel
  60. #pragma omp parallel for private (i)
  61.  
  62. for (m = 1; m < n; m++) {
  63. for (i = 1; i<= n-m; i++) {
  64. #pragma omp critical
  65.  
  66. ho = xa[i] - x;
  67. hp = xa[i+m] - x;
  68. w = c[i+1] - d[i]; den = ho - hp; den = w / den; d[i] = hp * den; c[i] = ho * den; }
  69. *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--])); }
  70. free_vector (d, 1, n);
  71. free_vector (c, 1, n);
  72. }
  73.  
  74. /* Functions 'sign' and 'init' are used to initialize the
  75. x and y vectors holding known values of the function.
  76. */
  77. int sign (int j)
  78. {
  79. if (j % 2 == 0) return 1;
  80. else return -1;
  81. }
  82.  
  83. void init (int i, double *x, double *y)
  84. {
  85. // int j;
  86. *x = (double) i;
  87. *y = sin(i);
  88. }
  89. /* Function 'main' demonstrates the polynomial interpolation function
  90. by generating some test points and then calling 'polint' with a
  91. value of x between two of the test points. */
  92. int main (int argc, char *argv[])
  93. {
  94. double x, y, dy;
  95. double *xa, *ya;
  96. int i;
  97.  
  98. double start_time = omp_get_wtime();
  99. omp_set_num_threads(NUM_THREADS); //define el numero de hilos
  100.  
  101. xa = vector (1, N);
  102. ya = vector (1, N);
  103. /* Initialize xa's and ya's */
  104.  
  105. #pragma omp parallel
  106. {
  107. #pragma omp for
  108. for (i = 1; i<= N; i++) {
  109. #pragma omp critical
  110.  
  111. init (i, &xa[i], &ya[i]);
  112. printf ("f(%4.2f) = %13.11f\n", xa[i], ya[i]);
  113. }
  114. }
  115.  
  116. /* Interpolate polynomial at X */
  117. polint (xa, ya, N, X, &y, &dy);
  118. printf ("\nf(%6.3f) = %13.11f with error bound %13.11f\n", X, y, fabs(dy)); free_vector (xa, 1, N);
  119. free_vector (ya, 1, N);
  120. printf("Time: \t %f \n", omp_get_wtime()-start_time);
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement