将 GDB 与 OpenMP 一起使用 [英] Using GDB with OpenMP
问题描述
使用 GDB,我似乎无法在 OpenMP 线程中打印共享变量的值.例如,使用以下程序:
Using GDB I can't seem to print the value of shared variables within OpenMP threads. For example, using the following program:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int priv, tid, pub = 100;
#pragma omp parallel private(priv, tid) num_threads(2)
{
tid = omp_get_thread_num();
priv = tid * 10;
#pragma omp sections
{
#pragma omp section
{
printf("SECTION 0: tid=%d, priv=%d, pub=%d
", tid, priv, pub);
}
#pragma omp section
{
printf("SECTION 1: tid=%d, priv=%d, pub=%d
", tid, priv, pub);
}
}
}
return EXIT_SUCCESS;
}
在 GDB 中,如果我在第 15 行中断(第 0 节的 printf),并尝试打印 "pub" 的值,我会得到 «No symbol "pub" in current context.»留言:
In GDB, if I break at line 15 (the printf of section 0), and I try to print the value of "pub", I get the «No symbol "pub" in current context.» message:
Breakpoint 1, main._omp_fn.0 () at omp_simplesec.c:15
15 printf("SECTION 0: tid=%d, priv=%d, pub=%d
", tid, priv, pub);
(gdb) print pub
No symbol "pub" in current context.
我正在使用 GCC 编译,并尝试了不同的调试标志 (-g3 -ggdb3 -gstabs3 -gstabs+3),但没有成功.我还尝试使用 -O0 禁用所有优化,但同样没有成功.但是,我可以使用 -gstabs+ 标志查看私有变量的值.
I am compiling with GCC, and tried different debugging flags (-g3 -ggdb3 -gstabs3 -gstabs+3), without success. I also tried disabling all optimizations with -O0, again with no success. However, I can see the value of private variables by using the -gstabs+ flag.
提前致谢.
推荐答案
当我运行你的代码时,我得到了类似的结果.如果您查看回溯,它会告诉您您处于与 GCC 如何实现 OpenMP 相关的 OpenMP 环境中.
When I run your code I get similar results. If you look at the backtrace, it tells you that you are inside an OpenMP environment that is related to how GCC implements OpenMP.
(gdb) backtrace
#0 main._omp_fn.0 () at tmp.c:15
#1 0x000000000040082e in main (argc=1, argv=0x7fffffffe6c8) at tmp.c:7
您可以通过以下方式获取 pub 的值:
You can get a value of pub by:
(gdb) up
(gdb) print pub
$1 = 100
但这只会让您在并行区域之前获得 pub 的值.您应该查看 Hristo Iliev 的答案,以获得更详细和更好的情况描述.
but this only gets you the value of pub before the parallel region. You should check the answer by Hristo Iliev for a much more detailed and better description of the situation.
这篇关于将 GDB 与 OpenMP 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!