您的位置:首页 > 运维架构

gdb 调试openmp多线程程序 示例介绍

2015-11-15 15:09 495 查看
2014-05-21 11:46 427人阅读 评论(0) 收藏 举报

[html] view
plaincopy





首先一个简单程序源代码:

[html] view
plaincopy





[html] view
plaincopy





1 #include <stdio.h>

2 #include <stdlib.h>

3 #include <omp.h>

4 int main(int argc, char * argv[]){

5 int i, k,m,n;

6 printf("i=%d,threads_num=%d\n",i, omp_get_num_threads());

7 printf("i=%d,thread_id=%d\n",i, omp_get_thread_num());

8 #pragma omp parallel for

9 for(i=0;i<4;i++)

10 {

11 k=10;

12 m=10;

13 n=k+m;

14 printf("i=%d,n=%d,thread_id=%d\n",i,n, omp_get_thread_num());

15 if(k>1)

16 printf("OK\n");

17 }

18 return 1;

19 }

编译成 test_omp

step 1:gdb test_omp

step 2:break 15

step 3:run

[html] view
plaincopy





(gdb) r

Starting program: /home/xqc/gmptest/test_omp

[Thread debugging using libthread_db enabled]

i=0,threads_num=1

i=0,thread_id=0

[New Thread 0x7ffff740f700 (LWP 17108)]

[New Thread 0x7ffff6c0e700 (LWP 17109)]

[New Thread 0x7ffff640d700 (LWP 17110)]

Breakpoint 1, main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15

15 k=10;

(gdb)

[html] view
plaincopy





(gdb) info threads

4 Thread 0x7ffff640d700 (LWP 17110) main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15

3 Thread 0x7ffff6c0e700 (LWP 17109) main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15

2 Thread 0x7ffff740f700 (LWP 17108) main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15

* 1 Thread 0x7ffff7fdf780 (LWP 17105) main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15

(gdb)

step4: thread 2

[html] view
plaincopy





(gdb) thread 2

[Switching to thread 2 (Thread 0x7ffff740f700 (LWP 17108))]#0 main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0)

at test_omp.c:15

15 k=10;

(gdb)

至此,就可以对thread 2号线程进行debug了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: