@@ -778,11 +778,11 @@ static int initialized = 0;
778778void gotoblas_affinity_init (void ) {
779779
780780 int cpu , num_avail ;
781- #ifndef USE_OPENMP
781+ #ifndef USE_OPENMP
782782 cpu_set_t cpu_mask ;
783783#endif
784784 int i ;
785-
785+
786786 if (initialized ) return ;
787787
788788 initialized = 1 ;
@@ -826,15 +826,54 @@ void gotoblas_affinity_init(void) {
826826 common -> shmid = pshmid ;
827827
828828 if (common -> magic != SH_MAGIC ) {
829+ cpu_set_t * cpusetp ;
830+ int nums ;
831+ int ret ;
832+
829833#ifdef DEBUG
830834 fprintf (stderr , "Shared Memory Initialization.\n" );
831835#endif
832836
833837 //returns the number of processors which are currently online
834- common -> num_procs = sysconf (_SC_NPROCESSORS_CONF );;
838+
839+ nums = sysconf (_SC_NPROCESSORS_CONF );
840+
841+ #if !defined(__GLIBC_PREREQ ) || !__GLIBC_PREREQ (2 , 3 )
842+ common -> num_procs = nums ;
843+ #elif __GLIBC_PREREQ (2 , 7 )
844+ cpusetp = CPU_ALLOC (nums );
845+ if (cpusetp == NULL ) {
846+ common -> num_procs = nums ;
847+ } else {
848+ size_t size ;
849+ size = CPU_ALLOC_SIZE (nums );
850+ ret = sched_getaffinity (0 ,size ,cpusetp );
851+ if (ret != 0 )
852+ common -> num_procs = nums ;
853+ else
854+ common -> num_procs = CPU_COUNT_S (size ,cpusetp );
855+ }
856+ CPU_FREE (cpusetp );
857+ #else
858+ ret = sched_getaffinity (0 ,sizeof (cpu_set_t ), cpusetp );
859+ if (ret != 0 ) {
860+ common -> num_procs = nums ;
861+ } else {
862+ #if !__GLIBC_PREREQ (2 , 6 )
863+ int i ;
864+ int n = 0 ;
865+ for (i = 0 ;i < nums ;i ++ )
866+ if (CPU_ISSET (i ,cpusetp )) n ++ ;
867+ common -> num_procs = n ;
868+ }
869+ #else
870+ common -> num_procs = CPU_COUNT (sizeof (cpu_set_t ),cpusetp );
871+ #endif
872+
873+ #endif
835874
836875 if(common -> num_procs > MAX_CPUS) {
837- fprintf (stderr , "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n" , common -> num_procs , MAX_CPUS );
876+ fprintf (stderr , "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n" , common -> num_procs , MAX_CPUS );
838877 exit (1 );
839878 }
840879
@@ -847,7 +886,7 @@ void gotoblas_affinity_init(void) {
847886 if (common -> num_nodes > 1 ) numa_mapping ();
848887
849888 common -> final_num_procs = 0 ;
850- for (i = 0 ; i < common -> avail_count ; i ++ ) common -> final_num_procs += rcount (common -> avail [i ]) + 1 ; //Make the max cpu number.
889+ for (i = 0 ; i < common -> avail_count ; i ++ ) common -> final_num_procs += rcount (common -> avail [i ]) + 1 ; //Make the max cpu number.
851890
852891 for (cpu = 0 ; cpu < common -> final_num_procs ; cpu ++ ) common -> cpu_use [cpu ] = 0 ;
853892
0 commit comments