6

Linux で 1 ソケット、4 コアのサーバーで sched_setaffinity テストを実行しました。次の /proc/cpuinfo に CPU 情報が表示されます。

processor       : 0
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 1
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 2
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

processor       : 3
model name      : Intel(R) Core(TM)2 Quad CPU    Q8400  @ 2.66GHz
cache size      : 2048 KB
physical id     : 0
siblings        : 4
cpu cores       : 4

私は簡単なテストアプリケーションを持っています:

struct foo {
    int x;
    int y;
}  ;

//globar var
volatile struct foo fvar ;

pid_t gettid( void )
{
    return syscall( __NR_gettid );
}

void *test_func0(void *arg)
{
    int proc_num = (int)(long)arg;
    cpu_set_t set;

    CPU_ZERO( &set );
    CPU_SET( proc_num, &set );
    printf("proc_num=(%d)\n",proc_num) ;
    if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &set ))
    {
        perror( "sched_setaffinity" );
        return NULL;
    }


    int i=0;
    for(i=0;i<1000000000;++i){
        __sync_fetch_and_add(&fvar.x,1);
    }
    return NULL;
} //test_func0

コンパイル: gcc testsync.c -D_GNU_SOURCE -lpthread -o testsync.exe 以下はテスト結果です:

2 threads running test_func0 in core 0,1  take 35 secs ;
2 threads running test_func0 in core 0,2  take 55 secs ;
2 threads running test_func0 in core 0,3  take 55 secs ;
2 threads running test_func0 in core 1,2  take 55 secs ;
2 threads running test_func0 in core 1,3  take 55 secs ;
2 threads running test_func0 in core 2,3  take 35 secs ;

コア (0,1) またはコア (2,3) で実行されている 2 つのスレッドが他のスレッドではるかに高速になるのはなぜでしょうか? core(1,1) 、 core(2,2)、core(3,3) のように、同じコアで 2 つのスレッドを実行すると、28 秒かかり、なぜこれが起こるのか混乱しますか?

4

1 に答える 1

7

コア0と1はL2キャッシュを共有し、コア2と3も共有します。キャッシュを共有する2つのコアで実行すると、共有変数がL2キャッシュにとどまり、処理が高速化されます。

これは、L2がコアごとにある今日のIntelプロセッサには当てはまりません。ただし、使用しているCPUでは、これがどのように機能するかを示しています(実際には、2つのデュアルコアCPUを接着して作成されたクアッドコアCPUです)。

于 2013-01-18T05:49:56.513 に答える