Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <omp.h>
- #include <time.h>
- void crear_matrices(float **A, float **B, int filas, int columnas){
- int i;
- double start, end;
- start = omp_get_wtime();
- A = (float*) malloc(sizeof(float) * filas);
- B = (float*) malloc(sizeof(float) * filas);
- for(i = 0; i < columnas; i++){
- A[i] = (float**) malloc(sizeof(float*) * columnas);
- B[i] = (float**) malloc(sizeof(float*) * columnas);
- }
- end = omp_get_wtime();
- printf("Tiempo crear = %.7f\n", end-start);
- }
- void rellenar_matrices(float **A, float **B, int filas, int columnas){
- int i,j;
- double start, end;
- start = omp_get_wtime();
- for(i = 0; i < filas; i++){
- for(j = 0; j < columnas;j++){
- A[i][j] = rand() % 100;
- B[i][j] = rand() % 100;
- }
- }
- end = omp_get_wtime();
- printf("Tiempo rellenar = %.7f\n", end-start);
- }
- void patron_2d_sec(float **A, float **B, int filas, int columnas, int iteraciones){
- int k,i,j;
- double start, end;
- start = omp_get_wtime();
- for (k=0; k<iteraciones; i++) {
- for (i=1; i<(filas-1); i++) {
- for (j=1; j<(columnas-1); j++) {
- B[i][j] = A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1];
- }
- }
- }
- end = omp_get_wtime();
- printf("Tiempo secuencial = %.7f\n",end-start);
- }
- void patron_2d_omp(float **A, float **B, int filas, int columnas, int iteraciones){
- int k,i,j;
- double start, end;
- start = omp_get_wtime();
- for (k=0; k<iteraciones; i++) {
- for (i=1; i<(filas-1); i++) {
- for (j=1; j<(columnas-1); j++) {
- B[i][j] = A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1];
- }
- }
- }
- end = omp_get_wtime();
- printf("Tiempo OMP = %.7f\n", end-start);
- }
- void main(){
- int iteraciones, filas, columnas, i, threads;
- float **A,**B;
- printf("Escribeme el numero de iteraciones: "); scanf("%d",&iteraciones);
- fflush(stdin);
- printf("Escribeme el numero de filas: "); scanf("%d", &filas);
- fflush(stdin);
- printf("Escribeme el numero de columnas: "); scanf("%d", &columnas);
- fflush(stdin);
- printf("Escribeme el numero de threads: "); scanf("%d", &threads);
- fflush(stdin);
- omp_set_num_threads(threads);
- crear_matrices(A,B,filas,columnas);
- rellenar_matrices(A,B,filas,columnas);
- //patron_2d_omp(A,B,filas,columnas,iteraciones);
- //patron_2d_sec(A,B,filas,columnas, iteraciones);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement