Created
November 6, 2020 23:20
-
-
Save sandeepkumar-skb/5bf9cea0dbeb71c9087fb731d00d421f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "omp.h" | |
#include <thread> | |
#include <iostream> | |
#include <vector> | |
#include <chrono> | |
void doNothing(){ | |
int count =0; | |
for (int i=0; i<1000; ++i) | |
++count; | |
} | |
int main(){ | |
int algorithmToRun = 1; | |
std::chrono::high_resolution_clock::time_point start; | |
std::chrono::high_resolution_clock::time_point end ; | |
std::chrono::duration<double> span; | |
start = std::chrono::high_resolution_clock::now(); | |
for(int j=1; j<100000; ++j) | |
{ | |
if(algorithmToRun == 1) | |
{ | |
std::vector<std::thread> threads; | |
for(int i=0; i<16; i++) | |
{ | |
threads.emplace_back(std::thread(doNothing)); | |
} | |
for(auto& t : threads) t.join(); | |
} | |
else if(algorithmToRun == 2) | |
{ | |
#pragma omp parallel for num_threads(16) | |
for(unsigned i=0; i<16; i++) | |
{ | |
doNothing(); | |
} | |
} | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
span = std::chrono::duration_cast<std::chrono::duration<double>>(end - start); | |
std::cout << "Time per Iteration: " << (span.count()*1000)<< "ms" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile:
g++ -fopenmp openmp_thread_pool.cpp -o out
C++ Threads:
91559.7ms
OpenMP Threads:
538.9ms