Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #83831
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: speed of unaligned accesses that cross page-boundaries |
| Date | 2022-04-28 12:19 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <t4dpmf$ud8$1@dont-email.me> (permalink) |
| References | (2 earlier) <t3leom$neh$1@dont-email.me> <cqz7K.73579$e%.42450@fx36.iad> <t3mg5e$pbb$1@dont-email.me> <t4b07g$ccl$1@dont-email.me> <t4dbkr$n2g$1@dont-email.me> |
There was a bug in my code. Now it's correct:
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__unix__)
#include <sys/mman.h>
#include <pthread.h>
#endif
#include <iostream>
#include <string_view>
#include <memory>
#include <thread>
#include <vector>
#include <latch>
#include <atomic>
#include <chrono>
#include <semaphore>
using namespace std;
using namespace chrono;
int main()
{
constexpr size_t
#if defined(__cpp_lib_hardware_interference_size)
CL_SIZE = hardware_destructive_interference_size,
#else
CL_SIZE = 64,
#endif
BLOCK_SIZE = 0x1000,
ROUNDS = 10'000'000;
#if defined(_WIN32)
char *begin = (char *)VirtualAlloc( nullptr, BLOCK_SIZE, MEM_RESERVE |
MEM_COMMIT, PAGE_READWRITE );
#elif defined(__unix__)
char *begin = (char *)mmap( nullptr, BLOCK_SIZE, PROT_READ |
PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
#endif
char *end = begin + BLOCK_SIZE;
atomic_uint readyCountDown;
binary_semaphore semReady( false );
counting_semaphore semRun( 0 );
atomic_uint synch;
atomic_uint64_t nsSum;
auto theThread = [&]( ptrdiff_t offset )
{
if( readyCountDown.fetch_sub( 1, memory_order_relaxed ) == 1 )
semReady.release();
semRun.acquire();
if( synch.fetch_sub( 1, memory_order_relaxed ) != 1 )
while( synch.load( memory_order_relaxed ) );
auto start = high_resolution_clock::now();
for( size_t r = ROUNDS; r--; )
for( char *p = begin + CL_SIZE; p != end; p += CL_SIZE )
(void)((atomic_uint &)p[offset]).load( memory_order_relaxed );
nsSum.fetch_add( (uint64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count(), memory_order_relaxed );
};
unsigned hc = thread::hardware_concurrency();
vector<jthread> threads;
threads.reserve( 2 );
static
struct offset_t
{
ptrdiff_t offset;
char const *description;
} const offsets[] =
{
{ 0, "aligned" },
{ 1, "unaligned" },
{ -1, "unaligned, crossing cachline boundary" }
};
for( offset_t const &off : offsets )
{
cout << off.description << ":" << endl;
for( unsigned nThreads = 1; nThreads <= 2; ++nThreads )
{
readyCountDown.store( nThreads, memory_order_relaxed );
synch.store( nThreads, memory_order_relaxed );
nsSum.store( 0, memory_order_relaxed );
for( unsigned t = 0; t != nThreads; ++t )
threads.emplace_back( theThread, off.offset );
semReady.acquire();
auto setAff = []( jthread::native_handle_type handle, unsigned cpu )
{
#if defined(_WIN32)
if( !SetThreadAffinityMask( handle, (DWORD_PTR)1 << cpu ) )
ExitProcess( EXIT_FAILURE );
#elif defined(__unix__)
cpu_set_t cpuSet;
CPU_ZERO(&cpuSet);
CPU_SET(cpu, &cpuSet);
if( pthread_setaffinity_np( handle, sizeof cpuSet, &cpuSet ) )
exit( EXIT_FAILURE );
#endif
};
for( size_t t = 0; t != nThreads; ++t )
setAff( threads[t].native_handle(), hc / 2 * (unsigned)t );
semRun.release( nThreads );
threads.resize( 0 );
cout << "\t" << nThreads << ": " << (double)(int64_t)nsSum.load(
memory_order_relaxed ) / ((int)nThreads * 1.0e9) << endl;
}
}
}
But the access-times are nearly still the same, i.e. crossing
a cacheline-boundary is nearly for free.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-03-20 18:04 +0100
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-03-20 18:08 +0100
Re: speed of unaligned accesses that cross page-boundaries scott@slp53.sl.home (Scott Lurndal) - 2022-03-20 18:12 +0000
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-03-20 19:36 +0100
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-03-20 16:13 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-03-21 06:58 +0100
Re: speed of unaligned accesses that cross page-boundaries scott@slp53.sl.home (Scott Lurndal) - 2022-03-21 14:49 +0000
Re: speed of unaligned accesses that cross page-boundaries Juha Nieminen <nospam@thanks.invalid> - 2022-03-21 05:59 +0000
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-03-21 08:56 +0100
Re: speed of unaligned accesses that cross page-boundaries scott@slp53.sl.home (Scott Lurndal) - 2022-03-21 14:53 +0000
Re: speed of unaligned accesses that cross page-boundaries scott@slp53.sl.home (Scott Lurndal) - 2022-03-21 16:59 +0000
Re: speed of unaligned accesses that cross page-boundaries Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-04-18 20:59 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-19 06:45 +0200
Re: speed of unaligned accesses that cross page-boundaries Juha Nieminen <nospam@thanks.invalid> - 2022-04-19 12:51 +0000
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-19 15:17 +0200
Re: speed of unaligned accesses that cross page-boundaries scott@slp53.sl.home (Scott Lurndal) - 2022-04-19 14:11 +0000
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-19 16:15 +0200
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-04-27 01:52 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-27 20:02 +0200
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-28 08:19 +0200
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-28 12:19 +0200
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-04-28 16:00 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-29 05:52 +0200
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-05-04 18:06 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-05 06:13 +0200
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-05-04 21:27 -0700
Re: speed of unaligned accesses that cross page-boundaries Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-05 07:37 +0200
Re: speed of unaligned accesses that cross page-boundaries Öö Tiib <ootiib@hot.ee> - 2022-05-05 04:34 -0700
Re: speed of unaligned accesses that cross page-boundaries "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-05-05 12:16 -0700
csiph-web