- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
以下代码改编自here使用 cufftPlan1d 应用于单个一维变换。最终我想执行批处理就地 R2C 转换,但下面的代码使用单独的输入和输出数组执行单个转换。
如何调整此代码以就地执行转换,从而减少设备上分配的内存量?
谢谢
Cuda 6.5 - 注意:我正在从 MATLAB 2015a 中的 mexFunction 运行代码
代码:
#include <stdlib.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include <cufft.h>
#define DATASIZE 8
#define BATCH 1
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
void main(int argc, char **argv)
{
// --- Host side input data allocation and initialization
cufftReal *hostInputData = (cufftReal*)malloc(DATASIZE*sizeof(cufftReal));
for (int j=0; j<DATASIZE; j++) hostInputData[j] = (cufftReal)(j + 1);
// --- Device side input data allocation and initialization
cufftReal *deviceInputData;
gpuErrchk(cudaMalloc((void**)&deviceInputData, DATASIZE * sizeof(cufftReal)));
cudaMemcpy(deviceInputData, hostInputData, DATASIZE * sizeof(cufftReal), cudaMemcpyHostToDevice);
// --- Host side output data allocation
cufftComplex *hostOutputData = (cufftComplex*)malloc((DATASIZE / 2 + 1) * BATCH * sizeof(cufftComplex));
// --- Device side output data allocation
cufftComplex *deviceOutputData; gpuErrchk(cudaMalloc((void**)&deviceOutputData, (DATASIZE / 2 + 1) * sizeof(cufftComplex)));
cufftResult cufftStatus;
cufftHandle handle;
cufftStatus = cufftPlan1d(&handle, DATASIZE, CUFFT_R2C, BATCH);
if (cufftStatus != cudaSuccess) { mexPrintf("cufftPlan1d failed!"); }
cufftStatus = cufftExecR2C(handle, deviceInputData, deviceOutputData);
if (cufftStatus != cudaSuccess) { mexPrintf("cufftExecR2C failed!"); }
// --- Device->Host copy of the results
gpuErrchk(cudaMemcpy(hostOutputData, deviceOutputData, (DATASIZE / 2 + 1) * sizeof(cufftComplex), cudaMemcpyDeviceToHost));
for (int j=0; j<(DATASIZE / 2 + 1); j++)
printf("%i %f %f\n", j, hostOutputData[j].x, hostOutputData[j].y);
cufftDestroy(handle);
gpuErrchk(cudaFree(deviceOutputData));
gpuErrchk(cudaFree(deviceInputData));
}
最佳答案
解决方案已经在另一个答案中给出:https://stackoverflow.com/a/19208070/678093
对于您的示例,这意味着:
将输入分配为 cufftComplex:
cufftComplex *deviceInputData;
gpuErrchk(cudaMalloc((void**)&deviceInputData, DATASIZE * sizeof(cufftComplex)));
cudaMemcpy(deviceInputData, hostInputData, DATASIZE * sizeof(cufftReal), cudaMemcpyHostToDevice);
就地转换:
cufftStatus = cufftExecR2C(handle, (cufftReal *)deviceInputData, deviceInputData);
gpuErrchk(cudaMemcpy(hostOutputData, deviceInputData, (DATASIZE / 2 + 1) * sizeof(cufftComplex), cudaMemcpyDeviceToHost));
顺便说一句:MATLAB 还包含 fft() 的 GPU 加速版本,也许这对您也有用:http://de.mathworks.com/help/distcomp/run-built-in-functions-on-a-gpu.html#btjw5gk
关于c - 如何使用 cuFFT 执行实数到复数的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29240591/
我现在脑子里有这个迫切的问题:在现代 Fortran 中声明 double 实数的“可接受”方式是什么?从最旧到最新,故事似乎是这样的:DOUBLE PRECISION ,然后 REAL(kind=8
我编写了一个程序,允许将中缀表达式转换为后缀表达式,但它仅适用于一位数字[A-Z][a-z][0-9]。我怎样才能使实数(正数和负数)成为可能? Example: (50 + 3.75) + 50 -
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
使用 z3/python Web 界面,如果我问: x = Real ('x') solve(x * x == 2, show=True) 我很好地理解: Problem: [x·x = 2] Sol
我写了一个正则表达式来匹配任何数字: 正面和负面 十进制 实数 下面的正则表达式很好,但有一个缺点 ([\+\-]{1}){0,1}?[\d]*(\.{1})?[\\d]* 对于 + 或 - 等输入也
为什么当我在 SQL Server 中将 40.54 的值保存到 Real 类型的列时,它返回给我的值更像是 40.53999878999 而不是 40.54?我已经见过几次这种情况,但一直不明白为什
很震惊地学习这个,我肯定在犯错误...... 如果我 DECLARE 一个实数(或十进制)局部变量,我不能分配 (SET) 表达式的值?我必须先设置一个值,然后逐步执行表达式。如果我尝试一次“计算”所
我正在用 Python 为 Fortran 库编写前端。 Python 模块应该可以在 32 位和 64 位机器上运行;适用于 Windows、Linux 和 Mac。 我想了解一些数据类型的字节宽度
在 C++ 中,想要对较长的 (2^20) 实数 vector 进行排序,显然 sort() 可以解决问题。在我习惯了漂亮的 order() 函数之前使用过 R,该函数产生导致排序 vector 的排
给定一个真实值,我们能否检查 float 数据类型是否足以存储数字,或者是否需要 double? 我知道精度因架构而异。是否有任何 C/C++ 函数可以确定正确的数据类型? 最佳答案 有关背景,请参阅
我有一个 data.table , DT其中包含一列 C具有满足不等式的实值条目 0 (i-1)/N & x (i - 1)/10 & x (i - 1)/10 & x (i - 1)/10 &
需要帮助:如何使用 Excel 公式或 VBA 获取字符串中的十进制(实数)数字?我在“A 列”中有一个字符串,上面只有一个十进制数。我只想提取十进制(实数)数字,但它正在提取字符串上的第一个数字。详
我有一个维度为 (Nx, Ny, Nz) 的 3D 数组。 我想使用 FFTW3 库沿 z 轴应用真正的 FFT 和 IFFT。 这里,'z' 是变化最快的索引。 我已经使用 python 编写了相同
目前,我正在考虑拍摄图像及其光谱。现在 Parceval 的定理说两者应该具有相等的能量。然而,当我尝试在某些图像上对此进行测试时,numpy 真实 FFT 函数似乎并非如此。 这是我用于测试的代码:
用于将数值/实数/ double 转换为字符变化/文本。 select (12.0/100)::Double Precision;# 0.12 select (12.0/100)::Double Pr
我正在验证一个函数来计算我实验室中某个标准的通过率。这背后的数学原理非常简单:给定一些通过或失败的测试,通过的百分比是多少。 数据将作为一列值提供,即 P1 (第一次测试通过),F1 (第一次测试失败
我是一名优秀的程序员,十分优秀!