diff --git a/config/ompi_setup_cc.m4 b/config/ompi_setup_cc.m4 index b73b8400af..dcf745003e 100644 --- a/config/ompi_setup_cc.m4 +++ b/config/ompi_setup_cc.m4 @@ -8,7 +8,7 @@ dnl of Tennessee Research Foundation. All rights dnl reserved. dnl Copyright (c) 2004-2006 High Performance Computing Center Stuttgart, dnl University of Stuttgart. All rights reserved. -dnl Copyright (c) 2004-2005 The Regents of the University of California. +dnl Copyright (c) 2004-2006 The Regents of the University of California. dnl All rights reserved. dnl $COPYRIGHT$ dnl @@ -153,6 +153,38 @@ AC_DEFUN([OMPI_SETUP_CC],[ unset add fi + # see if the C compiler supports __builtin_expect + AC_CACHE_CHECK([if $CC supports __builtin_expect], + [ompi_cv_cc_supports___builtin_expect], + [AC_TRY_COMPILE([], + [void *ptr = (void*) 0; + if (__builtin_expect (ptr != NULL, 1)) return 0;], + [ompi_cv_cc_supports___builtin_expect="yes"], + [ompi_cv_cc_supports___builtin_expect="no"])]) + if test "$ompi_cv_cc_supports___builtin_expect" = "yes" ; then + have_builtin_expect=1 + else + have_builtin_expect=0 + fi + AC_DEFINE_UNQUOTED([OMPI_C_HAVE_BUILTIN_EXPECT], [$have_builtin_expect], + [Whether C compiler supports __builtin_expect]) + + # see if the C compiler supports __builtin_prefetch + AC_CACHE_CHECK([if $CC supports __builtin_prefetch], + [ompi_cv_cc_supports___builtin_prefetch], + [AC_TRY_COMPILE([], + [int ptr; + __builtin_prefetch(&ptr);], + [ompi_cv_cc_supports___builtin_prefetch="yes"], + [ompi_cv_cc_supports___builtin_prefetch="no"])]) + if test "$ompi_cv_cc_supports___builtin_prefetch" = "yes" ; then + have_builtin_prefetch=1 + else + have_builtin_prefetch=0 + fi + AC_DEFINE_UNQUOTED([OMPI_C_HAVE_BUILTIN_PREFETCH], [$have_builtin_prefetch], + [Whether C compiler supports __builtin_prefetch]) + # Preload the optflags for the case where the user didn't specify # any. If we're using GNU compilers, use -O3 (since it GNU # doesn't require all compilation units to be compiled with the diff --git a/config/ompi_setup_cxx.m4 b/config/ompi_setup_cxx.m4 index 9167c08936..aafea2d404 100644 --- a/config/ompi_setup_cxx.m4 +++ b/config/ompi_setup_cxx.m4 @@ -8,7 +8,7 @@ dnl of Tennessee Research Foundation. All rights dnl reserved. dnl Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, dnl University of Stuttgart. All rights reserved. -dnl Copyright (c) 2004-2005 The Regents of the University of California. +dnl Copyright (c) 2004-2006 The Regents of the University of California. dnl All rights reserved. dnl $COPYRIGHT$ dnl @@ -135,6 +135,42 @@ AC_DEFUN([OMPI_SETUP_CXX],[ # config/cxx_find_template_parameters.m4 OMPI_CXX_FIND_TEMPLATE_PARAMETERS + # see if the C++ compiler supports __builtin_expect + AC_LANG_PUSH(C++) + AC_CACHE_CHECK([if $CXX supports __builtin_expect], + [ompi_cv_cxx_supports___builtin_expect], + [AC_TRY_COMPILE([], + [void *ptr = (void*) 0; + if (__builtin_expect (ptr != NULL, 1)) return 0;], + [ompi_cv_cxx_supports___builtin_expect="yes"], + [ompi_cv_cxx_supports___builtin_expect="no"])]) + if test "$ompi_cv_cxx_supports___builtin_expect" = "yes" ; then + have_builtin_expect=1 + else + have_builtin_expect=0 + fi + AC_DEFINE_UNQUOTED([OMPI_CXX_HAVE_BUILTIN_EXPECT], [$have_builtin_expect], + [Whether C++ compiler supports __builtin_expect]) + AC_LANG_POP(C++) + + # see if the C compiler supports __builtin_prefetch + AC_LANG_PUSH(C++) + AC_CACHE_CHECK([if $CXX supports __builtin_prefetch], + [ompi_cv_cxx_supports___builtin_prefetch], + [AC_TRY_COMPILE([], + [int ptr; + __builtin_prefetch(&ptr);], + [ompi_cv_cxx_supports___builtin_prefetch="yes"], + [ompi_cv_cxx_supports___builtin_prefetch="no"])]) + if test "$ompi_cv_cxx_supports___builtin_prefetch" = "yes" ; then + have_builtin_prefetch=1 + else + have_builtin_prefetch=0 + fi + AC_DEFINE_UNQUOTED([OMPI_CXX_HAVE_BUILTIN_PREFETCH], [$have_builtin_prefetch], + [Whether C++ compiler supports __builtin_prefetch]) + AC_LANG_POP(C++) + # If we are on HP-UX, ensure that we're using aCC case "$host" in *hpux*) diff --git a/opal/include/opal/prefetch.h b/opal/include/opal/prefetch.h new file mode 100644 index 0000000000..7b5f83939c --- /dev/null +++ b/opal/include/opal/prefetch.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2004-2006 The Regents of the University of California. + * All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + */ + +/** @file + * + * Compiler-specific prefetch functions + * + * A small set of prefetch / prediction interfaces for using compiler + * directives to improve memory prefetching and branch prediction + */ + +#ifndef OPAL_PREFETCH_H +#define OPAL_PREFETCH_H + +#if defined(c_plusplus) || defined(__cplusplus) +/* C++ code */ + +#if OMPI_CXX_HAVE_BUILTIN_EXPECT +#define OPAL_LIKELY(expression) __builtin_expect(!!(expression), 1) +#define OPAL_UNLIKELY(expression) __builtin_expect(!!(expression), 0) +#else +#define OPAL_LIKELY(expression) (expression) +#define OPAL_UNLIKELY(expression) (expression) +#endif + +#if OMPI_CXX_HAVE_BUILTIN_PREFETCH +#define OPAL_PREFETCH(address) __builtin_prefetch(address) +#else +#define OPAL_PREFETCH(address) +#endif + +#else +/* C code */ + +#if OMPI_C_HAVE_BUILTIN_EXPECT +#define OPAL_LIKELY(expression) __builtin_expect(!!(expression), 1) +#define OPAL_UNLIKELY(expression) __builtin_expect(!!(expression), 0) +#else +#define OPAL_LIKELY(expression) (expression) +#define OPAL_UNLIKELY(expression) (expression) +#endif + +#if OMPI_C_HAVE_BUILTIN_PREFETCH +#define OPAL_PREFETCH(address) __builtin_prefetch(address) +#else +#define OPAL_PREFETCH(address) +#endif + +#endif + +#endif