Reusing NvBuffers and maintaining cpu and gpu pointers

I was wondering if it’s OK to leave resources mapped/registered while copying into an NvBuffer. My program maintains a cache of NvBuffers in order to avoid continually creating and destroying them. On each acquired frame I call copyToNvBuffer to copy the new image into one of the cached buffers (perform block linear to pitch linear conversion and scaling). After that I access the image on the cpu or in cuda to perform processing. Rough pseudo-code would be:

function createNvBuffers()
{
  for (i=0; i<num_buffers; ++i)
    NvBufferCreateEx()
}

function destroyNvBuffers()
{
  for (i=0; i<num_buffers; ++i)
    NvBufferDestroy()
}

function process_image(my_buffer)
{
  // do some processing
  cuGraphicsUnregisterResource()
  NvDestroyEGLImage()
  NvBufferMemUnMap() - for y/u/v planes
  return_buffer_to_cache(my_buffer)
}

function main()
{
  cache = createNvBuffers()

  while (run)
  {
    acquire_frame()
    my_buffer = get_free_nvbuffer_from_cache()
    iNativeBuffer->copyToNvBuffer(my_buffer)
    NvBufferMemMap() + NvBufferMemSyncForCpu() - for y/u/v planes
    NvEGLImageFromFd()
    cuGraphicsEGLRegisterImage()
    cuGraphicsResourceGetMappedEglFrame()
    process_image(my_buffer) - runs processing in another thread
  }

  destroyNvBuffers(cache)
}

NvBufferMemMap + NvEGLImageFromFd + cuGraphicsEGLRegisterImage + cuGraphicsResourceGetMappedEglFrame take a non-negligible amount of time. Is it safe to perform those steps just once on buffer creation? Changing the code to:

function createNvBuffers()
{
  for (i=0; i<num_buffers; ++i)
  {
    NvBufferCreateEx()
    NvBufferMemMap() - for y/u/v planes
    NvEGLImageFromFd()
    cuGraphicsEGLRegisterImage()
    cuGraphicsResourceGetMappedEglFrame()
  }
}

function destroyNvBuffers()
{
  for (i=0; i<num_buffers; ++i)
  {
    cuGraphicsUnregisterResource()
    NvDestroyEGLImage()
    NvBufferMemUnMap() - for y/u/v planes
    NvBufferDestroy()
  }
}

function process_image(my_buffer)
{
  // do processing
  return_buffer_to_cache(my_buffer)
}

function main()
{
  cache = createNvBuffers()

  while (run)
  {
    acquire_frame()
    my_buffer = get_free_nvbuffer_from_cache()
    iNativeBuffer->copyToNvBuffer(my_buffer)
    NvBufferMemSyncForCpu()
    process_image(my_buffer) - runs processing in another thread
  }

  destroyNvBuffers(cache)
}

My processing only reads the image data (either in cuda or on the cpu). It doesn’t modify the data if that matters. Based on documentation this seems safe, but it wasn’t completely clear.

Hi,
It should work fine. You may notice to call NvBufferMemSyncForCpu() or NvBufferMemSyncForDevice() appropriately.

Thanks. Made the changes and everything seems to run correctly.