From 8659619fbd73361a129e02df19d24aa5054649cb Mon Sep 17 00:00:00 2001 From: Ilya Isaev Date: Fri, 20 Dec 2024 10:41:59 +0100 Subject: [PATCH] Fallback to GetThreadGroupAffinity if GetNativeSystemInfo reports wrongly (#1587) Signed-off-by: Isaev, Ilya --- src/tbb/misc_ex.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/tbb/misc_ex.cpp b/src/tbb/misc_ex.cpp index 13b7b04fb1..03b33b464f 100644 --- a/src/tbb/misc_ex.cpp +++ b/src/tbb/misc_ex.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2005-2023 Intel Corporation + Copyright (c) 2005-2024 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -297,11 +297,21 @@ static void initialize_hardware_concurrency_info () { if ( pam & m ) ++nproc; } - __TBB_ASSERT( nproc <= (int)si.dwNumberOfProcessors, nullptr); + int number_of_processors = (int)si.dwNumberOfProcessors; + if (nproc > number_of_processors && TBB_GetThreadGroupAffinity) { + // Sometimes on systems with multiple processor groups GetNativeSystemInfo + // reports mask and processor count from the parent process + TBB_GROUP_AFFINITY ga; + if (TBB_GetThreadGroupAffinity(GetCurrentThread(), &ga)) { + number_of_processors = (int)TBB_GetActiveProcessorCount(ga.Group); + } + } + + __TBB_ASSERT( nproc <= number_of_processors, nullptr); // By default setting up a number of processors for one processor group theProcessorGroups[0].numProcs = theProcessorGroups[0].numProcsRunningTotal = nproc; // Setting up processor groups in case the process does not restrict affinity mask and more than one processor group is present - if ( nproc == (int)si.dwNumberOfProcessors && TBB_GetActiveProcessorCount ) { + if ( nproc == number_of_processors && TBB_GetActiveProcessorCount ) { // The process does not have restricting affinity mask and multiple processor groups are possible ProcessorGroupInfo::NumGroups = (int)TBB_GetActiveProcessorGroupCount(); __TBB_ASSERT( ProcessorGroupInfo::NumGroups <= MaxProcessorGroups, nullptr);