From 53c49589f311bd3c9e3ea8f62b3fa8fe8e5d2c8c Mon Sep 17 00:00:00 2001
From: Patrick Mours <pmours@nvidia.com>
Date: Mon, 15 Apr 2024 12:17:32 +0200
Subject: [PATCH] Fix #119959: Enabling "Distribute memory between devices" for
 Cycles results in error

With the switch to using the primary CUDA context it became possible
for peer access between CUDA devices to already have been enabled for
that context, either by a previous Cycles session or third-party library,
thus causing the call to `cuCtxEnablePeerAccess` to return
`CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED`. This is not a failure
state however, so just needs to be handled like a success return value.

Pull Request: https://projects.blender.org/blender/blender/pulls/120255
---
 intern/cycles/device/cuda/device_impl.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/intern/cycles/device/cuda/device_impl.cpp b/intern/cycles/device/cuda/device_impl.cpp
index c8fb2255264..83460b16802 100644
--- a/intern/cycles/device/cuda/device_impl.cpp
+++ b/intern/cycles/device/cuda/device_impl.cpp
@@ -187,7 +187,7 @@ bool CUDADevice::check_peer_access(Device *peer_device)
   {
     const CUDAContextScope scope(this);
     CUresult result = cuCtxEnablePeerAccess(peer_device_cuda->cuContext, 0);
-    if (result != CUDA_SUCCESS) {
+    if (result != CUDA_SUCCESS && result != CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED) {
       set_error(string_printf("Failed to enable peer access on CUDA context (%s)",
                               cuewErrorString(result)));
       return false;
@@ -196,7 +196,7 @@ bool CUDADevice::check_peer_access(Device *peer_device)
   {
     const CUDAContextScope scope(peer_device_cuda);
     CUresult result = cuCtxEnablePeerAccess(cuContext, 0);
-    if (result != CUDA_SUCCESS) {
+    if (result != CUDA_SUCCESS && result != CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED) {
       set_error(string_printf("Failed to enable peer access on CUDA context (%s)",
                               cuewErrorString(result)));
       return false;
-- 
GitLab