最近在做时频混合域的全波形反演(FWI),用CUDA加速,要做复数运算,所以研究了一下CUDA中复数运算等。简单说一下CUDA中复数的基本应用。
在CUDA中用CUFFT的库来定义与运算复数,基本如下:
#include "cufft.h"
int main()
{
/* Transform real field to complex field */
cufftComplex *h_cf ; /* host 上的复数变量 */
cufftComplex *d_cf ; /* device 上的复数变量 */
h_cf = (cufftComplex *)malloc(sizeof(cufftComplex) * nf);
cudaMalloc((void**)&d_cf, sizeof(cufftComplex)*nf);
for (int it = 0,t=0.0; it < nt; ++it,t+=dt)
{
for (int iff=0;iff<nf;iff++)
{
h_cf[iff].x=exp(-damp*t)*cos(2.0*PI*invf[iff]*t);
h_cf[iff].y=exp(-damp*t)*sin(2.0*PI*invf[iff]*t);
}
}
CHECK(cudaMemcpy(d_cf,h_cf,sizeof(cufftComplex)*nf,cudaMemcpyHostToDevice));
free(h_cf);
CHECK(cudaFree(d_cf));
}
以上是CUDA中,复数的定义、开辟空间、CPU/GPU间的传递以及内存的释放。希望对大家有用