程序的目的

purpose of program

本文关键字:程序      更新时间:2023-10-16

我需要为一个项目编译一些代码,但我遇到了很多错误,不是因为代码写错了,而是我认为在尝试编译之前应该替换一些变量。实际的代码有点长,因为它是为并行计算而设计的,但下面是一个没有并行性的更简单的版本。这个程序的目的是什么,输入变量是什么:

int main(int argc, char *argv[]) {
  int n = ...;
  float *x, *y;
  x = new float[n+1];
  y = new float[n+1];
  ... // fill x, y
  // do computation
  float e = 0;
  for (int i=1; i<n; ++i) {
   x[i] += ( y[i+1] + y[i-1] )*.5;
   e += y[i] * y[i];
  }
  ... // output x, e
  delete[] x, y;
  return 0;
} 

好。代码似乎主要是示例或概念证明。我想Parallel For是关于它的特殊"ForEach"函数,以及一些数据网格。。。

我修改了网站上的一个示例(如果有人想要一个更可读的示例的话)。从这里

int main(int argc, char *argv[]) {
    int Repeat = 100000;        // Will perform the computation 100,000 times.
    Grid1 *Grd = new Grid1(0, Repeat+1);
    DistArray X(Grd), Y(Grd);   // Some data grid arrays (used for the computations)

    // Set X and Y...
    ForEach(int i, it,
    X(i) = 0;
    Y(i) = 1*i; )

    Grid1IteratorSub it(1, Repeat, Grd);
    float E = 0;    // One of the return values
    // Do the computations:
    ForEach(int i, it,
    X(i) += ( Y(i+1) + Y(i-1) )*.5;
    E += sqr( Y(i) ); )
    // Output the modified data:
    cout << "X: " << X;
    cout << "E: " << E;
    return 0;
}

**查看Parallel For 后编辑