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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  389. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  390. #endif
  391. #ifdef MONITOR
  392. main_status[cpu] = MAIN_RUNNING1;
  393. #endif
  394. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  395. if (sb == NULL) {
  396. if (!(queue -> mode & BLAS_COMPLEX)){
  397. #ifdef EXPRECISION
  398. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  399. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  400. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  401. } else
  402. #endif
  403. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE) {
  404. #ifdef BUILD_DOUBLE
  405. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  406. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  407. #endif
  408. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  409. #ifdef BUILD_SINGLE
  410. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  411. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  412. #endif
  413. } else {
  414. /* Other types in future */
  415. }
  416. } else {
  417. #ifdef EXPRECISION
  418. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  419. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  420. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  421. } else
  422. #endif
  423. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  424. #ifdef BUILD_COMPLEX16
  425. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  426. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  427. #endif
  428. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  429. #ifdef BUILD_COMPLEX
  430. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  431. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  432. #endif
  433. } else {
  434. /* Other types in future */
  435. }
  436. }
  437. queue->sb=sb;
  438. }
  439. #ifdef MONITOR
  440. main_status[cpu] = MAIN_RUNNING2;
  441. #endif
  442. if (queue -> mode & BLAS_LEGACY) {
  443. legacy_exec(routine, queue -> mode, queue -> args, sb);
  444. } else
  445. if (queue -> mode & BLAS_PTHREAD) {
  446. void (*pthreadcompat)(void *) = (void(*)(void*))queue -> routine;
  447. (pthreadcompat)(queue -> args);
  448. } else
  449. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  450. #ifdef SMP_DEBUG
  451. fprintf(STDERR, "Server[%2ld] Calculation finished!\n", cpu);
  452. #endif
  453. #ifdef MONITOR
  454. main_status[cpu] = MAIN_FINISH;
  455. #endif
  456. // arm: make sure all results are written out _before_
  457. // thread is marked as done and other threads use them
  458. MB;
  459. atomic_store_queue(&thread_status[cpu].queue, (blas_queue_t *)0);
  460. }
  461. #ifdef MONITOR
  462. main_status[cpu] = MAIN_DONE;
  463. #endif
  464. #ifdef TIMING_DEBUG
  465. stop = rpcc();
  466. fprintf(STDERR, "Thread[%ld] : %16lu %16lu (%8lu cycles)\n", cpu + 1,
  467. start, stop,
  468. stop - start);
  469. #endif
  470. }
  471. /* Shutdown procedure */
  472. #ifdef SMP_DEBUG
  473. fprintf(STDERR, "Server[%2ld] Shutdown!\n", cpu);
  474. #endif
  475. blas_memory_free(buffer);
  476. //pthread_exit(NULL);
  477. return NULL;
  478. }
  479. #ifdef MONITOR
  480. static BLASLONG num_suspend = 0;
  481. static int blas_monitor(void *arg){
  482. int i;
  483. while(1){
  484. for (i = 0; i < blas_num_threads - 1; i++){
  485. switch (main_status[i]) {
  486. case MAIN_ENTER :
  487. fprintf(STDERR, "THREAD[%2d] : Entering.\n", i);
  488. break;
  489. case MAIN_EXIT :
  490. fprintf(STDERR, "THREAD[%2d] : Exiting.\n", i);
  491. break;
  492. case MAIN_TRYLOCK :
  493. fprintf(STDERR, "THREAD[%2d] : Trying lock operation.\n", i);
  494. break;
  495. case MAIN_QUEUING :
  496. fprintf(STDERR, "THREAD[%2d] : Queuing.\n", i);
  497. break;
  498. case MAIN_RECEIVING :
  499. fprintf(STDERR, "THREAD[%2d] : Receiving.\n", i);
  500. break;
  501. case MAIN_RUNNING1 :
  502. fprintf(STDERR, "THREAD[%2d] : Running1.\n", i);
  503. break;
  504. case MAIN_RUNNING2 :
  505. fprintf(STDERR, "THREAD[%2d] : Running2.\n", i);
  506. break;
  507. case MAIN_RUNNING3 :
  508. fprintf(STDERR, "THREAD[%2d] : Running3.\n", i);
  509. break;
  510. case MAIN_WAITING :
  511. fprintf(STDERR, "THREAD[%2d] : Waiting.\n", i);
  512. break;
  513. case MAIN_SLEEPING :
  514. fprintf(STDERR, "THREAD[%2d] : Sleeping.\n", i);
  515. break;
  516. case MAIN_FINISH :
  517. fprintf(STDERR, "THREAD[%2d] : Finishing.\n", i);
  518. break;
  519. case MAIN_DONE :
  520. fprintf(STDERR, "THREAD[%2d] : Job is done.\n", i);
  521. break;
  522. }
  523. fprintf(stderr, "Total number of suspended ... %ld\n", num_suspend);
  524. }
  525. sleep(1);
  526. }
  527. return 0;
  528. }
  529. #endif
  530. /* Initializing routine */
  531. int blas_thread_init(void){
  532. BLASLONG i;
  533. int ret;
  534. int thread_timeout_env;
  535. #ifdef NEED_STACKATTR
  536. pthread_attr_t attr;
  537. #endif
  538. if (blas_server_avail) return 0;
  539. #ifdef NEED_STACKATTR
  540. pthread_attr_init(&attr);
  541. pthread_attr_setguardsize(&attr, 0x1000U);
  542. pthread_attr_setstacksize( &attr, 0x1000U);
  543. #endif
  544. LOCK_COMMAND(&server_lock);
  545. if (!blas_server_avail){
  546. thread_timeout_env=openblas_thread_timeout();
  547. if (thread_timeout_env>0) {
  548. if (thread_timeout_env < 4) thread_timeout_env = 4;
  549. if (thread_timeout_env > 30) thread_timeout_env = 30;
  550. thread_timeout = (1 << thread_timeout_env);
  551. }
  552. for(i = 0; i < blas_num_threads - 1; i++){
  553. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)0);
  554. thread_status[i].status = THREAD_STATUS_WAKEUP;
  555. pthread_mutex_init(&thread_status[i].lock, NULL);
  556. pthread_cond_init (&thread_status[i].wakeup, NULL);
  557. #ifdef NEED_STACKATTR
  558. ret=pthread_create(&blas_threads[i], &attr,
  559. &blas_thread_server, (void *)i);
  560. #else
  561. ret=pthread_create(&blas_threads[i], NULL,
  562. &blas_thread_server, (void *)i);
  563. #endif
  564. if(ret!=0){
  565. struct rlimit rlim;
  566. const char *msg = strerror(ret);
  567. fprintf(STDERR, "OpenBLAS blas_thread_init: pthread_create failed for thread %ld of %d: %s\n", i+1,blas_num_threads,msg);
  568. #ifdef RLIMIT_NPROC
  569. if(0 == getrlimit(RLIMIT_NPROC, &rlim)) {
  570. fprintf(STDERR, "OpenBLAS blas_thread_init: RLIMIT_NPROC "
  571. "%ld current, %ld max\n", (long)(rlim.rlim_cur), (long)(rlim.rlim_max));
  572. }
  573. #endif
  574. if(0 != raise(SIGINT)) {
  575. fprintf(STDERR, "OpenBLAS blas_thread_init: calling exit(3)\n");
  576. exit(EXIT_FAILURE);
  577. }
  578. }
  579. }
  580. #ifdef MONITOR
  581. pthread_create(&monitor_thread, NULL,
  582. (void *)&blas_monitor, (void *)NULL);
  583. #endif
  584. blas_server_avail = 1;
  585. }
  586. UNLOCK_COMMAND(&server_lock);
  587. return 0;
  588. }
  589. /*
  590. User can call one of two routines.
  591. exec_blas_async ... immediately returns after jobs are queued.
  592. exec_blas ... returns after jobs are finished.
  593. */
  594. static BLASULONG exec_queue_lock = 0;
  595. int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
  596. #ifdef SMP_SERVER
  597. // Handle lazy re-init of the thread-pool after a POSIX fork
  598. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  599. #endif
  600. BLASLONG i = 0;
  601. blas_queue_t *current = queue;
  602. blas_queue_t *tsiq,*tspq;
  603. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  604. int node = get_node();
  605. int nodes = get_num_nodes();
  606. #endif
  607. #ifdef SMP_DEBUG
  608. int exec_count = 0;
  609. fprintf(STDERR, "Exec_blas_async is called. Position = %d\n", pos);
  610. #endif
  611. blas_lock(&exec_queue_lock);
  612. while (queue) {
  613. queue -> position = pos;
  614. #ifdef CONSISTENT_FPCSR
  615. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue -> x87_mode));
  616. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue -> sse_mode));
  617. #endif
  618. #if defined(OS_LINUX) && !defined(NO_AFFINITY) && !defined(PARAMTEST)
  619. /* Node Mapping Mode */
  620. if (queue -> mode & BLAS_NODE) {
  621. do {
  622. while((thread_status[i].node != node || atomic_load_queue(&thread_status[i].queue)) && (i < blas_num_threads - 1)) i ++;
  623. if (i < blas_num_threads - 1) break;
  624. i ++;
  625. if (i >= blas_num_threads - 1) {
  626. i = 0;
  627. node ++;
  628. if (node >= nodes) node = 0;
  629. }
  630. } while (1);
  631. } else {
  632. tsiq = atomic_load_queue(&thread_status[i].queue);
  633. while(tsiq) {
  634. i ++;
  635. if (i >= blas_num_threads - 1) i = 0;
  636. tsiq = atomic_load_queue(&thread_status[i].queue);
  637. }
  638. }
  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. #endif
  647. queue -> assigned = i;
  648. MB;
  649. atomic_store_queue(&thread_status[i].queue, queue);
  650. queue = queue -> next;
  651. pos ++;
  652. #ifdef SMP_DEBUG
  653. exec_count ++;
  654. #endif
  655. }
  656. blas_unlock(&exec_queue_lock);
  657. #ifdef SMP_DEBUG
  658. fprintf(STDERR, "Done(Number of threads = %2ld).\n", exec_count);
  659. #endif
  660. while (current) {
  661. pos = current -> assigned;
  662. tspq = atomic_load_queue(&thread_status[pos].queue);
  663. if ((BLASULONG)tspq > 1) {
  664. pthread_mutex_lock (&thread_status[pos].lock);
  665. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  666. #ifdef MONITOR
  667. num_suspend ++;
  668. #endif
  669. if (thread_status[pos].status == THREAD_STATUS_SLEEP) {
  670. thread_status[pos].status = THREAD_STATUS_WAKEUP;
  671. pthread_cond_signal(&thread_status[pos].wakeup);
  672. }
  673. }
  674. pthread_mutex_unlock(&thread_status[pos].lock);
  675. }
  676. current = current -> next;
  677. }
  678. return 0;
  679. }
  680. int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
  681. blas_queue_t * tsqq;
  682. while ((num > 0) && queue) {
  683. tsqq = atomic_load_queue(&thread_status[queue->assigned].queue);
  684. while(tsqq) {
  685. YIELDING;
  686. tsqq = atomic_load_queue(&thread_status[queue->assigned].queue);
  687. };
  688. queue = queue -> next;
  689. num --;
  690. }
  691. MB;
  692. #ifdef SMP_DEBUG
  693. fprintf(STDERR, "Done.\n\n");
  694. #endif
  695. return 0;
  696. }
  697. /* Execute Threads */
  698. int exec_blas(BLASLONG num, blas_queue_t *queue){
  699. #ifdef SMP_SERVER
  700. // Handle lazy re-init of the thread-pool after a POSIX fork
  701. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  702. #endif
  703. int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
  704. #ifdef TIMING_DEBUG
  705. BLASULONG start, stop;
  706. #endif
  707. if ((num <= 0) || (queue == NULL)) return 0;
  708. #ifdef SMP_DEBUG
  709. fprintf(STDERR, "Exec_blas is called. Number of executing threads : %ld\n", num);
  710. #endif
  711. #ifdef __ELF__
  712. if (omp_in_parallel && (num > 1)) {
  713. if (omp_in_parallel() > 0) {
  714. fprintf(stderr,
  715. "OpenBLAS Warning : Detect OpenMP Loop and this application may hang. "
  716. "Please rebuild the library with USE_OPENMP=1 option.\n");
  717. }
  718. }
  719. #endif
  720. if ((num > 1) && queue -> next) exec_blas_async(1, queue -> next);
  721. #ifdef TIMING_DEBUG
  722. start = rpcc();
  723. fprintf(STDERR, "\n");
  724. #endif
  725. routine = (int (*)(blas_arg_t *, void *, void *, double *, double *, BLASLONG))queue -> routine;
  726. if (queue -> mode & BLAS_LEGACY) {
  727. legacy_exec(routine, queue -> mode, queue -> args, queue -> sb);
  728. } else
  729. if (queue -> mode & BLAS_PTHREAD) {
  730. void (*pthreadcompat)(void *) = (void (*)(void*))queue -> routine;
  731. (pthreadcompat)(queue -> args);
  732. } else
  733. (routine)(queue -> args, queue -> range_m, queue -> range_n,
  734. queue -> sa, queue -> sb, 0);
  735. #ifdef TIMING_DEBUG
  736. stop = rpcc();
  737. #endif
  738. if ((num > 1) && queue -> next) {
  739. exec_blas_async_wait(num - 1, queue -> next);
  740. // arm: make sure results from other threads are visible
  741. MB;
  742. }
  743. #ifdef TIMING_DEBUG
  744. fprintf(STDERR, "Thread[0] : %16lu %16lu (%8lu cycles)\n",
  745. start, stop,
  746. stop - start);
  747. #endif
  748. return 0;
  749. }
  750. void goto_set_num_threads(int num_threads) {
  751. long i;
  752. #ifdef SMP_SERVER
  753. // Handle lazy re-init of the thread-pool after a POSIX fork
  754. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  755. #endif
  756. if (num_threads < 1) num_threads = blas_num_threads;
  757. #ifndef NO_AFFINITY
  758. if (num_threads == 1) {
  759. if (blas_cpu_number == 1){
  760. //OpenBLAS is already single thread.
  761. return;
  762. }else{
  763. //From multi-threads to single thread
  764. //Restore the original affinity mask
  765. gotoblas_set_affinity(-1);
  766. }
  767. }
  768. #endif
  769. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  770. if (num_threads > blas_num_threads) {
  771. LOCK_COMMAND(&server_lock);
  772. increased_threads = 1;
  773. for(i = blas_num_threads - 1; i < num_threads - 1; i++){
  774. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)0);
  775. thread_status[i].status = THREAD_STATUS_WAKEUP;
  776. pthread_mutex_init(&thread_status[i].lock, NULL);
  777. pthread_cond_init (&thread_status[i].wakeup, NULL);
  778. #ifdef NEED_STACKATTR
  779. pthread_create(&blas_threads[i], &attr,
  780. &blas_thread_server, (void *)i);
  781. #else
  782. pthread_create(&blas_threads[i], NULL,
  783. &blas_thread_server, (void *)i);
  784. #endif
  785. }
  786. blas_num_threads = num_threads;
  787. UNLOCK_COMMAND(&server_lock);
  788. }
  789. #ifndef NO_AFFINITY
  790. if(blas_cpu_number == 1 && num_threads > 1){
  791. //Restore the thread 0 affinity.
  792. gotoblas_set_affinity(0);
  793. }
  794. #endif
  795. blas_cpu_number = num_threads;
  796. #if defined(ARCH_MIPS64)
  797. #ifndef DYNAMIC_ARCH
  798. //set parameters for different number of threads.
  799. blas_set_parameter();
  800. #endif
  801. #endif
  802. }
  803. void openblas_set_num_threads(int num_threads) {
  804. goto_set_num_threads(num_threads);
  805. }
  806. /* Compatible function with pthread_create / join */
  807. int gotoblas_pthread(int numthreads, void *function, void *args, int stride) {
  808. blas_queue_t queue[MAX_CPU_NUMBER];
  809. int i;
  810. if (numthreads <= 0) return 0;
  811. #ifdef SMP
  812. if (blas_cpu_number == 0) blas_get_cpu_number();
  813. #ifdef SMP_SERVER
  814. if (blas_server_avail == 0) blas_thread_init();
  815. #endif
  816. #endif
  817. for (i = 0; i < numthreads; i ++) {
  818. queue[i].mode = BLAS_PTHREAD;
  819. queue[i].routine = function;
  820. queue[i].args = args;
  821. queue[i].range_m = NULL;
  822. queue[i].range_n = NULL;
  823. queue[i].sa = args;
  824. queue[i].sb = args;
  825. queue[i].next = &queue[i + 1];
  826. args += stride;
  827. }
  828. queue[numthreads - 1].next = NULL;
  829. exec_blas(numthreads, queue);
  830. return 0;
  831. }
  832. /* Shutdown procedure, but user don't have to call this routine. The */
  833. /* kernel automatically kill threads. */
  834. int BLASFUNC(blas_thread_shutdown)(void){
  835. int i;
  836. LOCK_COMMAND(&server_lock);
  837. if (blas_server_avail) {
  838. for (i = 0; i < blas_num_threads - 1; i++) {
  839. pthread_mutex_lock (&thread_status[i].lock);
  840. atomic_store_queue(&thread_status[i].queue, (blas_queue_t *)-1);
  841. thread_status[i].status = THREAD_STATUS_WAKEUP;
  842. pthread_cond_signal (&thread_status[i].wakeup);
  843. pthread_mutex_unlock(&thread_status[i].lock);
  844. }
  845. for(i = 0; i < blas_num_threads - 1; i++){
  846. pthread_join(blas_threads[i], NULL);
  847. }
  848. for(i = 0; i < blas_num_threads - 1; i++){
  849. pthread_mutex_destroy(&thread_status[i].lock);
  850. pthread_cond_destroy (&thread_status[i].wakeup);
  851. }
  852. #ifdef NEED_STACKATTR
  853. pthread_attr_destroy(&attr);
  854. #endif
  855. blas_server_avail = 0;
  856. }
  857. UNLOCK_COMMAND(&server_lock);
  858. return 0;
  859. }
  860. #endif