ハイパースレッディングはIntelの実装simultaneous multithreading (SMT)です。現在のAMDプロセッサはSMTを実装していません(Bulldozerのマイクロアーキテクチャファミリには、AMDがクラスタベースのマルチスレッディングと呼ぶものがありますが、ZenのマイクロアーキテクチャはSMTを持つと考えられています)。 OpenMPにはSMTを検出するための組み込みサポートはありません。
ハイパースレッディングを検出する一般的な機能を使用するには、異なる世代のプロセッサをサポートし、プロセッサがAMDではなくIntelプロセッサであることを確認する必要があります。これにはライブラリを使用するのが最善です。
しかし、hereと記載されているように、多くの最新のIntelプロセッサで動作するOpenMPを使用して関数を作成できます。
次のコードは、現代のIntelプロセッサ(私が試したすべてのIntelプロセッサで動作しています)上の物理コアの数を数えます。これを動作させるには、スレッドをバインドする必要があります。 GCCでは export OMP_PROC_BIND=true
を使用できます。それ以外の場合はbind with code(これは私が行うことです)です。
このメソッドがVirtualBoxで信頼できるかどうかはわかりません。 VirtualBoxを4つのコア/ 8論理プロセッサーCPUに搭載し、Windowsをホストとし、Linuxの場合、VMのコア数を4に設定すると、このコードは2つのコアを報告し、/ proc/cpuinfoは2つのコアが実際に論理プロセッサであることを示します。
#include <stdio.h>
//cpuid function defined in instrset_detect.cpp by Agner Fog (2014 GNU General Public License)
//http://www.agner.org/optimize/vectorclass.zip
// Define interface to cpuid instruction.
// input: eax = functionnumber, ecx = 0
// output: eax = output[0], ebx = output[1], ecx = output[2], edx = output[3]
static inline void cpuid (int output[4], int functionnumber) {
#if defined (_MSC_VER) || defined (__INTEL_COMPILER) // Microsoft or Intel compiler, intrin.h included
__cpuidex(output, functionnumber, 0); // intrinsic function for CPUID
#elif defined(__GNUC__) || defined(__clang__) // use inline assembly, Gnu/AT&T syntax
int a, b, c, d;
__asm("cpuid" : "=a"(a),"=b"(b),"=c"(c),"=d"(d) : "a"(functionnumber),"c"(0) :);
output[0] = a;
output[1] = b;
output[2] = c;
output[3] = d;
#else // unknown platform. try inline assembly with masm/intel syntax
__asm {
mov eax, functionnumber
xor ecx, ecx
cpuid;
mov esi, output
mov [esi], eax
mov [esi+4], ebx
mov [esi+8], ecx
mov [esi+12], edx
}
#endif
}
int getNumCores(void) {
//Assuming an Intel processor with CPUID leaf 11
int cores = 0;
#pragma omp parallel reduction(+:cores)
{
int regs[4];
cpuid(regs,11);
if(!(regs[3]&1)) cores++;
}
return cores;
}
int main(void) {
printf("cores %d\n", getNumCores());
}
いいえ、これを完全に自動化する簡単な方法はありません。しかし、http://stackoverflow.com/q/2901694/620382 + 'omp_set_num_threads'があります。また、可能ならば、各システムのスレッド構成を手動で制御することをお勧めします。 – Zulan
なぜこの質問が下落したのですか? – cnst