Karim BELABAS on Wed, 4 Jun 2003 20:53:57 +0200 (MEST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: GEN serialisation


On Wed, 4 Jun 2003, Gonzalo Tornaria wrote:
> I am writing some distributed PARI program that run in a network and
> comunicate using PVM. I need a way to serialise GENs so as to be able to
> transfer them back and forth. Does someone has sample code for this?

There's a highly experimental and recently introduced set of routines,
to be used by writebin() behind the scene ( cf rdGEN() / wrGEN() ).

I'm not 100% happy with the implementation, so it's still undocumented
( usual problem ... )

  send_long(long *data, int count);
  recv_long(long *data, int count);
  GENbin* copy_bin_canon(GEN x)
  GEN     bin_copy(GENbin *p)

  void send(GEN x)
  {
    GENbin *p = copy_bin_canon(x);
    size_t  L = p->len      /* size ( taille() ) of x */
    GEN     x = p->x        /* "machine independent" binary copy of x */
    GEN     base = p->base  /* base address of x */

    send_long(&L, 1);
    send_long(&x, 1);
    send_long(&base, 1);
    send_long(GENbase(p), L);
  }

  GEN recv()
  {
    GENbin *p;
    size_t L;
    GEN    x;
    GEN    base;

    recv_long(&L, 1); p = (GENbin*)gpmalloc(sizeof(GENbin) + L*sizeof(long));
    recv_long(&x, 1);
    recv_long(&base, 1);
    recv_long(GENbase(p), L);
    p->len  = L;
    p->x    = x;
    p->base = base;
    return bin_copy(p); /* NOTE: p is freed within */
  }

WARNING: Untested. Basic idea should work, have a look at the init.c / es.c
routines if in doubt (these _do_ work). The mechanism is able to cope with
GMP / native kernel mixups but not with 32bit / 64bit or endianness
differences [ to be done sometime... ]

IMHO, it will be simpler to writebin() to a file, send it then readbin() the
file.

Hope this helps,

    Karim.
-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dép. de Mathématiques, Bât. 425   Fax: (+33) (0)1 69 15 60 19
Université Paris-Sud              http://www.math.u-psud.fr/~belabas/
F-91405 Orsay (France)            http://www.parigp-home.de/  [PARI/GP]