Skip to content

Commit

Permalink
Optionally disable thread-local storage. (#735)
Browse files Browse the repository at this point in the history
Details:
- Implemented a new configure option, --disable-tls, which allows the
  user to optionally disable the use of thread-local storage qualifiers
  on static variables in BLIS. This option will rarely be needed, but
  in some situations may allow BLIS to compile when TLS is unavailable.
  Thanks to Nick Knight for suggesting this option.
- Unlike the --disable-system option, --disable-tls does not forcibly
  disable threading. Instead, warnings of the possible consequences of
  using threading with TLS disabled are added to:
  - the output of './configure --help';
  - the output of 'configure' the --disable-tls option is parsed;
  - the informational header output by the testsuite.
  Thanks to Minh Quan Ho for suggesting these warnings.
- Modified frame/include/bli_lang_defs.h so that BLIS_THREAD_LOCAL is
  defined to nothing when BLIS_ENABLE_TLS is not defined.
- Defined bli_info_get_enable_tls(), which returns whether the cpp macro
  BLIS_ENABLE_TLS was defined.
- Edited --disable-system configure status output for clarity.
- Whitespace updates.
  • Loading branch information
fgvanzee authored Apr 3, 2023
1 parent 3f1432a commit aea8e1d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
6 changes: 6 additions & 0 deletions build/bli_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
#define BLIS_DISABLE_SYSTEM
#endif

#if @enable_tls@
#define BLIS_ENABLE_TLS
#else
#define BLIS_DISABLE_TLS
#endif

#if @enable_openmp@
#define BLIS_ENABLE_OPENMP
#if @enable_openmp_as_def@
Expand Down
38 changes: 37 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ print_usage()
library. When disabled, this option also forces the use
of --disable-threading.
--enable-tls, --disable-tls
Enable thread-local storage (TLS) for static variables
in BLIS. The default state is enabled. However, like with
--disable-system, there may be rare situations, such as
when --disable-system is appropriate, where thread-local
storage is unsupported by the compiler. In those cases,
disabling TLS may be a suitable workaround.
WARNING: DISABLING TLS IS DANGEROUS AND MAY CAUSE RACE
CONDITIONS! Please try combining --disable-tls with
--disable-threading if you suspect any correctness or
deadlock issues.
--disable-pba-pools, --enable-pba-pools
--disable-sba-pools, --enable-sba-pools
Expand Down Expand Up @@ -2512,6 +2525,9 @@ main()
# The system flag.
enable_system='yes'

# The thread-local storage flag.
enable_tls='yes'

# The threading flag.
threading_model='off'

Expand Down Expand Up @@ -2692,6 +2708,13 @@ main()
enable_system='no'
;;

enable-tls)
enable_tls='yes'
;;
disable-tls)
enable_tls='no'
;;

