Re: Is short-circuit evaluation possible with parfor() ?

"Ruud H.G. van Tol" <[email protected]> Tue, 10 Feb 2026 10:26:13 +0100
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <[email protected]>
On 2026-02-10 08:58, [email protected] wrote:
> Thanks, but break did not help:
>
> hermann@x3950-X6:~$ gp -q
> ? 
> isok2(n)={s=0;p=prime(n+1)#;export(s,p);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,if(r,s=1;break));s==0};
> ? isok2(1994)
> 0
> ? ##
>   ***   last result: cpu time 1h, 21min, 39,829 ms, real time 26,349 ms.
> ? isok2(1993)
> 1
> ? ##
>   ***   last result: cpu time 1h, 24min, 46,507 ms, real time 28,423 ms.
> ?
>
>
> So I looked in the doc and there it states that return should abort, 
> but I cannot measure that:
>
> ? 
> isok2(n)={p=prime(n+1)#;export(p);parfor(i=2,n+1,ispseudoprime(1+p/prime(i)),r,if(r,return(0)));1};
> ? isok2(1994)
> 0
> ? ##
>   ***   last result: cpu time 1h, 21min, 49,788 ms, real time 27,072 ms.
> ?
>
> I do the same  if(r,return(...)) as in the doc, what am I missing?

Hello Hermann,

I expect it to be related to your nbthreads setting.

Mine is at 10. I hardly ever go over 12, to avoid overhead.
Even with many (virtual) CPUs, a lower setting might work out better,
as then it will have initiated less superfluous threads.
Would be easy to try and test and count.


? isok2(500)
cpu time = 14,893 ms, real time = 1,577 ms.
%15 = 0

? isok3(500)
cpu time = 6,802 ms, real time = 717 ms.
%16 = 0


isok4(n)= {  \\ same performance as isok3
   my(p=prime(n+1)#);  \\ or: vecprod(primes(n+1))
   parfor(i=2,n+1
   , ispseudoprime(1+p/prime(i)), r, r && return(0)
   );
   1;
}

I don't call export(), as I think that for this case it is superfluous.

-- Ruud