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.12.0 lcov report (development 23690-5d6e28857) Lines: 203 214 94.9 %
Date: 2019-03-18 05:43:21 Functions: 17 19 89.5 %
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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include <pthread.h>
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : #include "mt.h"
      17             : #if defined(_WIN32)
      18             : #  include "../systems/mingw/mingw.h"
      19             : #endif
      20             : 
      21             : struct mt_queue
      22             : {
      23             :   long no;
      24             :   pari_sp avma;
      25             :   struct pari_mainstack *mainstack;
      26             :   GEN input, output;
      27             :   GEN worker;
      28             :   long workid;
      29             :   pthread_cond_t cond;
      30             :   pthread_mutex_t mut;
      31             :   pthread_cond_t *pcond;
      32             :   pthread_mutex_t *pmut;
      33             : };
      34             : 
      35             : struct mt_pstate
      36             : {
      37             :   pthread_t *th;
      38             :   struct pari_thread *pth;
      39             :   struct mt_queue *mq;
      40             :   long n, nbint, last;
      41             :   long pending;
      42             :   pthread_cond_t pcond;
      43             :   pthread_mutex_t pmut;
      44             : };
      45             : 
      46             : static THREAD long mt_thread_no = -1;
      47             : static struct mt_pstate *pari_mt;
      48             : 
      49             : #define LOCK(x) pthread_mutex_lock(x); do
      50             : #define UNLOCK(x) while(0); pthread_mutex_unlock(x)
      51             : 
      52             : void
      53   110633783 : mt_sigint_block(void)
      54             : {
      55   110633783 :   if (mt_thread_no>=0)
      56     3086170 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   111882038 : }
      58             : 
      59             : void
      60   111192318 : mt_sigint_unblock(void)
      61             : {
      62   111192318 :   if (mt_thread_no>=0)
      63     3632778 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   112111744 : }
      65             : 
      66             : void
      67        1322 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1322 :   if (mt_thread_no>=0)
      71             :   {
      72           2 :     struct mt_pstate *mt = pari_mt;
      73           2 :     struct mt_queue *mq = mt->mq+mt_thread_no;
      74           2 :     GEN err = pari_err_last();
      75           2 :     err = err_get_num(err)==e_STACK ? err_e_STACK: bin_copy(copy_bin(err));
      76           2 :     LOCK(mq->pmut)
      77             :     {
      78           2 :       mq->output = err;
      79           2 :       pthread_cond_signal(mq->pcond);
      80           2 :     } UNLOCK(mq->pmut);
      81           0 :     pthread_exit((void*)1);
      82             :   }
      83        1320 : }
      84             : 
      85             : void
      86           0 : mt_sigint(void)
      87             : {
      88           0 :   if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
      89           0 : }
      90             : 
      91             : int
      92      202503 : mt_is_parallel(void)
      93             : {
      94      202503 :   return !!pari_mt;
      95             : }
      96             : 
      97             : int
      98    27791048 : mt_is_thread(void)
      99             : {
     100    27791048 :   return mt_thread_no>=0;
     101             : }
     102             : 
     103             : void
     104           2 : mt_export_add(const char *str, GEN val)
     105             : {
     106           2 :   if (pari_mt)
     107           0 :     pari_err(e_MISC,"export() not allowed during parallel sections");
     108           2 :   export_add(str, val);
     109           2 : }
     110             : 
     111             : void
     112           2 : mt_export_del(const char *str)
     113             : {
     114           2 :   if (pari_mt)
     115           0 :     pari_err(e_MISC,"unexport() not allowed during parallel sections");
     116           2 :   export_del(str);
     117           2 : }
     118             : 
     119           0 : void mt_broadcast(GEN code) {(void) code;}
     120             : 
     121         224 : void pari_mt_init(void)
     122             : {
     123         224 :   pari_mt = NULL;
     124             : #ifdef _SC_NPROCESSORS_CONF
     125         224 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     126             : #elif defined(_WIN32)
     127             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     128             : #else
     129             :   pari_mt_nbthreads = 1;
     130             : #endif
     131         224 : }
     132             : 
     133         224 : void pari_mt_close(void) { }
     134             : 
     135             : static void
     136      121116 : mt_queue_cleanup(void *arg)
     137             : {
     138             :   (void) arg;
     139      121116 :   pari_thread_close();
     140      120389 : }
     141             : 
     142             : static void*
     143      121552 : mt_queue_run(void *arg)
     144             : {
     145      121552 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     146      121461 :   pari_sp av = avma;
     147      121461 :   struct mt_queue *mq = (struct mt_queue *) args;
     148      121461 :   mt_thread_no = mq->no;
     149      121461 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     150      121555 :   LOCK(mq->pmut)
     151             :   {
     152      121765 :     mq->mainstack = pari_mainstack;
     153      121765 :     mq->avma = av;
     154      121765 :     pthread_cond_signal(mq->pcond);
     155      121765 :   } UNLOCK(mq->pmut);
     156             :   for(;;)
     157      163583 :   {
     158             :     GEN work, done;
     159      285338 :     LOCK(&mq->mut)
     160             :     {
     161      722645 :       while(!mq->input)
     162      273731 :         pthread_cond_wait(&mq->cond, &mq->mut);
     163      163668 :     } UNLOCK(&mq->mut);
     164      163718 :     pari_mainstack = mq->mainstack;
     165      163718 :     set_avma(mq->avma);
     166      163579 :     work = mq->input;
     167      163579 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     168      163714 :     done = closure_callgenvec(mq->worker,work);
     169      156993 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     170      162650 :     LOCK(mq->pmut)
     171             :     {
     172      163681 :       mq->mainstack = pari_mainstack;
     173      163681 :       mq->avma = av;
     174      163681 :       mq->input = NULL;
     175      163681 :       mq->output = done;
     176      163681 :       pthread_cond_signal(mq->pcond);
     177      163681 :     } UNLOCK(mq->pmut);
     178             :   }
     179             :   pthread_cleanup_pop(1);
     180             : #ifdef __GNUC__
     181             :   return NULL; /* LCOV_EXCL_LINE */
     182             : #endif
     183             : }
     184             : 
     185             : static long
     186      264077 : mt_queue_check(struct mt_pstate *mt)
     187             : {
     188             :   long i;
     189     3699962 :   for(i=0; i<mt->n; i++)
     190             :   {
     191     3599566 :     struct mt_queue *mq = mt->mq+i;
     192     3599566 :     if (mq->output) return i;
     193             :   }
     194      100396 :   return -1;
     195             : }
     196             : 
     197             : static GEN
     198      272722 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     199             : {
     200      272722 :   struct mt_pstate *mt = pari_mt;
     201             :   struct mt_queue *mq;
     202      272722 :   GEN done = NULL;
     203             :   long last;
     204             :   (void) junk;
     205      272722 :   if (mt->nbint<mt->n)
     206             :   {
     207      109039 :     mt->last = mt->nbint;
     208      109039 :     *pending = mt->pending;
     209      109039 :     return NULL;
     210             :   }
     211      163683 :   BLOCK_SIGINT_START
     212      163683 :   LOCK(&mt->pmut)
     213             :   {
     214      427760 :     while ((last = mt_queue_check(mt)) < 0)
     215             :     {
     216      100396 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     217      100396 :       if (PARI_SIGINT_pending)
     218             :       {
     219           2 :         int sig = PARI_SIGINT_pending;
     220           2 :         PARI_SIGINT_pending = 0;
     221           2 :         pthread_mutex_unlock(&mt->pmut);
     222           2 :         PARI_SIGINT_block = 0;
     223           2 :         raise(sig);
     224           0 :         PARI_SIGINT_block = 1;
     225           0 :         pthread_mutex_lock(&mt->pmut);
     226             :       }
     227             :     }
     228      163681 :   } UNLOCK(&mt->pmut);
     229      163681 :   BLOCK_SIGINT_END
     230      163681 :   mq = mt->mq+last;
     231      163681 :   done = gcopy(mq->output);
     232      163681 :   mq->output = NULL;
     233      163681 :   if (workid) *workid = mq->workid;
     234      163681 :   if (typ(done) == t_ERROR)
     235             :   {
     236           2 :     if (err_get_num(done)==e_STACK)
     237           0 :       pari_err(e_STACKTHREAD);
     238             :     else
     239           2 :       pari_err(0,done);
     240             :   }
     241      163679 :   mt->last = last;
     242      163679 :   mt->pending--;
     243      163679 :   *pending = mt->pending;
     244      163679 :   return done;
     245             : }
     246             : 
     247             : static void
     248      272722 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     249             : {
     250      272722 :   struct mt_pstate *mt = pari_mt;
     251      272722 :   struct mt_queue *mq = mt->mq+mt->last;
     252             :   (void) junk;
     253      272722 :   if (!work) { mt->nbint=mt->n; return; }
     254      163763 :   BLOCK_SIGINT_START
     255      163763 :   if (mt->nbint<mt->n)
     256             :   {
     257      121263 :     mt->nbint++;
     258      121263 :     LOCK(mq->pmut)
     259             :     {
     260      351122 :       while(!mq->avma)
     261      108596 :         pthread_cond_wait(mq->pcond, mq->pmut);
     262      121263 :     } UNLOCK(mq->pmut);
     263             :   }
     264      163763 :   LOCK(&mq->mut)
     265             :   {
     266      163763 :     mq->output = NULL;
     267      163763 :     mq->workid = workid;
     268      163763 :     BLOCK_SIGINT_START
     269             :     {
     270      163763 :       pari_sp av = avma;
     271      163763 :       struct pari_mainstack *st = pari_mainstack;
     272      163763 :       pari_mainstack = mq->mainstack;
     273      163763 :       set_avma(mq->avma);
     274      163763 :       mq->input = gcopy(work);
     275      163763 :       mq->avma = avma;
     276      163763 :       mq->mainstack = pari_mainstack;
     277      163763 :       pari_mainstack = st;
     278      163763 :       set_avma(av);
     279             :     }
     280      163763 :     BLOCK_SIGINT_END
     281      163763 :     pthread_cond_signal(&mq->cond);
     282      163763 :   } UNLOCK(&mq->mut);
     283      163763 :   mt->pending++;
     284      163763 :   BLOCK_SIGINT_END
     285             : }
     286             : 
     287             : void
     288       12552 : mt_queue_reset(void)
     289             : {
     290       12552 :   struct mt_pstate *mt = pari_mt;
     291             :   long i;
     292       12552 :   BLOCK_SIGINT_START
     293      134317 :   for (i=0; i<mt->n; i++)
     294      121765 :     pthread_cancel(mt->th[i]);
     295      134317 :   for (i=0; i<mt->n; i++)
     296      121765 :     pthread_join(mt->th[i],NULL);
     297       12552 :   pari_mt = NULL;
     298       12552 :   BLOCK_SIGINT_END
     299       12552 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     300      134317 :   for (i=0;i<mt->n;i++)
     301             :   {
     302      121765 :     struct mt_queue *mq = mt->mq+i;
     303      121765 :     pthread_cond_destroy(&mq->cond);
     304      121765 :     pthread_mutex_destroy(&mq->mut);
     305      121765 :     pari_thread_free(&mt->pth[i]);
     306             :   }
     307       12552 :   pari_free(mt->mq);
     308       12552 :   pari_free(mt->pth);
     309       12552 :   pari_free(mt->th);
     310       12552 :   pari_free(mt);
     311       12552 : }
     312             : 
     313             : static long
     314       12552 : closure_has_clone(GEN fun)
     315             : {
     316       12552 :   if (isclone(fun)) return 1;
     317       12550 :   if (lg(fun) >= 8)
     318             :   {
     319       12546 :     GEN f = closure_get_frame(fun);
     320       12546 :     long i, l = lg(f);
     321       49952 :     for (i = 1; i < l; i++)
     322       37606 :       if (isclone(gel(f,i))) return 1;
     323             :   }
     324       12350 :   return 0;
     325             : }
     326             : 
     327             : void
     328       13088 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     329             : {
     330       13088 :   if (lim==0) lim = pari_mt_nbthreads;
     331       13070 :   else        lim = minss(pari_mt_nbthreads, lim);
     332       13088 :   if (pari_mt || lim <= 1)
     333         536 :     mtsingle_queue_start(pt, worker);
     334             :   else
     335             :   {
     336       12552 :     struct mt_pstate *mt =
     337             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     338       12552 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     339       12552 :     long mtparisizemax = GP_DATA->threadsizemax;
     340             :     long i;
     341       12552 :     if (closure_has_clone(worker))
     342         202 :       worker = gcopy(worker); /* to avoid clone_lock race */
     343       12552 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     344       12552 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     345       12552 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     346       12552 :     mt->pending = 0;
     347       12552 :     mt->n = lim;
     348       12552 :     mt->nbint = 0;
     349       12552 :     mt->last = 0;
     350       12552 :     pthread_cond_init(&mt->pcond,NULL);
     351       12552 :     pthread_mutex_init(&mt->pmut,NULL);
     352      134317 :     for (i=0;i<lim;i++)
     353             :     {
     354      121765 :       struct mt_queue *mq = mt->mq+i;
     355      121765 :       mq->no     = i;
     356      121765 :       mq->avma = 0;
     357      121765 :       mq->mainstack = NULL;
     358      121765 :       mq->worker = worker;
     359      121765 :       mq->input  = NULL;
     360      121765 :       mq->output = NULL;
     361      121765 :       mq->pcond  = &mt->pcond;
     362      121765 :       mq->pmut   = &mt->pmut;
     363      121765 :       pthread_cond_init(&mq->cond,NULL);
     364      121765 :       pthread_mutex_init(&mq->mut,NULL);
     365      121765 :       if (mtparisizemax)
     366           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     367             :       else
     368      121765 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     369             :     }
     370       12552 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     371       12552 :     BLOCK_SIGINT_START
     372      134317 :     for (i=0;i<lim;i++)
     373      121765 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     374       12552 :     pari_mt = mt;
     375       12552 :     BLOCK_SIGINT_END
     376       12552 :     pt->get=&mtpthread_queue_get;
     377       12552 :     pt->submit=&mtpthread_queue_submit;
     378       12552 :     pt->end=&mt_queue_reset;
     379             :   }
     380       13088 : }

Generated by: LCOV version 1.13