enable-threading=*)
threading_model=${OPTARG#*=}
;;
Expand Down Expand Up @@ -3543,13 +3566,25 @@ main()
enable_system_01=1
else
echo "${script_name}: disabling operating system support."
echo "${script_name}: WARNING: all threading will be disabled!"
echo "${script_name}: WARNING: disabling OS support forcibly disables all threading!"
enable_system_01=0

# Force threading to be disabled.
threading_model='off'
fi

# Check if we are building with or without thread-local storage support.
if [[ ${enable_tls} = yes ]]; then
echo "${script_name}: enabling thread-local storage (TLS) support."
enable_tls_01=1
else
echo "${script_name}: disabling thread-local storage (TLS) support."
echo "${script_name}: WARNING: THIS IS DANGEROUS! Disabling TLS may cause race conditions!"
echo "${script_name}: WARNING: Please try --disable-threading if you suspect any correctness"
echo "${script_name}: WARNING: or deadlock issues."
enable_tls_01=0
fi

# Check the threading model flag and standardize its value, if needed.
# Note that single-threaded mode will always be enabled, but not necessarily
# by default.
Expand Down Expand Up @@ -4202,6 +4237,7 @@ main()
| sed >"${bli_config_h_out_path}" \
-e "s/@version@/${version_esc}/g" \
-e "s/@enable_system@/${enable_system_01}/g" \
-e "s/@enable_tls@/${enable_tls_01}/g" \
-e "s/@enable_openmp@/${enable_openmp_01}/g" \
-e "s/@enable_openmp_as_def@/${enable_openmp_as_def_01}/g" \
-e "s/@enable_pthreads@/${enable_pthreads_01}/g" \
Expand Down
12 changes: 10 additions & 2 deletions frame/base/bli_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ gint_t bli_info_get_enable_sba_pools( void )
}
gint_t bli_info_get_enable_threading( void )
{
if ( bli_info_get_enable_openmp() ||
if ( bli_info_get_enable_openmp() ||
bli_info_get_enable_pthreads() ||
bli_info_get_enable_hpx() ) return 1;
else return 0;
else return 0;
}
gint_t bli_info_get_enable_openmp( void )
{
Expand Down Expand Up @@ -180,6 +180,14 @@ gint_t bli_info_get_thread_jrir_tlb( void )
return 0;
#endif
}
gint_t bli_info_get_enable_tls( void )
{
#ifdef BLIS_ENABLE_TLS
return 1;
#else
return 0;
#endif
}
gint_t bli_info_get_enable_memkind( void )
{
#ifdef BLIS_ENABLE_MEMKIND
Expand Down
1 change: 1 addition & 0 deletions frame/base/bli_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ BLIS_EXPORT_BLIS gint_t bli_info_get_enable_hpx_as_default( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_thread_jrir_slab( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_thread_jrir_rr( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_thread_jrir_tlb( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_enable_tls( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_enable_memkind( void );
BLIS_EXPORT_BLIS gint_t bli_info_get_enable_sandbox( void );

Expand Down
5 changes: 4 additions & 1 deletion frame/include/bli_lang_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@
// doesn't support __thread, as __GNUC__ is not quite unique to GCC.
// But the possibility of someone using such non-main-stream compiler
// for building BLIS is low.
#if defined(__GNUC__) || defined(__clang__) || defined(__ICC) || defined(__IBMC__)
#if defined(BLIS_ENABLE_TLS) && ( defined(__GNUC__) || \
defined(__clang__) || \
defined(__ICC) || \
defined(__IBMC__) )
#define BLIS_THREAD_LOCAL __thread
#else
#define BLIS_THREAD_LOCAL
Expand Down
14 changes: 14 additions & 0 deletions testsuite/src/test_libblis.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ void libblis_test_output_params_struct( FILE* os, test_params_t* params )
bli_rntm_set_ways_for_op( BLIS_TRSM, BLIS_LEFT, m, n, k, &trsm_l );
bli_rntm_set_ways_for_op( BLIS_TRSM, BLIS_RIGHT, m, n, k, &trsm_r );

const bool tls_enabled = bli_info_get_enable_tls();
const bool thr_enabled = bli_info_get_enable_threading();

// Output some system parameters.
libblis_test_fprintf_c( os, "\n" );
libblis_test_fprintf_c( os, "--- BLIS library info -------------------------------------\n" );
Expand Down Expand Up @@ -916,6 +919,17 @@ void libblis_test_output_params_struct( FILE* os, test_params_t* params )
libblis_test_fprintf_c( os, "\n" );
libblis_test_fprintf_c( os, "--- BLIS parallelization info ---\n" );
libblis_test_fprintf_c( os, "\n" );
libblis_test_fprintf_c( os, "thread-local storage (TLS) %d\n", ( int )tls_enabled );
if ( !tls_enabled && thr_enabled )
{
libblis_test_fprintf_c( os, "\n" );
libblis_test_fprintf_c( os, "[WARNING] BLIS was compiled with TLS disabled. We assume you know what\n" );
libblis_test_fprintf_c( os, "[WARNING] you're doing! Multithreaded race conditions, correctness\n" );
libblis_test_fprintf_c( os, "[WARNING] issues, and deadlocks may occur. If any of these happen,\n" );
libblis_test_fprintf_c( os, "[WARNING] please consider reconfiguring with --disable-threading.\n" );

}
libblis_test_fprintf_c( os, "\n" );
libblis_test_fprintf_c( os, "multithreading modes %s\n", impl_str );
libblis_test_fprintf_c( os, " default mode %s\n", def_impl_unset_str );
libblis_test_fprintf_c( os, " current mode %s\n", def_impl_set_str );
Expand Down

0 comments on commit aea8e1d

Please sign in to comment.