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

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