Changes

Jump to: navigation, search

GPU621/CamelCaseTeam

2,534 bytes added, 20:31, 28 July 2021
OpenMP Threading - adding example
The code above shows the creation of 3 threads in a parallel region which are performed in sequence using critical. This highlights the difference between the c++11 thread library as no joining is required after
the threads concludes their function.= ===Equivalent Counting Program=== <syntaxhighlight lang="cpp">#include <iostream>#include <omp.h>#include <chrono> int main() { const int iteration = 100000000; const int runs = 5; const int mthreads = omp_get_max_threads(); for (int x = 0; x < mthreads; x++) { omp_set_num_threads(x + 1); double t = 0; int count = 0; for (int y = 0; y < runs; y++) { count = 0; double ts = omp_get_wtime();#pragma omp parallel { int tid = omp_get_thread_num(); int nt = omp_get_num_threads(); int temp = 0; for (int j = tid; j < iteration; j += nt) { temp++; }#pragma omp critical count += temp; } double te = omp_get_wtime(); double temp = (te - ts)*1000; t += temp; } std::cout << "OpenMP Thread - Counting to: " << count << " - Number of threads created: " << omp_get_max_threads() << " | elapsed time: " << t / runs << " ms" << std::endl; }}</syntaxhighlight> <syntaxhighlight lang="cpp">OpenMP Thread - Counting to: 100000000 - Number of threads created: 1 | elapsed time: 109.845 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 2 | elapsed time: 56.1196 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 3 | elapsed time: 39.7748 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 4 | elapsed time: 33.0161 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 5 | elapsed time: 29.7252 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 6 | elapsed time: 23.1936 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 7 | elapsed time: 20.8697 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 8 | elapsed time: 18.5374 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 9 | elapsed time: 17.1628 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 10 | elapsed time: 16.4023 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 11 | elapsed time: 14.3493 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 12 | elapsed time: 14.0788 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 13 | elapsed time: 12.9446 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 14 | elapsed time: 13.284 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 15 | elapsed time: 13.0647 msOpenMP Thread - Counting to: 100000000 - Number of threads created: 16 | elapsed time: 17.9958 ms</syntaxhighlight>
== References ==
16
edits

Navigation menu