diff --git a/cblas.h b/cblas.h index 89b2f99cde..df278a57d4 100644 --- a/cblas.h +++ b/cblas.h @@ -73,26 +73,16 @@ typedef void (*openblas_xerbla_handler)(const char *name, #endif OPENBLAS_EXPORT openblas_xerbla_handler openblas_set_xerbla(openblas_xerbla_handler handler); -/* Cooperative cancellation of in-flight operations. - * - * Every thread owns a pointer-sized generation slot; - * openblas_cancel_token() returns its (stable) address for the calling - * thread. Supported compute drivers (currently the level-3 GEMM drivers) - * advance the slot to a fresh even generation at operation entry on the - * issuing thread and poll it during the computation. To cancel the - * operation in flight on a thread, load that thread's slot value, verify - * the operation you mean to cancel is still the current one, and call - * openblas_cancel(token, loaded_value): the cancel bit (bit 0) is set iff - * the slot still holds the loaded value, so stale requests cannot affect - * later operations. A cancelled operation leaves its output in an - * unspecified, partially-updated state; the caller must discard the - * result. The library itself stays consistent and can service further - * calls. openblas_cancel_token() may return NULL if per-thread state - * cannot be allocated; cancellation is then unavailable on that thread - * (openblas_cancel(NULL, ...) is a harmless no-op). These symbols are - * exported without any symbol prefix/suffix decoration. */ -size_t *openblas_cancel_token(void); -OPENBLAS_EXPORT void openblas_cancel(size_t *token, size_t loaded_token); +/* Cooperative cancellation of in-flight operations: designate `*flag` as the + * cancellation flag of the calling thread (NULL: none, the default), and + * return the previously designated flag, so that a nested designation can + * restore it. Supported compute drivers (currently the level-3 GEMM + * drivers) poll the issuing thread's flag during the computation and + * return early once it is nonzero, leaving their output in an unspecified, + * partially-updated state; the caller must discard the result. OpenBLAS + * only reads the flag: the caller sets it atomically (from any thread or + * signal handler) and must keep the pointer valid until unset. */ +OPENBLAS_EXPORT const unsigned char *openblas_set_thread_cancel_flag(const unsigned char *flag); #ifdef OPENBLAS_OS_LINUX /* Sets thread affinity for OpenBLAS threads. `thread_idx` is in [0, openblas_get_num_threads()-1]. */ diff --git a/common.h b/common.h index 63b22e4255..b7d5735a5c 100644 --- a/common.h +++ b/common.h @@ -772,15 +772,17 @@ int get_node_equal (void); OPENBLAS_EXPORT void goto_set_num_threads(int); /* Cooperative cancellation of in-flight operations - * (implemented in driver/others/openblas_cancel.c). These symbols are - * exported without SYMBOLPREFIX/SYMBOLSUFFIX decoration. */ -size_t *openblas_cancel_token(void); -OPENBLAS_EXPORT void openblas_cancel(size_t *token, size_t loaded_token); - -/* Internal helpers for the instrumented compute drivers. */ -size_t openblas_cancel_begin(void); -size_t *openblas_cancel_self(void); -int openblas_cancel_poll(size_t *slot, size_t gen); + * (implemented in driver/others/openblas_cancel.c). */ +OPENBLAS_EXPORT const unsigned char *openblas_set_thread_cancel_flag(const unsigned char *flag); +const unsigned char *openblas_get_thread_cancel_flag(void); +#if defined(HAVE_C11) && !defined(__cplusplus) +#define OPENBLAS_CANCELLED(flag) \ + ((flag) != NULL && atomic_load_explicit( \ + (volatile _Atomic unsigned char *)(flag), memory_order_relaxed) != 0) +#else +#define OPENBLAS_CANCELLED(flag) \ + ((flag) != NULL && *(volatile const unsigned char *)(flag) != 0) +#endif void gotoblas_affinity_init(void); void gotoblas_affinity_quit(void); diff --git a/common_macro.h b/common_macro.h index 6b0a61559f..a9219bfc1a 100644 --- a/common_macro.h +++ b/common_macro.h @@ -3002,12 +3002,8 @@ typedef struct { void * routine; int routine_mode; - /* Generation slot of the thread that issued this operation and the - generation it runs as (see driver/others/openblas_cancel.c), or - NULL/0. Consulted only by the level-3 thread drivers; must be set - explicitly wherever it is to be observed. */ - size_t * cancel_slot; - size_t cancel_gen; + /* Cancellation flag of the thread that issued this operation, or NULL. */ + const unsigned char * cancel_flag; } blas_arg_t; #endif diff --git a/driver/level3/level3.c b/driver/level3/level3.c index b1451b09ee..c9deb019e8 100644 --- a/driver/level3/level3.c +++ b/driver/level3/level3.c @@ -214,14 +214,11 @@ int CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, double total; #endif - /* Begin a cancellable generation on the calling thread and poll it at - * block granularity; openblas_cancel() against this thread's slot makes - * the operation return early, leaving C partially updated. (When this - * driver runs on a BLAS worker thread on behalf of another operation, - * the generation is the worker's own, which nobody cancels; the outer - * operation's cancellation is handled by the threading driver.) */ - size_t cancel_gen = openblas_cancel_begin(); - size_t *cancel_slot = openblas_cancel_self(); + /* Poll the calling thread's cancellation flag at block granularity; + * once it is nonzero the operation returns early, leaving C partially + * updated. (A BLAS worker thread has no flag of its own; the threading + * driver forwards the issuing thread's flag instead.) */ + const unsigned char *cancel_flag = openblas_get_thread_cancel_flag(); k = K; @@ -312,14 +309,14 @@ int CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, #endif for(js = n_from; js < n_to; js += GEMM_R){ - if (openblas_cancel_poll(cancel_slot, cancel_gen)) return 0; + if (OPENBLAS_CANCELLED(cancel_flag)) return 0; min_j = n_to - js; if (min_j > GEMM_R) min_j = GEMM_R; for(ls = 0; ls < k; ls += min_l){ - if (openblas_cancel_poll(cancel_slot, cancel_gen)) return 0; + if (OPENBLAS_CANCELLED(cancel_flag)) return 0; min_l = k - ls; @@ -367,7 +364,7 @@ int CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, #else for(jjs = js; jjs < js + min_j; jjs += min_jj){ - if (openblas_cancel_poll(cancel_slot, cancel_gen)) return 0; + if (OPENBLAS_CANCELLED(cancel_flag)) return 0; min_jj = min_j + js - jjs; #if defined(SKYLAKEX) || defined(COOPERLAKE) || defined(SAPPHIRERAPIDS) @@ -406,7 +403,7 @@ int CNAME(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, #endif for(is = m_from + min_i; is < m_to; is += min_i){ - if (openblas_cancel_poll(cancel_slot, cancel_gen)) return 0; + if (OPENBLAS_CANCELLED(cancel_flag)) return 0; min_i = m_to - is; diff --git a/driver/level3/level3_thread.c b/driver/level3/level3_thread.c index 680a9da39c..bbe7e127ea 100644 --- a/driver/level3/level3_thread.c +++ b/driver/level3/level3_thread.c @@ -254,15 +254,14 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *c; job_t *job = (job_t *)args -> common; - /* Cancellation token forwarded (by gemm_driver) from the thread that + /* Cancellation flag forwarded (by gemm_driver) from the thread that * issued this operation, or NULL. Once cancellation is observed the * remaining compute (copy/kernel calls) is skipped, but every * synchronization point - buffer publication and flag clearing - is * still executed, so sibling threads never deadlock and the operation * finishes quickly with C left in an unspecified, partially-updated * state. */ - size_t *cancel_slot = args -> cancel_slot; - size_t cancel_gen = args -> cancel_gen; + const unsigned char *cancel_flag = args -> cancel_flag; int cancelled = 0; BLASLONG nthreads_m; @@ -329,7 +328,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, n_to = range_n[mypos + 1]; } - cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + cancelled = OPENBLAS_CANCELLED(cancel_flag); /* Multiply C by beta if needed (skipped when cancelled) */ if (beta && !cancelled) { @@ -360,7 +359,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, for(ls = 0; ls < k; ls += min_l){ /* Poll for cancellation at block granularity */ - if (!cancelled) cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + if (!cancelled) cancelled = OPENBLAS_CANCELLED(cancel_flag); /* Determine step size in k */ min_l = k - ls; @@ -414,7 +413,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, /* Fused operation to copy region of B into workspace and apply kernel * (skipped when cancelled; the buffer is still published below so * sibling threads do not stall) */ - if (!cancelled) cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + if (!cancelled) cancelled = OPENBLAS_CANCELLED(cancel_flag); if (!cancelled) FUSED_KERNEL_OPERATION(min_i, MIN(n_to, js + div_n) - js, min_l, alpha, sa, buffer[bufferside], b, ldb, c, ldc, m_from, js, ls); @@ -439,7 +438,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, /* Poll for cancellation between blocks; when cancelled, skip the * copy and kernel but keep iterating so the buffer is still * published below and sibling threads do not stall */ - if (!cancelled) cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + if (!cancelled) cancelled = OPENBLAS_CANCELLED(cancel_flag); if (!cancelled) { /* Copy part of local region of B into workspace */ @@ -492,7 +491,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, /* Apply kernel with local region of A and part of other region of B * (skipped when cancelled; the flag is still cleared below) */ - if (!cancelled) cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + if (!cancelled) cancelled = OPENBLAS_CANCELLED(cancel_flag); if (!cancelled) { START_RPCC(); KERNEL_OPERATION(min_i, MIN(range_n[current + 1] - js, div_n), min_l, alpha, @@ -518,7 +517,7 @@ static int inner_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, * Note: First step has already been finished */ for(is = m_from + min_i; is < m_to; is += min_i){ /* Poll for cancellation between blocks */ - if (!cancelled) cancelled = openblas_cancel_poll(cancel_slot, cancel_gen); + if (!cancelled) cancelled = OPENBLAS_CANCELLED(cancel_flag); min_i = m_to - is; if (min_i >= GEMM_P * 2) { @@ -686,11 +685,9 @@ static int gemm_driver(blas_arg_t *args, BLASLONG *range_m, BLASLONG newarg.nthreads = args -> nthreads; newarg.common = (void *)job; - /* Begin a cancellable generation on this (the calling, i.e. issuing) - * thread and forward it to the worker threads through the job - * arguments. */ - newarg.cancel_gen = openblas_cancel_begin(); - newarg.cancel_slot = openblas_cancel_self(); + /* Forward the cancellation flag of this (the calling, i.e. issuing) + * thread to the worker threads through the job arguments. */ + newarg.cancel_flag = openblas_get_thread_cancel_flag(); #ifdef PARAMTEST newarg.gemm_p = args -> gemm_p; newarg.gemm_q = args -> gemm_q; @@ -798,7 +795,7 @@ static int gemm_driver(blas_arg_t *args, BLASLONG *range_m, BLASLONG /* Skip the remaining column blocks once this operation has been * cancelled. Each exec_blas() round is a full barrier, so bailing * out between rounds leaves the library in a consistent state. */ - if (openblas_cancel_poll(newarg.cancel_slot, newarg.cancel_gen)) break; + if (OPENBLAS_CANCELLED(newarg.cancel_flag)) break; } #ifdef USE_ALLOC_HEAP diff --git a/driver/others/openblas_cancel.c b/driver/others/openblas_cancel.c index 8286bd5ecd..6c75ca70a1 100644 --- a/driver/others/openblas_cancel.c +++ b/driver/others/openblas_cancel.c @@ -31,45 +31,27 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **********************************************************************************/ -#include #include "common.h" -/* Cooperative cancellation of in-flight OpenBLAS operations. +/* Cooperative cancellation of in-flight operations. * - * Every thread owns a pointer-sized generation slot in thread-local - * storage: + * Every thread may designate a cancellation flag: a byte that the caller + * owns and that becomes nonzero when the operations issued by that thread + * are to be abandoned. openblas_set_thread_cancel_flag() sets the + * calling thread's flag (NULL: none, the default) and returns the previous + * one, so that a nested designation can restore it. Supported compute + * drivers (currently the level-3 GEMM drivers) read the issuing + * thread's flag pointer at operation entry and poll the flag at block + * granularity; once it is nonzero the operation returns early, leaving + * its output in an unspecified, partially-updated state. The library + * itself stays consistent and can service further calls. * - * - openblas_cancel_token() returns the address of the calling thread's - * slot. The address is stable for the lifetime of the thread. - * - Instrumented compute drivers advance the slot to a fresh even value - * at operation entry (on the issuing thread) and poll it at block - * granularity: any change - the cancel bit or a later generation - - * makes the operation abandon its remaining work and return early. - * - openblas_cancel(token, loaded_token) requests cancellation of the - * operation that was in flight when the caller loaded *token: it - * atomically sets the cancel bit iff the slot still holds - * loaded_token. A racing or stale request (the operation completed; - * a new one may have started) either fails the compare-exchange or - * dirties an already-dead generation - both harmless. May be called - * from any thread. - * - * A cancelled operation returns quickly but leaves its output buffers in - * an unspecified, partially-updated state; the caller is responsible for - * discarding the result. The library itself remains in a consistent - * state and can service further calls. - * - * These symbols are deliberately exported without any SYMBOLPREFIX / - * SYMBOLSUFFIX decoration, like the other openblas_* utility functions - * that are not per-precision entry points. + * OpenBLAS only ever reads the flag: setting it (from any thread) and + * keeping it valid while it is designated are the caller's business, and + * a flag that stays nonzero simply makes every later operation issued + * under it return early. */ -/* Storage for the per-thread generation slot. Compiler-emitted TLS is - * used where the target supports it (clang reports per-target support - * via __has_feature(tls); e.g. 32-bit iOS with an old deployment minimum - * lacks it). Where it does not, the slot falls back to a - * pthread_getspecific() lookup of a lazily heap-allocated slot: slightly - * more expensive at operation entry, but the compute drivers cache the - * slot's address, so the polling fast path is identical. */ #if defined(_MSC_VER) && !defined(__clang__) #define OPENBLAS_CANCEL_TLS __declspec(thread) #elif defined(__clang__) @@ -82,107 +64,41 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef OPENBLAS_CANCEL_TLS -/* This thread's generation slot. Bit 0 is the cancel flag; the remaining - * bits count operations issued by this thread. */ -static OPENBLAS_CANCEL_TLS size_t openblas_cancel_slot = 0; +static OPENBLAS_CANCEL_TLS const unsigned char *openblas_cancel_flag = NULL; -/* Address of the calling thread's generation slot. */ -static size_t *openblas_cancel_slot_addr(void) { - return (size_t *)&openblas_cancel_slot; +const unsigned char *openblas_set_thread_cancel_flag(const unsigned char *flag) { + const unsigned char *old = openblas_cancel_flag; + openblas_cancel_flag = flag; + return old; +} + +const unsigned char *openblas_get_thread_cancel_flag(void) { + return openblas_cancel_flag; } #else #include -#include static pthread_key_t openblas_cancel_key; static pthread_once_t openblas_cancel_key_once = PTHREAD_ONCE_INIT; static int openblas_cancel_key_ok = 0; static void openblas_cancel_key_init(void) { - openblas_cancel_key_ok = (pthread_key_create(&openblas_cancel_key, free) == 0); + openblas_cancel_key_ok = (pthread_key_create(&openblas_cancel_key, NULL) == 0); } -/* Address of the calling thread's generation slot, allocated on first - * use and freed by the key destructor at thread exit. Returns NULL if - * per-thread state cannot be set up; cancellation is then unavailable - * on this thread (all other entry points accept a NULL slot). */ -static size_t *openblas_cancel_slot_addr(void) { - size_t *slot; +const unsigned char *openblas_get_thread_cancel_flag(void) { pthread_once(&openblas_cancel_key_once, openblas_cancel_key_init); if (!openblas_cancel_key_ok) return NULL; - slot = (size_t *)pthread_getspecific(openblas_cancel_key); - if (slot == NULL) { - slot = (size_t *)calloc(1, sizeof(size_t)); - if (slot == NULL) return NULL; - if (pthread_setspecific(openblas_cancel_key, slot) != 0) { - free(slot); - return NULL; - } - } - return slot; -} - -#endif - -#if defined(__GNUC__) || defined(__clang__) -#define OPENBLAS_CANCEL_LOAD(PTR) __atomic_load_n((PTR), __ATOMIC_RELAXED) -#define OPENBLAS_CANCEL_STORE(PTR, VAL) __atomic_store_n((PTR), (VAL), __ATOMIC_RELEASE) -#define OPENBLAS_CANCEL_CAS(PTR, EXPP, VAL) \ - __atomic_compare_exchange_n((PTR), (EXPP), (VAL), 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED) -#else -/* Fallback for compilers without the GNU atomic builtins. The slot is - * only advanced by its owning thread; the cancel request degrades to a - * check-then-store, which can at worst dirty a dead generation. */ -#define OPENBLAS_CANCEL_LOAD(PTR) (*(volatile size_t *)(PTR)) -#define OPENBLAS_CANCEL_STORE(PTR, VAL) do { WMB; *(volatile size_t *)(PTR) = (VAL); } while (0) -static int openblas_cancel_cas_fallback(size_t *ptr, size_t *expp, size_t val) { - if (*(volatile size_t *)ptr != *expp) return 0; - WMB; - *(volatile size_t *)ptr = val; - return 1; -} -#define OPENBLAS_CANCEL_CAS(PTR, EXPP, VAL) openblas_cancel_cas_fallback((PTR), (EXPP), (VAL)) -#endif - -/* Address of the calling thread's generation slot (NULL if unavailable). */ -size_t *openblas_cancel_token(void) { - return openblas_cancel_slot_addr(); -} - -/* Request cancellation of the operation that was in flight on the token's - * thread when the caller loaded LOADED_TOKEN from it. May be called from - * any thread. */ -void openblas_cancel(size_t *token, size_t loaded_token) { - if (token == NULL) return; - OPENBLAS_CANCEL_CAS(token, &loaded_token, loaded_token | (size_t)1); + return (const unsigned char *)pthread_getspecific(openblas_cancel_key); } -/* --- internal helpers (not part of the public API) --------------------- */ - -/* Begin an instrumented operation on the calling thread: advance the slot - * past any stale cancel bit to a fresh even generation and return it. - * Returns 0 if the thread has no slot (paired with a NULL slot from - * openblas_cancel_self, so polling never reports cancellation). */ -size_t openblas_cancel_begin(void) { - size_t *slot = openblas_cancel_slot_addr(); - size_t gen; - if (slot == NULL) return 0; - gen = (*slot | (size_t)1) + 1; - OPENBLAS_CANCEL_STORE(slot, gen); - return gen; +const unsigned char *openblas_set_thread_cancel_flag(const unsigned char *flag) { + const unsigned char *old = openblas_get_thread_cancel_flag(); + if (openblas_cancel_key_ok) + pthread_setspecific(openblas_cancel_key, (void *)flag); + return old; } -/* The calling thread's slot address (same as openblas_cancel_token, for - * internal use without going through the exported symbol). */ -size_t *openblas_cancel_self(void) { - return openblas_cancel_slot_addr(); -} - -/* Cheap poll usable from compute drivers: nonzero once GEN is no longer - * the live value of SLOT (cancelled, or superseded). SLOT may be NULL. */ -int openblas_cancel_poll(size_t *slot, size_t gen) { - if (slot == NULL) return 0; - return OPENBLAS_CANCEL_LOAD(slot) != gen; -} +#endif diff --git a/exports/gensymbol b/exports/gensymbol index 8acb950c45..df7f7258ba 100755 --- a/exports/gensymbol +++ b/exports/gensymbol @@ -188,20 +188,12 @@ misc_no_underscore_objs=" openblas_get_config openblas_get_corename openblas_set_threads_callback_function + openblas_set_thread_cancel_flag openblas_set_xerbla " misc_underscore_objs="" -# Symbols that are exported exactly as spelled, without any SYMBOLPREFIX / -# SYMBOLSUFFIX decoration (like the openblas_* utility functions, these are -# not per-precision entry points). They are therefore deliberately left out -# of the objcopy/objconv rename lists. -misc_no_suffix_objs=" - openblas_cancel - openblas_cancel_token -" - # These routines are provided by OpenBLAS. lapackobjss=" sgesv @@ -4088,10 +4080,6 @@ case "$p1" in for obj in $no_underscore_objs; do printf '_%s%s%s\n' "$symbolprefix" "$obj" "$symbolsuffix" done - - for obj in $misc_no_suffix_objs; do - printf '_%s\n' "$obj" - done ;; aix) @@ -4200,12 +4188,6 @@ case "$p1" in done printf '\t%sopenblas_set_xerbla%s=openblas_set_xerbla @%s\n' \ "$symbolprefix" "$symbolsuffix" "$count" - count=`expr $count + 1` - - for obj in $misc_no_suffix_objs; do - printf '\t%s @%s\n' "$obj" "$count" - count=`expr $count + 1` - done ;; win2khpl) @@ -4258,10 +4240,6 @@ case "$p1" in "$symbolprefix" "$uppercase" "$symbolsuffix" "$obj" "$count" count=`expr $count + 1` done - - for obj in $misc_no_suffix_objs; do - printf '\t%s\n' "$obj" - done ;; linktest)