Bill Allombert on Thu, 05 Jan 2006 20:21:47 +0100


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

Re: PARI and POSIX threads


Hello PARI-dev,

Here a new patch against current CVS for --enable-tls support. This one
is smaller due to lot of fix in PARI 2.2.12 proper fix issues with the
MPQS engine.

I expect all the basic library functions to work fine in multi-threaded 
programms (excluding e.g. the interpretor and the I/O facility), but 
in practice this patch is entirely untested outside very small test
cases.

See previous post here for example on usage.

Cheers,
Bill.
Index: Configure
===================================================================
RCS file: /home/cvs/pari/Configure,v
retrieving revision 1.148
diff -u -r1.148 Configure
--- Configure	8 Nov 2005 14:09:34 -0000	1.148
+++ Configure	5 Jan 2006 18:49:43 -0000
@@ -143,6 +143,7 @@
   sizeof_long doubleformat\
   X11 X11_INC X11_LIBS which_graphic_lib\
   FLTKDIR FLTK_LIBS QTDIR QTLIB\
+  enable_tls\
   $_test_list\
   $_install_list\
   $_perl_list\
Index: config/get_config_options
===================================================================
RCS file: /home/cvs/pari/config/get_config_options,v
retrieving revision 1.29
diff -u -r1.29 get_config_options
--- config/get_config_options	14 Dec 2005 17:08:53 -0000	1.29
+++ config/get_config_options	5 Jan 2006 18:49:43 -0000
@@ -105,6 +105,9 @@
   --with-fltk) with_fltk=yes ;;
   --with-fltk=*)
       with_fltk=`echo "$1" | sed -e 's/[-a-z]*=//'` ;;
+  --enable-tls) enable_tls=yes;;
+  --enable-tls=*) enable_tls=`echo "$1" | sed -e 's/[-a-z]*=//'` ;;
+  --disable-tls) enable_tls=no;;
   *) echo "*** Unrecognized option $1." >&2; error=true;;
   esac
   shift
@@ -143,6 +146,7 @@
 Additional developer options:
   -g              creates debugging version (in Oxxx.dbg)
   -pg             creates profiling version (in Oxxx.prf)
+  --enable-tls	  (*experimental*) enable thread-local stack
 
 Installation directories:
   --prefix=<dir>        install files in <dir> (default $prefix)
Index: config/paricfg.h.SH
===================================================================
RCS file: /home/cvs/pari/config/paricfg.h.SH,v
retrieving revision 1.26
diff -u -r1.26 paricfg.h.SH
--- config/paricfg.h.SH	9 Nov 2005 18:21:51 -0000	1.26
+++ config/paricfg.h.SH	5 Jan 2006 18:49:43 -0000
@@ -195,4 +195,8 @@
 yes) echo '#define HAS_STAT' >> $file;;
 esac
 
+case $enable_tls in
+yes) echo '#define ENABLE_TLS' >> $file;;
+esac
+
 echo '#endif' >> $file
Index: src/basemath/trans1.c
===================================================================
RCS file: /home/cvs/pari/src/basemath/trans1.c,v
retrieving revision 1.205
diff -u -r1.205 trans1.c
--- src/basemath/trans1.c	15 Dec 2005 13:37:08 -0000	1.205
+++ src/basemath/trans1.c	5 Jan 2006 18:49:43 -0000
@@ -29,11 +29,20 @@
 # define CBRTVERYBIGINT  1291
 #endif
 
-static GEN glog2;
+static THREAD GEN glog2;
 void
 pari_init_floats(void)
 {
   geuler = gpi = bernzone = glog2 = NULL;
+}
+
+void
+pari_close_floats(void)
+{
+  if (geuler) gunclone(geuler);
+  if (gpi) gunclone(gpi);
+  if (bernzone) gunclone(bernzone);
+  if (glog2) gunclone(glog2);
 }
 
 /********************************************************************/
