无法设置处理器关联

时间:2014-02-19 05:29:22

标签: multithreading multicore affinity pthread-join vtune

我正在尝试在8核群集上实现此代码。它有2个插座,每个插座有4个核心。我正在尝试使用pthread_attr_setaffinity_np函数创建8个线程并设置关联。但是当我看到我在VTunes中的表现时,它向我显示正在创建3969个奇数线程。我不明白为什么以及如何!最重要的是,我的表现与没有设置亲和力时(OS线程调度)完全相同。有人可以帮我调试这个问题吗?我的代码运行得很好但我无法控制线程!提前谢谢。

-------------------------------------- CODE -------- -----------------------------------

const int num_thrd=8;
bool RCTAlgorithmBackprojection(RabbitCtGlobalData* r)
{
float        O_L = r->O_L;
float        R_L = r->R_L;
double*      A_n = r->A_n;
float*       I_n = r->I_n;
float*       f_L = r->f_L;*/

cpu_set_t cpu[num_thrd];    
pthread_t thread[num_thrd];
pthread_attr_t attr[num_thrd];
for(int i =0; i< num_thrd; i++)
{
    threadCopy[i].L = r->L;
    threadCopy[i].O_L = r->O_L;
    threadCopy[i].R_L = r->R_L;
    threadCopy[i].A_n = r->A_n;
    threadCopy[i].I_n = r->I_n;
    threadCopy[i].f_L = r->f_L;
    threadCopy[i].slice= i;
    threadCopy[i].S_x = r->S_x;
    threadCopy[i].S_y = r->S_y;

    pthread_attr_init(&attr[i]);
    CPU_ZERO(&cpu[i]);
    CPU_SET(i, &cpu[i]);
    pthread_attr_setaffinity_np(&attr[i], CPU_SETSIZE, &cpu[i]);

    int rc=pthread_create(&thread[i], &attr[i], backProject, (void*)&threadCopy[i]);

    if (rc!=0)
    {
        cout<<"Can't create thread\n"<<endl;
        return -1;
    }
    //  sleep(1);
}
for (int i = 0; i < num_thrd; i++) {
    pthread_join(thread[i], NULL);
}   
//s_rcgd = r;       
return true;
}


void* backProject (void* parm)
{
copyStruct* s = (copyStruct*)parm;   // retrive the slice info
unsigned int L   = s->L;
float        O_L = s->O_L;
float        R_L = s->R_L;
double*      A_n = s->A_n;
float*       I_n = s->I_n;
float*       f_L = s->f_L;
int slice1 = s->slice; 
//cout<<"The size of volume is L= "<<L<<endl;
int from = (slice1 * L) / num_thrd; // note that this 'slicing' works fine
int to = ((slice1+1) * L) / num_thrd; // even if SIZE is not divisible by num_thrd
//cout<<"computing slice  " << slice1<< "  from row " << from<< "  to " << to-1<<endl;
for (unsigned int k=from; k<to; k++)
{
    double z = O_L + (double)k * R_L;
    for (unsigned int j=0; j<L; j++)
    {
        double y = O_L + (double)j * R_L;
        for (unsigned int i=0; i<L; i++)
        {
            double x = O_L + (double)i * R_L;

            double w_n =  A_n[2] * x + A_n[5] * y + A_n[8] * z + A_n[11];
            double u_n = (A_n[0] * x + A_n[3] * y + A_n[6] * z + A_n[9] ) / w_n;
            double v_n = (A_n[1] * x + A_n[4] * y + A_n[7] * z + A_n[10]) / w_n;

            f_L[k * L * L + j * L + i] += (float)(1.0 / (w_n * w_n) * p_hat_n(u_n, v_n)); 
        }
    }
}
//cout<<" finished slice "<<slice1<<endl;
return NULL;
}

1 个答案:

答案 0 :(得分:1)

好吧,所以我发现原因是因为CPU_SETSIZE我在pthread_attr_setaffinity_np中使用num_thrd作为参数。我用CPU_SETSIZE替换了它。显然#define __USE_GNU将在{{1}}内声明,但我的文件中没有包含这些内容。!!对不起,如果我打扰你们所有试图再次调试这个的人再次感谢!