由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Linux版 - OpenMP 求救。。。 (转载)
相关主题
如何确保多线程程序在 multicore server 用所有 core为什么我的python code总是显示D
Process and Threadin what case you still want to use process instead of thread in linux ?
用了nohup怎么查运行到什么程度了?when should use multiprocess not multithread: embedded multicore linux
is it possible to combine two threads into one in gmail?a dummy OS question (转载)
CPU确实可以留后门 (转载)openMP or boost::thread (pthread) for multithreading ?
【我很土】发现htop原来这么好用cluster上提交50+的interactive job算不算奇葩?
请教一个linux下面的多线程semaphore的问题。 (转载)请教一个基础C++问题 (转载)
gmail速度很慢读fork.c的时候发现一句脏话
相关话题的讨论汇总
话题: omp话题: threads话题: thread话题: num话题: endl
进入Linux版参与讨论
1 (共1页)
b*****l
发帖数: 9499
1
【 以下文字转载自 Thoughts 讨论区 】
发信人: bigsail (河马·旋木), 信区: Thoughts
标 题: OpenMP 求救。。。
发信站: BBS 未名空间站 (Sat Apr 30 02:18:47 2011, 美东)
在学 OpenMP,第一步就不通:设多线程失败。。。
TestOMP.cpp 的 code 很简单:开 5 个线程,每个介绍一下自己,就完事了.
#include
#include
using namespace std;
main () {
omp_set_num_threads(5);
cout << "Fork! " << endl;
#pragma omp parallel
{
// Obtain and print thread id
cout<< "Hello World from thread = " << omp_get_thread_num()
<< " of " << omp_get_num_threads() << endl;
// Only master thread does this
if (omp_get_thread_num() == 0)
cout << "Master thread: number of threads = " <<
omp_get_num_threads() << endl;
} // All threads join master thread and terminate
cout << "Joint! " << endl;
}
结果咋整都是一个线程:
$ make
g++ -c -o TestOMP.o TestOMP.cpp
g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
$ ./TestOMP
Fork!
Hello World from thread = 0 of 1
Master thread: number of threads = 1
Joint!
num_threads(5) 也试过了,不成。觉得俺哪里理解错了。。。
Q*T
发帖数: 263
2
Enable OpenMP support when linking:
g++ -fopenmp -c -o TestOMP.o TestOMP.cpp

【在 b*****l 的大作中提到】
: 【 以下文字转载自 Thoughts 讨论区 】
: 发信人: bigsail (河马·旋木), 信区: Thoughts
: 标 题: OpenMP 求救。。。
: 发信站: BBS 未名空间站 (Sat Apr 30 02:18:47 2011, 美东)
: 在学 OpenMP,第一步就不通:设多线程失败。。。
: TestOMP.cpp 的 code 很简单:开 5 个线程,每个介绍一下自己,就完事了.
: #include
: #include
: using namespace std;
: main () {

b*****l
发帖数: 9499
3
试了,照旧。。。

【在 Q*T 的大作中提到】
: Enable OpenMP support when linking:
: g++ -fopenmp -c -o TestOMP.o TestOMP.cpp

Q*T
发帖数: 263
4
Use -fopenmp in both compiling and linking
g++ -fopenmp -c TestOMP.cpp
g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
Or simply do
g++ -fopenmp -o TestOMP.cpp TestOMP.cpp
If these don't work, try another machine and see if it has anything to do
with the environment.

【在 b*****l 的大作中提到】
: 试了,照旧。。。
b*****l
发帖数: 9499
5
是啊,还是老样子。能麻烦你在你的机子上跑一下不?
$ g++ -fopenmp -c TestOMP.cpp
$ g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
$ ./TestOMP
omp_get_dynamic: 1
omp_get_max_threads: 48
Fork!
Hello World from thread = 0 of 1
Master thread: number of threads = 1
Joint!
$ less TestOMP.cpp
#include
#include
using namespace std;
main () {
int nthreads;
omp_set_dynamic(1);
cout << "omp_get_dynamic: " << omp_get_dynamic() << endl;
cout << "omp_get_max_threads: " << omp_get_max_threads() << endl;
omp_set_num_threads(5);
/* Fork a team of threads with each thread having a private tid variable */
cout << "Fork! " << endl;
#pragma omp parallel
{
// Obtain and print thread id
cout<< "Hello World from thread = " << omp_get_thread_num()
<< " of " << omp_get_num_threads() << endl;
// Only master thread does this
if (omp_get_thread_num() == 0)
cout << "Master thread: number of threads = " << omp_get_num_threads()
<< endl;
} // All threads join master thread and terminate
cout << "Joint! " << endl;
}
TestOMP.cpp (END)

【在 Q*T 的大作中提到】
: Use -fopenmp in both compiling and linking
: g++ -fopenmp -c TestOMP.cpp
: g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
: Or simply do
: g++ -fopenmp -o TestOMP.cpp TestOMP.cpp
: If these don't work, try another machine and see if it has anything to do
: with the environment.

Q*T
发帖数: 263
6
Tested on two computers. No problem at all.
What machine and operating system are you using?

【在 b*****l 的大作中提到】
: 是啊,还是老样子。能麻烦你在你的机子上跑一下不?
: $ g++ -fopenmp -c TestOMP.cpp
: $ g++ -o TestOMP TestOMP.o -I. -g -O -fopenmp -lm
: $ ./TestOMP
: omp_get_dynamic: 1
: omp_get_max_threads: 48
: Fork!
: Hello World from thread = 0 of 1
: Master thread: number of threads = 1
: Joint!

1 (共1页)
进入Linux版参与讨论
相关主题
读fork.c的时候发现一句脏话CPU确实可以留后门 (转载)
你们谁搞过。【我很土】发现htop原来这么好用
问道OS的面试题。请教一个linux下面的多线程semaphore的问题。 (转载)
问一个进程调度的问题。gmail速度很慢
如何确保多线程程序在 multicore server 用所有 core为什么我的python code总是显示D
Process and Threadin what case you still want to use process instead of thread in linux ?
用了nohup怎么查运行到什么程度了?when should use multiprocess not multithread: embedded multicore linux
is it possible to combine two threads into one in gmail?a dummy OS question (转载)
相关话题的讨论汇总
话题: omp话题: threads话题: thread话题: num话题: endl