Re: Maxima and thread safety

David Scherfgen via Maxima-discuss <[email protected]> Thu, 11 Jun 2026 17:03:01 +0200
Newsgroups gmane.comp.mathematics.maxima.general
Message-ID <CAMTHLKgsG6e=H8OGpHLVsZzTqAwpoOAk5obhz+Bcrcyx5HKzjQ@mail.gmail.com>
Yes, that seems to be working.
However, a note: You have to try with much higher numbers than just 10
computations. They finish so quickly that it's likely that they don't
really take place in parallel. If there were any problems, you would most
likely not notice them.
I adjusted the number to 150000 so that it takes several seconds. It still
worked, but I noticed that the parallel version is actually much *slower* than
the sequential one. Even if I chose just 1 thread, it's more than 10x
slower. I don't know why, maybe it has to do with memory allocations being
slower when there are multiple threads, because it uses a different
algorithm?
So, while this case works, it's actually counterproductive :-(
It would be nice if somebody else could reproduce this.

Best regards
David Scherfgen

Am Do., 11. Juni 2026 um 16:35 Uhr schrieb Michel Talon <
[email protected]>:

> Hello,
>
> this is an other example in which threads work OK
>
> (%i1) load("distribute_over_tranches_thread.lisp");
> (%o1)                distribute_over_tranches_thread.lisp
> (%i2)
> ee:x[i]^3+3*i^2*x[i]^2+8*i*x[i]^2+6*x[i]^2+6*i^3*x[i]+21*i^2*x[i]+26*i*x[i]
>       +11*x[i]+6*i^3+18*i^2+18*i+6$ /* A product of 3 factors */
> (%i3) pp:partfrac(1/ee,x[i])$
>
> (%i4) lll:makelist(subst(j,i,pp),j,1,10)$
>
> (%i5) llt:distribute_over_tranches (partfrac(1/ee,x[i]),i,10,3)$
>
> (%i6) is(lll=llt);
> (%o6)                                true
>
> In this example one runs partfrac with several main variables. Note that
> partfrac communicates with a program bprog which computes X and Y such as
>
> 1/(AB)=X/A+Y/B which in turns makes a lot of euclidean divisions, all of
> them communicating through the special variable *var* which is the "main"
> variable
>
> here x[i] for various values of i. You can note that the various main
> variables don't get mixed by threading, that the x[i] are correctly used,
> so the result
>
> is correct.
>
>
>
> Le 09/06/2026 à 17:15, David Scherfgen a écrit :
>
> You might add this to your examples:
>
> distribute_over_tranches(
>   '(block(
>       [x_n: float(N), x_next, i],
>       for i : 1 thru 1000 do (
>           x_next: 0.5 * (x_n + N / x_n),
>           x_n: x_next
>       ),
>       x_n)),
>   N, 1000, 16);
>
>
>
> As for this example, consider the following formulation:
> (%i1) compute(N):= (x:float(N), for i : 1 thru 10 do (
>           x: 0.5 * (x + N / x)), x)$
>
> (%i2) ll:distribute_over_tranches(compute(i),i,100,5)$
>
> (%i3) for i from 1 thru 100 do if (ll[i]-float(sqrt(i))>1e-15) then
> print(i);
> 73
> 89
> 95
>
> (%o3)                                done
>
> The error in question in these 3 cases is of order of 2e-15.  In other
> words this also seems to work.
>
> Note that if you iterate the inner loop to 1000 it takes forever to run,
> maybe this what can be taken as thread problem.
>
> --
> Michel Talon
>
> _______________________________________________
> Maxima-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/maxima-discuss
>

_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss