#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include "alloc.h" 
#include <assert.h>

#define N 100

static inline double tstamp(void) {
	struct timespec tv;
	clock_gettime(CLOCK_REALTIME, &tv);
	return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}

void init_matrix(int **a,int n)
{
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			a[i][j] = 1;
		}
	}
}


int ** alloc_matrix(int n,int p)
{
	int i;
	int ** m = (int**)my_alloc(n*sizeof(int*));
	assert(m != NULL);

	for(i=0;i<n;i++) {
		m[i] = (int*)my_alloc(p*sizeof(int));
		assert(m[i] != NULL);
	}
	return m;
}

void mult_matrix(int **p,int **a,int **b,int n)
{
	int i,j,k;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			p[i][j]=0;
			for(k=0;k<n;k++) 
				p[i][j]+=a[i][k]*b[k][j];
		}
	}
}
int check_matrix(int ** a,int n)
{
	int i,j;
	for (i=0;i<n;i++)
		for (j=0;j<n;j++)
			if (a[i][j] != n)
				return 0;

	return 1;
}

int main(int argc,char * argv[])
{
	double t1,t2;
	int **a,**b,**c;
	t1=tstamp();
	a=alloc_matrix(N,N);
	b=alloc_matrix(N,N);
	c=alloc_matrix(N,N);
	init_matrix(a,N);
	init_matrix(b,N);
	mult_matrix(c,a,b,N);
	t2=tstamp();
	fprintf(stderr,"time = %lf\n",t2-t1);
	assert(check_matrix(c,N) == 1);

}
