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

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