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

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