Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <assert.h>
- #include <omp.h>
- using namespace std;
- int main()
- {
- int i, j, k, n;
- double temp, start, end, run;
- cout << "Dimensión de la matriz ('N' para NxN) (2-5000): ";
- cin >> n;
- assert( n >= 2 && n <= 5000 );
- int **A = 0;
- int **B = 0;
- double **C = 0;
- //memory allocated for elements of rows.
- A = new int *[n];
- B = new int *[n];
- C = new double *[n];
- //memory allocated for elements of each column.
- for( int i = 0 ; i < n ; i++ )
- {
- A[i] = new int[n];
- B[i] = new int[n];
- C[i] = new double[n];
- }
- cout <<"Generando arreglo con valores random...\n";
- srand( time(NULL) );
- for(i=0; i<n; ++i) {
- for(j=0; j<n; ++j)
- {
- A[i][j] = (rand() % n);
- B[i][j] = (rand() % n);
- }
- }
- cout <<"Arreglos completados.\n";
- /*
- // showing the matrix on the screen
- for(int x=0;x<n;x++) // loop 3 times for three lines
- {
- for(int y=0;y<n;y++) // loop for the three elements on the line
- {
- cout<<A[x][y] << "\t"; // display the current element out of the array
- }
- cout<<endl; // when the inner loop is done, go to a new line
- }
- cout << endl;
- // showing the matrix on the screen
- for(int x=0;x<n;x++) // loop 3 times for three lines
- {
- for(int y=0;y<n;y++) // loop for the three elements on the line
- {
- cout<<B[x][y] << "\t"; // display the current element out of the array
- }
- cout<<endl; // when the inner loop is done, go to a new line
- }
- */
- //Multiplicacion
- cout << "Multiplicando con OMP...\n";
- // fflush(stdout);
- start = omp_get_wtime();
- //#pragma omp parallel for private(i, j, k, temp)
- for(i=0; i<n; ++i) {
- for(j=0; j<n; ++j) {
- temp = 0;
- for(k=0; k<n; ++k) {
- temp += A[i][k] * B[k][j];
- }
- C[i][j] = temp;
- }
- }
- end = omp_get_wtime();
- cout << "Le tomó "<< end-start << "segundos.\n";
- /*
- cout << endl;
- for(int x=0;x<n;x++) // loop 3 times for three lines
- {
- for(int y=0;y<n;y++) // loop for the three elements on the line
- {
- cout<<C[x][y] << "\t"; // display the current element out of the array
- }
- cout<<endl; // when the inner loop is done, go to a new line
- }
- */
- return 0; // return 0 to the OS.
- }
Advertisement
Add Comment
Please, Sign In to add comment