Let's assume we have
This task can be parallelized using MPI as follows:
- Let
$S = \sqrt{N}$ , and there are$P$ processes in MPI. - Each process will compute the primality test for the first
$S$ numbers locally. - The remaining
$N - S$ numbers are distributed equally among the$P$ processes. - Let
$F_i$ and$L_i$ be the first and the last number in the block for process$P_i$ .$B = (N - S) / P$ -
$F_i = S + rank * B$ (here$rank$ is the rank of the process in MPI) $L_i = F_i + B$
- As each process already has its local primality test result of numbers
$1$ ,$2$ , ...,$S$ , it can use that result to predict whether the number$K$ ($F_i \le K \le L_i$ ) is prime or not locally. - Then, it can count twin primes in its local block.
- As the computation for the first
$S$ number is done by all the processes, twin primes in this range can then be left to process with$rank = 0$ . - Then, we can use
MPI_Reduceto get the total number of twin primes.
We vary number of processors
We can observe that for high






