Advertisement
R3P3T

Untitled

Nov 7th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <omp.h>
  4. #include <time.h>
  5.  
  6. void crear_matrices(float **A, float **B, int filas, int columnas){
  7. int i;
  8. double start, end;
  9. start = omp_get_wtime();
  10. A = (float*) malloc(sizeof(float) * filas);
  11. B = (float*) malloc(sizeof(float) * filas);
  12. for(i = 0; i < columnas; i++){
  13. A[i] = (float**) malloc(sizeof(float*) * columnas);
  14. B[i] = (float**) malloc(sizeof(float*) * columnas);
  15. }
  16. end = omp_get_wtime();
  17. printf("Tiempo crear = %.7f\n", end-start);
  18. }
  19.  
  20. void rellenar_matrices(float **A, float **B, int filas, int columnas){
  21. int i,j;
  22. double start, end;
  23. start = omp_get_wtime();
  24. for(i = 0; i < filas; i++){
  25. for(j = 0; j < columnas;j++){
  26. A[i][j] = rand() % 100;
  27. B[i][j] = rand() % 100;
  28. }
  29. }
  30. end = omp_get_wtime();
  31. printf("Tiempo rellenar = %.7f\n", end-start);
  32. }
  33.  
  34. void patron_2d_sec(float **A, float **B, int filas, int columnas, int iteraciones){
  35. int k,i,j;
  36. double start, end;
  37. start = omp_get_wtime();
  38. for (k=0; k<iteraciones; i++) {
  39. for (i=1; i<(filas-1); i++) {
  40. for (j=1; j<(columnas-1); j++) {
  41. B[i][j] = A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1];
  42. }
  43. }
  44. }
  45. end = omp_get_wtime();
  46. printf("Tiempo secuencial = %.7f\n",end-start);
  47. }
  48.  
  49. void patron_2d_omp(float **A, float **B, int filas, int columnas, int iteraciones){
  50. int k,i,j;
  51. double start, end;
  52. start = omp_get_wtime();
  53. for (k=0; k<iteraciones; i++) {
  54. for (i=1; i<(filas-1); i++) {
  55. for (j=1; j<(columnas-1); j++) {
  56. B[i][j] = A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1];
  57. }
  58. }
  59. }
  60. end = omp_get_wtime();
  61. printf("Tiempo OMP = %.7f\n", end-start);
  62. }
  63. void main(){
  64.  
  65. int iteraciones, filas, columnas, i, threads;
  66. float **A,**B;
  67. printf("Escribeme el numero de iteraciones: "); scanf("%d",&iteraciones);
  68. fflush(stdin);
  69. printf("Escribeme el numero de filas: "); scanf("%d", &filas);
  70. fflush(stdin);
  71. printf("Escribeme el numero de columnas: "); scanf("%d", &columnas);
  72. fflush(stdin);
  73. printf("Escribeme el numero de threads: "); scanf("%d", &threads);
  74. fflush(stdin);
  75. omp_set_num_threads(threads);
  76. crear_matrices(A,B,filas,columnas);
  77. rellenar_matrices(A,B,filas,columnas);
  78.  
  79. //patron_2d_omp(A,B,filas,columnas,iteraciones);
  80. //patron_2d_sec(A,B,filas,columnas, iteraciones);
  81. return 0;
  82. }
  83.  
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement