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
110 #define GOTO_ATOM ATOM
116 #define ATOM GOTO_ATOM
120 #include <sys/mman.h>
124 #include <sys/time.h>
133 #if defined(OS_SUNOS)
139 #include <machine/builtins.h>
142 #if defined(ARCH_IA64) && defined(ENABLE_SSE_EXCEPTION)
148 #if defined(OS_WINDOWS) && defined(DOUBLE_DEFINED)
149 #define DOUBLE DOUBLE_DEFINED
150 #undef DOUBLE_DEFINED
156 #undef SMP_ALLOC_DEBUG
178 #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
180 #define ALLOCA_ALIGN 63UL
182 #define NUM_BUFFERS (MAX_CPU_NUMBER * 2 * MAX_PARALLEL_NUMBER)
184 #ifdef NEEDBUNDERSCORE
185 #define BLASFUNC(FUNC) FUNC##_
187 #define BLASFUNC(FUNC) FUNC
190 #undef USE_PTHREAD_LOCK
191 #undef USE_PTHREAD_SPINLOCK
193 #if defined(USE_PTHREAD_LOCK) && defined(USE_PTHREAD_SPINLOCK)
194 #error "You can't specify both LOCK operation!"
198 #define USE_PTHREAD_LOCK
199 #undef USE_PTHREAD_SPINLOCK
203 #undef USE_PTHREAD_LOCK
204 #undef USE_PTHREAD_SPINLOCK
207 #if defined(USE_PTHREAD_LOCK)
208 #define LOCK_COMMAND(x) pthread_mutex_lock(x)
209 #define UNLOCK_COMMAND(x) pthread_mutex_unlock(x)
210 #elif defined(USE_PTHREAD_SPINLOCK)
212 typedef volatile int pthread_spinlock_t;
213 int pthread_spin_lock (pthread_spinlock_t *__lock);
214 int pthread_spin_unlock (pthread_spinlock_t *__lock);
216 #define LOCK_COMMAND(x) pthread_spin_lock(x)
217 #define UNLOCK_COMMAND(x) pthread_spin_unlock(x)
219 #define LOCK_COMMAND(x) blas_lock(x)
220 #define UNLOCK_COMMAND(x) blas_unlock(x)
223 #define GOTO_SHMID 0x510510
236 #ifdef QUAD_PRECISION
240 #elif defined EXPRECISION
241 #define xdouble long double
243 #define xdouble double
246 #if defined(OS_WINDOWS) && defined(__64BIT__)
247 typedef long long BLASLONG;
248 typedef unsigned long long BLASULONG;
250 typedef long BLASLONG;
251 typedef unsigned long BLASULONG;
255 typedef BLASLONG blasint;
256 #define blasabs(x) labs(x)
259 #define blasabs(x) abs(x)
272 #define FLOAT xdouble
273 #ifdef QUAD_PRECISION
274 #define XFLOAT xidouble
276 #ifdef QUAD_PRECISION
279 #define ZBASE_SHIFT 6
283 #define ZBASE_SHIFT 5
285 #elif defined(DOUBLE)
289 #define ZBASE_SHIFT 4
294 #define ZBASE_SHIFT 3
308 #define Address_H(x) (((x)+(1<<15))>>16)
309 #define Address_L(x) ((x)-((Address_H(x))<<16))
311 #ifndef MAX_CPU_NUMBER
312 #define MAX_CPU_NUMBER 2
315 #if defined(OS_SUNOS)
316 #define YIELDING thr_yield()
319 #if defined(OS_WINDOWS)
320 #if defined(_MSC_VER) && !defined(__clang__)
321 #define YIELDING YieldProcessor()
323 #define YIELDING SwitchToThread()
327 #if defined(ARMV7) || defined(ARMV6) || defined(ARMV8) || defined(ARMV5)
328 #define YIELDING asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop; \n");
333 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
339 #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");
361 #define YIELDING sched_yield()
365 To alloc job_t on heap or statck.
366 please https://github.com/xianyi/OpenBLAS/issues/246
368 #if defined(OS_WINDOWS)
369 #define GETRF_MEM_ALLOC_THRESHOLD 32
370 #define BLAS3_MEM_ALLOC_THRESHOLD 32
373 #ifndef GETRF_MEM_ALLOC_THRESHOLD
374 #define GETRF_MEM_ALLOC_THRESHOLD 80
377 #ifndef BLAS3_MEM_ALLOC_THRESHOLD
378 #define BLAS3_MEM_ALLOC_THRESHOLD 160
381 #ifdef QUAD_PRECISION
382 #include "common_quad.h"
386 #include "common_alpha.h"
390 #include "common_x86.h"
394 #include "common_x86_64.h"
398 #include "common_ia64.h"
402 #include "common_power.h"
406 #include "common_sparc.h"
410 #include "common_mips.h"
414 #include "common_mips64.h"
418 #include "common_arm.h"
422 #include "common_arm64.h"
426 #include "common_zarch.h"
430 #ifdef OS_WINDOWSSTORE
431 typedef char env_var_t[MAX_PATH];
432 #define readenv(p, n) 0
435 typedef char env_var_t[MAX_PATH];
436 #define readenv(p, n) GetEnvironmentVariable((LPCTSTR)(n), (LPTSTR)(p), sizeof(p))
438 typedef char* env_var_t;
439 #define readenv(p, n) ((p)=getenv(n))
443 #if !defined(RPCC_DEFINED) && !defined(OS_WINDOWS)
444 #ifdef _POSIX_MONOTONIC_CLOCK
445 #if defined(__GLIBC_PREREQ) // cut the if condition if two lines, otherwise will fail at __GLIBC_PREREQ(2, 17)
446 #if __GLIBC_PREREQ(2, 17) // don't require -lrt
447 #define USE_MONOTONIC
449 #elif defined(OS_ANDROID)
450 #define USE_MONOTONIC
453 /* use similar scale as x86 rdtsc for timeouts to work correctly */
454 static inline unsigned long long rpcc(void){
457 clock_gettime(CLOCK_MONOTONIC, &ts);
458 return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
461 gettimeofday(&tv,NULL);
462 return (unsigned long long)tv.tv_sec * 1000000000ull + tv.tv_usec * 1000;
467 #endif // !RPCC_DEFINED
469 #if !defined(BLAS_LOCK_DEFINED) && defined(__GNUC__)
470 static void __inline blas_lock(volatile BLASULONG *address){
473 while (*address) {YIELDING;};
475 } while (!__sync_bool_compare_and_swap(address, 0, 1));
477 #define BLAS_LOCK_DEFINED
481 #error "rpcc() implementation is missing for your platform"
483 #ifndef BLAS_LOCK_DEFINED
484 #error "blas_lock() implementation is missing for your platform"
489 #include "common_linux.h"
492 #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
495 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANON)
497 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
501 /* C99 supports complex floating numbers natively, which GCC also offers as an
502 extension since version 3.0. If neither are available, use a compatible
503 structure as fallback (see Clause 6.2.5.13 of the C99 standard). */
504 #if ((defined(__STDC_IEC_559_COMPLEX__) || __STDC_VERSION__ >= 199901L || \
505 (__GNUC__ >= 3 && !defined(__cplusplus))) && !(defined(FORCE_OPENBLAS_COMPLEX_STRUCT))) && !defined(_MSC_VER)
506 #define OPENBLAS_COMPLEX_C99
510 typedef float _Complex openblas_complex_float;
511 typedef double _Complex openblas_complex_double;
512 typedef xdouble _Complex openblas_complex_xdouble;
513 #define openblas_make_complex_float(real, imag) ((real) + ((imag) * _Complex_I))
514 #define openblas_make_complex_double(real, imag) ((real) + ((imag) * _Complex_I))
515 #define openblas_make_complex_xdouble(real, imag) ((real) + ((imag) * _Complex_I))
517 #define OPENBLAS_COMPLEX_STRUCT
518 typedef struct { float real, imag; } openblas_complex_float;
519 typedef struct { double real, imag; } openblas_complex_double;
520 typedef struct { xdouble real, imag; } openblas_complex_xdouble;
521 #define openblas_make_complex_float(real, imag) {(real), (imag)}
522 #define openblas_make_complex_double(real, imag) {(real), (imag)}
523 #define openblas_make_complex_xdouble(real, imag) {(real), (imag)}
528 #include "common_param.h"
531 #define STDERR stderr
535 #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
538 #if defined(XDOUBLE) || defined(DOUBLE)
539 #define FLOATRET FLOAT
542 #define FLOATRET double
544 #define FLOATRET float
550 /* Inclusion of a standard header file is needed for definition of __STDC_*
551 predefined macros with some compilers (e.g. GCC 4.7 on Linux). This occurs
552 as a side effect of including either <features.h> or <stdc-predef.h>. */
557 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_xdouble
558 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_xdouble(r,i)
559 #elif defined(DOUBLE)
560 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_double
561 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_double(r,i)
563 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_float
564 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_float(r,i)
567 #if defined(C_PGI) || defined(C_SUN)
568 #if defined(__STDC_IEC_559_COMPLEX__)
569 #define CREAL(X) creal(X)
570 #define CIMAG(X) cimag(X)
572 #define CREAL(X) (*((FLOAT *)&X + 0))
573 #define CIMAG(X) (*((FLOAT *)&X + 1))
576 #ifdef OPENBLAS_COMPLEX_STRUCT
577 #define CREAL(Z) ((Z).real)
578 #define CIMAG(Z) ((Z).imag)
580 #define CREAL __real__
581 #define CIMAG __imag__
595 #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
602 #define inline __inline
608 #define MIN(a,b) (a>b? b:a)
612 #define MAX(a,b) (a<b? b:a)
615 #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
617 #if defined(__FreeBSD__) || defined(__APPLE__)
618 #define MAP_ANONYMOUS MAP_ANON
621 /* Common Memory Management Routine */
622 void blas_set_parameter(void);
623 int blas_get_cpu_number(void);
624 void *blas_memory_alloc (int);
625 void blas_memory_free (void *);
626 void *blas_memory_alloc_nolock (int); //use malloc without blas_lock
627 void blas_memory_free_nolock (void *);
629 int get_num_procs (void);
631 #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
632 int get_num_nodes (void);
633 int get_num_proc (int);
634 int get_node_equal (void);
637 void goto_set_num_threads(int);
639 void gotoblas_affinity_init(void);
640 void gotoblas_affinity_quit(void);
641 void gotoblas_dynamic_init(void);
642 void gotoblas_dynamic_quit(void);
643 void gotoblas_profile_init(void);
644 void gotoblas_profile_quit(void);
649 int omp_in_parallel(void);
650 int omp_get_num_procs(void);
652 __declspec(dllimport) int __cdecl omp_in_parallel(void);
653 __declspec(dllimport) int __cdecl omp_get_num_procs(void);
656 #if (__STDC_VERSION__ >= 201112L)
657 #if defined(C_GCC) && ( __GNUC__ < 7)
658 // workaround for GCC bug 65467
660 #define _Atomic volatile
663 #include <stdatomic.h>
666 #define _Atomic volatile
672 int omp_in_parallel (void) __attribute__ ((weak));
673 int omp_get_num_procs(void) __attribute__ ((weak));
677 static __inline void blas_unlock(volatile BLASULONG *address){
682 #ifdef OS_WINDOWSSTORE
683 static __inline int readenv_atoi(char *env) {
688 static __inline int readenv_atoi(char *env) {
690 return readenv(p,env) ? 0 : atoi(p);
693 static __inline int readenv_atoi(char *env) {
695 if (( p = getenv(env) ))
703 #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
705 static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
712 (fabsl(ar)) >= (fabsl(ai))
714 (fabs (ar)) >= (fabs (ai))
716 (fabsf(ar)) >= (fabsf(ai))
720 den = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
725 den = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
740 void *blas_debug_alloc(int);
741 void *blas_debug_free(void *);
744 #define malloc(a) blas_debug_alloc(a)
745 #define free(a) blas_debug_free (a)
749 #define GEMMRETTYPE int
759 #define GEMMRETTYPE copyoverhead_t
764 #define KNAME(A, B) A
766 #define KNAME(A, B) A##B
769 #include "common_interface.h"
771 #include "common_reference.h"
773 #include "common_macro.h"
774 #include "common_level1.h"
775 #include "common_level2.h"
776 #include "common_level3.h"
777 #include "common_lapack.h"
780 # define OPENBLAS_CONST /* see comment in cblas.h */
785 #include "common_stackalloc.h"
790 #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
791 #error Both SMP_SERVER and SMP_ONDEMAND are specified.
794 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
795 #include "common_thread.h"
802 #ifndef DEFAULT_CPU_NUMBER
803 #define DEFAULT_CPU_NUMBER 4
814 #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
818 unsigned long long calls, fops, area, cycles, tcycles;
821 extern func_profile_t function_profile_table[];
822 extern int gotoblas_profile;
825 #define NUMOPT QNUMOPT
827 #define NUMOPT DNUMOPT
829 #define NUMOPT SNUMOPT
832 #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
834 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
835 if (gotoblas_profile) { \
836 profile_end = rpcc(); \
837 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
838 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
839 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
840 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
841 function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
845 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
846 if (gotoblas_profile) { \
847 profile_end = rpcc(); \
848 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
849 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
850 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
851 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
852 function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
858 #define FUNCTION_PROFILE_START()
859 #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
863 #define PRINT_DEBUG_CNAME
864 #define PRINT_DEBUG_NAME
866 #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
867 #define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
873 #endif /* __cplusplus */