bridge/class-master/depend/pari/src/modules/mpqs.c
/* This function returns 1 when it actually did something and decided on a
 * good threshold  (although possibly the same as it was before),  -1 when
 * there was nothing to do and never will be ("don't call us again"), 0
 * when the caller should retry somewhat later.  Note that mpqs() already
 * knows the total number of candidates generated so far  (from the return
 * values of mpqs_eval_sieve()),  and won't call us too early;  but we also
 * insist on minimal standards for the column sums.  Conversely when we ever
 * lower the threshold, we ask for a re-evaluation later on.
 * NB With the present accounting, once the threshold has been raised, it
 * won't ever be lowered again, since the increasing counts above it will
 * totally swamp the few earlier measurements below which can no longer
 * grow.  So we might chop off those accumulating loops at the current sieve
 * threshold. */
This function returns 1 when it actually did something and decided on a
good threshold  (although possibly the same as it was before),  -1 when
there was nothing to do and never will be ("don't call us again"), 0
when the caller should retry somewhat later.  Note that mpqs() already
knows the total number of candidates generated so far  (from the return
values of mpqs_eval_sieve()),  and won't call us too early;  but we also
insist on minimal standards for the column sums.  Conversely when we ever
lower the threshold, we ask for a re-evaluation later on.
NB With the present accounting, once the threshold has been raised, it
won't ever be lowered again, since the increasing counts above it will
totally swamp the few earlier measurements below which can no longer
grow.  So we might chop off those accumulating loops at the current sieve
threshold.
This function returns 1 when it actually did something and decided on a good threshold ( although possibly the same as it was before ) - 1 when there was nothing to do and never will be ( " don ' t call us again " ) 0 when the caller should retry somewhat later . Note that mpqs () already knows the total number of candidates generated so far ( from the return values of mpqs_eval_sieve () ) and won ' t call us too early ; but we also insist on minimal standards for the column sums . Conversely when we ever lower the threshold we ask for a re - evaluation later on . NB With the present accounting once the threshold has been raised it won ' t ever be lowered again since the increasing counts above it will totally swamp the few earlier measurements below which can no longer grow . So we might chop off those accumulating loops at the current sieve threshold .
static int
mpqs_eval_histograms(mpqs_handle_t *h)
{
  long tot_full = 0, tot_lprl = 0, tot_drop = 0, total = 0;
  long target_full, i;
  int th_full, th_base, th_drop;
  int th = h->sieve_threshold - 128;
  if (!h->do_histograms) return -1;
  for (i = 127; i >= 0; i--)
  {
    tot_full += h->histo_full[i];
    tot_lprl += h->histo_lprl[i];
    tot_drop += h->histo_drop[i];
  }
  total = tot_full + tot_lprl + tot_drop;
  if ((total < MPQS_MIN_CANDS_FOR_HISTO) ||
      (tot_full < MPQS_MIN_FRELS_FOR_HISTO))
    return 0;                   
  th_full = th_drop = th_base = -1;
  target_full = tot_full - (tot_full * MPQS_HISTO_FREL_QUANTILE) / 100.;
  tot_full = 0;
  for (i = 127; i >= th; i--)
  {
    if ((tot_full += h->histo_full[i]) >= target_full)
    {
      th_full = i; break;
    }
  }
  for (i = 127; i >= th; i--)
  {
    if (h->histo_lprl[i] + 1 <
        MPQS_HISTO_LPREL_BASEFLOW * h->histo_drop[i])
    {
      th_base = i; break;
    }
  }
  tot_lprl = 0; tot_drop = 0;
  for (i = 127; i >= th; i--)
  {
    tot_lprl += h->histo_full[i] + h->histo_lprl[i];
    tot_drop += h->histo_drop[i];
    if (tot_drop >
        MPQS_HISTO_DROP_LIMIT * (tot_lprl + 1))
    {
      th_drop = i; break;
    }
  }
  if (MPQS_DEBUGLEVEL >= 5)
  {
    mpqs_print_histo(h);
    if (th_full >= 0)
      err_printf("MPQS: threshold estimate for full rels: %d\n",
                 th_full + 128);
    if (th_drop >= 0)
      err_printf("MPQS: threshold estimate for useful candidates: %d\n",
                 th_drop + 128);
  }
  if ((th > 0) && (th_base <= th) &&
      (h->histo_lprl[th] > (MPQS_MIN_FRELS_FOR_HISTO * 3.5)) )
  {
    h->sieve_threshold = th + 127;
    if (MPQS_DEBUGLEVEL >= 4)
      err_printf("MPQS: loosening sieve tolerance, new threshold %d\n",
                 h->sieve_threshold);
    return 0;                   
  }
  th = (th_full < th_drop ? th_full : th_drop) + 128;
  if (th > h->sieve_threshold)
  {
    h->sieve_threshold = th;
    if (MPQS_DEBUGLEVEL >= 4)
      err_printf("MPQS: tightening sieve tolerance, new threshold %d\n",
                 h->sieve_threshold);
  }
  return 1;                     
}
static int mpqs_eval_histograms ( mpqs_handle_t * h ) { long tot_full = 0 , tot_lprl = 0 , tot_drop = 0 , total = 0 ; long target_full , i ; int th_full , th_base , th_drop ; int th = h -> sieve_threshold - 128 ; if ( ! h -> do_histograms ) return -1 ; for ( i = 127 ; i >= 0 ; i -- ) { tot_full += h -> histo_full [ i ] ; tot_lprl += h -> histo_lprl [ i ] ; tot_drop += h -> histo_drop [ i ] ; } total = tot_full + tot_lprl + tot_drop ; if ( ( total < MPQS_MIN_CANDS_FOR_HISTO ) || ( tot_full < MPQS_MIN_FRELS_FOR_HISTO ) ) return 0 ; th_full = th_drop = th_base = -1 ; target_full = tot_full - ( tot_full * MPQS_HISTO_FREL_QUANTILE ) / 100. ; tot_full = 0 ; for ( i = 127 ; i >= th ; i -- ) { if ( ( tot_full += h -> histo_full [ i ] ) >= target_full ) { th_full = i ; break ; } } for ( i = 127 ; i >= th ; i -- ) { if ( h -> histo_lprl [ i ] + 1 < MPQS_HISTO_LPREL_BASEFLOW * h -> histo_drop [ i ] ) { th_base = i ; break ; } } tot_lprl = 0 ; tot_drop = 0 ; for ( i = 127 ; i >= th ; i -- ) { tot_lprl += h -> histo_full [ i ] + h -> histo_lprl [ i ] ; tot_drop += h -> histo_drop [ i ] ; if ( tot_drop > MPQS_HISTO_DROP_LIMIT * ( tot_lprl + 1 ) ) { th_drop = i ; break ; } } if ( MPQS_DEBUGLEVEL >= 5 ) { mpqs_print_histo ( h ) ; if ( th_full >= 0 ) err_printf ( " \n " , th_full + 128 ) ; if ( th_drop >= 0 ) err_printf ( " \n " , th_drop + 128 ) ; } if ( ( th > 0 ) && ( th_base <= th ) && ( h -> histo_lprl [ th ] > ( MPQS_MIN_FRELS_FOR_HISTO * 3.5 ) ) ) { h -> sieve_threshold = th + 127 ; if ( MPQS_DEBUGLEVEL >= 4 ) err_printf ( " \n " , h -> sieve_threshold ) ; return 0 ; } th = ( th_full < th_drop ? th_full : th_drop ) + 128 ; if ( th > h -> sieve_threshold ) { h -> sieve_threshold = th ; if ( MPQS_DEBUGLEVEL >= 4 ) err_printf ( " \n " , h -> sieve_threshold ) ; } return 1 ; }
This function returns 1 when it actually did something and decided on a
 good threshold  (although possibly the same as it was before),  -1 when
 there was nothing to do and never will be ("don't call us again"), 0
 when the caller should retry somewhat later.
This function returns 1 when it actually did something and decided on a good threshold ( although possibly the same as it was before ) - 1 when there was nothing to do and never will be ( " don ' t call us again " ) 0 when the caller should retry somewhat later .
/* first compute column sums */ /* too early to call the race */ /* find the full relations quantile point */ /* find the "lp relations baseflow" point */ /* find the wastefulness point */ /* if these loops found nothing, then th_(full|base|drop) will still be -1.
   * We won't tighten the sieve, but th_base would tell us we should loosen
   * it  (reluctantly). */ /* any reason to open up the sieve?  wait until a minimal number of lprels
   * at the threshold has been seen before going down... */ /* this should be re-examined after a bit */ /* otherwise, any reason to tighten it? */ /* maybe also loosen it if th_drop persistently stays below th... */ /* wait a good while before rechecking */