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

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