Commit 6db606d5 authored by Alexandre Peres Arias's avatar Alexandre Peres Arias

add naive matrix product

Signed-off-by: Alexandre Peres Arias's avatarAlexandre Peres Arias <apa13@inf.ufpr.br>
parent 3c251401
Pipeline #6335 passed with stage
in 1 minute and 14 seconds
......@@ -9,16 +9,22 @@
/* QUE FAZER:
* checar a questa do inline (HAVE_INLINE)
* melhorar a timestamp()
* implementar uma versao ingenua e comparar
* implementar uma versao ingenua e comparar (OK)
* brincar com SVD
*/
/* double matrix multipication
****************************
* EXECUTION: $ ./mult N M *
* (N rows, M columns) *
* EXECUTION: $ ./mult N M P *
* (N rows, M columns, P print matrices (0-no, 1-yes) *
****************************
Program compares gsl matrices product implementation to naive implementation.
From sizes bigger than 700x700 the results are expressives.
*gsl_matrix struct:
typedef struct{
......@@ -36,6 +42,8 @@ size_t N,M; //dimensions
void print_matrix(gsl_matrix *m);
double timestamp(void);
void naive_product(gsl_matrix *m1, gsl_matrix *m2, gsl_matrix *m3);
double timestamp(void){
struct timeval tp;
......@@ -49,14 +57,29 @@ void print_matrix(gsl_matrix *m){
printf(" %8lf|",gsl_matrix_get(m,i,j));
puts(" ");
}
//printf("TDA = %zu\nSIZE1 = %zu\nSIZE2 = %zu",m->tda,m->size1, m->size2);
printf("TDA = %zu\nSIZE1 = %zu\nSIZE2 = %zu\n",m->tda,m->size1, m->size2);
}
void naive_product(gsl_matrix *m1, gsl_matrix *m2, gsl_matrix *m3){
double sum=0.0;
for(size_t i=0; i<m1->size1; ++i)
for(size_t j=0; j<m1->tda; ++j){
for(size_t k=0; k<m2->size1; ++k)
sum += m1->data[i*m1->tda + k] * m2->data[k*m2->tda + j];
m3->data[i*m3->tda + j] = sum;
sum=0.0;
}
}
int main(int argc, char *argv[]){
if(argc < 3){ puts("Arguments error!"); exit(1);}
if(argc < 4){ puts("Arguments error!"); exit(1);}
N = (size_t) atoi(argv[1]);
M = (size_t) atoi(argv[2]);
int print = atoi(argv[3]);
gsl_matrix *m1 = gsl_matrix_alloc(N,M);
gsl_matrix *m2 = gsl_matrix_alloc(M,N);
......@@ -72,21 +95,30 @@ int main(int argc, char *argv[]){
}
printf("***** MATRIX 1 (%zux%zu)\n",N,M);
//print_matrix(m1);
if(print) print_matrix(m1);
printf("\n***** MATRIX 2 (%zux%zu)\n",M,N);
//print_matrix(m2);
double bef = timestamp();
if(print) print_matrix(m2);
double bef = timestamp();
// efficient cache usage product(d-double, mm-matrix-matrix)
gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,m1,m2,1.0,m3);
double aft = timestamp();
printf("\n***** MATRIX RES (%zux%zu)\n",N,N);
//print_matrix(m3);
printf("\n***** GSL MATRIX RES (%zux%zu)\n",N,N);
if(print) print_matrix(m3);
printf("GSL Product time = %lf\n", aft-bef);
// naive product
bef = timestamp();
naive_product(m1,m2,m3);
aft = timestamp();
printf("\n***** NAIVE MATRIX RES(%zux%zu)\n",N,N);
if(print) print_matrix(m3);
printf("Multiplication time = %lf\n", aft-bef);
printf("NAIVE Product time = %lf\n", aft-bef);
gsl_matrix_free(m1);
gsl_matrix_free(m2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment