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 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. typedef struct{
  51. CRITICAL_SECTION lock;
  52. HANDLE filled;
  53. HANDLE killed;
  54. blas_queue_t *queue; /* Parameter Pointer */
  55. int shutdown; /* server shutdown flag */
  56. } blas_pool_t;
  57. /* We need this global for checking if initialization is finished. */
  58. int blas_server_avail = 0;
  59. /* Local Variables */
  60. static BLASULONG server_lock = 0;
  61. static blas_pool_t pool;
  62. static HANDLE blas_threads [MAX_CPU_NUMBER];
  63. static DWORD blas_threads_id[MAX_CPU_NUMBER];
  64. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  65. if (!(mode & BLAS_COMPLEX)){
  66. #ifdef EXPRECISION
  67. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  68. /* REAL / Extended Double */
  69. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  70. xdouble *, BLASLONG, xdouble *, BLASLONG,
  71. xdouble *, BLASLONG, void *) = func;
  72. afunc(args -> m, args -> n, args -> k,
  73. ((xdouble *)args -> alpha)[0],
  74. args -> a, args -> lda,
  75. args -> b, args -> ldb,
  76. args -> c, args -> ldc, sb);
  77. } else
  78. #endif
  79. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  80. /* REAL / Double */
  81. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  82. double *, BLASLONG, double *, BLASLONG,
  83. double *, BLASLONG, void *) = func;
  84. afunc(args -> m, args -> n, args -> k,
  85. ((double *)args -> alpha)[0],
  86. args -> a, args -> lda,
  87. args -> b, args -> ldb,
  88. args -> c, args -> ldc, sb);
  89. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  90. /* REAL / Single */
  91. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  92. float *, BLASLONG, float *, BLASLONG,
  93. float *, BLASLONG, void *) = func;
  94. afunc(args -> m, args -> n, args -> k,
  95. ((float *)args -> alpha)[0],
  96. args -> a, args -> lda,
  97. args -> b, args -> ldb,
  98. args -> c, args -> ldc, sb);
  99. #ifdef BUILD_BFLOAT16
  100. } else if ((mode & BLAS_PREC) == BLAS_BFLOAT16){
  101. /* REAL / BFLOAT16 */
  102. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, bfloat16,
  103. bfloat16 *, BLASLONG, bfloat16 *, BLASLONG,
  104. bfloat16 *, BLASLONG, void *) = func;
  105. afunc(args -> m, args -> n, args -> k,
  106. ((bfloat16 *)args -> alpha)[0],
  107. args -> a, args -> lda,
  108. args -> b, args -> ldb,
  109. args -> c, args -> ldc, sb);
  110. } else if ((mode & BLAS_PREC) == BLAS_STOBF16){
  111. /* REAL / BLAS_STOBF16 */
  112. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  113. float *, BLASLONG, bfloat16 *, BLASLONG,
  114. float *, BLASLONG, void *) = func;
  115. afunc(args -> m, args -> n, args -> k,
  116. ((float *)args -> alpha)[0],
  117. args -> a, args -> lda,
  118. args -> b, args -> ldb,
  119. args -> c, args -> ldc, sb);
  120. } else if ((mode & BLAS_PREC) == BLAS_DTOBF16){
  121. /* REAL / BLAS_DTOBF16 */
  122. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  123. double *, BLASLONG, bfloat16 *, BLASLONG,
  124. double *, BLASLONG, void *) = func;
  125. afunc(args -> m, args -> n, args -> k,
  126. ((double *)args -> alpha)[0],
  127. args -> a, args -> lda,
  128. args -> b, args -> ldb,
  129. args -> c, args -> ldc, sb);
  130. #endif
  131. } else {
  132. /* REAL / Other types in future */
  133. }
  134. } else {
  135. #ifdef EXPRECISION
  136. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  137. /* COMPLEX / Extended Double */
  138. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  139. xdouble *, BLASLONG, xdouble *, BLASLONG,
  140. xdouble *, BLASLONG, void *) = func;
  141. afunc(args -> m, args -> n, args -> k,
  142. ((xdouble *)args -> alpha)[0],
  143. ((xdouble *)args -> alpha)[1],
  144. args -> a, args -> lda,
  145. args -> b, args -> ldb,
  146. args -> c, args -> ldc, sb);
  147. } else
  148. #endif
  149. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  150. /* COMPLEX / Double */
  151. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  152. double *, BLASLONG, double *, BLASLONG,
  153. double *, BLASLONG, void *) = func;
  154. afunc(args -> m, args -> n, args -> k,
  155. ((double *)args -> alpha)[0],
  156. ((double *)args -> alpha)[1],
  157. args -> a, args -> lda,
  158. args -> b, args -> ldb,
  159. args -> c, args -> ldc, sb);
  160. } else if ((mode & BLAS_PREC) == BLAS_SINGLE) {
  161. /* COMPLEX / Single */
  162. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  163. float *, BLASLONG, float *, BLASLONG,
  164. float *, BLASLONG, void *) = func;
  165. afunc(args -> m, args -> n, args -> k,
  166. ((float *)args -> alpha)[0],
  167. ((float *)args -> alpha)[1],
  168. args -> a, args -> lda,
  169. args -> b, args -> ldb,
  170. args -> c, args -> ldc, sb);
  171. } else {
  172. /* COMPLEX / Other types in future */
  173. }
  174. }
  175. }
  176. /* This is a main routine of threads. Each thread waits until job is */
  177. /* queued. */
  178. static DWORD WINAPI blas_thread_server(void *arg){
  179. /* Thread identifier */
  180. #ifdef SMP_DEBUG
  181. BLASLONG cpu = (BLASLONG)arg;
  182. #endif
  183. void *buffer, *sa, *sb;
  184. blas_queue_t *queue;
  185. DWORD action;
  186. HANDLE handles[] = {pool.filled, pool.killed};
  187. /* Each server needs each buffer */
  188. buffer = blas_memory_alloc(2);
  189. #ifdef SMP_DEBUG
  190. fprintf(STDERR, "Server[%2ld] Thread is started!\n", cpu);
  191. #endif
  192. while (1){
  193. /* Waiting for Queue */
  194. #ifdef SMP_DEBUG
  195. fprintf(STDERR, "Server[%2ld] Waiting for Queue.\n", cpu);
  196. #endif
  197. do {
  198. action = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
  199. } while ((action != WAIT_OBJECT_0) && (action != WAIT_OBJECT_0 + 1));
  200. if (action == WAIT_OBJECT_0 + 1) break;
  201. #ifdef SMP_DEBUG
  202. fprintf(STDERR, "Server[%2ld] Got it.\n", cpu);
  203. #endif
  204. EnterCriticalSection(&pool.lock);
  205. queue = pool.queue;
  206. if (queue) pool.queue = queue->next;
  207. LeaveCriticalSection(&pool.lock);
  208. if (queue) {
  209. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  210. if (pool.queue) SetEvent(pool.filled);
  211. sa = queue -> sa;
  212. sb = queue -> sb;
  213. #ifdef CONSISTENT_FPCSR
  214. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  215. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  216. #endif
  217. #ifdef SMP_DEBUG
  218. fprintf(STDERR, "Server[%2ld] Started. Mode = 0x%03x M = %3ld N=%3ld K=%3ld\n",
  219. cpu, queue->mode, queue-> args ->m, queue->args->n, queue->args->k);
  220. #endif
  221. // fprintf(stderr, "queue start[%ld]!!!\n", cpu);
  222. #ifdef MONITOR
  223. main_status[cpu] = MAIN_RUNNING1;
  224. #endif
  225. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  226. if (sb == NULL) {
  227. if (!(queue -> mode & BLAS_COMPLEX)){
  228. #ifdef EXPRECISION
  229. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  230. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * sizeof(xdouble)
  231. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  232. } else
  233. #endif
  234. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  235. #ifdef BUILD_DOUBLE
  236. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  237. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  238. #endif
  239. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  240. #ifdef BUILD_SINGLE
  241. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  242. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  243. #endif
  244. } else {
  245. /* Other types in future */
  246. }
  247. } else {
  248. #ifdef EXPRECISION
  249. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  250. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  251. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  252. } else
  253. #endif
  254. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  255. #ifdef BUILD_COMPLEX16
  256. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  257. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  258. #endif
  259. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  260. #ifdef BUILD_COMPLEX
  261. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  262. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  263. #endif
  264. } else {
  265. /* Other types in future */
  266. }
  267. }
  268. queue->sb=sb;
  269. }
  270. #ifdef MONITOR
  271. main_status[cpu] = MAIN_RUNNING2;
  272. #endif
  273. if (!(queue -> mode & BLAS_LEGACY)) {
  274. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  275. } else {
  276. legacy_exec(routine, queue -> mode, queue -> args, sb);
  277. }
  278. }else{
  279. continue; //if queue == NULL
  280. }
  281. #ifdef SMP_DEBUG
  282. fprintf(STDERR, "Server[%2ld] Finished!\n", cpu);
  283. #endif
  284. EnterCriticalSection(&queue->lock);
  285. queue -> status = BLAS_STATUS_FINISHED;
  286. LeaveCriticalSection(&queue->lock);
  287. SetEvent(queue->finish);
  288. }
  289. /* Shutdown procedure */
  290. #ifdef SMP_DEBUG
  291. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  292. #endif
  293. blas_memory_free(buffer);
  294. return 0;
  295. }
  296. /* Initializing routine */
  297. int blas_thread_init(void){
  298. BLASLONG i;
  299. if (blas_server_avail || (blas_cpu_number <= 1)) return 0;
  300. LOCK_COMMAND(&server_lock);
  301. #ifdef SMP_DEBUG
  302. fprintf(STDERR, "Initializing Thread(Num. threads = %d)\n",
  303. blas_cpu_number);
  304. #endif
  305. if (!blas_server_avail){
  306. InitializeCriticalSection(&pool.lock);
  307. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  308. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  309. pool.shutdown = 0;
  310. pool.queue = NULL;
  311. for(i = 0; i < blas_cpu_number - 1; i++){
  312. blas_threads[i] = CreateThread(NULL, 0,
  313. blas_thread_server, (void *)i,
  314. 0, &blas_threads_id[i]);
  315. }
  316. blas_server_avail = 1;
  317. }
  318. UNLOCK_COMMAND(&server_lock);
  319. return 0;
  320. }
  321. /*
  322. User can call one of two routines.
  323. exec_blas_async ... immediately returns after jobs are queued.
  324. exec_blas ... returns after jobs are finished.
  325. */
  326. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  327. #if defined(SMP_SERVER)
  328. // Handle lazy re-init of the thread-pool after a POSIX fork
  329. // on Cygwin or as delayed init when a static library is used
  330. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  331. #endif
  332. blas_queue_t *current;
  333. current = queue;
  334. while (current) {
  335. InitializeCriticalSection(&current -> lock);
  336. current -> finish = CreateEvent(NULL, FALSE, FALSE, NULL);
  337. current -> position = pos;
  338. #ifdef CONSISTENT_FPCSR
  339. __asm__ __volatile__ ("fnstcw %0" : "=m" (current -> x87_mode));
  340. __asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode));
  341. #endif
  342. current = current -> next;
  343. pos ++;
  344. }
  345. EnterCriticalSection(&pool.lock);
  346. if (pool.queue) {
  347. current = pool.queue;
  348. while (current -> next) current = current -> next;
  349. current -> next = queue;
  350. } else {
  351. pool.queue = queue;
  352. }
  353. LeaveCriticalSection(&pool.lock);
  354. SetEvent(pool.filled);
  355. return 0;
  356. }
  357. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  358. #ifdef SMP_DEBUG
  359. fprintf(STDERR, "Synchronization Waiting.\n");
  360. #endif
  361. while (num){
  362. #ifdef SMP_DEBUG
  363. fprintf(STDERR, "Waiting Queue ..\n");
  364. #endif
  365. WaitForSingleObject(queue->finish, INFINITE);
  366. CloseHandle(queue->finish);
  367. DeleteCriticalSection(&queue -> lock);
  368. queue = queue -> next;
  369. num --;
  370. }
  371. #ifdef SMP_DEBUG
  372. fprintf(STDERR, "Completely Done.\n\n");
  373. #endif
  374. return 0;
  375. }
  376. /* Execute Threads */
  377. int exec_blas(BLASLONG num, blas_queue_t *queue){
  378. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  379. // Handle lazy re-init of the thread-pool after a POSIX fork
  380. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  381. #endif
  382. #ifndef ALL_THREADED
  383. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  384. #endif
  385. if ((num <= 0) || (queue == NULL)) return 0;
  386. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  387. routine = queue -> routine;
  388. if (queue -> mode & BLAS_LEGACY) {
  389. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  390. } else
  391. if (queue -> mode & BLAS_PTHREAD) {
  392. void (*pthreadcompat)(void *) = queue -> routine;
  393. (pthreadcompat)(queue -> args);
  394. } else
  395. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  396. queue -> sa, queue -> sb, 0);
  397. if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
  398. return 0;
  399. }
  400. /* Shutdown procedure, but user don't have to call this routine. The */
  401. /* kernel automatically kill threads. */
  402. int BLASFUNC(blas_thread_shutdown)(void){
  403. int i;
  404. if (!blas_server_avail) return 0;
  405. LOCK_COMMAND(&server_lock);
  406. if (blas_server_avail){
  407. SetEvent(pool.killed);
  408. for(i = 0; i < blas_num_threads - 1; i++){
  409. // Could also just use WaitForMultipleObjects
  410. DWORD wait_thread_value = WaitForSingleObject(blas_threads[i], 50);
  411. #ifndef OS_WINDOWSSTORE
  412. // TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
  413. if (WAIT_OBJECT_0 != wait_thread_value) {
  414. TerminateThread(blas_threads[i],0);
  415. }
  416. #endif
  417. CloseHandle(blas_threads[i]);
  418. }
  419. CloseHandle(pool.filled);
  420. CloseHandle(pool.killed);
  421. blas_server_avail = 0;
  422. }
  423. UNLOCK_COMMAND(&server_lock);
  424. return 0;
  425. }
  426. void goto_set_num_threads(int num_threads)
  427. {
  428. long i;
  429. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  430. // Handle lazy re-init of the thread-pool after a POSIX fork
  431. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  432. #endif
  433. if (num_threads < 1) num_threads = blas_cpu_number;
  434. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  435. if (num_threads > blas_num_threads) {
  436. LOCK_COMMAND(&server_lock);
  437. //increased_threads = 1;
  438. if (!blas_server_avail){
  439. InitializeCriticalSection(&pool.lock);
  440. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  441. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  442. pool.shutdown = 0;
  443. pool.queue = NULL;
  444. blas_server_avail = 1;
  445. }
  446. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  447. blas_threads[i] = CreateThread(NULL, 0,
  448. blas_thread_server, (void *)i,
  449. 0, &blas_threads_id[i]);
  450. }
  451. blas_num_threads = num_threads;
  452. UNLOCK_COMMAND(&server_lock);
  453. }
  454. blas_cpu_number = num_threads;
  455. }
  456. void openblas_set_num_threads(int num)
  457. {
  458. goto_set_num_threads(num);
  459. }