hermann on Wed, 20 Mar 2024 01:53:12 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

How to efficiently count Proth primes with GP parfor?


I had done some simple parfor experiments.
Diffent sequences are easier to spot on slower computers:

pi@raspberrypi400:~ $ gp -q
? parfor(i=2,100,isprime(i)*i,b,if(b,print1(b,",")))
2,3,7,11,13,19,23,17,29,31,37,43,41,47,59,61,53,67,71,73,79,5,89,97,83,
? parfor(i=2,100,isprime(i)*i,b,if(b,print1(b,",")))
2,3,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,5,83,89,97,
? parfor(i=2,100,isprime(i)*i,b,if(b,print1(b,",")))
2,3,7,11,13,17,19,23,29,31,5,43,37,47,41,53,59,61,67,71,73,79,83,89,97,
? parfor(i=2,100,isprime(i)*i,b,if(b,print1(b,",")))
2,3,7,5,11,13,17,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,19,
?


I have quite fast CPU with 32 hardware threads that can be used for parfor:

hermann@7950x:~$ lscpu | grep name
Model name:                         AMD Ryzen 9 7950X 16-Core Processor
hermann@7950x:~$ nproc
32
hermann@7950x:~$


PARI/GP version has thread support enabled:

                 GP/PARI CALCULATOR Version 2.15.4 (released)
         amd64 running linux (x86-64/GMP-6.2.1 kernel) 64-bit version
compiled: Feb 29 2024, gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
                           threading engine: pthread
                (readline v8.1 enabled, extended help enabled)


Test for Proth number — can that be done faster?
hermann@7950x:~$ gp -q
? isProth(p)=v=valuation(p-1,2);p>>v<2^v;
?


A080076 Proth primes: primes of the form k*2^m + 1 with odd k < 2^m, m >= 1.

https://oeis.org/A080076/b080076.txt
...
9998 4634443777
9999 4635230209
10000 4636016641


? c=0;forprime(p=3,4636016641,c+=1);c
218629816
? ##
  ***   last result: cpu time 28,710 ms, real time 28,711 ms.
? c=0;forprime(p=3,4636016641,c+=isProth(p));c
10000
? ##
*** last result: cpu time 1min, 25,406 ms, real time 1min, 25,408 ms.
? c=0;forprime(p=3,4636016641,if(isProth(p),c+=1));c
10000
? ##
  ***   last result: cpu time 1min, 9,411 ms, real time 1min, 9,416 ms.
?

So the last two do count Proth primes correctly.
Can it be done faster sequentially?


How can parfor() be used best to have all hardware threads count their isProth tests each and finally sum up to overall number of Proth primes? Or sum on each step?


I am doing it not right just now:

? c=0;parforprime(p=3,4636016641,isProth(p),C,if(C,c+=1));c
  ***   at top-level: c=0;parforprime(p=3,4636016641,isProth(p),C,if
  ***                     ^------------------------------------------
  *** parforprime: mt: please use export(isProth).
  ***   Break loop: type 'break' to go back to GP prompt
break>


? export(isProth)
? c=0;parforprime(p=3,4636016641,isProth(p),C,if(C,c+=1));c
  ***   at top-level: c=0;parforprime(p=3,4636016641,isProth(p),C,if
  ***                     ^------------------------------------------
  *** parforprime: mt: attempt to change exported variable 'v'.
  ***   Break loop: type 'break' to go back to GP prompt
break>


Regards,

Hermann.