将数据复制到“cufftComplex"数据结构? [英] Copying data to "cufftComplex" data struct?

查看:29
本文介绍了将数据复制到“cufftComplex"数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据存储为浮点数组(单精度).我有一个数组用于我的真实数据,一个数组用于我的复杂数据,我将其用作 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