Bill Allombert on Thu, 19 Sep 2024 19:53:09 +0200


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

Re: concurrent computation of a function


On Thu, Sep 19, 2024 at 01:11:24PM -0400, Max Alekseyev wrote:
> Hi Bill,
> 
> Thank you for the elegant solution!
> Could you please elaborate on why the use of error() is not recommended? Is
> it because of side effects related to garbage collection or alike?

This is not related to error. The problem is with interrupting
a runnning thread at an arbitrary point. Using ^C has the same issue.
While PARI has some minimal protection against this, if the interruption
occurs at the wrong time, the system state might become inconsistent
and GP will crash.

> And is there a recommended alternative?

Only if you can change the inner loop of your program.
In that case you can check some shared state and stop when requested.
But there is no facility in GP for that, because that can lead to
race conditions.

The less ugly I could come up with is with using the fairly useless
seriesprecision:

fun(N)=
{
  while(1,
  if(default(seriesprecision)>16,return(0)); /*check signal */
  if(random(2^16)==0, /*Finish! */
    default(seriesprecision,17); /* signal other threads to stop */
    return(N)))
}

default(seriesprecision,16);parapply(fun,[1..20])
%5 = [0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Cheers,
Bill.