I am writing fast JPEG reading code that I'm going to use as a piece of a bigger project. I've decided to use CUDA together with NPP for the task, since NPP have all encoding and decoding functions already implemented.
Everything works fine until I try running inverse DCT using nppiDCTQuantInv8x8LS_JPEG_16s8u_C1R_NEW
function. This seems to break code integrity. After running DCT several cudaFree calls report cudaErrorLaunchFailure
. After using NSIGHT CUDA Debugger I can see that launching IDCT function reports CUDA Grid launch failed
error. What could be probable cause? If I don't use NSIGHT CUDA debugger IDCT function ends with NPP_NO_ERROR
, but still corrupts device pointers. I attach pieces of code that I figured to be relevant, but I can supply more on request. I have a feeling that I may have gotten confused with pointers at some point. Though, I've spent considerable amount of time checking and debugging host-side memory in vs debugger.
Actual IDCT Part:
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);
}
Error reported on deallocation of huffman tables:
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
}
}
Errors reported on destruction of 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;
}
Actual call for dct calculation and it's surroroundings:
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:
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;
};
Can anyone help me understand what is going on and what might I be doing wrong? cuda-memcheck reports no errors whatsoever even when I launch problematic IDCT part, I can only detect errors in VS debugger. I trust reading file itself is working correctly, I've ran many tests on it so initial data should be fine. Problems begin with device data. I can also add that launching CUDA profiler with IDCT turned on will crash application and throw non-zero exit code error. Otherwise it runs fine.