#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp parallel
cout<<"Hello from thread "<<omp_get_thread_num()<<", nthreads"
<< omp_get_num_threads()<<endl;
return 0;
}
#pragma omp parallel:是openmp的directives(指示字?),會產生threads來執行,此段程式碼.
omp_get_thread_num():此函式用來回傳thread編號
omp_get_num_threads():用來回傳thread總數
如果只需要一個thread執行程式的話可以用#pragma omp single
#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp single
cout<<"Hello from thread "<<omp_get_thread_num()<<", nthreads"
<< omp_get_num_threads()<<endl;
return 0;
}
沒有留言:
張貼留言