Jean-Luc Arnaud on Fri, 30 Jun 2023 17:22:33 +0200


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

Re: How to interrupt a MT calculation?


Le 30/06/2023 à 09:07, Bill Allombert a écrit :
On Fri, Jun 30, 2023 at 02:19:22AM +0200, Jean-Luc ARNAUD wrote:
Hi all,

In such a script:

|time()=[getabstime(),getwalltime()];||
||export(time);||
||
||Factor2_MT(n)=||
||
||{my(t);||||
||
||  print(pareval([
    ()->t=time(); factorint(n, 1); time()-t,||
||    ()->t=time(); factorint(n, 3); time()-t,||
||    ()->t=time(); if (n>2^468-1, factorint(n, 7), factorint(n, 6));
time()-t,||
||    ()->t=time(); factorint(n, 9); time()-t,||
||    ()->t=time(); factorint(n, 11); time()-t,||
||    ()->t=time(); print(factor(n)); time()-t
    ]))||
||};|

How is it possible to programmatically stop the calculations, let say as
soon as a result is returned?
This is not really advised, but you can do as follow
Use
default(factor_add_primes,1)

and then replace
factorint(n,...) by error(factorint(n, ...));
and then do

iferr(pareval([ .... ]),E,return(Vec(E)))

Cheers,
Bill

Thank you, Bill.

Your trick works, but not exactly as expected (at least as I expected it).

Actually, error does not interrupt calculations as soon as the error occurs, but at the end of pareval.

Here is the code I wrote:

time()=[getabstime(),getwalltime()];
export(time);
default(factor_add_primes,1);

Factor3_MT(n)=

{my(t);

  iferr(pareval([ ()->t=time(); error(factorint(n, 1); time()-t),
            ()->t=time(); error(factorint(n, 3); time()-t),
            ()->t=time(); if (n>2^468-1, error(factorint(n, 7);time()-t), error(factorint(n, 6);time()-t)),
            ()->t=time(); error(factorint(n, 9); time()-t),
            ()->t=time(); error(factorint(n, 11); time()-t),
        ()->t=time(); error(print(factor(n)); time()-t)]), E, return(Vec(E)))
};

And below the result of Factor3_MT(2^170-1):

[3, 1; 11, 1; 31, 1; 43691, 1; 131071, 1; 9520972806333758431, 1; 26831423036065352611, 1]
cpu time = 10,915 ms, real time = 5,436 ms.
%29 = ["e_USER", [[38, 8]]]

Factor2_MT(2^170-1) returns:

? Factor2_MT(2^170-1)
[3, 1; 11, 1; 31, 1; 43691, 1; 131071, 1; 9520972806333758431, 1; 26831423036065352611, 1]
[[10905, 5432], [10903, 5430], [67, 15], [35, 8], [42, 8], [93, 22]]
cpu time = 10,906 ms, real time = 5,432 ms.

So, first result is calculated in 8 ms (with factorint(n, 9)), but displayed only in 5,436 ms, that is in total calculation time.

With 2^470-1, first result in 1728 ms, displayed after... 7min, 24,773 ms !!!

Is there another way to interrupt the script as soon as the "error" occurs?

TIA

Jean-Luc