Re: Sieve of Erastosthenes optimized to the max

Liste des GroupesRevenir à cl c++ 
Sujet : Re: Sieve of Erastosthenes optimized to the max
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.c++
Date : 15. Jul 2024, 15:15:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86v8166bl1.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Vir Campestris <vir.campestris@invalid.invalid> writes:

On 02/07/2024 07:20, Tim Rentsch wrote:
>
I got the code.  There were some line wrappings but I just went
through and fixed those by hand.  After fixing the problem line
wraps I was able to compile and run the code (I used std=c++17,
which apparently works okay).  I did a simple check to make sure
the primes being found are right, which they do indeed seem to
be.  I haven't really experimented with changing the code;  I
only just ran it with various bounds on what primes to find,
mostly to get a sense of the performance.
>
I admit I don't understand the scheme the code is using.  It
looks to me like the code is doing divisions and remainders a
lot, which my code basically doesn't do at all (it does do one
division for each prime up to the square root of the limit of
primes being sought, but that's all).  I know the big template
near the beginning is crucial to understanding what the code is
doing, but I haven't been able to figure out what abstraction
that template is supposed to represent.  The core of my program
is between 50 and 60 lines, and all pretty straightforward.
>
Is there some suggestion you could make or a question you would
like to ask?  What can I do that might be helpful?
>
I checked.  The modulus operations (%) are easy to check for.
>
There are two for each prime number.  Not for each multiple of it, but
for the primes themselves.
>
And there's one in the "get" function that checks to see if a number
is prime.
>
The complexity is at least partly due to an optimisation.

It's taken me a while to get into this.  I think I understand
your code fairly well now.  My idea is to respond in pieces,
starting with the simpler aspects first.

You'll recall perhaps that Bonita's original code preset the entire
bitmap to aa - which is faster than setting all the numbers divisible
by 2.  I pointed out that you don't even need to set them, and set off
to write code that stored odd numbers only.
>
But then I realised that if you store the mask for 3 only you get a
pattern.  It has one odd word at the beginning, then a repeating
pattern of 3 words.  It doesn't matter what the word size is.
>
This is equally true of 5, 7, 11 and the first few primes.  It's also
true if you set the mask up for 3 and 5 together - except this time
the repeat length is 15.
>
And it's far faster to duplicate those 3, or 15, or 3*5*7, words than
to do each prime individually.
>
There comes a point where it isn't worth it though - the repeat length
for all the primes up to 31 is 1.5E12.
>
Exactly when it stops being worthwhile isn't obvious.
>
Then you came along with the mod30 suggestion.  There is still a repeat
length though, and it's still worth merging masks for small primes
together rather than doing each one individually.  But the code is
bigger.  MUCH bigger.

I am assuming a mod30 representation in all of my followup
comments.  Just to be explicit, there is an array of 8-bit
bytes, with bytes[i] indicating primeness or compositeness
for the 8 numbers 30*i + { 1, 7, 11, 13, 17, 19, 23, 29 },
one bit for each of those 8 possibilities.  (There may be
optimizations if the array elements are 64-bit quantities
rather than 8-bit quantities, but let's ignore that for
now.)

There are two plausible strategies for "pre-setting" the
multiples of small primes.  The first strategy uses two stages.
The first stage makes a mask for the four primes 7, 11, 13, and
17, which has 17017 elements, and simply replicates that mask
over and over through the entire array.  The second stage makes a
mask for the three primes 19, 23, and 29, which has 12673
elements, and uses bitwise operations to combine that mask
throughout the primes array.  Incidentally the convention I chose
is that a 1 bit means the number is definitely composite, so the
array could be initialized with all zero bits.

The second strategy makes a mask for all seven of 7, 11, 13, 17,
19, 23, and 29, which has 215656441 elements.  This mask is then
copied over and over throughout the array.  Because 215656441 is
quite a bit larger than your L1 cache number, it might be a win
to divide the mask into strips of size (L1 cache / 2), and copy
each strip separately, to get better cache performance.

Note that for the "setting" step (that is, the second strategy
or the first stage of the first strategy), the mask can be formed
in the initial part of the prime/composite array.  After taking
care of all multiples of the seven primes in the initial byte,
the initial byte should be set to indicate that those numbers
are prime, since otherwise they would be set wrongly.

I haven't experimented to see which of the above ideas has better
performance.  Like you say it's the small primes that matter,
because they have so many multiples, and it isn't obvious where
the cutoff should be.  But it's clear that every prime in the
first byte is worth doing, so you might try playing around with
just that part to see which approach does better.  I suggest
using a primes array that is as large as the memory on your test
system can accommodate without swapping.

I might be able to write followon segments at the rate of one per
day or so.  I'm not expecting to do them any faster than that,
and very likely will miss some days because of other activities.

