網頁

BloggerAds 廣告

熱門文章

搜尋此網誌

顯示具有 OpenMp 標籤的文章。 顯示所有文章
顯示具有 OpenMp 標籤的文章。 顯示所有文章

2011年7月25日 星期一

OpenMP-shared for loop



#include <omp.h>
#include <iostream>
using namespace std;
int main() {
int n=10;
#pragma omp parallel shared(n)
{
#pragma omp for
for (int i=0; i<n; i++)
cout <<"Thread "<<omp_get_thread_num()<<" executes loop iteration "<< i<<endl;
}
return 0;
}



references:
OpenMP Official Web sites
Using OpenMP: Portable Shared Memory Parallel Programming

OpenMP-Parallel Constructor

Parallel constructor(平行結構):可以告訴compile,以下的程式碼要用parallel 的方式執行,
並且程式設計師也可以運用此結構,來個別定義thread的行為.
i.e.

#include <omp.h>
#include <iostream>
using namespace std;
int main() {
#pragma omp parallel
{
cout <<"The parallel region is executed by thread "<<omp_get_thread_num();
if (omp_get_thread_num() == 1) {
cout <<" Thread "<<omp_get_thread_num()<<" does things differently\n";
}
}
return 0;
}


附帶一提parallel region有兩種狀態:active以及inactive.
所謂的active是指有多個thread在執行運算,若只有一個thread在做運算的話就是inactive的狀態.


references:
OpenMP Official Web sites
Using OpenMP: Portable Shared Memory Parallel Programming

OpenMP conditional compilation

Conditional compilation is used to prevent unwanted results created from a non openmp compliant compiler .

Following is its format:

#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_thread_num() 0
#endif
//some code here
int TID = omp_get_thread_num();


references:
OpenMP Official Web sites
Using OpenMP: Portable Shared Memory Parallel Programming

2011年7月21日 星期四

OpenMP Introduction(OpenMP 介紹)

1.OpenMP
  • 採取 fork-join(?分叉-結合?)的方式來做平行運算.

  • 因為對於c/c++/fortan語言的擴充性, openmp可以用來發展平行化的應用程式(Parallel Application Development)

  • It is a programming interface

  • shared-memory parallel programs
  • 2011年7月20日 星期三

    OpenMp 環境配置 for eclipse

    我是使用eclipse for c++ developer,一開始一直會出現1各undefined GOMP_parallel_end 的錯誤後來我發現只要到專案的property裡去設定就解決了.以下是圖檔,因為每個版本的eclipse不盡相同,請各位根據圖找到對應的欄位去改
    要改的兩個項目是GCC c++ compiler 還有MingGW c++ linker



    OpenMP-HelloWorld

    open mp是可以把程式平行化執行的c/c++ library.open mp很常被用在遊戲的動畫處理方面.另一方面因為現在的電腦都是雙核心的cpu,如果應用程式沒有寫成平行化的方式,無法完全發揮雙核心cpu的威力,不免俗的我的第一個open mp 程式當然是c++版本的hello world.網路上很多例子都是用c的方式來呈現,因為我不熟悉c,所以小改成c++的方式來實作.若compile顯示ignore #pragma的警告訊息, 那麼要在complie command line加上"-fopenmp"參數


    #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;
    }