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:

  tune_hgcd (void)
    static struct param_t  param;
    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

  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;
    /* 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,
    qstack_init(&quotients, asize,
  	      SPEED_TMP_ALLOC_LIMBS(qstack_scratch, s->align_wp),
    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,
  		   wp, hgcd_scratch);
    while (--i != 0);
    t = speed_endtime ();
    if (res)
      hgcd_sanity(&hgcd, ap, asize, bp, bsize, 0, 4);
    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.


More information about the gmp-devel mailing list