1 /*********************************************************************/
2 /* Copyright 2009, 2010 The University of Texas at Austin. */
3 /* All rights reserved. */
5 /* Redistribution and use in source and binary forms, with or */
6 /* without modification, are permitted provided that the following */
7 /* conditions are met: */
9 /* 1. Redistributions of source code must retain the above */
10 /* copyright notice, this list of conditions and the following */
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. */
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. */
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 /*********************************************************************/
44 /* Assume C declarations for C++ */
45 #endif /* __cplusplus */
60 #include "config_kernel.h"
65 #undef ENABLE_SSE_EXCEPTION
67 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
71 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
76 #define DOUBLE_DEFINED DOUBLE
81 #if !defined(NOINCLUDE) && !defined(ASSEMBLER)
86 #if !defined(_MSC_VER)
96 #if defined(OS_DARWIN) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_ANDROID)
102 //Android NDK only supports complex.h since Android 5.0
103 #if __ANDROID_API__ < 21
104 #define FORCE_OPENBLAS_COMPLEX_STRUCT
114 #define GOTO_ATOM ATOM
120 #define ATOM GOTO_ATOM
124 #include <sys/mman.h>
128 #include <sys/time.h>
137 #if defined(OS_SUNOS)
143 #include <machine/builtins.h>
146 #if defined(ARCH_IA64) && defined(ENABLE_SSE_EXCEPTION)
152 #if defined(OS_WINDOWS) && defined(DOUBLE_DEFINED)
153 #define DOUBLE DOUBLE_DEFINED
154 #undef DOUBLE_DEFINED
160 #undef SMP_ALLOC_DEBUG
182 #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
184 #define ALLOCA_ALIGN 63UL
186 #define NUM_BUFFERS (MAX_CPU_NUMBER * 2 * MAX_PARALLEL_NUMBER)
188 #ifdef NEEDBUNDERSCORE
189 #define BLASFUNC(FUNC) FUNC##_
191 #define BLASFUNC(FUNC) FUNC
194 #undef USE_PTHREAD_LOCK
195 #undef USE_PTHREAD_SPINLOCK
197 #if defined(USE_PTHREAD_LOCK) && defined(USE_PTHREAD_SPINLOCK)
198 #error "You can't specify both LOCK operation!"
202 #define USE_PTHREAD_LOCK
203 #undef USE_PTHREAD_SPINLOCK
207 #undef USE_PTHREAD_LOCK
208 #undef USE_PTHREAD_SPINLOCK
211 #if defined(USE_PTHREAD_LOCK)
212 #define LOCK_COMMAND(x) pthread_mutex_lock(x)
213 #define UNLOCK_COMMAND(x) pthread_mutex_unlock(x)
214 #elif defined(USE_PTHREAD_SPINLOCK)
216 typedef volatile int pthread_spinlock_t;
217 int pthread_spin_lock (pthread_spinlock_t *__lock);
218 int pthread_spin_unlock (pthread_spinlock_t *__lock);
220 #define LOCK_COMMAND(x) pthread_spin_lock(x)
221 #define UNLOCK_COMMAND(x) pthread_spin_unlock(x)
223 #define LOCK_COMMAND(x) blas_lock(x)
224 #define UNLOCK_COMMAND(x) blas_unlock(x)
227 #define GOTO_SHMID 0x510510
240 #ifdef QUAD_PRECISION
244 #elif defined EXPRECISION
245 #define xdouble long double
247 #define xdouble double
250 #if defined(OS_WINDOWS) && defined(__64BIT__)
251 typedef long long BLASLONG;
252 typedef unsigned long long BLASULONG;
254 typedef long BLASLONG;
255 typedef unsigned long BLASULONG;
259 typedef BLASLONG blasint;
260 #if defined(OS_WINDOWS) && defined(__64BIT__)
261 #define blasabs(x) llabs(x)
263 #define blasabs(x) labs(x)
267 #define blasabs(x) abs(x)
280 #define FLOAT xdouble
281 #ifdef QUAD_PRECISION
282 #define XFLOAT xidouble
284 #ifdef QUAD_PRECISION
287 #define ZBASE_SHIFT 6
291 #define ZBASE_SHIFT 5
293 #elif defined(DOUBLE)
297 #define ZBASE_SHIFT 4
302 #define ZBASE_SHIFT 3
316 #define Address_H(x) (((x)+(1<<15))>>16)
317 #define Address_L(x) ((x)-((Address_H(x))<<16))
319 #ifndef MAX_CPU_NUMBER
320 #define MAX_CPU_NUMBER 2
323 #if defined(OS_SUNOS)
324 #define YIELDING thr_yield()
327 #if defined(OS_WINDOWS)
328 #if defined(_MSC_VER) && !defined(__clang__)
329 #define YIELDING YieldProcessor()
331 #define YIELDING SwitchToThread()
335 #if defined(ARMV7) || defined(ARMV6) || defined(ARMV8) || defined(ARMV5)
336 #define YIELDING asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop; \n");
341 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
347 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
355 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
363 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
369 #define YIELDING sched_yield()
373 To alloc job_t on heap or statck.
374 please https://github.com/xianyi/OpenBLAS/issues/246
376 #if defined(OS_WINDOWS)
377 #define GETRF_MEM_ALLOC_THRESHOLD 32
378 #define BLAS3_MEM_ALLOC_THRESHOLD 32
381 #ifndef GETRF_MEM_ALLOC_THRESHOLD
382 #define GETRF_MEM_ALLOC_THRESHOLD 80
385 #ifndef BLAS3_MEM_ALLOC_THRESHOLD
386 #define BLAS3_MEM_ALLOC_THRESHOLD 160
389 #ifdef QUAD_PRECISION
390 #include "common_quad.h"
394 #include "common_alpha.h"
398 #include "common_x86.h"
402 #include "common_x86_64.h"
406 #include "common_ia64.h"
410 #include "common_power.h"
414 #include "common_sparc.h"
418 #include "common_mips.h"
422 #include "common_mips64.h"
426 #include "common_arm.h"
430 #include "common_arm64.h"
434 #include "common_zarch.h"
438 #ifdef OS_WINDOWSSTORE
439 typedef char env_var_t[MAX_PATH];
440 #define readenv(p, n) 0
443 typedef char env_var_t[MAX_PATH];
444 #define readenv(p, n) GetEnvironmentVariable((LPCTSTR)(n), (LPTSTR)(p), sizeof(p))
446 typedef char* env_var_t;
447 #define readenv(p, n) ((p)=getenv(n))
451 #if !defined(RPCC_DEFINED) && !defined(OS_WINDOWS)
452 #ifdef _POSIX_MONOTONIC_CLOCK
453 #if defined(__GLIBC_PREREQ) // cut the if condition if two lines, otherwise will fail at __GLIBC_PREREQ(2, 17)
454 #if __GLIBC_PREREQ(2, 17) // don't require -lrt
455 #define USE_MONOTONIC
457 #elif defined(OS_ANDROID)
458 #define USE_MONOTONIC
461 /* use similar scale as x86 rdtsc for timeouts to work correctly */
462 static inline unsigned long long rpcc(void){
465 clock_gettime(CLOCK_MONOTONIC, &ts);
466 return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
469 gettimeofday(&tv,NULL);
470 return (unsigned long long)tv.tv_sec * 1000000000ull + tv.tv_usec * 1000;
475 #endif // !RPCC_DEFINED
477 #if !defined(BLAS_LOCK_DEFINED) && defined(__GNUC__)
478 static void __inline blas_lock(volatile BLASULONG *address){
481 while (*address) {YIELDING;};
483 } while (!__sync_bool_compare_and_swap(address, 0, 1));
485 #define BLAS_LOCK_DEFINED
489 #error "rpcc() implementation is missing for your platform"
491 #ifndef BLAS_LOCK_DEFINED
492 #error "blas_lock() implementation is missing for your platform"
497 #include "common_linux.h"
500 #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
503 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANON)
505 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
509 /* C99 supports complex floating numbers natively, which GCC also offers as an
510 extension since version 3.0. If neither are available, use a compatible
511 structure as fallback (see Clause 6.2.5.13 of the C99 standard). */
512 #if ((defined(__STDC_IEC_559_COMPLEX__) || __STDC_VERSION__ >= 199901L || \
513 (__GNUC__ >= 3 && !defined(__cplusplus))) && !(defined(FORCE_OPENBLAS_COMPLEX_STRUCT))) && !defined(_MSC_VER)
514 #define OPENBLAS_COMPLEX_C99
518 typedef float _Complex openblas_complex_float;
519 typedef double _Complex openblas_complex_double;
520 typedef xdouble _Complex openblas_complex_xdouble;
521 #define openblas_make_complex_float(real, imag) ((real) + ((imag) * _Complex_I))
522 #define openblas_make_complex_double(real, imag) ((real) + ((imag) * _Complex_I))
523 #define openblas_make_complex_xdouble(real, imag) ((real) + ((imag) * _Complex_I))
525 #define OPENBLAS_COMPLEX_STRUCT
526 typedef struct { float real, imag; } openblas_complex_float;
527 typedef struct { double real, imag; } openblas_complex_double;
528 typedef struct { xdouble real, imag; } openblas_complex_xdouble;
529 #define openblas_make_complex_float(real, imag) {(real), (imag)}
530 #define openblas_make_complex_double(real, imag) {(real), (imag)}
531 #define openblas_make_complex_xdouble(real, imag) {(real), (imag)}
536 #include "common_param.h"
539 #define STDERR stderr
543 #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
546 #if defined(XDOUBLE) || defined(DOUBLE)
547 #define FLOATRET FLOAT
550 #define FLOATRET double
552 #define FLOATRET float
558 /* Inclusion of a standard header file is needed for definition of __STDC_*
559 predefined macros with some compilers (e.g. GCC 4.7 on Linux). This occurs
560 as a side effect of including either <features.h> or <stdc-predef.h>. */
565 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_xdouble
566 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_xdouble(r,i)
567 #elif defined(DOUBLE)
568 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_double
569 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_double(r,i)
571 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_float
572 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_float(r,i)
575 #if defined(C_PGI) || defined(C_SUN)
576 #if defined(__STDC_IEC_559_COMPLEX__)
577 #define CREAL(X) creal(X)
578 #define CIMAG(X) cimag(X)
580 #define CREAL(X) (*((FLOAT *)&X + 0))
581 #define CIMAG(X) (*((FLOAT *)&X + 1))
584 #ifdef OPENBLAS_COMPLEX_STRUCT
585 #define CREAL(Z) ((Z).real)
586 #define CIMAG(Z) ((Z).imag)
588 #define CREAL __real__
589 #define CIMAG __imag__
603 #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
610 #define inline __inline
616 #define MIN(a,b) (a>b? b:a)
620 #define MAX(a,b) (a<b? b:a)
623 #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
625 #if defined(__FreeBSD__) || defined(__APPLE__)
626 #define MAP_ANONYMOUS MAP_ANON
629 /* Common Memory Management Routine */
630 void blas_set_parameter(void);
631 int blas_get_cpu_number(void);
632 void *blas_memory_alloc (int);
633 void blas_memory_free (void *);
634 void *blas_memory_alloc_nolock (int); //use malloc without blas_lock
635 void blas_memory_free_nolock (void *);
637 int get_num_procs (void);
639 #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
640 int get_num_nodes (void);
641 int get_num_proc (int);
642 int get_node_equal (void);
645 void goto_set_num_threads(int);
647 void gotoblas_affinity_init(void);
648 void gotoblas_affinity_quit(void);
649 void gotoblas_dynamic_init(void);
650 void gotoblas_dynamic_quit(void);
651 void gotoblas_profile_init(void);
652 void gotoblas_profile_quit(void);
657 int omp_in_parallel(void);
658 int omp_get_num_procs(void);
660 __declspec(dllimport) int __cdecl omp_in_parallel(void);
661 __declspec(dllimport) int __cdecl omp_get_num_procs(void);
664 #if (__STDC_VERSION__ >= 201112L)
665 #if defined(C_GCC) && ( __GNUC__ < 7)
666 // workaround for GCC bug 65467
668 #define _Atomic volatile
671 #include <stdatomic.h>
674 #define _Atomic volatile
680 int omp_in_parallel (void) __attribute__ ((weak));
681 int omp_get_num_procs(void) __attribute__ ((weak));
685 static __inline void blas_unlock(volatile BLASULONG *address){
690 #ifdef OS_WINDOWSSTORE
691 static __inline int readenv_atoi(char *env) {
696 static __inline int readenv_atoi(char *env) {
698 return readenv(p,env) ? 0 : atoi(p);
701 static __inline int readenv_atoi(char *env) {
703 if (( p = getenv(env) ))
711 #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
713 static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
720 (fabsl(ar)) >= (fabsl(ai))
722 (fabs (ar)) >= (fabs (ai))
724 (fabsf(ar)) >= (fabsf(ai))
728 den = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
733 den = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
748 void *blas_debug_alloc(int);
749 void *blas_debug_free(void *);
752 #define malloc(a) blas_debug_alloc(a)
753 #define free(a) blas_debug_free (a)
757 #define GEMMRETTYPE int
767 #define GEMMRETTYPE copyoverhead_t
772 #define KNAME(A, B) A
774 #define KNAME(A, B) A##B
777 #include "common_interface.h"
779 #include "common_reference.h"
781 #include "common_macro.h"
782 #include "common_level1.h"
783 #include "common_level2.h"
784 #include "common_level3.h"
785 #include "common_lapack.h"
788 # define OPENBLAS_CONST /* see comment in cblas.h */
793 #include "common_stackalloc.h"
798 #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
799 #error Both SMP_SERVER and SMP_ONDEMAND are specified.
802 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
803 #include "common_thread.h"
810 #ifndef DEFAULT_CPU_NUMBER
811 #define DEFAULT_CPU_NUMBER 4
822 #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
826 unsigned long long calls, fops, area, cycles, tcycles;
829 extern func_profile_t function_profile_table[];
830 extern int gotoblas_profile;
833 #define NUMOPT QNUMOPT
835 #define NUMOPT DNUMOPT
837 #define NUMOPT SNUMOPT
840 #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
842 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
843 if (gotoblas_profile) { \
844 profile_end = rpcc(); \
845 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
846 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
847 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
848 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
849 function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
853 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
854 if (gotoblas_profile) { \
855 profile_end = rpcc(); \
856 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
857 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
858 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
859 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
860 function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
866 #define FUNCTION_PROFILE_START()
867 #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
871 #define PRINT_DEBUG_CNAME
872 #define PRINT_DEBUG_NAME
874 #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
875 #define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
881 #endif /* __cplusplus */