0

我正在使用 cuFFT 为 C++ 和 Cuda 中的学校作业编写频率过滤应用程序,但我无法让它工作。您可以在此处找到整个 Visual Studio 2010 解决方案。(需要过剩。)

这是我认为相关的部分:(fourierUtils.cu/194)

//////////////////////////////////////////////////////////////////////////////
// Function to help invoking the kernel, creates the parameters and gets 
// the result
__host__
void Process(
        BitmapStruct& in_img, // these contain an image in an rgba byte array
        BitmapStruct& out_img, 
        MaskGenerator maskGenerator, // this is a pointer to a device function
        float param1, // mask parameters
        float param2)
{    
    // Declare and allocate variables
    cufftHandle plan;

    cufftReal* img;
    cufftReal* dev_img;
    cufftComplex* dev_freq_img;

    int imgsize = in_img.image_size();
    int pixelcount = imgsize / 4;

    img = new float[pixelcount];
    checkResult(
        cudaMalloc(&dev_img, sizeof(cufftReal) * pixelcount));
    checkResult(
        cudaMalloc(&dev_freq_img, sizeof(cufftComplex) * pixelcount));

    // Optimize execution
    cudaFuncAttributes attrs;
    checkResult(
        cudaFuncGetAttributes(&attrs, &Filter));
    std::pair<dim3, dim3> params 
        = Optimizer::GetOptimalParameters(pixelcount, attrs);

    // Process r, g, b channels
    for(int chan = 0; chan <= 2; chan++)
    {
        // Init
        for(int i = 0; i < pixelcount; i++)
        {
            img[i] = in_img.pixels[4 * i + chan];
        }

        checkResult(
            cudaMemcpy(dev_img, img, pixelcount, cudaMemcpyHostToDevice));

        // Create frequency image
        checkResult(
            cufftPlan1d(&plan, pixelcount, CUFFT_R2C, 1));
        checkResult(
            cufftExecR2C(plan, dev_img, dev_freq_img));
        checkResult(
            cudaThreadSynchronize());
        checkResult(
            cufftDestroy(plan));

        // Mask frequency image
        Filter<<<params.first, params.second>>>(
            dev_freq_img, in_img.x, in_img.y, maskGenerator, param1, param2);
        getLastCudaError("Filtering the image failed.");

        // Get result
        checkResult(
            cufftPlan1d(&plan, pixelcount, CUFFT_C2R, 1));
        checkResult(
            cufftExecC2R(plan, dev_freq_img, dev_img));
        checkResult(
            cudaThreadSynchronize());
        checkResult(
            cufftDestroy(plan));
        checkResult(
            cudaMemcpy(img, dev_img, pixelcount, cudaMemcpyDeviceToHost));

        for(int i = 0; i < pixelcount; i++)
        {
            out_img.pixels[4 * i + chan] = img[i];
        }
    }

    // Copy alpha channel
    for(int i = 0; i < pixelcount; i++)
    {
        out_img.pixels[4 * i + 3] = in_img.pixels[4 * i + 3];
    }

    // Free memory
    checkResult(
        cudaFree(dev_freq_img));
    checkResult(
        cudaFree(dev_img));
    delete img;

    getLastCudaError("An error occured during processing the image.");
}

与我看到的官方示例相比,我看不到任何实际差异,但是当我使用 Nsight 对其进行调试时,我的内核接收到的所有 cufftComplex 值都是 NaN,输入图像和结果图像之间的唯一区别是结果底部有一个黑条,无论我使用哪种过滤蒙版和参数。所有 Cuda 和 cuFFT 调用都返回成功,内核调用后也没有报错。

我做错了什么?

我尝试用复杂的数组替换 img 和 dev_img 并使用 C2C 转换并就地进行,但它只改变了结果图像上黑条的大小。

感谢您的帮助。

编辑: 是一个不需要 glut 的简化版本,也应该在 linux 上编译。

4

2 回答 2

2

我还没有编译和运行你的简化版本,但我认为问题在于dev_imgand的大小dev_freq_imag

考虑 CUFFT 库用户指南第 4.2 节中的示例。它执行就地实数到复数变换,这与您首先执行的步骤相同。

#define NX 256

cufftHandle plan;
cufftComplex *data;
cudaMalloc((void**)&data, sizeof(cufftComplex)*(NX/2+1)*BATCH);

cufftPlan1d(&plan, NX, CUFFT_R2C, BATCH);
cufftExecR2C(plan, (cufftReal*)data, data);

