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

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

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