简体   繁体   English

将向量和矩阵与 C 问题相乘

[英]Multiplying a vector and a Matrix with C problem

my code is :我的代码是:

#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;
}

So I changed N 5000~10000.所以我改了N 5000~10000。 print is :打印是:

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] 

When N = 8000 is someting wrong, times and MFLOPS are stranger than 7000,9000.当 N = 8000 出现错误时,时间和 MFLOPS 比 7000,9000 更奇怪。

It is sure that 7000<8000<9000 in times, but not.可以肯定的是 7000<8000<9000 次,但不是。

and MFLOPS too.和 MFLOPS 也是。 my prints MFLOPS was 8000<7000<9000.我的打印 MFLOPS 是 8000<7000<9000。 its not normal I think我认为这不正常

what this happen?这是怎么回事?

I think what you meant to do is t_w = clk2 - clk1 .我想你的意思是t_w = clk2 - clk1 You haven't done that.你没有那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM