@@ -78,6 +78,31 @@ def init(**kwargs):
78
78
for key in args_dict .keys ():
79
79
args .append ('--%s=%s' % (key , str (args_dict [key ])))
80
80
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
+
81
106
if 'use_gpu' in kwargs :
82
107
cp .g_command_config_args ['use_gpu' ] = kwargs ['use_gpu' ]
83
108
if 'use_mkldnn' in kwargs :
0 commit comments