Deutsch English Français Italiano |
<vc1qf5$v5bi$2@raubtier-asyl.eternal-september.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail From: Bonita Montero <Bonita.Montero@gmail.com> Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: My PC's cores can synchronize to about 1.000 clock cycles accuracy Followup-To: comp.lang.c++ Date: Fri, 13 Sep 2024 18:49:51 +0200 Organization: A noiseless patient Spider Lines: 53 Message-ID: <vc1qf5$v5bi$2@raubtier-asyl.eternal-september.org> References: <vc1q7b$v34v$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 13 Sep 2024 18:49:41 +0200 (CEST) Injection-Info: raubtier-asyl.eternal-september.org; posting-host="d68882be51447eabb86122ef614d859e"; logging-data="1021298"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18LC8qEhxIpKj+rlPGg4taay99l8D5NE9E=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:CfnN5tYvbKUBzGmY88rWfOm2UaM= In-Reply-To: <vc1q7b$v34v$1@raubtier-asyl.eternal-september.org> Content-Language: de-DE Bytes: 3017 Sorry, wrong newsgroup. Am 13.09.2024 um 18:45 schrieb Bonita Montero: > #include <iostream> > #include <barrier> > #include <thread> > #include <vector> > #if defined(_WIN32) > #include <intrin.h> > #elif defined(__linux__) > #include <x86intrin.h> > #endif > > using namespace std; > > int main() > { > unsigned hc = thread::hardware_concurrency(); > barrier bar( hc ); > atomic_uint synch( hc ); > atomic_uint64_t zero( 0 ); > atomic_int64_t diffs( 0 ); > auto thr = [&]() > { > int64_t sum = 0; > for( unsigned t = 1'000; t; --t ) > { > bar.arrive_and_wait(); > if( synch.fetch_sub( 1, memory_order_relaxed ) > 1 ) > while( synch.load( memory_order_relaxed ) ); > uint64_t tsc = __rdtsc(), expected = 0; > if( !zero.compare_exchange_weak( expected, tsc, > memory_order_relaxed, memory_order_relaxed ) ) > sum += abs( (int64_t)(expected - tsc) ); > bar.arrive_and_wait(); > synch.store( hc ); > zero.store( 0, memory_order_relaxed ); > } > diffs.fetch_add( sum, memory_order_relaxed ); > }; > vector<jthread> threads; > threads.reserve( hc - 1 ); > for( unsigned t = hc - 1; t; --t ) > threads.emplace_back( thr ); > thr(); > threads.resize( 0 ); > cout << (double)diffs.load( memory_order_relaxed ) / (1'000.0 * hc) > << endl; > } > > My PC is a AMD 7950X 16-core system. >