简体   繁体   English

cufft正在向结果中添加随机数据

[英]cufft is adding random data to results

I made a quick program to make sure I could use the cufft library correctly. 我做了一个快速程序,以确保我可以正确使用cufft库。 When I run a batch size of "1" I get the result I expect. 当我运行批量大小“1”时,我得到了我期望的结果。 However, as I increase the batch size, I get what appears to be random bytes at the end of my data buffer. 但是,随着我增加批量大小,我得到了数据缓冲区末尾的随机字节。 If the batch size is 2, the last three entries are noise. 如果批量大小为2,则最后三个条目为噪声。 If the batch size is 3, I get noise in the last six entries at the end of the buffer, as well as in the three entries at the end of what should be the results from the second of the three transforms in the batch. 如果批量大小为3,则在缓冲区末尾的最后六个条目中以及在批次中三个转换中的第二个应该是结果的三个条目中得到噪声。

Example of bad data at the end of the results of the second transform in a batch: 批处理中第二次转换结果的错误数据示例:

7.680291 1.411589 <- good data 7.680291 1.411589 < - 良好的数据
7.748493 1.062853
7.797380 0.710554
7.826757 0.355854
-436781318144.000000 -436781318144.000000 <- start of bad results -436781318144.000000 -436781318144.000000 < - 开始不好的结果
5349828096.000000 5000401408.000000
5511789568.000000 4813803008.000000
5664713728.000000 4619900416.000000
<- end of output < - 输出结束

Code: 码:

#define NX 1024
#define BATCH 4

#include <cuda.h>
#include <cufft.h>
#include <stdio.h>
#include <Windows.h>
#include <math.h>

int main()
{
    cufftHandle plan;
    cufftComplex *deviceData;
    cufftComplex *hostData;
    FILE* output;
    char fileName[256];

    int i, j;

    cudaMalloc((void**)&deviceData, NX * BATCH * sizeof(cufftComplex));
    hostData = (cufftComplex*)malloc(NX * BATCH * sizeof(cufftComplex);

    //Initalize array with a real sin wave, increasing the frequency of the wave for each transform in the batch (indexed by "j")
    for (j = 0; j < BATCH; j++)
    {
        for (i = 0; i < NX; i++)
        {
            hostData[i + j*BATCH].x = sin(i*(j+1) / (float)10);
            hostData[i + j*BATCH].y = 0;
        }
    }

    cudaMemcpy(deviceData, hostData, NX * BATCH * sizeof(cufftComplex), cudaMemcpyHostToDevice);
    cufftPlan1d(&plan, NX, CUFFT_C2C, BATCH);
    cufftExecC2C(plan, deviceData, deviceData, CUFFT_FORWARD);
    cudaThreadSynchronize();
    cudaMemcpy(hostData, deviceData, NX * BATCH * sizeof(cufftComplex), cudaMemcpyDeviceToHost);
    cufftDestroy(plan);
    cudaFree(deviceData);

    output = fopen("outputFile.txt", "w");

    //Write one file for each transform in the batch
    for (j = 0; j < BATCH; j++)
    {
        memset(fileName, '\0', 256);
        sprintf(fileName, "outputFile_%d.txt", j);
        output = fopen(fileName, "w");
        for (i = 0; i < NX; i++)
            fprintf(output, "%f\t%f\n", hostData[i + j*BATCH].x, hostData[i + j*BATCH].y);
        fclose(output);
    }
}

you're mixing up the usage of BATCH and NX to index into your data sets. 你混淆了BATCH和NX的使用来索引你的数据集。

I think your final fprintf line should be this instead of what you have: 我认为你的最终fprintf线应该是这个,而不是你拥有的:

fprintf(output, "%f\\t%f\\n", hostData[i + j*NX].x, hostData[i + j*NX].y);

Likewise you need to change your data setup lines from 同样,您需要更改数据设置行

hostData[i + j*BATCH]...

to

hostData[i + j*NX]...

(2 instances.) (2个实例。)

And while we're at it, this line does not compile for me, it's missing a close parenthesis: 虽然我们正在使用它,但这行不能为我编译,它缺少一个紧密的括号:

hostData = (cufftComplex*)malloc(NX * BATCH * sizeof(cufftComplex);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM