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.c 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*****************************************************************************
  2. Copyright (c) 2011, Lab of Parallel Software and Computational Science,ICSAS
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the ISCAS nor the names of its contributors may
  14. be used to endorse or promote products derived from this software
  15. without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************************/
  27. /*********************************************************************/
  28. /* Copyright 2009, 2010 The University of Texas at Austin. */
  29. /* All rights reserved. */
  30. /* */
  31. /* Redistribution and use in source and binary forms, with or */
  32. /* without modification, are permitted provided that the following */
  33. /* conditions are met: */
  34. /* */
  35. /* 1. Redistributions of source code must retain the above */
  36. /* copyright notice, this list of conditions and the following */
  37. /* disclaimer. */
  38. /* */
  39. /* 2. Redistributions in binary form must reproduce the above */
  40. /* copyright notice, this list of conditions and the following */
  41. /* disclaimer in the documentation and/or other materials */
  42. /* provided with the distribution. */
  43. /* */
  44. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  45. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  46. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  47. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  48. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  49. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  50. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  51. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  52. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  53. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  54. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  55. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  56. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  57. /* POSSIBILITY OF SUCH DAMAGE. */
  58. /* */
  59. /* The views and conclusions contained in the software and */
  60. /* documentation are those of the authors and should not be */
  61. /* interpreted as representing official policies, either expressed */
  62. /* or implied, of The University of Texas at Austin. */
  63. /*********************************************************************/
  64. #include "common.h"
  65. #ifdef OS_LINUX
  66. #include <dlfcn.h>
  67. #include <sys/resource.h>
  68. #endif
  69. #ifdef SMP_SERVER
  70. #undef MONITOR
  71. #undef TIMING
  72. #undef TIMING_DEBUG
  73. #undef NEED_STACKATTR
  74. #define ATTRIBUTE_SIZE 128
  75. /* This is a thread server model implementation. The threads are */
  76. /* spawned at first access to blas library, and still remains until */
  77. /* destruction routine is called. The number of threads are */
  78. /* equal to "OMP_NUM_THREADS - 1" and thread only wakes up when */
  79. /* jobs is queued. */
  80. /* We need this grobal for cheking if initialization is finished. */
  81. int blas_server_avail __attribute__((aligned(ATTRIBUTE_SIZE))) = 0;
  82. /* Local Variables */
  83. #if defined(USE_PTHREAD_LOCK)
  84. static pthread_mutex_t server_lock = PTHREAD_MUTEX_INITIALIZER;
  85. #elif defined(USE_PTHREAD_SPINLOCK)
  86. static pthread_spinlock_t server_lock = 0;
  87. #else
  88. static unsigned long server_lock = 0;
  89. #endif
  90. #define THREAD_STATUS_SLEEP 2
  91. #define THREAD_STATUS_WAKEUP 4
  92. static pthread_t blas_threads [MAX_CPU_NUMBER];
  93. typedef struct {
  94. blas_queue_t * volatile queue __attribute__((aligned(ATTRIBUTE_SIZE)));
  95. #if defined(OS_LINUX) && !defined(NO_AFFINITY)
  96. int node;
  97. #endif
  98. volatile long status;
  99. pthread_mutex_t lock;
  100. pthread_cond_t wakeup;
  101. } thread_status_t;
  102. static thread_status_t thread_status[MAX_CPU_NUMBER] __attribute__((aligned(ATTRIBUTE_SIZE)));
  103. #ifndef THREAD_TIMEOUT
  104. #define THREAD_TIMEOUT 28
  105. #endif
  106. static unsigned int thread_timeout = (1U << (THREAD_TIMEOUT));
  107. #ifdef MONITOR
  108. /* Monitor is a function to see thread's status for every seconds. */
  109. /* Usually it turns off and it's for debugging. */
  110. static pthread_t monitor_thread;
  111. static int main_status[MAX_CPU_NUMBER];
  112. #define MAIN_ENTER 0x01
  113. #define MAIN_EXIT 0x02
  114. #define MAIN_TRYLOCK 0x03
  115. #define MAIN_LOCKSUCCESS 0x04
  116. #define MAIN_QUEUING 0x05
  117. #define MAIN_RECEIVING 0x06
  118. #define MAIN_RUNNING1 0x07
  119. #define MAIN_RUNNING2 0x08
  120. #define MAIN_RUNNING3 0x09
  121. #define MAIN_WAITING 0x0a
  122. #define MAIN_SLEEPING 0x0b
  123. #define MAIN_FINISH 0x0c
  124. #define MAIN_DONE 0x0d
  125. #endif
  126. #define BLAS_QUEUE_FINISHED 3
  127. #define BLAS_QUEUE_RUNNING 4
  128. #ifdef TIMING
  129. BLASLONG exit_time[MAX_CPU_NUMBER];
  130. #endif
  131. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  132. if (!(mode & BLAS_COMPLEX)){
  133. #ifdef EXPRECISION
  134. if (mode & BLAS_XDOUBLE){
  135. /* REAL / Extended Double */
  136. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  137. xdouble *, BLASLONG, xdouble *, BLASLONG,
  138. xdouble *, BLASLONG, void *) = func;
  139. afunc(args -> m, args -> n, args -> k,
  140. ((xdouble *)args -> alpha)[0],
  141. args -> a, args -> lda,
  142. args -> b, args -> ldb,
  143. args -> c, args -> ldc, sb);
  144. } else
  145. #endif
  146. if (mode & BLAS_DOUBLE){
  147. /* REAL / Double */
  148. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  149. double *, BLASLONG, double *, BLASLONG,
  150. double *, BLASLONG, void *) = func;
  151. afunc(args -> m, args -> n, args -> k,
  152. ((double *)args -> alpha)[0],
  153. args -> a, args -> lda,
  154. args -> b, args -> ldb,
  155. args -> c, args -> ldc, sb);
  156. } else {
  157. /* REAL / Single */
  158. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  159. float *, BLASLONG, float *, BLASLONG,
  160. float *, BLASLONG, void *) = func;
  161. afunc(args -> m, args -> n, args -> k,
  162. ((float *)args -> alpha)[0],
  163. args -> a, args -> lda,
  164. args -> b, args -> ldb,
  165. args -> c, args -> ldc, sb);
  166. }
  167. } else {
  168. #ifdef EXPRECISION
  169. if (mode & BLAS_XDOUBLE){
  170. /* COMPLEX / Extended Double */
  171. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  172. xdouble *, BLASLONG, xdouble *, BLASLONG,
  173. xdouble *, BLASLONG, void *) = func;
  174. afunc(args -> m, args -> n, args -> k,
  175. ((xdouble *)args -> alpha)[0],
  176. ((xdouble *)args -> alpha)[1],
  177. args -> a, args -> lda,
  178. args -> b, args -> ldb,
  179. args -> c, args -> ldc, sb);
  180. } else
  181. #endif
  182. if (mode & BLAS_DOUBLE){
  183. /* COMPLEX / Double */
  184. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  185. double *, BLASLONG, double *, BLASLONG,
  186. double *, BLASLONG, void *) = func;
  187. afunc(args -> m, args -> n, args -> k,
  188. ((double *)args -> alpha)[0],
  189. ((double *)args -> alpha)[1],
  190. args -> a, args -> lda,
  191. args -> b, args -> ldb,
  192. args -> c, args -> ldc, sb);
  193. } else {
  194. /* COMPLEX / Single */
  195. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  196. float *, BLASLONG, float *, BLASLONG,
  197. float *, BLASLONG, void *) = func;
  198. afunc(args -> m, args -> n, args -> k,
  199. ((float *)args -> alpha)[0],
  200. ((float *)args -> alpha)[1],
  201. args -> a, args -> lda,
  202. args -> b, args -> ldb,
  203. args -> c, args -> ldc, sb);
  204. }
  205. }
  206. }
  207. #if defined(OS_LINUX) && !defined(NO_AFFINITY)
  208. int gotoblas_set_affinity(int);
  209. int gotoblas_set_affinity2(int);
  210. int get_node(void);
  211. #endif
  212. static int increased_threads = 0;
  213. static int blas_thread_server(void *arg){
  214. /* Thread identifier */
  215. BLASLONG cpu = (BLASLONG)arg;
  216. unsigned int last_tick;
  217. void *buffer, *sa, *sb;
  218. blas_queue_t *queue;
  219. #ifdef TIMING_DEBUG
  220. unsigned long start, stop;
  221. #endif
  222. #if defined(OS_LINUX) && !defined(NO_AFFINITY)
  223. if (!increased_threads)
  224. thread_status[cpu].node = gotoblas_set_affinity(cpu + 1);
  225. else
  226. thread_status[cpu].node = gotoblas_set_affinity(-1);
  227. #endif
  228. #ifdef MONITOR
  229. main_status[cpu] = MAIN_ENTER;
  230. #endif
  231. buffer = blas_memory_alloc(2);
  232. #ifdef SMP_DEBUG
  233. fprintf(STDERR, "Server[%2ld] Thread has just been spawned!\n", cpu);
  234. #endif
  235. while (1){
  236. #ifdef MONITOR
  237. main_status[cpu] = MAIN_QUEUING;
  238. #endif
  239. #ifdef TIMING
  240. exit_time[cpu] = rpcc();
  241. #endif
  242. last_tick = (unsigned int)rpcc();
  243. while (!thread_status[cpu].queue) {
  244. YIELDING;
  245. if ((unsigned int)rpcc() - last_tick > thread_timeout) {
  246. pthread_mutex_lock (&thread_status[cpu].lock);
  247. if (!thread_status[cpu].queue) {
  248. thread_status[cpu].status = THREAD_STATUS_SLEEP;
  249. while (thread_status[cpu].status == THREAD_STATUS_SLEEP) {
  250. #ifdef MONITOR
  251. main_status[cpu] = MAIN_SLEEPING;
  252. #endif
  253. pthread_cond_wait(&thread_status[cpu].wakeup, &thread_status[cpu].lock);
  254. }
  255. }
  256. pthread_mutex_unlock(&thread_status[cpu].lock);
  257. last_tick = (unsigned int)rpcc();
  258. }
  259. }
  260. queue = thread_status[cpu].queue;
  261. if ((long)queue == -1) break;
  262. #ifdef MONITOR
  263. main_status[cpu] = MAIN_RECEIVING;
  264. #endif
  265. #ifdef TIMING_DEBUG
  266. start = rpcc();
  267. #endif
  268. if (queue) {
  269. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  270. thread_status[cpu].queue = (blas_queue_t *)1;
  271. sa = queue -> sa;
  272. sb = queue -> sb;
  273. #ifdef SMP_DEBUG
  274. if (queue -> args) {
  275. fprintf(STDERR, "Server[%2ld] Calculation started. Mode = 0x%03x M = %3ld N=%3ld K=%3ld\n",
  276. cpu, queue->mode, queue-> args ->m, queue->args->n, queue->args->k);
  277. }
  278. #endif
  279. #ifdef CONSISTENT_FPCSR
  280. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  281. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  282. #endif
  283. #ifdef MONITOR
  284. main_status[cpu] = MAIN_RUNNING1;
  285. #endif
  286. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  287. if (sb == NULL) {
  288. if (!(queue -> mode & BLAS_COMPLEX)){
  289. #ifdef EXPRECISION
  290. if (queue -> mode & BLAS_XDOUBLE){
  291. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  292. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  293. } else
  294. #endif
  295. if (queue -> mode & BLAS_DOUBLE){
  296. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  297. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  298. } else {
  299. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  300. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  301. }
  302. } else {
  303. #ifdef EXPRECISION
  304. if (queue -> mode & BLAS_XDOUBLE){
  305. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  306. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  307. } else
  308. #endif
  309. if (queue -> mode & BLAS_DOUBLE){
  310. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  311. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  312. } else {
  313. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  314. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  315. }
  316. }
  317. }
  318. #ifdef MONITOR
  319. main_status[cpu] = MAIN_RUNNING2;
  320. #endif
  321. if (queue -> mode & BLAS_LEGACY) {
  322. legacy_exec(routine, queue -> mode, queue -> args, sb);
  323. } else
  324. if (queue -> mode & BLAS_PTHREAD) {
  325. void (*pthreadcompat)(void *) = queue -> routine;
  326. (pthreadcompat)(queue -> args);
  327. } else
  328. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  329. #ifdef SMP_DEBUG
  330. fprintf(STDERR, "Server[%2ld] Calculation finished!\n", cpu);
  331. #endif
  332. #ifdef MONITOR
  333. main_status[cpu] = MAIN_FINISH;
  334. #endif
  335. thread_status[cpu].queue = (blas_queue_t * volatile) ((long)thread_status[cpu].queue & 0); /* Need a trick */
  336. WMB;
  337. }
  338. #ifdef MONITOR
  339. main_status[cpu] = MAIN_DONE;
  340. #endif
  341. #ifdef TIMING_DEBUG
  342. stop = rpcc();
  343. fprintf(STDERR, "Thread[%ld] : %16lu %16lu (%8lu cycles)\n", cpu + 1,
  344. start, stop,
  345. stop - start);
  346. #endif
  347. }
  348. /* Shutdown procedure */
  349. #ifdef SMP_DEBUG
  350. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  351. #endif
  352. blas_memory_free(buffer);
  353. pthread_exit(NULL);
  354. return 0;
  355. }
  356. #ifdef MONITOR
  357. static BLASLONG num_suspend = 0;
  358. static int blas_monitor(void *arg){
  359. int i;
  360. while(1){
  361. for (i = 0; i < blas_num_threads - 1; i++){
  362. switch (main_status[i]) {
  363. case MAIN_ENTER :
  364. fprintf(STDERR, "THREAD[%2d] : Entering.\n", i);
  365. break;
  366. case MAIN_EXIT :
  367. fprintf(STDERR, "THREAD[%2d] : Exiting.\n", i);
  368. break;
  369. case MAIN_TRYLOCK :
  370. fprintf(STDERR, "THREAD[%2d] : Trying lock operation.\n", i);
  371. break;
  372. case MAIN_QUEUING :
  373. fprintf(STDERR, "THREAD[%2d] : Queuing.\n", i);
  374. break;
  375. case MAIN_RECEIVING :
  376. fprintf(STDERR, "THREAD[%2d] : Receiving.\n", i);
  377. break;
  378. case MAIN_RUNNING1 :
  379. fprintf(STDERR, "THREAD[%2d] : Running1.\n", i);
  380. break;
  381. case MAIN_RUNNING2 :
  382. fprintf(STDERR, "THREAD[%2d] : Running2.\n", i);
  383. break;
  384. case MAIN_RUNNING3 :
  385. fprintf(STDERR, "THREAD[%2d] : Running3.\n", i);
  386. break;
  387. case MAIN_WAITING :
  388. fprintf(STDERR, "THREAD[%2d] : Waiting.\n", i);
  389. break;
  390. case MAIN_SLEEPING :
  391. fprintf(STDERR, "THREAD[%2d] : Sleeping.\n", i);
  392. break;
  393. case MAIN_FINISH :
  394. fprintf(STDERR, "THREAD[%2d] : Finishing.\n", i);
  395. break;
  396. case MAIN_DONE :
  397. fprintf(STDERR, "THREAD[%2d] : Job is done.\n", i);
  398. break;
  399. }
  400. fprintf(stderr, "Total number of suspended ... %ld\n", num_suspend);
  401. }
  402. sleep(1);
  403. }
  404. return 0;
  405. }
  406. #endif
  407. /* Initializing routine */
  408. int blas_thread_init(void){
  409. BLASLONG i;
  410. int ret;
  411. #ifdef NEED_STACKATTR
  412. pthread_attr_t attr;
  413. #endif
  414. if (blas_server_avail) return 0;
  415. #ifdef NEED_STACKATTR
  416. pthread_attr_init(&attr);
  417. pthread_attr_setguardsize(&attr, 0x1000U);
  418. pthread_attr_setstacksize( &attr, 0x1000U);
  419. #endif
  420. LOCK_COMMAND(&server_lock);
  421. if (!blas_server_avail){
  422. char *p;
  423. p = getenv("THREAD_TIMEOUT");
  424. if (p) {
  425. thread_timeout = atoi(p);
  426. if (thread_timeout < 4) thread_timeout = 4;
  427. if (thread_timeout > 30) thread_timeout = 30;
  428. thread_timeout = (1 << thread_timeout);
  429. }else{
  430. p = getenv("GOTO_THREAD_TIMEOUT");
  431. if (p) {
  432. thread_timeout = atoi(p);
  433. if (thread_timeout < 4) thread_timeout = 4;
  434. if (thread_timeout > 30) thread_timeout = 30;
  435. thread_timeout = (1 << thread_timeout);
  436. }
  437. }
  438. for(i = 0; i < blas_num_threads - 1; i++){
  439. thread_status[i].queue = (blas_queue_t *)NULL;
  440. thread_status[i].status = THREAD_STATUS_WAKEUP;
  441. pthread_mutex_init(&thread_status[i].lock, NULL);
  442. pthread_cond_init (&thread_status[i].wakeup, NULL);
  443. #ifdef NEED_STACKATTR
  444. ret=pthread_create(&blas_threads[i], &attr,
  445. (void *)&blas_thread_server, (void *)i);
  446. #else
  447. ret=pthread_create(&blas_threads[i], NULL,
  448. (void *)&blas_thread_server, (void *)i);
  449. #endif
  450. if(ret!=0){
  451. fprintf(STDERR,"OpenBLAS: pthread_creat error in blas_thread_init function. Error code:%d\n",ret);
  452. exit(1);
  453. }
  454. }
  455. #ifdef MONITOR
  456. pthread_create(&monitor_thread, NULL,
  457. (void *)&blas_monitor, (void *)NULL);
  458. #endif
  459. blas_server_avail = 1;
  460. }
  461. UNLOCK_COMMAND(&server_lock);
  462. return 0;
  463. }
  464. /*
  465. User can call one of two routines.
  466. exec_blas_async ... immediately returns after jobs are queued.
  467. exec_blas ... returns after jobs are finished.
  468. */
  469. static BLASULONG exec_queue_lock = 0;
  470. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  471. BLASLONG i = 0;
  472. blas_queue_t *current = queue;
  473. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  474. int node = get_node();
  475. int nodes = get_num_nodes();
  476. #endif
  477. #ifdef SMP_DEBUG
  478. int exec_count = 0;
  479. fprintf(STDERR, "Exec_blas_async is called. Position = %d\n", pos);
  480. #endif
  481. blas_lock(&exec_queue_lock);
  482. while (queue) {
  483. queue -> position = pos;
  484. #ifdef CONSISTENT_FPCSR
  485. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue -> x87_mode));
  486. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue -> sse_mode));
  487. #endif
  488. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  489. /* Node Mapping Mode */
  490. if (queue -> mode & BLAS_NODE) {
  491. do {
  492. while((thread_status[i].node != node || thread_status[i].queue) && (i < blas_num_threads - 1)) i ++;
  493. if (i < blas_num_threads - 1) break;
  494. i ++;
  495. if (i >= blas_num_threads - 1) {
  496. i = 0;
  497. node ++;
  498. if (node >= nodes) node = 0;
  499. }
  500. } while (1);
  501. } else {
  502. while(thread_status[i].queue) {
  503. i ++;
  504. if (i >= blas_num_threads - 1) i = 0;
  505. }
  506. }
  507. #else
  508. while(thread_status[i].queue) {
  509. i ++;
  510. if (i >= blas_num_threads - 1) i = 0;
  511. }
  512. #endif
  513. queue -> assigned = i;
  514. WMB;
  515. thread_status[i].queue = queue;
  516. WMB;
  517. queue = queue -> next;
  518. pos ++;
  519. #ifdef SMP_DEBUG
  520. exec_count ++;
  521. #endif
  522. }
  523. blas_unlock(&exec_queue_lock);
  524. #ifdef SMP_DEBUG
  525. fprintf(STDERR, "Done(Number of threads = %2ld).\n", exec_count);
  526. #endif
  527. while (current) {
  528. pos = current -> assigned;
  529. if ((BLASULONG)thread_status[pos].queue > 1) {
  530. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  531. pthread_mutex_lock (&thread_status[pos].lock);
  532. #ifdef MONITOR
  533. num_suspend ++;
  534. #endif
  535. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  536. thread_status[pos].status = THREAD_STATUS_WAKEUP;
  537. pthread_cond_signal(&thread_status[pos].wakeup);
  538. }
  539. pthread_mutex_unlock(&thread_status[pos].lock);
  540. }
  541. }
  542. current = current -> next;
  543. }
  544. return 0;
  545. }
  546. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  547. while ((num > 0) && queue) {
  548. while(thread_status[queue -> assigned].queue) {
  549. YIELDING;
  550. };
  551. queue = queue -> next;
  552. num --;
  553. }
  554. #ifdef SMP_DEBUG
  555. fprintf(STDERR, "Done.\n\n");
  556. #endif
  557. return 0;
  558. }
  559. /* Execute Threads */
  560. int exec_blas(BLASLONG num, blas_queue_t *queue){
  561. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  562. #ifdef TIMING_DEBUG
  563. BLASULONG start, stop;
  564. #endif
  565. if ((num <= 0) || (queue == NULL)) return 0;
  566. #ifdef SMP_DEBUG
  567. fprintf(STDERR, "Exec_blas is called. Number of executing threads : %ld\n", num);
  568. #endif
  569. #ifdef __ELF__
  570. if (omp_in_parallel && (num > 1)) {
  571. if (omp_in_parallel() > 0) {
  572. fprintf(stderr,
  573. "OpenBLAS Warning : Detect OpenMP Loop and this application may hang. "
  574. "Please rebuild the library with USE_OPENMP=1 option.\n");
  575. }
  576. }
  577. #endif
  578. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  579. #ifdef TIMING_DEBUG
  580. start = rpcc();
  581. fprintf(STDERR, "\n");
  582. #endif
  583. routine = queue -> routine;
  584. if (queue -> mode & BLAS_LEGACY) {
  585. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  586. } else
  587. if (queue -> mode & BLAS_PTHREAD) {
  588. void (*pthreadcompat)(void *) = queue -> routine;
  589. (pthreadcompat)(queue -> args);
  590. } else
  591. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  592. queue -> sa, queue -> sb, 0);
  593. #ifdef TIMING_DEBUG
  594. stop = rpcc();
  595. #endif
  596. if ((num > 1) && queue -> next) exec_blas_async_wait(num - 1, queue -> next);
  597. #ifdef TIMING_DEBUG
  598. fprintf(STDERR, "Thread[0] : %16lu %16lu (%8lu cycles)\n",
  599. start, stop,
  600. stop - start);
  601. #endif
  602. return 0;
  603. }
  604. void goto_set_num_threads(int num_threads) {
  605. long i;
  606. if (num_threads < 1) num_threads = blas_num_threads;
  607. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  608. if (num_threads > blas_num_threads) {
  609. LOCK_COMMAND(&server_lock);
  610. increased_threads = 1;
  611. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  612. thread_status[i].queue = (blas_queue_t *)NULL;
  613. thread_status[i].status = THREAD_STATUS_WAKEUP;
  614. pthread_mutex_init(&thread_status[i].lock, NULL);
  615. pthread_cond_init (&thread_status[i].wakeup, NULL);
  616. #ifdef NEED_STACKATTR
  617. pthread_create(&blas_threads[i], &attr,
  618. (void *)&blas_thread_server, (void *)i);
  619. #else
  620. pthread_create(&blas_threads[i], NULL,
  621. (void *)&blas_thread_server, (void *)i);
  622. #endif
  623. }
  624. blas_num_threads = num_threads;
  625. UNLOCK_COMMAND(&server_lock);
  626. }
  627. blas_cpu_number = num_threads;
  628. #if defined(ARCH_MIPS64)
  629. //set parameters for different number of threads.
  630. blas_set_parameter();
  631. #endif
  632. }
  633. void openblas_set_num_threads(int num_threads) {
  634. goto_set_num_threads(num_threads);
  635. }
  636. /* Compatible function with pthread_create / join */
  637. int gotoblas_pthread(int numthreads, void *function, void *args, int stride) {
  638. blas_queue_t queue[MAX_CPU_NUMBER];
  639. int i;
  640. if (numthreads <= 0) return 0;
  641. #ifdef SMP
  642. if (blas_cpu_number == 0) blas_get_cpu_number();
  643. #ifdef SMP_SERVER
  644. if (blas_server_avail == 0) blas_thread_init();
  645. #endif
  646. #endif
  647. for (i = 0; i < numthreads; i ++) {
  648. queue[i].mode = BLAS_PTHREAD;
  649. queue[i].routine = function;
  650. queue[i].args = args;
  651. queue[i].range_m = NULL;
  652. queue[i].range_n = NULL;
  653. queue[i].sa = args;
  654. queue[i].sb = args;
  655. queue[i].next = &queue[i + 1];
  656. args += stride;
  657. }
  658. queue[numthreads - 1].next = NULL;
  659. exec_blas(numthreads, queue);
  660. return 0;
  661. }
  662. /* Shutdown procedure, but user don't have to call this routine. The */
  663. /* kernel automatically kill threads. */
  664. int BLASFUNC(blas_thread_shutdown)(void){
  665. int i;
  666. if (!blas_server_avail) return 0;
  667. LOCK_COMMAND(&server_lock);
  668. for (i = 0; i < blas_num_threads - 1; i++) {
  669. blas_lock(&exec_queue_lock);
  670. thread_status[i].queue = (blas_queue_t *)-1;
  671. blas_unlock(&exec_queue_lock);
  672. pthread_mutex_lock (&thread_status[i].lock);
  673. thread_status[i].status = THREAD_STATUS_WAKEUP;
  674. pthread_cond_signal (&thread_status[i].wakeup);
  675. pthread_mutex_unlock(&thread_status[i].lock);
  676. }
  677. for(i = 0; i < blas_num_threads - 1; i++){
  678. pthread_join(blas_threads[i], NULL);
  679. }
  680. for(i = 0; i < blas_num_threads - 1; i++){
  681. pthread_mutex_destroy(&thread_status[i].lock);
  682. pthread_cond_destroy (&thread_status[i].wakeup);
  683. }
  684. #ifdef NEED_STACKATTR
  685. pthread_attr_destory(&attr);
  686. #endif
  687. blas_server_avail = 0;
  688. UNLOCK_COMMAND(&server_lock);
  689. return 0;
  690. }
  691. #endif

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.