使用vs直接到setting里面設置即可支持openmp了,然而我太懶了裝了個codeblocks with mingw版本
本來gcc4.4后就完全支持openmp了,結果codeblocks怎么配置都提示-fopenmp這些找不到。
搞了一上午終於發現,雖然mingw支持openmp但是在codeblocks安裝過程中並不讓裝這些組件,需要自己裝個tdm-gcc with mingw,蛋疼╮(╯▽╰)╭
配置流程:
1)下載tdm-gcc,安裝的時候注意默認openmp支持是沒有勾選的,記得勾選起來,要么安裝全部組件...
路徑的話安裝的時候會自己配置,沒有的話自己搞一下
2)安裝codeblocks(不用帶mingw了)
3)Settings->compiler里設置codeblocks編譯器路徑
4)Setting->compiler->Compiler settings->other options里輸入-fopenmp
Setting->compiler->linker settings->other linker options里輸入-lgomp -lpthread
5)至此配置完畢_(:зゝ∠)_ 寫一下代碼試試看
#include "omp.h" #include <iostream> #include <time.h> void test() { int a = 0; for (int i=0;i<100000000;i++) a++; } int main() { clock_t t1 = clock(); for (int i=0;i<8;i++) test(); clock_t t2 = clock(); std::cout<<"sequential time: "<<t2-t1<<std::endl; clock_t t3 = clock(); #pragma omp parallel for for (int i=0;i<8;i++) test(); clock_t t4 = clock(); std::cout<<"parallel time: "<<t4-t3<<std::endl; return 0; }
運行結果發現串行和並行運行時間還是有區別的!
還有一點是編譯時候我是debug模式編譯的,用release模式編譯時間就全部都是0了(因為速度太快了....)