Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #83341 > unrolled thread

speed of unaligned accesses that cross page-boundaries

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2022-03-20 18:04 +0100
Last post2022-05-05 12:16 -0700
Articles 9 on this page of 29 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#83831

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-04-28 12:19 +0200
Message-ID<t4dpmf$ud8$1@dont-email.me>
In reply to#83824
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.

[toc] | [prev] | [next] | [standalone]


#83861

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-04-28 16:00 -0700
Message-ID<t4f6b1$i9d$1@dont-email.me>
In reply to#83831
On 4/28/2022 3:19 AM, Bonita Montero wrote:
> There was a bug in my code. Now it's correct:
[...]
> But the access-times are nearly still the same, i.e. crossing
> a cacheline-boundary is nearly for free.

One big problem is that it can lead to false-sharing. If threads, say A 
and B are working on their own data sets all padded to and aligned on 
cache lines, fine. Now, if they were not properly padded and aligned, 
thread B can interfere with thread A and vise versa via false sharing. 
This is not good at all. Remember that old hyperthreading aliasing 
issue? ;^)

[toc] | [prev] | [next] | [standalone]


#83867

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-04-29 05:52 +0200
Message-ID<t4fndk$u63$1@dont-email.me>
In reply to#83861
> One big problem is that it can lead to false-sharing. ...

Totally different story ...

[toc] | [prev] | [next] | [standalone]


#83945

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-05-04 18:06 -0700
Message-ID<t4v7vf$u4m$1@dont-email.me>
In reply to#83867
On 4/28/2022 8:52 PM, Bonita Montero wrote:
>> One big problem is that it can lead to false-sharing. ...
> 
> Totally different story ...

Have you ever had to deal with a false sharing bug? The program works, 
but it's slow..... Slower than a snail hiking up a mountain of salt? 
Well, shit.

[toc] | [prev] | [next] | [standalone]


#83946

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-05-05 06:13 +0200
Message-ID<t4viro$vba$1@dont-email.me>
In reply to#83945
Am 05.05.2022 um 03:06 schrieb Chris M. Thomasson:
> On 4/28/2022 8:52 PM, Bonita Montero wrote:
>>> One big problem is that it can lead to false-sharing. ...
>>
>> Totally different story ...
> 
> Have you ever had to deal with a false sharing bug? The program works, 
> but it's slow..... Slower than a snail hiking up a mountain of salt? 
> Well, shit.

I'm talking about unaligned accesses and not false sharing.

[toc] | [prev] | [next] | [standalone]


#83947

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-05-04 21:27 -0700
Message-ID<t4vjnp$45i$1@dont-email.me>
In reply to#83946
On 5/4/2022 9:13 PM, Bonita Montero wrote:
> Am 05.05.2022 um 03:06 schrieb Chris M. Thomasson:
>> On 4/28/2022 8:52 PM, Bonita Montero wrote:
>>>> One big problem is that it can lead to false-sharing. ...
>>>
>>> Totally different story ...
>>
>> Have you ever had to deal with a false sharing bug? The program works, 
>> but it's slow..... Slower than a snail hiking up a mountain of salt? 
>> Well, shit.
> 
> I'm talking about unaligned accesses and not false sharing.

Straddling a cache line can case false sharing...

[toc] | [prev] | [next] | [standalone]


#83948

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-05-05 07:37 +0200
Message-ID<t4vnpc$q30$1@dont-email.me>
In reply to#83947
Am 05.05.2022 um 06:27 schrieb Chris M. Thomasson:
> On 5/4/2022 9:13 PM, Bonita Montero wrote:
>> Am 05.05.2022 um 03:06 schrieb Chris M. Thomasson:
>>> On 4/28/2022 8:52 PM, Bonita Montero wrote:
>>>>> One big problem is that it can lead to false-sharing. ...
>>>>
>>>> Totally different story ...
>>>
>>> Have you ever had to deal with a false sharing bug? The program 
>>> works, but it's slow..... Slower than a snail hiking up a mountain of 
>>> salt? Well, shit.
>>
>> I'm talking about unaligned accesses and not false sharing.
> 
> Straddling a cache line can case false sharing...

That's not what I'm talking about.

[toc] | [prev] | [next] | [standalone]


#83954

FromÖö Tiib <ootiib@hot.ee>
Date2022-05-05 04:34 -0700
Message-ID<ac70b023-9fc3-4937-8d3e-c2576f4503dcn@googlegroups.com>
In reply to#83947
On Thursday, 5 May 2022 at 07:27:55 UTC+3, Chris M. Thomasson wrote:
> On 5/4/2022 9:13 PM, Bonita Montero wrote: 
> > Am 05.05.2022 um 03:06 schrieb Chris M. Thomasson: 
> >> On 4/28/2022 8:52 PM, Bonita Montero wrote: 
> >>>> One big problem is that it can lead to false-sharing. ... 
> >>> 
> >>> Totally different story ... 
> >> 
> >> Have you ever had to deal with a false sharing bug? The program works, 
> >> but it's slow..... Slower than a snail hiking up a mountain of salt? 
> >> Well, shit. 
> > 
> > I'm talking about unaligned accesses and not false sharing.
> Straddling a cache line can case false sharing...

But don't you see BM does not want to talk about it. He uses
unaligned accesses only in his single threaded software.

[toc] | [prev] | [next] | [standalone]


#83956

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2022-05-05 12:16 -0700
Message-ID<t517pl$asa$1@dont-email.me>
In reply to#83954
On 5/5/2022 4:34 AM, Öö Tiib wrote:
> On Thursday, 5 May 2022 at 07:27:55 UTC+3, Chris M. Thomasson wrote:
>> On 5/4/2022 9:13 PM, Bonita Montero wrote:
>>> Am 05.05.2022 um 03:06 schrieb Chris M. Thomasson:
>>>> On 4/28/2022 8:52 PM, Bonita Montero wrote:
>>>>>> One big problem is that it can lead to false-sharing. ...
>>>>>
>>>>> Totally different story ...
>>>>
>>>> Have you ever had to deal with a false sharing bug? The program works,
>>>> but it's slow..... Slower than a snail hiking up a mountain of salt?
>>>> Well, shit.
>>>
>>> I'm talking about unaligned accesses and not false sharing.
>> Straddling a cache line can case false sharing...
> 
> But don't you see BM does not want to talk about it. He uses
> unaligned accesses only in his single threaded software.

Yeah, I am being rather intrusive here. Sorry everybody. ;^o

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web