Skip to content

Commit d66d6c6

Browse files
committed
auto set cpu environment in V2 API
1 parent a6f5f6e commit d66d6c6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

python/paddle/v2/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ def init(**kwargs):
7878
for key in args_dict.keys():
7979
args.append('--%s=%s' % (key, str(args_dict[key])))
8080

81+
# auto set cpu environment
82+
def set_env(key, value):
83+
'''If the key has not been set in the environment, set it with value.'''
84+
assert isinstance(key, str)
85+
assert isinstance(value, str)
86+
envset = os.environ.get(key)
87+
if envset is None:
88+
os.environ[key] = value
89+
90+
ht = os.popen("lscpu |grep \"per core\"|awk -F':' '{print $2}'|xargs")
91+
ht = int(ht.read())
92+
if ht == 1: # ht is off
93+
set_env("OMP_DYNAMIC", "false")
94+
set_env("KMP_AFFINITY", "granularity=fine,compact,0,0")
95+
else:
96+
set_env("OMP_DYNAMIC", "true")
97+
set_env("KMP_AFFINITY", "granularity=fine,compact,1,0")
98+
processors = os.popen("grep \"processor\" /proc/cpuinfo|sort -u|wc -l")
99+
processors = int(processors.read())
100+
trainers = kwargs.get('trainer_count', 1)
101+
threads = processors / trainers
102+
threads = '1' if threads < 1 else str(threads)
103+
set_env("OMP_NUM_THREADS", threads)
104+
set_env("MKL_NUM_THREADS", threads)
105+
81106
if 'use_gpu' in kwargs:
82107
cp.g_command_config_args['use_gpu'] = kwargs['use_gpu']
83108
if 'use_mkldnn' in kwargs:

0 commit comments

Comments
 (0)