guitar-player

mm.cpp

Mar 30th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <assert.h>
  5. #include <omp.h>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int i, j, k, n;
  11.     double temp, start, end, run;
  12.    
  13.     cout << "Dimensión de la matriz ('N' para NxN) (2-5000): ";
  14.     cin >> n;
  15.    
  16.     assert( n >= 2 && n <= 5000 );
  17.  
  18.   int **A = 0;
  19.   int **B = 0;
  20.   double **C = 0;
  21.  
  22. //memory allocated for elements of rows.
  23. A = new int *[n];
  24. B = new int *[n];
  25. C = new double *[n];
  26.  
  27. //memory allocated for  elements of each column.
  28. for( int i = 0 ; i < n ; i++ )
  29.   {
  30.    A[i] = new int[n];
  31.    B[i] = new int[n];
  32.    C[i] = new double[n];
  33.   }
  34.    
  35.    
  36.    cout <<"Generando arreglo con valores random...\n";
  37.    srand( time(NULL) );
  38.  
  39.     for(i=0; i<n; ++i) {
  40.         for(j=0; j<n; ++j)
  41.          {
  42.             A[i][j] = (rand() % n);
  43.             B[i][j] = (rand() % n);
  44.          }
  45.     }
  46.     cout <<"Arreglos completados.\n";
  47.    
  48. /*
  49.      // showing the matrix on the screen
  50.  
  51.     for(int x=0;x<n;x++)  // loop 3 times for three lines
  52.     {
  53.         for(int y=0;y<n;y++)  // loop for the three elements on the line
  54.         {
  55.             cout<<A[x][y] << "\t";  // display the current element out of the array
  56.         }
  57.     cout<<endl;  // when the inner loop is done, go to a new line
  58.     }
  59.  
  60.    cout << endl;
  61.  
  62.      // showing the matrix on the screen
  63.  
  64.     for(int x=0;x<n;x++)  // loop 3 times for three lines
  65.     {
  66.         for(int y=0;y<n;y++)  // loop for the three elements on the line
  67.         {
  68.             cout<<B[x][y] << "\t";  // display the current element out of the array
  69.         }
  70.     cout<<endl;  // when the inner loop is done, go to a new line
  71.     }
  72.  */
  73.  
  74.  
  75.  //Multiplicacion
  76.   cout << "Multiplicando con OMP...\n";
  77. //  fflush(stdout);
  78.   start = omp_get_wtime();
  79.  
  80. //#pragma omp parallel for private(i, j, k, temp)
  81.       for(i=0; i<n; ++i) {
  82.         for(j=0; j<n; ++j) {
  83.             temp = 0;
  84.             for(k=0; k<n; ++k) {
  85.                 temp += A[i][k] * B[k][j];
  86.             }
  87.             C[i][j] = temp;
  88.         }
  89.     }
  90.  
  91.     end = omp_get_wtime();
  92.     cout << "Le tomó "<< end-start << "segundos.\n";  
  93.  
  94.  /*
  95.   cout << endl;
  96.   for(int x=0;x<n;x++)  // loop 3 times for three lines
  97.     {
  98.         for(int y=0;y<n;y++)  // loop for the three elements on the line
  99.         {
  100.             cout<<C[x][y] << "\t";  // display the current element out of the array
  101.         }
  102.     cout<<endl;  // when the inner loop is done, go to a new line
  103.     }
  104.  
  105. */
  106.  
  107.  
  108.     return 0;  // return 0 to the OS.
  109. }
Advertisement
Add Comment
Please, Sign In to add comment