Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Openmp。如何检索运行线程的核心id_C_Openmp - Fatal编程技术网

C Openmp。如何检索运行线程的核心id

C Openmp。如何检索运行线程的核心id,c,openmp,C,Openmp,我已将KMP_AFFINITY设置为分散,但执行时间增加了很多 这就是为什么我认为OpenMP只在一个内核上产生线程 所以我需要一些东西 返回线程当前正在使用的核心 这是我在for循环之前使用的pragma: int procs = omp_get_num_procs(); #pragma omp parallel for num_threads(procs)\ shared (c, u, v, w, k, j, i, nx, ny) \ reduction(+: a, b, c, d, e,

我已将KMP_AFFINITY设置为分散,但执行时间增加了很多

这就是为什么我认为OpenMP只在一个内核上产生线程

所以我需要一些东西 返回线程当前正在使用的核心

这是我在for循环之前使用的pragma:

int procs = omp_get_num_procs();
#pragma omp parallel for num_threads(procs)\
shared (c, u, v, w, k, j, i, nx, ny) \
reduction(+: a, b, c, d, e, f, g, h, i)
以下是我所做的出口:

export OMP_NUM_THREADS=5
export KMP_AFFINITY=verbose,scatter 
如果有帮助的话,我也会粘贴详细内容:

OMP: Info #149: KMP_AFFINITY: Affinity capable, using global cpuid instr info
OMP: Info #154: KMP_AFFINITY: Initial OS proc set respected: {0,1,2,3,4,5,6,7}
OMP: Info #156: KMP_AFFINITY: 8 available OS procs
OMP: Info #157: KMP_AFFINITY: Uniform topology
OMP: Info #159: KMP_AFFINITY: 2 packages x 4 cores/pkg x 1 threads/core (8 total cores)
OMP: Info #160: KMP_AFFINITY: OS proc to physical thread map ([] => level not in map):
OMP: Info #168: KMP_AFFINITY: OS proc 0 maps to package 0 core 0 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 4 maps to package 0 core 1 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 2 maps to package 0 core 2 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 6 maps to package 0 core 3 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 1 maps to package 1 core 0 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 5 maps to package 1 core 1 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 3 maps to package 1 core 2 [thread 0]
OMP: Info #168: KMP_AFFINITY: OS proc 7 maps to package 1 core 3 [thread 0]
OMP: Info #147: KMP_AFFINITY: Internal thread 0 bound to OS proc set {0}
OMP: Info #147: KMP_AFFINITY: Internal thread 1 bound to OS proc set {1}
OMP: Info #147: KMP_AFFINITY: Internal thread 2 bound to OS proc set {4}
OMP: Info #147: KMP_AFFINITY: Internal thread 3 bound to OS proc set {5}
OMP: Info #147: KMP_AFFINITY: Internal thread 4 bound to OS proc set {2}
OMP: Info #147: KMP_AFFINITY: Internal thread 5 bound to OS proc set {3}
OMP: Info #147: KMP_AFFINITY: Internal thread 6 bound to OS proc set {6}
OMP: Info #147: KMP_AFFINITY: Internal thread 7 bound to OS proc set {7}

提前谢谢

如果您在linux上,您可以使用函数
sched\u getcpu()
。这里有一个链接来解释它的工作原理及其声明:

希望这能对@user3018144有所帮助,
sched_getcpu(3)
是可以用来获取CPU号的

考虑以下代码:

#define _GNU_SOURCE // sched_getcpu(3) is glibc-specific (see the man page)

#include <stdio.h>
#include <sched.h>
#include <omp.h>

int main() {
#pragma omp parallel
    {
        int thread_num = omp_get_thread_num();
        int cpu_num = sched_getcpu();
        printf("Thread %3d is running on CPU %3d\n", thread_num, cpu_num);
    }

    return 0;
}
这是具有关联性的输出:

$> OMP_NUM_THREADS=4 ./a.out | sort
Thread   0 is running on CPU   2
Thread   1 is running on CPU   0
Thread   2 is running on CPU   3
Thread   3 is running on CPU   1
$> GOMP_CPU_AFFINITY='0,1,2,3' OMP_NUM_THREADS=4 ./a.out | sort
Thread   0 is running on CPU   0
Thread   1 is running on CPU   1
Thread   2 is running on CPU   2
Thread   3 is running on CPU   3

默认情况下,在并行区域之前声明的变量是共享的。您没有任何
private
子句,因此您认为私有的许多变量可能实际上是共享的。数据争用和错误共享会极大地降低程序的性能,并使您认为所有线程都在一个内核上运行。您显示的详细列表似乎与您声称要进行的运行不一致,因为它显示了八个OpenMP线程,它们(您可以看到)都绑定到一个单独的逻辑CPU,而你声称有五个线程在运行。(所以它肯定是在使用所有的硬件)。你没有说基本情况是什么,只是散射比…慢一些。。。在您的机器中,如果存在大量数据共享,那么一个套接字中的四个线程很可能比两个套接字中的四个线程更快。如果您不信任运行时的输出,并且假设您在Linux上,那么您可以简单地运行xosview,并在运行代码时查看每个逻辑CPU上的负载。这是真的。我使用omp_get_num_procs()而不是omp_get_max_threads(),这就是为什么它总是生成8个线程(因为它有8个内核)。我已经解决了这个问题,但我仍然面临同样的结果:(您好。我已经尝试使用此函数。但它报告了未定义的函数sched_getcpu。我想这是因为我使用的是英特尔编译器。@CrashLaker,这是因为您可能忘记在代码顶部的某个位置添加
#include
。我没有忘记包含。我猜此库不存在。但如果这样的话c编译器不应该警告我吗?我用英特尔编译器试过一个测试程序,它运行得很好,所以你可以检查你的代码,你应该在某个地方有错误。或者试着在你的文件的顶部写:
#define(定义)GNU(源代码)
这也适用于我,但我得到一个警告:
隐式函数声明'sched_getcpu';你是说'sched_getparam'?
来自我使用的每个编译器。@RobertSawko,在代码中使用
#define(定义)GNU(源代码)
或使用
-D_GNU(源代码)编译。原因是
sched_getcpu(3)
是glibc特有的。我已经用这些信息更新了答案。