Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What does this program do ?

18 views
Skip to first unread message

Bonita Montero

unread,
Nov 27, 2022, 7:29:49 AM11/27/22
to
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__unix__)
#include <unistd.h>
#endif
#include <iostream>
#include <vector>
#include <atomic>
#include <thread>
#include <string_view>
#include <memory>
#include <chrono>

using namespace std;
using namespace chrono;

int main()
{
#if defined(_WIN32)
SYSTEM_INFO si;
GetSystemInfo( &si );
ptrdiff_t pageSize = si.dwPageSize;
#elif defined(__unix__)
ptrdiff_t pageSize = sysconf( _SC_PAGESIZE );
#endif
constexpr size_t
N_BOUNDARIES = 100,
ROUNDS = 1'000'000;
size_t range = pageSize * (N_BOUNDARIES + 2);
vector<char> vc( range + pageSize - 1 );
char *begin = (char *)(((size_t)to_address( vc.begin() ) + pageSize - 1
& -pageSize) + pageSize);
string_view view( begin, begin + N_BOUNDARIES * pageSize );
constexpr ptrdiff_t HALF_SIZE_T = sizeof(size_t) / 2;
static struct
{
ptrdiff_t offset;
char const *what;
} const offsets[3] =
{
{ -HALF_SIZE_T, "page-boundary", },
{ 0, "aligned" },
{ HALF_SIZE_T, "unaligned", }
};
for( auto const &offset : offsets )
{
auto start = high_resolution_clock::now();
ptrdiff_t off = offset.offset;
for( size_t r = ROUNDS; r--; )
for( string_view::iterator it = view.begin(), end = view.end(); ; )
{
(void)((atomic<size_t> &)to_address( it )[off]).load(
memory_order_relaxed );
// before increment because of iterator-debugging
if( it == end ) [[unlikely]]
break;
it += pageSize;
}
double ns = duration_cast<nanoseconds>( high_resolution_clock::now() -
start ).count() / ((double)N_BOUNDARIES * ROUNDS);
cout << offset.what << ": " << ns << endl;
}
}

Alf P. Steinbach

unread,
Nov 27, 2022, 9:05:57 AM11/27/22
to
On 27 Nov 2022 13:29, Bonita Montero wrote:
> What does this program do ?
> > [snipped code]

It's gone too far when you can decipher your own code. :-o


- Alf

Bonita Montero

unread,
Nov 27, 2022, 9:19:16 AM11/27/22
to
The code isn't really hard to unerstand, especially
because of the string literals in the code.


Bonita Montero

unread,
Nov 28, 2022, 5:07:54 AM11/28/22
to
The program tests the throughput of aligned memory accesses vs.
unaligned memory accesses and unaligned memory accesses crossing
a page-boundary. Aligned and unaligned memory accesses have the
same speed on current x86-CPUs if they don't cross a page-boun-
dary, the latter ones cost more than twice the CPU-time on my
AMD Zen2 CPU.
I asked myself about the cost of unaligned accesses when I had
a large lineary accessed data structure for which I used #pragma
pack(1) and when I got a measureable speed-up.
0 new messages