Re: Give up with MT, need help!!!
Bill Allombert <[email protected]>
| Newsgroups | gmane.comp.mathematics.pari.devel |
|---|---|
| Message-ID | <ZIoVPOC14zaWkb3T@seventeen> |
On Wed, Jun 14, 2023 at 05:56:38PM +0200, Jean-Luc ARNAUD wrote:
> Hi all,
>
> As a newby to PARI/GP, I'm trying to understand how it works in MT
> environment.
> And I'm getting a lot of difficulties.
>
> As an example, how could I replace the for ... loop by a parfor ... one in
> the chudnovsky_parallel(n) function ?
>
> After many and many tries, getting always either "Please export ..." or
> "Impossible div ..." error message, I give up ...
>
> Would somebody be so kind as to modify the below script in order to use
> parfor loop instead of for?
>
> |chudnovsky_parallel(n) = {||
> ||Â Â Â my(k, res, Result);||
> ||
> ||Â Â Â for(k = 0, ceil(n/14),||
> ||Â Â Â Â Â Â Â Result=pareval([||
> ||Â Â Â Â Â Â ()-> (-1)^k *(6*k)! * (13591409 + 545140134*k),||
> ||Â Â Â Â Â Â Â ()-> (3*k)! *k!^3 * (640320^(3*k + 3 / 2))]); /* +3/2 -> 583 ms,
> +1.5 -> 18,536 s !!! */||
> ||Â Â Â Â Â Â res += Result[1] / Result[2];||
> ||Â Â Â );||
> ||
> ||Â Â Â res = 1 / (res * 12);||
> ||
> ||};||
You can do this (using parfor)
chudnovsky_parsum(n) =
{
   my(res);
   parfor(k = 0, ceil(n/14),
my(d,n);
      d = (-1)^k *(6*k)! * (13591409 + 545140134*k);
       n = (3*k)! *k!^3 * (640320^(3*k + 3 / 2));
      d/n,
dn,
res += dn
   );
   res = 1 / (res * 12);
}
You could also use parsum:
chudnovsky_parsum(n) =
{
my(res);
res = parsum(k = 0, ceil(n/14),
my(d,n);
d = (-1)^k *(6*k)! * (13591409 + 545140134*k);
n = (3*k)! *k!^3 * (640320^(3*k + 3 / 2));
d/n
);
res = 1 / (res * 12);
}
or parapply
chudnovsky_parapply(n) =
{
my(res);
res = vecsum(parapply(k->
my(d,n);
d = (-1)^k *(6*k)! * (13591409 + 545140134*k);
n = (3*k)! *k!^3 * (640320^(3*k + 3 / 2));
d/n,
[0..ceil(n/14)]
));
res = 1 / (res * 12);
}
But this is not the right way to compute this. You should use
the recurrence formula for the factorial
k! = k*(k-1)! and not recompute k!^3, (3*k)!, (6*k)! for all k from scratch.
(3*k)!= (3*k)*(3*k-1)*(3*k-2)*(3*(k-1))!
etc.
Also you should factor out 640320^(3/2) to avoid recomputing it.
Cheers,
Bill