Crash in program using OpenMP, x64 only

折月煮酒 提交于 2019-12-10 22:23:49

问题


The program below crashes when I build it in Release x64 (all other configurations run fine).

Am I doing it wrong or is it an OpenMP issue? Well-grounded workarounds are highly appreciated.

To reproduce build a project (console application) with the code below. Build with /openmp and /GL and (/O1 or /O2 or /Ox) options in Release x64 configuration. That is OpenMP support and C++ optimization must be turned on. The resulting program should (should not) crash.

#include <omp.h>
#include <vector>

class EmptyClass
  {
  public:
    EmptyClass() {}
  };

class SuperEdge
  {
  public:
    SuperEdge() {mp_points[0] = NULL; mp_points[1] = NULL;}

  private:
    const int* mp_points[2];
  };

EmptyClass CreateEmptyClass(SuperEdge s)
  {
  return EmptyClass();
  }

int main(int argc, wchar_t* argv[], wchar_t* envp[])
  {
  std::vector<int> v;
  long count = 1000000;

  SuperEdge edge;
  #pragma omp parallel for 
  for(long i = 0; i < count; ++i)
    {
    EmptyClass p = CreateEmptyClass(edge);
    #pragma omp critical
       {
       v.push_back(0);
       }
    }

  return 0;
  }

回答1:


I think it is a bug. Looking at the ASM output with /O2 on the push_back call has been optimized away and there are just a couple of reserve calls and what looks like direct accesses instead. The reserve calls however don't appear to be in the critical section and you end up with Heap corruption. Doing a release x64 with /openmp /GL /Od you will see that there is a call to push_back in the asm, and it is between the _vcomp_enter_critsect calls, and doesn't crash. I'd report it to MS. (tested with VS 2010)



来源:https://stackoverflow.com/questions/3812230/crash-in-program-using-openmp-x64-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!