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 23344-f0cc1b3f6) Lines: 194 205 94.6 %
Date: 2018-12-12 05:41:43 Functions: 16 18 88.9 %
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   110481635 : mt_sigint_block(void)
      54             : {
      55   110481635 :   if (mt_thread_no>=0)
      56     3495920 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   112499524 : }
      58             : 
      59             : void
      60   111315525 : mt_sigint_unblock(void)
      61             : {
      62   111315525 :   if (mt_thread_no>=0)
      63     4321540 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   112351529 : }
      65             : 
      66             : void
      67        1300 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1300 :   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        1298 : }
      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      202207 : mt_is_parallel(void)
      93             : {
      94      202207 :   return !!pari_mt;
      95             : }
      96             : 
      97             : int
      98    27787946 : mt_is_thread(void)
      99             : {
     100    27787946 :   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         223 : void pari_mt_init(void)
     122             : {
     123         223 :   pari_mt = NULL;
     124             : #ifdef _SC_NPROCESSORS_CONF
     125         223 :   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         223 : }
     132             : 
     133         223 : void pari_mt_close(void) { }
     134             : 
     135             : static void
     136      116879 : mt_queue_cleanup(void *arg)
     137             : {
     138             :   (void) arg;
     139      116879 :   pari_thread_close();
     140      116419 : }
     141             : 
     142             : static void*
     143      116674 : mt_queue_run(void *arg)
     144             : {
     145      116674 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     146      117500 :   pari_sp av = avma;
     147      117500 :   struct mt_queue *mq = (struct mt_queue *) args;
     148      117500 :   mt_thread_no = mq->no;
     149      117500 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     150      117477 :   LOCK(mq->pmut)
     151             :   {
     152      117657 :     mq->mainstack = pari_mainstack;
     153      117657 :     mq->avma = av;
     154      117657 :     pthread_cond_signal(mq->pcond);
     155      117657 :   } UNLOCK(mq->pmut);
     156             :   for(;;)
     157      152514 :   {
     158             :     GEN work, done;
     159      270168 :     LOCK(&mq->mut)
     160             :     {
     161      685725 :       while(!mq->input)
     162      263132 :         pthread_cond_wait(&mq->cond, &mq->mut);
     163      152458 :     } UNLOCK(&mq->mut);
     164      152497 :     pari_mainstack = mq->mainstack;
     165      152497 :     set_avma(mq->avma);
     166      152433 :     work = mq->input;
     167      152433 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     168      152583 :     done = closure_callgenvec(mq->worker,work);
     169      151308 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     170      152360 :     LOCK(mq->pmut)
     171             :     {
     172      152533 :       mq->mainstack = pari_mainstack;
     173      152533 :       mq->avma = av;
     174      152533 :       mq->input = NULL;
     175      152533 :       mq->output = done;
     176      152533 :       pthread_cond_signal(mq->pcond);
     177      152533 :     } 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      231745 : mt_queue_check(struct mt_pstate *mt)
     187             : {
     188             :   long i;
     189     3025426 :   for(i=0; i<mt->n; i++)
     190             :   {
     191     2946214 :     struct mt_queue *mq = mt->mq+i;
     192     2946214 :     if (mq->output) return i;
     193             :   }
     194       79212 :   return -1;
     195             : }
     196             : 
     197             : static GEN
     198      257328 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     199             : {
     200      257328 :   struct mt_pstate *mt = pari_mt;
     201             :   struct mt_queue *mq;
     202      257328 :   GEN done = NULL;
     203             :   long last;
     204             :   (void) junk;
     205      257328 :   if (mt->nbint<mt->n)
     206             :   {
     207      104793 :     mt->last = mt->nbint;
     208      104793 :     *pending = mt->pending;
     209      104793 :     return NULL;
     210             :   }
     211      152535 :   BLOCK_SIGINT_START
     212      152535 :   LOCK(&mt->pmut)
     213             :   {
     214      384280 :     while ((last = mt_queue_check(mt)) < 0)
     215             :     {
     216       79212 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     217       79212 :       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      152533 :   } UNLOCK(&mt->pmut);
     229      152533 :   BLOCK_SIGINT_END
     230      152533 :   mq = mt->mq+last;
     231      152533 :   done = gcopy(mq->output);
     232      152533 :   mq->output = NULL;
     233      152533 :   if (workid) *workid = mq->workid;
     234      152533 :   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      152531 :   mt->last = last;
     242      152531 :   mt->pending--;
     243      152531 :   *pending = mt->pending;
     244      152531 :   return done;
     245             : }
     246             : 
     247             : static void
     248      257328 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     249             : {
     250      257328 :   struct mt_pstate *mt = pari_mt;
     251      257328 :   struct mt_queue *mq = mt->mq+mt->last;
     252             :   (void) junk;
     253      257328 :   if (!work) { mt->nbint=mt->n; return; }
     254      152615 :   BLOCK_SIGINT_START
     255      152615 :   if (mt->nbint<mt->n)
     256             :   {
     257      116343 :     mt->nbint++;
     258      116343 :     LOCK(mq->pmut)
     259             :     {
     260      348263 :       while(!mq->avma)
     261      115577 :         pthread_cond_wait(mq->pcond, mq->pmut);
     262      116343 :     } UNLOCK(mq->pmut);
     263             :   }
     264      152615 :   LOCK(&mq->mut)
     265             :   {
     266      152615 :     mq->output = NULL;
     267      152615 :     mq->workid = workid;
     268      152615 :     BLOCK_SIGINT_START
     269             :     {
     270      152615 :       pari_sp av = avma;
     271      152615 :       struct pari_mainstack *st = pari_mainstack;
     272      152615 :       pari_mainstack = mq->mainstack;
     273      152615 :       set_avma(mq->avma);
     274      152615 :       mq->input = gcopy(work);
     275      152615 :       mq->avma = avma;
     276      152615 :       mq->mainstack = pari_mainstack;
     277      152615 :       pari_mainstack = st;
     278      152615 :       set_avma(av);
     279             :     }
     280      152615 :     BLOCK_SIGINT_END
     281      152615 :     pthread_cond_signal(&mq->cond);
     282      152615 :   } UNLOCK(&mq->mut);
     283      152615 :   mt->pending++;
     284      152615 :   BLOCK_SIGINT_END
     285             : }
     286             : 
     287             : void
     288       11913 : mt_queue_reset(void)
     289             : {
     290       11913 :   struct mt_pstate *mt = pari_mt;
     291             :   long i;
     292       11913 :   BLOCK_SIGINT_START
     293      129570 :   for (i=0; i<mt->n; i++)
     294      117657 :     pthread_cancel(mt->th[i]);
     295      129570 :   for (i=0; i<mt->n; i++)
     296      117657 :     pthread_join(mt->th[i],NULL);
     297       11913 :   pari_mt = NULL;
     298       11913 :   BLOCK_SIGINT_END
     299       11913 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     300      129570 :   for (i=0;i<mt->n;i++)
     301             :   {
     302      117657 :     struct mt_queue *mq = mt->mq+i;
     303      117657 :     pthread_cond_destroy(&mq->cond);
     304      117657 :     pthread_mutex_destroy(&mq->mut);
     305      117657 :     pari_thread_free(&mt->pth[i]);
     306             :   }
     307       11913 :   pari_free(mt->mq);
     308       11913 :   pari_free(mt->pth);
     309       11913 :   pari_free(mt->th);
     310       11913 :   pari_free(mt);
     311       11913 : }
     312             : 
     313             : void
     314       12449 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     315             : {
     316       12449 :   if (lim==0) lim = pari_mt_nbthreads;
     317       12388 :   else        lim = minss(pari_mt_nbthreads, lim);
     318       12449 :   if (pari_mt || lim <= 1)
     319         536 :     mtsingle_queue_start(pt, worker);
     320             :   else
     321             :   {
     322       11913 :     struct mt_pstate *mt =
     323             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     324       11913 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     325       11913 :     long mtparisizemax = GP_DATA->threadsizemax;
     326             :     long i;
     327       11913 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     328       11913 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     329       11913 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     330       11913 :     mt->pending = 0;
     331       11913 :     mt->n = lim;
     332       11913 :     mt->nbint = 0;
     333       11913 :     mt->last = 0;
     334       11913 :     pthread_cond_init(&mt->pcond,NULL);
     335       11913 :     pthread_mutex_init(&mt->pmut,NULL);
     336       11913 :     pari_thread_sync();
     337      129570 :     for (i=0;i<lim;i++)
     338             :     {
     339      117657 :       struct mt_queue *mq = mt->mq+i;
     340      117657 :       mq->no     = i;
     341      117657 :       mq->avma = 0;
     342      117657 :       mq->mainstack = NULL;
     343      117657 :       mq->worker = worker;
     344      117657 :       mq->input  = NULL;
     345      117657 :       mq->output = NULL;
     346      117657 :       mq->pcond  = &mt->pcond;
     347      117657 :       mq->pmut   = &mt->pmut;
     348      117657 :       pthread_cond_init(&mq->cond,NULL);
     349      117657 :       pthread_mutex_init(&mq->mut,NULL);
     350      117657 :       if (mtparisizemax)
     351           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     352             :       else
     353      117657 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     354             :     }
     355       11913 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     356       11913 :     BLOCK_SIGINT_START
     357      129570 :     for (i=0;i<lim;i++)
     358      117657 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     359       11913 :     pari_mt = mt;
     360       11913 :     BLOCK_SIGINT_END
     361       11913 :     pt->get=&mtpthread_queue_get;
     362       11913 :     pt->submit=&mtpthread_queue_submit;
     363       11913 :     pt->end=&mt_queue_reset;
     364             :   }
     365       12448 : }

Generated by: LCOV version 1.13