CUDA NPP功能不正确启动会破坏设备数据

时间:2015-04-16 16:23:11

标签: c++ memory-management cuda jpeg npp

我正在编写快速的JPEG阅读代码,我将把它作为一个更大的项目。我已决定将CUDA与NPP一起用于此任务,因为NPP已经实现了所有编码和解码功能。 一切正常,直到我尝试使用nppiDCTQuantInv8x8LS_JPEG_16s8u_C1R_NEW函数运行逆DCT。这似乎打破了代码的完整性。运行DCT后,几个cudaFree呼叫报告cudaErrorLaunchFailure。使用NSIGHT CUDA调试器后,我可以看到启动IDCT函数报告CUDA Grid launch failed错误。可能的原因是什么?如果我不使用NSIGHT CUDA调试器IDCT函数以NPP_NO_ERROR结尾,但仍会破坏设备指针。我附上了我认为相关的代码片段,但我可以根据要求提供更多代码。我有一种感觉,在某些方面我可能会对指针感到困惑。虽然,我花了相当多的时间在vs调试器中检查和调试主机端内存。

实际IDCT部分:

void CJPEGDecoder::InverseDCT(CJPGFile* file, NppiDCTState* pDCTState, CJPEGDeviceData* dataNPP)
{
    cudaError_t quantError, huffmanError, quantAllocError;
    NppStatus DCTstatus;
    Npp8u* deviceQuantizationTables;

    quantAllocError = cudaMalloc(&deviceQuantizationTables, 64 * file->m_quantizationTables.size());
    for (int i = 0; i < file->m_quantizationTables.size(); i++)
    {
        quantError = cudaMemcpyAsync(deviceQuantizationTables + i * 64, file->m_quantizationTables.at(i).aTable, 64, cudaMemcpyHostToDevice);
    }
    for (int i = 0; i < m_numComponent; i++)
    {
        int blockHeight = dataNPP[i].m_srcSize.height / 8;
        huffmanError = cudaMemcpyAsync(dataNPP[i].m_devDCT, m_hostDCT[i], dataNPP[i].m_DCTStep*blockHeight, cudaMemcpyHostToDevice);
    }

    // Inverse DCT
    for (int i = 0; i < m_numComponent; i++)
    {
        DCTstatus = nppiDCTQuantInv8x8LS_JPEG_16s8u_C1R_NEW(dataNPP[i].m_devDCT, dataNPP[i].m_DCTStep,
            dataNPP[i].m_srcImage, dataNPP[i].m_srcImageStep,
            deviceQuantizationTables + file->m_frameHeader.quantizationSelector[i] * 64,
            dataNPP[i].m_srcSize,
            pDCTState);
    }

    cudaFree(deviceQuantizationTables);
}

报告霍夫曼表的重新分配错误:

void CJPEGDecoder::HuffmanDealloc()
{
    NppStatus DCerror, ACerror;
    cudaError_t error;
    for (int i = 0; i < m_numComponent; i++)
    {
        DCerror = nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanDCTable[i]); //NPP_OK
        ACerror = nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanACTable[i]); //NPP_OK
        error = cudaFreeHost(m_hostDCT[i]); // cudaErrorLaunchFailure if DCT was launched, cudaSuccess otherwise
    }
}

有关销毁CJPEGDeviceData的错误:

void CJPEGDeviceData::ClearData()
{
    cudaError_t errorDCT, errorImg;
    m_DCTStep = 0;
    m_srcImageStep = 0;

    errorDCT = cudaFree(m_devDCT); // cudaErrorLaunchFailure if DCT was launched, cudaSuccess otherwise
    errorImg = cudaFree(m_srcImage); // cudaErrorLaunchFailure if DCT was launched, cudaSuccess otherwise

    m_allocated = false;
}

dct计算的实际调用和它的错误:

void CJPEGWrapper::DecodeJPG()
{
    int numComponents = m_JPGFile->m_frameHeader.numberOfComponents;
    m_deviceData = new CJPEGDeviceData[numComponents];

    uint8_t maxV{ 0 }, maxH{ 0 };
    for (int i = 0; i < numComponents; i++)
    {
        uint8_t testH = m_JPGFile->m_frameHeader.samplingFactor[i] & 0x0F;
        uint8_t testV = m_JPGFile->m_frameHeader.samplingFactor[i] >> 4;
        if (testH > maxH)
            maxH = testH;
        if (testV > maxV)
            maxV = testV;
    }

    m_JPGdecoder.SetImgSize(m_JPGFile->m_frameHeader.width, m_JPGFile->m_frameHeader.height,numComponents);
    m_JPGdecoder.SetMaxMCUSize(maxH, maxV);

    for (int i = 0; i < numComponents; i++)
    {
        m_JPGdecoder.DecodeMCU(m_JPGFile->m_frameHeader.samplingFactor[i],m_deviceData[i]);
    }

    m_JPGdecoder.HuffmanAlloc(m_JPGFile);
    m_JPGdecoder.HuffmanDecode(m_JPGFile, m_deviceData);
    m_JPGdecoder.InverseDCT(m_JPGFile, m_pDCTState, m_deviceData); // IDCT is launched here
    m_JPGdecoder.HuffmanDealloc();
}

CJPEGDeviceData类:

class CJPEGDeviceData
{
public:
    NppiSize m_blockSize;
    NppiSize m_srcSize;
    Npp16s* m_devDCT;
    Npp32s m_DCTStep;

    Npp8u* m_srcImage;
    Npp32s m_srcImageStep;
public:
    CJPEGDeviceData();
    CJPEGDeviceData(const CJPEGDeviceData& object);
    CJPEGDeviceData(CJPEGDeviceData&& object);
    ~CJPEGDeviceData();

    void AllocDevicePointers(NppiSize blocksSize);
    void ClearData();
    bool IsAllocated() const;
private:
    bool m_allocated;
};

任何人都可以帮助我了解发生了什么以及我可能做错了什么? cuda-memcheck报告没有任何错误,即使我启动有问题的IDCT部分,我只能检测VS调试器中的错误。我相信阅读文件本身工作正常,我已经对它进行了很多测试,所以初始数据应该没问题。问题始于设备数据。我还可以补充说,启用IDCT的启动CUDA探查器会使应用程序崩溃并抛出非零退出代码错误。否则运行正常。

0 个答案:

没有答案