Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions cblas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]. */
Expand Down
20 changes: 11 additions & 9 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions common_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions driver/level3/level3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down
27 changes: 12 additions & 15 deletions driver/level3/level3_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading