Question:

I have had a heavy mathematical computation to count the number of <a href="https://en.wikipedia.org/wiki/Twin_prime" rel="nofollow">twin prime</a> numbers within a range and I have divided the task between threads.

Here you see the profile of the execution time against the number of threads.

My questions are about justification of:

<ol><li>Why do single thread and dual threads have very similar performance?

</li> <li>Why there is a drop in execution time when it is 5- or 7-threaded, while execution time increases when 6 or 8 threads are used? (I have experienced that in several tests.)

</li> <li>I have used an 8-core computer. Can I claim that 2×<em>n</em> (where <em>n</em> is the number of cores) is a good number of threads as a rule of thumb?

</li> <li>If I use a code with a high usage of RAM, would I expect similar trends in the profile, or would it dramatically change with an increasing number of threads?

</li> </ol><a href="https://i.stack.imgur.com/t3sZb.png" rel="nofollow"><img alt="multithreading benchmark" class="b-lazy" data-src="https://i.stack.imgur.com/t3sZb.png" data-original="https://i.stack.imgur.com/t3sZb.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

This is the main part of the code only to show it does not use much RAM.

```
bool is_prime(long a)
{
if(a<2l)
return false;
if(a==2l)
return true;
for(long i=2;i*i<=a;i++)
if(a%i==0)
return false;
return true;
}
uint twin_range(long l1,long l2,int processDiv)
{
uint count=0;
for(long l=l1;l<=l2;l+=long(processDiv))
if(is_prime(l) && is_prime(l+2))
{
count++;
}
return count;
}
```

Specifications:

```
$ lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 94
Model name: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
Stepping: 3
CPU MHz: 799.929
CPU max MHz: 4000.0000
CPU min MHz: 800.0000
BogoMIPS: 6815.87
Virtualisation: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0-7
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
```

<hr /><strong>Update (After the accepted answer)</strong>

New profile:

<a href="https://i.stack.imgur.com/8tGjE.png" rel="nofollow"><img alt="Multi-threading performance" class="b-lazy" data-src="https://i.stack.imgur.com/8tGjE.png" data-original="https://i.stack.imgur.com/8tGjE.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

The improved code is as follows. Now, the workload is distributed fairly.

```
bool is_prime(long a)
{
if(a<2l)
return false;
if(a==2l)
return true;
for(long i=2;i*i<=a;i++)
if(a%i==0)
return false;
return true;
}
void twin_range(long n_start,long n_stop,int index,int processDiv)
{
// l1+(0,1,...,999)+0*1000
// l1+(0,1,...,999)+1*1000
// l1+(0,1,...,999)+2*1000
// ...
count=0;
const long chunks=1000;
long r_begin=0,k=0;
for(long i=0;r_begin<=n_stop;i++)
{
r_begin=n_start+(i*processDiv+index)*chunks;
for(k=r_begin;(k<r_begin+chunks) && (k<=n_stop);k++)
{
if(is_prime(k) && is_prime(k+2))
{
count++;
}
}
}
std::cout
<<"Thread "<<index<<" finished."
<<std::endl<<std::flush;
return count;
}
```

Answer1:Consider that your program will finish when the <em>last</em> thread has finished checking its range of numbers. Perhaps some threads are faster than others?

How long does `is_prime()`

take to determine that an even number is prime? It will find this on the first iteration. Finding the primality of an odd number will take at least two iterations and possibly up to sqrt(a) iterations if a is prime. `is_prime()`

will be very much slower when it is given a large prime than an even number!

In your two thread case, one thread will check the primality of 100000000, 100000002, 100000004, etc. while the other thread will check 100000001, 100000003, 100000005, etc. One thread checks all the even numbers while the other checks all the odd numbers (including all those slow primes!).

Have your threads print out `("Thread at %ld done", l1)`

when they finish, and I think you will find that some threads are very much faster than others, due to the way you are dividing the domain between the threads. An even number of threads will give all even values to the same thread(s), resulting in a particularly poor partitioning, which is why your even thread numbers are slower than the odd.

This would make a great XKCD-esque comic. "We need to check all these numbers to find primes! By hand!" "Ok, I'll check the evens, you do the odds."

Your real problem here is that a fixed domain decomposition like you have done requires that each partition take the same amount of time to be optimal.

The way to solve this is to dynamically do the partitioning. A pattern commonly used involves a pool of worker threads that request work in chunks. If the chunk is small compared to the total work a thread will do, then all threads will finish their work in a similar amount of time.

For your problem, you could have a mutex protected global data set `start_number, stop_number, total_twins`

. Each thread will save `start_number`

before incrementing its global value by `chunk_size`

. Then it searches the range `[saved_start_number, saved_start_number+chunk_size)`

, adding the number of twins found to the global `total_twins`

when done. The worker threads keep doing this until `start_number >= stop_number`

. Access to the globals use the mutex for protection. One has to adjust the chunk size to limit inefficiency from the cost of getting a chunk and contention on the mutex vs the inefficiency of idle worker threads having no more chunks to allocate while another thread is still working on it's last chunk. If you used an atomic increment to request a chunk, maybe the chunk size could be as small as a single value, but if one needed a network request in a distributed computing system then the chunk size would need to be much larger. That's the overview of how it works.

BTW, your `is_prime()`

test is naive and exceedingly slow. If a number was not divisible by 2, can it be divided by 4? One can do much better!

8 threads won't work faster than 7 since you have 8 CPUs (that are obviously only handling one thread - EDIT: thanks to @Algridas - <em>from your application</em>) each and your `main()`

needs a thread to run on.