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.

common.h 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #ifndef COMMON_H
  39. #define COMMON_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. /* Assume C declarations for C++ */
  43. #endif /* __cplusplus */
  44. #ifndef _GNU_SOURCE
  45. #define _GNU_SOURCE
  46. #endif
  47. #ifndef __USE_XOPEN
  48. #define __USE_XOPEN
  49. #endif
  50. #ifndef __USE_SVID
  51. #define __USE_SVID
  52. #endif
  53. #ifdef BUILD_KERNEL
  54. #include "config_kernel.h"
  55. #else
  56. #include "config.h"
  57. #endif
  58. #undef ENABLE_SSE_EXCEPTION
  59. #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
  60. #define SMP
  61. #endif
  62. #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_Interix)
  63. #define WINDOWS_ABI
  64. #define OS_WINDOWS
  65. #ifdef DOUBLE
  66. #define DOUBLE_DEFINED DOUBLE
  67. #undef DOUBLE
  68. #endif
  69. #endif
  70. #if !defined(NOINCLUDE) && !defined(ASSEMBLER)
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <unistd.h>
  75. #ifdef OS_LINUX
  76. #include <malloc.h>
  77. #include <sched.h>
  78. #endif
  79. #ifdef OS_WINDOWS
  80. #ifdef ATOM
  81. #define GOTO_ATOM ATOM
  82. #undef ATOM
  83. #endif
  84. #include <windows.h>
  85. #include <math.h>
  86. #ifdef GOTO_ATOM
  87. #define ATOM GOTO_ATOM
  88. #undef GOTO_ATOM
  89. #endif
  90. #else
  91. #include <sys/mman.h>
  92. #include <sys/shm.h>
  93. #include <sys/time.h>
  94. #include <unistd.h>
  95. #include <math.h>
  96. #ifdef SMP
  97. #include <pthread.h>
  98. #endif
  99. #endif
  100. #if defined(OS_SUNOS)
  101. #include <thread.h>
  102. #endif
  103. #ifdef __DECC
  104. #include <c_asm.h>
  105. #include <machine/builtins.h>
  106. #endif
  107. #if defined(ARCH_IA64) && defined(ENABLE_SSE_EXCEPTION)
  108. #include <fenv.h>
  109. #endif
  110. #endif
  111. #if defined(OS_WINDOWS) && defined(DOUBLE_DEFINED)
  112. #define DOUBLE DOUBLE_DEFINED
  113. #undef DOUBLE_DEFINED
  114. #endif
  115. #undef DEBUG_INFO
  116. #define SMP_DEBUG
  117. #undef MALLOC_DEBUG
  118. #undef SMP_ALLOC_DEBUG
  119. #ifndef ZERO
  120. #ifdef XDOUBLE
  121. #define ZERO 0.e0L
  122. #elif defined DOUBLE
  123. #define ZERO 0.e0
  124. #else
  125. #define ZERO 0.e0f
  126. #endif
  127. #endif
  128. #ifndef ONE
  129. #ifdef XDOUBLE
  130. #define ONE 1.e0L
  131. #elif defined DOUBLE
  132. #define ONE 1.e0
  133. #else
  134. #define ONE 1.e0f
  135. #endif
  136. #endif
  137. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  138. #define ALLOCA_ALIGN 63UL
  139. #define NUM_BUFFERS (MAX_CPU_NUMBER * 2)
  140. #ifdef NEEDBUNDERSCORE
  141. #define BLASFUNC(FUNC) FUNC##_
  142. #else
  143. #define BLASFUNC(FUNC) FUNC
  144. #endif
  145. #undef USE_PTHREAD_LOCK
  146. #undef USE_PTHREAD_SPINLOCK
  147. #if defined(USE_PTHREAD_LOCK) && defined(USE_PTHREAD_SPINLOCK)
  148. #error "You can't specify both LOCK operation!"
  149. #endif
  150. #ifdef SMP
  151. #define USE_PTHREAD_LOCK
  152. #undef USE_PTHREAD_SPINLOCK
  153. #endif
  154. #ifdef OS_WINDOWS
  155. #undef USE_PTHREAD_LOCK
  156. #undef USE_PTHREAD_SPINLOCK
  157. #endif
  158. #if defined(USE_PTHREAD_LOCK)
  159. #define LOCK_COMMAND(x) pthread_mutex_lock(x)
  160. #define UNLOCK_COMMAND(x) pthread_mutex_unlock(x)
  161. #elif defined(USE_PTHREAD_SPINLOCK)
  162. #ifndef ASSEMBLER
  163. typedef volatile int pthread_spinlock_t;
  164. int pthread_spin_lock (pthread_spinlock_t *__lock);
  165. int pthread_spin_unlock (pthread_spinlock_t *__lock);
  166. #endif
  167. #define LOCK_COMMAND(x) pthread_spin_lock(x)
  168. #define UNLOCK_COMMAND(x) pthread_spin_unlock(x)
  169. #else
  170. #define LOCK_COMMAND(x) blas_lock(x)
  171. #define UNLOCK_COMMAND(x) blas_unlock(x)
  172. #endif
  173. #define GOTO_SHMID 0x510510
  174. #if 0
  175. #ifndef __CUDACC__
  176. #define __global__
  177. #define __device__
  178. #define __host__
  179. #define __shared__
  180. #endif
  181. #endif
  182. #ifndef ASSEMBLER
  183. #ifdef QUAD_PRECISION
  184. typedef struct {
  185. unsigned long x[2];
  186. } xdouble;
  187. #elif defined EXPRECISION
  188. #define xdouble long double
  189. #else
  190. #define xdouble double
  191. #endif
  192. #if defined(OS_WINDOWS) && defined(__64BIT__)
  193. typedef long long BLASLONG;
  194. typedef unsigned long long BLASULONG;
  195. #else
  196. typedef long BLASLONG;
  197. typedef unsigned long BLASULONG;
  198. #endif
  199. #ifdef USE64BITINT
  200. typedef BLASLONG blasint;
  201. #else
  202. typedef int blasint;
  203. #endif
  204. #else
  205. #ifdef USE64BITINT
  206. #define INTSHIFT 3
  207. #define INTSIZE 8
  208. #else
  209. #define INTSHIFT 2
  210. #define INTSIZE 4
  211. #endif
  212. #endif
  213. #ifdef XDOUBLE
  214. #define FLOAT xdouble
  215. #ifdef QUAD_PRECISION
  216. #define XFLOAT xidouble
  217. #endif
  218. #ifdef QUAD_PRECISION
  219. #define SIZE 32
  220. #define BASE_SHIFT 5
  221. #define ZBASE_SHIFT 6
  222. #else
  223. #define SIZE 16
  224. #define BASE_SHIFT 4
  225. #define ZBASE_SHIFT 5
  226. #endif
  227. #elif defined(DOUBLE)
  228. #define FLOAT double
  229. #define SIZE 8
  230. #define BASE_SHIFT 3
  231. #define ZBASE_SHIFT 4
  232. #else
  233. #define FLOAT float
  234. #define SIZE 4
  235. #define BASE_SHIFT 2
  236. #define ZBASE_SHIFT 3
  237. #endif
  238. #ifndef XFLOAT
  239. #define XFLOAT FLOAT
  240. #endif
  241. #ifndef COMPLEX
  242. #define COMPSIZE 1
  243. #else
  244. #define COMPSIZE 2
  245. #endif
  246. #if defined(C_PGI) || defined(C_SUN)
  247. #define CREAL(X) (*((FLOAT *)&X + 0))
  248. #define CIMAG(X) (*((FLOAT *)&X + 1))
  249. #else
  250. #define CREAL __real__
  251. #define CIMAG __imag__
  252. #endif
  253. #define Address_H(x) (((x)+(1<<15))>>16)
  254. #define Address_L(x) ((x)-((Address_H(x))<<16))
  255. #ifndef MAX_CPU_NUMBER
  256. #define MAX_CPU_NUMBER 2
  257. #endif
  258. #if defined(OS_SUNOS)
  259. #define YIELDING thr_yield()
  260. #endif
  261. #if defined(OS_WINDOWS)
  262. #define YIELDING SwitchToThread()
  263. #endif
  264. #ifndef YIELDING
  265. #define YIELDING sched_yield()
  266. #endif
  267. #ifdef QUAD_PRECISION
  268. #include "common_quad.h"
  269. #endif
  270. #ifdef ARCH_ALPHA
  271. #include "common_alpha.h"
  272. #endif
  273. #ifdef ARCH_X86
  274. #include "common_x86.h"
  275. #endif
  276. #ifdef ARCH_X86_64
  277. #include "common_x86_64.h"
  278. #endif
  279. #ifdef ARCH_IA64
  280. #include "common_ia64.h"
  281. #endif
  282. #ifdef ARCH_POWER
  283. #include "common_power.h"
  284. #endif
  285. #ifdef sparc
  286. #include "common_sparc.h"
  287. #endif
  288. #ifdef ARCH_MIPS64
  289. #include "common_mips64.h"
  290. #endif
  291. #ifdef OS_LINUX
  292. #include "common_linux.h"
  293. #endif
  294. #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
  295. #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
  296. #include "param.h"
  297. #include "common_param.h"
  298. #ifndef STDERR
  299. #define STDERR stderr
  300. #endif
  301. #ifndef MASK
  302. #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
  303. #endif
  304. #if defined(XDOUBLE) || defined(DOUBLE)
  305. #define FLOATRET FLOAT
  306. #else
  307. #ifdef NEED_F2CCONV
  308. #define FLOATRET double
  309. #else
  310. #define FLOATRET float
  311. #endif
  312. #endif
  313. #ifndef IFLUSH
  314. #define IFLUSH
  315. #endif
  316. #ifndef IFLUSH_HALF
  317. #define IFLUSH_HALF
  318. #endif
  319. #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
  320. #ifdef USE_OPENMP
  321. #undef USE_OPENMP
  322. #endif
  323. #endif
  324. #ifndef ASSEMBLER
  325. #ifndef MIN
  326. #define MIN(a,b) (a>b? b:a)
  327. #endif
  328. #ifndef MAX
  329. #define MAX(a,b) (a<b? b:a)
  330. #endif
  331. #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
  332. #if defined(__FreeBSD__) || defined(__APPLE__)
  333. #define MAP_ANONYMOUS MAP_ANON
  334. #endif
  335. /* Common Memory Management Routine */
  336. void blas_set_parameter(void);
  337. int blas_get_cpu_number(void);
  338. void *blas_memory_alloc (int);
  339. void blas_memory_free (void *);
  340. int get_num_procs (void);
  341. #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
  342. int get_num_nodes (void);
  343. int get_num_proc (int);
  344. int get_node_equal (void);
  345. #endif
  346. void goto_set_num_threads(int);
  347. void gotoblas_affinity_init(void);
  348. void gotoblas_affinity_quit(void);
  349. void gotoblas_dynamic_init(void);
  350. void gotoblas_dynamic_quit(void);
  351. void gotoblas_profile_init(void);
  352. void gotoblas_profile_quit(void);
  353. #ifdef USE_OPENMP
  354. int omp_in_parallel(void);
  355. int omp_get_num_procs(void);
  356. #else
  357. #ifdef __ELF__
  358. int omp_in_parallel (void) __attribute__ ((weak));
  359. int omp_get_num_procs(void) __attribute__ ((weak));
  360. #endif
  361. #endif
  362. static __inline void blas_unlock(volatile BLASULONG *address){
  363. MB;
  364. *address = 0;
  365. }
  366. static __inline int readenv(char *env) {
  367. char *p;
  368. p = getenv(env);
  369. if (p == NULL) return 0; else return atoi(p);
  370. }
  371. #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
  372. static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
  373. #ifndef UNIT
  374. FLOAT ratio, den;
  375. if (
  376. #ifdef XDOUBLE
  377. (fabsl(ar)) >= (fabsl(ai))
  378. #elif defined DOUBLE
  379. (fabs (ar)) >= (fabs (ai))
  380. #else
  381. (fabsf(ar)) >= (fabsf(ai))
  382. #endif
  383. ) {
  384. ratio = ai / ar;
  385. den = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
  386. ar = den;
  387. ai = -ratio * den;
  388. } else {
  389. ratio = ar / ai;
  390. den = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
  391. ar = ratio * den;
  392. ai = -den;
  393. }
  394. b[0] = ar;
  395. b[1] = ai;
  396. #else
  397. b[0] = ONE;
  398. b[1] = ZERO;
  399. #endif
  400. }
  401. #endif
  402. #ifdef MALLOC_DEBUG
  403. void *blas_debug_alloc(int);
  404. void *blas_debug_free(void *);
  405. #undef malloc
  406. #undef free
  407. #define malloc(a) blas_debug_alloc(a)
  408. #define free(a) blas_debug_free (a)
  409. #endif
  410. #ifndef COPYOVERHEAD
  411. #define GEMMRETTYPE int
  412. #else
  413. typedef struct {
  414. double outercopy;
  415. double innercopy;
  416. double kernel;
  417. double mflops;
  418. } copyoverhead_t;
  419. #define GEMMRETTYPE copyoverhead_t
  420. #endif
  421. #endif
  422. #ifndef BUILD_KERNEL
  423. #define KNAME(A, B) A
  424. #else
  425. #define KNAME(A, B) A##B
  426. #endif
  427. #include "common_interface.h"
  428. #ifdef SANITY_CHECK
  429. #include "common_reference.h"
  430. #endif
  431. #include "common_macro.h"
  432. #include "common_level1.h"
  433. #include "common_level2.h"
  434. #include "common_level3.h"
  435. #include "common_lapack.h"
  436. #ifdef CBLAS
  437. #include "cblas.h"
  438. #endif
  439. #ifndef ASSEMBLER
  440. #if 0
  441. #include "symcopy.h"
  442. #endif
  443. #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
  444. #error Both SMP_SERVER and SMP_ONDEMAND are specified.
  445. #endif
  446. #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
  447. #include "common_thread.h"
  448. #endif
  449. #endif
  450. #define INFO_NUM 99
  451. #ifndef DEFAULT_CPU_NUMBER
  452. #define DEFAULT_CPU_NUMBER 4
  453. #endif
  454. #ifndef IDEBUG_START
  455. #define IDEBUG_START
  456. #endif
  457. #ifndef IDEBUG_END
  458. #define IDEBUG_END
  459. #endif
  460. #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
  461. typedef struct {
  462. int func;
  463. unsigned long long calls, fops, area, cycles, tcycles;
  464. } func_profile_t;
  465. extern func_profile_t function_profile_table[];
  466. extern int gotoblas_profile;
  467. #ifdef XDOUBLE
  468. #define NUMOPT QNUMOPT
  469. #elif defined DOUBLE
  470. #define NUMOPT DNUMOPT
  471. #else
  472. #define NUMOPT SNUMOPT
  473. #endif
  474. #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
  475. #ifdef SMP
  476. #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
  477. if (gotoblas_profile) { \
  478. profile_end = rpcc(); \
  479. function_profile_table[PROFILE_FUNC_NAME].calls ++; \
  480. function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
  481. function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
  482. function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
  483. function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
  484. } \
  485. }
  486. #else
  487. #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
  488. if (gotoblas_profile) { \
  489. profile_end = rpcc(); \
  490. function_profile_table[PROFILE_FUNC_NAME].calls ++; \
  491. function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
  492. function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
  493. function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
  494. function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
  495. } \
  496. }
  497. #endif
  498. #else
  499. #define FUNCTION_PROFILE_START()
  500. #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
  501. #endif
  502. #if 1
  503. #define PRINT_DEBUG_CNAME
  504. #define PRINT_DEBUG_NAME
  505. #else
  506. #define PRINT_DEBUG_CNAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
  507. #define PRINT_DEBUG_NAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
  508. #endif
  509. #ifdef __cplusplus
  510. }
  511. #endif /* __cplusplus */
  512. #endif

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.