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
\def\TITLE{Developer's Guide to the PARI library}
2
\input parimacro.tex
3
4
% START TYPESET
5
\begintitle
6
\vskip 2.5truecm
7
\centerline{\mine Developer's Guide}
8
\vskip 1.truecm
9
\centerline{\mine to}
10
\vskip 1.truecm
11
\centerline{\mine the PARI library}
12
\vskip 1.truecm
13
\centerline{\sectiontitlebf (version \vers)}
14
\vskip 1.truecm
15
\authors
16
\endtitle
17
18
\copyrightpage
19
\tableofcontents
20
\openin\std=develop.aux
21
\ifeof\std
22
\else
23
\input develop.aux
24
\fi
25
\chapno=0
26
27
\chapter{Work in progress}
28
29
This draft documents private internal functions and structures for hard-core
30
PARI developers. Anything in here is liable to change on short notice. Don't
31
use anything in the present document, unless you are implementing new
32
features for the PARI library. Try to fix the interfaces before using them,
33
or document them in a better way.
34
If you find an undocumented hack somewhere, add it here.
35
36
Hopefully, this will eventually document everything that we buried in
37
\kbd{paripriv.h} or even more private header files like \kbd{anal.h}.
38
Possibly, even implementation choices! Way to go.
39
40
\section{The type \typ{CLOSURE}}\kbdsidx{t_CLOSURE}\sidx{closure}
41
This type holds closures and functions in compiled form, so is deeply
42
linked to the internals of the GP compiler and evaluator.
43
The length of this type can be $6$, $7$ or $8$ depending whether the
44
object is an ``inline closure'', a ``function'' or a ``true closure''.
45
46
A function is a regular GP function. The GP input line is treated as a
47
function of arity $0$.
48
49
A true closure is a GP function defined in a nonempty lexical context.
50
51
An inline closure is a closure that appears in the code without
52
the preceding \kbd{->} token. They are generally attached to the prototype
53
code 'E' and 'I'. Inline closures can only exist as data of other closures,
54
see below.
55
56
In the following example,
57
\bprog
58
f(a=Euler)=x->sin(x+a);
59
g=f(Pi/2);
60
plot(x=0,2*Pi,g(x))
61
@eprog\noindent
62
\kbd{f} is a function, \kbd{g} is a true closure and both \kbd{Euler} and
63
\kbd{g(x)} are inline closures.
64
65
This type has a second codeword \kbd{z[1]}, which is the arity of the
66
function or closure. This is zero for inline closures. To access it, use
67
68
\fun{long}{closure_arity}{GEN C}
69
70
\item \kbd{z[2]} points to a \typ{STR} which holds the opcodes. To access it, use
71
72
\fun{GEN}{closure_get_code}{GEN C}.
73
74
\fun{const char *}{closure_codestr}{GEN C} returns as an array of \kbd{char}
75
starting at $1$.
76
77
\item \kbd{z[3]} points to a \typ{VECSMALL} which holds the operands of the opcodes.
78
To access it, use
79
80
\fun{GEN}{closure_get_oper}{GEN C}
81
82
\item \kbd{z[4]} points to a \typ{VEC} which hold the data referenced by the
83
\kbd{pushgen} opcodes, which can be \typ{CLOSURE}, and in particular
84
inline closures. To access it, use
85
86
\fun{GEN}{closure_get_data}{GEN C}
87
88
\item \kbd{z[5]} points to a \typ{VEC} which hold extra data needed for
89
error-reporting and debugging. See \secref{se:dbgclosure} for details.
90
To access it, use
91
92
\fun{GEN}{closure_get_dbg}{GEN C}
93
94
Additionally, for functions and true closures,
95
96
\item \kbd{z[6]} usually points to a \typ{VEC} with two components which are \typ{STR}.
97
The first one displays the list of arguments of the closure without the
98
enclosing parentheses, the second one the GP code of the function at the
99
right of the \kbd{->} token. They are used to display the closure, either in
100
implicit or explicit form. However for closures that were not generated from GP
101
code, \kbd{z[6]} can point to a \typ{STR} instead. To access it, use
102
103
\fun{GEN}{closure_get_text}{GEN C}
104
105
Additionally, for true closure,
106
107
\item \kbd{z[7]} points to a \typ{VEC} which holds the values of all lexical
108
variables defined in the scope the closure was defined. To access it, use
109
110
\fun{GEN}{closure_get_frame}{GEN C}
111
112
\subsec{Debugging information in closure}\label{se:dbgclosure}
113
114
Every \typ{CLOSURE} object \kbd{z} has a component \kbd{dbg=z[5]}
115
which hold extra data needed for error-reporting and debugging.
116
The object \kbd{dbg} is a \typ{VEC} with $3$ components:
117
118
\kbd{dbg[1]} is a \typ{VECSMALL} of the same length than \kbd{z[3]}. For each
119
opcode, it holds the position of the corresponding GP source code in the
120
strings stored in \kbd{z[6]} for function or true closures, positive indices
121
referring to the second strings, and negative indices referring to the first
122
strings, the last element being indexed as $-1$. For inline closures, the
123
string of the parent function or true closure is used instead.
124
125
\kbd{dbg[2]} is a \typ{VECSMALL} that lists opcodes index where new lexical
126
local variables are created. The value $0$ denotes the position before the
127
first offset and variables created by the prototype code 'V'.
128
129
\kbd{dbg[3]} is a \typ{VEC} of \typ{VECSMALL}s that give the list of
130
\kbd{entree*} of the lexical local variables created at a given index in
131
\kbd{dbg[2]}.
132
133
\section{The type \typ{LIST}}\kbdsidx{t_LIST}\sidx{list} This type needs to go
134
through various hoops to support GP's inconvenient memory model. Don't
135
use \typ{LIST}s in pure library mode, reimplement ordinary lists! This
136
dynamic type is implemented by a \kbd{GEN} of length 3: two codewords and a
137
vector containing the actual entries. In a normal setup (a finished list,
138
ready to be used),
139
140
\item the vector is malloc'ed, so that it can be realloc'ated without moving
141
the parent \kbd{GEN}.
142
143
\item all the entries are clones, possibly with cloned subcomponents; they
144
must be deleted with \tet{gunclone_deep}, not \tet{gunclone}.
145
146
The following macros are proper lvalues and access the components
147
148
\fun{long}{list_nmax}{GEN L}: current maximal number of elements. This grows
149
as needed.
150
151
\fun{GEN}{list_data}{GEN L}: the elements. If \kbd{v = list\_data(L)}, then
152
either \kbd{v} is \kbd{NULL} (empty list) or \kbd{l = lg(v)} is defined, and
153
the elements are \kbd{v[1]}, \dots, \kbd{v[l-1]}.
154
155
In most \kbd{gerepile} scenarios, the list components are not inspected
156
and a shallow copy of the malloc'ed vector is made. The functions
157
\kbd{gclone}, \kbd{copy\_bin\_canon} are exceptions, and make a full copy of
158
the list.
159
160
The main problem with lists is to avoid memory leaks; in the above setup,
161
a statement like \kbd{a = List(1)} would already leak memory, since
162
\kbd{List(1)} allocates memory, which is cloned (second allocation) when
163
assigned to \kbd{a}; and the original list is lost. The solution we
164
implemented is
165
166
\item to create anonymous lists (from \kbd{List}, \kbd{gtolist},
167
\kbd{concat} or \kbd{vecsort}) entirely on the stack, \emph{not} as described
168
above, and to set \kbd{list\_nmax} to $0$. Such a list is not yet proper and
169
trying to append elements to it fails:
170
\bprog
171
? listput(List(),1)
172
*** variable name expected: listput(List(),1)
173
*** ^----------------
174
@eprog\noindent
175
If we had been malloc'ing memory for the
176
\kbd{List([1,2,3])}, it would have leaked already.
177
178
\item as soon as a list is assigned to a variable (or a component thereof)
179
by the GP evaluator, the assigned list is converted to the proper format
180
(with \kbd{list\_nmax} set) previously described.
181
182
\fun{GEN}{listcopy}{GEN L} return a full copy of the \typ{LIST}~\kbd{L},
183
allocated on the \emph{stack} (hence \kbd{list\_nmax} is $0$). Shortcut for
184
\kbd{gcopy}.
185
186
\fun{GEN}{mklistcopy}{GEN x} returns a list with a single element $x$,
187
allocated on the stack. Used to implement most cases of \kbd{gtolist}
188
(except vectors and lists).
189
190
A typical low-level construct:
191
\bprog
192
long l;
193
/* assume L is a t_LIST */
194
L = list_data(L); /* discard t_LIST wrapper */
195
l = L? lg(L): 1;
196
for (i = 1; i < l; i++) output( gel(L, i) );
197
for (i = 1; i < l; i++) gel(L, i) = gclone( ... );
198
@eprog
199
200
\subsec{Maps as Lists}
201
202
GP's maps are implemented on top of \typ{LIST}s so as to benefit from
203
their peculiar memory models. Lists thus come in two subtypes: \typ{LIST\_RAW}
204
(actual lists) and \typ{LIST\_MAP} (a map).
205
206
\fun{GEN}{mklist_typ}{long t} create a list of subtype $t$.
207
\fun{GEN}{mklist}{void} is an alias for
208
\bprog
209
mklist_typ(t_LIST_RAW);
210
@eprog
211
and
212
\fun{GEN}{mkmap}{void} is an alias for
213
\bprog
214
mklist_typ(t_LIST_MAP);
215
@eprog
216
217
\fun{long}{list_typ}{GEN L} return the list subtype, either \typ{LIST\_RAW} or
218
\typ{LIST\_MAP}.
219
220
\fun{void}{listpop}{GEN L, long index} as \kbd{listpop0},
221
assuming that $L$ is a \typ{LIST\_RAW}.
222
223
\fun{GEN}{listput}{GEN list, GEN object, long index} as \kbd{listput0},
224
assuming that $L$ is a \typ{LIST\_RAW}.
225
226
\fun{GEN}{mapdomain}{GEN T} vector of keys of the map $T$.
227
228
\fun{GEN}{mapdomain_shallow}{GEN T} shallow version of \kbd{mapdomain}.
229
230
\fun{GEN}{maptomat}{GEN T} convert a map to a factorization matrix.
231
232
\fun{GEN}{maptomat_shallow}{GEN T} shallow version of \kbd{maptomat}.
233
234
\section{Protection of noninterruptible code}
235
236
GP allows the user to interrupt a computation by issuing SIGINT
237
(usually by entering control-C) or SIGALRM (usually using alarm()).
238
To avoid such interruption to occurs in section of code which are not
239
reentrant (in particular \kbd{malloc} and \kbd{free})
240
the following mechanism is provided:
241
242
\fun{}{BLOCK_SIGINT_START}{}
243
Start a noninterruptible block code. Block both \kbd{SIGINT} and \kbd{SIGARLM}.
244
245
\fun{}{BLOCK_SIGALRM_START}{}
246
Start a noninterruptible block code. Block only \kbd{SIGARLM}.
247
This is used in the \kbd{SIGINT} handler itself to delay an eventual pending
248
alarm.
249
250
\fun{}{BLOCK_SIGINT_END}{}
251
End a noninterruptible block code
252
253
The above macros make use of the following global variables:
254
255
\tet{PARI_SIGINT_block}: set to $1$ (resp. $2$) by \kbd{BLOCK\_SIGINT\_START}
256
(resp. \kbd{BLOCK\_SIGALRM\_START}).
257
258
\tet{PARI_SIGINT_pending}: Either $0$ (no signal was blocked), \kbd{SIGINT}
259
(\kbd{SIGINT} was blocked) or \kbd{SIGALRM} (\kbd{SIGALRM} was blocked).
260
This need to be set by the signal handler.
261
262
Within a block, an automatic variable \kbd{int block} is defined which
263
records the value of \kbd{PARI\_SIGINT\_block} when entering the block.
264
265
\subsec{Multithread interruptions}
266
267
To support multithreaded programs, \kbd{BLOCK\_SIGINT\_START} and
268
\kbd{BLOCK\_SIGALRM\_START} call \kbd{MT\_SIGINT\_BLOCK(block)}, and
269
\kbd{BLOCK\_SIGINT\_END} calls \kbd{MT\_SIGINT\_UNBLOCK(block)}.
270
271
\tet{MT_SIGINT_BLOCK} and \tet{MT_SIGINT_UNBLOCK} are defined by the
272
multithread engine. They can calls the following public functions defined by
273
the multithread engine.
274
275
\fun{void}{mt_sigint_block}{void}
276
277
\fun{void}{mt_sigint_unblock}{void}
278
279
In practice this mechanism is used by the POSIX thread engine to protect against
280
asychronous cancellation.
281
282
\section{$\F_{l^2}$ field for small primes $l$}
283
Let $l>2$ be a prime \kbd{ulong}. A \kbd{Fl2} is an element of the finite
284
field $\F_{l^2}$ represented (currently) by a \kbd{Flx} of degree at most $1$
285
modulo a polynomial of the form $x^2-D$ for some non square $0\leq D<p$.
286
Below \kbd{pi} denotes the pseudo inverse of \kbd{p}, see \kbd{Fl\_mul\_pre}
287
288
\fun{int}{Fl2_equal1}{GEN x} return $1$ if $x=1$, else return $0$.
289
290
\fun{GEN}{Fl2_mul_pre}{GEN x, GEN y, ulong D, ulong p, ulong pi} return $x\*y$.
291
292
\fun{GEN}{Fl2_sqr_pre}{GEN x, ulong D, ulong p, ulong pi} return $x^2$.
293
294
\fun{GEN}{Fl2_inv_pre}{GEN x, ulong D, ulong p, ulong pi} return $x^{-1}$.
295
296
\fun{GEN}{Fl2_pow_pre}{GEN x, GEN n, ulong D, ulong p, ulong pi} return
297
$x^n$.
298
299
\fun{GEN}{Fl2_sqrtn_pre}{GEN a, GEN n, ulong D, ulong p, ulong pi, GEN *zeta}
300
$n$-th root, as \kbd{Flxq\_sqrtn}.
301
302
\fun{GEN}{Fl2_norm_pre}{GEN x, GEN n, ulong D, ulong p, ulong pi} return the
303
norm of $x$.
304
305
\fun{GEN}{Flx_Fl2_eval_pre}{GEN P, GEN x, ulong D, ulong p, ulong pi}
306
return $P(x)$.
307
308
\section{Public functions useless outside of GP context}
309
310
These functions implement GP functionality for which the C language or
311
other libpari routines provide a better equivalent; or which are so tied
312
to the \kbd{gp} interpreter as to be virtually useless in \kbd{libpari}. Some
313
may be generated by \kbd{gp2c}. We document them here for completeness.
314
315
\subsec{Conversions}
316
317
\fun{GEN}{toser_i}{GEN x} internal shallow function, used to implement
318
automatic conversions to power series in GP (as in \kbd{cos(x)}).
319
Converts a \typ{POL} or a \typ{RFRAC} to a \typ{SER} in the same variable and
320
precision \kbd{precdl} (the global variable corresponding to
321
\kbd{seriesprecision}). Returns $x$ itself for a \typ{SER}, and \kbd{NULL}
322
for other argument types. The fact that it uses a global variable makes it
323
awkward whenever you're not implementing a new transcendental function in GP.
324
Use \tet{RgX_to_ser} or \tet{rfrac_to_ser} for a fast clean alternative to
325
\kbd{gtoser}.
326
327
\fun{GEN}{listinit}{GEN x} a \typ{LIST} (from \kbd{List} or \kbd{Map}) may
328
exist in two different forms due to GP memory model:
329
330
\item an ordinary \emph{read-only} copy on the PARI stack (as produced by
331
\kbd{gtolist} or \kbd{gtomap}) to which one may not assign elements
332
(\kbd{listput} will fail) unless the list is empty.
333
334
\item a feature-complete GP list using (malloc'ed) \kbd{block}s to
335
allow dynamic insertions. An empty list is automaticaly promoted to this
336
status on first insertion.
337
338
The \kbd{listinit} function creates a copy of existing \typ{SER} $x$ and
339
makes sure it is of the second kind. Variants of this are automatically
340
called by \kbd{gp} when assigning a \typ{LIST} to a GP variable; the
341
mecanism avoid memory leaks when creating a constant list, e.g.
342
\kbd{List([1,2,3])} (read-only), without assigning it to a variable. Whereas
343
after \kbd{L = List([1,2,3])} (GP list), we keep a pointer to the object and
344
may delete it when $L$ goes out of scope.
345
346
This \kbd{libpari} function allows \kbd{gp2c} to simulate this process by
347
generating \kbd{listinit} calls at appropriate places.
348
349
\subsec{Output}
350
351
\fun{void}{print0}{GEN g, long flag} internal function underlying the
352
\kbd{print} GP function. Prints the entries of the \typ{VEC} $g$, one by one,
353
without any separator; entries of type \typ{STR} are printed without enclosing
354
quotes. \fl is one of \tet{f_RAW}, \tet{f_PRETTYMAT} or \tet{f_TEX}, using the
355
current default output context.
356
357
\fun{void}{out_print0}{PariOUT *out, const char *sep, GEN g, long flag} as
358
\tet{print0}, using output context \kbd{out} and separator \kbd{sep} between
359
successive entries (no separator if \kbd{NULL}).
360
361
\fun{void}{printsep}{const char *s, GEN g, long flag} \tet{out_print0} on
362
\tet{pariOut} followed by a newline.
363
364
\fun{void}{printsep1}{const char *s, GEN g, long flag} \tet{out_print0} on
365
\tet{pariOut}.
366
367
\fun{char*}{pari_sprint0}{const char *s, GEN g, long flag} displays $s$,
368
then \kbd{print0(g, flag)}.
369
370
\fun{void}{print}{GEN g} equivalent to \kbd{print0(g, f\_RAW)}, followed
371
by a \kbd{\bs n} then an \kbd{fflush}.
372
373
\fun{void}{printp}{GEN g} equivalent to \kbd{print0(g, f\_PRETTYMAT)},
374
followed by a \kbd{\bs n} then an \kbd{fflush}.
375
376
\fun{void}{print1}{GEN g} as above, without the \kbd{\bs n}. Use
377
\tet{pari_printf} or \tet{output} instead.
378
379
\fun{void}{printtex}{GEN g} equivalent to \kbd{print0(g, t\_TEX)}, followed
380
by a \kbd{\bs n} then an \kbd{fflush}. Use \tet{GENtoTeXstr} and
381
\tet{pari_printf} instead.
382
383
\fun{void}{write0}{const char *s, GEN g}
384
385
\fun{void}{write1}{const char *s, GEN g} use \kbd{fprintf}
386
387
\fun{void}{writetex}{const char *s, GEN g} use \tet{GENtoTeXstr} and
388
\kbd{fprintf}.
389
390
\fun{void}{printf0}{GEN fmt, GEN args} use \tet{pari_printf}.
391
392
\fun{GEN}{strprintf}{GEN fmt, GEN args} use \tet{pari_sprintf}.
393
394
\subsec{Input}
395
396
\kbd{gp}'s input is read from the stream \tet{pari_infile}, which is changed
397
using
398
399
\fun{FILE*}{switchin}{const char *name}
400
401
Note that this function is quite complicated, maintaining stacks of files
402
to allow smooth error recovery and \kbd{gp} interaction. You will be better
403
off using \tet{gp_read_file}.
404
405
\subsec{Control flow statements}
406
407
\fun{GEN}{break0}{long n}. Use the C control statement \kbd{break}. Since
408
\kbd{break(2)} is invalid in C, either rework your code or use \kbd{goto}.
409
410
\fun{GEN}{next0}{long n}. Use the C control statement \kbd{continue}. Since
411
\kbd{continue(2)} is invalid in C, either rework your code or use \kbd{goto}.
412
413
\fun{GEN}{return0}{GEN x}. Use \kbd{return}!
414
415
\fun{void}{error0}{GEN g}. Use \kbd{pari\_err(e\_USER,)}
416
417
\fun{void}{warning0}{GEN g}. Use \kbd{pari\_warn(e\_USER,)}
418
419
\subsec{Accessors}
420
421
\fun{GEN}{vecslice0}{GEN A, long a, long b} implements $A[a..b]$.
422
423
\fun{GEN}{matslice0}{GEN A, long a, long b, long c, long d}
424
implements $A[a..b, c..d]$.
425
426
\subsec{Iterators}
427
428
\fun{GEN}{apply0}{GEN f, GEN A} gp wrapper calling \tet{genapply}, where $f$
429
is a \typ{CLOSURE}, applied to $A$. Use \kbd{genapply} or a standard C loop.
430
431
\fun{GEN}{select0}{GEN f, GEN A} gp wrapper calling \tet{genselect}, where $f$
432
is a \typ{CLOSURE} selecting from $A$. Use \kbd{genselect} or a standard C loop.
433
434
\fun{GEN}{vecapply}{void *E, GEN (*f)(void* E, GEN x), GEN x} implements
435
\kbd{[a(x)|x<-b]}.
436
437
\fun{GEN}{veccatapply}{void *E, GEN (*f)(void* E, GEN x), GEN x} implements
438
\kbd{concat([a(x)|x<-b])} which used to implement \kbd{[a0(x,y)|x<-b;y<-c(b)]}
439
which is equal to \kbd{concat([[a0(x,y)|y<-c(b)]|x<-b])}.
440
441
\fun{GEN}{vecselect}{void *E, long (*f)(void* E, GEN x), GEN A}
442
implements \kbd{[x<-b,c(x)]}.
443
444
\fun{GEN}{vecselapply}{void *Epred, long (*pred)(void* E, GEN x), void *Efun, GEN (*fun)(void* E, GEN x), GEN A}
445
implements \kbd{[a(x)|x<-b,c(x)]}.
446
447
\subsec{Local precision}
448
449
These functions allow to change \kbd{realprecision} locally when
450
calling the GP interpretor.
451
452
\fun{void}{push_localprec}{long p} set the local precision to $p$.
453
454
\fun{void}{push_localbitprec}{long b} set the local precision to $b$ bits.
455
456
\fun{void}{pop_localprec}{void} reset the local precision to the previous
457
value.
458
459
\fun{long}{get_localprec}{void} returns the current local precision.
460
461
\fun{long}{get_localbitprec}{void} returns the current local precision in bits.
462
463
\fun{void}{localprec}{long p} trivial wrapper around \kbd{push\_localprec}
464
(sanity checks and convert from decimal digits to a number of codewords).
465
Use \kbd{push\_localprec}.
466
467
\fun{void}{localbitprec}{long p}
468
trivial wrapper around \kbd{push\_localbitprec}
469
(sanity checks). Use \kbd{push\_localbitprec}.
470
471
These two function are used to implement \kbd{getlocalprec} and
472
\kbd{getlocalbitprec} for the GP interpreter and essentially return their
473
argument (the current dynamic precision, respectively in bits or as a
474
\kbd{prec} word count):
475
476
\fun{long}{getlocalbitprec}{long bit}
477
478
\fun{long}{getlocalprec}{long prec}
479
480
481
\subsec{Functions related to the GP evaluator}
482
483
The prototype code \kbd{C} instructs the GP compiler to save the current
484
lexical context (pairs made of a lexical variable name and its value)
485
in a \kbd{GEN}, called \kbd{pack} in the sequel. This \kbd{pack} can be used
486
to evaluate expressions in the corresponding lexical context, providing it is
487
current.
488
489
\fun{GEN}{localvars_read_str}{const char *s, GEN pack} evaluate the string $s$
490
in the lexical context given by \kbd{pack}. Used by \tet{geval_gp} in GP
491
to implement the behavior below:
492
\bprog
493
? my(z=3);eval("z=z^2");z
494
%1 = 9
495
@eprog
496
497
\fun{long}{localvars_find}{GEN pack, entree *ep} does \kbd{pack} contain
498
a pair whose variable corresponds to \kbd{ep}? If so, where is the
499
corresponding value? (returns an offset on the value stack).
500
501
\subsec{Miscellaneous}
502
503
\fun{char*}{os_getenv}{const char *s} either calls \kbd{getenv}, or directly
504
return \kbd{NULL} if the \kbd{libc} does not provide it. Use \tet{getenv}.
505
506
\fun{sighandler_t}{os_signal}{int sig, pari_sighandler_t fun} after a
507
\bprog
508
typedef void (*pari_sighandler_t)(int);
509
@eprog\noindent
510
(private type, not exported). Installs signal handler \kbd{fun} for
511
signal \kbd{sig}, using \tet{sigaction} with flag \tet{SA_NODEFER}. If
512
\kbd{sigaction} is not available use \tet{signal}. If even the latter is not
513
available, just return \tet{SIG_IGN}. Use \tet{sigaction}.
514
515
\section{Embedded GP interpretor}
516
These function provide a simplified interface to embed a GP
517
interpretor in a program.
518
519
\fun{void}{gp_embedded_init}{long rsize, long vsize}
520
Initialize the GP interpretor (like \kbd{pari\_init} does) with
521
\kbd{parisize=rsize} \kbd{rsize} and \kbd{parisizemax=vsize}.
522
523
\fun{char *}{gp_embedded}{const char *s}
524
Evaluate the string \kbd{s} with GP and return the result as a string,
525
in a format similar to what GP displays (with the history index).
526
The resulting string is allocated on the PARI stack, so subsequent call
527
to \kbd{gp\_embedded} will destroy it.
528
529
\section{Readline interface}
530
531
Code which wants to use libpari readline (such as the Jupyter notebook)
532
needs to do the following:
533
\bprog
534
#include <readline.h>
535
#include <paripriv.h>
536
pari_rl_interface S;
537
...
538
pari_use_readline(S);
539
@eprog\noindent The variable $S$, as initialized above, encapsulates
540
the libpari readline interface. (And allow us to move gp's readline code
541
to libpari without introducing a mandatory dependency on readline in
542
libpari.) The following functions then become available:
543
544
\fun{char**}{pari_completion_matches}{pari_rl_interface *pS, const char *s,
545
long pos, long *wordpos} given a command string $s$, where the cursor
546
is at index \kbd{pos}, return an array of completion matches.
547
548
If \kbd{wordpos} is not \kbd{NULL}, set \kbd{*wordpos} to the index for the
549
start of the expression we complete.
550
551
\fun{char**}{pari_completion}{pari_rl_interface *pS, char *text, int start,
552
int end} the low-level completer called by \tet{pari_completion_matches}.
553
The following wrapper
554
\bprog
555
char**
556
gp_completion(char *text, int START, int END)
557
{ return pari_completion(&S, text, START, END);)
558
@eprog\noindent is a valid value for
559
\tet{rl_attempted_completion_function}.
560
561
\section{Constructors called by \kbd{pari\_init} functions}
562
563
\fun{void}{pari_init_buffers}{}
564
565
\fun{void}{pari_init_compiler}{}
566
567
\fun{void}{pari_init_defaults}{}
568
569
\fun{void}{pari_init_evaluator}{}
570
571
\fun{void}{pari_init_files}{}
572
573
\fun{void}{pari_init_floats}{}
574
575
\fun{void}{pari_init_graphics}{}
576
577
\fun{void}{pari_init_homedir}{}
578
579
\fun{void}{pari_init_parser}{}
580
581
\fun{void}{pari_init_paths}{}
582
583
\fun{void}{pari_init_primetab}{}
584
585
\fun{void}{pari_init_rand}{}
586
587
\fun{void}{pari_init_seadata}{}
588
589
\section{Destructors called by \kbd{pari\_close}}
590
591
\fun{void}{pari_close_compiler}{}
592
593
\fun{void}{pari_close_evaluator}{}
594
595
\fun{void}{pari_close_files}{}
596
597
\fun{void}{pari_close_floats}{}
598
599
\fun{void}{pari_close_homedir}{}
600
601
\fun{void}{pari_close_mf}{}
602
603
\fun{void}{pari_close_parser}{}
604
605
\fun{void}{pari_close_paths}{}
606
607
\fun{void}{pari_close_primes}{}
608
609
\section{Constructors and destructors used by the \kbd{pthreads} interface}
610
611
\item Called by \tet{pari_thread_close}
612
613
\fun{void}{pari_thread_close_files}{}
614
615
\newpage
616
617
\chapter{Regression tests, benches}
618
619
This chapter documents how to write an automated test module, say \kbd{fun},
620
so that \kbd{make test-fun} executes the statements in the \kbd{fun} module
621
and times them, compares the output to a template, and prints an error
622
message if they do not match.
623
624
\item Pick a \emph{new} name for your test, say \kbd{fun}, and write down a
625
GP script named \kbd{fun}. Make sure it produces some useful output and tests
626
adequately a set of routines.
627
628
\item The script should not be too long: one minute runs should be enough.
629
Try to break your script into independent easily reproducible tests, this way
630
regressions are easier to debug; e.g. include \kbd{setrand(1)} statement before
631
a randomized computation. The expected output may be different on 32-bit and
632
64-bit machines but should otherwise be platform-independent. If possible, the
633
output shouldn't even depend on \kbd{sizeof(long)}; using a \kbd{realprecision}
634
that exists on both 32-bit and 64-bit architectures, e.g. \kbd{\bs p 38} is a
635
good first step. You can use \kbd{sizebyte(0)==16} to detect a 64-bit
636
architecture and \kbd{sizebyte(0)==8} for 32-bit.
637
638
\item Dump your script into \kbd{src/test/in/} and run \kbd{Configure}.
639
640
\item \kbd{make test-fun} now runs the new test, producing a \kbd{[BUG]} error
641
message and a \kbd{.dif} file in the relevant object directory \kbd{Oxxx}.
642
In fact, we compared the output to a nonexisting template, so this must fail.
643
644
\item Now
645
\bprog
646
patch -p1 < Oxxx/fun.dif
647
@eprog\noindent
648
generates a template output in the right place \kbd{src/test/32/fun}, for
649
instance on a 32-bit machine.
650
651
\item If different output is expected on 32-bit and 64-bit machines, run the
652
test on a 64-bit machine and patch again, thereby
653
producing \kbd{src/test/64/fun}. If, on the contrary, the output must be the
654
same (preferred behavior!), make sure the output template land in the
655
\kbd{src/test/32/} directory which provides a default template when the
656
64-bit output file is missing; in particular move the file from
657
\kbd{src/test/64/} to \kbd{src/test/32/} if the test was run on a 64-bit
658
machine.
659
660
\item You can now re-run the test to check for regressions: no \kbd{[BUG]}
661
is expected this time! Of course you can at any time add some checks, and
662
iterate the test / patch phases. In particular, each time a bug in the
663
\kbd{fun} module is fixed, it is a good idea to add a minimal test case to
664
the test suite.
665
666
\item By default, your new test is now included in \kbd{make test-all}. If
667
it is particularly annoying, e.g. opens tons of graphical windows as
668
\kbd{make test-ploth} or just much longer than the recommended minute, you
669
may edit \kbd{config/get\_tests} and add the \kbd{fun} test to the list of
670
excluded tests, in the \kbd{test\_extra\_out} variable.
671
672
\item You can run a subset of existing tests by using the following idiom:
673
\bprog
674
cd Oxxx # call from relevant build directory
675
make TESTS="lfuntype lfun gamma" test-all
676
@eprog\noindent will run the \kbd{lfuntype}, \kbd{lfun} and \kbd{gamma} tests.
677
This produces a combined output whereas the alternative
678
\bprog
679
make test-lfuntype test-lfun test-gamma
680
@eprog\noindent would not.
681
682
\item By default, the test is run on both the \kbd{gp-sta} and \kbd{gp-dyn}
683
binaries, making it twice as slow. If the test is somewhat long, it can
684
be annoying; you can restrict to one binary only using the \kbd{statest-all}
685
or \kbd{dyntest-all} targets. Both accept the \kbd{TESTS} argument seen above.
686
687
\bprog
688
make test-lfuntype test-lfun gamma
689
@eprog\noindent would not.
690
691
\item Finally, the \kbd{get\_tests} script also defines the recipe for
692
\kbd{make bench} timings, via the variable \kbd{test\_basic}. A test is
693
included as \kbd{fun} or \kbd{fun\_$n$}, where $n$ is an integer $\leq 1000$;
694
the latter means that the timing is weighted by a factor $n/1000$. (This was
695
introduced a long time ago, when the \kbd{nfields} bench was so much slower
696
than the others that it hid slowdowns elsewhere.)
697
698
\section{Functions for GP2C}
699
700
\subsec{Functions for safe access to components}
701
702
These functions return the address of the requested component after checking
703
it is actually valid. This is used by GP2C -C.
704
705
\fun{GEN*}{safegel}{GEN x, long l}, safe version of \kbd{gel(x,l)} for \typ{VEC},
706
\typ{COL} and \typ{MAT}.
707
708
\fun{long*}{safeel}{GEN x, long l}, safe version of \kbd{x[l]} for \typ{VECSMALL}.
709
710
\fun{GEN*}{safelistel}{GEN x, long l} safe access to \typ{LIST} component.
711
712
\fun{GEN*}{safegcoeff}{GEN x, long a, long b} safe version of
713
\kbd{gcoeff(x,a, b)} for \typ{MAT}.
714
715
\newpage
716
\chapter{Parallelism}
717
718
PARI provides an abstraction, herafter called the MT engine, for doing
719
parallel computations. The exact same high level routines are used whether
720
the underlying communication protocol is POSIX threads or MPI and they behave
721
differently depending on how \kbd{libpari} was configured, specifically on
722
\kbd{Configure}'s \kbd{--mt} option. Sequential computation is also supported
723
(no \kbd{--mt} argument) which is helpful for debugging newly written
724
parallel code. The final section in this chapter comments a complete example.
725
726
\section{The PARI multithread interface}
727
728
\fun{void}{mt_queue_start}{struct pari_mt *pt, GEN worker} Let \kbd{worker}
729
be a \typ{CLOSURE} object of arity $1$. Initialize the opaque structure
730
\kbd{pt} to evaluate \kbd{worker} in parallel, using \kbd{nbthreads} threads.
731
This allocates data in
732
various ways, e.g., on the PARI stack or as malloc'ed objects: you may not
733
collect garbage on the PARI stack starting from an earlier \kbd{avma} point
734
until the parallel computation is over, it could destroy something in \kbd{pt}.
735
All ressources allocated outside the PARI stack are freed by
736
\kbd{mt\_queue\_end}.
737
738
\fun{void}{mt_queue_start_lim}{struct pari_mt *pt, GEN worker, long lim}
739
as \kbd{mt\_queue\_start}, where \kbd{lim} is an upper bound on the number
740
of \kbd{tasks} to perform. Concretely the number of threads is the minimum
741
of \kbd{lim} and \kbd{nbthreads}. The values $0$ and $1$ of \kbd{lim} are
742
special:
743
744
\item $0$: no limit, equivalent to \kbd{mt\_queue\_start} (use
745
\kbd{nbthreads} threads).
746
747
\item $1$: no parallelism, evaluate the tasks sequentially.
748
749
\fun{void}{mt_queue_submit}{struct pari_mt *pt, long taskid, GEN task} submit
750
\kbd{task} to be evaluated by \kbd{worker}; use \kbd{task = NULL} if no
751
further task needs to be submitted. The parameter \kbd{taskid} is attached to
752
the \kbd{task} but not used in any way by the \kbd{worker} or the MT engine,
753
it will be returned to you by \kbd{mt\_queue\_get} together with the result
754
for the task, allowing to match up results and submitted tasks if desired.
755
For instance, if the tasks $(t_1,\dots, t_m)$ are known in advance, stored in
756
a vector, and you want to recover the evaluation results in the same order as
757
in that vector, you may use consecutive integers $1, \dots, m$ as
758
\kbd{taskid}s. If you do not care about the ordering, on the other hand, you
759
can just use $\kbd{taskid} = 0$ for all tasks.
760
761
The \kbd{taskid} parameter is ignored when \kbd{task} is \kbd{NULL}. It is
762
forbidden to call this function twice without an intervening
763
\kbd{mt\_queue\_get}.
764
765
\fun{GEN}{mt_queue_get}{struct pari_mt *pt, long *taskid, long *pending}
766
return \kbd{NULL} until \kbd{mt\_queue\_submit} has submitted
767
tasks for the required number (\kbd{nbthreads}) of threads; then return the
768
result of the evaluation by \kbd{worker} of one of the previously submitted
769
tasks, in random order. Set \kbd{pending} to the number of remaining pending
770
tasks: if this is $0$ then no more tasks are pending and it is safe to call
771
\tet{mt_queue_end}. Set \kbd{*taskid} to the value attached to this task by
772
\kbd{mt\_queue\_submit}, unless the \kbd{taskid} pointer is \kbd{NULL}. It is
773
forbidden to call this function twice without an intervening
774
\kbd{mt\_queue\_submit}.
775
776
\fun{void}{mt_queue_end}{struct pari_mt *pt} end the parallel execution
777
and free ressources attached to the opaque \kbd{pari\_mt} structure. For
778
instance malloc'ed data; in the \kbd{pthreads} interface, it would destroy
779
mutex locks, condition variables, etc. This must be called once there are no
780
longer pending tasks to avoid leaking ressources; but not before all tasks
781
have been processed else crashes will occur.
782
783
\fun{long}{mt_nbthreads}{void} return the effective number of parallel threads
784
that would be started by \tet{mt_queue_start} if it has been called in place
785
of \tet{mt_nbthreads}.
786
787
\section{Technical functions required by MPI}
788
789
The functions in this section are needed when writing complex independent
790
programs in order to support the MPI MT engine, as more flexible
791
complement/variants of \kbd{pari\_init} and \kbd{pari\_close}.
792
793
\fun{void}{mt_broadcast}{GEN code}: do nothing unless the MPI threading engine
794
is in use. In that case, evaluates the closure \kbd{code} on all secondary
795
nodes. This can be used to change the state of all MPI child nodes, e.g.,
796
in \tet{gpinstall} run in the main thread, which allows all nodes to use the
797
new function.
798
799
\fun{void}{pari_mt_init}{void} \label{pari_mt_init}
800
when using MPI, it is often necessary to run initialization code on the child
801
nodes after PARI is initialized. This is done by calling successively:
802
803
\item \tet{pari_init_opts} with the flag \tet{INIT_noIMTm}:
804
this initializes PARI, but not the MT engine;
805
806
\item the required initialization code;
807
808
\item \tet{pari_mt_init} to initialize the MT engine.
809
Note that under MPI, this function returns on the master node but enters
810
slave mode on the child nodes. Thus it is no longer possible to run
811
initialization code on the child nodes.
812
813
\fun{void}{pari_mt_close}{void} \label{pari_mt_close}
814
when using MPI, calling \tet{pari_close} terminates the MPI execution
815
environment and it will not be possible to restart it. If this is
816
undesirable, call \tet{pari_close_opts} with the flag \tet{INIT_noIMTm}
817
instead of \kbd{pari\_close}: this closes PARI without terminating the MPI
818
execution environment. You may later call \kbd{pari\_mt\_close} to terminate
819
it. It is an error for a program to end without terminating the MPI execution
820
environment.
821
822
\section{A complete example}
823
824
We now proceed to an example exhibiting complex features of this
825
interface, in particular showing how to generate a valid \kbd{worker}.
826
Explanations and details follow.
827
828
\bprogfile{../examples/pari-mt.c}
829
830
We start from some arbitrary C function \kbd{Cworker} and create an
831
\kbd{entree} summarizing all that GP would need to know about it, in
832
particular
833
834
\item a GP name \kbd{\_worker}; the leading \kbd{\_} is not necessary,
835
we use it as a namespace mechanism grouping private functions;
836
837
\item the name of the C function;
838
839
\item and its prototype, see \kbd{install} for an introduction to Prototype
840
Codes.
841
842
\noindent The other three arguments ($0$, $20$ and \kbd{""}) are required in an
843
\kbd{entree} but not useful in our simple context: they are respectively a
844
valence ($0$ means ``nothing special''), a help section (20 is customary for
845
internal functions which need to be exported for technical reasons, see
846
\kbd{?20}), and a help text (no help).
847
848
Then we initialize the MT engine; doing things in this order with a two part
849
initialization ensures that nodes have access to our \kbd{Cworker}. We
850
convert the \kbd{ep} data to a \typ{CLOSURE} using \kbd{strtofunction}, which
851
provides a valid \kbd{worker} to \kbd{mt\_queue\_start}. This creates a
852
parallel evaluation queue \kbd{mt}, and we proceed to submit all tasks,
853
recording all results. Results are stored in the right order
854
by making good use of the \kbd{taskid} label, although we have no control
855
over \emph{when} each result is returned. We finally free all ressources
856
attached to the \kbd{mt} structure. If needed, we could have collected all
857
garbage on the PARI stack using \kbd{gerepilecopy} on the \kbd{out} array and
858
gone on working instead of quitting.
859
860
Note the argument passing convention for \kbd{Cworker}: the task consists of a
861
single vector containing all arguments as \kbd{GEN}s, which are interpreted
862
according to the function prototype, here \kbd{GL} so the first argument is
863
left as is and the second one is converted to a long integer. In more
864
complicated situations, this second (and possibly further) argument could
865
provide arbitrary evaluation contexts. In this example, we just used it as a
866
flag to indicate the kind of evaluation expected on the data: integer
867
factorization (0) or matrix determinant (1).
868
869
Note also that
870
\bprog
871
gel(out, taskid) = mt_queue_get(&mt, &taskid, &pending);
872
@eprog \noindent instead of our use of a temporary \kbd{done} would have
873
undefined behaviour (\kbd{taskid} may be uninitialized in the left hand side).
874
\vfill\eject
875
\input index\end
876
877