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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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(OS_CYGWIN_NT) && !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. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  236. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  237. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  238. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  239. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  240. } else {
  241. /* Other types in future */
  242. }
  243. } else {
  244. #ifdef EXPRECISION
  245. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  246. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  247. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  248. } else
  249. #endif
  250. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  251. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  252. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  253. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  254. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  255. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  256. } else {
  257. /* Other types in future */
  258. }
  259. }
  260. queue->sb=sb;
  261. }
  262. #ifdef MONITOR
  263. main_status[cpu] = MAIN_RUNNING2;
  264. #endif
  265. if (!(queue -> mode & BLAS_LEGACY)) {
  266. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  267. } else {
  268. legacy_exec(routine, queue -> mode, queue -> args, sb);
  269. }
  270. }else{
  271. continue; //if queue == NULL
  272. }
  273. #ifdef SMP_DEBUG
  274. fprintf(STDERR, "Server[%2ld] Finished!\n", cpu);
  275. #endif
  276. EnterCriticalSection(&queue->lock);
  277. queue -> status = BLAS_STATUS_FINISHED;
  278. LeaveCriticalSection(&queue->lock);
  279. SetEvent(queue->finish);
  280. }
  281. /* Shutdown procedure */
  282. #ifdef SMP_DEBUG
  283. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  284. #endif
  285. blas_memory_free(buffer);
  286. return 0;
  287. }
  288. /* Initializing routine */
  289. int blas_thread_init(void){
  290. BLASLONG i;
  291. if (blas_server_avail || (blas_cpu_number <= 1)) return 0;
  292. LOCK_COMMAND(&server_lock);
  293. #ifdef SMP_DEBUG
  294. fprintf(STDERR, "Initializing Thread(Num. threads = %d)\n",
  295. blas_cpu_number);
  296. #endif
  297. if (!blas_server_avail){
  298. InitializeCriticalSection(&pool.lock);
  299. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  300. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  301. pool.shutdown = 0;
  302. pool.queue = NULL;
  303. for(i = 0; i < blas_cpu_number - 1; i++){
  304. blas_threads[i] = CreateThread(NULL, 0,
  305. blas_thread_server, (void *)i,
  306. 0, &blas_threads_id[i]);
  307. }
  308. blas_server_avail = 1;
  309. }
  310. UNLOCK_COMMAND(&server_lock);
  311. return 0;
  312. }
  313. /*
  314. User can call one of two routines.
  315. exec_blas_async ... immediately returns after jobs are queued.
  316. exec_blas ... returns after jobs are finished.
  317. */
  318. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  319. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  320. // Handle lazy re-init of the thread-pool after a POSIX fork
  321. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  322. #endif
  323. blas_queue_t *current;
  324. current = queue;
  325. while (current) {
  326. InitializeCriticalSection(&current -> lock);
  327. current -> finish = CreateEvent(NULL, FALSE, FALSE, NULL);
  328. current -> position = pos;
  329. #ifdef CONSISTENT_FPCSR
  330. __asm__ __volatile__ ("fnstcw %0" : "=m" (current -> x87_mode));
  331. __asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode));
  332. #endif
  333. current = current -> next;
  334. pos ++;
  335. }
  336. EnterCriticalSection(&pool.lock);
  337. if (pool.queue) {
  338. current = pool.queue;
  339. while (current -> next) current = current -> next;
  340. current -> next = queue;
  341. } else {
  342. pool.queue = queue;
  343. }
  344. LeaveCriticalSection(&pool.lock);
  345. SetEvent(pool.filled);
  346. return 0;
  347. }
  348. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  349. #ifdef SMP_DEBUG
  350. fprintf(STDERR, "Synchronization Waiting.\n");
  351. #endif
  352. while (num){
  353. #ifdef SMP_DEBUG
  354. fprintf(STDERR, "Waiting Queue ..\n");
  355. #endif
  356. WaitForSingleObject(queue->finish, INFINITE);
  357. CloseHandle(queue->finish);
  358. DeleteCriticalSection(&queue -> lock);
  359. queue = queue -> next;
  360. num --;
  361. }
  362. #ifdef SMP_DEBUG
  363. fprintf(STDERR, "Completely Done.\n\n");
  364. #endif
  365. return 0;
  366. }
  367. /* Execute Threads */
  368. int exec_blas(BLASLONG num, blas_queue_t *queue){
  369. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  370. // Handle lazy re-init of the thread-pool after a POSIX fork
  371. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  372. #endif
  373. #ifndef ALL_THREADED
  374. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  375. #endif
  376. if ((num <= 0) || (queue == NULL)) return 0;
  377. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  378. routine = queue -> routine;
  379. if (queue -> mode & BLAS_LEGACY) {
  380. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  381. } else
  382. if (queue -> mode & BLAS_PTHREAD) {
  383. void (*pthreadcompat)(void *) = queue -> routine;
  384. (pthreadcompat)(queue -> args);
  385. } else
  386. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  387. queue -> sa, queue -> sb, 0);
  388. if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
  389. return 0;
  390. }
  391. /* Shutdown procedure, but user don't have to call this routine. The */
  392. /* kernel automatically kill threads. */
  393. int BLASFUNC(blas_thread_shutdown)(void){
  394. int i;
  395. if (!blas_server_avail) return 0;
  396. LOCK_COMMAND(&server_lock);
  397. if (blas_server_avail){
  398. SetEvent(pool.killed);
  399. for(i = 0; i < blas_num_threads - 1; i++){
  400. // Could also just use WaitForMultipleObjects
  401. DWORD wait_thread_value = WaitForSingleObject(blas_threads[i], 50);
  402. #ifndef OS_WINDOWSSTORE
  403. // TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
  404. if (WAIT_OBJECT_0 != wait_thread_value) {
  405. TerminateThread(blas_threads[i],0);
  406. }
  407. #endif
  408. CloseHandle(blas_threads[i]);
  409. }
  410. CloseHandle(pool.filled);
  411. CloseHandle(pool.killed);
  412. blas_server_avail = 0;
  413. }
  414. UNLOCK_COMMAND(&server_lock);
  415. return 0;
  416. }
  417. void goto_set_num_threads(int num_threads)
  418. {
  419. long i;
  420. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  421. // Handle lazy re-init of the thread-pool after a POSIX fork
  422. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  423. #endif
  424. if (num_threads < 1) num_threads = blas_cpu_number;
  425. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  426. if (num_threads > blas_num_threads) {
  427. LOCK_COMMAND(&server_lock);
  428. //increased_threads = 1;
  429. if (!blas_server_avail){
  430. InitializeCriticalSection(&pool.lock);
  431. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  432. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  433. pool.shutdown = 0;
  434. pool.queue = NULL;
  435. blas_server_avail = 1;
  436. }
  437. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  438. blas_threads[i] = CreateThread(NULL, 0,
  439. blas_thread_server, (void *)i,
  440. 0, &blas_threads_id[i]);
  441. }
  442. blas_num_threads = num_threads;
  443. UNLOCK_COMMAND(&server_lock);
  444. }
  445. blas_cpu_number = num_threads;
  446. }
  447. void openblas_set_num_threads(int num)
  448. {
  449. goto_set_num_threads(num);
  450. }