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)
89 #define snprintf _snprintf
98 #if defined(OS_DARWIN) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_ANDROID)
104 //Android NDK only supports complex.h since Android 5.0
105 #if __ANDROID_API__ < 21
106 #define FORCE_OPENBLAS_COMPLEX_STRUCT
116 #define GOTO_ATOM ATOM
122 #define ATOM GOTO_ATOM
126 #include <sys/mman.h>
130 #include <sys/time.h>
134 #if defined(SMP) || defined(USE_LOCKING)
139 #if defined(OS_SUNOS)
145 #include <machine/builtins.h>
148 #if defined(ARCH_IA64) && defined(ENABLE_SSE_EXCEPTION)
154 #if defined(OS_WINDOWS) && defined(DOUBLE_DEFINED)
155 #define DOUBLE DOUBLE_DEFINED
156 #undef DOUBLE_DEFINED
162 #undef SMP_ALLOC_DEBUG
184 #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
186 #define ALLOCA_ALIGN 63UL
188 #define NUM_BUFFERS MAX(50,(MAX_CPU_NUMBER * 2 * MAX_PARALLEL_NUMBER))
190 #ifdef NEEDBUNDERSCORE
191 #define BLASFUNC(FUNC) FUNC##_
193 #define BLASFUNC(FUNC) FUNC
196 #undef USE_PTHREAD_LOCK
197 #undef USE_PTHREAD_SPINLOCK
199 #if defined(USE_PTHREAD_LOCK) && defined(USE_PTHREAD_SPINLOCK)
200 #error "You can't specify both LOCK operation!"
203 #if defined(SMP) || defined(USE_LOCKING)
204 #define USE_PTHREAD_LOCK
205 #undef USE_PTHREAD_SPINLOCK
209 #undef USE_PTHREAD_LOCK
210 #undef USE_PTHREAD_SPINLOCK
213 #if defined(USE_PTHREAD_LOCK)
214 #define LOCK_COMMAND(x) pthread_mutex_lock(x)
215 #define UNLOCK_COMMAND(x) pthread_mutex_unlock(x)
216 #elif defined(USE_PTHREAD_SPINLOCK)
218 typedef volatile int pthread_spinlock_t;
219 int pthread_spin_lock (pthread_spinlock_t *__lock);
220 int pthread_spin_unlock (pthread_spinlock_t *__lock);
222 #define LOCK_COMMAND(x) pthread_spin_lock(x)
223 #define UNLOCK_COMMAND(x) pthread_spin_unlock(x)
225 #define LOCK_COMMAND(x) blas_lock(x)
226 #define UNLOCK_COMMAND(x) blas_unlock(x)
229 #define GOTO_SHMID 0x510510
242 #ifdef QUAD_PRECISION
246 #elif defined EXPRECISION
247 #define xdouble long double
249 #define xdouble double
252 #if defined(OS_WINDOWS) && defined(__64BIT__)
253 typedef long long BLASLONG;
254 typedef unsigned long long BLASULONG;
256 typedef long BLASLONG;
257 typedef unsigned long BLASULONG;
261 typedef BLASLONG blasint;
262 #if defined(OS_WINDOWS) && defined(__64BIT__)
263 #define blasabs(x) llabs(x)
265 #define blasabs(x) labs(x)
269 #define blasabs(x) abs(x)
282 #define FLOAT xdouble
283 #ifdef QUAD_PRECISION
284 #define XFLOAT xidouble
286 #ifdef QUAD_PRECISION
289 #define ZBASE_SHIFT 6
293 #define ZBASE_SHIFT 5
295 #elif defined(DOUBLE)
299 #define ZBASE_SHIFT 4
304 #define ZBASE_SHIFT 3
318 #define Address_H(x) (((x)+(1<<15))>>16)
319 #define Address_L(x) ((x)-((Address_H(x))<<16))
321 #ifndef MAX_CPU_NUMBER
322 #define MAX_CPU_NUMBER 2
325 #if defined(OS_SUNOS)
326 #define YIELDING thr_yield()
329 #if defined(OS_WINDOWS)
330 #if defined(_MSC_VER) && !defined(__clang__)
331 #define YIELDING YieldProcessor()
333 #define YIELDING SwitchToThread()
337 #if defined(ARMV7) || defined(ARMV6) || defined(ARMV8) || defined(ARMV5)
338 #define YIELDING asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop; \n");
343 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
349 #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");
362 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
370 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
376 #define YIELDING sched_yield()
380 To alloc job_t on heap or statck.
381 please https://github.com/xianyi/OpenBLAS/issues/246
383 #if defined(OS_WINDOWS)
384 #define GETRF_MEM_ALLOC_THRESHOLD 32
385 #define BLAS3_MEM_ALLOC_THRESHOLD 32
388 #ifndef GETRF_MEM_ALLOC_THRESHOLD
389 #define GETRF_MEM_ALLOC_THRESHOLD 80
392 #ifndef BLAS3_MEM_ALLOC_THRESHOLD
393 #define BLAS3_MEM_ALLOC_THRESHOLD 160
396 #ifdef QUAD_PRECISION
397 #include "common_quad.h"
401 #include "common_alpha.h"
405 #include "common_x86.h"
409 #include "common_x86_64.h"
413 #include "common_ia64.h"
417 #include "common_power.h"
421 #include "common_sparc.h"
425 #include "common_mips.h"
429 #include "common_mips64.h"
433 #include "common_arm.h"
437 #include "common_arm64.h"
441 #include "common_zarch.h"
445 #ifdef OS_WINDOWSSTORE
446 typedef char env_var_t[MAX_PATH];
447 #define readenv(p, n) 0
449 #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
450 typedef char env_var_t[MAX_PATH];
451 #define readenv(p, n) GetEnvironmentVariable((LPCTSTR)(n), (LPTSTR)(p), sizeof(p))
453 typedef char* env_var_t;
454 #define readenv(p, n) ((p)=getenv(n))
458 #if !defined(RPCC_DEFINED) && !defined(OS_WINDOWS)
459 #ifdef _POSIX_MONOTONIC_CLOCK
460 #if defined(__GLIBC_PREREQ) // cut the if condition if two lines, otherwise will fail at __GLIBC_PREREQ(2, 17)
461 #if __GLIBC_PREREQ(2, 17) // don't require -lrt
462 #define USE_MONOTONIC
464 #elif defined(OS_ANDROID)
465 #define USE_MONOTONIC
468 /* use similar scale as x86 rdtsc for timeouts to work correctly */
469 static inline unsigned long long rpcc(void){
472 clock_gettime(CLOCK_MONOTONIC, &ts);
473 return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
476 gettimeofday(&tv,NULL);
477 return (unsigned long long)tv.tv_sec * 1000000000ull + tv.tv_usec * 1000;
482 #endif // !RPCC_DEFINED
484 #if !defined(BLAS_LOCK_DEFINED) && defined(__GNUC__)
485 static void __inline blas_lock(volatile BLASULONG *address){
488 while (*address) {YIELDING;};
490 } while (!__sync_bool_compare_and_swap(address, 0, 1));
492 #define BLAS_LOCK_DEFINED
496 #error "rpcc() implementation is missing for your platform"
498 #ifndef BLAS_LOCK_DEFINED
499 #error "blas_lock() implementation is missing for your platform"
504 #include "common_linux.h"
507 #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
510 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANON)
512 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
516 /* C99 supports complex floating numbers natively, which GCC also offers as an
517 extension since version 3.0. If neither are available, use a compatible
518 structure as fallback (see Clause 6.2.5.13 of the C99 standard). */
519 #if ((defined(__STDC_IEC_559_COMPLEX__) || __STDC_VERSION__ >= 199901L || \
520 (__GNUC__ >= 3 && !defined(__cplusplus))) && !(defined(FORCE_OPENBLAS_COMPLEX_STRUCT))) && !defined(_MSC_VER)
521 #define OPENBLAS_COMPLEX_C99
525 typedef float _Complex openblas_complex_float;
526 typedef double _Complex openblas_complex_double;
527 typedef xdouble _Complex openblas_complex_xdouble;
528 #define openblas_make_complex_float(real, imag) ((real) + ((imag) * _Complex_I))
529 #define openblas_make_complex_double(real, imag) ((real) + ((imag) * _Complex_I))
530 #define openblas_make_complex_xdouble(real, imag) ((real) + ((imag) * _Complex_I))
532 #define OPENBLAS_COMPLEX_STRUCT
533 typedef struct { float real, imag; } openblas_complex_float;
534 typedef struct { double real, imag; } openblas_complex_double;
535 typedef struct { xdouble real, imag; } openblas_complex_xdouble;
536 #define openblas_make_complex_float(real, imag) {(real), (imag)}
537 #define openblas_make_complex_double(real, imag) {(real), (imag)}
538 #define openblas_make_complex_xdouble(real, imag) {(real), (imag)}
543 #include "common_param.h"
546 #define STDERR stderr
550 #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
553 #if defined(XDOUBLE) || defined(DOUBLE)
554 #define FLOATRET FLOAT
557 #define FLOATRET double
559 #define FLOATRET float
565 /* Inclusion of a standard header file is needed for definition of __STDC_*
566 predefined macros with some compilers (e.g. GCC 4.7 on Linux). This occurs
567 as a side effect of including either <features.h> or <stdc-predef.h>. */
572 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_xdouble
573 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_xdouble(r,i)
574 #elif defined(DOUBLE)
575 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_double
576 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_double(r,i)
578 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_float
579 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_float(r,i)
582 #if defined(C_PGI) || defined(C_SUN)
583 #if defined(__STDC_IEC_559_COMPLEX__)
584 #define CREAL(X) creal(X)
585 #define CIMAG(X) cimag(X)
587 #define CREAL(X) (*((FLOAT *)&X + 0))
588 #define CIMAG(X) (*((FLOAT *)&X + 1))
591 #ifdef OPENBLAS_COMPLEX_STRUCT
592 #define CREAL(Z) ((Z).real)
593 #define CIMAG(Z) ((Z).imag)
595 #define CREAL __real__
596 #define CIMAG __imag__
610 #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
617 #define inline __inline
623 #define MIN(a,b) (a>b? b:a)
627 #define MAX(a,b) (a<b? b:a)
630 #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
632 #if defined(__FreeBSD__) || defined(__APPLE__)
633 #define MAP_ANONYMOUS MAP_ANON
636 /* Common Memory Management Routine */
637 void blas_set_parameter(void);
638 int blas_get_cpu_number(void);
639 void *blas_memory_alloc (int);
640 void blas_memory_free (void *);
641 void *blas_memory_alloc_nolock (int); //use malloc without blas_lock
642 void blas_memory_free_nolock (void *);
644 int get_num_procs (void);
646 #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
647 int get_num_nodes (void);
648 int get_num_proc (int);
649 int get_node_equal (void);
652 void goto_set_num_threads(int);
654 void gotoblas_affinity_init(void);
655 void gotoblas_affinity_quit(void);
656 void gotoblas_dynamic_init(void);
657 void gotoblas_dynamic_quit(void);
658 void gotoblas_profile_init(void);
659 void gotoblas_profile_quit(void);
664 int omp_in_parallel(void);
665 int omp_get_num_procs(void);
667 __declspec(dllimport) int __cdecl omp_in_parallel(void);
668 __declspec(dllimport) int __cdecl omp_get_num_procs(void);
671 #if (__STDC_VERSION__ >= 201112L)
672 #if defined(C_GCC) && ( __GNUC__ < 7)
673 // workaround for GCC bug 65467
675 #define _Atomic volatile
678 #include <stdatomic.h>
681 #define _Atomic volatile
687 int omp_in_parallel (void) __attribute__ ((weak));
688 int omp_get_num_procs(void) __attribute__ ((weak));
692 static __inline void blas_unlock(volatile BLASULONG *address){
697 #ifdef OS_WINDOWSSTORE
698 static __inline int readenv_atoi(char *env) {
703 static __inline int readenv_atoi(char *env) {
705 return readenv(p,env) ? 0 : atoi(p);
708 static __inline int readenv_atoi(char *env) {
710 if (( p = getenv(env) ))
718 #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
720 static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
727 (fabsl(ar)) >= (fabsl(ai))
729 (fabs (ar)) >= (fabs (ai))
731 (fabsf(ar)) >= (fabsf(ai))
735 den = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
740 den = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
755 void *blas_debug_alloc(int);
756 void *blas_debug_free(void *);
759 #define malloc(a) blas_debug_alloc(a)
760 #define free(a) blas_debug_free (a)
764 #define GEMMRETTYPE int
774 #define GEMMRETTYPE copyoverhead_t
779 #define KNAME(A, B) A
781 #define KNAME(A, B) A##B
784 #include "common_interface.h"
786 #include "common_reference.h"
788 #include "common_macro.h"
789 #include "common_level1.h"
790 #include "common_level2.h"
791 #include "common_level3.h"
792 #include "common_lapack.h"
795 # define OPENBLAS_CONST /* see comment in cblas.h */
800 #include "common_stackalloc.h"
805 #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
806 #error Both SMP_SERVER and SMP_ONDEMAND are specified.
809 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
810 #include "common_thread.h"
817 #ifndef DEFAULT_CPU_NUMBER
818 #define DEFAULT_CPU_NUMBER 4
829 #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
833 unsigned long long calls, fops, area, cycles, tcycles;
836 extern func_profile_t function_profile_table[];
837 extern int gotoblas_profile;
840 #define NUMOPT QNUMOPT
842 #define NUMOPT DNUMOPT
844 #define NUMOPT SNUMOPT
847 #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
849 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
850 if (gotoblas_profile) { \
851 profile_end = rpcc(); \
852 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
853 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
854 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
855 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
856 function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
860 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
861 if (gotoblas_profile) { \
862 profile_end = rpcc(); \
863 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
864 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
865 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
866 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
867 function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
873 #define FUNCTION_PROFILE_START()
874 #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
878 #define PRINT_DEBUG_CNAME
879 #define PRINT_DEBUG_NAME
881 #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
882 #define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
888 #endif /* __cplusplus */