Index: src/headers/paricom.h
===================================================================
RCS file: /home/cvs/pari/src/headers/paricom.h,v
retrieving revision 1.95
diff -u -r1.95 paricom.h
--- src/headers/paricom.h	13 Dec 2005 18:42:48 -0000	1.95
+++ src/headers/paricom.h	5 Jan 2006 18:49:43 -0000
@@ -97,7 +97,7 @@
 /* Common global variables: */
 extern ulong DEBUGFILES, DEBUGLEVEL, DEBUGMEM, precdl;
 extern long  *ordvar;
-extern GEN   bernzone,gpi,geuler;
+extern THREAD GEN  bernzone,gpi,geuler;
 extern GEN   polvar,*pol_1,*pol_x,primetab;
 extern GEN   gen_m1,gen_1,gen_2,ghalf,gi,gen_0,gnil;
 
Index: src/headers/paridecl.h
===================================================================
RCS file: /home/cvs/pari/src/headers/paridecl.h,v
retrieving revision 1.559
diff -u -r1.559 paridecl.h
--- src/headers/paridecl.h	17 Dec 2005 16:10:11 -0000	1.559
+++ src/headers/paridecl.h	5 Jan 2006 18:49:43 -0000
@@ -1249,6 +1249,8 @@
 void    pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts);
 void    pari_init(size_t parisize, ulong maxprime);
 void    pari_sig_init(void (*f)(int));
+void    pari_thread_init(size_t parisize);
+void    pari_thread_close(void);
 void    pari_warn(long numerr, ...);
 GEN     reorder(GEN x);
 GEN     shallowcopy(GEN x);
Index: src/headers/paripriv.h
===================================================================
RCS file: /home/cvs/pari/src/headers/paripriv.h,v
retrieving revision 1.97
diff -u -r1.97 paripriv.h
--- src/headers/paripriv.h	15 Dec 2005 12:39:22 -0000	1.97
+++ src/headers/paripriv.h	5 Jan 2006 18:49:43 -0000
@@ -826,6 +826,7 @@
 GEN     padic_sqrtn_ram(GEN x, long e);
 GEN     padic_sqrtn_unram(GEN x, GEN n, GEN *zetan);
 void    pari_init_floats(void);
+void    pari_close_floats(void);
 GEN     rootsof1complex(GEN n, long prec);
 GEN     rootsof1padic(GEN n, GEN y);
 
Index: src/headers/paristio.h
===================================================================
RCS file: /home/cvs/pari/src/headers/paristio.h,v
retrieving revision 1.34
diff -u -r1.34 paristio.h
--- src/headers/paristio.h	14 Dec 2005 16:42:48 -0000	1.34
+++ src/headers/paristio.h	5 Jan 2006 18:49:43 -0000
@@ -74,7 +74,7 @@
 #define TEXSTYLE_PAREN	2
 #define TEXSTYLE_BREAK	4
 
-extern pari_sp avma,bot,top;
+extern pari_sp THREAD avma,bot,top;
 #define DISABLE_MEMUSED (size_t)-1
 extern size_t memused;
 extern byteptr diffptr;
Index: src/headers/parisys.h
===================================================================
RCS file: /home/cvs/pari/src/headers/parisys.h,v
retrieving revision 1.12
diff -u -r1.12 parisys.h
--- src/headers/parisys.h	23 Nov 2005 12:25:48 -0000	1.12
+++ src/headers/parisys.h	5 Jan 2006 18:49:43 -0000
@@ -62,6 +62,11 @@
 #  define INLINE_IS_STATIC
 #  define INLINE static
 #endif
+#ifdef ENABLE_TLS
+#  define THREAD __thread
+#else
+#  define THREAD
+#endif
 
 #if defined(_WIN32) || defined(__CYGWIN32__)
 /* ANSI C does not allow to longjmp() out of a signal handler, in particular,
Index: src/kernel/none/mp_indep.c
===================================================================
RCS file: /home/cvs/pari/src/kernel/none/mp_indep.c,v
retrieving revision 1.32
diff -u -r1.32 mp_indep.c
--- src/kernel/none/mp_indep.c	12 Dec 2005 08:27:00 -0000	1.32
+++ src/kernel/none/mp_indep.c	5 Jan 2006 18:49:43 -0000
@@ -610,7 +610,7 @@
 /**                         RANDOM INTEGERS                        **/
 /**                                                                **/
 /********************************************************************/
