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

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