├── .img └── alipay_jd_t.png ├── 00_dep ├── Makefile └── main.cpp ├── 01_parallel_cout ├── Makefile └── main.cpp ├── 02_parallel_no_omp ├── Makefile └── main.cpp ├── 03_reduce ├── Makefile └── main.cpp ├── 04_get_idx ├── Makefile └── main.cpp ├── 05_atomic_barrier ├── Makefile └── main.cpp ├── 06_mix ├── Makefile └── main.cpp ├── LICENSE └── README.md /.img/alipay_jd_t.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tlqtangok/openmp_demo/a9c992b3896e25b7d7676c91551f8902d2d9e5e5/.img/alipay_jd_t.png -------------------------------------------------------------------------------- /00_dep/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /00_dep/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | #if _OPENMP 8 | cout << " support openmp " << endl; 9 | #else 10 | cout << " not support openmp" << endl; 11 | #endif 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /01_parallel_cout/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /01_parallel_cout/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | #pragma omp parallel for num_threads(4) 10 | for(int i=0; i<10; i++) 11 | { 12 | cout << i << endl; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /02_parallel_no_omp/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /02_parallel_no_omp/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | using namespace std; 9 | 10 | template 11 | void exec_e(T &e) 12 | { 13 | cout << e << endl; 14 | } 15 | 16 | 17 | 18 | template 19 | void exec_multi_threads_on_data(vector& arr_src, const int& N, const int& td_num, void (*cb)(T &e)) 20 | { 21 | 22 | #define FLAG_USE_MULTIPLE_THREAD_ 1 23 | 24 | #define FLAG_USE_SINGLE_THREAD_ (!FLAG_USE_MULTIPLE_THREAD_) 25 | 26 | 27 | //typedef pair i_td; 28 | //int td_num_real = td_num - 1; 29 | int mod_num = ceil (N * 1.0f / td_num ); 30 | vector< pair > arr_pair_i_td{}; 31 | 32 | arr_pair_i_td.resize(td_num); 33 | 34 | //arr_pair_i_td.resize(N % mod_num == 0 ? N / mod_num : N / mod_num + 1); 35 | 36 | int cnt = 0; 37 | int cnt_arr = 0; 38 | 39 | cnt = 0; 40 | int flag_break_at_pad = 0; 41 | int cnt_threads = 0; 42 | for(int i=0;i N ? N : cnt + mod_num); 63 | 64 | 65 | arr_pair_i_td[id_td].first = id_td; 66 | arr_pair_i_td[id_td].second = thread([&cb](int id_td, vector& arr_src, pair pair_i_i) { 67 | #if 0 68 | float sleep_time_seconds = id_td * 0.02f; 69 | string cmd_sleep = string("sleep ") + tools_to_string(sleep_time_seconds) + "s"; 70 | system(cmd_sleep.c_str()); 71 | //cout << cmd_sleep << endl; 72 | #endif 73 | //cout << "- id_td: " << id_td << endl; 74 | 75 | for (auto i = pair_i_i.first; i < pair_i_i.second; i++) 76 | { 77 | //cout << arr_src[i] << " "; 78 | cb(arr_src[i]); 79 | } 80 | //cout << endl << "--------------------" << endl; 81 | 82 | }, 83 | id_td, std::ref(arr_src), pair_i_i 84 | ); 85 | 86 | 87 | 88 | 89 | 90 | #if FLAG_USE_SINGLE_THREAD_ 91 | cout << "- FLAG_USE_SINGLE_THREAD_ " << endl; 92 | arr_pair_i_td[id_td].second.join(); 93 | #endif 94 | 95 | if (flag_break_at_pad) break; 96 | } 97 | 98 | //cout << "- cnt_threads : " << cnt_threads << endl; 99 | 100 | #if FLAG_USE_MULTIPLE_THREAD_ 101 | for (auto& e_pair : arr_pair_i_td) 102 | { 103 | e_pair.second.join(); 104 | } 105 | 106 | #endif 107 | 108 | 109 | arr_pair_i_td.clear(); 110 | 111 | 112 | } 113 | 114 | 115 | 116 | int main() 117 | { 118 | 119 | const int N = 10; 120 | const int td_num = 3; 121 | 122 | 123 | vector arr_src{}; 124 | arr_src.resize(N); 125 | int cnt = 0; 126 | for (auto &e : arr_src) 127 | { 128 | e = cnt; 129 | cnt++; 130 | } 131 | 132 | exec_multi_threads_on_data(arr_src, N, td_num, exec_e); 133 | 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /03_reduce/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /03_reduce/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | 9 | #if 0 10 | // TODO : this is incorrect way to reduce !!! 11 | int sum = 0; 12 | #pragma omp parallel for num_threads(33) 13 | for(int i=0; i<100; i++) 14 | { 15 | sum += i; 16 | } 17 | 18 | cout << sum << endl; 19 | 20 | #endif 21 | 22 | #if 1 23 | 24 | int sum = 0; 25 | #pragma omp parallel for num_threads(32) reduction(+:sum) 26 | for(int i=0; i<100; i++) 27 | { 28 | sum += i; 29 | } 30 | 31 | cout << sum << endl; 32 | 33 | #endif 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /04_get_idx/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /04_get_idx/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define DEFINE_idx auto idx = omp_get_thread_num(); 8 | #define _ROWS (omp_get_num_threads()) 9 | 10 | 11 | int main() 12 | { 13 | 14 | 15 | 16 | #pragma omp parallel for num_threads(3) 17 | 18 | for(int i=0; i<10; i++) 19 | { 20 | DEFINE_idx; 21 | printf("- idx is %d, i is %d, total thread num is %d\n", idx, i, _ROWS); 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /05_atomic_barrier/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /05_atomic_barrier/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define DEFINE_idx auto idx = omp_get_thread_num(); 8 | #define _ROWS (omp_get_num_threads()) 9 | 10 | 11 | int main() 12 | { 13 | 14 | 15 | int sum = 0; 16 | #pragma omp parallel num_threads(3) 17 | { 18 | #pragma omp atomic 19 | sum += 10; 20 | #pragma omp barrier // TODO : disable this to see 21 | cout << sum << endl; 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /06_mix/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ -std=c++11 2 | 3 | nullstring := 4 | dep_I_incpath = -I./include/hiredis -I./include 5 | dep_L_libpath = -L./lib 6 | dep_l_libname = -lhiredis 7 | #deps_extra = $(dep_I_incpath) $(dep_L_libpath) $(dep_l_libname) 8 | 9 | deps_extra = $(nullstring) 10 | 11 | #c_flag = -g -Wno-format -fpermissive 12 | c_flag = -g -pthread -Wno-format -fpermissive -fopenmp 13 | 14 | c_files = \ 15 | $(wildcard *.cpp) $(wildcard *.c) $(wildcard *.cc) 16 | h_files = \ 17 | $(wildcard *.hpp) $(wildcard *.h) 18 | o_files_mess = \ 19 | $(patsubst %.cpp,%.o,${c_files}) $(patsubst %.c,%.o,${c_files}) $(patsubst %.cc,%.o,${c_files}) 20 | o_files = \ 21 | $(filter %.o, $(o_files_mess)) 22 | elf_file= \ 23 | mainapp.exe 24 | 25 | rm_files = \ 26 | *.o *.dep *.elf *.s *.exe 27 | 28 | 29 | all:$(elf_file) 30 | 31 | $(elf_file):$(o_files) 32 | $(CC) $(c_flag) -o $@ $(o_files) $(deps_extra) 33 | main.o: main.cpp 34 | $(CC) $(c_flag) -o $@ -c $< $(deps_extra) 35 | # @echo "c_file is:$(c_files)"; echo "h_files:$(h_files)"; echo "o_files:$(o_files)" 36 | 37 | clean: 38 | rm -rf $(rm_files) 39 | 40 | run:$(elf_file) 41 | ./$(elf_file) 42 | 43 | -------------------------------------------------------------------------------- /06_mix/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | 12 | using namespace std; 13 | 14 | #define DEFINE_idx auto idx = omp_get_thread_num(); 15 | #define _ROWS (omp_get_num_threads()) 16 | 17 | 18 | int main() 19 | { 20 | 21 | auto diff_in_seconds_jd = [](timeval *end, timeval *start) 22 | { 23 | double sec; 24 | sec=(end->tv_sec - start->tv_sec); 25 | sec+=(end->tv_usec - start->tv_usec)/1e6; 26 | return sec; 27 | }; 28 | 29 | struct timeval start, end; 30 | 31 | vector v_i{}; 32 | 33 | int len = int(1e7); 34 | v_i.resize(len); 35 | vector v_ans {}; 36 | v_ans.resize(len); 37 | 38 | 39 | for(int i=0;i