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 21203-392a176) Lines: 186 194 95.9 %
Date: 2017-10-23 06:23:29 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   100368347 : mt_sigint_block(void)
      54             : {
      55   100368347 :   if (mt_thread_no>=0)
      56     5885437 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      57   101260062 : }
      58             : 
      59             : void
      60   100754596 : mt_sigint_unblock(void)
      61             : {
      62   100754596 :   if (mt_thread_no>=0)
      63     6238286 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      64   101368414 : }
      65             : 
      66             : void
      67        1130 : mt_err_recover(long er)
      68             : {
      69             :   (void) er;
      70        1130 :   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           2 :     pthread_exit((void*)1);
      81             :   }
      82        1128 : }
      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      199513 : mt_is_parallel(void)
      92             : {
      93      199513 :   return !!pari_mt;
      94             : }
      95             : 
      96             : int
      97    14746960 : mt_is_thread(void)
      98             : {
      99    14746960 :   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      120982 : mt_queue_cleanup(void *arg)
     120             : {
     121             :   (void) arg;
     122      120982 :   pari_thread_close();
     123      120051 : }
     124             : 
     125             : static void*
     126      119617 : mt_queue_run(void *arg)
     127             : {
     128      119617 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     129      121119 :   pari_sp av = avma;
     130      121119 :   struct mt_queue *mq = (struct mt_queue *) args;
     131      121119 :   mt_thread_no = mq->no;
     132      121119 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     133      121184 :   LOCK(mq->pmut)
     134             :   {
     135      121215 :     mq->mainstack = pari_mainstack;
     136      121215 :     mq->avma = av;
     137      121215 :     pthread_cond_signal(mq->pcond);
     138      121215 :   } UNLOCK(mq->pmut);
     139             :   for(;;)
     140             :   {
     141             :     GEN work, done;
     142      296210 :     LOCK(&mq->mut)
     143             :     {
     144      764322 :       while(!mq->input)
     145      294286 :         pthread_cond_wait(&mq->cond, &mq->mut);
     146      173873 :     } UNLOCK(&mq->mut);
     147      174757 :     pari_mainstack = mq->mainstack;
     148      174757 :     avma = mq->avma;
     149      174757 :     work = mq->input;
     150      174757 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     151      174239 :     done = closure_callgenvec(mq->worker,work);
     152      173231 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     153      174479 :     LOCK(mq->pmut)
     154             :     {
     155      175039 :       mq->mainstack = pari_mainstack;
     156      175039 :       mq->avma = av;
     157      175039 :       mq->input = NULL;
     158      175039 :       mq->output = done;
     159      175039 :       pthread_cond_signal(mq->pcond);
     160      175039 :     } UNLOCK(mq->pmut);
     161      175000 :   }
     162             :   pthread_cleanup_pop(1);
     163             : #ifdef __GNUC__
     164             :   return NULL; /* LCOV_EXCL_LINE */
     165             : #endif
     166             : }
     167             : 
     168             : static long
     169      266946 : mt_queue_check(struct mt_pstate *mt)
     170             : {
     171             :   long i;
     172     2250333 :   for(i=0; i<mt->n; i++)
     173             :   {
     174     2158426 :     struct mt_queue *mq = mt->mq+i;
     175     2158426 :     if (mq->output) return i;
     176             :   }
     177       91907 :   return -1;
     178             : }
     179             : 
     180             : static GEN
     181      279441 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     182             : {
     183      279441 :   struct mt_pstate *mt = pari_mt;
     184             :   struct mt_queue *mq;
     185      279441 :   GEN done = NULL;
     186             :   long last;
     187             :   (void) junk;
     188      279441 :   if (mt->nbint<mt->n)
     189             :   {
     190      104400 :     mt->last = mt->nbint;
     191      104400 :     *pending = mt->pending;
     192      104400 :     return NULL;
     193             :   }
     194      175041 :   BLOCK_SIGINT_START
     195      175041 :   LOCK(&mt->pmut)
     196             :   {
     197      441987 :     while ((last = mt_queue_check(mt)) < 0)
     198             :     {
     199       91907 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     200       91907 :       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      175039 :   } UNLOCK(&mt->pmut);
     212      175039 :   BLOCK_SIGINT_END
     213      175039 :   mq = mt->mq+last;
     214      175039 :   done = gcopy(mq->output);
     215      175039 :   mq->output = NULL;
     216      175039 :   if (workid) *workid = mq->workid;
     217      175039 :   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      175037 :   mt->last = last;
     225      175037 :   mt->pending--;
     226      175037 :   *pending = mt->pending;
     227      175037 :   return done;
     228             : }
     229             : 
     230             : static void
     231      279441 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     232             : {
     233      279441 :   struct mt_pstate *mt = pari_mt;
     234      279441 :   struct mt_queue *mq = mt->mq+mt->last;
     235             :   (void) junk;
     236      558882 :   if (!work) { mt->nbint=mt->n; return; }
     237      175089 :   BLOCK_SIGINT_START
     238      175089 :   if (mt->nbint<mt->n)
     239             :   {
     240      120933 :     mt->nbint++;
     241      120933 :     LOCK(mq->pmut)
     242             :     {
     243      347042 :       while(!mq->avma)
     244      105176 :         pthread_cond_wait(mq->pcond, mq->pmut);
     245      120933 :     } UNLOCK(mq->pmut);
     246             :   }
     247      175089 :   LOCK(&mq->mut)
     248             :   {
     249      175089 :     mq->output = NULL;
     250      175089 :     mq->workid = workid;
     251      175089 :     BLOCK_SIGINT_START
     252             :     {
     253      175089 :       pari_sp av = avma;
     254      175089 :       struct pari_mainstack *st = pari_mainstack;
     255      175089 :       pari_mainstack = mq->mainstack;
     256      175089 :       avma = mq->avma;
     257      175089 :       mq->input = gcopy(work);
     258      175089 :       mq->avma = avma;
     259      175089 :       mq->mainstack = pari_mainstack;
     260      175089 :       pari_mainstack = st;
     261      175089 :       avma = av;
     262             :     }
     263      175089 :     BLOCK_SIGINT_END
     264      175089 :     pthread_cond_signal(&mq->cond);
     265      175089 :   } UNLOCK(&mq->mut);
     266      175089 :   mt->pending++;
     267      175089 :   BLOCK_SIGINT_END
     268             : }
     269             : 
     270             : void
     271       16579 : mt_queue_reset(void)
     272             : {
     273       16579 :   struct mt_pstate *mt = pari_mt;
     274             :   long i;
     275       16579 :   BLOCK_SIGINT_START
     276      137794 :   for (i=0; i<mt->n; i++)
     277      121215 :     pthread_cancel(mt->th[i]);
     278      137794 :   for (i=0; i<mt->n; i++)
     279      121215 :     pthread_join(mt->th[i],NULL);
     280       16579 :   pari_mt = NULL;
     281       16579 :   BLOCK_SIGINT_END
     282       16579 :   if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
     283      137794 :   for (i=0;i<mt->n;i++)
     284             :   {
     285      121215 :     struct mt_queue *mq = mt->mq+i;
     286      121215 :     pthread_cond_destroy(&mq->cond);
     287      121215 :     pthread_mutex_destroy(&mq->mut);
     288      121215 :     pari_thread_free(&mt->pth[i]);
     289             :   }
     290       16579 :   pari_free(mt->mq);
     291       16579 :   pari_free(mt->pth);
     292       16579 :   pari_free(mt->th);
     293       16579 :   pari_free(mt);
     294       16579 : }
     295             : 
     296             : void
     297       16918 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
     298             : {
     299       16918 :   if (lim==0) lim = pari_mt_nbthreads;
     300       16859 :   else        lim = minss(pari_mt_nbthreads, lim);
     301       16918 :   if (pari_mt || lim <= 1)
     302         339 :     mtsingle_queue_start(pt, worker);
     303             :   else
     304             :   {
     305       16579 :     struct mt_pstate *mt =
     306             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     307       16579 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     308       16579 :     long mtparisizemax = GP_DATA->threadsizemax;
     309             :     long i;
     310       16579 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
     311       16579 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
     312       16579 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
     313       16579 :     mt->pending = 0;
     314       16579 :     mt->n = lim;
     315       16579 :     mt->nbint = 0;
     316       16579 :     mt->last = 0;
     317       16579 :     pthread_cond_init(&mt->pcond,NULL);
     318       16579 :     pthread_mutex_init(&mt->pmut,NULL);
     319       16579 :     pari_thread_sync();
     320      137794 :     for (i=0;i<lim;i++)
     321             :     {
     322      121215 :       struct mt_queue *mq = mt->mq+i;
     323      121215 :       mq->no     = i;
     324      121215 :       mq->avma   = 0;
     325      121215 :       mq->mainstack = NULL;
     326      121215 :       mq->worker = worker;
     327      121215 :       mq->input  = NULL;
     328      121215 :       mq->output = NULL;
     329      121215 :       mq->pcond  = &mt->pcond;
     330      121215 :       mq->pmut   = &mt->pmut;
     331      121215 :       pthread_cond_init(&mq->cond,NULL);
     332      121215 :       pthread_mutex_init(&mq->mut,NULL);
     333      121215 :       if (mtparisizemax)
     334           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     335             :       else
     336      121215 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     337             :     }
     338       16579 :     if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
     339       16579 :     BLOCK_SIGINT_START
     340      137794 :     for (i=0;i<lim;i++)
     341      121215 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     342       16579 :     pari_mt = mt;
     343       16579 :     BLOCK_SIGINT_END
     344       16579 :     pt->get=&mtpthread_queue_get;
     345       16579 :     pt->submit=&mtpthread_queue_submit;
     346       16579 :     pt->end=&mt_queue_reset;
     347             :   }
     348       16918 : }

Generated by: LCOV version 1.11