Cycles: Use default CUDA context instead of creating a new one #117230

Merged
Stefan Werner merged 3 commits from Stefan_Werner/blender:cuda_default_ctx into main 2024-01-23 15:31:55 +01:00
Member

This allows for Cycles and OIDN to share the same context.

This allows for Cycles and OIDN to share the same context.
Stefan Werner added 1 commit 2024-01-17 14:33:19 +01:00
8451ecd1a3 Cycles: Use default CUDA context instead of creating a new one
This allow for Cycles and OIDN to share the same context.
Author
Member

Note: CU_CTX_MAP_HOST was deprecated in CUDA 11. Trying to set it on the default context throws an error.

Note: `CU_CTX_MAP_HOST` was deprecated in CUDA 11. Trying to set it on the default context throws an error.
Stefan Werner changed title from Cycles: Use default CUDA context instead of creating a new one to WIP: Cycles: Use default CUDA context instead of creating a new one 2024-01-18 15:32:59 +01:00
Stefan Werner requested review from Brecht Van Lommel 2024-01-18 15:33:20 +01:00
Stefan Werner requested review from Patrick Mours 2024-01-18 15:33:21 +01:00
Stefan Werner self-assigned this 2024-01-18 15:33:28 +01:00
Stefan Werner changed title from WIP: Cycles: Use default CUDA context instead of creating a new one to Cycles: Use default CUDA context instead of creating a new one 2024-01-18 15:33:59 +01:00
Brecht Van Lommel requested changes 2024-01-18 17:02:43 +01:00
Brecht Van Lommel left a comment
Owner

Thanks for working on this.

This fails for me with the following steps:

  • Open classroom.blend
  • Enable GPU rendering
  • Split 3D viewport in two
  • Enable rendered shading mode in both
  • Error: Failed to configure CUDA context (Primary context active) in the second viewport

I got it with the default cube too, but it's not as reliable to reproduce.

I guess we need some global lock around cuDevicePrimaryCtxRetain and cuCtxPopCurrent in the constructor, and in CUDAContextScope?

And then see how that affects performance for 3D viewport + material preview renders, and multiple 3D viewports.

Thanks for working on this. This fails for me with the following steps: * Open classroom.blend * Enable GPU rendering * Split 3D viewport in two * Enable rendered shading mode in both * Error: `Failed to configure CUDA context (Primary context active)` in the second viewport I got it with the default cube too, but it's not as reliable to reproduce. I guess we need some global lock around `cuDevicePrimaryCtxRetain` and `cuCtxPopCurrent` in the constructor, and in `CUDAContextScope`? And then see how that affects performance for 3D viewport + material preview renders, and multiple 3D viewports.
Stefan Werner added 2 commits 2024-01-23 09:35:03 +01:00
Author
Member

I went for an approach without locks:

  1. It first checks if the context is already active, and will only configure an inactive context
  2. In the rare case that two threads still try to configure the same context (race condition), it will catch the respective error and not fail.

This is under the assumption that cu* calls operating on the primary context are thread safe.

I went for an approach without locks: 1) It first checks if the context is already active, and will only configure an inactive context 2) In the rare case that two threads still try to configure the same context (race condition), it will catch the respective error and not fail. This is under the assumption that cu* calls operating on the primary context are thread safe.
Stefan Werner requested review from Brecht Van Lommel 2024-01-23 09:41:51 +01:00
Brecht Van Lommel requested changes 2024-01-23 14:15:35 +01:00
Brecht Van Lommel left a comment
Owner

Seems stable in testing now. Just one minor thing.

Seems stable in testing now. Just one minor thing.
@ -106,2 +117,3 @@
/* Create context. */
result = cuCtxCreate(&cuContext, ctx_flags, cuDevice);
result = cuDevicePrimaryCtxRetain(&cuContext, cuDevice);

Technically speaking there is still a race condition here, in the unlikely event two CUDA devices are created at the same time.

So I would still suggest to add this:

{
  static thread_mutex primary_ctx_init_mutex;
  thread_scoped_lock lock(primary_ctx_init_mutex);

  int active = 0;
  ...
  result = cuDevicePrimaryCtxRetain(&cuContext, cuDevice);
}
Technically speaking there is still a race condition here, in the unlikely event two CUDA devices are created at the same time. So I would still suggest to add this: ``` { static thread_mutex primary_ctx_init_mutex; thread_scoped_lock lock(primary_ctx_init_mutex); int active = 0; ... result = cuDevicePrimaryCtxRetain(&cuContext, cuDevice); } ```
Member

cuDevicePrimaryCtxRetain and cuDevicePrimaryCtxGetState are thread-safe, per device (the driver has a mutex per device which is held during the call). So should still be safe even if two different CUDA devices were created and it would get the primary context for each at the same time here (since the primary context is also per device), without needing another lock.

`cuDevicePrimaryCtxRetain` and `cuDevicePrimaryCtxGetState` are thread-safe, per device (the driver has a mutex per device which is held during the call). So should still be safe even if two different CUDA devices were created and it would get the primary context for each at the same time here (since the primary context is also per device), without needing another lock.

I wasn't clear, I meant a race condition when two Cycles CUDADevice instances are created at the same time, for the same GPU.

Imagine:

THREAD A: cuDevicePrimaryCtxGetState (active = 0)
THREAD B: cuDevicePrimaryCtxGetState (active = 0)
THREAD A: cuDevicePrimaryCtxSetFlags
THREAD A: cuDevicePrimaryCtxRetain
THREAD B: cuDevicePrimaryCtxSetFlags -> Failed to configure CUDA context (Primary context active)
I wasn't clear, I meant a race condition when two Cycles `CUDADevice` instances are created at the same time, for the same GPU. Imagine: ``` THREAD A: cuDevicePrimaryCtxGetState (active = 0) THREAD B: cuDevicePrimaryCtxGetState (active = 0) THREAD A: cuDevicePrimaryCtxSetFlags THREAD A: cuDevicePrimaryCtxRetain THREAD B: cuDevicePrimaryCtxSetFlags -> Failed to configure CUDA context (Primary context active) ```
Member

But shouldn't that be handled by the second condition in if (result != CUDA_SUCCESS && result != CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE)?

But shouldn't that be handled by the second condition in `if (result != CUDA_SUCCESS && result != CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE)`?
Brecht Van Lommel approved these changes 2024-01-23 15:15:57 +01:00
Brecht Van Lommel left a comment
Owner

Ah yes, I missed that.

Ah yes, I missed that.
Stefan Werner merged commit 4f58cffb4e into main 2024-01-23 15:31:55 +01:00
Stefan Werner deleted branch cuda_default_ctx 2024-01-23 15:31:57 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#117230
No description provided.