Advertisement
Guest User

tema1.c

a guest
Oct 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <omp.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. #define N 50 /* dimensiunea vectorilor; experimentati cu diverse valori pentru N */
  6. #define M 40
  7. #define R 60
  8.  
  9. double a[N][M], b[M][R], c[N][R];
  10.  
  11. double get_time();
  12.  
  13. int main (int argc, char *argv[])
  14. {
  15. int i, j, k, tid, nthreads;
  16.  
  17. /* aici puteti initializa vectorii a si b dupa dorinta, prin expresii sau prin citire din fisier */
  18.  
  19. srand(time(NULL));
  20. printf("Generating the elements of the first matrix\n");
  21. for(i = 0; i < N; i++)
  22. {
  23. for(j = 0; j < M; j++)
  24. {
  25. a[i][j] = rand() % 100;
  26. }
  27. }
  28.  
  29. printf("Generating the elements of the second matrix\n");
  30. for(i = 0; i < M; i++)
  31. {
  32. for(j = 0; j < R; j++)
  33. {
  34. b[i][j] = rand() % 100;
  35. }
  36. }
  37.  
  38. display_matrix(a, N, N);
  39. display_matrix(b, N, N);
  40.  
  41. double start_time = omp_get_wtime();
  42. #pragma omp parallel shared(a,b,c,nthreads) private(i,tid)
  43. {
  44. tid = omp_get_thread_num();
  45. if (tid == 0)
  46. {
  47. nthreads = omp_get_num_threads();
  48. printf("Number of threads = %d\n", nthreads);
  49. }
  50.  
  51. printf("Thread %d starting...\n",tid);
  52.  
  53. for (i=0;i<N;i++)
  54. {
  55. for (j=0;j<R;j++)
  56. {
  57. #pragma omp for
  58. for (k=0;k<M;k++)
  59. {
  60. c[i][j] += a[i][k] * b[k][j];
  61. printf("Thread %d: i = %d, j = %d, c[%d][%d] = %f\n", tid, i, j, i, j, c[i][j]);
  62. }
  63. }
  64. }
  65. /* end of for */
  66. printf ("Thread %d done!\n", tid);
  67.  
  68. } /* end of parallel region */
  69. double end = omp_get_wtime();
  70. double duration = end - start_time;
  71. //printf("Parallel program duration: %d\n", duration);
  72.  
  73. for (i = 0; i < N; i++) {
  74. for (j = 0; j < R; j++)
  75. c[i][j] = 0;
  76. }
  77.  
  78. //se poate pune la omp for: schedule(static, 4) nowait - de incercat
  79. //collapse(n) extinde paralelizarea unei bucle pe mai multe bucle imbricate (asta reprezinta n-ul)
  80. //tine cont de scheduler - dynamic vs static, valoarea optima a chunk-ului, etc
  81. double start_time_secv = get_time();
  82. for (i=0;i<N;i++)
  83. {
  84. for (j=0;j<R;j++)
  85. {
  86. for (k=0;k<M;k++)
  87. {
  88. c[i][j] += a[i][k] * b[k][j];
  89. printf("i = %d, j = %d, c[%d][%d] = %f\n", i, j, i, j, c[i][j]);
  90. }
  91. }
  92. }
  93. double end_secv = get_time();
  94. double duration_secv = end_secv - start_time_secv;
  95.  
  96. /*
  97. input data: a[50][40], b[40][60], c[50][60];
  98.  
  99. --paralelizat primul for
  100. Parallel program duration: 805306368
  101. Secvential program duration: 2147483648
  102.  
  103. Parallel program duration: 1879048192
  104. Secvential program duration: 1073741824
  105.  
  106. Parallel program duration: 939524096
  107. Secvential program duration: 536870912
  108.  
  109. Parallel program duration: 1610612736
  110. Secvential program duration: 268435456
  111.  
  112. Parallel program duration: 671088640
  113. Secvential program duration: 268435456
  114.  
  115. --paralelizat al doilea for
  116.  
  117. Parallel program duration: 1207959552
  118. Secvential program duration: 1610612736
  119.  
  120. Parallel program duration: 134217728
  121. Secvential program duration: 1879048192
  122.  
  123. Parallel program duration: 671088640
  124. Secvential program duration: 1879048192
  125.  
  126. Parallel program duration: 402653184
  127. Secvential program duration: 1610612736
  128.  
  129. Parallel program duration: 2013265920
  130. Secvential program duration: 2147483648
  131.  
  132. --paralelizat al treilea for
  133.  
  134. Parallel program duration: 1073741824
  135. Secvential program duration: 1342177280
  136.  
  137. */
  138.  
  139. printf("Parallel program duration: %d\n", duration);
  140. printf("Secvential program duration: %d\n", duration_secv);
  141. printf("Program end.\n");
  142. }
  143.  
  144. void display_matrix(long matrix[150][150], int r, int c)
  145. {
  146. int i, j;
  147. // Displaying the matrix
  148. for (i = 0; i < r; i++) {
  149. for (j = 0; j < c; j++)
  150. printf("%d\t", matrix[i][j]);
  151. printf("\n");
  152. }
  153. }
  154.  
  155. double get_time()
  156. {
  157. struct timeval t;
  158. struct timezone tzp;
  159. gettimeofday(&t, &tzp);
  160. return t.tv_sec + t.tv_usec*1e-6;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement