Doug Torrance on Thu, 19 May 2016 22:55:27 +0200


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

Re: GMP memory allocation and Macaulay2


On 05/19/2016 03:59 PM, Bill Allombert wrote:
On Thu, May 19, 2016 at 02:58:16PM -0400, Doug Torrance wrote:
Hello!

Currently, PARI sets the GMP memory allocation functions in
pari_kernel_init().  However, this may cause problems for
applications which use both the PARI and GMP libraries such as
Macaulay2.  Building Macaulay2 against unpatched GMP and PARI
libraries causes segmentation faults.
You issue is related to the bug #1317.
Do you know why the segmentation faults occurs ? This is the
crux of the matter.

I don't recall at the moment. I'll work on getting back to you with a backtrace.

Currently, Macaulay2 builds its own GMP (actually MPIR, a drop-in
replacement) in which mp_set_memory_functions() does nothing.  This
is not ideal, however.  For example, this is the main problem
keeping Macaulay2 out of Debian, as it cannot be built with the
current Debian GMP and PARI packages.  (See [1] for some discussion
on this matter.)

Would it be possible to remove the calls to
mp_set_memory_functions() from PARI?
PARI only runs pari_kernel_init() once (in pari_init_opts),
so you can just write a wrapper around pari_init() or pari_init_opts()
that saves the values returned by mp_get_memory_functions() and resets
them using mp_set_memory_functions() just after pari_init:

void
my_pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
{
   void *(*new_gmp_malloc)(size_t new_size);
   void *(*new_gmp_realloc)(void *ptr, size_t old_size, size_t new_size);
   void (*new_gmp_free)(void *ptr, size_t old_size);
   mp_get_memory_functions (&new_gmp_malloc, &new_gmp_realloc, &new_gmp_free);
   pari_init_opts(parisize,maxprime,init_opts);
   mp_set_memory_functions(new_gmp_malloc, new_gmp_realloc, new_gmp_free);
}

It is not possible to remove the call to mp_set_memory_functions() from
the Debian packages in a reasonable timeframe.

Great suggestion -- thank you!  I'll try it out.