Guest User

Untitled

a guest
Oct 21st, 2019
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---- Lab 2 -----
  2. ---- fake ------
  3. ---- lab 3 ------
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <omp.h>
  9.  
  10. #define N 500
  11. #define M 600
  12. #define P 700
  13.  
  14. int main() {
  15. srand(time(NULL));
  16. int i, j, k;
  17. double suma = 0;
  18.  
  19. double** dyn_matrix1;
  20. dyn_matrix1 = (double**)malloc(N * sizeof(double*));
  21. for(i = 0; i < N; i++)
  22. dyn_matrix1[i] = (double*)malloc(M * sizeof(double));
  23.  
  24. double** dyn_matrix2;
  25. dyn_matrix2 = (double**)malloc(M * sizeof(double*));
  26. for(i = 0; i < M; i++)
  27. dyn_matrix2[i] = (double*)malloc(P * sizeof(double));
  28.  
  29. double** dyn_result;
  30. dyn_result = (double**)malloc(N * sizeof(double*));
  31. for(i = 0; i < N; i++)
  32. dyn_result[i] = (double*)malloc(P * sizeof(double));
  33.  
  34. double** dyn_result_serial;
  35. dyn_result_serial = (double**)malloc(N * sizeof(double*));
  36. for(i = 0; i < N; i++)
  37. dyn_result_serial[i] = (double*)malloc(P * sizeof(double));
  38.  
  39.  
  40. for (i = 0; i < N; i++) {
  41. for (j = 0; j < M; j++)
  42. dyn_matrix1[i][j] = rand() % 1000;
  43. }
  44.  
  45. for (i = 0; i < M; i++) {
  46. for (j = 0; j < P; j++)
  47. dyn_matrix2[i][j] = rand() % 1000;;
  48. }
  49.  
  50. double start = omp_get_wtime();
  51. for (i = 0; i < N; i++) {
  52. #pragma omp parallel for num_threads(6) schedule(static) private(k) reduction(+:suma)
  53. for (j = 0; j < P; j++) {
  54. for (k = 0; k < M; k++) {
  55. suma = suma + dyn_matrix1[i][k] * dyn_matrix2[k][j];
  56. }
  57.  
  58. dyn_result[i][j] = suma;
  59. suma = 0;
  60. }
  61. }
  62. printf("Czas równoległy: %f \n", omp_get_wtime() - start);
  63.  
  64. suma = 0;
  65.  
  66.  
  67. start = omp_get_wtime();
  68. for (i = 0; i < N; i++) {
  69. for (j = 0; j < P; j++) {
  70. for (k = 0; k < M; k++) {
  71. suma = suma + dyn_matrix1[i][k] * dyn_matrix2[k][j];
  72. }
  73.  
  74. dyn_result_serial[i][j] = suma;
  75. suma = 0;
  76. }
  77. }
  78. printf("Czas szeregowy: %f \n", omp_get_wtime() - start);
  79.  
  80. for (i = 0; i < N; i++) {
  81. for (j = 0; j < P; j++) {
  82. if(dyn_result_serial[i][j] != dyn_result[i][j]) printf("NIE!");
  83. }
  84. }
  85.  
  86. /*for (i = 0; i < N; i++) {
  87. for (j = 0; j < P; j++) {
  88. printf("%lf ", dyn_result[i][j]);
  89. }
  90. printf("\n");
  91. }
  92.  
  93. printf("\n");printf("\n");
  94.  
  95. for (i = 0; i < N; i++) {
  96. for (j = 0; j < P; j++) {
  97. printf("%lf ", dyn_result_serial[i][j]);
  98. }
  99. printf("\n");
  100. }*/
  101.  
  102. return 0;
  103. }
RAW Paste Data