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 19355-c7ae729) Lines: 164 171 95.9 %
Date: 2016-08-26 06:12:17 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    79564190 : mt_sigint_block(void)
      53             : {
      54    79564190 :   if (mt_thread_no>=0)
      55     5100941 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      56    80367295 : }
      57             : 
      58             : void
      59    79895131 : mt_sigint_unblock(void)
      60             : {
      61    79895131 :   if (mt_thread_no>=0)
      62     5388842 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      63    80521191 : }
      64             : 
      65             : void
      66         997 : mt_err_recover(long er)
      67             : {
      68             :   (void) er;
      69         997 :   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 :     LOCK(mq->pmut)
      74             :     {
      75           2 :       mq->output = pari_err_last();
      76           2 :       pthread_cond_signal(mq->pcond);
      77           2 :     } UNLOCK(mq->pmut);
      78           1 :     pthread_exit((void*)1);
      79             :   }
      80         995 : }
      81             : 
      82             : void
      83           0 : mt_sigint(void)
      84             : {
      85           0 :   if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
      86           0 : }
      87             : 
      88             : int
      89      189562 : mt_is_parallel(void)
      90             : {
      91      189562 :   return !!pari_mt;
      92             : }
      93             : 
      94             : int
      95    11804734 : mt_is_thread(void)
      96             : {
      97    11804734 :   return mt_thread_no>=0;
      98             : }
      99             : 
     100           1 : void mt_broadcast(GEN code) {(void) code;}
     101             : 
     102         193 : void pari_mt_init(void)
     103             : {
     104         193 :   pari_mt = NULL;
     105             : #ifdef _SC_NPROCESSORS_CONF
     106         193 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     107             : #elif defined(_WIN32)
     108             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     109             : #else
     110             :   pari_mt_nbthreads = 1;
     111             : #endif
     112         193 : }
     113             : 
     114         193 : void pari_mt_close(void) { }
     115             : 
     116             : static void
     117      210423 : mt_queue_cleanup(void *arg)
     118             : {
     119             :   (void) arg;
     120      210423 :   pari_thread_close();
     121      208743 : }
     122             : 
     123             : static void*
     124      208551 : mt_queue_run(void *arg)
     125             : {
     126      208551 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     127      210651 :   pari_sp av = avma;
     128      210651 :   struct mt_queue *mq = (struct mt_queue *) args;
     129      210651 :   mt_thread_no = mq->no;
     130      210651 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     131      210763 :   LOCK(mq->pmut)
     132             :   {
     133      210800 :     mq->avma = av;
     134      210800 :     pthread_cond_signal(mq->pcond);
     135      210800 :   } UNLOCK(mq->pmut);
     136             :   for(;;)
     137             :   {
     138             :     GEN work, done;
     139      292426 :     LOCK(&mq->mut)
     140             :     {
     141      663688 :       while(!mq->input)
     142      290058 :         pthread_cond_wait(&mq->cond, &mq->mut);
     143       81283 :     } UNLOCK(&mq->mut);
     144       81523 :     avma = mq->avma;
     145       81523 :     work = mq->input;
     146       81523 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     147       81406 :     done = closure_callgenvec(mq->worker,work);
     148       79990 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     149       81268 :     LOCK(mq->pmut)
     150             :     {
     151       81664 :       mq->avma = av;
     152       81664 :       mq->input = NULL;
     153       81664 :       mq->output = done;
     154       81664 :       pthread_cond_signal(mq->pcond);
     155       81664 :     } UNLOCK(mq->pmut);
     156       81635 :   }
     157             :   pthread_cleanup_pop(1);
     158             : #ifdef __GNUC__
     159             :   return NULL; /* NOT REACHED */
     160             : #endif
     161             : }
     162             : 
     163             : static long
     164      160313 : mt_queue_check(struct mt_pstate *mt)
     165             : {
     166             :   long i;
     167     1722996 :   for(i=0; i<mt->n; i++)
     168             :   {
     169     1644347 :     struct mt_queue *mq = mt->mq+i;
     170     1644347 :     if (mq->output) return i;
     171             :   }
     172       78649 :   return -1;
     173             : }
     174             : 
     175             : static GEN
     176      130790 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     177             : {
     178      130790 :   struct mt_pstate *mt = pari_mt;
     179             :   struct mt_queue *mq;
     180      130790 :   GEN done = NULL;
     181             :   long last;
     182             :   (void) junk;
     183      130790 :   if (mt->nbint<mt->n) { mt->last = mt->nbint; *pending = mt->pending; return NULL; }
     184       81666 :   if ((last = mt_queue_check(mt)) < 0)
     185             :   {
     186       34392 :     BLOCK_SIGINT_START
     187       34392 :     LOCK(&mt->pmut)
     188             :     {
     189      113039 :       while ((last = mt_queue_check(mt)) < 0)
     190             :       {
     191       44257 :         pthread_cond_wait(&mt->pcond, &mt->pmut);
     192       44257 :         if (PARI_SIGINT_pending)
     193             :         {
     194           2 :           int sig = PARI_SIGINT_pending;
     195           2 :           PARI_SIGINT_pending = 0;
     196           2 :           pthread_mutex_unlock(&mt->pmut);
     197           2 :           PARI_SIGINT_block = 0;
     198           2 :           raise(sig);
     199           0 :           PARI_SIGINT_block = 1;
     200           0 :           pthread_mutex_lock(&mt->pmut);
     201             :         }
     202             :       }
     203       34390 :     } UNLOCK(&mt->pmut);
     204       34390 :     BLOCK_SIGINT_END
     205             :   }
     206       81664 :   mq = mt->mq+last;
     207       81664 :   if (mq->output==err_e_STACK) pari_err(e_STACKTHREAD);
     208       81664 :   done = gcopy(mq->output);
     209       81664 :   mq->output = NULL;
     210       81664 :   if (workid) *workid = mq->workid;
     211       81664 :   if (typ(done)==t_ERROR) pari_err(0,done);
     212       81662 :   mt->last = last;
     213       81662 :   mt->pending--;
     214       81662 :   *pending = mt->pending;
     215       81662 :   return done;
     216             : }
     217             : 
     218             : static void
     219      130790 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     220             : {
     221      130790 :   struct mt_pstate *mt = pari_mt;
     222      130790 :   struct mt_queue *mq = mt->mq+mt->last;
     223             :   (void) junk;
     224      261580 :   if (!work) { mt->nbint=mt->n; return; }
     225       81714 :   BLOCK_SIGINT_START
     226       81714 :   if (mt->nbint<mt->n)
     227             :   {
     228       49605 :     mt->nbint++;
     229       49605 :     if (!mq->avma)
     230             :     {
     231       25477 :       LOCK(mq->pmut)
     232             :       {
     233      173647 :         while(!mq->avma)
     234      122693 :           pthread_cond_wait(mq->pcond, mq->pmut);
     235       25477 :       } UNLOCK(mq->pmut);
     236             :     }
     237             :   }
     238       81714 :   LOCK(&mq->mut)
     239             :   {
     240       81714 :     mq->output = NULL;
     241       81714 :     mq->workid = workid;
     242       81714 :     mq->input = gcopy_avma(work, &mq->avma);
     243       81714 :     pthread_cond_signal(&mq->cond);
     244       81714 :   } UNLOCK(&mq->mut);
     245       81714 :   mt->pending++;
     246       81714 :   BLOCK_SIGINT_END
     247             : }
     248             : 
     249             : void
     250       13175 : mt_queue_reset(void)
     251             : {
     252       13175 :   struct mt_pstate *mt = pari_mt;
     253             :   long i;
     254      223975 :   for (i=0; i<mt->n; i++)
     255      210800 :     pthread_cancel(mt->th[i]);
     256      223975 :   for (i=0; i<mt->n; i++)
     257      210800 :     pthread_join(mt->th[i],NULL);
     258       13175 :   if (DEBUGLEVEL) pari_warn(warner,"stop threads");
     259       13175 :   pari_mt = NULL;
     260      223975 :   for (i=0;i<mt->n;i++)
     261             :   {
     262      210800 :     struct mt_queue *mq = mt->mq+i;
     263      210800 :     pthread_cond_destroy(&mq->cond);
     264      210800 :     pthread_mutex_destroy(&mq->mut);
     265      210800 :     pari_thread_free(&mt->pth[i]);
     266             :   }
     267       13175 :   pari_free(mt->mq);
     268       13175 :   pari_free(mt->pth);
     269       13175 :   pari_free(mt->th);
     270       13175 :   pari_free(mt);
     271       13175 : }
     272             : 
     273             : void
     274       13175 : mt_queue_start(struct pari_mt *pt, GEN worker)
     275             : {
     276       13175 :   if (pari_mt)
     277           0 :     mtsingle_queue_start(pt, worker);
     278             :   else
     279             :   {
     280       13175 :     long NBT = pari_mt_nbthreads;
     281       13175 :     struct mt_pstate *mt =
     282             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     283       13175 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     284       13175 :     long mtparisizemax = GP_DATA->threadsizemax;
     285             :     long i;
     286       13175 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*NBT);
     287       13175 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*NBT);
     288       13175 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*NBT);
     289       13175 :     mt->pending = 0;
     290       13175 :     mt->n = NBT;
     291       13175 :     mt->nbint = 0;
     292       13175 :     mt->last = 0;
     293       13175 :     pari_mt = mt;
     294       13175 :     pthread_cond_init(&mt->pcond,NULL);
     295       13175 :     pthread_mutex_init(&mt->pmut,NULL);
     296       13175 :     pari_thread_sync();
     297      223975 :     for (i=0;i<NBT;i++)
     298             :     {
     299      210800 :       struct mt_queue *mq = mt->mq+i;
     300      210800 :       mq->no     = i;
     301      210800 :       mq->avma   = 0;
     302      210800 :       mq->worker = worker;
     303      210800 :       mq->input  = NULL;
     304      210800 :       mq->output = NULL;
     305      210800 :       mq->pcond  = &mt->pcond;
     306      210800 :       mq->pmut   = &mt->pmut;
     307      210800 :       pthread_cond_init(&mq->cond,NULL);
     308      210800 :       pthread_mutex_init(&mq->mut,NULL);
     309      210800 :       if (mtparisizemax)
     310           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     311             :       else
     312      210800 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     313             :     }
     314       13175 :     if (DEBUGLEVEL) pari_warn(warner,"start threads");
     315      223975 :     for (i=0;i<NBT;i++)
     316      210800 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     317       13175 :     pt->get=&mtpthread_queue_get;
     318       13175 :     pt->submit=&mtpthread_queue_submit;
     319       13175 :     pt->end=&mt_queue_reset;
     320             :   }
     321       13175 : }

Generated by: LCOV version 1.11