-static long pari_randseed = 1;
+static THREAD long pari_randseed = 1;
 
 /* BSD rand gives this: seed = 1103515245*seed + 12345 */
 /*Return 31 ``random'' bits.*/
Index: src/language/init.c
===================================================================
RCS file: /home/cvs/pari/src/language/init.c,v
retrieving revision 1.296
diff -u -r1.296 init.c
--- src/language/init.c	15 Dec 2005 12:39:22 -0000	1.296
+++ src/language/init.c	5 Jan 2006 18:49:43 -0000
@@ -29,7 +29,7 @@
 #endif
 
 GEN     gnil, gen_0, gen_1, gen_m1, gen_2, ghalf, gi;
-GEN     gpi, geuler, bernzone;
+THREAD GEN     gpi, geuler, bernzone;
 GEN     primetab; /* private primetable */
 byteptr diffptr;
 FILE    *pari_outfile, *errfile, *logfile, *infile;
@@ -44,7 +44,7 @@
 long    *ordvar;
 GEN     polvar, *pol_1, *pol_x;
 
-pari_sp bot, top, avma;
+THREAD pari_sp bot, top, avma;
 size_t memused;
 void    *global_err_data;
 
@@ -585,6 +585,20 @@
   return gp_init_entrees(new_fun_set? MODULES: OLDMODULES, functions_hash);
 }
 
+void
+pari_thread_init(size_t parisize)
+{
+  init_stack(parisize);
+  pari_init_floats();
+}
+
+void
+pari_thread_close(void)
+{
+  free((void *)bot);
+  pari_close_floats();
+}
+
 /* initialize PARI data. Initialize [new|old]fun to NULL for default set. */
 void
 pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
@@ -1993,9 +2007,9 @@
 TIMERstart(pari_timer *T) { (void)TIMER(T); }
 
 long
-timer(void)   { static pari_timer T; return TIMER(&T);}
+timer(void)   { static THREAD pari_timer T; return TIMER(&T);}
 long
-timer2(void)  { static pari_timer T; return TIMER(&T);}
+timer2(void)  { static THREAD pari_timer T; return TIMER(&T);}
 
 void
 msgTIMER(pari_timer *T, char *format, ...)
Index: src/modules/mpqs.c
===================================================================
RCS file: /home/cvs/pari/src/modules/mpqs.c,v
retrieving revision 1.72
diff -u -r1.72 mpqs.c
--- src/modules/mpqs.c	31 Dec 2005 17:38:58 -0000	1.72
+++ src/modules/mpqs.c	5 Jan 2006 18:49:43 -0000
@@ -76,6 +76,12 @@
 #include "pari.h"
 #include "paripriv.h"
 
+#ifdef ENABLE_TLS
+static THREAD char *saveptr;
+#define strtok(x,y) strtok_r(x,y,&saveptr)
+#endif
+
+/** DEBUG **/
 /* #define MPQS_DEBUG_VERBOSE 1 */
 /* histograms are pretty, but don't help performance after all (see below) */
 /* #define MPQS_USE_HISTOGRAMS */
@@ -355,9 +361,9 @@
 /* our own pointer to PARI's or to our own prime diffs table.
  * NB the latter is never freed, unless we need to replace it with
  * an even larger one. */
-static byteptr mpqs_diffptr = NULL;
-static long mpqs_prime_count = 0;
-static int mpqs_use_our_diffptr = 0;
+static THREAD byteptr mpqs_diffptr = NULL;
+static THREAD long mpqs_prime_count = 0;
+static THREAD int mpqs_use_our_diffptr = 0;
 
 /* return next prime larger than p, using *primes_ptr on the diffptr table
  * first and pari's other wits after that */