Version 8 (modified by jazz, 14 years ago) (diff) |
---|
實作二
多核心程式設計 : OpenMP
Multi-Cores Programming : OpenMP
Multi-Cores Programming : OpenMP
- GCC 4.2 以上版本就有支援 OpenMP 了....用 "gcc -fopenmp" 就可以編譯
Example 1 : HelloWorld
- 首先從 OpenMP Exercise 下載 omp_hello.c
~$ wget https://computing.llnl.gov/tutorials/openMP/samples/C/omp_hello.c
- 確認目前 gcc 版本,須大於 4.2 以後的版本
~$ gcc -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.5 (Debian 4.4.5-8)
- 使用 GCC 編譯 omp_hello.c
~$ gcc -o omp_hello -fopenmp omp_hello.c
- 執行 omp_hello 執行檔
~$ ./omp_hello Hello World from thread = 2 Hello World from thread = 3 Hello World from thread = 0 Number of threads = 4 Hello World from thread = 1
Example 2 : Loop work-sharing
- 首先從 OpenMP Exercise 下載 omp_workshare1.c
~$ wget https://computing.llnl.gov/tutorials/openMP/samples/C/omp_workshare1.c
- 使用 GCC 編譯 omp_workshare1.c
~$ gcc -o omp_workshare1 -fopenmp omp_workshare1.c
- 執行 omp_workshare1 執行檔
~$ ./omp_workshare1 ~$ ./omp_workshare1 Thread 3 starting... Thread 1 starting... Number of threads = 4 Thread 0 starting... Thread 0: c[20]= 40.000000 Thread 0: c[21]= 42.000000 Thread 0: c[22]= 44.000000 Thread 0: c[23]= 46.000000 Thread 0: c[24]= 48.000000 Thread 0: c[25]= 50.000000 ... skipped ... Thread 2 starting... Thread 2: c[90]= 180.000000 Thread 2: c[91]= 182.000000 Thread 2: c[92]= 184.000000 Thread 2: c[93]= 186.000000 Thread 2: c[94]= 188.000000 ... skipped ... Thread 3: c[0]= 0.000000 Thread 3: c[1]= 2.000000 Thread 3: c[2]= 4.000000 Thread 3: c[3]= 6.000000 Thread 3: c[4]= 8.000000 Thread 3: c[5]= 10.000000 ... skipped ... Thread 1: c[10]= 20.000000 Thread 1: c[11]= 22.000000 Thread 1: c[12]= 24.000000 Thread 1: c[13]= 26.000000 Thread 1: c[14]= 28.000000
OpenMP 參考資料
- 多核心平行化程式設計 Multi-Cores Programming
- <官網> OpenMP Tutorial
- <官網> OpenMP Exercise
- OpenMP Tutorial at National Energy Research Scientific Computing Center (NERSC)
- 簡易的程式平行化方法-OpenMP(一)簡介
- 簡易的程式平行化-OpenMP(二)語法說明
- 簡易的程式平行化-OpenMP(三)範例 parallel、section
- 簡易的程式平行化-OpenMP(四)範例 for
- 簡易的程式平行化-OpenMP(五) 變數的平行化
Perl v.s. multicore
- Parallel::ForkManager - A simple parallel processing fork manager
Python v.s. multicore
- Does python support multiprocessor/multicore programming?
- Parallel Python - Examples
- python-multiprocessing - Python 2.5/2.4 back port of the multiprocessing package