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;
270 #define FLOAT xdouble
271 #ifdef QUAD_PRECISION
272 #define XFLOAT xidouble
274 #ifdef QUAD_PRECISION
277 #define ZBASE_SHIFT 6
281 #define ZBASE_SHIFT 5
283 #elif defined(DOUBLE)
287 #define ZBASE_SHIFT 4
292 #define ZBASE_SHIFT 3
306 #define Address_H(x) (((x)+(1<<15))>>16)
307 #define Address_L(x) ((x)-((Address_H(x))<<16))
309 #ifndef MAX_CPU_NUMBER
310 #define MAX_CPU_NUMBER 2
313 #if defined(OS_SUNOS)
314 #define YIELDING thr_yield()
317 #if defined(OS_WINDOWS)
318 #if defined(_MSC_VER) && !defined(__clang__)
319 #define YIELDING YieldProcessor()
321 #define YIELDING SwitchToThread()
325 #if defined(ARMV7) || defined(ARMV6) || defined(ARMV8) || defined(ARMV5)
326 #define YIELDING asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop; \n");
331 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
337 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
345 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
353 #define YIELDING __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
359 #define YIELDING sched_yield()
363 To alloc job_t on heap or statck.
364 please https://github.com/xianyi/OpenBLAS/issues/246
366 #if defined(OS_WINDOWS)
367 #define GETRF_MEM_ALLOC_THRESHOLD 32
368 #define BLAS3_MEM_ALLOC_THRESHOLD 32
371 #ifndef GETRF_MEM_ALLOC_THRESHOLD
372 #define GETRF_MEM_ALLOC_THRESHOLD 80
375 #ifndef BLAS3_MEM_ALLOC_THRESHOLD
376 #define BLAS3_MEM_ALLOC_THRESHOLD 160
379 #ifdef QUAD_PRECISION
380 #include "common_quad.h"
384 #include "common_alpha.h"
388 #include "common_x86.h"
392 #include "common_x86_64.h"
396 #include "common_ia64.h"
400 #include "common_power.h"
404 #include "common_sparc.h"
408 #include "common_mips.h"
412 #include "common_mips64.h"
416 #include "common_arm.h"
420 #include "common_arm64.h"
424 #include "common_zarch.h"
428 #ifdef OS_WINDOWSSTORE
429 typedef char env_var_t[MAX_PATH];
430 #define readenv(p, n) 0
433 typedef char env_var_t[MAX_PATH];
434 #define readenv(p, n) GetEnvironmentVariable((LPCTSTR)(n), (LPTSTR)(p), sizeof(p))
436 typedef char* env_var_t;
437 #define readenv(p, n) ((p)=getenv(n))
441 #if !defined(RPCC_DEFINED) && !defined(OS_WINDOWS)
442 #ifdef _POSIX_MONOTONIC_CLOCK
443 #if defined(__GLIBC_PREREQ) // cut the if condition if two lines, otherwise will fail at __GLIBC_PREREQ(2, 17)
444 #if __GLIBC_PREREQ(2, 17) // don't require -lrt
445 #define USE_MONOTONIC
447 #elif defined(OS_ANDROID)
448 #define USE_MONOTONIC
451 /* use similar scale as x86 rdtsc for timeouts to work correctly */
452 static inline unsigned long long rpcc(void){
455 clock_gettime(CLOCK_MONOTONIC, &ts);
456 return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
459 gettimeofday(&tv,NULL);
460 return (unsigned long long)tv.tv_sec * 1000000000ull + tv.tv_usec * 1000;
465 #endif // !RPCC_DEFINED
467 #if !defined(BLAS_LOCK_DEFINED) && defined(__GNUC__)
468 static void __inline blas_lock(volatile BLASULONG *address){
471 while (*address) {YIELDING;};
473 } while (!__sync_bool_compare_and_swap(address, 0, 1));
475 #define BLAS_LOCK_DEFINED
479 #error "rpcc() implementation is missing for your platform"
481 #ifndef BLAS_LOCK_DEFINED
482 #error "blas_lock() implementation is missing for your platform"
487 #include "common_linux.h"
490 #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
493 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANON)
495 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
499 /* C99 supports complex floating numbers natively, which GCC also offers as an
500 extension since version 3.0. If neither are available, use a compatible
501 structure as fallback (see Clause 6.2.5.13 of the C99 standard). */
502 #if ((defined(__STDC_IEC_559_COMPLEX__) || __STDC_VERSION__ >= 199901L || \
503 (__GNUC__ >= 3 && !defined(__cplusplus))) && !(defined(FORCE_OPENBLAS_COMPLEX_STRUCT))) && !defined(_MSC_VER)
504 #define OPENBLAS_COMPLEX_C99
508 typedef float _Complex openblas_complex_float;
509 typedef double _Complex openblas_complex_double;
510 typedef xdouble _Complex openblas_complex_xdouble;
511 #define openblas_make_complex_float(real, imag) ((real) + ((imag) * _Complex_I))
512 #define openblas_make_complex_double(real, imag) ((real) + ((imag) * _Complex_I))
513 #define openblas_make_complex_xdouble(real, imag) ((real) + ((imag) * _Complex_I))
515 #define OPENBLAS_COMPLEX_STRUCT
516 typedef struct { float real, imag; } openblas_complex_float;
517 typedef struct { double real, imag; } openblas_complex_double;
518 typedef struct { xdouble real, imag; } openblas_complex_xdouble;
519 #define openblas_make_complex_float(real, imag) {(real), (imag)}
520 #define openblas_make_complex_double(real, imag) {(real), (imag)}
521 #define openblas_make_complex_xdouble(real, imag) {(real), (imag)}
526 #include "common_param.h"
529 #define STDERR stderr
533 #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
536 #if defined(XDOUBLE) || defined(DOUBLE)
537 #define FLOATRET FLOAT
540 #define FLOATRET double
542 #define FLOATRET float
548 /* Inclusion of a standard header file is needed for definition of __STDC_*
549 predefined macros with some compilers (e.g. GCC 4.7 on Linux). This occurs
550 as a side effect of including either <features.h> or <stdc-predef.h>. */
555 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_xdouble
556 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_xdouble(r,i)
557 #elif defined(DOUBLE)
558 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_double
559 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_double(r,i)
561 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_float
562 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_float(r,i)
565 #if defined(C_PGI) || defined(C_SUN)
566 #if defined(__STDC_IEC_559_COMPLEX__)
567 #define CREAL(X) creal(X)
568 #define CIMAG(X) cimag(X)
570 #define CREAL(X) (*((FLOAT *)&X + 0))
571 #define CIMAG(X) (*((FLOAT *)&X + 1))
574 #ifdef OPENBLAS_COMPLEX_STRUCT
575 #define CREAL(Z) ((Z).real)
576 #define CIMAG(Z) ((Z).imag)
578 #define CREAL __real__
579 #define CIMAG __imag__
593 #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
600 #define inline __inline
606 #define MIN(a,b) (a>b? b:a)
610 #define MAX(a,b) (a<b? b:a)
613 #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
615 #if defined(__FreeBSD__) || defined(__APPLE__)
616 #define MAP_ANONYMOUS MAP_ANON
619 /* Common Memory Management Routine */
620 void blas_set_parameter(void);
621 int blas_get_cpu_number(void);
622 void *blas_memory_alloc (int);
623 void blas_memory_free (void *);
624 void *blas_memory_alloc_nolock (int); //use malloc without blas_lock
625 void blas_memory_free_nolock (void *);
627 int get_num_procs (void);
629 #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
630 int get_num_nodes (void);
631 int get_num_proc (int);
632 int get_node_equal (void);
635 void goto_set_num_threads(int);
637 void gotoblas_affinity_init(void);
638 void gotoblas_affinity_quit(void);
639 void gotoblas_dynamic_init(void);
640 void gotoblas_dynamic_quit(void);
641 void gotoblas_profile_init(void);
642 void gotoblas_profile_quit(void);
647 int omp_in_parallel(void);
648 int omp_get_num_procs(void);
650 __declspec(dllimport) int __cdecl omp_in_parallel(void);
651 __declspec(dllimport) int __cdecl omp_get_num_procs(void);
654 #if (__STDC_VERSION__ >= 201112L)
655 #if defined(C_GCC) && ( __GNUC__ < 7)
656 // workaround for GCC bug 65467
658 #define _Atomic volatile
661 #include <stdatomic.h>
664 #define _Atomic volatile
670 int omp_in_parallel (void) __attribute__ ((weak));
671 int omp_get_num_procs(void) __attribute__ ((weak));
675 static __inline void blas_unlock(volatile BLASULONG *address){
680 #ifdef OS_WINDOWSSTORE
681 static __inline int readenv_atoi(char *env) {
686 static __inline int readenv_atoi(char *env) {
688 return readenv(p,env) ? 0 : atoi(p);
691 static __inline int readenv_atoi(char *env) {
693 if (( p = getenv(env) ))
701 #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
703 static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
710 (fabsl(ar)) >= (fabsl(ai))
712 (fabs (ar)) >= (fabs (ai))
714 (fabsf(ar)) >= (fabsf(ai))
718 den = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
723 den = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
738 void *blas_debug_alloc(int);
739 void *blas_debug_free(void *);
742 #define malloc(a) blas_debug_alloc(a)
743 #define free(a) blas_debug_free (a)
747 #define GEMMRETTYPE int
757 #define GEMMRETTYPE copyoverhead_t
762 #define KNAME(A, B) A
764 #define KNAME(A, B) A##B
767 #include "common_interface.h"
769 #include "common_reference.h"
771 #include "common_macro.h"
772 #include "common_level1.h"
773 #include "common_level2.h"
774 #include "common_level3.h"
775 #include "common_lapack.h"
778 # define OPENBLAS_CONST /* see comment in cblas.h */
783 #include "common_stackalloc.h"
788 #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
789 #error Both SMP_SERVER and SMP_ONDEMAND are specified.
792 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
793 #include "common_thread.h"
800 #ifndef DEFAULT_CPU_NUMBER
801 #define DEFAULT_CPU_NUMBER 4
812 #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
816 unsigned long long calls, fops, area, cycles, tcycles;
819 extern func_profile_t function_profile_table[];
820 extern int gotoblas_profile;
823 #define NUMOPT QNUMOPT
825 #define NUMOPT DNUMOPT
827 #define NUMOPT SNUMOPT
830 #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
832 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
833 if (gotoblas_profile) { \
834 profile_end = rpcc(); \
835 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
836 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
837 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
838 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
839 function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
843 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
844 if (gotoblas_profile) { \
845 profile_end = rpcc(); \
846 function_profile_table[PROFILE_FUNC_NAME].calls ++; \
847 function_profile_table[PROFILE_FUNC_NAME].area += SIZE * COMPSIZE * (AREA); \
848 function_profile_table[PROFILE_FUNC_NAME].fops += (COMP) * (OPS) / NUMOPT; \
849 function_profile_table[PROFILE_FUNC_NAME].cycles += (profile_end - profile_start); \
850 function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
856 #define FUNCTION_PROFILE_START()
857 #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
861 #define PRINT_DEBUG_CNAME
862 #define PRINT_DEBUG_NAME
864 #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
865 #define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
871 #endif /* __cplusplus */