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 19825-b77c7f8) Lines: 169 177 95.5 %
Date: 2016-12-04 05:49:01 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    78648766 : mt_sigint_block(void)
      53             : {
      54    78648766 :   if (mt_thread_no>=0)
      55     4948185 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
      56    79415827 : }
      57             : 
      58             : void
      59    78947460 : mt_sigint_unblock(void)
      60             : {
      61    78947460 :   if (mt_thread_no>=0)
      62     5240437 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
      63    79510041 : }
      64             : 
      65             : void
      66        1012 : mt_err_recover(long er)
      67             : {
      68             :   (void) er;
      69        1012 :   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        1010 : }
      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      189910 : mt_is_parallel(void)
      91             : {
      92      189910 :   return !!pari_mt;
      93             : }
      94             : 
      95             : int
      96    11897804 : mt_is_thread(void)
      97             : {
      98    11897804 :   return mt_thread_no>=0;
      99             : }
     100             : 
     101           0 : void mt_broadcast(GEN code) {(void) code;}
     102             : 
     103         195 : void pari_mt_init(void)
     104             : {
     105         195 :   pari_mt = NULL;
     106             : #ifdef _SC_NPROCESSORS_CONF
     107         195 :   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         195 : }
     114             : 
     115         195 : void pari_mt_close(void) { }
     116             : 
     117             : static void
     118       62716 : mt_queue_cleanup(void *arg)
     119             : {
     120             :   (void) arg;
     121       62716 :   pari_thread_close();
     122       62271 : }
     123             : 
     124             : static void*
     125       61528 : mt_queue_run(void *arg)
     126             : {
     127       61528 :   GEN args = pari_thread_start((struct pari_thread*) arg);
     128       62771 :   pari_sp av = avma;
     129       62771 :   struct mt_queue *mq = (struct mt_queue *) args;
     130       62771 :   mt_thread_no = mq->no;
     131       62771 :   pthread_cleanup_push(mt_queue_cleanup,NULL);
     132       62829 :   LOCK(mq->pmut)
     133             :   {
     134       62848 :     mq->avma = av;
     135       62848 :     pthread_cond_signal(mq->pcond);
     136       62848 :   } UNLOCK(mq->pmut);
     137             :   for(;;)
     138             :   {
     139             :     GEN work, done;
     140      127735 :     LOCK(&mq->mut)
     141             :     {
     142      318523 :       while(!mq->input)
     143      126323 :         pthread_cond_wait(&mq->cond, &mq->mut);
     144       64527 :     } UNLOCK(&mq->mut);
     145       64891 :     avma = mq->avma;
     146       64891 :     work = mq->input;
     147       64891 :     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     148       64719 :     done = closure_callgenvec(mq->worker,work);
     149       63852 :     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
     150       64596 :     LOCK(mq->pmut)
     151             :     {
     152       64914 :       mq->avma = av;
     153       64914 :       mq->input = NULL;
     154       64914 :       mq->output = done;
     155       64914 :       pthread_cond_signal(mq->pcond);
     156       64914 :     } UNLOCK(mq->pmut);
     157       64899 :   }
     158             :   pthread_cleanup_pop(1);
     159             : #ifdef __GNUC__
     160             :   return NULL; /* NOT REACHED */
     161             : #endif
     162             : }
     163             : 
     164             : static long
     165       96025 : mt_queue_check(struct mt_pstate *mt)
     166             : {
     167             :   long i;
     168      893877 :   for(i=0; i<mt->n; i++)
     169             :   {
     170      862766 :     struct mt_queue *mq = mt->mq+i;
     171      862766 :     if (mq->output) return i;
     172             :   }
     173       31111 :   return -1;
     174             : }
     175             : 
     176             : static GEN
     177       91507 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
     178             : {
     179       91507 :   struct mt_pstate *mt = pari_mt;
     180             :   struct mt_queue *mq;
     181       91507 :   GEN done = NULL;
     182             :   long last;
     183             :   (void) junk;
     184       91507 :   if (mt->nbint<mt->n)
     185             :   {
     186       26591 :     mt->last = mt->nbint;
     187       26591 :     *pending = mt->pending;
     188       26591 :     return NULL;
     189             :   }
     190       64916 :   BLOCK_SIGINT_START
     191       64916 :   LOCK(&mt->pmut)
     192             :   {
     193      160941 :     while ((last = mt_queue_check(mt)) < 0)
     194             :     {
     195       31111 :       pthread_cond_wait(&mt->pcond, &mt->pmut);
     196       31111 :       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       64914 :   } UNLOCK(&mt->pmut);
     208       64914 :   BLOCK_SIGINT_END
     209       64914 :   mq = mt->mq+last;
     210       64914 :   if (mq->output==err_e_STACK) pari_err(e_STACKTHREAD);
     211       64914 :   done = gcopy(mq->output);
     212       64914 :   mq->output = NULL;
     213       64914 :   if (workid) *workid = mq->workid;
     214       64914 :   if (typ(done)==t_ERROR) pari_err(0,done);
     215       64912 :   mt->last = last;
     216       64912 :   mt->pending--;
     217       64912 :   *pending = mt->pending;
     218       64912 :   return done;
     219             : }
     220             : 
     221             : static void
     222       91507 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
     223             : {
     224       91507 :   struct mt_pstate *mt = pari_mt;
     225       91507 :   struct mt_queue *mq = mt->mq+mt->last;
     226             :   (void) junk;
     227      183014 :   if (!work) { mt->nbint=mt->n; return; }
     228       64964 :   BLOCK_SIGINT_START
     229       64964 :   if (mt->nbint<mt->n)
     230             :   {
     231       27098 :     mt->nbint++;
     232       27098 :     LOCK(mq->pmut)
     233             :     {
     234       96405 :       while(!mq->avma)
     235       42209 :         pthread_cond_wait(mq->pcond, mq->pmut);
     236       27098 :     } UNLOCK(mq->pmut);
     237             :   }
     238       64964 :   LOCK(&mq->mut)
     239             :   {
     240       64964 :     mq->output = NULL;
     241       64964 :     mq->workid = workid;
     242       64964 :     mq->input = gcopy_avma(work, &mq->avma);
     243       64964 :     pthread_cond_signal(&mq->cond);
     244       64964 :   } UNLOCK(&mq->mut);
     245       64964 :   mt->pending++;
     246       64964 :   BLOCK_SIGINT_END
     247             : }
     248             : 
     249             : void
     250        3928 : mt_queue_reset(void)
     251             : {
     252        3928 :   struct mt_pstate *mt = pari_mt;
     253             :   long i;
     254        3928 :   BLOCK_SIGINT_START
     255       66776 :   for (i=0; i<mt->n; i++)
     256       62848 :     pthread_cancel(mt->th[i]);
     257       66776 :   for (i=0; i<mt->n; i++)
     258       62848 :     pthread_join(mt->th[i],NULL);
     259        3928 :   pari_mt = NULL;
     260        3928 :   BLOCK_SIGINT_END
     261        3928 :   if (DEBUGLEVEL) pari_warn(warner,"stop threads");
     262       66776 :   for (i=0;i<mt->n;i++)
     263             :   {
     264       62848 :     struct mt_queue *mq = mt->mq+i;
     265       62848 :     pthread_cond_destroy(&mq->cond);
     266       62848 :     pthread_mutex_destroy(&mq->mut);
     267       62848 :     pari_thread_free(&mt->pth[i]);
     268             :   }
     269        3928 :   pari_free(mt->mq);
     270        3928 :   pari_free(mt->pth);
     271        3928 :   pari_free(mt->th);
     272        3928 :   pari_free(mt);
     273        3928 : }
     274             : 
     275             : void
     276        3928 : mt_queue_start(struct pari_mt *pt, GEN worker)
     277             : {
     278        3928 :   if (pari_mt)
     279           0 :     mtsingle_queue_start(pt, worker);
     280             :   else
     281             :   {
     282        3928 :     long NBT = pari_mt_nbthreads;
     283        3928 :     struct mt_pstate *mt =
     284             :            (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
     285        3928 :     long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
     286        3928 :     long mtparisizemax = GP_DATA->threadsizemax;
     287             :     long i;
     288        3928 :     mt->mq  = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*NBT);
     289        3928 :     mt->th  = (pthread_t *) pari_malloc(sizeof(*mt->th)*NBT);
     290        3928 :     mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*NBT);
     291        3928 :     mt->pending = 0;
     292        3928 :     mt->n = NBT;
     293        3928 :     mt->nbint = 0;
     294        3928 :     mt->last = 0;
     295        3928 :     pthread_cond_init(&mt->pcond,NULL);
     296        3928 :     pthread_mutex_init(&mt->pmut,NULL);
     297        3928 :     pari_thread_sync();
     298       66776 :     for (i=0;i<NBT;i++)
     299             :     {
     300       62848 :       struct mt_queue *mq = mt->mq+i;
     301       62848 :       mq->no     = i;
     302       62848 :       mq->avma   = 0;
     303       62848 :       mq->worker = worker;
     304       62848 :       mq->input  = NULL;
     305       62848 :       mq->output = NULL;
     306       62848 :       mq->pcond  = &mt->pcond;
     307       62848 :       mq->pmut   = &mt->pmut;
     308       62848 :       pthread_cond_init(&mq->cond,NULL);
     309       62848 :       pthread_mutex_init(&mq->mut,NULL);
     310       62848 :       if (mtparisizemax)
     311           0 :         pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
     312             :       else
     313       62848 :         pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
     314             :     }
     315        3928 :     if (DEBUGLEVEL) pari_warn(warner,"start threads");
     316        3928 :     BLOCK_SIGINT_START
     317       66776 :     for (i=0;i<NBT;i++)
     318       62848 :       pthread_create(&mt->th[i],NULL, &mt_queue_run, (void*)&mt->pth[i]);
     319        3928 :     pari_mt = mt;
     320        3928 :     BLOCK_SIGINT_END
     321        3928 :     pt->get=&mtpthread_queue_get;
     322        3928 :     pt->submit=&mtpthread_queue_submit;
     323        3928 :     pt->end=&mt_queue_reset;
     324             :   }
     325        3928 : }

Generated by: LCOV version 1.11