Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-detect no. of available cores with -j 0
Closes #322
- Loading branch information
Showing
6 changed files
with
61 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import multiprocessing | ||
import re | ||
|
||
|
||
def available_cpu_count(): | ||
""" | ||
Return the number of available virtual or physical CPUs on this system. | ||
The number of available CPUs can be smaller than the total number of CPUs | ||
when the cpuset(7) mechanism is in use, as is the case on some cluster | ||
systems. | ||
Adapted from http://stackoverflow.com/a/1006301/715090 | ||
""" | ||
try: | ||
with open('/proc/self/status') as f: | ||
status = f.read() | ||
m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$', status) | ||
if m: | ||
res = bin(int(m.group(1).replace(',', ''), 16)).count('1') | ||
if res > 0: | ||
return min(res, multiprocessing.cpu_count()) | ||
except IOError: | ||
pass | ||
|
||
return multiprocessing.cpu_count() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters