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 23022-5b494f78e) Lines: 186 195 95.4 %
Date: 2018-09-24 05:37:44 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   110679460 : mt_sigint_block(void)
      54             : {
      55   110679460 :   if (mt_thread_no>=0)
      56     3744372 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   113103635 : }
      58             : 
      59             : void
      60   111831465 : mt_sigint_unblock(void)
      61             : {
      62   111831465 :   if (mt_thread_no>=0)
      63     4890963 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   113293935 : }
      65             : 
      66             : void
      67        1258 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1258 :   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        1256 : }
      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      201906 : mt_is_parallel(void)
      93             : {
      94      201906 :   return !!pari_mt;
      95             : }
      96             : 
      97             : int
      98    16021033 : mt_is_thread(void)
      99             : {
     100    16021033 :   return mt_thread_no>=0;
     101             : }
     102             : 
     103           0 : void mt_broadcast(GEN code) {(void) code;}
     104             : 
     105         222 : void pari_mt_init(void)
     106             : {
     107         222 :   pari_mt = NULL;
     108             : #ifdef _SC_NPROCESSORS_CONF
     109         222 :   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         222 : }
     116             : 
     117         222 : void pari_mt_close(void) { }
     118             : 
     119             : static void
     120      111575 : mt_queue_cleanup(void *arg)
     121             : {
     122             :   (void) arg;
     123      111575 :   pari_thread_close();
     124      111155 : }
     125             : 
     126             : static void*
     127      111458 : mt_queue_run(void *arg)
     128             : {
     129      111458 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     130      112137 :   pari_sp av = avma;
     131      112137 :   struct mt_queue *mq = (struct mt_queue *) args;
     132      112137 :   mt_thread_no = mq->no;
     133      112137 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     134      112218 :   LOCK(mq->pmut)
     135             :   {
     136      112351 :     mq->mainstack = pari_mainstack;
     137      112351 :     mq->avma = av;
     138      112351 :     pthread_cond_signal(mq->pcond);
     139      112351 :   } UNLOCK(mq->pmut);
     140             :   for(;;)
     141      147261 :   {
     142             :     GEN work, done;
     143      259600 :     LOCK(&mq->mut)
     144             :     {
     145      660246 :       while(!mq->input)
     146      253414 :         pthread_cond_wait(&mq->cond, &mq->mut);
     147      147245 :     } UNLOCK(&mq->mut);
     148      147276 :     pari_mainstack = mq->mainstack;
     149      147276 :     set_avma(mq->avma);
     150      147204 :     work = mq->input;
     151      147204 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     152      147314 :     done = closure_callgenvec(mq->worker,work);
     153      145792 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     154      147081 :     LOCK(mq->pmut)
     155             :     {
     156      147280 :       mq->mainstack = pari_mainstack;
     157      147280 :       mq->avma = av;
     158      147280 :       mq->input = NULL;
     159      147280 :       mq->output = done;
     160      147280 :       pthread_cond_signal(mq->pcond);
     161      147280 :     } UNLOCK(mq->pmut);
     162             :   }
     163             :   pthread_cleanup_pop(1);
     164             : #ifdef __GNUC__
     165             :   return NULL; /* LCOV_EXCL_LINE */
     166             : #endif
     167             : }
     168             : 
     169             : static long
     170      223203 : mt_queue_check(struct mt_pstate *mt)
     171             : {
     172             :   long i;
     173     2934830 :   for(i=0; i<mt->n; i++)
     174             :   {
     175     2858907 :     struct mt_queue *mq = mt->mq+i;
     176     2858907 :     if (mq->output) return i;
     177             :   }
     178       75923 :   return -1;
     179             : }
     180             : 
     181             : static GEN
     182      247522 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     183             : {
     184      247522 :   struct mt_pstate *mt = pari_mt;
     185             :   struct mt_queue *mq;
     186      247522 :   GEN done = NULL;
     187             :   long last;
     188             :   (void) junk;
     189      247522 :   if (mt->nbint<mt->n)
     190             :   {
     191      100240 :     mt->last = mt->nbint;
     192      100240 :     *pending = mt->pending;
     193      100240 :     return NULL;
     194             :   }
     195      147282 :   BLOCK_SIGINT_START
     196      147282 :   LOCK(&mt->pmut)
     197             :   {
     198      370485 :     while ((last = mt_queue_check(mt)) < 0)
     199             :     {
     200       75923 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     201       75923 :       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      147280 :   } UNLOCK(&mt->pmut);
     213      147280 :   BLOCK_SIGINT_END
     214      147280 :   mq = mt->mq+last;
     215      147280 :   done = gcopy(mq->output);
     216      147280 :   mq->output = NULL;
     217      147280 :   if (workid) *workid = mq->workid;
     218      147280 :   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      147278 :   mt->last = last;
     226      147278 :   mt->pending--;
     227      147278 :   *pending = mt->pending;
     228      147278 :   return done;
     229             : }
     230             : 
     231             : static void
     232      247522 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     233             : {
     234      247522 :   struct mt_pstate *mt = pari_mt;
     235      247522 :   struct mt_queue *mq = mt->mq+mt->last;
     236             :   (void) junk;
     237      247522 :   if (!work) { mt->nbint=mt->n; return; }
     238      147362 :   BLOCK_SIGINT_START
     239      147362 :   if (mt->nbint<mt->n)
     240             :   {
     241      111333 :     mt->nbint++;
     242      111333 :     LOCK(mq->pmut)
     243             :     {
     244      329471 :       while(!mq->avma)
     245      106805 :         pthread_cond_wait(mq->pcond, mq->pmut);
     246      111333 :     } UNLOCK(mq->pmut);
     247             :   }
     248      147362 :   LOCK(&mq->mut)
     249             :   {
     250      147362 :     mq->output = NULL;
     251      147362 :     mq->workid = workid;
     252      147362 :     BLOCK_SIGINT_START
     253             :     {
     254      147362 :       pari_sp av = avma;
     255      147362 :       struct pari_mainstack *st = pari_mainstack;
     256      147362 :       pari_mainstack = mq->mainstack;
     257      147362 :       set_avma(mq->avma);
     258      147362 :       mq->input = gcopy(work);
     259      147362 :       mq->avma = avma;
     260      147362 :       mq->mainstack = pari_mainstack;
     261      147362 :       pari_mainstack = st;
     262      147362 :       set_avma(av);
     263             :     }
     264      147362 :     BLOCK_SIGINT_END
     265      147362 :     pthread_cond_signal(&mq->cond);
     266      147362 :   } UNLOCK(&mq->mut);
     267      147362 :   mt->pending++;
     268      147362 :   BLOCK_SIGINT_END
     269             : }
     270             : 
     271             : void
     272       11139 : mt_queue_reset(void)
     273             : {
     274       11139 :   struct mt_pstate *mt = pari_mt;
     275             :   long i;
     276       11139 :   BLOCK_SIGINT_START
     277      123490 :   for (i=0; i<mt->n; i++)
     278      112351 :     pthread_cancel(mt->th[i]);
     279      123490 :   for (i=0; i<mt->n; i++)
     280      112351 :     pthread_join(mt->th[i],NULL);
     281       11139 :   pari_mt = NULL;
     282       11139 :   BLOCK_SIGINT_END
     283       11139 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     284      123490 :   for (i=0;i<mt->n;i++)
     285             :   {
     286      112351 :     struct mt_queue *mq = mt->mq+i;
     287      112351 :     pthread_cond_destroy(&mq->cond);
     288      112351 :     pthread_mutex_destroy(&mq->mut);
     289      112351 :     pari_thread_free(&mt->pth[i]);
     290             :   }
     291       11139 :   pari_free(mt->mq);
     292       11139 :   pari_free(mt->pth);
     293       11139 :   pari_free(mt->th);
     294       11139 :   pari_free(mt);
     295       11139 : }
     296             : 
     297             : void
     298       11478 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     299             : {
     300       11478 :   if (lim==0) lim = pari_mt_nbthreads;
     301       11418 :   else        lim = minss(pari_mt_nbthreads, lim);
     302       11478 :   if (pari_mt || lim <= 1)
     303         339 :     mtsingle_queue_start(pt, worker);
     304             :   else
     305             :   {
     306       11139 :     struct mt_pstate *mt =
     307             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     308       11139 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     309       11139 :     long mtparisizemax = GP_DATA->threadsizemax;
     310             :     long i;
     311       11139 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     312       11139 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     313       11139 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     314       11139 :     mt->pending = 0;
     315       11139 :     mt->n = lim;
     316       11139 :     mt->nbint = 0;
     317       11139 :     mt->last = 0;
     318       11139 :     pthread_cond_init(&mt->pcond,NULL);
     319       11139 :     pthread_mutex_init(&mt->pmut,NULL);
     320       11139 :     pari_thread_sync();
     321      123490 :     for (i=0;i<lim;i++)
     322             :     {
     323      112351 :       struct mt_queue *mq = mt->mq+i;
     324      112351 :       mq->no     = i;
     325      112351 :       mq->avma = 0;
     326      112351 :       mq->mainstack = NULL;
     327      112351 :       mq->worker = worker;
     328      112351 :       mq->input  = NULL;
     329      112351 :       mq->output = NULL;
     330      112351 :       mq->pcond  = &mt->pcond;
     331      112351 :       mq->pmut   = &mt->pmut;
     332      112351 :       pthread_cond_init(&mq->cond,NULL);
     333      112351 :       pthread_mutex_init(&mq->mut,NULL);
     334      112351 :       if (mtparisizemax)
     335           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     336             :       else
     337      112351 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     338             :     }
     339       11139 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     340       11139 :     BLOCK_SIGINT_START
     341      123490 :     for (i=0;i<lim;i++)
     342      112351 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     343       11139 :     pari_mt = mt;
     344       11139 :     BLOCK_SIGINT_END
     345       11139 :     pt->get=&mtpthread_queue_get;
     346       11139 :     pt->submit=&mtpthread_queue_submit;
     347       11139 :     pt->end=&mt_queue_reset;
     348             :   }
     349       11478 : }

Generated by: LCOV version 1.13