Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
% Copyright (c) 2000 The PARI Group
2
%
3
% This file is part of the PARI/GP documentation
4
%
5
% Permission is granted to copy, distribute and/or modify this document
6
% under the terms of the GNU General Public License
7
\appendix{PARI and threads}
8
9
To use PARI in multi-threaded programs, you must configure it using
10
\kbd{Configure --enable-tls}. Your system must implement the \kbd{\_\_thread}
11
storage class. As a major side effect, this breaks the \kbd{libpari} ABI: the
12
resulting library is not compatible with the old one, and \kbd{-tls} is
13
appended to the PARI library \kbd{soname}. On the other hand, this library is
14
now thread-safe.
15
16
PARI provides some functions to set up PARI subthreads\sidx{threads}. In our
17
model, each concurrent thread needs its own PARI stack. The following scheme
18
is used:
19
20
\noindent Child thread:
21
\bprog
22
void *child_thread(void *arg)
23
{
24
GEN data = pari_thread_start((struct pari_thread*)arg);
25
GEN result = ...; /* Compute result from data */
26
pari_thread_close();
27
return (void*)result;
28
}
29
@eprog
30
\noindent Parent thread:
31
\bprog
32
pthread_t th;
33
struct pari_thread pth;
34
GEN data, result;
35
36
pari_thread_alloc(&pth, s, data);
37
pthread_create(&th, NULL, &child_thread, (void*)&pth); /* start child */
38
... /* do stuff in parent */
39
pthread_join(th, (void*)&result); /* wait until child terminates */
40
result = gcopy(result); /* copy result from thread stack to main stack */
41
pari_thread_free(&pth); /* ... and clean up */
42
@eprog
43
44
\fun{void}{pari_thread_valloc}{struct pari_thread *pth, size_t s, size_t v, GEN arg}
45
Allocate a PARI stack of size \kbd{s} which can grow to at most \kbd{v} (as
46
with \kbd{parisize} and \kbd{parisizemax}) and associate it, together with the
47
argument \kbd{arg}, with the PARI thread data \kbd{pth}.
48
49
\fun{void}{pari_thread_alloc}{struct pari_thread *pth, size_t s, GEN arg}
50
As above but the stack cannot grow beyond \kbd{s}.
51
52
\fun{void}{pari_thread_free}{struct pari_thread *pth}
53
Free the PARI stack attached to the PARI thread data \kbd{pth}. This
54
is called after the child thread terminates, i.e.~after
55
\tet{pthread_join} in the parent. Any \kbd{GEN} objects returned by the
56
child in the thread stack need to be saved before running this command.
57
58
\fun{void}{pari_thread_init}{void}
59
Initialize the thread-local PARI data structures. This function is called by
60
\kbd{pari\_thread\_start}.
61
62
\fun{GEN}{pari_thread_start}{struct pari_thread *t}
63
Initialize the thread-local PARI data structures and set up the thread stack
64
using the PARI thread data \kbd{pth}. This function returns the thread
65
argument \kbd{arg} that was given to \kbd{pari\_thread\_alloc}.
66
67
\fun{void}{pari_thread_close}{void}
68
Free the thread-local PARI data structures, but keeping the thread stack, so
69
that a \kbd{GEN} returned by the thread remains valid.
70
71
\noindent Under this model, some PARI states are reset in new threads. In
72
particular
73
74
\item the random number generator is reset to the starting seed;
75
76
\item the system stack exhaustion checking code, meant to catch infinite
77
recursions, is disabled (use \kbd{pari\_stackcheck\_init()} to reenable it);
78
79
\item cached real constants (returned by \kbd{mppi}, \kbd{mpeuler} and
80
\kbd{mplog2}) are not shared between threads and will be recomputed as
81
needed;
82
83
\noindent The following sample program can be compiled using
84
\bprog
85
cc thread.c -o thread.o -lpari -lpthread
86
@eprog\noindent
87
(Add \kbd{-I/-L} paths as necessary.)
88
89
\noindent\bprogfile{../examples/thread.c}
90
91
\vfill\eject
92
93