简体   繁体   English

如何将GDB与openMP和并行编程一起使用?

[英]How do I use GDB with openMP and parallel programming?

I was given a text file as input with 1,000,000 entries between 0 and 9. Each entry corresponds to a vote for a candidate made by a citizen of a country. 我获得了一个文本文件作为输入,在0到9之间有1,000,000个条目。每个条目对应一个国家公民对候选人的投票。 I was asked to read the file into a sequence and then determine who won the race. 我被要求将文件读入一个序列,然后确定谁赢得了比赛。 I was asked to do this with both parallel and non-parallel programming. 我被要求用并行和非并行编程来做这件事。 My parallel function (pCounter) does not work and I am trying to understand why, but I can't get GDB to run in openMP. 我的并行函数(pCounter)不起作用,我试图理解为什么,但我不能让GDB在openMP中运行。 It just "continues" over the function when I want it to "step" through. 当我希望它“逐步”通过时,它只是“继续”在功能上。 Does anyone know how I can use GDB for openMP and parallel programming? 有谁知道如何使用GDB进行openMP和并行编程?

EDIT When I try to step through a parallel function, I get the following result: 编辑当我尝试单步执行并行功能时,我得到以下结果:

[New Thread 0x7ffff619e700 (LWP 11152)]
[New Thread 0x7ffff599d700 (LWP 11153)]
[New Thread 0x7ffff519c700 (LWP 11154)]

Compiler line: g++ -g homework_5_1.cpp -o hw51 -std=c++11 -fopenmp 编译器行: g++ -g homework_5_1.cpp -o hw51 -std=c++11 -fopenmp

Code: 码:

void sCounter(myint *x, myint len){
  myint candidates[10];
  for(int i=0; i<10; ++i){
    candidates[i]=0;
  }
  for(int i=0; i<len; ++i){
    candidates[x[i]]+=1;
  }
  int winner=0;
  for(int i=0; i<10; ++i){
    if(i==winner)
      ++i;
    if(candidates[i]>candidates[winner]){
      winner=i;
      i=0;
    }
  }
  cout<<"The winner is candidate "<<winner<<endl;
  for(int i=0; i<10; i++){
    cout<<candidates[i]<<" ";
  }
  cout<<endl;
  int sum=0;
  for(int i=0; i<10; ++i){
    sum+=candidates[i];
  }
  cout<<sum<<endl;
}

void pCounter(myint *x, myint len){
  myint numberOfThreads;
  myint candidates[10];
  for(int i=0; i<10; ++i){
    candidates[i]=0;
  }
  int winner=0;
  #pragma omp parallel
  {
    #pragma omp for
    for(int i=0; i<len; ++i){
      candidates[x[i]]+=1;
    }
    #pragma omp for
    for(int i=0; i<10; ++i){
      if(i==winner)
        ++i;
      if(candidates[i]>candidates[winner]){
        winner=i;
        i=0;
      }
    }
  }
  cout<<"The winner is candidate "<<winner<<endl;
  for(int i=0; i<10; i++){
    cout<<candidates[i]<<" ";
  }
  cout<<endl;
  int sum=0;
  for(int i=0; i<10; ++i){
    sum+=candidates[i];
  }
  cout<<sum<<endl;
}

To step through the parallel part of your code use the breakpoint command to reach the start of the parallel region. 要单步执行代码的并行部分,请使用breakpoint命令到达并行区域的开头。

The syntax is as follows: 语法如下:

b <filename>:<linenumber>

When you've started your gdb session enter the command above, then enter "y" when prompted with the following question: 当您启动gdb会话时,请输入上面的命令,然后在出现以下问题时输入“y”:

No source file named <filename>.
Make breakpoint pending on future shared library load? (y or [n])

Now run your code by entering "r". 现在输入“r”运行代码。

Once your code reaches the breakpoint it should start with the following line: 一旦你的代码到达断点,它应该从以下行开始:

Thread 8 hit Breakpoint 1.

Now you can step through it like you did in the single-threaded region. 现在,您可以像在单线程区域中那样逐步完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM