Skip to content

Commit

Permalink
Lazyly reinit threads after a fork in OMP mode
Browse files Browse the repository at this point in the history
This initializes the per-thread memory buffers which get
cleared/released on a fork via pthread_at_fork. Not doing so leads to
each thread calling blas_memory_alloc on almost every execution which
slows down the code significantly as the threads race for the memory
allocation using locks to serialize that.
  • Loading branch information
Flamefire committed Oct 1, 2020
1 parent d3c0d68 commit 3094fc6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions driver/others/blas_server_omp.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@

#else

#ifndef likely
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#else
#define likely(x) (x)
#endif
#endif
#ifndef unlikely
#ifdef __GNUC__
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define unlikely(x) (x)
#endif
#endif

#ifndef OMP_SCHED
#define OMP_SCHED static
#endif
Expand Down Expand Up @@ -350,6 +365,9 @@ static void exec_threads(blas_queue_t *queue, int buf_index){

int exec_blas(BLASLONG num, blas_queue_t *queue){

// Handle lazy re-init of the thread-pool after a POSIX fork
if (unlikely(blas_server_avail == 0)) blas_thread_init();

BLASLONG i, buf_index;

if ((num <= 0) || (queue == NULL)) return 0;
Expand Down

0 comments on commit 3094fc6

Please sign in to comment.