Skip to content

Commit

Permalink
ENH: Use get_schedaffin to determine number of threads
Browse files Browse the repository at this point in the history
  • Loading branch information
blowekamp committed Nov 12, 2024
1 parent 8e97a5d commit ff55ce8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Modules/Core/Common/CMake/itkCheckHasSchedGetAffinity.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed 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
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* 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 <sched.h>

int
main(void)
{

cpu_set_t mask;

sched_getaffinity(0, sizeof(cpu_set_t), &mask);
CPU_COUNT(&mask);

return 0;
}
4 changes: 4 additions & 0 deletions Modules/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ try_compile(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/itkCheckHasFenvtStructMember.cxx
COMPILE_DEFINITIONS -DITK_CHECK_FENV_T_CW)

try_compile(
ITK_HAS_SCHED_GETAFFINITY ${ITK_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/CMake/itkCheckHasSchedGetAffinity.cxx)

#-----------------------------------------------------------------------------
# Make default visibility as an option for generated export header

Expand Down
2 changes: 2 additions & 0 deletions Modules/Core/Common/src/itkConfigure.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@
# endif
#endif

#cmakedefine ITK_HAS_SCHED_GETAFFINITY

#endif //itkConfigure_h
18 changes: 15 additions & 3 deletions Modules/Core/Common/src/itkMultiThreaderBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include <cctype>
#include <utility> // For move.

#if defined(ITK_HAS_SCHED_GETAFFINITY)
# include <sched.h>
#endif

#if defined(ITK_USE_TBB)
# include "itkTBBMultiThreader.h"
#endif
Expand Down Expand Up @@ -334,15 +338,23 @@ MultiThreaderBase::GetGlobalDefaultNumberOfThreads()
ThreadIdType
MultiThreaderBase::GetGlobalDefaultNumberOfThreadsByPlatform()
{
#if defined(ITK_HAS_SCHED_GETAFFINITY)
cpu_set_t mask;
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) != -1)
{
return static_cast<ThreadIdType>(CPU_COUNT(&mask));
}
#endif

#if defined(ITK_LEGACY_REMOVE)
return std::thread::hardware_concurrency();
#endif

#if defined(ITK_USE_PTHREADS)
ThreadIdType num;

// Default the number of threads to be the number of available
// processors if we are using pthreads()
// Default the number of threads to be the number of available
// processors if we are using pthreads()
# ifdef _SC_NPROCESSORS_ONLN
num = static_cast<ThreadIdType>(sysconf(_SC_NPROCESSORS_ONLN));
# elif defined(_SC_NPROC_ONLN)
Expand Down Expand Up @@ -371,7 +383,7 @@ MultiThreaderBase::GetGlobalDefaultNumberOfThreadsByPlatform()
#else
return 1;
#endif
}
} // namespace itk

MultiThreaderBase::Pointer
MultiThreaderBase::New()
Expand Down

0 comments on commit ff55ce8

Please sign in to comment.