#define index(i,j,power) (((i)<<(power))+(j))
void recMultiply(int i, int j, float a[], int k, int l, float b[], int x, int y, float c[], int s);
int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize;
float sum, maxr, total=0.0, startmult, finishmult, multtime;
float* A = NULL;
float* B = NULL;
float* C = NULL;
int main(int argc, char *argv[])
{
//functions for producing random numbers
srand(99999);
maxr = (float)RAND_MAX;
//store user parameters into variables
power = atoi( argv[1] );
bsize = atoi( argv[2] );
matrixsize = int(pow(2,power));
blocksize = int(pow(2,bsize));
cout<<"Matrix Size = 2^"<<power<<" = "<<"("<<matrixsize<<"*"<<matrixsize<<")"<<" matrix"<<endl<<endl;
A = new float[matrixsize*matrixsize];
B = new float[matrixsize*matrixsize];
C = new float[matrixsize*matrixsize];
//fill matrices A and B with pseudo-random float numbers
for (i = 0; i < matrixsize; i++)
for (j = 0; j < matrixsize; j++)
{
A[index(i,j,power)] = rand()/maxr;
B[index(i,j,power)] = rand()/maxr;
C[index(i,j,power)] = 0.0;
}
//start clock - multiplication
startmult = clock();
//call matrix multiplication function
recMultiply(0, 0, A, 0, 0, B, 0, 0, C, matrixsize);
//Stop clock - multiplication
finishmult = clock();
//Calculate time needed to execute multiplication
multtime = (finishmult-startmult)/CLOCKS_PER_SEC;
cout<<"Sum of the elements of matrix C: "<<total<<endl<<endl;
cout<<"Time for matrix multiplication: "<<multtime<<" seconds"<<endl<<endl;
cin>>s;
return 0;
}
void recMultiply(int i, int j, float a[], int k, int l, float b[], int x, int y, float c[], int s){
//recursive matrix multiplication algorithm
if (s == 2)
{
c[0][0]=a[0][0]*b[0][0]+a[0][1]*b[1][0];
c[0][1]=a[0][0]*b[0][1]+a[0][1]*b[1][1];
c[1][0]=a[1][0]*b[0][0]+a[1][1]*b[1][0];
c[1][1]=a[1][0]*b[1][0]+a[1][1]*b[1][1];
}
else {
recMultiply(0, 0, a, 0, 0, b, 0, 0, c, s/2);
recMultiply(0, s/2, a, s/2, 0, b, 0, 0, c, s/2);
recMultiply(0, 0, a, 0, s/2, b, 0, s/2, c, s/2);
recMultiply(0, s/2, a, s/2, s/2, b, 0, s/2, c, s/2);
recMultiply(s/2, 0, a, 0, 0, b, s/2, 0, c, s/2);
recMultiply(s/2, s/2, a, s/2, 0, b, s/2, 0, c, s/2);
recMultiply(s/2, 0, a, 0, s/2, b, s/2, s/2, c, s/2);
recMultiply(s/2, s/2, a, s/2, s/2, b, s/2, s/2, c, s/2);
}
}
This code doesn't work good because for the case where the size of the matrix is two,it gives me the product between matrix correctly but when the size of the matrix is power of two doesn't work good .This code is designed only for the matrix where the size is a power of two.Can you help me?Thank you for attention
Bye Alcantarex

New Topic/Question
Reply




MultiQuote










|