Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Multiplicador de matrices
- Para compilar: icc -O3 -openmp -march=native mmult.c -o mmult
- */
- /*Librerias necesarias*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <assert.h>
- #include <omp.h>
- int main(int argc, char **argv)
- {
- int i,j,k;
- int n;
- double temp;
- double start, end, run;
- printf("Dimension de la matriz ('N' para 'NxN' matriz) (2-5000): ");
- scanf("%d", &n);
- assert( n >= 2 && n <= 5000 );
- int **arr1 = malloc( sizeof(int*) * n);
- int **arr2 = malloc( sizeof(int*) * n);
- int **arr3 = malloc( sizeof(int*) * n);
- for(i=0; i<n; ++i) {
- arr1[i] = malloc( sizeof(int) * n );
- arr2[i] = malloc( sizeof(int) * n );
- arr3[i] = malloc( sizeof(int) * n );
- }
- printf("Generando arreglo con valores random...\n");
- srand( time(NULL) );
- for(i=0; i<n; ++i) {
- for(j=0; j<n; ++j) {
- arr1[i][j] = (rand() % n);
- arr2[i][j] = (rand() % n);
- /* printf("%d \t ", arr1[i][j]);
- printf("%d \t ", arr2[i][j]); */
- }
- }
- printf("Arreglos completados.\n");
- printf("Multplicando sin OMP...");
- fflush(stdout);
- start = omp_get_wtime();
- for(i=0; i<n; ++i) {
- for(j=0; j<n; ++j) {
- temp = 0;
- for(k=0; k<n; ++k) {
- temp += arr1[i][k] * arr2[k][j];
- }
- arr3[i][j] = temp;
- }
- }
- end = omp_get_wtime();
- printf(" Le tomo %f segundos.\n", end-start);
- printf("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 += arr1[i][k] * arr2[k][j];
- }
- arr3[i][j] = temp;
- /* printf("%d \t ", arr3[i][j]); */
- }
- }
- end = omp_get_wtime();
- printf(" Le tomó %f segundos.\n", end-start);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment