[英]performance tuning a thrust application
我正在装有9600M GT gpu的macbook pro上运行一个小的C ++ / thrust程序(如下),并且对了解函数h中的时间花了很多钱,因为目标是尽可能快地运行此代码,以便运行更大的代码。 NEPS的值。
为此,我用clock()调用乱扔了函数。
显示的时间表明几乎所有时间都用在推力::减少中。 实际上,所报告的推力::减少的时间比推力::变换的时间长数百倍,后者对每个元素调用三个余弦调用。 为什么?
自然,我对测得的时间表示怀疑。 我插入了第二个推力:: reduce只是为了看看报告的时间是否相似:不是。 为第二次呼叫报告的时间方差更大,并且更短。 更困惑:为什么?
我还尝试使用推力::: 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重写了代码,可以将其与推力版本进行比较,并选择运行速度更快的那个:
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调用)
最好不要在多核环境中使用clock()函数。 给出错误的答案是微不足道的。
最好使用挂钟时间clock_gettime 。 同样在Windows中,我们有一些高分辨率计时器。
在使用CUDA时,最好使用CUDA本身提供的计时器。 cutil_timer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.