<h2>Overview</h2>
<p>This answer provides probable explanations. Put it shortly, all parallel workload does not infinitely scale. When many cores compete for the same shared resource (eg. DRAM), using too many cores is often detrimental because <strong>there is a point where there are enough cores to saturate a given shared resource and using more core only increase the overheads</strong>.</p>
<p>More specifically, in your case, the L3 cache and the IMCs are likely the problem. Enabling <strong>Sub-NUMA Clustering</strong> and <strong>non-temporal prefetch</strong> should improve a bit the performances and the scalability of your benchmark. Still, there are other architectural hardware limitations that can cause the benchmark not to scale well. The next section describes how Intel Skylake SP processors deal with memory accesses and how to find the bottlenecks.</p>
<hr />
<h2>Under the hood</h2>
<p>The layout of Intel Xeon Skylake SP processors is like the following in your case:</p>
<p><a href="https://i.stack.imgur.com/7zTUM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7zTUM.png" alt="processor-configuration" /></a></p>
<p><a href="https://i.stack.imgur.com/D3Lgk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D3Lgk.png" alt="core-configuration" /></a> <br />
Source: <a href="https://www.intel.com/content/www/us/en/developer/articles/technical/xeon-processor-scalable-family-technical-overview.html" rel="nofollow noreferrer">Intel</a></p>
<p>There are two sockets connected with an UPI interconnect and each processor is connected to its own set of DRAM. There are 2 Integrated Memory Controller (IMC) per processor and each is connected to 3 DDR4 DRAM @ 2666MHz. This means the theoretical bandwidth is <code>2*2*3*2666e6*8 = 256 GB/s = 238 GiB/s</code>.</p>
<p>Assuming your benchmark is well designed and each processor access only to its NUMA node, I expect a very low UPI throughput and a very low number of remote NUMA pages. You can check this with hardware counters. Linux <code>perf</code> or VTune enable you to check this relatively easily.</p>
<p>The L3 cache is split in <strong>slices</strong>. All physical addresses are distributed across the cache slices using an <strong>hash function</strong> (see <a href="https://arxiv.org/abs/1508.03767" rel="nofollow noreferrer">here</a> for more informations). This method enable the processor to <strong>balance the throughput</strong> between all the L3 slices. This method also enable the processor to balance the throughput between the two IMCs so that in-fine the processor <em>looks like a SMP architecture</em> instead of a NUMA one. This was also use in Sandy Bridge and Xeon Phi processors (mainly to mitigate NUMA effects).</p>
<p>Hashing does not guarantee a perfect balancing though (no hash function is perfect, especially the ones that are fast to compute), but it is often quite good in practice, <em>especially for contiguous accesses</em>. A bad balancing decreases the memory throughput due to partial stalls. This is one reason you cannot reach the theoretical bandwidth.</p>
<p>With a good hash function, the balancing should be independent of the number of core used. If the hash function is not good enough, one IMC can be more saturated than the other one oscillating over time. The bad news is that the hash function is undocumented and checking this behaviour is complex: AFAIK you can get hardware counters for the each IMC throughput but they have a limited granularity which is quite big. On my Skylake machine the name of the hardware counters are <code>uncore_imc/data_reads/</code> and <code>uncore_imc/data_writes/</code> but on your platform you certainly have 4 counters for that (one for each IMC).</p>
<p>Fortunately, Intel provides a feature called <strong>Sub-NUMA Clustering</strong> (SNC) on Xeon SP processors like your. The idea is to split the processor in two NUMA nodes that have their own dedicated IMC. This solve the balancing issue due to the hash function and so result in faster memory operations as long as your application is NUMA-friendly. Otherwise, it can actually be significantly slower due to NUMA effects. In the worst case, the pages of an application can all be mapped to the same NUMA node resulting in only half the bandwidth being usable. Since your benchmark is supposed to be NUMA-friendly, SNC should be more efficient.</p>
<p><a href="https://i.stack.imgur.com/VJpfn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VJpfn.png" alt="Sub-NUMA Clustering" /></a> <br />
Source: Intel</p>
<p>Furthermore, having more cores accessing the L3 in parallel can cause more <strong>early evictions of prefetched cache lines</strong> which need to be fetched again later when the core actual need them (with an additional DRAM latency time to pay). This effect is not as unusual as it seems. Indeed, due to the high latency of DDR4 DRAMs, hardware prefetching units have to prefetch data a long time in advance so to reduce the impact of the latency. They also need to perform a lot of requests concurrently. This is generally not a problem with sequential accesses, but <strong>more cores causes accesses to look more random</strong> from the caches and IMCs point-of-view. The thing is DRAM are designed so that contiguous accesses are faster than random one (multiple <em>contiguous</em> cache lines should be loaded consecutively to fully saturate the bandwidth). You can analyse the value of the <code>LLC-load-misses</code> hardware counter to check if more data are re-fetched with more threads (I see such effect on my Skylake-based PC with only 6-cores but it is not strong enough to cause any visible impact on the final throughput). To mitigate this problem, you can use <strong>software non-temporal prefetch (<code>prefetchnta</code>)</strong> to request the processor to load data directly into the line fill buffer instead of the L3 cache resulting in a lower pollution (<a href="https://stackoverflow.com/questions/71818324/how-does-the-cpu-cache-affect-the-performance-of-a-c-program/71911346#71911346">here</a> is a related answer). This may be slower with fewer cores due to a lower concurrency, but it should be a bit faster with a lot of cores. Note that this does not solve the problem of having fetched address that looks more random from the IMCs point-of-view and there is not much to do about that.</p>
<p>The low-level architecture DRAM and caches is very complex in practice. More information about memory can be found in the following links:</p>
<ul>
<li><a href="https://people.freebsd.org/%7Elstewart/articles/cpumemory.pdf" rel="nofollow noreferrer">What Every Programmer Should Know About Memory</a></li>
<li><a href="https://web.corral.tacc.utexas.edu/CompEdu/pdf/stc/EijkhoutIntroToHPC.pdf" rel="nofollow noreferrer">Introduction to High Performance Scientific Computing</a> (Section 1.3)</li>
<li><a href="https://www.youtube.com/watch?v=IUk9o9wvX1Y" rel="nofollow noreferrer">Lecture: Main Memory and the DRAM System</a></li>
<li><a href="https://www.youtube.com/watch?v=I-9XWtdW_Co" rel="nofollow noreferrer">Short lectures: Dynamic Random Access Memory</a> (in 7 parts)</li>
<li><a href="https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html" rel="nofollow noreferrer">Intel® 64 and IA-32 Architectures Software Developer's Manual</a> (Volume 3)</li>
</ul>
Explanation for why effective DRAM bandwidth reduces upon adding CPUs
<p>This question is a spin-off of the one posted here: <a href="https://stackoverflow.com/questions/72182595/measuring-bandwidth-on-a-ccnuma-system">Measuring bandwidth on a ccNUMA system</a></p>
<p>I've written a micro-benchmark for the memory bandwidth on a ccNUMA system with 2x Intel(R) Xeon(R) Platinum 8168:</p>
<ol>
<li>24 cores @ 2.70 GHz,</li>
<li>L1 cache 32 kB, L2 cache 1 MB and L3 cache 33 MB.</li>
</ol>
<p>As a reference, I'm using the Intel Advisor's roof-line plot, which depicts the bandwidths of each CPU data-path available. According to this, the bandwidth is 230 GB/s.</p>
<p>Strong scaling of bandwidth:
<a href="https://i.stack.imgur.com/M8TMI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M8TMI.png" alt="enter image description here" /></a></p>
<p><strong>Question:</strong> If you look at the strong scaling diagram, you can see that the peak effective bandwidth is actually achieved at 33 CPUs, following which adding CPUs only reduces it. Why is this happening?</p>