Tuning framework

Niels Möller nisse at lysator.liu.se
Tue Nov 18 21:48:46 CET 2003


Kevin Ryde <user42 at zip.com.au> writes:

> The first thing you need is a measuring function.  It takes care of
> setting up temp space and then calling the routine for the requested
> number of repetitions.

I hope I've got this right now (it still doesn't quite work, but
that's probably due to bugs in my code). The first threshold I want to
tune i HGCD_SCHOENHAGE_THRESHOLD, so I added some magic to gmp-impl.h,
just like the other tuned thresholds.

I put into tuneup.c:

  void
  tune_hgcd (void)
  {
    static struct param_t  param;
    param.name = "HGCD_SCHOENHAGE_THRESHOLD";
    param.function = speed_mpn_hgcd;
    param.min_size = 20;
    one (&hgcd_schoenhage_threshold, &param);
  }

and call that from all(), before the other gcd functions. The hairier
part is in common.c, where I wrote

  double
  speed_mpn_hgcd (struct speed_params *s)
  {
    mp_ptr wp;
    mp_size_t hgcd_init_scratch = mpn_hgcd_init_itch(s->size);
    mp_size_t qstack_scratch = qstack_itch(s->size);
    mp_size_t hgcd_scratch = mpn_hgcd_itch(s->size);
    mp_srcptr ap;
    mp_srcptr bp;
    mp_size_t asize;
    mp_size_t bsize;
  
    struct hgcd hgcd;
    struct qstack quotients;
    int res;
    unsigned i;
    double t;
    TMP_DECL(marker);
  
    /* Input numbers should be normalized, and a > b */
    ap = s->xp; asize = s->size;
    bp = s->yp; bsize = s->size;
  
    /* FIXME: Figure out if caller guarantees normalization. */
    MPN_NORMALIZE(ap, asize);
    MPN_NORMALIZE(bp, bsize);
  
    if (asize < bsize || (bsize == asize && mpn_cmp(ap, bp, asize) < 0))
      {
        MPN_SRCPTR_SWAP(ap, asize, bp, bsize);
      }
  
    /* FIXME: It may happen (with low probability) that asize or bsize
       are too small. What to do then? */
  
    mpn_hgcd_init(&hgcd, asize,
  		SPEED_TMP_ALLOC_LIMBS(hgcd_init_scratch,
  		s->align_wp));
    qstack_init(&quotients, asize,
  	      SPEED_TMP_ALLOC_LIMBS(qstack_scratch, s->align_wp),
  	      qstack_scratch);
    wp = SPEED_TMP_ALLOC_LIMBS(hgcd_scratch, s->align_wp);
  
    speed_starttime ();
    i = s->reps;
    do {
      qstack_reset(&quotients, s->size);
      res = mpn_hgcd(&hgcd, ap, asize, bp, bsize,
  		   &quotients,
  		   wp, hgcd_scratch);
    }
    while (--i != 0);
    t = speed_endtime ();
  #if WANT_CHECKS
    if (res)
      hgcd_sanity(&hgcd, ap, asize, bp, bsize, 0, 4);
  #endif
    TMP_FREE(marker);
    return t;
  }

I suspect I still have some bugs in my code, which is why I want to
first run this with asserts and debugging and sanity checks enabled.
Comments appreciated.

Regards,
/Niels


More information about the gmp-devel mailing list