Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #ifdef _OPENMP
  5. #include <omp.h>
  6. #else
  7. #define omp_get_thread_num() 0
  8. #define opm_set_num_threads() 0
  9. #define omp_get_num_threads() 1
  10. #endif
  11.  
  12. #define N 100
  13.  
  14. float work(int a) {
  15. return a+1;
  16. }
  17.  
  18.  
  19. int main(void) {
  20. float a[N], b[N], x, f, sum, sum_expected, psum;
  21. int i;
  22. int num_threads;
  23.  
  24. /* Example 1 (this example contains an OpenMP parallelization error) */
  25. /* --------- */
  26.  
  27. a[0] = 0;
  28. // Added private(i) lastprivate(a,b) schedule(static) #pragma section
  29. // And:
  30. num_threads=omp_get_max_threads(); //Get number of threads
  31. printf("Trabajamos con: %d threads\n", num_threads);
  32. # pragma omp parallel for private(i) lastprivate(a,b) schedule(static)
  33. for (i=1; i<N; i++) {
  34. a[i] = 2.0*i*(i-1);
  35. b[i] = a[i] - a[i-1];
  36. } /* end of omp parallel for */
  37.  
  38. /* testing the correctness of the numerical result: */
  39. sum=0; for (i=1; i<N; i++) { sum = sum + b[i]; }
  40. sum_expected = a[N-1]-a[0];
  41. printf("Exa.1: sum computed=%8.1f, expected=%8.1f, difference=%8.5f \n",
  42. sum, sum_expected, sum-sum_expected);
  43.  
  44.  
  45. /* Example 2 (this example contains an OpenMP parallelization error) */
  46. /* --------- */
  47.  
  48. a[0] = 0;
  49.  
  50. // Changed nowait clause from first loop to second
  51. // And:
  52. # pragma omp parallel
  53. {
  54. # pragma omp for private(i) schedule(static)
  55. for (i=1; i<N; i++){
  56. a[i] = 3.0*i*(i+1);
  57. } /* end of omp for nowait */
  58. # pragma omp for private(i) schedule(static) nowait
  59. for (i=1; i<N; i++){
  60. b[i] = a[i] - a[i-1];
  61. } /* end of omp for */
  62. } /* end of omp parallel */
  63.  
  64. /* testing the correctness of the numerical result: */
  65. sum=0; for (i=1; i<N; i++) { sum = sum + b[i]; }
  66. sum_expected = a[N-1]-a[0];
  67. printf("Exa.2: sum computed=%8.1f, expected=%8.1f, difference=%8.5f \n",
  68. sum, sum_expected, sum-sum_expected);
  69.  
  70.  
  71. /* Example 3 (this example contains an OpenMP parallelization error) */
  72. /* --------- */
  73.  
  74. // Added private(i,x)
  75. # pragma omp parallel for private(i,x)
  76. for (i=1; i<N; i++){
  77. x = sqrt(b[i]) - 1;
  78. a[i] = x*x + 2*x + 1;
  79. } /* end of omp parallel for */
  80.  
  81. /* testing the correctness of the numerical result: */
  82. sum=0; for (i=1; i<N; i++) { sum = sum + a[i]; }
  83. /* sum_expected = same as in Exa.2 */
  84. printf("Exa.3: sum computed=%8.1f, expected=%8.1f, difference=%8.5f \n",
  85. sum, sum_expected, sum-sum_expected);
  86.  
  87. /* Example 4 (this example contains an OpenMP parallelization error) */
  88. /* --------- */
  89.  
  90. f = 2;
  91. // Added lastprivate(x) firstprivate(f)
  92. # pragma omp parallel for lastprivate(x) firstprivate(f)
  93. for (i=1; i<N; i++){
  94. x = f * b[i];
  95. a[i] = x - 7;
  96. } /* end of omp parallel for */
  97. a[0] = x;
  98.  
  99. /* testing the correctness of the numerical result: */
  100. printf("Exa.4: a[0] computed=%8.1f, expected=%8.1f, difference=%8.5f \n",
  101. a[0], 2*b[N-1], a[0] - 2*b[N-1] );
  102.  
  103.  
  104. /* Example 5 (this example contains an OpenMP parallelization error) */
  105. /* --------- */
  106.  
  107. sum = 0;
  108. // Added reduction(+:sum)
  109. # pragma omp parallel for reduction(+:sum)
  110. for (i=1; i<N; i++){
  111. sum = sum + b[i];
  112. } /* end of omp parallel for */
  113.  
  114. /* testing the correctness of the numerical result: */
  115. /* sum_expected = same as in Exa.2 */
  116. printf("Exa.5: sum computed=%8.1f, expected=%8.1f, difference=%8.5f \n",
  117. sum, sum_expected, sum-sum_expected);
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement