-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-45860: [C++] Respect CPU affinity in cpu_count and ThreadPool default capacity #46034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #pragma once | ||
|
|
||
| #include <thread> | ||
|
|
||
| #ifdef __linux__ | ||
| #include <sched.h> | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| namespace arrow { | ||
| namespace internal { | ||
|
|
||
| // Returns the number of CPUs the current process is allowed to use. | ||
| // Falls back to std::thread::hardware_concurrency() if affinity is not available. | ||
| inline int GetAffinityCpuCount() { | ||
| #ifdef __linux__ | ||
| cpu_set_t mask; | ||
| if (sched_getaffinity(0, sizeof(mask), &mask) == 0) { | ||
| return CPU_COUNT(&mask); | ||
| } | ||
| #endif | ||
| return std::thread::hardware_concurrency(); | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace arrow | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| #include <thread> | ||
|
|
||
| #include "arrow/result.h" | ||
| #include "arrow/util/affinity.h" | ||
| #include "arrow/util/io_util.h" | ||
| #include "arrow/util/logging_internal.h" | ||
| #include "arrow/util/string.h" | ||
|
|
@@ -513,7 +514,7 @@ struct CpuInfo::Impl { | |
| OsRetrieveCacheSize(&cache_sizes); | ||
| OsRetrieveCpuInfo(&hardware_flags, &vendor, &model_name); | ||
| original_hardware_flags = hardware_flags; | ||
| num_cores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1); | ||
| num_cores = std::max(GetAffinityCpuCount(), 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about not changing this and adding a new method (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Just to double-ceck: do we want
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me. |
||
|
|
||
| // parse user simd level | ||
| auto maybe_env_var = GetEnvVar("ARROW_USER_SIMD_LEVEL"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <gtest/gtest.h> | ||
| #include "arrow/util/cpu_info.h" | ||
|
|
||
| #ifdef __linux__ | ||
| #include <sched.h> | ||
| #endif | ||
|
|
||
| TEST(CpuInfoTest, CpuAffinity) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks this test will never fail? But I don't have better suggestion :) |
||
| #ifdef __linux__ | ||
| auto cpu_info = arrow::internal::CpuInfo::GetInstance(); | ||
| int affinity_cores = cpu_info->num_cores(); | ||
|
|
||
| cpu_set_t mask; | ||
| ASSERT_EQ(sched_getaffinity(0, sizeof(mask), &mask), 0); | ||
| int expected = CPU_COUNT(&mask); | ||
|
|
||
| ASSERT_EQ(affinity_cores, expected); | ||
| #else | ||
| GTEST_SKIP() << "CpuInfo affinity check only applies on Linux."; | ||
| #endif | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |||||
| #include "arrow/testing/executor_util.h" | ||||||
| #include "arrow/testing/future_util.h" | ||||||
| #include "arrow/testing/gtest_util.h" | ||||||
| #include "arrow/util/affinity.h" | ||||||
| #include "arrow/util/config.h" | ||||||
| #include "arrow/util/io_util.h" | ||||||
| #include "arrow/util/logging.h" | ||||||
|
|
@@ -1039,35 +1040,63 @@ TEST(TestGlobalThreadPool, Capacity) { | |||||
| // Exercise default capacity heuristic | ||||||
| ASSERT_OK(DelEnvVar("OMP_NUM_THREADS")); | ||||||
| ASSERT_OK(DelEnvVar("OMP_THREAD_LIMIT")); | ||||||
|
|
||||||
| #ifdef __linux__ | ||||||
| int expected_capacity = arrow::internal::GetAffinityCpuCount(); | ||||||
| #else | ||||||
| int hw_capacity = std::thread::hardware_concurrency(); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity); | ||||||
| int expected_capacity = hw_capacity; | ||||||
| #endif | ||||||
|
|
||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), expected_capacity); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this?
Suggested change
|
||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "13")); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), 13); | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "7,5,13")); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), 7); | ||||||
| ASSERT_OK(DelEnvVar("OMP_NUM_THREADS")); | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "1")); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), 1); | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "999")); | ||||||
| if (hw_capacity <= 999) { | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity); | ||||||
| } | ||||||
| #ifdef __linux__ | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), std::min(999, arrow::internal::GetAffinityCpuCount())); | ||||||
| #else | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), std::min(999, hw_capacity)); | ||||||
| #endif | ||||||
|
Comment on lines
+1064
to
+1068
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we simplify it? Leave only line 1065, and remove |
||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "6,5,13")); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), 6); | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "2")); | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), 2); | ||||||
|
|
||||||
| // Invalid env values | ||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "0")); | ||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "0")); | ||||||
| #ifdef __linux__ | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), arrow::internal::GetAffinityCpuCount()); | ||||||
| #else | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity); | ||||||
| #endif | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "zzz")); | ||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "x")); | ||||||
| #ifdef __linux__ | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), arrow::internal::GetAffinityCpuCount()); | ||||||
| #else | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity); | ||||||
| #endif | ||||||
|
|
||||||
| ASSERT_OK(SetEnvVar("OMP_THREAD_LIMIT", "-1")); | ||||||
| ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "99999999999999999999999999")); | ||||||
| #ifdef __linux__ | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), arrow::internal::GetAffinityCpuCount()); | ||||||
| #else | ||||||
| ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity); | ||||||
| #endif | ||||||
|
|
||||||
| ASSERT_OK(DelEnvVar("OMP_NUM_THREADS")); | ||||||
| ASSERT_OK(DelEnvVar("OMP_THREAD_LIMIT")); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving this to
cpp/src/arrow/util/cpu_info.cc?