Beispiel #1
0
void init_memory_functions()
{
#if 0
    void* (*mpir_malloc)(size_t);
    void* (*mpir_realloc)(void*, size_t, size_t);
    void (*mpir_free)(void*, size_t);
    mp_get_memory_functions(&mpir_malloc, &mpir_realloc, &mpir_free);
    printf("MPIR memory functions BEFORE: %p %p %p\n", mpir_malloc, mpir_realloc, mpir_free);
#endif
    mp_set_memory_functions(sage_mpir_malloc, sage_mpir_realloc, sage_mpir_free);
#if 0
    mp_get_memory_functions(&mpir_malloc, &mpir_realloc, &mpir_free);
    printf("MPIR memory functions AFTER:  %p %p %p\n", mpir_malloc, mpir_realloc, mpir_free);
#endif
}
Beispiel #2
0
char *
mpc_alloc_str (size_t len)
{
  void * (*allocfunc) (size_t);
  mp_get_memory_functions (&allocfunc, NULL, NULL);
  return (char *) ((*allocfunc) (len));
}
/* No need to parse '-' since that's handled as an operator.
   This function also by mpq_expr_a, so it's not static.  */
size_t
mpexpr_mpz_number (mpz_ptr res, __gmp_const char *e, size_t elen, int base)
{
  char    *edup;
  size_t  i, ret;
  int     base_effective = (base == 0 ? 10 : base);
  void    *(*allocate_func) (size_t);
  void    (*free_func) (void *, size_t);

  i = 0;
  if (e[i] == '0')
    {
      i++;
      if (e[i] == 'x' || e[i] == 'b')
        i++;
    }

  for ( ; i < elen; i++)
    if (! isasciidigit_in_base (e[i], base_effective))
      break;

  mp_get_memory_functions (&allocate_func, NULL, &free_func);
  edup = (*allocate_func) (i+1);
  memcpy (edup, e, i);
  edup[i] = '\0';

  if (mpz_set_str (res, edup, base) == 0)
    ret = i;
  else
    ret = 0;

  (*free_func) (edup, i+1);
  return ret;
}
Beispiel #4
0
void
mpc_free_str (char *str)
{
  void (*freefunc) (void *, size_t);
  mp_get_memory_functions (NULL, NULL, &freefunc);
  (*freefunc) (str, strlen (str) + 1);
}
Beispiel #5
0
		// stolen from boost.multiprecision
		std::string str(std::ios_base::fmtflags f = std::ios_base::fmtflags(0)) const
		{
			int base = 10;
			if((f & std::ios_base::oct) == std::ios_base::oct)
				base = 8;
			else if((f & std::ios_base::hex) == std::ios_base::hex)
				base = 16;
			//
			// sanity check, bases 8 and 16 are only available for positive numbers:
			//
			if((base != 10) && (mpz_sgn(m_data) < 0))
				BOOST_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers"));
			void *(*alloc_func_ptr) (size_t);
			void *(*realloc_func_ptr) (void *, size_t, size_t);
			void (*free_func_ptr) (void *, size_t);
			const char* ps = mpz_get_str (0, base, m_data);
			std::string s = ps;
			mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
			(*free_func_ptr)((void*)ps, std::strlen(ps) + 1);

			if((base != 10) && (f & std::ios_base::showbase))
			{
				int pos = s[0] == '-' ? 1 : 0;
				const char* pp = base == 8 ? "0" : "0x";
				s.insert(pos, pp);
			}
			if((f & std::ios_base::showpos) && (s[0] != '-'))
				s.insert(0, 1, '+');

			return s;
		}
