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 21348-d75f58f) Lines: 186 194 95.9 %
Date: 2017-11-20 06:21:05 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    99984791 : mt_sigint_block(void)
      54             : {
      55    99984791 :   if (mt_thread_no>=0)
      56     5606012 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   100899279 : }
      58             : 
      59             : void
      60   100355544 : mt_sigint_unblock(void)
      61             : {
      62   100355544 :   if (mt_thread_no>=0)
      63     5958325 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   100985719 : }
      65             : 
      66             : void
      67        1162 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1162 :   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 = bin_copy(copy_bin(pari_err_last()));
      75           2 :     LOCK(mq->pmut)
      76             :     {
      77           2 :       mq->output = err;
      78           2 :       pthread_cond_signal(mq->pcond);
      79           2 :     } UNLOCK(mq->pmut);
      80           1 :     pthread_exit((void*)1);
      81             :   }
      82        1160 : }
      83             : 
      84             : void
      85           0 : mt_sigint(void)
      86             : {
      87           0 :   if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
      88           0 : }
      89             : 
      90             : int
      91      199897 : mt_is_parallel(void)
      92             : {
      93      199897 :   return !!pari_mt;
      94             : }
      95             : 
      96             : int
      97    14748210 : mt_is_thread(void)
      98             : {
      99    14748210 :   return mt_thread_no>=0;
     100             : }
     101             : 
     102           0 : void mt_broadcast(GEN code) {(void) code;}
     103             : 
     104         216 : void pari_mt_init(void)
     105             : {
     106         216 :   pari_mt = NULL;
     107             : #ifdef _SC_NPROCESSORS_CONF
     108         216 :   if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
     109             : #elif defined(_WIN32)
     110             :   if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
     111             : #else
     112             :   pari_mt_nbthreads = 1;
     113             : #endif
     114         216 : }
     115             : 
     116         216 : void pari_mt_close(void) { }
     117             : 
     118             : static void
     119       91442 : mt_queue_cleanup(void *arg)
     120             : {
     121             :   (void) arg;
     122       91442 :   pari_thread_close();
     123       90856 : }
     124             : 
     125             : static void*
     126       90696 : mt_queue_run(void *arg)
     127             : {
     128       90696 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     129       91486 :   pari_sp av = avma;
     130       91486 :   struct mt_queue *mq = (struct mt_queue *) args;
     131       91486 :   mt_thread_no = mq->no;
     132       91486 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     133       91564 :   LOCK(mq->pmut)
     134             :   {
     135       91600 :     mq->mainstack = pari_mainstack;
     136       91600 :     mq->avma = av;
     137       91600 :     pthread_cond_signal(mq->pcond);
     138       91600 :   } UNLOCK(mq->pmut);
     139             :   for(;;)
     140             :   {
     141             :     GEN work, done;
     142      236915 :     LOCK(&mq->mut)
     143             :     {
     144      615963 :       while(!mq->input)
     145      234580 :         pthread_cond_wait(&mq->cond, &mq->mut);
     146      144631 :     } UNLOCK(&mq->mut);
     147      145208 :     pari_mainstack = mq->mainstack;
     148      145208 :     avma = mq->avma;
     149      145208 :     work = mq->input;
     150      145208 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     151      145005 :     done = closure_callgenvec(mq->worker,work);
     152      143900 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     153      144862 :     LOCK(mq->pmut)
     154             :     {
     155      145350 :       mq->mainstack = pari_mainstack;
     156      145350 :       mq->avma = av;
     157      145350 :       mq->input = NULL;
     158      145350 :       mq->output = done;
     159      145350 :       pthread_cond_signal(mq->pcond);
     160      145350 :     } UNLOCK(mq->pmut);
     161      145321 :   }
     162             :   pthread_cleanup_pop(1);
     163             : #ifdef __GNUC__
     164             :   return NULL; /* LCOV_EXCL_LINE */
     165             : #endif
     166             : }
     167             : 
     168             : static long
     169      224384 : mt_queue_check(struct mt_pstate *mt)
     170             : {
     171             :   long i;
     172     2030968 :   for(i=0; i<mt->n; i++)
     173             :   {
     174     1951934 :     struct mt_queue *mq = mt->mq+i;
     175     1951934 :     if (mq->output) return i;
     176             :   }
     177       79034 :   return -1;
     178             : }
     179             : 
     180             : static GEN
     181      224926 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     182             : {
     183      224926 :   struct mt_pstate *mt = pari_mt;
     184             :   struct mt_queue *mq;
     185      224926 :   GEN done = NULL;
     186             :   long last;
     187             :   (void) junk;
     188      224926 :   if (mt->nbint<mt->n)
     189             :   {
     190       79574 :     mt->last = mt->nbint;
     191       79574 :     *pending = mt->pending;
     192       79574 :     return NULL;
     193             :   }
     194      145352 :   BLOCK_SIGINT_START
     195      145352 :   LOCK(&mt->pmut)
     196             :   {
     197      369736 :     while ((last = mt_queue_check(mt)) < 0)
     198             :     {
     199       79034 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     200       79034 :       if (PARI_SIGINT_pending)
     201             :       {
     202           2 :         int sig = PARI_SIGINT_pending;
     203           2 :         PARI_SIGINT_pending = 0;
     204           2 :         pthread_mutex_unlock(&mt->pmut);
     205           2 :         PARI_SIGINT_block = 0;
     206           2 :         raise(sig);
     207           0 :         PARI_SIGINT_block = 1;
     208           0 :         pthread_mutex_lock(&mt->pmut);
     209             :       }
     210             :     }
     211      145350 :   } UNLOCK(&mt->pmut);
     212      145350 :   BLOCK_SIGINT_END
     213      145350 :   mq = mt->mq+last;
     214      145350 :   done = gcopy(mq->output);
     215      145350 :   mq->output = NULL;
     216      145350 :   if (workid) *workid = mq->workid;
     217      145350 :   if (typ(done) == t_ERROR)
     218             :   {
     219           2 :     if (err_get_num(done)==e_STACK)
     220           0 :       pari_err(e_STACKTHREAD);
     221             :     else
     222           2 :       pari_err(0,done);
     223             :   }
     224      145348 :   mt->last = last;
     225      145348 :   mt->pending--;
     226      145348 :   *pending = mt->pending;
     227      145348 :   return done;
     228             : }
     229             : 
     230             : static void
     231      224926 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     232             : {
     233      224926 :   struct mt_pstate *mt = pari_mt;
     234      224926 :   struct mt_queue *mq = mt->mq+mt->last;
     235             :   (void) junk;
     236      449852 :   if (!work) { mt->nbint=mt->n; return; }
     237      145400 :   BLOCK_SIGINT_START
     238      145400 :   if (mt->nbint<mt->n)
     239             :   {
     240       91318 :     mt->nbint++;
     241       91318 :     LOCK(mq->pmut)
     242             :     {
     243      253397 :       while(!mq->avma)
     244       70761 :         pthread_cond_wait(mq->pcond, mq->pmut);
     245       91318 :     } UNLOCK(mq->pmut);
     246             :   }
     247      145400 :   LOCK(&mq->mut)
     248             :   {
     249      145400 :     mq->output = NULL;
     250      145400 :     mq->workid = workid;
     251      145400 :     BLOCK_SIGINT_START
     252             :     {
     253      145400 :       pari_sp av = avma;
     254      145400 :       struct pari_mainstack *st = pari_mainstack;
     255      145400 :       pari_mainstack = mq->mainstack;
     256      145400 :       avma = mq->avma;
     257      145400 :       mq->input = gcopy(work);
     258      145400 :       mq->avma = avma;
     259      145400 :       mq->mainstack = pari_mainstack;
     260      145400 :       pari_mainstack = st;
     261      145400 :       avma = av;
     262             :     }
     263      145400 :     BLOCK_SIGINT_END
     264      145400 :     pthread_cond_signal(&mq->cond);
     265      145400 :   } UNLOCK(&mq->mut);
     266      145400 :   mt->pending++;
     267      145400 :   BLOCK_SIGINT_END
     268             : }
     269             : 
     270             : void
     271       11790 : mt_queue_reset(void)
     272             : {
     273       11790 :   struct mt_pstate *mt = pari_mt;
     274             :   long i;
     275       11790 :   BLOCK_SIGINT_START
     276      103390 :   for (i=0; i<mt->n; i++)
     277       91600 :     pthread_cancel(mt->th[i]);
     278      103390 :   for (i=0; i<mt->n; i++)
     279       91600 :     pthread_join(mt->th[i],NULL);
     280       11790 :   pari_mt = NULL;
     281       11790 :   BLOCK_SIGINT_END
     282       11790 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     283      103390 :   for (i=0;i<mt->n;i++)
     284             :   {
     285       91600 :     struct mt_queue *mq = mt->mq+i;
     286       91600 :     pthread_cond_destroy(&mq->cond);
     287       91600 :     pthread_mutex_destroy(&mq->mut);
     288       91600 :     pari_thread_free(&mt->pth[i]);
     289             :   }
     290       11790 :   pari_free(mt->mq);
     291       11790 :   pari_free(mt->pth);
     292       11790 :   pari_free(mt->th);
     293       11790 :   pari_free(mt);
     294       11790 : }
     295             : 
     296             : void
     297       12129 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     298             : {
     299       12129 :   if (lim==0) lim = pari_mt_nbthreads;
     300       12070 :   else        lim = minss(pari_mt_nbthreads, lim);
     301       12129 :   if (pari_mt || lim <= 1)
     302         339 :     mtsingle_queue_start(pt, worker);
     303             :   else
     304             :   {
     305       11790 :     struct mt_pstate *mt =
     306             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     307       11790 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     308       11790 :     long mtparisizemax = GP_DATA->threadsizemax;
     309             :     long i;
     310       11790 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     311       11790 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     312       11790 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     313       11790 :     mt->pending = 0;
     314       11790 :     mt->n = lim;
     315       11790 :     mt->nbint = 0;
     316       11790 :     mt->last = 0;
     317       11790 :     pthread_cond_init(&mt->pcond,NULL);
     318       11790 :     pthread_mutex_init(&mt->pmut,NULL);
     319       11790 :     pari_thread_sync();
     320      103390 :     for (i=0;i<lim;i++)
     321             :     {
     322       91600 :       struct mt_queue *mq = mt->mq+i;
     323       91600 :       mq->no     = i;
     324       91600 :       mq->avma   = 0;
     325       91600 :       mq->mainstack = NULL;
     326       91600 :       mq->worker = worker;
     327       91600 :       mq->input  = NULL;
     328       91600 :       mq->output = NULL;
     329       91600 :       mq->pcond  = &mt->pcond;
     330       91600 :       mq->pmut   = &mt->pmut;
     331       91600 :       pthread_cond_init(&mq->cond,NULL);
     332       91600 :       pthread_mutex_init(&mq->mut,NULL);
     333       91600 :       if (mtparisizemax)
     334           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     335             :       else
     336       91600 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     337             :     }
     338       11790 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     339       11790 :     BLOCK_SIGINT_START
     340      103390 :     for (i=0;i<lim;i++)
     341       91600 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     342       11790 :     pari_mt = mt;
     343       11790 :     BLOCK_SIGINT_END
     344       11790 :     pt->get=&mtpthread_queue_get;
     345       11790 :     pt->submit=&mtpthread_queue_submit;
     346       11790 :     pt->end=&mt_queue_reset;
     347             :   }
     348       12129 : }

Generated by: LCOV version 1.11