local-host 2018-11-02 00:45 采纳率: 100%
浏览 572

尽管使用临界区{ OpenMP } ,数据仍处于竞争状态

I'm trying to adapt this pascal triangle program to a parallel program using openMp. I used the for directive to parallelize the printPas function for loop, and put the conditional statements inside of the critical section so only one thread can print at a time, but it seems like I'm still getting a data race because my output is really inconsistent.

The code:

#include <stdio.h>

#ifndef N
#define N 2
#endif
unsigned int t1[2*N+1], t2[2*N+1];
unsigned int *e=t1, *r=t2;
int l = 0;

//the problem is here in this function
void printPas() {
  #pragma omp parallel for private(l)
  for (l=0; l<2*N+1; l++) {
    #pragma omp critical
    if (e[l]==0)
      printf("      ");
    else
      printf("%6u", e[l]);
  }
  printf("\n");

}

void update() {
  r[0] = e[1];
#pragma omp parallel for
  for (int u=1; u<2*N; u++)
    r[u] = e[u-1]+e[u+1];
  r[2*N] = e[2*N-1];
  unsigned int *tmp = e; e=r; r=tmp; 
}

int main() {
  e[N] = 1;
  for (int i=0; i<N; i++) {
    printPas();
    update();
  }
  printPas();
}

转载于:https://stackoverflow.com/questions/53111292/data-race-despite-using-critical-section-openmp

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2018-11-03 15:58
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services