Beispiel #6
0
char *
mpc_realloc_str (char * str, size_t oldlen, size_t newlen)
{
  void * (*reallocfunc) (void *, size_t, size_t);
  mp_get_memory_functions (NULL, &reallocfunc, NULL);
  return (char *) ((*reallocfunc) (str, oldlen, newlen));
}
Beispiel #7
0
void Float::write(std::ostream& out) const
{
  char* buffer;
  const int bufferSize = gmp_asprintf(&buffer, "%.Fe", value);

  out << buffer;

  void (*freefunc)(void*, size_t);
  mp_get_memory_functions (NULL, NULL, &freefunc);
  freefunc(buffer, bufferSize);
}
Beispiel #8
0
/*
#ifdef __GNUC__
#pragma message "MPFI_VERSION_MAJOR=" BOOST_STRINGIZE(MPFI_VERSION_MAJOR)
#pragma message "MPFI_VERSION_MAJOR=" BOOST_STRINGIZE(MPFI_VERSION_MAJOR)
#endif 

#if MPFI_VERSION_MAJOR < 1
#error "Incompatible MPFI version"
#endif
#if (MPFI_VERSION_MAJOR == 1) && (MPFI_VERSION_MINOR < 5)
#error "Incompatible MPFI version"
#endif
*/
int main()
{
   void *(*alloc_func_ptr) (size_t);
   void *(*realloc_func_ptr) (void *, size_t, size_t);
   void (*free_func_ptr) (void *, size_t);

   mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);

   mpfr_buildopt_tls_p();

   return 0;
}
Beispiel #9
0
int main()
{
   void *(*alloc_func_ptr) (size_t);
   void *(*realloc_func_ptr) (void *, size_t, size_t);
   void (*free_func_ptr) (void *, size_t);

   mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);

   mpz_t integ;
   mpz_init (integ);
   mpz_clear (integ);

   return 0;
}
Beispiel #10
0
static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
	isl_int i)
{
	char *s;
	int len;
	isl_int_print_gmp_free_t gmp_free;

	s = isl_int_get_str(i);
	len = strlen(s);
	if (len < p->width)
		p = str_print_indent(p, p->width - len);
	p = str_print(p, s, len);
	mp_get_memory_functions(NULL, NULL, &gmp_free);
	(*gmp_free)(s, len + 1);
	return p;
}
Beispiel #11
0
/**
  * Helper function to convert an mpz_class object into
  * its internal QString representation. Mainly used for
  * debugging.
  */
static QString mpzToString(const mpz_class & val)
{
  char *p = 0;
  // use the gmp provided conversion routine
  gmp_asprintf(&p, "%Zd", val.get_mpz_t());

  // convert it into a QString
  QString result(QString::fromLatin1(p));

  // and free up the resources allocated by gmp_asprintf
  __gmp_freefunc_t freefunc;
  mp_get_memory_functions(NULL, NULL, &freefunc);
  (*freefunc)(p, std::strlen(p) + 1);

  // done
  return result;
}
Beispiel #12
0
int main()
{
   void *(*alloc_func_ptr) (size_t);
   void *(*realloc_func_ptr) (void *, size_t, size_t);
   void (*free_func_ptr) (void *, size_t);

   mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);

   mpfr_buildopt_tls_p();

   mpfr_t t;
   mpfr_init2(t, 128);
   if(t[0]._mpfr_d)
      mpfr_clear(t);

   return 0;
}
Beispiel #13
0
/**
  * Helper function to convert an mpq_class object into
  * its internal QString representation. Mainly used for
  * debugging.
  */
static QString mpqToString(const mpq_class & val)
{
  char *p = 0;
  // use the gmp provided conversion routine
  gmp_asprintf(&p, "%Qd", val.get_mpq_t());

  // convert it into a QString
  QString result = QString::fromLatin1(p);

  // and free up the resources allocated by gmp_asprintf
  __gmp_freefunc_t freefunc;
  mp_get_memory_functions(NULL, NULL, &freefunc);
  (*freefunc)(p, std::strlen(p) + 1);

  if (!result.contains(QLatin1Char('/'))) {
    result += QString::fromLatin1("/1");
  }

  // done
  return result;
}
Beispiel #14
0
static void
pdr_stride_in_loop (mpz_t stride, graphite_dim_t depth, poly_dr_p pdr)
{
  poly_bb_p pbb = PDR_PBB (pdr);
  isl_map *map;
  isl_set *set;
  isl_aff *aff;
  isl_space *dc;
  isl_constraint *lma, *c;
  isl_int islstride;
  graphite_dim_t time_depth;
  unsigned offset, nt;
  unsigned i;
  /* XXX isl rewrite following comments.  */
  /* Builds a partial difference equations and inserts them
     into pointset powerset polyhedron P.  Polyhedron is assumed
     to have the format: T|I|T'|I'|G|S|S'|l1|l2.

     TIME_DEPTH is the time dimension w.r.t. which we are
     differentiating.
     OFFSET represents the number of dimensions between
     columns t_{time_depth} and t'_{time_depth}.
     DIM_SCTR is the number of scattering dimensions.  It is
     essentially the dimensionality of the T vector.

     The following equations are inserted into the polyhedron P:
     | t_1 = t_1'
     | ...
     | t_{time_depth-1} = t'_{time_depth-1}
     | t_{time_depth} = t'_{time_depth} + 1
     | t_{time_depth+1} = t'_{time_depth + 1}
     | ...
     | t_{dim_sctr} = t'_{dim_sctr}.  */

  /* Add the equality: t_{time_depth} = t'_{time_depth} + 1.
     This is the core part of this alogrithm, since this
     constraint asks for the memory access stride (difference)
     between two consecutive points in time dimensions.  */

  /* Add equalities:
     | t1 = t1'
     | ...
     | t_{time_depth-1} = t'_{time_depth-1}
     | t_{time_depth+1} = t'_{time_depth+1}
     | ...
     | t_{dim_sctr} = t'_{dim_sctr}

     This means that all the time dimensions are equal except for
     time_depth, where the constraint is t_{depth} = t'_{depth} + 1
     step.  More to this: we should be careful not to add equalities
     to the 'coupled' dimensions, which happens when the one dimension
     is stripmined dimension, and the other dimension corresponds
     to the point loop inside stripmined dimension.  */

  /* pdr->accesses:    [P1..nb_param,I1..nb_domain]->[a,S1..nb_subscript]
          ??? [P] not used for PDRs?
     pdr->extent:      [a,S1..nb_subscript]
     pbb->domain:      [P1..nb_param,I1..nb_domain]
     pbb->transformed: [P1..nb_param,I1..nb_domain]->[T1..Tnb_sctr]
          [T] includes local vars (currently unused)
     
     First we create [P,I] -> [T,a,S].  */
  
  map = isl_map_flat_range_product (isl_map_copy (pbb->transformed),
				    isl_map_copy (pdr->accesses));
  /* Add a dimension for L: [P,I] -> [T,a,S,L].*/
  map = isl_map_add_dims (map, isl_dim_out, 1);
  /* Build a constraint for "lma[S] - L == 0", effectively calculating
     L in terms of subscripts.  */
  lma = build_linearized_memory_access (map, pdr);
  /* And add it to the map, so we now have:
     [P,I] -> [T,a,S,L] : lma([S]) == L.  */
  map = isl_map_add_constraint (map, lma);

  /* Then we create  [P,I,P',I'] -> [T,a,S,L,T',a',S',L'].  */
  map = isl_map_flat_product (map, isl_map_copy (map));

  /* Now add the equality T[time_depth] == T'[time_depth]+1.  This will
     force L' to be the linear address at T[time_depth] + 1. */
  time_depth = psct_dynamic_dim (pbb, depth);
  /* Length of [a,S] plus [L] ...  */
  offset = 1 + isl_map_dim (pdr->accesses, isl_dim_out);
  /* ... plus [T].  */
  offset += isl_map_dim (pbb->transformed, isl_dim_out);

  c = isl_equality_alloc (isl_local_space_from_space (isl_map_get_space (map)));
  c = isl_constraint_set_coefficient_si (c, isl_dim_out, time_depth, 1);
  c = isl_constraint_set_coefficient_si (c, isl_dim_out,
					 offset + time_depth, -1);
  c = isl_constraint_set_constant_si (c, 1);
  map = isl_map_add_constraint (map, c);

  /* Now we equate most of the T/T' elements (making PITaSL nearly
     the same is (PITaSL)', except for one dimension, namely for 'depth'
     (an index into [I]), after translating to index into [T].  Take care
     to not produce an empty map, which indicates we wanted to equate
     two dimensions that are already coupled via the above time_depth
     dimension.  Happens with strip mining where several scatter dimension
     are interdependend.  */
  /* Length of [T].  */
  nt = pbb_nb_scattering_transform (pbb) + pbb_nb_local_vars (pbb);
  for (i = 0; i < nt; i++)
    if (i != time_depth)
      {
	isl_map *temp = isl_map_equate (isl_map_copy (map),
					isl_dim_out, i,
					isl_dim_out, offset + i);
	if (isl_map_is_empty (temp))
	  isl_map_free (temp);
	else
	  {
	    isl_map_free (map);
	    map = temp;
	  }
      }

  /* Now maximize the expression L' - L.  */
  set = isl_map_range (map);
  dc = isl_set_get_space (set);
  aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
  aff = isl_aff_set_coefficient_si (aff, isl_dim_in, offset - 1, -1);
  aff = isl_aff_set_coefficient_si (aff, isl_dim_in, offset + offset - 1, 1);
  isl_int_init (islstride);
  isl_set_max (set, aff, &islstride);
  isl_int_get_gmp (islstride, stride);
  isl_int_clear (islstride);
  isl_aff_free (aff);
  isl_set_free (set);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      char *str;
      void (*gmp_free) (void *, size_t);

      fprintf (dump_file, "\nStride in BB_%d, DR_%d, depth %d:",
	       pbb_index (pbb), PDR_ID (pdr), (int) depth);
      str = mpz_get_str (0, 10, stride);
      fprintf (dump_file, "  %s ", str);
      mp_get_memory_functions (NULL, NULL, &gmp_free);
      (*gmp_free) (str, strlen (str) + 1);
    }
}
Beispiel #15
0
void FindPrimes(unsigned int ThreadCount, unsigned int StartingPValue)
{
	PCALC_THREAD_CONTEXT threads;
	unsigned int i;
	int err;

	PRINT_MSG(MSG_INFO, "Mersy %d.%d (built with GMP %d.%d.%d)",
	          __MERSY_MAJOR_VER, __MERSY_MINOR_VER,
	          __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR,
	          __GNU_MP_VERSION_PATCHLEVEL);

	threads = (PCALC_THREAD_CONTEXT) malloc(sizeof(*threads) * ThreadCount);
	if (threads == NULL)
		return;

	// Get a pointer to the GMP free function
	mp_get_memory_functions(NULL, NULL, &GmpFree);

	// Initialize the lock to synchronize fetching a new P value
	pthread_mutex_init(&NextPMutex, NULL);

	// Initialize the next prime P
	mpz_init_set_ui(NextPrimeP, StartingPValue);
	mpz_sub_ui(NextPrimeP, NextPrimeP, 1);
	mpz_nextprime(NextPrimeP, NextPrimeP);

	// Setup and start the threads
	for (i = 0; i < ThreadCount; i++)
	{
		// Do one-time setup of thread context index
		threads[i].threadIndex = i;

		// Initialize the temps for this thread
		mpz_init(threads[i].nextPrime);

		// Spawn the thread
		err = pthread_create(&threads[i].id, NULL, CalculationThread, &threads[i]);
		if (err)
		{
			PRINT_MSG(MSG_WARNING, "Unable to create thread %d!", threads[i].threadIndex);

			// Cleanup this thread's state
			mpz_clear(threads[i].nextPrime);

			// Modify the thread count to the actual number of created threads
			ThreadCount = i;

			// Execution can continue fine from here
			break;
		}
	}

	// Join the threads (which hopefully shouldn't terminate anyways)
	for (i = 0; i < ThreadCount; i++)
	{
		pthread_join(threads[i].id, NULL);

		PRINT_MSG(MSG_WARNING, "Unexpected termination of thread %d!", threads[i].threadIndex);

		// Cleanup this thread's working state
		mpz_clear(threads[i].nextPrime);
	}

	// Everybody's dead :(
	mpz_clear(NextPrimeP);
	pthread_mutex_destroy(&NextPMutex);
	free(threads);

	PRINT_MSG(MSG_ERROR, "All threads died!");
}
Beispiel #16
0
static size_t
e_mpf_number (mpf_ptr res, const char *e, size_t elen, int base)
{
  char    *edup;
  size_t  i, ret, extra=0;
  int     mant_base, exp_base;
  void    *(*allocate_func) (size_t);
  void    (*free_func) (void *, size_t);

  TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e));

  /* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it
     here instead.  FIXME: Would prefer to let mpf_set_str handle this.  */
  if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X'))
    {
      base = 16;
      extra = 2;
      e += extra;
      elen -= extra;
    }

  if (base == 0)
    mant_base = 10;
  else if (base < 0)
    mant_base = -base;
  else
    mant_base = base;

  /* exponent in decimal if base is negative */
  if (base < 0)
    exp_base = 10;
  else if (base == 0)
    exp_base = 10;
  else
    exp_base = base;

#define IS_EXPONENT(c) \
  (c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E')))

  i = 0;
  for (;;)
    {
      if (i >= elen)
        goto parsed;
      if (e[i] == '.')
        break;
      if (IS_EXPONENT (e[i]))
        goto exponent;
      if (! isasciidigit_in_base (e[i], mant_base))
        goto parsed;
      i++;
    }

  /* fraction */
  i++;
  for (;;)
    {
      if (i >= elen)
        goto parsed;
      if (IS_EXPONENT (e[i]))
        goto exponent;
      if (! isasciidigit_in_base (e[i], mant_base))
        goto parsed;
      i++;
    }

 exponent:
  i++;
  if (i >= elen)
    goto parsed;
  if (e[i] == '-')
    i++;
  for (;;)
    {
      if (i >= elen)
        goto parsed;
      if (! isasciidigit_in_base (e[i], exp_base))
        break;
      i++;
    }

 parsed:
  TRACE (printf ("  parsed i=%u \"%.*s\"\n", i, (int) i, e));

  mp_get_memory_functions (&allocate_func, NULL, &free_func);
  edup = (*allocate_func) (i+1);
  memcpy (edup, e, i);
  edup[i] = '\0';

  if (mpf_set_str (res, edup, base) == 0)
    ret = i + extra;
  else
    ret = 0;

  (*free_func) (edup, i+1);
  return ret;
}
Beispiel #17
0
void _jl_gmp_free(void *p)
{
    void (*freefunc) (void *, size_t);
    mp_get_memory_functions (NULL, NULL, &freefunc);
    freefunc(p, 0);
}