Skip to content

Commit ae93532

Browse files
authored
Merge pull request #1249 from martin-frbg/cgroup
Honor cgroup/cpuset limits when enumerating cpus
2 parents 480e697 + c4af196 commit ae93532

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

driver/others/init.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,15 +826,58 @@ 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);;
835838

839+
nums = sysconf(_SC_NPROCESSORS_CONF);
840+
841+
#if !defined(__GLIBC_PREREQ)
842+
common->num_procs = nums;
843+
#else
844+
845+
#if !__GLIBC_PREREQ(2, 3)
846+
common->num_procs = nums;
847+
#elif __GLIBC_PREREQ(2, 7)
848+
cpusetp = CPU_ALLOC(nums);
849+
if (cpusetp == NULL) {
850+
common->num_procs = nums;
851+
} else {
852+
size_t size;
853+
size = CPU_ALLOC_SIZE(nums);
854+
ret = sched_getaffinity(0,size,cpusetp);
855+
if (ret!=0)
856+
common->num_procs = nums;
857+
else
858+
common->num_procs = CPU_COUNT_S(size,cpusetp);
859+
}
860+
CPU_FREE(cpusetp);
861+
#else
862+
ret = sched_getaffinity(0,sizeof(cpu_set_t), cpusetp);
863+
if (ret!=0) {
864+
common->num_procs = nums;
865+
} else {
866+
#if !__GLIBC_PREREQ(2, 6)
867+
int i;
868+
int n = 0;
869+
for (i=0;i<nums;i++)
870+
if (CPU_ISSET(i,cpusetp)) n++;
871+
common->num_procs = n;
872+
}
873+
#else
874+
common->num_procs = CPU_COUNT(sizeof(cpu_set_t),cpusetp);
875+
#endif
876+
877+
#endif
878+
#endif
836879
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);
880+
fprintf(stderr, "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n", common->num_procs, MAX_CPUS);
838881
exit(1);
839882
}
840883

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)