简体   繁体   English

cuda,OpenGL互操作性:cudaGraphicsGLRegisterBuffer上的cudaErrorMemoryAllocation错误

[英]cuda, OpenGL interoperability: cudaErrorMemoryAllocation error on cudaGraphicsGLRegisterBuffer

I am having random cuda memory allocation error on use of cudaGraphicsGLRegisterBuffer() . 我在使用cudaGraphicsGLRegisterBuffer()时遇到随机cuda内存分配错误。 I have a fairly large OpenGL PBO object which is shared with it and CUDA. 我有一个相当大的OpenGL PBO对象,它与它和CUDA共享。 The PBO object is created as follows: PBO对象创建如下:

GLuint          buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
glBufferData(target, rows * cols * 4, NULL, GL_DYNAMIC_COPY);
glUnmapBuffer(_target);
glBindBuffer(_target, 0);

The object is quite large. 对象非常大。 width and height are 5000. However, it allocates fine on my GPU. 宽度和高度都是5000.但是,它在我的GPU上分配很好。 Now, I am sharing this between OpenGL and CUDA as follows. 现在,我将在OpenGL和CUDA之间分享如下内容。 I have a simple class to manage the it as follows: 我有一个简单的类来管理它如下:

class CudaPBOGraphicsResource
{
public:
    CudaPBOGraphicsResource(GLuint pbo_id);
    ~CudaPBOGraphicsResource();
     inline cudaGraphicsResource_t resource() const { return _cgr; }
private:
    cudaGraphicsResource_t          _cgr;
};

CudaPBOGraphicsResource::CudaPBOGraphicsResource(GLuint pbo_id)
{
    checkCudaErrors(cudaGraphicsGLRegisterBuffer(&_cgr, pbo_id,
                    cudaGraphicsRegisterFlagsNone));
    checkCudaErrors(cudaGraphicsMapResources(1, &_cgr, 0));
}

CudaPBOGraphicsResource::~CudaPBOGraphicsResource()
{
    if (_cgr) {
        checkCudaErrors(cudaGraphicsUnmapResources(1, &_cgr, 0));
    }
}

Now I do the OpenGL and CUDA interoperability as follows: 现在我按如下方式进行OpenGL和CUDA互操作:

{
    CudaPBOGraphicsResource input_cpgr(pbo_id);
    uchar4 * input_ptr = 0;
    size_t num_bytes;
    checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void 
                    **)&input_ptr, &num_bytes,
                    input_cpgr.resource()));

    call_my_kernel(input_ptr);
}

This runs for my inputs for a while but after sometime it crashes with: 这会为我的输入运行一段时间,但过了一段时间它会崩溃:

CUDA error code=2(cudaErrorMemoryAllocation) 
                 "cudaGraphicsGLRegisterBuffer(&_cgr, pbo_id, 
                  cudaGraphicsRegisterFlagsNone)" 
Segmentation fault

I am not sure why there is memory allocation going on as I thought this was shared. 我不确定为什么会有内存分配,因为我认为这是共享的。 I added cudaDeviceSynchronize() after the kernel call but the error still persists. 我在内核调用后添加了cudaDeviceSynchronize() ,但错误仍然存​​在。 My call_my_kernel() function is now pretty much doing nothing, so there are no other CUDA calls that can raise this error! 我的call_my_kernel()函数现在几乎什么都不做,因此没有其他CUDA调用可以引发此错误!

I am using Cuda 7 on linux with a K4000 Quadro card. 我在Linux上使用Cuda 7和K4000 Quadro卡。

EDIT I updated the driver to the latest 346.72 version and the error still happens. 编辑我将驱动程序更新到最新的346.72版本,但错误仍然存​​在。 It also does not depend on the kernel call. 它也不依赖于内核调用。 Just calling cudaGraphicsGLRegisterBuffer() seems to leak memory on the GPU. 只是调用cudaGraphicsGLRegisterBuffer()似乎泄漏了GPU上的内存。 Running nvidia-smi as the program is running shows the memory going up steadily. 程序运行时运行nvidia-smi会显示内存稳定上升。 I am still at a loss as to why there is any copying happening... 我仍然不知道为什么会发生任何复制......

Ok, I found the answer to my conundrum and I hope it will help anyone else using CUDA-OGL together. 好的,我找到了我的难题的答案,我希望它能帮助其他人一起使用CUDA-OGL。

The problem was that I was calling: 问题是我在打电话:

checkCudaErrors(cudaGraphicsGLRegisterBuffer(&_cgr, pbo_id,
                cudaGraphicsRegisterFlagsNone));

everytime. 每次。 This actually needs to be called only once and then I just need to call map/unmap on the _cgr object. 这实际上只需要调用一次然后我只需要在_cgr对象上调用map / unmap。

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

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