|
| 1 | +Give the Windows thread pool its own queue link, see MacPython/openblas-libs#283 |
| 2 | + |
| 3 | +diff --git a/common_thread.h b/common_thread.h |
| 4 | +--- a/common_thread.h |
| 5 | ++++ b/common_thread.h |
| 6 | +@@ -119,6 +119,14 @@ typedef struct blas_queue { |
| 7 | + struct blas_queue *next; |
| 8 | + |
| 9 | + #if defined( __WIN32__) || defined(__CYGWIN32__) || defined(_WIN32) || defined(__CYGWIN__) |
| 10 | ++ /* |
| 11 | ++ * Link used exclusively by the Windows thread pool to chain the jobs that |
| 12 | ++ * are waiting to be picked up. It is deliberately kept separate from |
| 13 | ++ * "next" above, which is owned (and rebuilt on every call) by whoever |
| 14 | ++ * submitted the job: appending to the pool must never write into the |
| 15 | ++ * queue nodes of another caller. |
| 16 | ++ */ |
| 17 | ++ struct blas_queue *pool_next; |
| 18 | + CRITICAL_SECTION lock; |
| 19 | + HANDLE finish; |
| 20 | + volatile int finished; |
| 21 | +diff --git a/driver/others/blas_server_win32.c b/driver/others/blas_server_win32.c |
| 22 | +--- a/driver/others/blas_server_win32.c |
| 23 | ++++ b/driver/others/blas_server_win32.c |
| 24 | +@@ -57,6 +57,7 @@ typedef struct{ |
| 25 | + HANDLE killed; |
| 26 | + |
| 27 | + blas_queue_t *queue; /* Parameter Pointer */ |
| 28 | ++ blas_queue_t *tail; /* Last job waiting in "queue", for O(1) append */ |
| 29 | + int shutdown; /* server shutdown flag */ |
| 30 | + |
| 31 | + } blas_pool_t; |
| 32 | +@@ -239,7 +240,10 @@ static DWORD WINAPI blas_thread_server(void *arg){ |
| 33 | + EnterCriticalSection(&pool.lock); |
| 34 | + |
| 35 | + queue = pool.queue; |
| 36 | +- if (queue) pool.queue = queue->next; |
| 37 | ++ if (queue) { |
| 38 | ++ pool.queue = queue->pool_next; |
| 39 | ++ if (pool.queue == NULL) pool.tail = NULL; |
| 40 | ++ } |
| 41 | + |
| 42 | + LeaveCriticalSection(&pool.lock); |
| 43 | + |
| 44 | +@@ -374,6 +378,7 @@ int blas_thread_init(void){ |
| 45 | + |
| 46 | + pool.shutdown = 0; |
| 47 | + pool.queue = NULL; |
| 48 | ++ pool.tail = NULL; |
| 49 | + |
| 50 | + for(i = 0; i < blas_cpu_number - 1; i++){ |
| 51 | + blas_threads[i] = CreateThread(NULL, 0, |
| 52 | +@@ -405,7 +410,7 @@ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){ |
| 53 | + if (unlikely(blas_server_avail == 0)) blas_thread_init(); |
| 54 | + #endif |
| 55 | + |
| 56 | +- blas_queue_t *current; |
| 57 | ++ blas_queue_t *current, *last = NULL; |
| 58 | + |
| 59 | + current = queue; |
| 60 | + |
| 61 | +@@ -419,19 +424,22 @@ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){ |
| 62 | + __asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode)); |
| 63 | + #endif |
| 64 | + |
| 65 | ++ /* Build the pool's own chain over this caller's jobs */ |
| 66 | ++ current -> pool_next = current -> next; |
| 67 | ++ last = current; |
| 68 | ++ |
| 69 | + current = current -> next; |
| 70 | + pos ++; |
| 71 | + } |
| 72 | + |
| 73 | + EnterCriticalSection(&pool.lock); |
| 74 | + |
| 75 | +- if (pool.queue) { |
| 76 | +- current = pool.queue; |
| 77 | +- while (current -> next) current = current -> next; |
| 78 | +- current -> next = queue; |
| 79 | ++ if (pool.tail) { |
| 80 | ++ pool.tail -> pool_next = queue; |
| 81 | + } else { |
| 82 | + pool.queue = queue; |
| 83 | + } |
| 84 | ++ pool.tail = last; |
| 85 | + |
| 86 | + LeaveCriticalSection(&pool.lock); |
| 87 | + |
| 88 | +@@ -568,6 +576,7 @@ void goto_set_num_threads(int num_threads) |
| 89 | + |
| 90 | + pool.shutdown = 0; |
| 91 | + pool.queue = NULL; |
| 92 | ++ pool.tail = NULL; |
| 93 | + blas_server_avail = 1; |
| 94 | + } |
| 95 | + |
0 commit comments