| 1 | [[PageOutline]] |
| 2 | |
| 3 | ◢ <[wiki:VNU120925/Lab1 實作一]> | <[wiki:VNU120925 回課程大綱]> ▲ | <[wiki:VNU120925/Lab3 實作三]> ◣ |
| 4 | |
| 5 | = 實作二 = |
| 6 | |
| 7 | {{{ |
| 8 | #!html |
| 9 | <div style="text-align: center;"><big style="font-weight: bold;"><big>多核心程式設計 : OpenMP<br/>Multi-Cores Programming : OpenMP</big></big></div> |
| 10 | }}} |
| 11 | |
| 12 | == 編譯環境檢驗 Compiler Version Check == |
| 13 | |
| 14 | * GCC 4.2 以上版本就有支援 OpenMP 了....用 "gcc '''-fopenmp'''" 就可以編譯 |
| 15 | * 確認目前 gcc 版本,須大於 4.2 以後的版本 |
| 16 | {{{ |
| 17 | ~$ gcc -v |
| 18 | Using built-in specs. |
| 19 | Target: x86_64-linux-gnu |
| 20 | 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 |
| 21 | Thread model: posix |
| 22 | gcc version 4.4.5 (Debian 4.4.5-8) |
| 23 | }}} |
| 24 | |
| 25 | == Example 1 : !HelloWorld == |
| 26 | |
| 27 | * 首先從 [https://computing.llnl.gov/tutorials/openMP/exercise.html OpenMP Exercise] 下載 omp_hello.c |
| 28 | {{{ |
| 29 | ~$ wget https://computing.llnl.gov/tutorials/openMP/samples/C/omp_hello.c |
| 30 | }}} |
| 31 | * 使用 GCC 編譯 omp_hello.c |
| 32 | {{{ |
| 33 | ~$ gcc -o omp_hello -fopenmp omp_hello.c |
| 34 | }}} |
| 35 | * 執行 omp_hello 執行檔 |
| 36 | {{{ |
| 37 | ~$ ./omp_hello |
| 38 | Hello World from thread = 2 |
| 39 | Hello World from thread = 3 |
| 40 | Hello World from thread = 0 |
| 41 | Number of threads = 4 |
| 42 | Hello World from thread = 1 |
| 43 | }}} |
| 44 | |
| 45 | == Example 2 : Loop work-sharing == |
| 46 | |
| 47 | * 首先從 [https://computing.llnl.gov/tutorials/openMP/exercise.html OpenMP Exercise] 下載 omp_workshare1.c |
| 48 | {{{ |
| 49 | ~$ wget https://computing.llnl.gov/tutorials/openMP/samples/C/omp_workshare1.c |
| 50 | }}} |
| 51 | * 使用 GCC 編譯 omp_workshare1.c |
| 52 | {{{ |
| 53 | ~$ gcc -o omp_workshare1 -fopenmp omp_workshare1.c |
| 54 | }}} |
| 55 | * 執行 omp_workshare1 執行檔 |
| 56 | {{{ |
| 57 | ~$ ./omp_workshare1 |
| 58 | ~$ ./omp_workshare1 |
| 59 | Thread 3 starting... |
| 60 | Thread 1 starting... |
| 61 | Number of threads = 4 |
| 62 | Thread 0 starting... |
| 63 | Thread 0: c[20]= 40.000000 |
| 64 | Thread 0: c[21]= 42.000000 |
| 65 | Thread 0: c[22]= 44.000000 |
| 66 | Thread 0: c[23]= 46.000000 |
| 67 | Thread 0: c[24]= 48.000000 |
| 68 | Thread 0: c[25]= 50.000000 |
| 69 | |
| 70 | ... skipped ... |
| 71 | |
| 72 | Thread 2 starting... |
| 73 | Thread 2: c[90]= 180.000000 |
| 74 | Thread 2: c[91]= 182.000000 |
| 75 | Thread 2: c[92]= 184.000000 |
| 76 | Thread 2: c[93]= 186.000000 |
| 77 | Thread 2: c[94]= 188.000000 |
| 78 | |
| 79 | ... skipped ... |
| 80 | |
| 81 | Thread 3: c[0]= 0.000000 |
| 82 | Thread 3: c[1]= 2.000000 |
| 83 | Thread 3: c[2]= 4.000000 |
| 84 | Thread 3: c[3]= 6.000000 |
| 85 | Thread 3: c[4]= 8.000000 |
| 86 | Thread 3: c[5]= 10.000000 |
| 87 | |
| 88 | ... skipped ... |
| 89 | |
| 90 | Thread 1: c[10]= 20.000000 |
| 91 | Thread 1: c[11]= 22.000000 |
| 92 | Thread 1: c[12]= 24.000000 |
| 93 | Thread 1: c[13]= 26.000000 |
| 94 | Thread 1: c[14]= 28.000000 |
| 95 | |
| 96 | }}} |
| 97 | |
| 98 | == OpenMP 參考資料 == |
| 99 | |
| 100 | * 多核心平行化程式設計 Multi-Cores Programming |
| 101 | * <官網> [https://computing.llnl.gov/tutorials/openMP/ OpenMP Tutorial] |
| 102 | * <官網> [https://computing.llnl.gov/tutorials/openMP/exercise.html OpenMP Exercise] |
| 103 | * [http://www.nersc.gov/nusers/help/tutorials/openmp/ OpenMP Tutorial] at National Energy Research Scientific Computing Center (NERSC) |
| 104 | * [http://kheresy.wordpress.com/2006/06/09/%E7%B0%A1%E6%98%93%E7%9A%84%E7%A8%8B%E5%BC%8F%E5%B9%B3%E8%A1%8C%E5%8C%96%E6%96%B9%E6%B3%95%EF%BC%8Dopenmp%EF%BC%88%E4%B8%80%EF%BC%89%E7%B0%A1%E4%BB%8B/ 簡易的程式平行化方法-OpenMP(一)簡介] |
| 105 | * [http://kheresy.wordpress.com/2006/09/13/%E7%B0%A1%E6%98%93%E7%9A%84%E7%A8%8B%E5%BC%8F%E5%B9%B3%E8%A1%8C%E5%8C%96%EF%BC%8Dopenmp%EF%BC%88%E4%BA%8C%EF%BC%89%E8%AA%9E%E6%B3%95%E8%AA%AA%E6%98%8E/ 簡易的程式平行化-OpenMP(二)語法說明] |
| 106 | * [http://kheresy.wordpress.com/2006/09/15/%E7%B0%A1%E6%98%93%E7%9A%84%E7%A8%8B%E5%BC%8F%E5%B9%B3%E8%A1%8C%E5%8C%96%EF%BC%8Dopenmp%EF%BC%88%E4%B8%89%EF%BC%89%E7%AF%84%E4%BE%8B-parallel%E3%80%81section/ 簡易的程式平行化-OpenMP(三)範例 parallel、section] |
| 107 | * [http://kheresy.wordpress.com/2006/09/15/%E7%B0%A1%E6%98%93%E7%9A%84%E7%A8%8B%E5%BC%8F%E5%B9%B3%E8%A1%8C%E5%8C%96%EF%BC%8Dopenmp%EF%BC%88%E5%9B%9B%EF%BC%89%E7%AF%84%E4%BE%8B-for/ 簡易的程式平行化-OpenMP(四)範例 for] |
| 108 | * [http://kheresy.wordpress.com/2006/09/22/%E7%B0%A1%E6%98%93%E7%9A%84%E7%A8%8B%E5%BC%8F%E5%B9%B3%E8%A1%8C%E5%8C%96%EF%BC%8Dopenmp%EF%BC%88%E4%BA%94%EF%BC%89-%E8%AE%8A%E6%95%B8%E7%9A%84%E5%B9%B3%E8%A1%8C%E5%8C%96/ 簡易的程式平行化-OpenMP(五) 變數的平行化] |
| 109 | |
| 110 | == Perl v.s. multicore == |
| 111 | |
| 112 | * [http://search.cpan.org/~dlux/Parallel-ForkManager-0.7.9/ Parallel::ForkManager] - A simple parallel processing fork manager |
| 113 | * [http://stackoverflow.com/questions/1961682/how-can-i-make-my-perl-script-use-multiple-cores-for-child-processes How can I make my Perl script use multiple cores for child processes?] |
| 114 | * [http://computationalbiologynews.blogspot.com/2008/07/harnessing-power-of-multicore.html Harnessing the power of multicore] |
| 115 | |
| 116 | == Python v.s. multicore == |
| 117 | |
| 118 | * [http://stackoverflow.com/questions/203912/does-python-support-multiprocessor-multicore-programming Does python support multiprocessor/multicore programming?] |
| 119 | * Parallel Python - [http://www.parallelpython.com/content/view/17/31/ Examples] |
| 120 | * [http://code.google.com/p/python-multiprocessing/ python-multiprocessing] - Python 2.5/2.4 back port of the multiprocessing package |