Skip to content

Commit 7224022

Browse files
authored
Merge pull request #1239 from martin-frbg/cgroups
Honor cgroup/cpuset limits when enumerating cpus
2 parents 468ac3d + 80373ea commit 7224022

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

driver/others/init.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,11 @@ static int initialized = 0;
778778
void 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

driver/others/memory.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,44 @@ int get_num_procs(void);
175175
#else
176176
int get_num_procs(void) {
177177
static int nums = 0;
178+
cpu_set_t *cpusetp;
179+
size_t size;
180+
int ret;
181+
int i,n;
182+
178183
if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
184+
#if !defined(OS_LINUX)
185+
return nums;
186+
#endif
187+
188+
#if !defined(__GLIBC_PREREQ)
189+
return nums;
190+
#endif
191+
#if !__GLIBC_PREREQ(2, 3)
192+
return nums;
193+
#endif
194+
195+
#if !__GLIBC_PREREQ(2, 7)
196+
ret = sched_getaffinity(0,sizeof(cpu_set_t), cpusetp);
197+
if (ret!=0) return nums;
198+
n=0;
199+
#if !__GLIBC_PREREQ(2, 6)
200+
for (i=0;i<nums;i++)
201+
if (CPU_ISSET(i,cpusetp)) n++;
202+
nums=n;
203+
#else
204+
nums = CPU_COUNT(sizeof(cpu_set_t),cpusetp);
205+
#endif
206+
return nums;
207+
#endif
208+
209+
cpusetp = CPU_ALLOC(nums);
210+
if (cpusetp == NULL) return nums;
211+
size = CPU_ALLOC_SIZE(nums);
212+
ret = sched_getaffinity(0,size,cpusetp);
213+
if (ret!=0) return nums;
214+
nums = CPU_COUNT_S(size,cpusetp);
215+
CPU_FREE(cpusetp);
179216
return nums;
180217
}
181218
#endif

0 commit comments

Comments
 (0)