You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2026/06/17 07:09:03 UTC

Re: [PR] [VL] Add concurrency control to GpuLock [gluten]

Copilot commented on code in PR #12090:
URL: https://github.com/apache/gluten/pull/12090#discussion_r3426234226


##########
backends-velox/src/main/scala/org/apache/gluten/config/VeloxConfig.scala:
##########
@@ -758,6 +758,12 @@ object VeloxConfig extends ConfigRegistry {
       .booleanConf
       .createWithDefault(true)
 
+  val CUDF_CONCURRENT_GPU_TASKS =
+    buildStaticConf("spark.gluten.sql.columnar.backend.velox.cudf.concurrentGpuTasks")
+      .doc("The number of concurrent GPU tasks to run.")
+      .intConf
+      .createWithDefault(1)

Review Comment:
   `CUDF_CONCURRENT_GPU_TASKS` is parsed as an int with no validation, but the native backend throws if the value is 0. A misconfiguration would therefore fail executor init at runtime; add a config-side check to enforce a positive value (and optionally clarify it in the doc string).



##########
cpp/velox/cudf/GpuLock.cc:
##########
@@ -37,51 +39,63 @@ GpuLockState& getGpuLockState() {
 }
 } // namespace
 
+void configureGpuTaskConcurrency(size_t maxConcurrentTasks) {
+  if (maxConcurrentTasks <= 0) {
+    throw std::invalid_argument("configureGpuTaskConcurrency() requires maxConcurrentTasks > 0");
+  }
+
+  std::lock_guard<std::mutex> lock(getGpuLockState().gGpuMutex);
+  getGpuLockState().maxConcurrentTasks = maxConcurrentTasks;
+  getGpuLockState().gGpuCv.notify_all();
+}
+
 void lockGpu() {
-  std::thread::id tid = std::this_thread::get_id();
-  std::unique_lock<std::mutex> lock(getGpuLockState().gGpuMutex);
-  if (getGpuLockState().gGpuOwner == tid) {
-    // Reentrant call from the same thread — do nothing
+  if (gThreadGpuHolder) {
     return;
   }
 
-  // Wait until the GPU lock becomes available
-  getGpuLockState().gGpuCv.wait(lock, [] { return !getGpuLockState().gGpuOwner.has_value(); });
+  std::unique_lock<std::mutex> lock(getGpuLockState().gGpuMutex);
+  getGpuLockState().gGpuCv.wait(
+      lock, [] { return getGpuLockState().activeTasks < getGpuLockState().maxConcurrentTasks; });
 
-  // Acquire ownership
-  getGpuLockState().gGpuOwner = tid;
+  ++getGpuLockState().activeTasks;
+  gThreadGpuHolder = true;
 }
 
 bool tryLockGpu() {
-  std::thread::id tid = std::this_thread::get_id();
-  std::unique_lock<std::mutex> lock(getGpuLockState().gGpuMutex);
-  if (getGpuLockState().gGpuOwner == tid) {
+  if (gThreadGpuHolder) {
     return true;
   }
-  if (getGpuLockState().gGpuOwner.has_value()) {
-    return false;
+
+  std::unique_lock<std::mutex> lock(getGpuLockState().gGpuMutex);
+
+  // Check if a permit is available without blocking
+  if (getGpuLockState().activeTasks < getGpuLockState().maxConcurrentTasks) {
+    ++getGpuLockState().activeTasks;
+    gThreadGpuHolder = true;
+    return true;
   }
-  getGpuLockState().gGpuOwner = tid;
-  return true;
+
+  return false;
 }
 
 void unlockGpu() {
-  std::thread::id tid = std::this_thread::get_id();
-  std::unique_lock<std::mutex> lock(getGpuLockState().gGpuMutex);
-  if (!getGpuLockState().gGpuOwner.has_value()) {
-    LOG(INFO) << "unlockGpu() called when no thread holds the lock!";
+  if (!gThreadGpuHolder) {
+    LOG(INFO) << "unlockGpu() called by non-owner thread!" << std::endl;
     return;
   }

Review Comment:
   `unlockGpu()` is documented as releasing a permit held by the current thread, but in the non-owner case it only logs and returns. This silently hides incorrect lock/unlock pairing and can leave the system stuck with permits never released; throwing (as the previous implementation did for cross-thread unlock) makes the contract enforceable.



##########
cpp/velox/tests/GpuLockTest.cc:
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "cudf/GpuLock.h"
+
+#include <atomic>
+#include <chrono>
+#include <future>
+#include <stdexcept>
+#include <thread>
+
+#include <gtest/gtest.h>
+
+namespace gluten {
+
+TEST(GpuLockTest, configureRejectsZeroConcurrency) {
+  configureGpuTaskConcurrency(1);
+  EXPECT_THROW(configureGpuTaskConcurrency(0), std::invalid_argument);
+}
+
+TEST(GpuLockTest, sameThreadLockIsReentrant) {
+  configureGpuTaskConcurrency(1);
+  lockGpu();
+  EXPECT_TRUE(tryLockGpu());
+
+  unlockGpu();
+  EXPECT_TRUE(tryLockGpu());
+
+  unlockGpu();
+}
+
+TEST(GpuLockTest, tryLockFailsWhilePermitHeldByAnotherThread) {
+  configureGpuTaskConcurrency(1);
+  lockGpu();
+
+  auto acquired = std::async(std::launch::async, [] { return tryLockGpu(); });
+  EXPECT_FALSE(acquired.get());
+
+  unlockGpu();
+}
+
+TEST(GpuLockTest, lockBlocksUntilPermitIsReleased) {
+  configureGpuTaskConcurrency(1);
+  lockGpu();
+
+  std::promise<void> workerStarted;
+  auto workerStartedFuture = workerStarted.get_future();
+  std::atomic<bool> workerAcquired{false};
+
+  std::thread worker([&] {
+    workerStarted.set_value();
+    EXPECT_FALSE(tryLockGpu());
+    lockGpu();
+    workerAcquired.store(true);
+    unlockGpu();
+  });
+
+  workerStartedFuture.wait();
+  std::this_thread::sleep_for(std::chrono::milliseconds(50));
+  EXPECT_FALSE(workerAcquired.load());

Review Comment:
   This test relies on `sleep_for(50ms)` to assert the worker is blocked, which can be flaky under CI load. Prefer deterministic synchronization (e.g., a second promise) to confirm the worker has reached the blocking path without using timing assumptions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org