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 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.10.0 lcov report (development 22303-eb3e11d) Lines: 187 195 95.9 %
Date: 2018-04-21 06:16:28 Functions: 14 16 87.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   110962436 : mt_sigint_block(void)
      54             : {
      55   110962436 :   if (mt_thread_no>=0)
      56     5094467 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   112230982 : }
      58             : 
      59             : void
      60   111507592 : mt_sigint_unblock(void)
      61             : {
      62   111507592 :   if (mt_thread_no>=0)
      63     5630299 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   112431050 : }
      65             : 
      66             : void
      67        1214 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1214 :   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           2 :     pthread_exit((void*)1);
      82             :   }
      83        1212 : }
      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      200883 : mt_is_parallel(void)
      93             : {
      94      200883 :   return !!pari_mt;
      95             : }
      96             : 
      97             : int
      98    15975957 : mt_is_thread(void)
      99             : {
     100    15975957 :   return mt_thread_no>=0;
     101             : }
     102             : 
     103           0 : void mt_broadcast(GEN code) {(void) code;}
     104             : 
     105         220 : void pari_mt_init(void)
     106             : {
     107         220 :   pari_mt = NULL;
     108             : #ifdef _SC_NPROCESSORS_CONF
     109         220 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     110             : #elif defined(_WIN32)
     111             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     112             : #else
     113             :   pari_mt_nbthreads = 1;
     114             : #endif
     115         220 : }
     116             : 
     117         220 : void pari_mt_close(void) { }
     118             : 
     119             : static void
     120       89094 : mt_queue_cleanup(void *arg)
     121             : {
     122             :   (void) arg;
     123       89094 :   pari_thread_close();
     124       88708 : }
     125             : 
     126             : static void*
     127       87649 : mt_queue_run(void *arg)
     128             : {
     129       87649 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     130       89069 :   pari_sp av = avma;
     131       89069 :   struct mt_queue *mq = (struct mt_queue *) args;
     132       89069 :   mt_thread_no = mq->no;
     133       89069 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     134       89167 :   LOCK(mq->pmut)
     135             :   {
     136       89206 :     mq->mainstack = pari_mainstack;
     137       89206 :     mq->avma = av;
     138       89206 :     pthread_cond_signal(mq->pcond);
     139       89206 :   } UNLOCK(mq->pmut);
     140             :   for(;;)
     141             :   {
     142             :     GEN work, done;
     143      233395 :     LOCK(&mq->mut)
     144             :     {
     145      608455 :       while(!mq->input)
     146      231336 :         pthread_cond_wait(&mq->cond, &mq->mut);
     147      143761 :     } UNLOCK(&mq->mut);
     148      144136 :     pari_mainstack = mq->mainstack;
     149      144136 :     avma = mq->avma;
     150      144136 :     work = mq->input;
     151      144136 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     152      144113 :     done = closure_callgenvec(mq->worker,work);
     153      143219 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     154      143795 :     LOCK(mq->pmut)
     155             :     {
     156      144198 :       mq->mainstack = pari_mainstack;
     157      144198 :       mq->avma = av;
     158      144198 :       mq->input = NULL;
     159      144198 :       mq->output = done;
     160      144198 :       pthread_cond_signal(mq->pcond);
     161      144198 :     } UNLOCK(mq->pmut);
     162      144194 :   }
     163             :   pthread_cleanup_pop(1);
     164             : #ifdef __GNUC__
     165             :   return NULL; /* LCOV_EXCL_LINE */
     166             : #endif
     167             : }
     168             : 
     169             : static long
     170      221817 : mt_queue_check(struct mt_pstate *mt)
     171             : {
     172             :   long i;
     173     2014807 :   for(i=0; i<mt->n; i++)
     174             :   {
     175     1937188 :     struct mt_queue *mq = mt->mq+i;
     176     1937188 :     if (mq->output) return i;
     177             :   }
     178       77619 :   return -1;
     179             : }
     180             : 
     181             : static GEN
     182      222677 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     183             : {
     184      222677 :   struct mt_pstate *mt = pari_mt;
     185             :   struct mt_queue *mq;
     186      222677 :   GEN done = NULL;
     187             :   long last;
     188             :   (void) junk;
     189      222677 :   if (mt->nbint<mt->n)
     190             :   {
     191       78477 :     mt->last = mt->nbint;
     192       78477 :     *pending = mt->pending;
     193       78477 :     return NULL;
     194             :   }
     195      144200 :   BLOCK_SIGINT_START
     196      144200 :   LOCK(&mt->pmut)
     197             :   {
     198      366017 :     while ((last = mt_queue_check(mt)) < 0)
     199             :     {
     200       77619 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     201       77619 :       if (PARI_SIGINT_pending)
     202             :       {
     203           2 :         int sig = PARI_SIGINT_pending;
     204           2 :         PARI_SIGINT_pending = 0;
     205           2 :         pthread_mutex_unlock(&mt->pmut);
     206           2 :         PARI_SIGINT_block = 0;
     207           2 :         raise(sig);
     208           0 :         PARI_SIGINT_block = 1;
     209           0 :         pthread_mutex_lock(&mt->pmut);
     210             :       }
     211             :     }
     212      144198 :   } UNLOCK(&mt->pmut);
     213      144198 :   BLOCK_SIGINT_END
     214      144198 :   mq = mt->mq+last;
     215      144198 :   done = gcopy(mq->output);
     216      144198 :   mq->output = NULL;
     217      144198 :   if (workid) *workid = mq->workid;
     218      144198 :   if (typ(done) == t_ERROR)
     219             :   {
     220           2 :     if (err_get_num(done)==e_STACK)
     221           0 :       pari_err(e_STACKTHREAD);
     222             :     else
     223           2 :       pari_err(0,done);
     224             :   }
     225      144196 :   mt->last = last;
     226      144196 :   mt->pending--;
     227      144196 :   *pending = mt->pending;
     228      144196 :   return done;
     229             : }
     230             : 
     231             : static void
     232      222677 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     233             : {
     234      222677 :   struct mt_pstate *mt = pari_mt;
     235      222677 :   struct mt_queue *mq = mt->mq+mt->last;
     236             :   (void) junk;
     237      445354 :   if (!work) { mt->nbint=mt->n; return; }
     238      144248 :   BLOCK_SIGINT_START
     239      144248 :   if (mt->nbint<mt->n)
     240             :   {
     241       88924 :     mt->nbint++;
     242       88924 :     LOCK(mq->pmut)
     243             :     {
     244      253959 :       while(!mq->avma)
     245       76111 :         pthread_cond_wait(mq->pcond, mq->pmut);
     246       88924 :     } UNLOCK(mq->pmut);
     247             :   }
     248      144248 :   LOCK(&mq->mut)
     249             :   {
     250      144248 :     mq->output = NULL;
     251      144248 :     mq->workid = workid;
     252      144248 :     BLOCK_SIGINT_START
     253             :     {
     254      144248 :       pari_sp av = avma;
     255      144248 :       struct pari_mainstack *st = pari_mainstack;
     256      144248 :       pari_mainstack = mq->mainstack;
     257      144248 :       avma = mq->avma;
     258      144248 :       mq->input = gcopy(work);
     259      144248 :       mq->avma = avma;
     260      144248 :       mq->mainstack = pari_mainstack;
     261      144248 :       pari_mainstack = st;
     262      144248 :       avma = av;
     263             :     }
     264      144248 :     BLOCK_SIGINT_END
     265      144248 :     pthread_cond_signal(&mq->cond);
     266      144248 :   } UNLOCK(&mq->mut);
     267      144248 :   mt->pending++;
     268      144248 :   BLOCK_SIGINT_END
     269             : }
     270             : 
     271             : void
     272       10493 : mt_queue_reset(void)
     273             : {
     274       10493 :   struct mt_pstate *mt = pari_mt;
     275             :   long i;
     276       10493 :   BLOCK_SIGINT_START
     277       99699 :   for (i=0; i<mt->n; i++)
     278       89206 :     pthread_cancel(mt->th[i]);
     279       99699 :   for (i=0; i<mt->n; i++)
     280       89206 :     pthread_join(mt->th[i],NULL);
     281       10493 :   pari_mt = NULL;
     282       10493 :   BLOCK_SIGINT_END
     283       10493 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     284       99699 :   for (i=0;i<mt->n;i++)
     285             :   {
     286       89206 :     struct mt_queue *mq = mt->mq+i;
     287       89206 :     pthread_cond_destroy(&mq->cond);
     288       89206 :     pthread_mutex_destroy(&mq->mut);
     289       89206 :     pari_thread_free(&mt->pth[i]);
     290             :   }
     291       10493 :   pari_free(mt->mq);
     292       10493 :   pari_free(mt->pth);
     293       10493 :   pari_free(mt->th);
     294       10493 :   pari_free(mt);
     295       10493 : }
     296             : 
     297             : void
     298       10828 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     299             : {
     300       10828 :   if (lim==0) lim = pari_mt_nbthreads;
     301       10769 :   else        lim = minss(pari_mt_nbthreads, lim);
     302       10828 :   if (pari_mt || lim <= 1)
     303         335 :     mtsingle_queue_start(pt, worker);
     304             :   else
     305             :   {
     306       10493 :     struct mt_pstate *mt =
     307             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     308       10493 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     309       10493 :     long mtparisizemax = GP_DATA->threadsizemax;
     310             :     long i;
     311       10493 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     312       10493 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     313       10493 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     314       10493 :     mt->pending = 0;
     315       10493 :     mt->n = lim;
     316       10493 :     mt->nbint = 0;
     317       10493 :     mt->last = 0;
     318       10493 :     pthread_cond_init(&mt->pcond,NULL);
     319       10493 :     pthread_mutex_init(&mt->pmut,NULL);
     320       10493 :     pari_thread_sync();
     321       99699 :     for (i=0;i<lim;i++)
     322             :     {
     323       89206 :       struct mt_queue *mq = mt->mq+i;
     324       89206 :       mq->no     = i;
     325       89206 :       mq->avma   = 0;
     326       89206 :       mq->mainstack = NULL;
     327       89206 :       mq->worker = worker;
     328       89206 :       mq->input  = NULL;
     329       89206 :       mq->output = NULL;
     330       89206 :       mq->pcond  = &mt->pcond;
     331       89206 :       mq->pmut   = &mt->pmut;
     332       89206 :       pthread_cond_init(&mq->cond,NULL);
     333       89206 :       pthread_mutex_init(&mq->mut,NULL);
     334       89206 :       if (mtparisizemax)
     335           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     336             :       else
     337       89206 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     338             :     }
     339       10493 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     340       10493 :     BLOCK_SIGINT_START
     341       99699 :     for (i=0;i<lim;i++)
     342       89206 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     343       10493 :     pari_mt = mt;
     344       10493 :     BLOCK_SIGINT_END
     345       10493 :     pt->get=&mtpthread_queue_get;
     346       10493 :     pt->submit=&mtpthread_queue_submit;
     347       10493 :     pt->end=&mt_queue_reset;
     348             :   }
     349       10828 : }

Generated by: LCOV version 1.11