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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?