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.8.0 lcov report (development 19623-dc26710) Lines: 169 176 96.0 %
Date: 2016-09-30 05:54:20 Functions: 15 16 93.8 %
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             :   GEN input, output;
      26             :   GEN worker;
      27             :   long workid;
      28             :   pthread_cond_t cond;
      29             :   pthread_mutex_t mut;
      30             :   pthread_cond_t *pcond;
      31             :   pthread_mutex_t *pmut;
      32             : };
      33             : 
      34             : struct mt_pstate
      35             : {
      36             :   pthread_t *th;
      37             :   struct pari_thread *pth;
      38             :   struct mt_queue *mq;
      39             :   long n, nbint, last;
      40             :   long pending;
      41             :   pthread_cond_t pcond;
      42             :   pthread_mutex_t pmut;
      43             : };
      44             : 
      45             : static THREAD long mt_thread_no = -1;
      46             : static struct mt_pstate *pari_mt;
      47             : 
      48             : #define LOCK(x) pthread_mutex_lock(x); do
      49             : #define UNLOCK(x) while(0); pthread_mutex_unlock(x)
      50             : 
      51             : void
      52    79890449 : mt_sigint_block(void)
      53             : {
      54    79890449 :   if (mt_thread_no>=0)
      55     4972432 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      56    80816228 : }
      57             : 
      58             : void
      59    80230116 : mt_sigint_unblock(void)
      60             : {
      61    80230116 :   if (mt_thread_no>=0)
      62     5284989 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      63    80919983 : }
      64             : 
      65             : void
      66        1008 : mt_err_recover(long er)
      67             : {
      68             :   (void) er;
      69        1008 :   if (mt_thread_no>=0)
      70             :   {
      71           2 :     struct mt_pstate *mt = pari_mt;
      72           2 :     struct mt_queue *mq = mt->mq+mt_thread_no;
      73           2 :     GEN err = bin_copy(copy_bin(pari_err_last()));
      74           2 :     LOCK(mq->pmut)
      75             :     {
      76           2 :       mq->output = err;
      77           2 :       pthread_cond_signal(mq->pcond);
      78           2 :     } UNLOCK(mq->pmut);
      79           2 :     pthread_exit((void*)1);
      80             :   }
      81        1006 : }
      82             : 
      83             : void
      84           0 : mt_sigint(void)
      85             : {
      86           0 :   if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
      87           0 : }
      88             : 
      89             : int
      90      189852 : mt_is_parallel(void)
      91             : {
      92      189852 :   return !!pari_mt;
      93             : }
      94             : 
      95             : int
      96    11897661 : mt_is_thread(void)
      97             : {
      98    11897661 :   return mt_thread_no>=0;
      99             : }
     100             : 
     101           1 : void mt_broadcast(GEN code) {(void) code;}
     102             : 
     103         195 : void pari_mt_init(void)
     104             : {
     105         195 :   pari_mt = NULL;
     106             : #ifdef _SC_NPROCESSORS_CONF
     107         195 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     108             : #elif defined(_WIN32)
     109             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     110             : #else
     111             :   pari_mt_nbthreads = 1;
     112             : #endif
     113         195 : }
     114             : 
     115         195 : void pari_mt_close(void) { }
     116             : 
     117             : static void
     118      203729 : mt_queue_cleanup(void *arg)
     119             : {
     120             :   (void) arg;
     121      203729 :   pari_thread_close();
     122      202019 : }
     123             : 
     124             : static void*
     125      201418 : mt_queue_run(void *arg)
     126             : {
     127      201418 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     128      203923 :   pari_sp av = avma;
     129      203923 :   struct mt_queue *mq = (struct mt_queue *) args;
     130      203923 :   mt_thread_no = mq->no;
     131      203923 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     132      204069 :   LOCK(mq->pmut)
     133             :   {
     134      204112 :     mq->avma = av;
     135      204112 :     pthread_cond_signal(mq->pcond);
     136      204112 :   } UNLOCK(mq->pmut);
     137             :   for(;;)
     138             :   {
     139             :     GEN work, done;
     140      285083 :     LOCK(&mq->mut)
     141             :     {
     142      648776 :       while(!mq->input)
     143      283126 :         pthread_cond_wait(&mq->cond, &mq->mut);
     144       80642 :     } UNLOCK(&mq->mut);
     145       80931 :     avma = mq->avma;
     146       80931 :     work = mq->input;
     147       80931 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     148       80858 :     done = closure_callgenvec(mq->worker,work);
     149       79810 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     150       80655 :     LOCK(mq->pmut)
     151             :     {
     152       80998 :       mq->avma = av;
     153       80998 :       mq->input = NULL;
     154       80998 :       mq->output = done;
     155       80998 :       pthread_cond_signal(mq->pcond);
     156       80998 :     } UNLOCK(mq->pmut);
     157       80988 :   }
     158             :   pthread_cleanup_pop(1);
     159             : #ifdef __GNUC__
     160             :   return NULL; /* NOT REACHED */
     161             : #endif
     162             : }
     163             : 
     164             : static long
     165      156128 : mt_queue_check(struct mt_pstate *mt)
     166             : {
     167             :   long i;
     168     1641016 :   for(i=0; i<mt->n; i++)
     169             :   {
     170     1565886 :     struct mt_queue *mq = mt->mq+i;
     171     1565886 :     if (mq->output) return i;
     172             :   }
     173       75130 :   return -1;
     174             : }
     175             : 
     176             : static GEN
     177      129478 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     178             : {
     179      129478 :   struct mt_pstate *mt = pari_mt;
     180             :   struct mt_queue *mq;
     181      129478 :   GEN done = NULL;
     182             :   long last;
     183             :   (void) junk;
     184      129478 :   if (mt->nbint<mt->n) { mt->last = mt->nbint; *pending = mt->pending; return NULL; }
     185       81000 :   if ((last = mt_queue_check(mt)) < 0)
     186             :   {
     187       32884 :     BLOCK_SIGINT_START
     188       32884 :     LOCK(&mt->pmut)
     189             :     {
     190      108012 :       while ((last = mt_queue_check(mt)) < 0)
     191             :       {
     192       42246 :         pthread_cond_wait(&mt->pcond, &mt->pmut);
     193       42246 :         if (PARI_SIGINT_pending)
     194             :         {
     195           2 :           int sig = PARI_SIGINT_pending;
     196           2 :           PARI_SIGINT_pending = 0;
     197           2 :           pthread_mutex_unlock(&mt->pmut);
     198           2 :           PARI_SIGINT_block = 0;
     199           2 :           raise(sig);
     200           0 :           PARI_SIGINT_block = 1;
     201           0 :           pthread_mutex_lock(&mt->pmut);
     202             :         }
     203             :       }
     204       32882 :     } UNLOCK(&mt->pmut);
     205       32882 :     BLOCK_SIGINT_END
     206             :   }
     207       80998 :   mq = mt->mq+last;
     208       80998 :   if (mq->output==err_e_STACK) pari_err(e_STACKTHREAD);
     209       80998 :   done = gcopy(mq->output);
     210       80998 :   mq->output = NULL;
     211       80998 :   if (workid) *workid = mq->workid;
     212       80998 :   if (typ(done)==t_ERROR) pari_err(0,done);
     213       80996 :   mt->last = last;
     214       80996 :   mt->pending--;
     215       80996 :   *pending = mt->pending;
     216       80996 :   return done;
     217             : }
     218             : 
     219             : static void
     220      129478 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     221             : {
     222      129478 :   struct mt_pstate *mt = pari_mt;
     223      129478 :   struct mt_queue *mq = mt->mq+mt->last;
     224             :   (void) junk;
     225      258956 :   if (!work) { mt->nbint=mt->n; return; }
     226       81048 :   BLOCK_SIGINT_START
     227       81048 :   if (mt->nbint<mt->n)
     228             :   {
     229       48964 :     mt->nbint++;
     230       48964 :     if (!mq->avma)
     231             :     {
     232       23019 :       LOCK(mq->pmut)
     233             :       {
     234      158730 :         while(!mq->avma)
     235      112692 :           pthread_cond_wait(mq->pcond, mq->pmut);
     236       23019 :       } UNLOCK(mq->pmut);
     237             :     }
     238             :   }
     239       81048 :   LOCK(&mq->mut)
     240             :   {
     241       81048 :     mq->output = NULL;
     242       81048 :     mq->workid = workid;
     243       81048 :     mq->input = gcopy_avma(work, &mq->avma);
     244       81048 :     pthread_cond_signal(&mq->cond);
     245       81048 :   } UNLOCK(&mq->mut);
     246       81048 :   mt->pending++;
     247       81048 :   BLOCK_SIGINT_END
     248             : }
     249             : 
     250             : void
     251       12757 : mt_queue_reset(void)
     252             : {
     253       12757 :   struct mt_pstate *mt = pari_mt;
     254             :   long i;
     255       12757 :   BLOCK_SIGINT_START
     256      216869 :   for (i=0; i<mt->n; i++)
     257      204112 :     pthread_cancel(mt->th[i]);
     258      216869 :   for (i=0; i<mt->n; i++)
     259      204112 :     pthread_join(mt->th[i],NULL);
     260       12757 :   pari_mt = NULL;
     261       12757 :   BLOCK_SIGINT_END
     262       12757 :   if (DEBUGLEVEL) pari_warn(warner,"stop threads");
     263      216869 :   for (i=0;i<mt->n;i++)
     264             :   {
     265      204112 :     struct mt_queue *mq = mt->mq+i;
     266      204112 :     pthread_cond_destroy(&mq->cond);
     267      204112 :     pthread_mutex_destroy(&mq->mut);
     268      204112 :     pari_thread_free(&mt->pth[i]);
     269             :   }
     270       12757 :   pari_free(mt->mq);
     271       12757 :   pari_free(mt->pth);
     272       12757 :   pari_free(mt->th);
     273       12757 :   pari_free(mt);
     274       12757 : }
     275             : 
     276             : void
     277       12757 : mt_queue_start(struct pari_mt *pt, GEN worker)
     278             : {
     279       12757 :   if (pari_mt)
     280           0 :     mtsingle_queue_start(pt, worker);
     281             :   else
     282             :   {
     283       12757 :     long NBT = pari_mt_nbthreads;
     284       12757 :     struct mt_pstate *mt =
     285             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     286       12757 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     287       12757 :     long mtparisizemax = GP_DATA->threadsizemax;
     288             :     long i;
     289       12757 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*NBT);
     290       12757 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*NBT);
     291       12757 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*NBT);
     292       12757 :     mt->pending = 0;
     293       12757 :     mt->n = NBT;
     294       12757 :     mt->nbint = 0;
     295       12757 :     mt->last = 0;
     296       12757 :     pthread_cond_init(&mt->pcond,NULL);
     297       12757 :     pthread_mutex_init(&mt->pmut,NULL);
     298       12757 :     pari_thread_sync();
     299      216869 :     for (i=0;i<NBT;i++)
     300             :     {
     301      204112 :       struct mt_queue *mq = mt->mq+i;
     302      204112 :       mq->no     = i;
     303      204112 :       mq->avma   = 0;
     304      204112 :       mq->worker = worker;
     305      204112 :       mq->input  = NULL;
     306      204112 :       mq->output = NULL;
     307      204112 :       mq->pcond  = &mt->pcond;
     308      204112 :       mq->pmut   = &mt->pmut;
     309      204112 :       pthread_cond_init(&mq->cond,NULL);
     310      204112 :       pthread_mutex_init(&mq->mut,NULL);
     311      204112 :       if (mtparisizemax)
     312           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     313             :       else
     314      204112 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     315             :     }
     316       12757 :     if (DEBUGLEVEL) pari_warn(warner,"start threads");
     317       12757 :     BLOCK_SIGINT_START
     318      216869 :     for (i=0;i<NBT;i++)
     319      204112 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     320       12757 :     pari_mt = mt;
     321       12757 :     BLOCK_SIGINT_END
     322       12757 :     pt->get=&mtpthread_queue_get;
     323       12757 :     pt->submit=&mtpthread_queue_submit;
     324       12757 :     pt->end=&mt_queue_reset;
     325             :   }
     326       12757 : }

Generated by: LCOV version 1.11