Re: shared variables in parfor()

"Ruud H.G. van Tol" <[email protected]>
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <[email protected]>
On 2025-01-17 21:31, Max Alekseyev wrote:
> I naively thought that the following code would print a few zeros, 
> which would then follow by all ones:
>
> my(q=0); parfor(i=1,10^6, print(q); 0, R, q=1)
>
> but it constantly prints zeros. So, the value of q is not shared 
> between threads.
> What is the right way to create a shared variable (with pthreads if 
> that matters)?

A shared value is a synchronization point, so that would need locks.
This results in waiting threads, which likely slows things down.

Sometimes a memory-mapped file is used for shared values.

In general, prepare an environment for each thread with start values (Map),
then let the concurrent threads run (Reduce),
then post-process their results (Merge).

Essential is that all Reducing writers are independent ("lock-free").
Such a Map-Reduce-Merge "unit" can be nested, with proper care.

For what case did you want to use a shared variable?
Then we could try and untangle it.

-- Ruud
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.