最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Cuda-OpenGL interops thread safety - Stack Overflow

programmeradmin9浏览0评论
/* This executes on a second thread */
static void do_progressive_render_gpu() {
 for(sample s = 0 ; s < SOMEVALUE ; s++){
  updateMeshBuffersPtrs(); //Calls cudaGraphicsResourceGetMappedPointer() down the call stack to get those VBOs arrays. 
  bake_scene_gpu(); // Launches a cuda kernel.
 } 
}

/* This executes on the main thread with the OpenGL context we use.  
 * The caller of this function has already registered the VBOs with cudaGraphicsGLRegisterBuffer() 
 */
static void start_baking() {
 std::function<void()> callback; 
 registerGraphicsResources(); // Calls cudaGraphicsResource
 haltRenderers(); // This can be a solution to prevent OpenGL from accessing those resources, but I would prefer to avoid it if possible. 
 callback = []() { do_progressive_render_gpu(); };
 std::thread worker_baking_thread(callback);
  //...
  //...
  // Cleanup and unmap the resources on the main thread later
}

This code launches a cuda thread that executes cudaGraphicsResourceGetMappedPointer()

The main thread already registered and mapped the cuda resources. PS: the access on the gpu buffers is read-only .

Is it thread safe ?

/* This executes on a second thread */
static void do_progressive_render_gpu() {
 for(sample s = 0 ; s < SOMEVALUE ; s++){
  updateMeshBuffersPtrs(); //Calls cudaGraphicsResourceGetMappedPointer() down the call stack to get those VBOs arrays. 
  bake_scene_gpu(); // Launches a cuda kernel.
 } 
}

/* This executes on the main thread with the OpenGL context we use.  
 * The caller of this function has already registered the VBOs with cudaGraphicsGLRegisterBuffer() 
 */
static void start_baking() {
 std::function<void()> callback; 
 registerGraphicsResources(); // Calls cudaGraphicsResource
 haltRenderers(); // This can be a solution to prevent OpenGL from accessing those resources, but I would prefer to avoid it if possible. 
 callback = []() { do_progressive_render_gpu(); };
 std::thread worker_baking_thread(callback);
  //...
  //...
  // Cleanup and unmap the resources on the main thread later
}

This code launches a cuda thread that executes cudaGraphicsResourceGetMappedPointer()

The main thread already registered and mapped the cuda resources. PS: the access on the gpu buffers is read-only .

Is it thread safe ?

Share Improve this question edited Jan 25 at 7:08 eyllanesc 244k19 gold badges200 silver badges279 bronze badges asked Jan 16 at 17:13 Amine BensalemAmine Bensalem 464 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2 +350

Thread-safe generally means that there are no unexpected race conditions even when many threads are executed in parallel so if access on the GPU buffers is read-only and only 1 thread that calls cudaGraphicsResourceGetMappedPointer, it should be thread safe as CUDA resources is always mapped and registered in start_baking in the main thread before cudaGraphicsResourceGetMappedPointer being called in child thread, hence there is no race condition.

If you worry about cleanup and unmap the resources on the main thread modifies the resources before do_progressive_render_gpu finishes, you could use .join() to wait until the thread finishes before the cleanup step

/* This executes on the main thread with the OpenGL context we use.  
 * The caller of this function has already registered the VBOs with cudaGraphicsGLRegisterBuffer() 
 */
static void start_baking() {
 std::function<void()> callback; 
 registerGraphicsResources(); // Calls cudaGraphicsResource
 haltRenderers(); // This can be a solution to prevent OpenGL from accessing those resources, but I would prefer to avoid it if possible. 
 callback = []() { do_progressive_render_gpu(); };
 std::thread worker_baking_thread(callback);
  //...
  //...
 worker_baking_thread.join();
  // Cleanup and unmap the resources on the main thread later
}
发布评论

评论列表(0)

  1. 暂无评论