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

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