c++ - 性能调整推力应用程序

标签 c++ performance thrust benchmarking

我正在我的带有 9600M GT gpu 的 macbook pro 上运行一个小的 C++/thrust 程序(如下),我有兴趣了解函数 h 的时间花在哪里,因为目标是尽可能快地运行这段代码可能有更大的 NEPS 值。

为此,我在函数中散布了 clock() 调用。

打印的时间表明几乎所有的时间都花在了thrust::reduce上。 实际上,报告的 thrust::reduce 时间比 thrust::transform 的时间长几百倍,后者对每个元素调用 3 次余弦。为什么?

当然,我对测量的时间持怀疑态度。 我插入了对 thrust::reduce 的第二次调用,只是为了查看报告的时间是否相似:事实并非如此。第二次通话报告的时间具有更高的方差并且更小。 更多的困惑:为什么?

我也曾尝试使用 thrust::transform_reduce(已注释掉)代替两个内核调用,希望它运行得更快——相反,它慢了 4%。为什么?

感谢建议!

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <iostream>

#include <stdio.h>
#include <stdint.h>


 float NEPS = 6.0;
 __device__ float EPS;
 __device__ float SQEPS;

 __device__ float CNV_win;
 __device__ float CNV_dt;
 int CNV_n;
 float EU_dt;

__host__ __device__ float f(float x,float t){
    return x*cos(t)+x*cos(t/SQEPS)+cos(t/EPS);
}

struct h_functor
{
  const float x, t;
  h_functor(float _x, float _t) : x(_x),t(_t) {}
  __host__ __device__
  float operator()(const float & t_f) const {
    return f(x,   t-CNV_win+CNV_dt*(t_f+1)   )*CNV_dt;
  } 
};


clock_t my_clock() __attribute__ ((noinline));
clock_t my_clock() {
  return clock();
}
float h(float x,float t){
    float sum;

    sum = CNV_dt*(f(x,t-CNV_win/2)+f(x,t+CNV_win/2))/2;
    clock_t start = my_clock(), diff1, diff2, diff3, diff4, diff5;
    thrust::device_vector<float> t_f(CNV_n-2);
    diff1 = my_clock() - start;
    /* initialize t_f to 0.. CNV_n-3 */
    start = my_clock();
    thrust::sequence(t_f.begin(), t_f.end());
    diff2 = my_clock() - start;

    start = my_clock();
    thrust::transform(t_f.begin(), t_f.end(), t_f.begin(), h_functor(x,t));
    diff3 = my_clock() - start;
    start = my_clock();
    sum += thrust::reduce(t_f.begin(), t_f.end());
    diff4 = my_clock() - start;
    start = my_clock();
    sum += thrust::reduce(t_f.begin(), t_f.end());
    diff5 = my_clock() - start;
#define usec(d) (d)
    fprintf(stderr, "Time taken %ld %ld %ld %ld %ld usecs\n", usec(diff1), usec(diff2), usec(diff3), usec(diff4), usec(diff5));
        /* a bit slower, surprisingly:
       sum += thrust::transform_reduce(t_f.begin(), t_f.end(), h_functor(x,t), 0, thrust::plus<float>());
       */

    return sum;
}
main(int argc, char ** argv) {
  if (argc >= 1) NEPS = strtod(argv[1], 0);
  fprintf(stderr, "NEPS = %g\n", NEPS);

  EPS= powf(10.0,-NEPS);
  SQEPS= powf(10.0,-NEPS/2.0);
  CNV_win= powf(EPS,1.0/4.0);
  CNV_dt = EPS;
  CNV_n = powf(EPS,-3.0/4.0);
  EU_dt = powf(EPS,3.0/4.0);

  cudaMemcpyToSymbol(CNV_win, &CNV_win, sizeof(float));
  cudaMemcpyToSymbol(CNV_dt, &CNV_dt, sizeof(float));
  cudaMemcpyToSymbol(SQEPS, &SQEPS, sizeof(float));
  cudaMemcpyToSymbol(EPS, &EPS, sizeof(float));

  float x=1.0;
  float t = 0.0;
  int n = floor(1.0/EU_dt);
  fprintf(stderr, "CNV_n = %d\n", CNV_n);
  while (n--) {
    float sum = h(x,t);
    x=x+EU_dt*sum;
    t=t+EU_dt;
  }
  printf("%f\n",x);
}

最佳答案

如果您想优化算法以提高性能,使用 arrayfire 可能是一个选择。我冒昧地为 arrayfire 重写了您的代码,您可以将其与 thrust 版本进行比较,并选择运行速度更快的版本:

float h(float x,float t){

 float sum = CNV_dt * (f(x, t - CNV_win/2) + f(x, t + CNV_win/2)) / 2;
 // initialize t_f with a sequence 0..CNV_n-3
 af::array t_f(af::seq(0, CNV_n-3));

 // transform vector on the GPU
 t_f =  t - CNV_win + CNV_dt*(t_f+1); 
 t_f = (x*cos(t_f) + x*cos(t_f/SQEPS) + cos(t_f/EPS)) * CNV_dt;

 sum += af::sum<float>(t_f); // sum up all elements of the vector
 return sum;
}

另请注意,无需将变量显式复制到 GPU(即无需调用 cudaMemcpyToSymbol)

关于c++ - 性能调整推力应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12869725/

相关文章:

c++ - 编译器独立的类名

c++ - 引用模板参数类型的 static_assert

python - Selenium Python webscraper 真的很慢

c++ - 在输出中混合推力和 cuBLAS 意外结果

c++ - 读/写同一配置文件不同版本的设计方法

java - 从Java插入数千条记录到Oracle,如何才能获得最佳性能?

javascript - 如何减少多个 if 语句的代码

cuda - 是否可以一次包含所有 Thrust header ?

c++ - 将元组的 const 引用传递给仿函数

c++ - 创建将模板类作为构造函数中的参数的模板类