私は。。をしようとしています:
プロセッサのピン留めと同時に16コピーを実行します(コアごとに2コピー)
プロセッサのピン留め(コアあたり2コピー)と同時に8コピーを実行し、特定の機能(機能1が終了した場合など)の後でプロセッサコアを最も遠いコアに反転します。
私が直面している問題は、最も遠いプロセッサをどのように選択するかです。
sched_getaffinityとsched_setaffinityの使用を提案した友人もいますが、良い例は見つかりません。
sched_setaffinityを使用して現在のプロセスをコア7で実行するには、次のようにします。
cpu_set_t my_set; /* Define your cpu_set bit mask. */
CPU_ZERO(&my_set); /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(7, &my_set); /* set the bit that represents core 7. */
sched_setaffinity(0, sizeof(cpu_set_t), &my_set); /* Set affinity of tihs process to */
/* the defined mask, i.e. only 7. */
詳細については、http: //linux.die.net/man/2/sched_setaffinityおよびhttp://www.gnu.org/software/libc/manual/html_node/CPU-Affinity.htmlを参照してください。
最小限の実行可能な例
この例では、アフィニティを取得して変更し、 で有効になっているかどうかを確認しsched_getcpu()
ます。
main.c
#define _GNU_SOURCE
#include <assert.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void print_affinity() {
cpu_set_t mask;
long nproc, i;
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
perror("sched_getaffinity");
assert(false);
}
nproc = sysconf(_SC_NPROCESSORS_ONLN);
printf("sched_getaffinity = ");
for (i = 0; i < nproc; i++) {
printf("%d ", CPU_ISSET(i, &mask));
}
printf("\n");
}
int main(void) {
cpu_set_t mask;
print_affinity();
printf("sched_getcpu = %d\n", sched_getcpu());
CPU_ZERO(&mask);
CPU_SET(0, &mask);
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
perror("sched_setaffinity");
assert(false);
}
print_affinity();
/* TODO is it guaranteed to have taken effect already? Always worked on my tests. */
printf("sched_getcpu = %d\n", sched_getcpu());
return EXIT_SUCCESS;
}
コンパイルして実行します。
gcc -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out
出力例:
sched_getaffinity = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
sched_getcpu = 9
sched_getaffinity = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sched_getcpu = 0
つまり、次のことを意味します。
このプログラムを次のように実行するのも楽しいですtaskset
:
taskset -c 1,3 ./a.out
次の形式の出力が得られます。
sched_getaffinity = 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
sched_getcpu = 2
sched_getaffinity = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sched_getcpu = 0
したがって、最初から親和性が制限されていることがわかります。
これは、アフィニティが子プロセスによって継承されるため機能します。これtaskset
はフォークです: フォークされた子プロセスによる CPU アフィニティの継承を防ぐにはどうすればよいですか?
nproc
sched_getaffinity
に示されているように、デフォルトで尊重します: How to find the number of CPUs using python
パイソン:os.sched_getaffinity
とos.sched_setaffinity
Ubuntu 16.04 でテスト済み。
CPU_SETSIZE を sched_[set|get] アフィニティの cpusetsize パラメータとして使用しないでください。名前は誤解を招きますが、これは間違っています。マクロ CPU_SETSIZE は (man 3 cpu_set を引用) 「cpu_set_t に格納できる最大 CPU 数より 1 大きい値」です。使用する必要があります
sched_setaffinity(0, sizeof(cpu_set_t), &my_set);
代わりは。