由于变换的对称性,cufftExecR2C只填充NX/2+1输出元素,其中NX是输入数组的大小。

在您的情况下,您正在执行以下操作:

cufftHandle plan;

cufftReal* dev_img;
cufftComplex* dev_freq_img;

cudaMalloc(&dev_img, sizeof(cufftReal) * pixelcount);
cudaMalloc(&dev_freq_img, sizeof(cufftComplex) * pixelcount);

所以你正在分配一个cufftReal数组和一个cufftComplex相同大小的数组。当你使用

cufftPlan1d(&plan, pixelcount, CUFFT_R2C, 1);
cufftExecR2C(plan, dev_img, dev_freq_img);

然后只有一半dev_freq_img被 填充cufftExecR2C,其余部分包含垃圾。dev_freq_img如果您在函数中使用完整范围的Filter __global__,那么这可能是您NaN的 s.

于 2013-11-28T21:47:53.640 回答
1

我的错误是在某些调用中忘记将项目的数量乘以它们的大小cudaMemcpy,因此馈送到 cuFFT 的向量的末端由 NaN 组成。解决这些问题就解决了问题。

我还用 cufftComplex 数组替换了 cufftReal 数组,因为 C2C 转换似乎更可预测,并增加了值的标准化。

所以最终的工作方法是:

///////////////////////////////////////////////////////////////////////////////
// Function to help invoking the kernel, creates the parameters and gets 
// the result
__host__
void Process(
        BitmapStruct& in_img, 
        BitmapStruct& out_img, 
        MaskGenerator maskGenerator, 
        float param1, 
        float param2)
{    
    // Declare and allocate variables
    cufftHandle plan;

    cufftComplex* img;
    cufftComplex* dev_img;
    cufftComplex* dev_freq_img;

    int imgsize = in_img.image_size();
    int pixelcount = imgsize / 4;

    img = new cufftComplex[pixelcount];
    checkResult(
        cudaMalloc(&dev_img, sizeof(cufftComplex) * pixelcount));
    checkResult(
        cudaMalloc(&dev_freq_img, sizeof(cufftComplex) * pixelcount));

    // Optimize execution
    cudaFuncAttributes attrs;
    checkResult(
        cudaFuncGetAttributes(&attrs, &Filter));
    std::pair<dim3, dim3> params = 
            Optimizer::GetOptimalParameters(pixelcount, attrs);

    // Process r, g, b channels
    for(int chan = 0; chan <= 2; chan++)
    {
        // Init
        for(int i = 0; i < pixelcount; i++)
        {
            img[i].x = in_img.pixels[4 * i + chan];
            img[i].y = 0;
        }

        checkResult(
            cudaMemcpy(
                dev_img, 
                img, 
                pixelcount * sizeof(cufftComplex), 
                cudaMemcpyHostToDevice));

        // Create frequency image
        checkResult(
            cufftPlan1d(&plan, pixelcount, CUFFT_C2C, 1));
        checkResult(
            cufftExecC2C(plan, dev_img, dev_freq_img, CUFFT_FORWARD));
        checkResult(
            cudaThreadSynchronize());
        checkResult(
            cufftDestroy(plan));

        // Mask frequency image
        Filter<<<params.first, params.second>>>(
            dev_freq_img, 
            in_img.x, 
            in_img.y, 
            maskGenerator, 
            param1, 
            param2);
        getLastCudaError("Filtering the image failed.");

        // Get result
        checkResult(
            cufftPlan1d(&plan, pixelcount, CUFFT_C2C, 1));
        checkResult(
            cufftExecC2C(plan, dev_freq_img, dev_img, CUFFT_INVERSE));
        checkResult(
            cudaThreadSynchronize());
        checkResult(
            cufftDestroy(plan));
        checkResult(
            cudaMemcpy(
                img, 
                dev_img, 
                pixelcount * sizeof(cufftComplex), 
                cudaMemcpyDeviceToHost));

        for(int i = 0; i < pixelcount; i++)
        {
            out_img.pixels[4 * i + chan] = img[i].x / pixelcount;
        }
    }

    // Copy alpha channel
    for(int i = 0; i < pixelcount; i++)
    {
        out_img.pixels[4 * i + 3] = in_img.pixels[4 * i + 3];
    }

    // Free memory
    checkResult(
        cudaFree(dev_freq_img));
    checkResult(
        cudaFree(dev_img));
    delete img;

    getLastCudaError("An error occured during processing the image.");
}

感谢您的帮助。

于 2013-11-29T22:10:12.933 回答