【问题标题】:cuComplex.h and exp()cuComplex.h 和 exp()
【发布时间】:2012-04-09 06:47:14
【问题描述】:

Q0:

cuComplex.h 是否支持 exp()?

第一季度:

如何写 A = B * exp(i * C),其中 A、B、C 是相同大小的实数数组?是这样吗?

主要:

cuComplex A;
float B;
cuComplex c;

内核:

c[idx] = ( 0, C[idx] );

A[idx] = B[idx] * exp( c[idx] ); 

第二季度:

cuComplex 包含 2 个浮点数,这意味着我必须为原始矩阵分配 2 倍的内存。有没有办法创建纯虚数?

【问题讨论】:

  • 什么是“有什么方法可以产生纯虚数?”什么意思?
  • 这里有很多问题 - 尝试查看 cuComplex.h 并使用那里的函数进行复数运算。
  • talonmies: sry, "create pure imaginary number" like: a = i * 4, where i imaginary unit

标签: c cuda


【解决方案1】:

cuComplex.h 仅提供对 cuComplex 的一些基本操作(主要是在 CUBLAS 和 CUFFT 库中使用的那些),不支持指数函数。

您可以使用逐分量算术自己实现指数。 cuComplex 将复数的实部存储在 x 分量中,将虚部存储在 y 分量中。给定一个复数 z = x + i*y,指数可以计算为:

exp(z) = exp(x) * (cos(y) + i * sin(y))

这导致以下 CUDA 代码(未经测试):

cuComplex my_complex_exp (cuComplex arg)
{
   cuComplex res;
   float s, c;
   float e = expf(arg.x);
   sincosf(arg.y, &s, &c);
   res.x = c * e;
   res.y = s * e;
   return res;
}

【讨论】:

  • 请注意,OP 不需要float e=expf(cuCrealf(arg)),因为它们处理的是纯虚指数。 +1 sincosf,确实比sinfcosf 加起来快很多;几乎和我的 GT540M 上的每个一样快。
【解决方案2】:

Thrust 现在还支持complex exponentials

没有必要为此功能使用推力。您可以包含推力复合头文件,并在普通 CUDA 代码中使用这些结构。

$ cat t1793.cu
#include <cuComplex.h>
#include <thrust/complex.h>
#include <stdio.h>

__host__ __device__
cuFloatComplex my_complex_exp (cuFloatComplex arg)
{
   cuFloatComplex res;
   float s, c;
   float e = expf(arg.x);
   sincosf(arg.y, &s, &c);
   res.x = c * e;
   res.y = s * e;
   return res;
}

__global__ void demo(){

  cuFloatComplex a = make_cuFloatComplex(1.0f, 1.0f);
  thrust::complex<float> b(1.0f, 1.0f);
  printf("thrust: %f,%f, cuComplex: %f,%f\n", exp(b).real(), exp(b).imag(), cuCrealf( my_complex_exp(a)), cuCimagf(my_complex_exp(a)));
}

int main(){

  demo<<<1,1>>>();
  cudaDeviceSynchronize();
}
$ nvcc -o t1793 t1793.cu
$ cuda-memcheck ./t1793
========= CUDA-MEMCHECK
thrust: 1.468694,2.287355, cuComplex: 1.468694,2.287355
========= ERROR SUMMARY: 0 errors
$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多