/* 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 badges1 Answer
Reset to default 2 +350Thread-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
}