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 20084-3366ab4) Lines: 169 177 95.5 %
Date: 2017-01-19 05:50:13 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             :   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    79451015 : mt_sigint_block(void)
      53             : {
      54    79451015 :   if (mt_thread_no>=0)
      55     4919567 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      56    80307108 : }
      57             : 
      58             : void
      59    79946492 : mt_sigint_unblock(void)
      60             : {
      61    79946492 :   if (mt_thread_no>=0)
      62     5377219 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      63    80429866 : }
      64             : 
      65             : void
      66        1074 : mt_err_recover(long er)
      67             : {
      68             :   (void) er;
      69        1074 :   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        1072 : }
      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      190447 : mt_is_parallel(void)
      91             : {
      92      190447 :   return !!pari_mt;
      93             : }
      94             : 
      95             : int
      96    12002385 : mt_is_thread(void)
      97             : {
      98    12002385 :   return mt_thread_no>=0;
      99             : }
     100             : 
     101           0 : void mt_broadcast(GEN code) {(void) code;}
     102             : 
     103         202 : void pari_mt_init(void)
     104             : {
     105         202 :   pari_mt = NULL;
     106             : #ifdef _SC_NPROCESSORS_CONF
     107         202 :   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         202 : }
     114             : 
     115         202 : void pari_mt_close(void) { }
     116             : 
     117             : static void
     118       62715 : mt_queue_cleanup(void *arg)
     119             : {
     120             :   (void) arg;
     121       62715 :   pari_thread_close();
     122       62277 : }
     123             : 
     124             : static void*
     125       61669 : mt_queue_run(void *arg)
     126             : {
     127       61669 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     128       62805 :   pari_sp av = avma;
     129       62805 :   struct mt_queue *mq = (struct mt_queue *) args;
     130       62805 :   mt_thread_no = mq->no;
     131       62805 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     132       62846 :   LOCK(mq->pmut)
     133             :   {
     134       62864 :     mq->avma = av;
     135       62864 :     pthread_cond_signal(mq->pcond);
     136       62864 :   } UNLOCK(mq->pmut);
     137             :   for(;;)
     138             :   {
     139             :     GEN work, done;
     140      127773 :     LOCK(&mq->mut)
     141             :     {
     142      318335 :       while(!mq->input)
     143      126094 :         pthread_cond_wait(&mq->cond, &mq->mut);
     144       64543 :     } UNLOCK(&mq->mut);
     145       64844 :     avma = mq->avma;
     146       64844 :     work = mq->input;
     147       64844 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     148       64738 :     done = closure_callgenvec(mq->worker,work);
     149       63717 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     150       64575 :     LOCK(mq->pmut)
     151             :     {
     152       64935 :       mq->avma = av;
     153       64935 :       mq->input = NULL;
     154       64935 :       mq->output = done;
     155       64935 :       pthread_cond_signal(mq->pcond);
     156       64935 :     } UNLOCK(mq->pmut);
     157       64910 :   }
     158             :   pthread_cleanup_pop(1);
     159             : #ifdef __GNUC__
     160             :   return NULL; /* LCOV_EXCL_LINE */
     161             : #endif
     162             : }
     163             : 
     164             : static long
     165       95412 : mt_queue_check(struct mt_pstate *mt)
     166             : {
     167             :   long i;
     168      896700 :   for(i=0; i<mt->n; i++)
     169             :   {
     170      866223 :     struct mt_queue *mq = mt->mq+i;
     171      866223 :     if (mq->output) return i;
     172             :   }
     173       30477 :   return -1;
     174             : }
     175             : 
     176             : static GEN
     177       91542 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     178             : {
     179       91542 :   struct mt_pstate *mt = pari_mt;
     180             :   struct mt_queue *mq;
     181       91542 :   GEN done = NULL;
     182             :   long last;
     183             :   (void) junk;
     184       91542 :   if (mt->nbint<mt->n)
     185             :   {
     186       26605 :     mt->last = mt->nbint;
     187       26605 :     *pending = mt->pending;
     188       26605 :     return NULL;
     189             :   }
     190       64937 :   BLOCK_SIGINT_START
     191       64937 :   LOCK(&mt->pmut)
     192             :   {
     193      160349 :     while ((last = mt_queue_check(mt)) < 0)
     194             :     {
     195       30477 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     196       30477 :       if (PARI_SIGINT_pending)
     197             :       {
     198           2 :         int sig = PARI_SIGINT_pending;
     199           2 :         PARI_SIGINT_pending = 0;
     200           2 :         pthread_mutex_unlock(&mt->pmut);
     201           2 :         PARI_SIGINT_block = 0;
     202           2 :         raise(sig);
     203           0 :         PARI_SIGINT_block = 1;
     204           0 :         pthread_mutex_lock(&mt->pmut);
     205             :       }
     206             :     }
     207       64935 :   } UNLOCK(&mt->pmut);
     208       64935 :   BLOCK_SIGINT_END
     209       64935 :   mq = mt->mq+last;
     210       64935 :   if (mq->output==err_e_STACK) pari_err(e_STACKTHREAD);
     211       64935 :   done = gcopy(mq->output);
     212       64935 :   mq->output = NULL;
     213       64935 :   if (workid) *workid = mq->workid;
     214       64935 :   if (typ(done)==t_ERROR) pari_err(0,done);
     215       64933 :   mt->last = last;
     216       64933 :   mt->pending--;
     217       64933 :   *pending = mt->pending;
     218       64933 :   return done;
     219             : }
     220             : 
     221             : static void
     222       91542 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     223             : {
     224       91542 :   struct mt_pstate *mt = pari_mt;
     225       91542 :   struct mt_queue *mq = mt->mq+mt->last;
     226             :   (void) junk;
     227      183084 :   if (!work) { mt->nbint=mt->n; return; }
     228       64985 :   BLOCK_SIGINT_START
     229       64985 :   if (mt->nbint<mt->n)
     230             :   {
     231       27112 :     mt->nbint++;
     232       27112 :     LOCK(mq->pmut)
     233             :     {
     234       97502 :       while(!mq->avma)
     235       43278 :         pthread_cond_wait(mq->pcond, mq->pmut);
     236       27112 :     } UNLOCK(mq->pmut);
     237             :   }
     238       64985 :   LOCK(&mq->mut)
     239             :   {
     240       64985 :     mq->output = NULL;
     241       64985 :     mq->workid = workid;
     242       64985 :     mq->input = gcopy_avma(work, &mq->avma);
     243       64985 :     pthread_cond_signal(&mq->cond);
     244       64985 :   } UNLOCK(&mq->mut);
     245       64985 :   mt->pending++;
     246       64985 :   BLOCK_SIGINT_END
     247             : }
     248             : 
     249             : void
     250        3929 : mt_queue_reset(void)
     251             : {
     252        3929 :   struct mt_pstate *mt = pari_mt;
     253             :   long i;
     254        3929 :   BLOCK_SIGINT_START
     255       66793 :   for (i=0; i<mt->n; i++)
     256       62864 :     pthread_cancel(mt->th[i]);
     257       66793 :   for (i=0; i<mt->n; i++)
     258       62864 :     pthread_join(mt->th[i],NULL);
     259        3929 :   pari_mt = NULL;
     260        3929 :   BLOCK_SIGINT_END
     261        3929 :   if (DEBUGLEVEL) pari_warn(warner,"stop threads");
     262       66793 :   for (i=0;i<mt->n;i++)
     263             :   {
     264       62864 :     struct mt_queue *mq = mt->mq+i;
     265       62864 :     pthread_cond_destroy(&mq->cond);
     266       62864 :     pthread_mutex_destroy(&mq->mut);
     267       62864 :     pari_thread_free(&mt->pth[i]);
     268             :   }
     269        3929 :   pari_free(mt->mq);
     270        3929 :   pari_free(mt->pth);
     271        3929 :   pari_free(mt->th);
     272        3929 :   pari_free(mt);
     273        3929 : }
     274             : 
     275             : void
     276        3929 : mt_queue_start(struct pari_mt *pt, GEN worker)
     277             : {
     278        3929 :   if (pari_mt)
     279           0 :     mtsingle_queue_start(pt, worker);
     280             :   else
     281             :   {
     282        3929 :     long NBT = pari_mt_nbthreads;
     283        3929 :     struct mt_pstate *mt =
     284             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     285        3929 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     286        3929 :     long mtparisizemax = GP_DATA->threadsizemax;
     287             :     long i;
     288        3929 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*NBT);
     289        3929 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*NBT);
     290        3929 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*NBT);
     291        3929 :     mt->pending = 0;
     292        3929 :     mt->n = NBT;
     293        3929 :     mt->nbint = 0;
     294        3929 :     mt->last = 0;
     295        3929 :     pthread_cond_init(&mt->pcond,NULL);
     296        3929 :     pthread_mutex_init(&mt->pmut,NULL);
     297        3929 :     pari_thread_sync();
     298       66793 :     for (i=0;i<NBT;i++)
     299             :     {
     300       62864 :       struct mt_queue *mq = mt->mq+i;
     301       62864 :       mq->no     = i;
     302       62864 :       mq->avma   = 0;
     303       62864 :       mq->worker = worker;
     304       62864 :       mq->input  = NULL;
     305       62864 :       mq->output = NULL;
     306       62864 :       mq->pcond  = &mt->pcond;
     307       62864 :       mq->pmut   = &mt->pmut;
     308       62864 :       pthread_cond_init(&mq->cond,NULL);
     309       62864 :       pthread_mutex_init(&mq->mut,NULL);
     310       62864 :       if (mtparisizemax)
     311           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     312             :       else
     313       62864 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     314             :     }
     315        3929 :     if (DEBUGLEVEL) pari_warn(warner,"start threads");
     316        3929 :     BLOCK_SIGINT_START
     317       66793 :     for (i=0;i<NBT;i++)
     318       62864 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     319        3929 :     pari_mt = mt;
     320        3929 :     BLOCK_SIGINT_END
     321        3929 :     pt->get=&mtpthread_queue_get;
     322        3929 :     pt->submit=&mtpthread_queue_submit;
     323        3929 :     pt->end=&mt_queue_reset;
     324             :   }
     325        3929 : }

Generated by: LCOV version 1.11