将数据复制到“cufftComplex”数据结构? [英] Copying data to "cufftComplex" data struct?
问题描述
我有数据存储为浮点数组(单精度)。我有一个数组用于我的真实数据,一个数组用于我的复杂数据,我用作FFT的输入。如果我想使用CUDA cufft库,我需要将这个数据复制到 cufftComplex
数据类型。从nVidia: cufftComplex
是一个单精度,浮点复数数据类型,由交织的实部和虚部组成。要由cufft操作的数据存储在 cufftComplex
的数组中。
I have data stored as arrays of floats (single precision). I have one array for my real data, and one array for my complex data, which I use as the input to FFTs. I need to copy this data into the cufftComplex
data type if I want to use the CUDA cufft library. From nVidia: " cufftComplex
is a single‐precision, floating‐point complex data type that consists of interleaved real and imaginary components." Data to be operated on by cufft is stored in arrays of cufftComplex
.
如何快速复制我的数据一个正常的C数组转换成 cufftComplex
的数组?我不想使用 for
循环,因为它可能是最慢的可能选项。我不知道如何使用 memcpy
这种类型的数组数据,因为我不知道它是如何存储在内存中。谢谢!
How do I quickly copy my data from a normal C array into an array of cufftComplex
? I don't want to use a for
loop because it's probably the slowest possible option. I don't know how to use memcpy
on arrays data of this type, because I do not know how it is stored in memory. Thanks!
推荐答案
您可以做为主机设备副本的一部分。每个副本将占用主机上的一个连续输入数组,并以分层方式将其复制到设备。 CUDA中复杂数据类型的存储布局与为Fortran和C ++中的复杂类型定义的布局兼容,即作为实数部分后跟虚数部分的结构。
You could do this as part of a host-> device copy. Each copy would take one of the contiguous input arrays on the host and copy it in strided fashion to the device. The storage layout of complex data types in CUDA is compatible with the layout defined for complex types in Fortran and C++, i.e. as a structure with the real part followed by imaginary part.
float * real_vec; // host vector, real part
float * imag_vec; // host vector, imaginary part
float2 * complex_vec_d; // device vector, single-precision complex
float * tmp_d = (float *) complex_vec_d;
cudaStat = cudaMemcpy2D (tmp_d, 2 * sizeof(tmp_d[0]),
real_vec, 1 * sizeof(real_vec[0]),
sizeof(real_vec[0]), n, cudaMemcpyHostToDevice);
cudaStat = cudaMemcpy2D (tmp_d + 1, 2 * sizeof(tmp_d[0]),
imag_vec, 1 * sizeof(imag_vec[0]),
sizeof(imag_vec[0]), n, cudaMemcpyHostToDevice);
这篇关于将数据复制到“cufftComplex”数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!