用C问题乘向量和矩阵
我的代码是:用C问题乘向量和矩阵,c,algorithm,matrix,vector,flops,C,Algorithm,Matrix,Vector,Flops,我的代码是: #define N 8000 static double A[N][N]; static double x[N], y[N]; void MyMatVec(double y[N], double A[N][N], double x[N], int n) { int i,j; for(i=0;i<n;i++){ y[i]=0.0; for(j=0;j<n;j++){ y[i]+=A[i][j]*x[j]; }
#define N 8000
static double A[N][N];
static double x[N], y[N];
void MyMatVec(double y[N], double A[N][N], double x[N], int n)
{
int i,j;
for(i=0;i<n;i++){
y[i]=0.0;
for(j=0;j<n;j++){
y[i]+=A[i][j]*x[j];
}
}
}
int main(){
int i, j;
clock_t clk1, clk2;
double t_w,d_mflops;
for(i=0; i<N; i++) {
x[i] = 1.0;
}
for(j=0; j<N; j++) {
for(i=0; i<N; i++) {
A[j][i] = 1.0;
}
}
clk1 = clock();
MyMatVec(y, A, x, N);
clk2 = clock();
t_w=clk2-clk1;
printf("N = %d, \n",N);
printf("multply times = %lf [sec] \n",t_w);
d_mflops = 2.0*(double)N*(double)N/t_w* 1.0e-6;
fprintf(" %lf [MFLOPS] \n", d_mflops);
return 0;
}
当N=8000出现错误时,时间和MFLOP比70009000更奇怪
可以肯定的是,我想你想做的是
t_w=clk2-clk1
。您还没有这样做。t\u w
从未设置。
N = 7000
multply times = 1.068000 [sec]
91.760300 [MFLOPS]
N = 8000
multply times = 2.665000 [sec]
48.030019 [MFLOPS]
N = 9000
multply times = 2.384000 [sec]
67.953020 [MFLOPS]