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!
|