Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<pthread.h>
  4. #include <math.h>
  5.  
  6. #include"pomiar_czasu.h"
  7.  
  8. #define ROZMIAR 10000000
  9. #define LICZBA_W 8
  10.  
  11. #define PI 3.141592653
  12.  
  13. pthread_mutex_t muteks;
  14. pthread_t watki[LICZBA_W];
  15. double global_array_of_local_sums[LICZBA_W];
  16.  
  17.  
  18. void *suma_w( void *arg_wsk);
  19. void *suma_w_no_mutex( void *arg_wsk);
  20. void *calka_w ( void *calka_w );
  21.  
  22. double *tab;
  23. double suma=0;
  24.  
  25. int main( int argc, char *argv[] ){
  26.  
  27. int i;
  28.  
  29. double t1,t2,t3;
  30.  
  31. int indeksy[LICZBA_W];
  32. for(i=0;i<LICZBA_W;i++) indeksy[i]=i;
  33.  
  34. tab = (double *) malloc(ROZMIAR*sizeof(double));
  35. for(i=0; i<ROZMIAR; i++ ) tab[i] = ((double) i+1) / ROZMIAR;
  36.  
  37.  
  38. pthread_mutex_init( &muteks, NULL);
  39. printf("Liczba watkow: %d, Rozmiar: %d\n", LICZBA_W, ROZMIAR);
  40. printf("Poczatek tworzenia watkow\n");
  41.  
  42. t1 = czas_zegara();
  43.  
  44. for(i=0; i<LICZBA_W; i++ )
  45. pthread_create( &watki[i], NULL, calka_w, (void *) &indeksy[i] );
  46. // synchronization error !!!
  47. // pthread_create( &watki[i], NULL, suma_w, (void *) &i );
  48.  
  49. for(i=0; i<LICZBA_W; i++ ) pthread_join( watki[i], NULL );
  50.  
  51. t1 = czas_zegara() - t1;
  52. printf("suma = %lf\n", suma);
  53. printf("Czas obliczen = %lf\n", t1);
  54.  
  55. /* // version with array of local sums (to eliminate the mutex)
  56. suma =0;
  57. printf("Poczatek tworzenia watkow\n");
  58. t1 = czas_zegara();
  59.  
  60. for(i=0; i<LICZBA_W; i++ ) {
  61. global_array_of_local_sums[i]=0.0;
  62. pthread_create( &watki[i], NULL, suma_w_no_mutex, (void *) &indeksy[i] );
  63. // synchronization error !!!
  64. //pthread_create( &watki[i], NULL, suma_w, (void *) &i );
  65. }
  66.  
  67. for(i=0; i<LICZBA_W; i++ ) {
  68. pthread_join( watki[i], NULL );
  69. suma += global_array_of_local_sums[i];
  70. }
  71.  
  72. t1 = czas_zegara() - t1;
  73. printf("suma = %lf\n", suma);
  74. printf("Czas obliczen (no mutex) = %lf\n", t1);
  75. */
  76. /* suma =0; */
  77.  
  78. /* printf("Poczatek tworzenia watkow OpenMP\n"); */
  79. /* t1 = czas_zegara(); */
  80.  
  81. /* #pragma omp parallel for num_threads(LICZBA_W) default(none) shared (tab) reduction(+:suma) */
  82. /* for(i=0;i<ROZMIAR;i++){ */
  83. /* suma += tab[i]; */
  84. /* } */
  85.  
  86.  
  87.  
  88. /* t1 = czas_zegara() - t1; */
  89. /* printf("suma = %lf\n", suma); */
  90. /* printf("Czas obliczen OpenMP = %lf\n", t1); */
  91.  
  92.  
  93.  
  94. }
  95.  
  96. void *suma_w( void *arg_wsk){
  97.  
  98. int i, j, moj_id;
  99.  
  100. double moja_suma=0;
  101.  
  102. moj_id = *( (int *) arg_wsk );
  103.  
  104. j=ROZMIAR/LICZBA_W;
  105. for( i=j*moj_id+1; i<=j*(moj_id+1); i++){
  106. moja_suma += tab[i];
  107. }
  108.  
  109. pthread_mutex_lock( &muteks );
  110. suma += moja_suma;
  111. pthread_mutex_unlock( &muteks );
  112.  
  113. pthread_exit( (void *)0);
  114.  
  115. }
  116.  
  117. void *suma_w_no_mutex( void *arg_wsk){
  118.  
  119. int i, j, moj_id;
  120.  
  121. moj_id = *( (int *) arg_wsk );
  122.  
  123. j=ROZMIAR/LICZBA_W;
  124. for( i=j*moj_id+1; i<=j*(moj_id+1); i++){
  125. global_array_of_local_sums[moj_id] += tab[i];
  126. }
  127.  
  128. pthread_exit( (void *)0);
  129.  
  130. }
  131.  
  132. double f(double x) {
  133. return sin(x);
  134. }
  135.  
  136. void *calka_w(void *arg_wsk) {
  137. double c, dx, b, a, x1, x2;
  138. int i, j, moj_id;
  139. j=ROZMIAR/LICZBA_W;
  140.  
  141. a=j*moj_id+1;
  142. b=PI;
  143. x1=a;
  144. c=0.0;
  145. dx = (b-a) / ROZMIAR;
  146. moj_id = *( (int *) arg_wsk );
  147.  
  148. for(i=j*moj_id+1; i<=j*(moj_id+1); ++i) {
  149. x2=x1+dx;
  150. c+=f(x1)+f(x2);
  151. x1=x2;
  152. }
  153.  
  154. c *= 0.5*dx;
  155. pthread_mutex_lock( &muteks );
  156. suma += c;
  157. pthread_mutex_unlock( &muteks );
  158.  
  159. pthread_exit( (void *)0);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement