[m-rev.] for review: new runtime option for setting fp rounding mode
Julien Fischer
juliensf at csse.unimelb.edu.au
Mon Nov 3 13:31:58 AEDT 2008
Hi,
Could some please take a look at this one.
Thanks
Julien.
On Tue, 28 Oct 2008, Julien Fischer wrote:
> Add a new option to the Mercury runtime that allows the floating point
> roundingmode to be set via the MERCURY_OPTIONS variable. Being able to
> change the
> rounding mode is useful when debugging stability problems with numerical
> algorithms. This only works on systems that support the relevant
> bits of C99.
>
> configure.in:
> Check for the header file fenv.h and the function fesetround().
> The presence of the former should imply that latter but C99 support
> is still a bit dodgy on some systems (and non-existent on others.)
>
> aclocal.m4:
> Add a new macro for use by the above (and which could be further used
> for testing for functions declared in fenv.h if we find we need
> them.)
>
> runtime/mercury_conf.h.in:
> Add new macros MR_HAVE_FENV_H and MR_HAVE_FESETROUND that are defined
> when the above are available.
>
> runtime/mercury_wrapper.c:
> Implement the new runtime option. (Attempting to change the rounding
> mode on systems that do not support doing so, or that do not support
> the selected rounding mode, will cause an error message to be emitted
> and execution to abort.)
>
> doc/user_guide.texi:
> Document the new option.
>
> Fix some typos in other places.
>
> Julien.
>
> Index: aclocal.m4
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/aclocal.m4,v
> retrieving revision 1.28
> diff -u -r1.28 aclocal.m4
> --- aclocal.m4 20 Jul 2007 06:55:26 -0000 1.28
> +++ aclocal.m4 28 Oct 2008 08:00:12 -0000
> @@ -54,6 +54,43 @@
> ])
>
> #-----------------------------------------------------------------------------#
> +
> +# Test for C99 fp environment functions in the math library.
> +# The second argument to this macro should be the flags required by the
> +# C compiler to link against the math library. This is needed because
> +# some OSs, e.g. Mac OS X, don't have a separate math library.
> +
> +AC_DEFUN([MERCURY_CHECK_FOR_FENV_FUNC],
> +[
> +AC_MSG_CHECKING(for $1 function)
> +mercury_cv_math_func_define="MR_HAVE_`echo $1 | \
> + tr abcdefghijklmnopqrstuvwxyz./ ABCDEFGHIJKLMNOPQRSTUVWXYZ__`"
> +
> +save_libs="$LIBS"
> +LIBS="$2 $LIBS"
> +
> +AC_TRY_LINK([
> +#ifdef MR_HAVE_FENV_H
> + #include <fenv.h>
> +#endif
> +],[
> +
> + int i = 0;
> + $1(i);
> +],[mercury_cv_have_math_func=yes],[mercury_cv_have_math_func=no])
> +
> +LIBS="$save_libs"
> +
> +if test "$mercury_cv_have_math_func" = "yes"
> +then
> + AC_MSG_RESULT([yes])
> + AC_DEFINE_UNQUOTED([$mercury_cv_math_func_define])
> +else
> + AC_MSG_RESULT([no])
> +fi
> +])
> +
> +#-----------------------------------------------------------------------------#
> #
> # Turn off MacOS's so-called "smart" C preprocessor, if present,
> # since it causes lots of spurious warning messages,
> Index: configure.in
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/configure.in,v
> retrieving revision 1.529
> diff -u -r1.529 configure.in
> --- configure.in 28 Oct 2008 05:03:59 -0000 1.529
> +++ configure.in 28 Oct 2008 06:52:53 -0000
> @@ -1090,7 +1090,7 @@
> asm/sigcontext.h sys/param.h sys/time.h sys/times.h \
> sys/types.h sys/stat.h fcntl.h termios.h sys/ioctl.h \
> sys/stropts.h windows.h dirent.h getopt.h malloc.h \
> - semaphore.h pthread.h time.h spawn.h)
> + semaphore.h pthread.h time.h spawn.h fenv.h)
>
> if test "$MR_HAVE_GETOPT_H" = 1; then
> GETOPT_H_AVAILABLE=yes
> @@ -1105,6 +1105,13 @@
>
> #-----------------------------------------------------------------------------#
> #
> +# Check whether we can set the FP roudning mode
> +#
> +
> +MERCURY_CHECK_FOR_FENV_FUNC([fesetround], [$MATH_LIB])
> +
> +#-----------------------------------------------------------------------------#
> +#
> # Check the basics of sigaction
> #
>
> Index: doc/user_guide.texi
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/doc/user_guide.texi,v
> retrieving revision 1.579
> diff -u -r1.579 user_guide.texi
> --- doc/user_guide.texi 24 Oct 2008 02:47:43 -0000 1.579
> +++ doc/user_guide.texi 28 Oct 2008 07:46:37 -0000
> @@ -7617,7 +7617,7 @@
> @sp 1
> @item @code{--no-profile-deep-coverage-use-portcounts}
> @findex --no-profile-deep-coverage-use-portcounts
> -Turn off usage of port counts in the deep profilier to provide some coverage
> +Turn off usage of port counts in the deep profiler to provide some coverage
> information.
>
> @sp
> @@ -9692,11 +9692,11 @@
>
> @sp 1
> @item --trail-segment-size-kwords @var{size}
> - at findex --trail-seggment-size-kwords
> + at findex --trail-segment-size-kwords
> @cindex Trail size
> Set the size of each trail segment to be @var{size} kilobytes
> multiplied by the words size in bytes.
> -This option is ignored in grades that do not use trail segements.
> +This option is ignored in grades that do not use trail segments.
>
> @sp 1
> @item --genstack-size @var{size}
> @@ -9989,6 +9989,14 @@
> in the @samp{benchmarking} module of the standard library
> to report this information.
>
> + at sp 1
> + at item --fp-rounding-mode @var{mode}
> + at findex --fp-rounding-mode @var{mode}
> +Set the rounding mode for floating point operations to @var{mode}.
> +Recognised modes are @samp{downward}, @samp{upward}, @samp{to_nearest}
> +and @samp{toward_zero}. Exactly what modes are available and even
> +if it is possible to set the rounding mode is system dependent.
> +
> @end table
>
> @sp 1
> Index: runtime/mercury_conf.h.in
> ===================================================================
> RCS file:
> /home/mercury/mercury1/repository/mercury/runtime/mercury_conf.h.in,v
> retrieving revision 1.61
> diff -u -r1.61 mercury_conf.h.in
> --- runtime/mercury_conf.h.in 22 Jan 2008 02:36:37 -0000 1.61
> +++ runtime/mercury_conf.h.in 28 Oct 2008 06:56:42 -0000
> @@ -133,6 +133,7 @@
> ** MR_HAVE_PTHREAD_H we have <pthread.h>
> ** MR_HAVE_TIME_H we have <time.h>
> ** MR_HAVE_SPAWN_H we have <spawn.h>
> +** MR_HAVE_FENV_H we have <fenv.h>
> */
> #undef MR_HAVE_SYS_SIGINFO_H
> #undef MR_HAVE_SYS_SIGNAL_H
> @@ -159,6 +160,7 @@
> #undef MR_HAVE_PTHREAD_H
> #undef MR_HAVE_TIME_H
> #undef MR_HAVE_SPAWN_H
> +#undef MR_HAVE_FENV_H
>
> /*
> ** MR_HAVE_POSIX_TIMES is defined if we have the POSIX
> @@ -256,6 +258,7 @@
> ** MR_HAVE_PUTENV we have the putenv() function.
> ** MR_HAVE__PUTENV we have the _putenv() function.
> ** MR_HAVE_POSIX_SPAWN we have the posix_spawn() function.
> +** MR_HAVE_FESETROUND we have the fesetround() function.
> */
> #undef MR_HAVE_GETPID
> #undef MR_HAVE_SETPGID
> @@ -316,6 +319,7 @@
> #undef MR_HAVE_PUTENV
> #undef MR_HAVE__PUTENV
> #undef MR_HAVE_POSIX_SPAWN
> +#undef MR_HAVE_FESETROUND
>
> /*
> ** We use mprotect() and signals to catch stack and heap overflows.
> Index: runtime/mercury_wrapper.c
> ===================================================================
> RCS file:
> /home/mercury/mercury1/repository/mercury/runtime/mercury_wrapper.c,v
> retrieving revision 1.193
> diff -u -r1.193 mercury_wrapper.c
> --- runtime/mercury_wrapper.c 16 Sep 2008 07:52:27 -0000 1.193
> +++ runtime/mercury_wrapper.c 28 Oct 2008 07:38:46 -0000
> @@ -51,6 +51,10 @@
> #include <excpt.h>
> #endif
>
> +#ifdef MR_HAVE_FENV_H
> + #include <fenv.h>
> +#endif
> +
> #include "mercury_getopt.h"
> #include "mercury_timing.h"
> #include "mercury_init.h"
> @@ -1221,7 +1225,8 @@
> MR_MEM_USAGE_REPORT,
> MR_BOEHM_GC_MUNMAP,
> MR_BOEHM_GC_FREE_SPACE_DIVISOR,
> - MR_BOEHM_GC_CALC_TIME
> + MR_BOEHM_GC_CALC_TIME,
> + MR_FP_ROUNDING_MODE
> };
>
> struct MR_option MR_long_opts[] = {
> @@ -1325,6 +1330,7 @@
> { "boehm-gc-munmap", 0, 0, MR_BOEHM_GC_MUNMAP },
> { "boehm-gc-free-space-divisor", 1, 0, MR_BOEHM_GC_FREE_SPACE_DIVISOR
> },
> { "boehm-gc-calc-time", 0, 0, MR_BOEHM_GC_CALC_TIME },
> + { "fp-rounding-mode", 1, 0, MR_FP_ROUNDING_MODE },
>
> /* This needs to be kept at the end. */
> { NULL, 0, 0, 0 }
> @@ -1899,6 +1905,70 @@
> #endif
> break;
>
> + case MR_FP_ROUNDING_MODE:
> +#if defined(MR_HAVE_FENV_H) && defined(MR_HAVE_FESETROUND)
> + {
> + int rounding_mode;
> + + /*
> + ** Particular rounding modes are only supported if the
> + ** corresponding FE_* macro is defined. The four below
> are
> + ** the ones from C99. C99 says that these macros
> + ** should expand to a nonnegative value, so we use a
> negative value
> + ** to indicate that the selected rounding mode is not
> supported by
> + ** the system.
> + */
> + if (MR_streq(MR_optarg, "downward")) {
> + #if defined(FE_DOWNWARD)
> + rounding_mode = FE_DOWNWARD;
> + #else
> + rounding_mode = -1;
> + #endif
> + } else if (MR_streq(MR_optarg, "upward")) {
> + #if defined(FE_UPWARD)
> + rounding_mode = FE_UPWARD;
> + #else
> + rounding_mode = -1;
> + #endif + } else if
> (MR_streq(MR_optarg, "toward_zero")) {
> + #if defined(FE_TOWARDZERO)
> + rounding_mode = FE_TOWARDZERO;
> + #else
> + rounding_mode = -1;
> + #endif
> + } else if (MR_streq(MR_optarg, "to_nearest")) {
> + #if defined(FE_TONEAREST)
> + rounding_mode = FE_TONEAREST;
> + #else
> + rounding_mode = -1;
> + #endif
> + } else {
> + MR_usage();
> + }
> +
> + if (rounding_mode < 0) {
> + printf("Mercury runtime: the selected rounding mode
> is "
> + "not supported by this system.\n");
> + fflush(stdout);
> + exit(1);
> + } else {
> + if (fesetround(rounding_mode) != 0) {
> + printf("Mercury runtime: could not establish
> selected "
> + "rounding mode.\n");
> + fflush(stdout);
> + exit(1);
> + }
> + }
> + }
> +#else
> + printf("Mercury runtime: `--fp-rounding-mode' is specified "
> + "in MERCURY_OPTIONS\n");
> + printf("but the rounding mode cannot be changed on this
> platform.\n");
> + fflush(stdout);
> + exit(1);
> +#endif
> + break;
> +
> case 'a':
> benchmark_all_solns = MR_TRUE;
> break;
>
> --------------------------------------------------------------------------
> mercury-reviews mailing list
> Post messages to: mercury-reviews at csse.unimelb.edu.au
> Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
> Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
> --------------------------------------------------------------------------
>
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list