Date Sujet#  Auteur
23 Mar 24 * Re: Sieve of Erastosthenes optimized to the max68Chris M. Thomasson
23 Mar 24 `* Re: Sieve of Erastosthenes optimized to the max67Bonita Montero
23 Mar 24  `* Re: Sieve of Erastosthenes optimized to the max66Chris M. Thomasson
24 Mar 24   `* Re: Sieve of Erastosthenes optimized to the max65Bonita Montero
24 Mar 24    `* Re: Sieve of Erastosthenes optimized to the max64Chris M. Thomasson
24 Mar 24     `* Re: Sieve of Erastosthenes optimized to the max63Bonita Montero
24 Mar 24      `* Re: Sieve of Erastosthenes optimized to the max62Chris M. Thomasson
16 May 24       `* Re: Sieve of Erastosthenes optimized to the max61Vir Campestris
16 May 24        +- Re: Sieve of Erastosthenes optimized to the max1Ben Bacarisse
22 May 24        `* Re: Sieve of Erastosthenes optimized to the max59Tim Rentsch
30 May 24         `* Re: Sieve of Erastosthenes optimized to the max58Vir Campestris
30 May 24          +- Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
30 May 24          +* Re: Sieve of Erastosthenes optimized to the max3Paavo Helde
31 May 24          i`* Re: Sieve of Erastosthenes optimized to the max2Bonita Montero
31 May 24          i `- Re: Sieve of Erastosthenes optimized to the max1Paavo Helde
31 May 24          `* Re: Sieve of Erastosthenes optimized to the max53Tim Rentsch
1 Jun 24           `* Re: Sieve of Erastosthenes optimized to the max52Vir Campestris
2 Jun 24            +- Re: Sieve of Erastosthenes optimized to the max1Richard Damon
2 Jun 24            `* Re: Sieve of Erastosthenes optimized to the max50Tim Rentsch
3 Jun 24             `* Re: Sieve of Erastosthenes optimized to the max49Tim Rentsch
18 Jun 24              `* Re: Sieve of Erastosthenes optimized to the max48Vir Campestris
19 Jun 24               `* Re: Sieve of Erastosthenes optimized to the max47Tim Rentsch
30 Jun 24                `* Re: Sieve of Erastosthenes optimized to the max46Vir Campestris
2 Jul 24                 `* Re: Sieve of Erastosthenes optimized to the max45Tim Rentsch
2 Jul 24                  +- Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
3 Jul 24                  `* Re: Sieve of Erastosthenes optimized to the max43Vir Campestris
15 Jul 24                   +- Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
20 Jul 24                   +* Re: Sieve of Erastosthenes optimized to the max40Tim Rentsch
25 Jul 24                   i`* OT: Re: Sieve of Erastosthenes optimized to the max39Vir Campestris
10 Aug 24                   i +* Re: OT: Re: Sieve of Erastosthenes optimized to the max36Tim Rentsch
12 Aug 24                   i i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Vir Campestris
16 Aug 24                   i ii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
15 Aug 24                   i i`* Re: OT: Re: Sieve of Erastosthenes optimized to the max33Vir Campestris
16 Aug 24                   i i `* Re: OT: Re: Sieve of Erastosthenes optimized to the max32Tim Rentsch
16 Aug 24                   i i  +* Re: OT: Re: Sieve of Erastosthenes optimized to the max30Bonita Montero
16 Aug 24                   i i  i+- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
19 Aug 24                   i i  i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max12Vir Campestris
20 Aug 24                   i i  ii`* Re: OT: Re: Sieve of Erastosthenes optimized to the max11Bonita Montero
20 Aug 24                   i i  ii `* Re: OT: Re: Sieve of Erastosthenes optimized to the max10Bonita Montero
20 Aug 24                   i i  ii  +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  ii  `* Re: OT: Re: Sieve of Erastosthenes optimized to the max8Vir Campestris
20 Aug 24                   i i  ii   +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
26 Aug 24                   i i  ii   `* Re: OT: Re: Sieve of Erastosthenes optimized to the max6Tim Rentsch
27 Aug 24                   i i  ii    `* Re: OT: Re: Sieve of Erastosthenes optimized to the max5Bonita Montero
1 Sep 24                   i i  ii     `* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Vir Campestris
2 Sep 24                   i i  ii      `* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Tim Rentsch
2 Sep 24                   i i  ii       +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
3 Sep 24                   i i  ii       `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
19 Aug 24                   i i  i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max12Vir Campestris
20 Aug 24                   i i  ii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max4red floyd
20 Aug 24                   i i  iii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Vir Campestris
26 Aug 24                   i i  iiii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
26 Aug 24                   i i  iii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
20 Aug 24                   i i  ii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Bonita Montero
20 Aug 24                   i i  iii+- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  iii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  ii`* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Bonita Montero
22 Aug 24                   i i  ii `* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Vir Campestris
22 Aug 24                   i i  ii  `* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Bonita Montero
22 Aug 24                   i i  ii   `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
22 Aug 24                   i i  i`* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Vir Campestris
23 Aug 24                   i i  i +* Re: OT: Re: Sieve of Erastosthenes optimized to the max2red floyd
26 Aug 24                   i i  i i`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
26 Aug 24                   i i  i `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
19 Aug 24                   i i  `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
11 Aug 24                   i +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
11 Aug 24                   i `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
23 Jul 24                   `- Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal