我不清楚实现 sincos() 的最佳方法是什么。我到处都查过,但似乎一致认为它比单独计算 sin 和 cos 更好。下面基本上是我在内核中使用 sincos 的内容。然而,当我分别对 sin 和 cos 进行计时时,它的速度会变慢。我认为这与我如何使用 cPtr 和 sPtr 有关。有没有更好的办法?
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < dataSize)
{
idx += lower;
double f = ((double) idx) * deltaF;
double cosValue;
double sinValue;
double *sPtr = &sinValue;
double *cPtr = &cosValue;
sincos(twopit * f, sPtr, cPtr);
d_re[idx - lower] = cosValue;
d_im[idx - lower] = - sinValue;
//d_re[idx - lower] = cos(twopit * f);
//d_im[idx - lower] = - sin(twopit * f);
}
Run Code Online (Sandbox Code Playgroud)
指针是多余的 - 你可以摆脱它们,例如
double cosValue;
double sinValue;
sincos(twopit * f, &sinValue, &cosValue);
Run Code Online (Sandbox Code Playgroud)
但我不确定这会对性能产生多大影响(不过值得一试)。
在精度要求允许的情况下,还可以考虑使用 float 而不是 double,并使用相应的单精度函数(sincosf
在本例中)。
归档时间: |
|
查看次数: |
3936 次 |
最近记录: |