使用 OpenMP 在两个内核上设置线程关联

Set thread affinity on two cores using OpenMP

我正在使用 C 程序,在 Windows7 上使用 gcc 4.9.2 编译,使用 OpenMP 4.0。 我的电脑是双核的,有四个线程。 我想使用线程亲和力传播并使用放在不同内核上的 2 个线程。所以当我从 DOS 设置环境变量时:

  • set OMP_NUM_THREADS=2
  • set OMP_PROC_BIND=spread
  • set OMP_PLACES="cores"

我得到,变量 OMP_DISPLAY_ENV=true,这个:

libgomp: Invalid value for environment variable OMP_PLACES

OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = ''
  OMP_STACKSIZE = '12872703'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
OPENMP DISPLAY ENVIRONMENT END

好像输入的不对,所以改成:

  • set OMP_PLACES="cores"
  libgomp: Affinity not supported on this configuration 
  OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = ''
  OMP_STACKSIZE = '3107827'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
  OPENMP DISPLAY ENVIRONMENT END

这是结果:不支持关联。我得到了相同的结果,即使是:

set OMP_PLACES="{0},{2},{1},{3}"

你们知道如何解决这个问题吗?

我在 Linux 上尝试使用 gcc 4.9.3 编译的简单 hello world 代码,使用您建议的环境变量:

~/tmp$ OMP_DISPLAY_ENV=true OMP_NUM_THREADS=2 OMP_PROC_BIND=spread OMP_PLACES=cores ./a.out 

OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = '{0:2},{2:2}'
  OMP_STACKSIZE = '140736864318339'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
OPENMP DISPLAY ENVIRONMENT END
Hello from thread 0 / 2
Hello from thread 1 / 2

所以基本上,它对我有用,我相信你尝试的是合法的。但是,您收到的消息 Affinity not supported on this configuration 让我相信 affinity 可能根本无法在您的 Windows 7 机器上使用。我没有这样的环境来测试,但 OpenMP 标准说:

The determination of whether the affinity request can be fulfilled is implementation defined. If the affinity request cannot be fulfilled, then the affinity of threads in the team is implementation defined.

我想那是我们...