-
-
Save ACUVE/ef5cec31bfb3e506a779 to your computer and use it in GitHub Desktop.
This file contains 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 <atomic> | |
#include <chrono> | |
#include <cmath> | |
#include <iomanip> | |
#include <iostream> | |
#include <mutex> | |
#include <thread> | |
#include <vector> | |
#include <omp.h> | |
constexpr double D_PI = 3.1415926535897932384626433832795; | |
template< typename FUNC, typename... Args > | |
inline std::chrono::nanoseconds time( FUNC &&func, Args &&... args ) | |
{ | |
auto const start = std::chrono::high_resolution_clock::now(); | |
func( std::forward< Args >( args )... ); | |
auto const end = std::chrono::high_resolution_clock::now(); | |
auto const duration = end - start; | |
return std::chrono::duration_cast< std::chrono::nanoseconds >( duration ); | |
} | |
template< typename CHAR, typename TRAIT > | |
std::basic_ostream< CHAR, TRAIT > &operator<<( std::basic_ostream< CHAR, TRAIT > &os, unsigned __int128 value ) | |
{ | |
char str[ 40 ] = {}; | |
int index = 38; | |
if( value == 0 ) | |
str[ index-- ] = '0'; | |
else | |
{ | |
while( value != 0 ) | |
{ | |
int v = value % 10; | |
value /= 10; | |
str[ index-- ] = v + '0'; | |
} | |
} | |
return os << &str[ index + 1 ]; | |
} | |
void openmp() | |
{ | |
unsigned __int128 value = 0; | |
#pragma omp parallel num_threads( 2 ) | |
{ | |
if( omp_get_thread_num() == 0 ) | |
{ | |
for( unsigned int i = 0; i < 200000; ++i ) | |
{ | |
if( i % 2 ) | |
value = static_cast< unsigned __int128 >( 1 ) << 65; | |
else | |
value = static_cast< unsigned __int128 >( 1 ) << 1; | |
#pragma omp flush( value ) | |
} | |
} | |
else if( omp_get_thread_num() == 1 ) | |
{ | |
for( unsigned int i = 0; i < 2000; ++i ) | |
{ | |
#pragma omp flush( value ) | |
std::cout << value << std::endl; | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
constexpr unsigned long long int N = 1000ull * 1000 * 1000 * 10; | |
constexpr unsigned int count = 1; | |
std::cout << "omp_get_max_threads() = " << omp_get_max_threads() << std::endl; | |
std::cout << "std::thread::hardware_concurrency() = " << std::thread::hardware_concurrency() << std::endl; | |
for( unsigned int i = 0; i < count; ++i ) | |
std::cout << "OpenMP Elapsed time [ns] = " << time( openmp ).count() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment