Page 1 of 1

Stockfish-1.7.0 Hyper-threading Detection

Posted: Fri Apr 09, 2010 2:35 pm
by zullil
Having now found a way to toggle hyper-threading off and on, I can confirm that the code below does not work on my dual quad-core Intel 5520 Mac Pro.

The printf statement produces 16 8 8 in both cases.

Code: Select all

/// HT_enabled() returns true if hyper-threading is enabled on current machine

static bool HT_enabled() {

  char CPUString[0x20];
  int CPUInfo[4] = {-1};
  int nIds, nLogicalCPU, nCores;

  // Detect CPU producer
  __cpuid(CPUInfo, 0);
  nIds = CPUInfo[0];

  memset(CPUString, 0, sizeof(CPUString));
  memcpy(&CPUString[0], &CPUInfo[1], sizeof(int));
  memcpy(&CPUString[4], &CPUInfo[3], sizeof(int));
  memcpy(&CPUString[8], &CPUInfo[2], sizeof(int));

  // Not an Intel CPU or CPUID.4 not supported
  if &#40;strcmp&#40;CPUString, "GenuineIntel") || nIds < 4&#41;
      return false;

  // Detect if HT Technology is supported
  __cpuid&#40;CPUInfo, 1&#41;;
  if (!(&#40;CPUInfo&#91;3&#93; >> 28&#41; & 1&#41;)
      return false;

  nLogicalCPU = &#40;CPUInfo&#91;1&#93; >> 16&#41; & 0xFF;

  // Detect number of cores
  __cpuid&#40;CPUInfo, 4&#41;;
  nCores = 1 + (&#40;CPUInfo&#91;0&#93; >> 26&#41; & 0x3F&#41;;
  printf&#40;"%d   %d    %d\n", nLogicalCPU, nCores, builtin_cpu_count&#40;));
  return nLogicalCPU > nCores;
&#125;

Re: Stockfish-1.7.0 Hyper-threading Detection

Posted: Fri Apr 09, 2010 7:16 pm
by mcostalba
zullil wrote:Having now found a way to toggle hyper-threading off and on, I can confirm that the code below does not work on my dual quad-core Intel 5520 Mac Pro.

The printf statement produces 16 8 8 in both cases.
Hi Louis,

thanks for testing this.

We will probably revert HT detection entirely and fall back on 1.6.3 behaviour that is not perfect but at least gives no surprises and people seems already used to it.

Making HT detection to work in all cases seems very complex: you need to add a lot of obscure code very different from what you are supposed to find in a chess engine, more similar to OS kernel code :-(


I also take the chance to highlight that HT detection has _nothing_ to do with playing strength, crashes or other strange behavior. The only net effect is that your engine will run using less cores of what actually could run, but you can easily workaround this simply setting "threads" UCI parameters to the number of actual cores your machine has. You can do this with any GUI or even from command line.

Re: Stockfish-1.7.0 Hyper-threading Detection

Posted: Fri Apr 09, 2010 9:53 pm
by BrandonSi
I haven't looked at this myself, but you should be able to get a physical core count vs logical core count by deconstructing the APIC ID for each logical cpu (or x2APIC for x64).. LOGICAL_ID vs CORE_ID. Was that the route taken in Stockfish?

I agree though, not much benefit in figuring this out, just let the user do the work.

Re: Stockfish-1.7.0 Hyper-threading Detection

Posted: Sat Apr 10, 2010 12:36 am
by mcostalba
BrandonSi wrote:I haven't looked at this myself
Please, if interested take a look at:

http://software.intel.com/en-us/article ... umeration/

If you (or someone else) is able to come up with a working patch for SF out of that document then you are my new code idol :-)

Re: Stockfish-1.7.0 Hyper-threading Detection

Posted: Sat Apr 10, 2010 2:47 am
by Ralph Stoesser
mcostalba wrote:
BrandonSi wrote:I haven't looked at this myself
Please, if interested take a look at:

http://software.intel.com/en-us/article ... umeration/

If you (or someone else) is able to come up with a working patch for SF out of that document then you are my new code idol :-)
At the Intel site is also source code available.

The desired function:

Code: Select all

/*
 * GetSysProcessorCoreCount
 *
 * Returns count of processor cores in the system that were enumerated by this app
 *
 * Arguments&#58;     None
 *
 * Return&#58;        Number of physical processors or 0 if number can not be calculated
 */
unsigned  GetSysProcessorCoreCount&#40;)
&#123;
	if (!glbl_ptr->EnumeratedCoreCount&#41;
		InitCpuTopology&#40;);

	if &#40;glbl_ptr->error&#41; return 0;

	return glbl_ptr->EnumeratedCoreCount;
&#125;
Only a few thousend lines of utility code behind the scene (InitCpuTopology()) to include ... :lol: