Commit a807a28f authored by Alexandre Peres Arias's avatar Alexandre Peres Arias

add matrix multiplication test with gsl

Signed-off-by: Alexandre Peres Arias's avatarAlexandre Peres Arias <apa13@inf.ufpr.br>
parent f99907d3
CC=gcc
CFLAGS= -std=c99 -g -O3 \
-pipe \
-ggdb3 -Wstrict-overflow=5 -fstack-protector-all \
-W -Wall -Wextra \
-Wbad-function-cast \
-Wcast-align \
-Wcast-qual \
-Wconversion \
-Wfloat-equal \
-Wformat-y2k \
-Winit-self \
-Winline \
-Winvalid-pch \
-Wmissing-declarations \
-Wmissing-field-initializers \
-Wmissing-format-attribute \
-Wmissing-include-dirs \
-Wmissing-noreturn \
-Wmissing-prototypes \
-Wnested-externs \
-Wnormalized=nfc \
-Wold-style-definition \
-Woverlength-strings \
-Wpacked \
-Wpadded \
-Wpointer-arith \
-Wredundant-decls \
-Wshadow \
-Wsign-compare \
-Wstack-protector \
-Wstrict-aliasing=2 \
-Wstrict-prototypes \
-Wundef \
-Wunsafe-loop-optimizations \
-Wvolatile-register-var \
-Wwrite-strings
INCLUDES=-I/usr
LFLAGS=-L/usr/lib
LIBS= -lgsl -lgslcblas -lm
all: mult
@echo Compiled!
mult: mult_matrix.o
$(CC) $(CFLAGS) -o mult mult_matrix.o $(LFLAGS) $(LIBS)
mult_matrix.o: mult_matrix.c
$(CC) $(CFLAGS) $(INCLUDES) -c mult_matrix.c
clean:
rm -f mult *.o *~
#include <stdlib.h>
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <time.h>
#include <gsl/gsl_blas.h>
#include <sys/time.h>
/* QUE FAZER:
* checar a questa do inline (HAVE_INLINE)
* melhorar a timestamp()
* implementar uma versao ingenua e comparar
* brincar com SVD
*/
/* double matrix multipication
****************************
* EXECUTION: $ ./mult N M *
* (N rows, M columns) *
****************************
*gsl_matrix struct:
typedef struct{
size_t size1; column size
size_t size2; row size
size_t tda; row occupied size in memory
double *data; address of first pointer
gsl_block *block; block pointer address (block where the matrix is located in)
int owner; owner of itself?
}gsl_matrix;
*/
size_t N,M; //dimensions
void print_matrix(gsl_matrix *m);
double timestamp(void);
double timestamp(void){
struct timeval tp;
gettimeofday(&tp,NULL);
return((double)(tp.tv_sec + tp.tv_usec/1000000.0));
}
void print_matrix(gsl_matrix *m){
for (size_t i=0; i<m->size1;++i){
for (size_t j=0; j<m->size2; ++j)
printf(" %8lf|",gsl_matrix_get(m,i,j));
puts(" ");
}
//printf("TDA = %zu\nSIZE1 = %zu\nSIZE2 = %zu",m->tda,m->size1, m->size2);
}
int main(int argc, char *argv[]){
if(argc < 3){ puts("Arguments error!"); exit(1);}
N = (size_t) atoi(argv[1]);
M = (size_t) atoi(argv[2]);
gsl_matrix *m1 = gsl_matrix_alloc(N,M);
gsl_matrix *m2 = gsl_matrix_alloc(M,N);
gsl_matrix *m3 = gsl_matrix_alloc(N,N);
srand(1);
//initialize matrices
for (size_t i=0;i<N;++i)
for (size_t j=0;j<M;++j){
gsl_matrix_set(m1,i,j,(double) (rand()/(RAND_MAX/5)));
gsl_matrix_set(m2,j,i,(double) (rand()/(RAND_MAX/5)));
}
printf("***** MATRIX 1 (%zux%zu)\n",N,M);
//print_matrix(m1);
printf("\n***** MATRIX 2 (%zux%zu)\n",M,N);
//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("Multiplication time = %lf\n", aft-bef);
gsl_matrix_free(m1);
gsl_matrix_free(m2);
gsl_matrix_free(m3);
return 0;
}
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