0

我在 OpenMP 中实现了一个简单的传播障碍,它存在潜在的死锁,我无法弄清楚原因。

线程共享一个共同的数据结构标志,看起来像这样:

  // each other.
  typedef struct Flags{
    int myflags[2][MAX_THREADS];
    int *partnerFlags[2][MAX_THREADS];
  }Flags;
    #pragma omp parallel shared(processors, rounds, totalTime)
    {
        int parity = 0;
        int sense = 1;
        int threadID = omp_get_thread_num();
        int i;
        Flags *localFlags = &processors[threadID];
        
        for(i=0; i<MAX_BARRIERS; i++) {
            double startTime = omp_get_wtime();
            disseminationBarrier(localFlags, &sense, &parity, &rounds);
            double endTime = omp_get_wtime();

            double elapsedTime = (endTime - startTime);

            printf("Time spent at the barrier(in ms) %d by thread %d is %lf\n", i, threadID, (elapsedTime / 1000.0));            

            #pragma omp critical
            {
                totalTime = totalTime + elapsedTime;
            }
        }
    }

传播功能的代码看起来像这样

 void disseminationBarrier(Flags *localFlags, int *sense, int *parity, int *rounds){
    int i;
    for(i=0; i<*rounds; i++) {
        *localFlags->partnerFlags[*parity][i] = *sense;
        while(localFlags->myflags[*parity][i] != *sense);  -> Point of probable deadlock
    }

    if(*parity) {
        *sense = !*sense;
    }
    
    *parity = 1- *parity;
}
4

0 回答 0