Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - mt - pthread.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29115-f22e516b23) Lines: 215 227 94.7 %
Date: 2024-03-28 08:06:56 Functions: 21 23 91.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2013  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : #include <pthread.h>
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : #include "mt.h"
      18             : #if defined(_WIN32)
      19             : #  include "../systems/mingw/mingw.h"
      20             : #endif
      21             : 
      22             : #define DEBUGLEVEL DEBUGLEVEL_mt
      23             : 
      24             : struct mt_queue
      25             : {
      26             :   long no;
      27             :   pari_sp avma;
      28             :   struct pari_mainstack *mainstack;
      29             :   GEN input, output;
      30             :   GEN worker;
      31             :   long workid;
      32             :   pthread_cond_t cond;
      33             :   pthread_mutex_t mut;
      34             :   pthread_cond_t *pcond;
      35             :   pthread_mutex_t *pmut;
      36             : };
      37             : 
      38             : struct mt_pstate
      39             : {
      40             :   pthread_t *th;
      41             :   struct pari_thread *pth;
      42             :   struct mt_queue *mq;
      43             :   long n, nbint, last;
      44             :   long pending;
      45             :   pthread_cond_t pcond;
      46             :   pthread_mutex_t pmut;
      47             : };
      48             : 
      49             : static THREAD long mt_thread_no = -1;
      50             : static struct mt_pstate *pari_mt;
      51             : 
      52             : #define LOCK(x) pthread_mutex_lock(x); do
      53             : #define UNLOCK(x) while(0); pthread_mutex_unlock(x)
      54             : 
      55             : void
      56   197086631 : mt_sigint_block(void)
      57             : {
      58   197086631 :   if (mt_thread_no>=0)
      59    27750182 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      60   198738686 : }
      61             : 
      62             : void
      63   197626385 : mt_sigint_unblock(void)
      64             : {
      65   197626385 :   if (mt_thread_no>=0)
      66    28265697 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      67   198689918 : }
      68             : 
      69             : void
      70        1730 : mt_err_recover(long er)
      71             : {
      72        1730 :   if (mt_thread_no>=0)
      73             :   {
      74          11 :     struct mt_pstate *mt = pari_mt;
      75          11 :     struct mt_queue *mq = mt->mq+mt_thread_no;
      76          11 :     GEN err = pari_err_last();
      77          11 :     err = err_get_num(err)==e_STACK ? err_e_STACK: bin_copy(copy_bin(err));
      78          11 :     LOCK(mq->pmut)
      79             :     {
      80          11 :       mq->output = err;
      81          11 :       pthread_cond_signal(mq->pcond);
      82          11 :     } UNLOCK(mq->pmut);
      83           9 :     pthread_exit((void*)1);
      84             :   }
      85        1719 :   else mtsingle_err_recover(er);
      86        1719 : }
      87             : 
      88             : void
      89           0 : mt_break_recover(void)
      90             : {
      91           0 :   if (mt_thread_no<0) mtsingle_err_recover(0);
      92           0 : }
      93             : 
      94             : void
      95           0 : mt_sigint(void)
      96             : {
      97           0 :   if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
      98           0 : }
      99             : 
     100             : int
     101      215805 : mt_is_parallel(void)
     102             : {
     103      215805 :   return !!pari_mt;
     104             : }
     105             : 
     106             : int
     107    30898440 : mt_is_thread(void)
     108             : {
     109    30898440 :   return mt_thread_no>=0 ? 1: mtsingle_is_thread();
     110             : }
     111             : 
     112             : long
     113      381637 : mt_nbthreads(void)
     114             : {
     115      381637 :   return pari_mt ? 1: pari_mt_nbthreads;
     116             : }
     117             : 
     118             : void
     119      314872 : mt_thread_init(void) { mt_thread_no = 0; }
     120             : 
     121             : void
     122          13 : mt_export_add(const char *str, GEN val)
     123             : {
     124          13 :   if (pari_mt)
     125           0 :     pari_err(e_MISC,"export() not allowed during parallel sections");
     126          13 :   export_add(str, val);
     127          13 : }
     128             : 
     129             : void
     130           8 : mt_export_del(const char *str)
     131             : {
     132           8 :   if (pari_mt)
     133           0 :     pari_err(e_MISC,"unexport() not allowed during parallel sections");
     134           8 :   export_del(str);
     135           8 : }
     136             : 
     137           1 : void mt_broadcast(GEN code) {(void) code;}
     138             : 
     139         256 : void pari_mt_init(void)
     140             : {
     141         256 :   pari_mt = NULL;
     142             : #ifdef _SC_NPROCESSORS_CONF
     143         256 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     144             : #elif defined(_WIN32)
     145             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     146             : #else
     147             :   pari_mt_nbthreads = 1;
     148             : #endif
     149         256 : }
     150             : 
     151         256 : void pari_mt_close(void) { }
     152             : 
     153             : static void
     154      315849 : mt_queue_cleanup(void *arg)
     155             : {
     156             :   (void) arg;
     157      315849 :   pari_thread_close();
     158      314443 : }
     159             : 
     160             : static void
     161      315671 : mt_queue_unlock(void *arg)
     162      315671 : { pthread_mutex_unlock((pthread_mutex_t*) arg); }
     163             : 
     164             : static void*
     165      316533 : mt_queue_run(void *arg)
     166             : {
     167      316533 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     168      314563 :   pari_sp av = avma;
     169      314563 :   struct mt_queue *mq = (struct mt_queue *) args;
     170      314563 :   mt_thread_no = mq->no;
     171      314563 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     172      314925 :   LOCK(mq->pmut)
     173             :   {
     174      316677 :     mq->mainstack = pari_mainstack;
     175      316677 :     mq->avma = av;
     176      316677 :     pthread_cond_signal(mq->pcond);
     177      316677 :   } UNLOCK(mq->pmut);
     178             :   for(;;)
     179      427048 :   {
     180             :     GEN work, done;
     181      743665 :     LOCK(&mq->mut)
     182             :     {
     183      743618 :       pthread_cleanup_push(mt_queue_unlock, &mq->mut);
     184     1141546 :       while(!mq->input)
     185      714690 :         pthread_cond_wait(&mq->cond, &mq->mut);
     186      426856 :       pthread_cleanup_pop(0);
     187      426799 :     } UNLOCK(&mq->mut);
     188      426818 :     pari_mainstack = mq->mainstack;
     189      426818 :     set_avma(mq->avma);
     190      426538 :     work = mq->input;
     191      426538 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     192      427032 :     done = closure_callgenvec(mq->worker,work);
     193      425563 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     194      426851 :     LOCK(mq->pmut)
     195             :     {
     196      427081 :       mq->mainstack = pari_mainstack;
     197      427081 :       mq->avma = av;
     198      427081 :       mq->input = NULL;
     199      427081 :       mq->output = done;
     200      427081 :       pthread_cond_signal(mq->pcond);
     201      427081 :     } UNLOCK(mq->pmut);
     202             :   }
     203             :   pthread_cleanup_pop(1);
     204             : #ifdef __GNUC__
     205             :   return NULL; /* LCOV_EXCL_LINE */
     206             : #endif
     207             : }
     208             : 
     209             : static long
     210      611779 : mt_queue_check(struct mt_pstate *mt)
     211             : {
     212             :   long i;
     213     4811147 :   for(i=0; i<mt->n; i++)
     214             :   {
     215     4626452 :     struct mt_queue *mq = mt->mq+i;
     216     4626452 :     if (mq->output) return i;
     217             :   }
     218      184695 :   return -1;
     219             : }
     220             : 
     221             : static GEN
     222      687455 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     223             : {
     224      687455 :   struct mt_pstate *mt = pari_mt;
     225             :   struct mt_queue *mq;
     226      687455 :   GEN done = NULL;
     227             :   long last;
     228             :   (void) junk;
     229      687455 :   if (mt->nbint<mt->n)
     230             :   {
     231      260369 :     mt->last = mt->nbint;
     232      260369 :     *pending = mt->pending;
     233      260369 :     return NULL;
     234             :   }
     235      427086 :   BLOCK_SIGINT_START
     236      427086 :   LOCK(&mt->pmut)
     237             :   {
     238      611779 :     while ((last = mt_queue_check(mt)) < 0)
     239             :     {
     240      184695 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     241      184695 :       if (PARI_SIGINT_pending)
     242             :       {
     243           2 :         int sig = PARI_SIGINT_pending;
     244           2 :         PARI_SIGINT_pending = 0;
     245           2 :         pthread_mutex_unlock(&mt->pmut);
     246           2 :         PARI_SIGINT_block = 0;
     247           2 :         raise(sig);
     248           0 :         PARI_SIGINT_block = 1;
     249           0 :         pthread_mutex_lock(&mt->pmut);
     250             :       }
     251             :     }
     252      427084 :   } UNLOCK(&mt->pmut);
     253      427084 :   BLOCK_SIGINT_END
     254      427084 :   mq = mt->mq+last;
     255      427084 :   done = gcopy(mq->output);
     256      427084 :   mq->output = NULL;
     257      427084 :   if (workid) *workid = mq->workid;
     258      427084 :   if (typ(done) == t_ERROR)
     259             :   {
     260           5 :     if (err_get_num(done)==e_STACK)
     261           0 :       pari_err(e_STACKTHREAD);
     262             :     else
     263           5 :       pari_err(0,done);
     264             :   }
     265      427079 :   mt->last = last;
     266      427079 :   mt->pending--;
     267      427079 :   *pending = mt->pending;
     268      427079 :   return done;
     269             : }
     270             : 
     271             : static void
     272      687455 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     273             : {
     274      687455 :   struct mt_pstate *mt = pari_mt;
     275      687455 :   struct mt_queue *mq = mt->mq+mt->last;
     276             :   (void) junk;
     277      687455 :   if (!work) { mt->nbint=mt->n; return; }
     278      427140 :   BLOCK_SIGINT_START
     279      427140 :   if (mt->nbint<mt->n)
     280             :   {
     281      316240 :     mt->nbint++;
     282      316240 :     LOCK(mq->pmut)
     283             :     {
     284      397776 :       while(!mq->avma)
     285       81536 :         pthread_cond_wait(mq->pcond, mq->pmut);
     286      316240 :     } UNLOCK(mq->pmut);
     287             :   }
     288      427140 :   LOCK(&mq->mut)
     289             :   {
     290      427140 :     mq->output = NULL;
     291      427140 :     mq->workid = workid;
     292      427140 :     BLOCK_SIGINT_START
     293             :     {
     294      427140 :       pari_sp av = avma;
     295      427140 :       struct pari_mainstack *st = pari_mainstack;
     296      427140 :       pari_mainstack = mq->mainstack;
     297      427140 :       set_avma(mq->avma);
     298      427140 :       mq->input = gcopy(work);
     299      427140 :       mq->avma = avma;
     300      427140 :       mq->mainstack = pari_mainstack;
     301      427140 :       pari_mainstack = st;
     302      427140 :       set_avma(av);
     303             :     }
     304      427140 :     BLOCK_SIGINT_END
     305      427140 :     pthread_cond_signal(&mq->cond);
     306      427140 :   } UNLOCK(&mq->mut);
     307      427140 :   mt->pending++;
     308      427140 :   BLOCK_SIGINT_END
     309             : }
     310             : 
     311             : void
     312       56216 : mt_queue_reset(void)
     313             : {
     314       56216 :   struct mt_pstate *mt = pari_mt;
     315             :   long i;
     316       56216 :   BLOCK_SIGINT_START
     317      372893 :   for (i=0; i<mt->n; i++)
     318      316677 :     pthread_cancel(mt->th[i]);
     319      372893 :   for (i=0; i<mt->n; i++)
     320      316677 :     pthread_join(mt->th[i],NULL);
     321       56216 :   pari_mt = NULL;
     322       56216 :   BLOCK_SIGINT_END
     323       56216 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     324      372893 :   for (i=0;i<mt->n;i++)
     325             :   {
     326      316677 :     struct mt_queue *mq = mt->mq+i;
     327      316677 :     pthread_cond_destroy(&mq->cond);
     328      316677 :     pthread_mutex_destroy(&mq->mut);
     329      316677 :     pari_thread_free(&mt->pth[i]);
     330             :   }
     331       56216 :   pari_free(mt->mq);
     332       56216 :   pari_free(mt->pth);
     333       56216 :   pari_free(mt->th);
     334       56216 :   pari_free(mt);
     335       56216 : }
     336             : 
     337             : static long
     338       56216 : closure_has_clone(GEN fun)
     339             : {
     340       56216 :   if (isclone(fun)) return 1;
     341       56210 :   if (lg(fun) >= 8)
     342             :   {
     343       55651 :     GEN f = closure_get_frame(fun);
     344       55651 :     long i, l = lg(f);
     345      202938 :     for (i = 1; i < l; i++)
     346      148778 :       if (isclone(gel(f,i))) return 1;
     347             :   }
     348       54719 :   return 0;
     349             : }
     350             : 
     351             : void
     352      127918 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     353             : {
     354      127918 :   if (lim==0) lim = pari_mt_nbthreads;
     355      127899 :   else        lim = minss(pari_mt_nbthreads, lim);
     356      127917 :   if (mt_thread_no >= 0)
     357       41779 :     mtsequential_queue_start(pt, worker);
     358       86138 :   else if (pari_mt || lim <= 1)
     359       29922 :     mtsingle_queue_start(pt, worker);
     360             :   else
     361             :   {
     362             :     struct mt_pstate *mt =
     363       56216 :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     364       56216 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     365       56216 :     long mtparisizemax = GP_DATA->threadsizemax;
     366             :     long i;
     367       56216 :     if (closure_has_clone(worker))
     368        1497 :       worker = gcopy(worker); /* to avoid clone_lock race */
     369       56216 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     370       56216 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     371       56216 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     372       56216 :     mt->pending = 0;
     373       56216 :     mt->n = lim;
     374       56216 :     mt->nbint = 0;
     375       56216 :     mt->last = 0;
     376       56216 :     pthread_cond_init(&mt->pcond,NULL);
     377       56216 :     pthread_mutex_init(&mt->pmut,NULL);
     378      372893 :     for (i=0;i<lim;i++)
     379             :     {
     380      316677 :       struct mt_queue *mq = mt->mq+i;
     381      316677 :       mq->no     = i;
     382      316677 :       mq->avma = 0;
     383      316677 :       mq->mainstack = NULL;
     384      316677 :       mq->worker = worker;
     385      316677 :       mq->input  = NULL;
     386      316677 :       mq->output = NULL;
     387      316677 :       mq->pcond  = &mt->pcond;
     388      316677 :       mq->pmut   = &mt->pmut;
     389      316677 :       pthread_cond_init(&mq->cond,NULL);
     390      316677 :       pthread_mutex_init(&mq->mut,NULL);
     391      316677 :       if (mtparisizemax)
     392           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     393             :       else
     394      316677 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     395             :     }
     396       56216 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     397       56216 :     BLOCK_SIGINT_START
     398      372893 :     for (i=0;i<lim;i++)
     399      316677 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     400       56216 :     pari_mt = mt;
     401       56216 :     BLOCK_SIGINT_END
     402       56216 :     pt->get=&mtpthread_queue_get;
     403       56216 :     pt->submit=&mtpthread_queue_submit;
     404       56216 :     pt->end=&mt_queue_reset;
     405             :   }
     406      127917 : }

Generated by: LCOV version 1.14