guitar-player

mmult.c

Mar 29th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. /* Multiplicador de matrices
  2. Para compilar: icc -O3 -openmp -march=native mmult.c -o mmult
  3.  */
  4.  
  5. /*Librerias necesarias*/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <assert.h>
  10. #include <omp.h>
  11.  
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     int i,j,k;
  16.     int n;
  17.     double temp;
  18.     double start, end, run;
  19.  
  20.     printf("Dimension de la matriz ('N' para 'NxN' matriz) (2-5000): ");
  21.     scanf("%d", &n);
  22.  
  23.     assert( n >= 2 && n <= 5000 );
  24.  
  25.     int **arr1 = malloc( sizeof(int*) * n);
  26.     int **arr2 = malloc( sizeof(int*) * n);
  27.     int **arr3 = malloc( sizeof(int*) * n);
  28.  
  29.     for(i=0; i<n; ++i) {
  30.         arr1[i] = malloc( sizeof(int) * n );
  31.         arr2[i] = malloc( sizeof(int) * n );
  32.         arr3[i] = malloc( sizeof(int) * n );
  33.     }
  34.  
  35.     printf("Generando arreglo con valores random...\n");
  36.     srand( time(NULL) );
  37.  
  38.     for(i=0; i<n; ++i) {
  39.         for(j=0; j<n; ++j) {
  40.             arr1[i][j] = (rand() % n);
  41.             arr2[i][j] = (rand() % n);
  42.    /*         printf("%d \t ", arr1[i][j]);
  43.         printf("%d \t ", arr2[i][j]); */
  44.         }
  45.     }
  46.  
  47.     printf("Arreglos completados.\n");
  48.  
  49.     printf("Multplicando sin OMP...");
  50.     fflush(stdout);
  51.     start = omp_get_wtime();
  52.  
  53.     for(i=0; i<n; ++i) {
  54.         for(j=0; j<n; ++j) {
  55.             temp = 0;
  56.             for(k=0; k<n; ++k) {
  57.                 temp += arr1[i][k] * arr2[k][j];
  58.             }
  59.             arr3[i][j] = temp;
  60.         }
  61.     }
  62.  
  63.     end = omp_get_wtime();
  64.  
  65.     printf(" Le tomo %f segundos.\n", end-start);
  66.    
  67.  
  68.  
  69.     printf("Multiplicando con OMP...\n");
  70.     fflush(stdout);
  71.     start = omp_get_wtime();
  72.  
  73. #pragma omp parallel for private(i, j, k, temp)
  74.     for(i=0; i<n; ++i) {
  75.         for(j=0; j<n; ++j) {
  76.             temp = 0;
  77.             for(k=0; k<n; ++k) {
  78.                 temp += arr1[i][k] * arr2[k][j];
  79.             }
  80.             arr3[i][j] = temp;
  81.     /*    printf("%d \t ", arr3[i][j]); */
  82.         }
  83.     }
  84.  
  85.  
  86.     end = omp_get_wtime();
  87.     printf(" Le tomó %f segundos.\n", end-start);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment