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

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