numactl 和 move_pages 不匹配
numactl and move_pages mismatch
我开发了一个简单的程序来测试一个页面在哪个 NUMA 节点中,基于 this question。
问题是将我的程序结果与 Xeon E5-2698 v4(两个 NUMA 节点)上的 numactl -H
进行比较显示不同的输出。 numactl -H
显示(裁剪):
available: 2 nodes (0-1)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
node 1 cpus: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
因此,例如,numactl
表示 cpu 20 在节点 1。我有以下代码:
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <numaif.h>
#include <omp.h>
int numa_node(void *ptr) {
int status;
int ret_code;
if((ret_code = move_pages(0, 1, &ptr, NULL, &status, 0)) == -1) {
perror("move_pages");
return -1;
}
return status;
}
int main(int argc, char* argv[]) {
int pgsize = getpagesize();
printf("NUMA test(pgsize=%d)\n",pgsize);
#pragma omp parallel firstprivate(pgsize)
{
if(omp_get_thread_num() == 20) {
char *m = aligned_alloc(pgsize, pgsize);
m[0] = 'a';
if(mlock(m, 10) == -1) {
perror("mlock");
}
else {
int node = numa_node(m);
printf("thread %d: node %d\n",20,node);
}
}
}
}
我正在使用 aligned_alloc
尝试只分配一个对齐的页面,这样
当这个线程"touches"这个页面时,它会被映射到这个线程所在的NUMA节点(first touch policy)。然后我用mlock
,你可以在this question中查看。我想我正在使用 first touch,因为我没有修改任何与此相关的东西,但是,我不知道如何检查它,以确保。
我正在用 icc -fopenmp -lnuma
和 运行 KMP_AFFINITY=granularity=fine,compact
,OMP_NUM_THREADS=80
和
numactl -m 0,1 ./numa
。我正在使用这种亲和力,因为我认为它执行与 numactl 看到系统相同的分配。这输出:
NUMA test(pgsize=4096)
thread 20: node 0
所以,这个程序说线程 20 在节点 0 但 numactl
说线程 20 在节点 1。为什么?我期待在两者上看到相同的输出。
请求的关联设置 compact
会将连续线程放置到同一核心上的相邻硬件线程(超线程)。 OS cpus 的编号在后面对附加的硬件线程进行编号,因此 cpu 0 和 40 在同一个核心上。映射如下:
tid -> cpu
0 -> 0
1 -> 40
2 -> 1
3 -> 41
...
20 -> 10
您可以通过将 ,verbose
添加到 KMP_AFFINITY
来看到这一点。如果你想要直接映射,你可以使用 GOMP_CPU_AFFINITY=0-79
而不是 KMP_AFFINITY
设置。这应该可以在正确的 NUMA 节点上获取内存。
我开发了一个简单的程序来测试一个页面在哪个 NUMA 节点中,基于 this question。
问题是将我的程序结果与 Xeon E5-2698 v4(两个 NUMA 节点)上的 numactl -H
进行比较显示不同的输出。 numactl -H
显示(裁剪):
available: 2 nodes (0-1)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
node 1 cpus: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
因此,例如,numactl
表示 cpu 20 在节点 1。我有以下代码:
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <numaif.h>
#include <omp.h>
int numa_node(void *ptr) {
int status;
int ret_code;
if((ret_code = move_pages(0, 1, &ptr, NULL, &status, 0)) == -1) {
perror("move_pages");
return -1;
}
return status;
}
int main(int argc, char* argv[]) {
int pgsize = getpagesize();
printf("NUMA test(pgsize=%d)\n",pgsize);
#pragma omp parallel firstprivate(pgsize)
{
if(omp_get_thread_num() == 20) {
char *m = aligned_alloc(pgsize, pgsize);
m[0] = 'a';
if(mlock(m, 10) == -1) {
perror("mlock");
}
else {
int node = numa_node(m);
printf("thread %d: node %d\n",20,node);
}
}
}
}
我正在使用 aligned_alloc
尝试只分配一个对齐的页面,这样
当这个线程"touches"这个页面时,它会被映射到这个线程所在的NUMA节点(first touch policy)。然后我用mlock
,你可以在this question中查看。我想我正在使用 first touch,因为我没有修改任何与此相关的东西,但是,我不知道如何检查它,以确保。
我正在用 icc -fopenmp -lnuma
和 运行 KMP_AFFINITY=granularity=fine,compact
,OMP_NUM_THREADS=80
和
numactl -m 0,1 ./numa
。我正在使用这种亲和力,因为我认为它执行与 numactl 看到系统相同的分配。这输出:
NUMA test(pgsize=4096)
thread 20: node 0
所以,这个程序说线程 20 在节点 0 但 numactl
说线程 20 在节点 1。为什么?我期待在两者上看到相同的输出。
请求的关联设置 compact
会将连续线程放置到同一核心上的相邻硬件线程(超线程)。 OS cpus 的编号在后面对附加的硬件线程进行编号,因此 cpu 0 和 40 在同一个核心上。映射如下:
tid -> cpu
0 -> 0
1 -> 40
2 -> 1
3 -> 41
...
20 -> 10
您可以通过将 ,verbose
添加到 KMP_AFFINITY
来看到这一点。如果你想要直接映射,你可以使用 GOMP_CPU_AFFINITY=0-79
而不是 KMP_AFFINITY
设置。这应该可以在正确的 NUMA 节点上获取内存。