Bill Allombert on Thu, 19 Sep 2024 17:57:17 +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 10:40:45AM -0400, Max Alekseyev wrote:
> Hello,
> 
> Suppose I have two functions func1(x) and func2(x) computing the same
> entity, where depending on the input one may be much faster than the other
> (or vice versa).
> For the same input x, I'd like to compute them in parallel and return the
> result as soon as one of them completes, and terminate execution of the
> other.
> Is there a way to achieve this in PARI/GP?

The issue to terminate the second computation safely.
You can do that with pthread using error() but this is not recommended.

f1(a,b)=a+b
f2(a,b)=a*b

export(f1,f2);
{
  iferr(pareval([
       ()->error(f1(5,6)),
       ()->error(f2(5,6))
  ]),E,return(component(E,1)[1]),err_name(E)="e_USER")
}

Cheers,
Bill.