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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. #ifdef HAVE_C11
  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_PREC) == 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_PREC) == 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 if ((mode & BLAS_PREC) == BLAS_SINGLE){
  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. #ifdef BUILD_HALF
  193. } else if ((mode & BLAS_PREC) == BLAS_BFLOAT16){
  194. /* REAL / BFLOAT16 */
  195. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, bfloat16,
  196. bfloat16 *, BLASLONG, bfloat16 *, BLASLONG,
  197. bfloat16 *, BLASLONG, void *) = func;
  198. afunc(args -> m, args -> n, args -> k,
  199. ((bfloat16 *)args -> alpha)[0],
  200. args -> a, args -> lda,
  201. args -> b, args -> ldb,
  202. args -> c, args -> ldc, sb);
  203. } else if ((mode & BLAS_PREC) == BLAS_STOBF16){
  204. /* REAL / BLAS_STOBF16 */
  205. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  206. float *, BLASLONG, bfloat16 *, BLASLONG,
  207. float *, BLASLONG, void *) = func;
  208. afunc(args -> m, args -> n, args -> k,
  209. ((float *)args -> alpha)[0],
  210. args -> a, args -> lda,
  211. args -> b, args -> ldb,
  212. args -> c, args -> ldc, sb);
  213. } else if ((mode & BLAS_PREC) == BLAS_DTOBF16){
  214. /* REAL / BLAS_DTOBF16 */
  215. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  216. double *, BLASLONG, bfloat16 *, BLASLONG,
  217. double *, BLASLONG, void *) = func;
  218. afunc(args -> m, args -> n, args -> k,
  219. ((double *)args -> alpha)[0],
  220. args -> a, args -> lda,
  221. args -> b, args -> ldb,
  222. args -> c, args -> ldc, sb);
  223. #endif
  224. } else {
  225. /* REAL / Other types in future */
  226. }
  227. } else {
  228. #ifdef EXPRECISION
  229. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  230. /* COMPLEX / Extended Double */
  231. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  232. xdouble *, BLASLONG, xdouble *, BLASLONG,
  233. xdouble *, BLASLONG, void *) = func;
  234. afunc(args -> m, args -> n, args -> k,
  235. ((xdouble *)args -> alpha)[0],
  236. ((xdouble *)args -> alpha)[1],
  237. args -> a, args -> lda,
  238. args -> b, args -> ldb,
  239. args -> c, args -> ldc, sb);
  240. } else
  241. #endif
  242. if ((mode & BLAS_PREC) == BLAS_DOUBLE) {
  243. /* COMPLEX / Double */
  244. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  245. double *, BLASLONG, double *, BLASLONG,
  246. double *, BLASLONG, void *) = func;
  247. afunc(args -> m, args -> n, args -> k,
  248. ((double *)args -> alpha)[0],
  249. ((double *)args -> alpha)[1],
  250. args -> a, args -> lda,
  251. args -> b, args -> ldb,
  252. args -> c, args -> ldc, sb);
  253. } else if ((mode & BLAS_PREC) == BLAS_SINGLE) {
  254. /* COMPLEX / Single */
  255. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  256. float *, BLASLONG, float *, BLASLONG,
  257. float *, BLASLONG, void *) = func;
  258. afunc(args -> m, args -> n, args -> k,
  259. ((float *)args -> alpha)[0],
  260. ((float *)args -> alpha)[1],
  261. args -> a, args -> lda,
  262. args -> b, args -> ldb,
  263. args -> c, args -> ldc, sb);
  264. } else {
  265. /* COMPLEX / Other types in future */
  266. }
  267. }
  268. }
  269. #if defined(OS_LINUX) && !defined(NO_AFFINITY)
  270. int gotoblas_set_affinity(int);
  271. int gotoblas_set_affinity2(int);
  272. int get_node(void);
  273. #endif
  274. static int increased_threads = 0;
  275. #ifdef OS_LINUX
  276. extern int openblas_get_num_threads(void);
  277. int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set) {
  278. const int active_threads = openblas_get_num_threads();
  279. if (thread_idx < 0 || thread_idx >= active_threads) {
  280. errno = EINVAL;
  281. return -1;
  282. }
  283. pthread_t thread = (thread_idx == active_threads - 1)
  284. ? pthread_self()
  285. : blas_threads[thread_idx];
  286. return pthread_setaffinity_np(thread, cpusetsize, cpu_set);
  287. }
  288. #endif
  289. static void* blas_thread_server(void *arg){
  290. /* Thread identifier */
  291. BLASLONG cpu = (BLASLONG)arg;
  292. unsigned int last_tick;
  293. void *buffer, *sa, *sb;
  294. blas_queue_t *queue;
  295. blas_queue_t *tscq;
  296. #ifdef TIMING_DEBUG
  297. unsigned long start, stop;
  298. #endif
  299. #if defined(OS_LINUX) && !defined(NO_AFFINITY)
  300. if (!increased_threads)
  301. thread_status[cpu].node = gotoblas_set_affinity(cpu + 1);
  302. else
  303. thread_status[cpu].node = gotoblas_set_affinity(-1);
  304. #endif
  305. #ifdef MONITOR
  306. main_status[cpu] = MAIN_ENTER;
  307. #endif
  308. buffer = blas_memory_alloc(2);
  309. #ifdef SMP_DEBUG
  310. fprintf(STDERR, "Server[%2ld] Thread has just been spawned!\n", cpu);
  311. #endif
  312. while (1){
  313. #ifdef MONITOR
  314. main_status[cpu] = MAIN_QUEUING;
  315. #endif
  316. #ifdef TIMING
  317. exit_time[cpu] = rpcc();
  318. #endif
  319. last_tick = (unsigned int)rpcc();
  320. tscq = atomic_load_queue(&thread_status[cpu].queue);
  321. while(!tscq) {
  322. YIELDING;
  323. if ((unsigned int)rpcc() - last_tick > thread_timeout) {
  324. if (!atomic_load_queue(&thread_status[cpu].queue)) {
  325. pthread_mutex_lock (&thread_status[cpu].lock);
  326. thread_status[cpu].status = THREAD_STATUS_SLEEP;
  327. while (thread_status[cpu].status == THREAD_STATUS_SLEEP &&
  328. !atomic_load_queue(&thread_status[cpu].queue)) {
  329. #ifdef MONITOR
  330. main_status[cpu] = MAIN_SLEEPING;
  331. #endif
  332. pthread_cond_wait(&thread_status[cpu].wakeup, &thread_status[cpu].lock);
  333. }
  334. pthread_mutex_unlock(&thread_status[cpu].lock);
  335. }
  336. last_tick = (unsigned int)rpcc();
  337. }
  338. tscq = atomic_load_queue(&thread_status[cpu].queue);
  339. }
  340. queue = atomic_load_queue(&thread_status[cpu].queue);
  341. MB;
  342. if ((long)queue == -1) break;
  343. #ifdef MONITOR
  344. main_status[cpu] = MAIN_RECEIVING;
  345. #endif
  346. #ifdef TIMING_DEBUG
  347. start = rpcc();
  348. #endif
  349. if (queue) {
  350. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  351. atomic_store_queue(&thread_status[cpu].queue, (blas_queue_t *)1);
  352. sa = queue -> sa;
  353. sb = queue -> sb;
  354. #ifdef SMP_DEBUG
  355. if (queue -> args) {
  356. fprintf(STDERR, "Server[%2ld] Calculation started. Mode = 0x%03x M = %3ld N=%3ld K=%3ld\n",
  357. cpu, queue->mode, queue-> args ->m, queue->args->n, queue->args->k);
  358. }
  359. #endif
  360. #ifdef CONSISTENT_FPCSR
  361. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  362. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  363. #endif
  364. #ifdef MONITOR
  365. main_status[cpu] = MAIN_RUNNING1;
  366. #endif
  367. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  368. if (sb == NULL) {
  369. if (!(queue -> mode & BLAS_COMPLEX)){
  370. #ifdef EXPRECISION
  371. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  372. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  373. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  374. } else
  375. #endif
  376. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE) {
  377. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  378. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  379. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  380. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  381. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  382. } else {
  383. /* Other types in future */
  384. }
  385. } else {
  386. #ifdef EXPRECISION
  387. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  388. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  389. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  390. } else
  391. #endif
  392. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  393. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  394. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  395. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  396. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  397. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  398. } else {
  399. /* Other types in future */
  400. }
  401. }
  402. queue->sb=sb;
  403. }
  404. #ifdef MONITOR
  405. main_status[cpu] = MAIN_RUNNING2;
  406. #endif
  407. if (queue -> mode & BLAS_LEGACY) {
  408. legacy_exec(routine, queue -> mode, queue -> args, sb);
  409. } else
  410. if (queue -> mode & BLAS_PTHREAD) {
  411. void (*pthreadcompat)(void *) = queue -> routine;
  412. (pthreadcompat)(queue -> args);
  413. } else
  414. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  415. #ifdef SMP_DEBUG
  416. fprintf(STDERR, "Server[%2ld] Calculation finished!\n", cpu);
  417. #endif
  418. #ifdef MONITOR
  419. main_status[cpu] = MAIN_FINISH;
  420. #endif
  421. // arm: make sure all results are written out _before_
  422. // thread is marked as done and other threads use them
  423. MB;
  424. atomic_store_queue(&thread_status[cpu].queue, (blas_queue_t *)0);
  425. }
  426. #ifdef MONITOR
  427. main_status[cpu] = MAIN_DONE;
  428. #endif
  429. #ifdef TIMING_DEBUG
  430. stop = rpcc();
  431. fprintf(STDERR, "Thread[%ld] : %16lu %16lu (%8lu cycles)\n", cpu + 1,
  432. start, stop,
  433. stop - start);
  434. #endif
  435. }
  436. /* Shutdown procedure */
  437. #ifdef SMP_DEBUG
  438. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  439. #endif
  440. blas_memory_free(buffer);
  441. //pthread_exit(NULL);
  442. return NULL;
  443. }
  444. #ifdef MONITOR
  445. static BLASLONG num_suspend = 0;
  446. static int blas_monitor(void *arg){
  447. int i;
  448. while(1){
  449. for (i = 0; i < blas_num_threads - 1; i++){
  450. switch (main_status[i]) {
  451. case MAIN_ENTER :
  452. fprintf(STDERR, "THREAD[%2d] : Entering.\n", i);
  453. break;
  454. case MAIN_EXIT :
  455. fprintf(STDERR, "THREAD[%2d] : Exiting.\n", i);
  456. break;
  457. case MAIN_TRYLOCK :
  458. fprintf(STDERR, "THREAD[%2d] : Trying lock operation.\n", i);
  459. break;
  460. case MAIN_QUEUING :
  461. fprintf(STDERR, "THREAD[%2d] : Queuing.\n", i);
  462. break;
  463. case MAIN_RECEIVING :
  464. fprintf(STDERR, "THREAD[%2d] : Receiving.\n", i);
  465. break;
  466. case MAIN_RUNNING1 :
  467. fprintf(STDERR, "THREAD[%2d] : Running1.\n", i);
  468. break;
  469. case MAIN_RUNNING2 :
  470. fprintf(STDERR, "THREAD[%2d] : Running2.\n", i);
  471. break;
  472. case MAIN_RUNNING3 :
  473. fprintf(STDERR, "THREAD[%2d] : Running3.\n", i);
  474. break;
  475. case MAIN_WAITING :
  476. fprintf(STDERR, "THREAD[%2d] : Waiting.\n", i);
  477. break;
  478. case MAIN_SLEEPING :
  479. fprintf(STDERR, "THREAD[%2d] : Sleeping.\n", i);
  480. break;
  481. case MAIN_FINISH :
  482. fprintf(STDERR, "THREAD[%2d] : Finishing.\n", i);
  483. break;
  484. case MAIN_DONE :
  485. fprintf(STDERR, "THREAD[%2d] : Job is done.\n", i);
  486. break;
  487. }
  488. fprintf(stderr, "Total number of suspended ... %ld\n", num_suspend);
  489. }
  490. sleep(1);
  491. }
  492. return 0;
  493. }
  494. #endif
  495. /* Initializing routine */
  496. int blas_thread_init(void){
  497. BLASLONG i;
  498. int ret;
  499. int thread_timeout_env;
  500. #ifdef NEED_STACKATTR
  501. pthread_attr_t attr;
  502. #endif
  503. if (blas_server_avail) return 0;
  504. #ifdef NEED_STACKATTR
  505. pthread_attr_init(&attr);
  506. pthread_attr_setguardsize(&attr, 0x1000U);
  507. pthread_attr_setstacksize( &attr, 0x1000U);
  508. #endif
  509. LOCK_COMMAND(&server_lock);
  510. if (!blas_server_avail){
  511. thread_timeout_env=openblas_thread_timeout();
  512. if (thread_timeout_env>0) {
  513. if (thread_timeout_env < 4) thread_timeout_env = 4;
  514. if (thread_timeout_env > 30) thread_timeout_env = 30;
  515. thread_timeout = (1 << thread_timeout_env);
  516. }
  517. for(i = 0; i < blas_num_threads - 1; i++){
  518. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)0);
  519. thread_status[i].status = THREAD_STATUS_WAKEUP;
  520. pthread_mutex_init(&thread_status[i].lock, NULL);
  521. pthread_cond_init (&thread_status[i].wakeup, NULL);
  522. #ifdef NEED_STACKATTR
  523. ret=pthread_create(&blas_threads[i], &attr,
  524. &blas_thread_server, (void *)i);
  525. #else
  526. ret=pthread_create(&blas_threads[i], NULL,
  527. &blas_thread_server, (void *)i);
  528. #endif
  529. if(ret!=0){
  530. struct rlimit rlim;
  531. const char *msg = strerror(ret);
  532. fprintf(STDERR, "OpenBLAS blas_thread_init: pthread_create failed for thread %ld of %d: %s\n", i+1,blas_num_threads,msg);
  533. #ifdef RLIMIT_NPROC
  534. if(0 == getrlimit(RLIMIT_NPROC, &rlim)) {
  535. fprintf(STDERR, "OpenBLAS blas_thread_init: RLIMIT_NPROC "
  536. "%ld current, %ld max\n", (long)(rlim.rlim_cur), (long)(rlim.rlim_max));
  537. }
  538. #endif
  539. if(0 != raise(SIGINT)) {
  540. fprintf(STDERR, "OpenBLAS blas_thread_init: calling exit(3)\n");
  541. exit(EXIT_FAILURE);
  542. }
  543. }
  544. }
  545. #ifdef MONITOR
  546. pthread_create(&monitor_thread, NULL,
  547. (void *)&blas_monitor, (void *)NULL);
  548. #endif
  549. blas_server_avail = 1;
  550. }
  551. UNLOCK_COMMAND(&server_lock);
  552. return 0;
  553. }
  554. /*
  555. User can call one of two routines.
  556. exec_blas_async ... immediately returns after jobs are queued.
  557. exec_blas ... returns after jobs are finished.
  558. */
  559. static BLASULONG exec_queue_lock = 0;
  560. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  561. #ifdef SMP_SERVER
  562. // Handle lazy re-init of the thread-pool after a POSIX fork
  563. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  564. #endif
  565. BLASLONG i = 0;
  566. blas_queue_t *current = queue;
  567. blas_queue_t *tsiq,*tspq;
  568. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  569. int node = get_node();
  570. int nodes = get_num_nodes();
  571. #endif
  572. #ifdef SMP_DEBUG
  573. int exec_count = 0;
  574. fprintf(STDERR, "Exec_blas_async is called. Position = %d\n", pos);
  575. #endif
  576. blas_lock(&exec_queue_lock);
  577. while (queue) {
  578. queue -> position = pos;
  579. #ifdef CONSISTENT_FPCSR
  580. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue -> x87_mode));
  581. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue -> sse_mode));
  582. #endif
  583. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  584. /* Node Mapping Mode */
  585. if (queue -> mode & BLAS_NODE) {
  586. do {
  587. while((thread_status[i].node != node || atomic_load_queue(&thread_status[i].queue)) && (i < blas_num_threads - 1)) i ++;
  588. if (i < blas_num_threads - 1) break;
  589. i ++;
  590. if (i >= blas_num_threads - 1) {
  591. i = 0;
  592. node ++;
  593. if (node >= nodes) node = 0;
  594. }
  595. } while (1);
  596. } else {
  597. tsiq = atomic_load_queue(&thread_status[i].queue);
  598. while(tsiq) {
  599. i ++;
  600. if (i >= blas_num_threads - 1) i = 0;
  601. tsiq = atomic_load_queue(&thread_status[i].queue);
  602. }
  603. }
  604. #else
  605. tsiq = atomic_load_queue(&thread_status[i].queue);
  606. while(tsiq) {
  607. i ++;
  608. if (i >= blas_num_threads - 1) i = 0;
  609. tsiq = atomic_load_queue(&thread_status[i].queue);
  610. }
  611. #endif
  612. queue -> assigned = i;
  613. MB;
  614. atomic_store_queue(&thread_status[i].queue, queue);
  615. queue = queue -> next;
  616. pos ++;
  617. #ifdef SMP_DEBUG
  618. exec_count ++;
  619. #endif
  620. }
  621. blas_unlock(&exec_queue_lock);
  622. #ifdef SMP_DEBUG
  623. fprintf(STDERR, "Done(Number of threads = %2ld).\n", exec_count);
  624. #endif
  625. while (current) {
  626. pos = current -> assigned;
  627. tspq = atomic_load_queue(&thread_status[pos].queue);
  628. if ((BLASULONG)tspq > 1) {
  629. pthread_mutex_lock (&thread_status[pos].lock);
  630. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  631. #ifdef MONITOR
  632. num_suspend ++;
  633. #endif
  634. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  635. thread_status[pos].status = THREAD_STATUS_WAKEUP;
  636. pthread_cond_signal(&thread_status[pos].wakeup);
  637. }
  638. }
  639. pthread_mutex_unlock(&thread_status[pos].lock);
  640. }
  641. current = current -> next;
  642. }
  643. return 0;
  644. }
  645. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  646. blas_queue_t * tsqq;
  647. while ((num > 0) && queue) {
  648. tsqq = atomic_load_queue(&thread_status[queue->assigned].queue);
  649. while(tsqq) {
  650. YIELDING;
  651. tsqq = atomic_load_queue(&thread_status[queue->assigned].queue);
  652. };
  653. queue = queue -> next;
  654. num --;
  655. }
  656. MB;
  657. #ifdef SMP_DEBUG
  658. fprintf(STDERR, "Done.\n\n");
  659. #endif
  660. return 0;
  661. }
  662. /* Execute Threads */
  663. int exec_blas(BLASLONG num, blas_queue_t *queue){
  664. #ifdef SMP_SERVER
  665. // Handle lazy re-init of the thread-pool after a POSIX fork
  666. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  667. #endif
  668. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  669. #ifdef TIMING_DEBUG
  670. BLASULONG start, stop;
  671. #endif
  672. if ((num <= 0) || (queue == NULL)) return 0;
  673. #ifdef SMP_DEBUG
  674. fprintf(STDERR, "Exec_blas is called. Number of executing threads : %ld\n", num);
  675. #endif
  676. #ifdef __ELF__
  677. if (omp_in_parallel && (num > 1)) {
  678. if (omp_in_parallel() > 0) {
  679. fprintf(stderr,
  680. "OpenBLAS Warning : Detect OpenMP Loop and this application may hang. "
  681. "Please rebuild the library with USE_OPENMP=1 option.\n");
  682. }
  683. }
  684. #endif
  685. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  686. #ifdef TIMING_DEBUG
  687. start = rpcc();
  688. fprintf(STDERR, "\n");
  689. #endif
  690. routine = queue -> routine;
  691. if (queue -> mode & BLAS_LEGACY) {
  692. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  693. } else
  694. if (queue -> mode & BLAS_PTHREAD) {
  695. void (*pthreadcompat)(void *) = queue -> routine;
  696. (pthreadcompat)(queue -> args);
  697. } else
  698. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  699. queue -> sa, queue -> sb, 0);
  700. #ifdef TIMING_DEBUG
  701. stop = rpcc();
  702. #endif
  703. if ((num > 1) && queue -> next) {
  704. exec_blas_async_wait(num - 1, queue -> next);
  705. // arm: make sure results from other threads are visible
  706. MB;
  707. }
  708. #ifdef TIMING_DEBUG
  709. fprintf(STDERR, "Thread[0] : %16lu %16lu (%8lu cycles)\n",
  710. start, stop,
  711. stop - start);
  712. #endif
  713. return 0;
  714. }
  715. void goto_set_num_threads(int num_threads) {
  716. long i;
  717. #ifdef SMP_SERVER
  718. // Handle lazy re-init of the thread-pool after a POSIX fork
  719. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  720. #endif
  721. if (num_threads < 1) num_threads = blas_num_threads;
  722. #ifndef NO_AFFINITY
  723. if (num_threads == 1) {
  724. if (blas_cpu_number == 1){
  725. //OpenBLAS is already single thread.
  726. return;
  727. }else{
  728. //From multi-threads to single thread
  729. //Restore the original affinity mask
  730. gotoblas_set_affinity(-1);
  731. }
  732. }
  733. #endif
  734. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  735. if (num_threads > blas_num_threads) {
  736. LOCK_COMMAND(&server_lock);
  737. increased_threads = 1;
  738. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  739. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)0);
  740. thread_status[i].status = THREAD_STATUS_WAKEUP;
  741. pthread_mutex_init(&thread_status[i].lock, NULL);
  742. pthread_cond_init (&thread_status[i].wakeup, NULL);
  743. #ifdef NEED_STACKATTR
  744. pthread_create(&blas_threads[i], &attr,
  745. &blas_thread_server, (void *)i);
  746. #else
  747. pthread_create(&blas_threads[i], NULL,
  748. &blas_thread_server, (void *)i);
  749. #endif
  750. }
  751. blas_num_threads = num_threads;
  752. UNLOCK_COMMAND(&server_lock);
  753. }
  754. #ifndef NO_AFFINITY
  755. if(blas_cpu_number == 1 && num_threads > 1){
  756. //Restore the thread 0 affinity.
  757. gotoblas_set_affinity(0);
  758. }
  759. #endif
  760. blas_cpu_number = num_threads;
  761. #if defined(ARCH_MIPS64)
  762. //set parameters for different number of threads.
  763. blas_set_parameter();
  764. #endif
  765. }
  766. void openblas_set_num_threads(int num_threads) {
  767. goto_set_num_threads(num_threads);
  768. }
  769. /* Compatible function with pthread_create / join */
  770. int gotoblas_pthread(int numthreads, void *function, void *args, int stride) {
  771. blas_queue_t queue[MAX_CPU_NUMBER];
  772. int i;
  773. if (numthreads <= 0) return 0;
  774. #ifdef SMP
  775. if (blas_cpu_number == 0) blas_get_cpu_number();
  776. #ifdef SMP_SERVER
  777. if (blas_server_avail == 0) blas_thread_init();
  778. #endif
  779. #endif
  780. for (i = 0; i < numthreads; i ++) {
  781. queue[i].mode = BLAS_PTHREAD;
  782. queue[i].routine = function;
  783. queue[i].args = args;
  784. queue[i].range_m = NULL;
  785. queue[i].range_n = NULL;
  786. queue[i].sa = args;
  787. queue[i].sb = args;
  788. queue[i].next = &queue[i + 1];
  789. args += stride;
  790. }
  791. queue[numthreads - 1].next = NULL;
  792. exec_blas(numthreads, queue);
  793. return 0;
  794. }
  795. /* Shutdown procedure, but user don't have to call this routine. The */
  796. /* kernel automatically kill threads. */
  797. int BLASFUNC(blas_thread_shutdown)(void){
  798. int i;
  799. if (!blas_server_avail) return 0;
  800. LOCK_COMMAND(&server_lock);
  801. for (i = 0; i < blas_num_threads - 1; i++) {
  802. pthread_mutex_lock (&thread_status[i].lock);
  803. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)-1);
  804. thread_status[i].status = THREAD_STATUS_WAKEUP;
  805. pthread_cond_signal (&thread_status[i].wakeup);
  806. pthread_mutex_unlock(&thread_status[i].lock);
  807. }
  808. for(i = 0; i < blas_num_threads - 1; i++){
  809. pthread_join(blas_threads[i], NULL);
  810. }
  811. for(i = 0; i < blas_num_threads - 1; i++){
  812. pthread_mutex_destroy(&thread_status[i].lock);
  813. pthread_cond_destroy (&thread_status[i].wakeup);
  814. }
  815. #ifdef NEED_STACKATTR
  816. pthread_attr_destory(&attr);
  817. #endif
  818. blas_server_avail = 0;
  819. UNLOCK_COMMAND(&server_lock);
  820. return 0;
  821. }
  822. #endif