Guest User

Untitled

a guest
Oct 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // This program is under development
  2. #include "stdio.h"
  3. #include "math.h"
  4. #include "assert.h"
  5. #include "omp.h"
  6.  
  7. #define EPS 0.001
  8. #define NUM_THREADS 4
  9.  
  10. typedef double f_t(double x);
  11. typedef struct {
  12. double so,se;
  13. double xo,xe;
  14. } calc_t;
  15.  
  16. #define T get_thread_num()
  17.  
  18. double sim(f_t * f, double a, double b, int n) {
  19. double par = f(a)+f(b);
  20. calc_t s[NUM_THREADS]; // s means "state"
  21. double h=(b-a)/n;
  22. double in = 2*NUM_THREADS*h;
  23. assert (n % 8 == 0);
  24. int k = n >> 1, i=0;
  25.  
  26. omp_set_num_threads(NUM_THREADS);
  27.  
  28. int j;
  29. for (j=0;j<NUM_THREADS;j++) {
  30. s[j].se=s[j].so=0.0;
  31. }
  32.  
  33. #pragma omp parallel
  34. {
  35. s[T].xe=a+h*(T+1);
  36. s[T].xo=s[T].xe+h;
  37.  
  38. for (i=0;i<k;i+=2*NUM_THREADS) {
  39. s[T].se+=f(s[T].xe);
  40. s[T].so+=f(s[T].xo);
  41. s[T].xe=s[T].xe+in;
  42. s[T].xo=s[T].xe+h;
  43. }
  44. }
  45. double so=0.0,se=0.0;
  46. for (j=0;j<NUM_THREADS;j++) {
  47. so+=s[j].so;
  48. se+=s[j].se;
  49. }
  50. return (b-a)/(3*n)*(par+4*so+2*se);
  51. }
  52.  
  53. double sin_(double x) {
  54. return sin(x);
  55. }
  56.  
  57. void test_sim_spec() {
  58. double r = sim(sin_, 0, M_PI, 8*80);
  59. printf("test_sim_spec..");
  60. assert (fabs(r)<EPS);
  61. printf("DONE\n");
  62. }
  63. void test_sim_non_null() {
  64. double r = sim(sin_, 0, M_PI/2, 8*80);
  65. printf("test_sim_non_null..");
  66. printf(" r=%f \n", r);
  67. assert (fabs(r)>1.0);
  68. printf("DONE\n");
  69. }
  70.  
  71.  
  72. int main() {
  73. test_sim_spec();
  74. test_sim_non_null();
  75. }
Add Comment
Please, Sign In to add comment