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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 infomation */
  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 cheking 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_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_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 {
  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. }
  100. } else {
  101. #ifdef EXPRECISION
  102. if (mode & BLAS_XDOUBLE){
  103. /* COMPLEX / Extended Double */
  104. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  105. xdouble *, BLASLONG, xdouble *, BLASLONG,
  106. xdouble *, BLASLONG, void *) = func;
  107. afunc(args -> m, args -> n, args -> k,
  108. ((xdouble *)args -> alpha)[0],
  109. ((xdouble *)args -> alpha)[1],
  110. args -> a, args -> lda,
  111. args -> b, args -> ldb,
  112. args -> c, args -> ldc, sb);
  113. } else
  114. #endif
  115. if (mode & BLAS_DOUBLE){
  116. /* COMPLEX / Double */
  117. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  118. double *, BLASLONG, double *, BLASLONG,
  119. double *, BLASLONG, void *) = func;
  120. afunc(args -> m, args -> n, args -> k,
  121. ((double *)args -> alpha)[0],
  122. ((double *)args -> alpha)[1],
  123. args -> a, args -> lda,
  124. args -> b, args -> ldb,
  125. args -> c, args -> ldc, sb);
  126. } else {
  127. /* COMPLEX / Single */
  128. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  129. float *, BLASLONG, float *, BLASLONG,
  130. float *, BLASLONG, void *) = func;
  131. afunc(args -> m, args -> n, args -> k,
  132. ((float *)args -> alpha)[0],
  133. ((float *)args -> alpha)[1],
  134. args -> a, args -> lda,
  135. args -> b, args -> ldb,
  136. args -> c, args -> ldc, sb);
  137. }
  138. }
  139. }
  140. /* This is a main routine of threads. Each thread waits until job is */
  141. /* queued. */
  142. static DWORD WINAPI blas_thread_server(void *arg){
  143. /* Thread identifier */
  144. #ifdef SMP_DEBUG
  145. BLASLONG cpu = (BLASLONG)arg;
  146. #endif
  147. void *buffer, *sa, *sb;
  148. blas_queue_t *queue;
  149. DWORD action;
  150. HANDLE handles[] = {pool.filled, pool.killed};
  151. /* Each server needs each buffer */
  152. buffer = blas_memory_alloc(2);
  153. #ifdef SMP_DEBUG
  154. fprintf(STDERR, "Server[%2ld] Thread is started!\n", cpu);
  155. #endif
  156. while (1){
  157. /* Waiting for Queue */
  158. #ifdef SMP_DEBUG
  159. fprintf(STDERR, "Server[%2ld] Waiting for Queue.\n", cpu);
  160. #endif
  161. do {
  162. action = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
  163. } while ((action != WAIT_OBJECT_0) && (action != WAIT_OBJECT_0 + 1));
  164. if (action == WAIT_OBJECT_0 + 1) break;
  165. #ifdef SMP_DEBUG
  166. fprintf(STDERR, "Server[%2ld] Got it.\n", cpu);
  167. #endif
  168. EnterCriticalSection(&pool.lock);
  169. queue = pool.queue;
  170. if (queue) pool.queue = queue->next;
  171. LeaveCriticalSection(&pool.lock);
  172. if (queue) {
  173. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  174. if (pool.queue) SetEvent(pool.filled);
  175. sa = queue -> sa;
  176. sb = queue -> sb;
  177. #ifdef CONSISTENT_FPCSR
  178. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  179. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  180. #endif
  181. #ifdef SMP_DEBUG
  182. fprintf(STDERR, "Server[%2ld] Started. Mode = 0x%03x M = %3ld N=%3ld K=%3ld\n",
  183. cpu, queue->mode, queue-> args ->m, queue->args->n, queue->args->k);
  184. #endif
  185. // fprintf(stderr, "queue start[%ld]!!!\n", cpu);
  186. #ifdef MONITOR
  187. main_status[cpu] = MAIN_RUNNING1;
  188. #endif
  189. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  190. if (sb == NULL) {
  191. if (!(queue -> mode & BLAS_COMPLEX)){
  192. #ifdef EXPRECISION
  193. if (queue -> mode & BLAS_XDOUBLE){
  194. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * sizeof(xdouble)
  195. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  196. } else
  197. #endif
  198. if (queue -> mode & BLAS_DOUBLE){
  199. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  200. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  201. } else {
  202. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  203. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  204. }
  205. } else {
  206. #ifdef EXPRECISION
  207. if (queue -> mode & BLAS_XDOUBLE){
  208. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  209. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  210. } else
  211. #endif
  212. if (queue -> mode & BLAS_DOUBLE){
  213. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  214. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  215. } else {
  216. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  217. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  218. }
  219. }
  220. queue->sb=sb;
  221. }
  222. #ifdef MONITOR
  223. main_status[cpu] = MAIN_RUNNING2;
  224. #endif
  225. if (!(queue -> mode & BLAS_LEGACY)) {
  226. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  227. } else {
  228. legacy_exec(routine, queue -> mode, queue -> args, sb);
  229. }
  230. }else{
  231. continue; //if queue == NULL
  232. }
  233. #ifdef SMP_DEBUG
  234. fprintf(STDERR, "Server[%2ld] Finished!\n", cpu);
  235. #endif
  236. EnterCriticalSection(&queue->lock);
  237. queue -> status = BLAS_STATUS_FINISHED;
  238. LeaveCriticalSection(&queue->lock);
  239. SetEvent(queue->finish);
  240. }
  241. /* Shutdown procedure */
  242. #ifdef SMP_DEBUG
  243. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  244. #endif
  245. blas_memory_free(buffer);
  246. return 0;
  247. }
  248. /* Initializing routine */
  249. int blas_thread_init(void){
  250. BLASLONG i;
  251. if (blas_server_avail || (blas_cpu_number <= 1)) return 0;
  252. LOCK_COMMAND(&server_lock);
  253. #ifdef SMP_DEBUG
  254. fprintf(STDERR, "Initializing Thread(Num. threads = %d)\n",
  255. blas_cpu_number);
  256. #endif
  257. if (!blas_server_avail){
  258. InitializeCriticalSection(&pool.lock);
  259. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  260. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  261. pool.shutdown = 0;
  262. pool.queue = NULL;
  263. for(i = 0; i < blas_cpu_number - 1; i++){
  264. blas_threads[i] = CreateThread(NULL, 0,
  265. blas_thread_server, (void *)i,
  266. 0, &blas_threads_id[i]);
  267. }
  268. blas_server_avail = 1;
  269. }
  270. UNLOCK_COMMAND(&server_lock);
  271. return 0;
  272. }
  273. /*
  274. User can call one of two routines.
  275. exec_blas_async ... immediately returns after jobs are queued.
  276. exec_blas ... returns after jobs are finished.
  277. */
  278. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  279. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  280. // Handle lazy re-init of the thread-pool after a POSIX fork
  281. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  282. #endif
  283. blas_queue_t *current;
  284. current = queue;
  285. while (current) {
  286. InitializeCriticalSection(&current -> lock);
  287. current -> finish = CreateEvent(NULL, FALSE, FALSE, NULL);
  288. current -> position = pos;
  289. #ifdef CONSISTENT_FPCSR
  290. __asm__ __volatile__ ("fnstcw %0" : "=m" (current -> x87_mode));
  291. __asm__ __volatile__ ("stmxcsr %0" : "=m" (current -> sse_mode));
  292. #endif
  293. current = current -> next;
  294. pos ++;
  295. }
  296. EnterCriticalSection(&pool.lock);
  297. if (pool.queue) {
  298. current = pool.queue;
  299. while (current -> next) current = current -> next;
  300. current -> next = queue;
  301. } else {
  302. pool.queue = queue;
  303. }
  304. LeaveCriticalSection(&pool.lock);
  305. SetEvent(pool.filled);
  306. return 0;
  307. }
  308. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  309. #ifdef SMP_DEBUG
  310. fprintf(STDERR, "Synchronization Waiting.\n");
  311. #endif
  312. while (num){
  313. #ifdef SMP_DEBUG
  314. fprintf(STDERR, "Waiting Queue ..\n");
  315. #endif
  316. WaitForSingleObject(queue->finish, INFINITE);
  317. CloseHandle(queue->finish);
  318. DeleteCriticalSection(&queue -> lock);
  319. queue = queue -> next;
  320. num --;
  321. }
  322. #ifdef SMP_DEBUG
  323. fprintf(STDERR, "Completely Done.\n\n");
  324. #endif
  325. return 0;
  326. }
  327. /* Execute Threads */
  328. int exec_blas(BLASLONG num, blas_queue_t *queue){
  329. #if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
  330. // Handle lazy re-init of the thread-pool after a POSIX fork
  331. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  332. #endif
  333. #ifndef ALL_THREADED
  334. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  335. #endif
  336. if ((num <= 0) || (queue == NULL)) return 0;
  337. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  338. routine = queue -> routine;
  339. if (!(queue -> mode & BLAS_LEGACY)) {
  340. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  341. queue -> sa, queue -> sb, 0);
  342. } else {
  343. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  344. }
  345. if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
  346. return 0;
  347. }
  348. /* Shutdown procedure, but user don't have to call this routine. The */
  349. /* kernel automatically kill threads. */
  350. int BLASFUNC(blas_thread_shutdown)(void){
  351. int i;
  352. if (!blas_server_avail) return 0;
  353. LOCK_COMMAND(&server_lock);
  354. if (blas_server_avail){
  355. SetEvent(pool.killed);
  356. for(i = 0; i < blas_num_threads - 1; i++){
  357. WaitForSingleObject(blas_threads[i], 5); //INFINITE);
  358. #ifndef OS_WINDOWSSTORE
  359. // TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
  360. TerminateThread(blas_threads[i],0);
  361. #endif
  362. }
  363. blas_server_avail = 0;
  364. }
  365. UNLOCK_COMMAND(&server_lock);
  366. return 0;
  367. }
  368. void goto_set_num_threads(int num_threads)
  369. {
  370. long i;
  371. if (num_threads < 1) num_threads = blas_cpu_number;
  372. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  373. if (num_threads > blas_num_threads) {
  374. LOCK_COMMAND(&server_lock);
  375. //increased_threads = 1;
  376. if (!blas_server_avail){
  377. InitializeCriticalSection(&pool.lock);
  378. pool.filled = CreateEvent(NULL, FALSE, FALSE, NULL);
  379. pool.killed = CreateEvent(NULL, TRUE, FALSE, NULL);
  380. pool.shutdown = 0;
  381. pool.queue = NULL;
  382. blas_server_avail = 1;
  383. }
  384. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  385. blas_threads[i] = CreateThread(NULL, 0,
  386. blas_thread_server, (void *)i,
  387. 0, &blas_threads_id[i]);
  388. }
  389. blas_num_threads = num_threads;
  390. UNLOCK_COMMAND(&server_lock);
  391. }
  392. blas_cpu_number = num_threads;
  393. }
  394. void openblas_set_num_threads(int num)
  395. {
  396. goto_set_num_threads(num);
  397. }