Created
May 10, 2016 21:17
-
-
Save Titinx/a038238dbf70e62e522fb991e76d7411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <float.h> | |
| //Dimension por defecto de las matrices | |
| int N=100; | |
| int k=4; | |
| //Para calcular tiempo | |
| double dwalltime(){ | |
| double sec; | |
| struct timeval tv; | |
| gettimeofday(&tv,NULL); | |
| sec = tv.tv_sec + tv.tv_usec/1000000.0; | |
| return sec; | |
| } | |
| void mtx_multiply( double* A, double* B, double* R ) | |
| { | |
| int i, j, c; | |
| for ( i = 0; i < N; ++i) | |
| { | |
| for ( j = 0; j < N; ++j) | |
| { | |
| R[ i * N + j ] = 0; | |
| for ( c = 0; c < N; ++c) | |
| { | |
| R[ i * N + j ]+= A[ i * N + c ] * B[ c + N * j ]; | |
| } | |
| } | |
| } | |
| } | |
| void mtx_scalar_multiply( double* M, double s, int NxN) | |
| { | |
| int i; | |
| for ( i = 0; i < (NxN) ; ++i ) | |
| { | |
| M[i]*=s; | |
| } | |
| } | |
| double all_mtx_max(double** M, int NxN) | |
| { | |
| int i,j; | |
| double max = DBL_MIN; | |
| for ( i = 0; i < k ; ++i ) | |
| { | |
| for ( j = 0; j < (NxN); ++j) | |
| { | |
| if ( M[i][j] > max ) { | |
| max=M[i][j]; | |
| } | |
| } | |
| } | |
| return max; | |
| } | |
| double all_mtx_min(double** M, int NxN) | |
| { | |
| int i,j; | |
| double min = DBL_MAX; | |
| for ( i = 0; i < k ; ++i ) | |
| { | |
| for ( j = 0; j < (NxN); ++j) | |
| { | |
| if ( M[i][j] < min ) { | |
| min=M[i][j]; | |
| } | |
| } | |
| } | |
| return min; | |
| } | |
| double all_mtx_avg(double** M, int NxN) | |
| { | |
| int i,j; | |
| double sum = 0; | |
| for ( i = 0; i < k ; ++i ) | |
| { | |
| for ( j = 0; j < (NxN); ++j) | |
| { | |
| sum+=M[i][j]; | |
| } | |
| } | |
| return ( sum / (NxN*k) ); | |
| } | |
| int main(int argc,char*argv[]){ | |
| /* | |
| parametros: | |
| => tamaño de la matriz = N | |
| (matriz NxN) | |
| => Cantidad de matrices = k | |
| */ | |
| double timetick; // para el tiempo | |
| //Controla los argumentos al programa | |
| if ((argc != 3) || ((N = atoi(argv[1])) <= 0) || ((k = atoi(argv[2])) <= 1) ){ | |
| printf("\nUsar: %s n k\n n: Dimension de la matriz (nxn X nxn)\n k cantidad de matrices \n", argv[0] | |
| ); | |
| exit(1); | |
| } | |
| double* M[k]; | |
| double* R1; | |
| double* R2; | |
| int i; | |
| int j; | |
| int c; | |
| int NxN = N * N; | |
| R1 = malloc( sizeof(double) * NxN ); | |
| R2 = malloc( sizeof(double) * NxN ); | |
| // inicializacion | |
| M[0] = malloc( sizeof(double) * NxN ); | |
| for ( j = 0; j < (NxN); j++ ) | |
| { | |
| M[0][j] = j+1; | |
| } | |
| for (i = 1; i < k; ++i) | |
| { | |
| // inicializacion | |
| M[i] = malloc( sizeof(double) * NxN ); | |
| for (c = 0; c < N; c++) | |
| { | |
| for ( j = 0; j < N; j++ ) | |
| { | |
| M[i][c*N+j] = j*N+c+1; | |
| } | |
| } | |
| } | |
| //Realiza la multiplicacion no optimizado | |
| printf("Calculando secuencial\n"); | |
| timetick = dwalltime(); | |
| double max = all_mtx_max(M, NxN); | |
| double min = all_mtx_min(M, NxN); | |
| double avg = all_mtx_avg(M, NxN); | |
| double factor = (max * min) / avg; | |
| // printf("max = %.2f \n", max); | |
| //printf("min = %.2f \n", min); | |
| //printf("avg = %.2f \n", avg); | |
| for (i = 0; i < k; i++) | |
| { | |
| mtx_scalar_multiply(M[i], factor, NxN); | |
| } | |
| mtx_multiply(M[0], M[1], R1); | |
| for (i = 2; i < k; i+=2) | |
| { | |
| mtx_multiply(R1, M[i], R2); | |
| mtx_multiply(R2, M[i+1], R1); | |
| } | |
| printf("Tiempo en segundos %f\n", dwalltime() - timetick); | |
| return(0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment