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

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