You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

blas_server_win32.c 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "common.h"
  41. #if !defined(unlikely)
  42. #ifdef __GNUC__
  43. #define unlikely(x) __builtin_expect(!!(x), 0)
  44. #else
  45. #define unlikely(x) (x)
  46. #endif
  47. #endif
  48. /* This is a thread implementation for Win32 lazy implementation */
  49. /* Thread server common information */
  50. static blas_queue_t *work_queue = NULL;
  51. static HANDLE kickoff_event = NULL;
  52. static CRITICAL_SECTION queue_lock;
  53. /* We need this global for checking if initialization is finished. */
  54. int blas_server_avail = 0;
  55. int blas_omp_threads_local = 1;
  56. /* Local Variables */
  57. static BLASULONG server_lock = 0;
  58. static HANDLE blas_threads [MAX_CPU_NUMBER];
  59. static DWORD blas_threads_id[MAX_CPU_NUMBER];
  60. static volatile int thread_target; // target num of live threads, volatile for cross-thread reads
  61. #if defined (__GNUC__) && (__GNUC__ < 6)
  62. #define WIN_CAS(dest, exch, comp) __sync_val_compare_and_swap(dest, comp, exch)
  63. #else
  64. #if defined(_WIN64)
  65. #define WIN_CAS(dest, exch, comp) InterlockedCompareExchange64(dest, exch, comp)
  66. #else
  67. #define WIN_CAS(dest, exch, comp) InterlockedCompareExchange(dest, exch, comp)
  68. #endif
  69. #endif
  70. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  71. if (!(mode & BLAS_COMPLEX)){
  72. #ifdef EXPRECISION
  73. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  74. /* REAL / Extended Double */
  75. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  76. xdouble *, BLASLONG, xdouble *, BLASLONG,
  77. xdouble *, BLASLONG, void *) = func;
  78. afunc(args -> m, args -> n, args -> k,
  79. ((xdouble *)args -> alpha)[0],
  80. args -> a, args -> lda,
  81. args -> b, args -> ldb,
  82. args -> c, args -> ldc, sb);
  83. } else
  84. #endif
  85. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  86. /* REAL / Double */
  87. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  88. double *, BLASLONG, double *, BLASLONG,
  89. double *, BLASLONG, void *) = func;
  90. afunc(args -> m, args -> n, args -> k,
  91. ((double *)args -> alpha)[0],
  92. args -> a, args -> lda,
  93. args -> b, args -> ldb,
  94. args -> c, args -> ldc, sb);
  95. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  96. /* REAL / Single */
  97. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  98. float *, BLASLONG, float *, BLASLONG,
  99. float *, BLASLONG, void *) = func;
  100. afunc(args -> m, args -> n, args -> k,
  101. ((float *)args -> alpha)[0],
  102. args -> a, args -> lda,
  103. args -> b, args -> ldb,
  104. args -> c, args -> ldc, sb);
  105. #ifdef BUILD_BFLOAT16
  106. } else if ((mode & BLAS_PREC) == BLAS_BFLOAT16){
  107. /* REAL / BFLOAT16 */
  108. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, bfloat16,
  109. bfloat16 *, BLASLONG, bfloat16 *, BLASLONG,
  110. bfloat16 *, BLASLONG, void *) = func;
  111. afunc(args -> m, args -> n, args -> k,
  112. ((bfloat16 *)args -> alpha)[0],
  113. args -> a, args -> lda,
  114. args -> b, args -> ldb,
  115. args -> c, args -> ldc, sb);
  116. } else if ((mode & BLAS_PREC) == BLAS_STOBF16){
  117. /* REAL / BLAS_STOBF16 */
  118. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  119. float *, BLASLONG, bfloat16 *, BLASLONG,
  120. float *, BLASLONG, void *) = func;
  121. afunc(args -> m, args -> n, args -> k,
  122. ((float *)args -> alpha)[0],
  123. args -> a, args -> lda,
  124. args -> b, args -> ldb,
  125. args -> c, args -> ldc, sb);
  126. } else if ((mode & BLAS_PREC) == BLAS_DTOBF16){
  127. /* REAL / BLAS_DTOBF16 */
  128. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  129. double *, BLASLONG, bfloat16 *, BLASLONG,
  130. double *, BLASLONG, void *) = func;
  131. afunc(args -> m, args -> n, args -> k,
  132. ((double *)args -> alpha)[0],
  133. args -> a, args -> lda,
  134. args -> b, args -> ldb,
  135. args -> c, args -> ldc, sb);
  136. #endif
  137. } else {
  138. /* REAL / Other types in future */
  139. }
  140. } else {
  141. #ifdef EXPRECISION
  142. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  143. /* COMPLEX / Extended Double */
  144. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  145. xdouble *, BLASLONG, xdouble *, BLASLONG,
  146. xdouble *, BLASLONG, void *) = func;
  147. afunc(args -> m, args -> n, args -> k,
  148. ((xdouble *)args -> alpha)[0],
  149. ((xdouble *)args -> alpha)[1],
  150. args -> a, args -> lda,
  151. args -> b, args -> ldb,
  152. args -> c, args -> ldc, sb);
  153. } else
  154. #endif
  155. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  156. /* COMPLEX / Double */
  157. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  158. double *, BLASLONG, double *, BLASLONG,
  159. double *, BLASLONG, void *) = func;
  160. afunc(args -> m, args -> n, args -> k,
  161. ((double *)args -> alpha)[0],
  162. ((double *)args -> alpha)[1],
  163. args -> a, args -> lda,
  164. args -> b, args -> ldb,
  165. args -> c, args -> ldc, sb);
  166. } else if ((mode & BLAS_PREC) == BLAS_SINGLE) {
  167. /* COMPLEX / Single */
  168. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  169. float *, BLASLONG, float *, BLASLONG,
  170. float *, BLASLONG, void *) = func;
  171. afunc(args -> m, args -> n, args -> k,
  172. ((float *)args -> alpha)[0],
  173. ((float *)args -> alpha)[1],
  174. args -> a, args -> lda,
  175. args -> b, args -> ldb,
  176. args -> c, args -> ldc, sb);
  177. } else {
  178. /* COMPLEX / Other types in future */
  179. }
  180. }
  181. }
  182. /* This is a main routine of threads. Each thread waits until job is */
  183. /* queued. */
  184. static DWORD WINAPI blas_thread_server(void *arg){
  185. /* Thread identifier */
  186. BLASLONG cpu = (BLASLONG)arg;
  187. void *buffer, *sa, *sb;
  188. blas_queue_t *queue;
  189. /* Each server needs each buffer */
  190. buffer = blas_memory_alloc(2);
  191. #ifdef SMP_DEBUG
  192. fprintf(STDERR, "Server[%2ld] Thread is started!\n", cpu);
  193. #endif
  194. while (1){
  195. /* Waiting for Queue */
  196. #ifdef SMP_DEBUG
  197. fprintf(STDERR, "Server[%2ld] Waiting for Queue.\n", cpu);
  198. #endif
  199. // event raised when work is added to the queue
  200. WaitForSingleObject(kickoff_event, INFINITE);
  201. if (cpu > thread_target - 2)
  202. {
  203. //printf("thread [%d] exiting.\n", cpu);
  204. break; // excess thread, so worker thread exits
  205. }
  206. #ifdef SMP_DEBUG
  207. fprintf(STDERR, "Server[%2ld] Got it.\n", cpu);
  208. #endif
  209. #if 1
  210. EnterCriticalSection(&queue_lock);
  211. queue = work_queue;
  212. if (queue)
  213. work_queue = work_queue->next;
  214. LeaveCriticalSection(&queue_lock);
  215. #else
  216. volatile blas_queue_t* queue_next;
  217. INT_PTR prev_value;
  218. do {
  219. queue = (volatile blas_queue_t*)work_queue;
  220. if (!queue)
  221. break;
  222. queue_next = (volatile blas_queue_t*)queue->next;
  223. prev_value = WIN_CAS((INT_PTR*)&work_queue, (INT_PTR)queue_next, (INT_PTR)queue);
  224. } while (prev_value != queue);
  225. #endif
  226. if (queue) {
  227. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  228. sa = queue -> sa;
  229. sb = queue -> sb;
  230. #ifdef CONSISTENT_FPCSR
  231. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  232. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  233. #endif
  234. #ifdef SMP_DEBUG
  235. fprintf(STDERR, "Server[%2ld] Started. Mode = 0x%03x M = %3ld N=%3ld K=%3ld\n",
  236. cpu, queue->mode, queue-> args ->m, queue->args->n, queue->args->k);
  237. #endif
  238. // fprintf(stderr, "queue start[%ld]!!!\n", cpu);
  239. #ifdef MONITOR
  240. main_status[cpu] = MAIN_RUNNING1;
  241. #endif
  242. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  243. if (sb == NULL) {
  244. if (!(queue -> mode & BLAS_COMPLEX)){
  245. #ifdef EXPRECISION
  246. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  247. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * sizeof(xdouble)
  248. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  249. } else
  250. #endif
  251. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  252. #ifdef BUILD_DOUBLE
  253. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  254. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  255. #endif
  256. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  257. #ifdef BUILD_SINGLE
  258. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  259. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  260. #endif
  261. } else {
  262. /* Other types in future */
  263. }
  264. } else {
  265. #ifdef EXPRECISION
  266. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  267. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  268. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  269. } else
  270. #endif
  271. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  272. #ifdef BUILD_COMPLEX16
  273. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  274. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  275. #endif
  276. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  277. #ifdef BUILD_COMPLEX
  278. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  279. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  280. #endif
  281. } else {
  282. /* Other types in future */
  283. }
  284. }
  285. queue->sb=sb;
  286. }
  287. #ifdef MONITOR
  288. main_status[cpu] = MAIN_RUNNING2;
  289. #endif
  290. if (!(queue -> mode & BLAS_LEGACY)) {
  291. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  292. } else {
  293. legacy_exec(routine, queue -> mode, queue -> args, sb);
  294. }
  295. }else{
  296. continue; //if queue == NULL
  297. }
  298. #ifdef SMP_DEBUG
  299. fprintf(STDERR, "Server[%2ld] Finished!\n", cpu);
  300. #endif
  301. queue->finished = 1;
  302. }
  303. /* Shutdown procedure */
  304. #ifdef SMP_DEBUG
  305. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  306. #endif
  307. blas_memory_free(buffer);
  308. return 0;
  309. }
  310. /* Initializing routine */
  311. int blas_thread_init(void){
  312. BLASLONG i;
  313. if (blas_server_avail || (blas_cpu_number <= 1)) return 0;
  314. LOCK_COMMAND(&server_lock);
  315. #ifdef SMP_DEBUG
  316. fprintf(STDERR, "Initializing Thread(Num. threads = %d)\n",
  317. blas_cpu_number);
  318. #endif
  319. if (!blas_server_avail){
  320. // create the kickoff Event
  321. kickoff_event = CreateEvent(NULL, TRUE, FALSE, NULL);
  322. thread_target = blas_cpu_number;
  323. InitializeCriticalSection(&queue_lock);
  324. for(i = 0; i < blas_cpu_number - 1; i++){
  325. //printf("thread_init: creating thread [%d]\n", i);
  326. blas_threads[i] = CreateThread(NULL, 0,
  327. blas_thread_server, (void *)i,
  328. 0, &blas_threads_id[i]);
  329. }
  330. blas_server_avail = 1;
  331. }
  332. UNLOCK_COMMAND(&server_lock);
  333. return 0;
  334. }
  335. /*
  336. User can call one of two routines.
  337. exec_blas_async ... immediately returns after jobs are queued.
  338. exec_blas ... returns after jobs are finished.
  339. */
  340. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  341. #if defined(SMP_SERVER)
  342. // Handle lazy re-init of the thread-pool after a POSIX fork
  343. // on Cygwin or as delayed init when a static library is used
  344. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  345. #endif
  346. blas_queue_t *current;
  347. current = queue;
  348. while (current) {
  349. current -> position = pos;
  350. #ifdef CONSISTENT_FPCSR
  351. __asm__ __volatile__ ("fnstcw %0" : "=m" (current -> x87_mode));
  352. __asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode));
  353. #endif
  354. current->finished = 0;
  355. current = current -> next;
  356. pos ++;
  357. }
  358. EnterCriticalSection(&queue_lock);
  359. if (!work_queue)
  360. {
  361. work_queue = queue;
  362. }
  363. else
  364. {
  365. blas_queue_t *next_item = work_queue;
  366. // find the end of the work queue
  367. while (next_item)
  368. next_item = next_item->next;
  369. // add new work to the end
  370. next_item = queue;
  371. }
  372. LeaveCriticalSection(&queue_lock);
  373. SetEvent(kickoff_event);
  374. return 0;
  375. }
  376. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  377. #ifdef SMP_DEBUG
  378. fprintf(STDERR, "Synchronization Waiting.\n");
  379. #endif
  380. while (num){
  381. #ifdef SMP_DEBUG
  382. fprintf(STDERR, "Waiting Queue ..\n");
  383. #endif
  384. while (!queue->finished)
  385. YIELDING;
  386. queue = queue->next;
  387. num--;
  388. }
  389. #ifdef SMP_DEBUG
  390. fprintf(STDERR, "Completely Done.\n\n");
  391. #endif
  392. // if work was added to the queue after this batch we can't sleep the worker threads
  393. // by resetting the event
  394. EnterCriticalSection(&queue_lock);
  395. if (work_queue == NULL)
  396. ResetEvent(kickoff_event);
  397. LeaveCriticalSection(&queue_lock);
  398. return 0;
  399. }
  400. /* Execute Threads */
  401. int exec_blas(BLASLONG num, blas_queue_t *queue){
  402. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  403. // Handle lazy re-init of the thread-pool after a POSIX fork
  404. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  405. #endif
  406. #ifndef ALL_THREADED
  407. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  408. #endif
  409. if ((num <= 0) || (queue == NULL)) return 0;
  410. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  411. routine = queue -> routine;
  412. if (queue -> mode & BLAS_LEGACY) {
  413. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  414. } else
  415. if (queue -> mode & BLAS_PTHREAD) {
  416. void (*pthreadcompat)(void *) = queue -> routine;
  417. (pthreadcompat)(queue -> args);
  418. } else
  419. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  420. queue -> sa, queue -> sb, 0);
  421. if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
  422. return 0;
  423. }
  424. /* Shutdown procedure, but user don't have to call this routine. The */
  425. /* kernel automatically kill threads. */
  426. int BLASFUNC(blas_thread_shutdown)(void){
  427. int i;
  428. if (!blas_server_avail) return 0;
  429. LOCK_COMMAND(&server_lock);
  430. if (blas_server_avail){
  431. for(i = 0; i < blas_num_threads - 1; i++){
  432. // Could also just use WaitForMultipleObjects
  433. DWORD wait_thread_value = WaitForSingleObject(blas_threads[i], 50);
  434. #ifndef OS_WINDOWSSTORE
  435. // TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
  436. if (WAIT_OBJECT_0 != wait_thread_value) {
  437. TerminateThread(blas_threads[i],0);
  438. }
  439. #endif
  440. CloseHandle(blas_threads[i]);
  441. }
  442. blas_server_avail = 0;
  443. }
  444. UNLOCK_COMMAND(&server_lock);
  445. return 0;
  446. }
  447. void goto_set_num_threads(int num_threads)
  448. {
  449. long i;
  450. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  451. // Handle lazy re-init of the thread-pool after a POSIX fork
  452. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  453. #endif
  454. if (num_threads < 1) num_threads = blas_cpu_number;
  455. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  456. if (blas_server_avail && num_threads < blas_num_threads) {
  457. LOCK_COMMAND(&server_lock);
  458. thread_target = num_threads;
  459. SetEvent(kickoff_event);
  460. for (i = num_threads - 1; i < blas_num_threads - 1; i++) {
  461. //printf("set_num_threads: waiting on thread [%d] to quit.\n", i);
  462. WaitForSingleObject(blas_threads[i], INFINITE);
  463. //printf("set_num_threads: thread [%d] has quit.\n", i);
  464. CloseHandle(blas_threads[i]);
  465. }
  466. blas_num_threads = num_threads;
  467. ResetEvent(kickoff_event);
  468. UNLOCK_COMMAND(&server_lock);
  469. }
  470. if (num_threads > blas_num_threads) {
  471. LOCK_COMMAND(&server_lock);
  472. thread_target = num_threads;
  473. //increased_threads = 1;
  474. if (!blas_server_avail){
  475. // create the kickoff Event
  476. kickoff_event = CreateEvent(NULL, TRUE, FALSE, NULL);
  477. InitializeCriticalSection(&queue_lock);
  478. blas_server_avail = 1;
  479. }
  480. for(i = (blas_num_threads > 0) ? blas_num_threads - 1 : 0; i < num_threads - 1; i++){
  481. //printf("set_num_threads: creating thread [%d]\n", i);
  482. blas_threads[i] = CreateThread(NULL, 0,
  483. blas_thread_server, (void *)i,
  484. 0, &blas_threads_id[i]);
  485. }
  486. blas_num_threads = num_threads;
  487. UNLOCK_COMMAND(&server_lock);
  488. }
  489. blas_cpu_number = num_threads;
  490. }
  491. void openblas_set_num_threads(int num)
  492. {
  493. goto_set_num_threads(num);
  494. }