#include<stdlib.h>
#include<stdio.h>
typedef double myfloat;
typedef unsigned int myindex;
// always row, colum
// row major
myindex rm(int i, int m, int N, int M) {return i*M + m; }
// column major
myindex cm(int i, int m, int N, int M) {return m*N + i; }
void MxMnaive(int N, int M, int K, myfloat *A, myfloat *B, myfloat *C) {
// computes C += AB, C: N x M, A: N x K, B: K x M, all in row major layout
int i,j,k;
for(i=0; i< N; i++)
for(j=0; j<M; j++)
for(k=0; k<K; k++)
C[rm(i,j, N,M)] += A[rm(i,k, N,K)] * B[rm(k,j, K,M)];
}
int x = 0;
int nextPR() {
x = (x+234532)*((x>> 5 )+12234);
return x & 1023;
}
int hash(int a, int b) { return (a>>5 | a<<27)*(b+2352351);}
int main(int argc, char **argv){
if( argc != 3 ) {
printf("Usage: mult N seed\n");
exit(1);
}
myindex N = atoi(argv[1]);
x = atoi(argv[2]);
myfloat *A = malloc( N*N*sizeof(myfloat));
myfloat *B = malloc( N*N*sizeof(myfloat));
myfloat *C = malloc( N*N*sizeof(myfloat));
int i,j;
if( A == NULL || B==NULL || C==NULL ) {
printf("Could not allocate memory");
exit(2);
}
for(int k=0;k<N/2+1;k++) {
for(i=0; i< N; i++) {
for(j=0; j<N; j++) {
A[rm(i,j,N,N)] = nextPR();
B[rm(i,j,N,N)] = nextPR();
C[rm(i,j,N,N)] = 0;
}
}
MxMnaive(N,N,N, A,B,C);
}
int h = atoi(argv[2]);
for(int k=0;k<3;k++)
for(int i=0; i< N*N; i++) {
// printf("%f ", C[i]);
h = hash(h, (int) C[i]);
}
printf( "%d\n", h & 1023);
return 0;
}
上面的代码是我们在应用算法类中的练习。它是计算两个矩阵的乘法。我问我的老师,但并不是真的理解答案,而且一遍又一遍地问同样的问题有点尴尬,所以我求助于SO。
我不理解的部分是围绕矩阵初始化和散列的循环:
for(int k=0;k<N/2+1;k++) {
...
}
和
for(int k=0;k<3;k++)
首先,为什么我们运行矩阵初始化和矩阵乘法N / 2 + 1次?
其次,当我们对矩阵C进行散列时,为什么k <3的神奇值?
谢谢。