62
62
cp .begin_parse ()
63
63
64
64
65
- def init ( ** kwargs ):
66
- import py_paddle . swig_paddle as api
67
- args = []
68
- args_dict = {}
69
- # NOTE: append arguments if they are in ENV
70
- for ek , ev in os . environ . iteritems ():
71
- if ek . startswith ( "PADDLE_INIT_" ) :
72
- args_dict [ ek . replace ( "PADDLE_INIT_" , "" ). lower ()] = str ( ev )
65
+ def set_omp_mkl_env_vars ( trainer_count ):
66
+ '''Auto set CPU environment if have not set before.
67
+ export KMP_AFFINITY, OMP_DYNAMIC according to the Hyper Threading status.
68
+ export OMP_NUM_THREADS, MKL_NUM_THREADS according to trainer_count.
69
+ '''
70
+ import platform
71
+ if not platform . system () in [ 'Linux' , 'Darwin' ] :
72
+ return
73
73
74
- args_dict .update (kwargs )
75
- # NOTE: overwrite arguments from ENV if it is in kwargs
76
- for key in args_dict .keys ():
77
- args .append ('--%s=%s' % (key , str (args_dict [key ])))
78
-
79
- # auto set cpu environment
80
74
def set_env (key , value ):
81
75
'''If the key has not been set in the environment, set it with value.'''
82
76
assert isinstance (key , str )
@@ -85,22 +79,59 @@ def set_env(key, value):
85
79
if envset is None :
86
80
os .environ [key ] = value
87
81
88
- ht = os .popen ("lscpu |grep \" per core\" |awk -F':' '{print $2}'|xargs" )
89
- ht = int (ht .read ())
90
- if ht == 1 : # ht is off
91
- set_env ("OMP_DYNAMIC" , "false" )
92
- set_env ("KMP_AFFINITY" , "granularity=fine,compact,0,0" )
93
- else :
82
+ def num_physical_cores ():
83
+ '''Get the number of physical cores'''
84
+ if platform .system () == "Linux" :
85
+ num_sockets = int (
86
+ os .popen ("lscpu |grep \" Socket\" |awk -F':' '{print $2}'|xargs" )
87
+ .read ())
88
+ num_cores_per_socket = int (
89
+ os .popen (
90
+ "lscpu |grep \" per socket\" |awk -F':' '{print $2}'|xargs" )
91
+ .read ())
92
+ return num_sockets * num_cores_per_socket
93
+ else :
94
+ cmds = {"Darwin" : "sysctl hw.physicalcpu" }
95
+ return int (os .popen (cmds .get (platform .system (), "expr 1" )).read ())
96
+
97
+ def num_logical_processors ():
98
+ '''Get the number of logical processors'''
99
+ cmds = {
100
+ "Linux" : "grep \" processor\" /proc/cpuinfo|sort -u|wc -l" ,
101
+ "Darwin" : "sysctl hw.logicalcpu"
102
+ }
103
+ return int (os .popen (cmds .get (platform .system (), "expr 1" )).read ())
104
+
105
+ num_cores = num_physical_cores ()
106
+ num_processors = num_logical_processors ()
107
+ if num_processors > num_cores : # Hyper Threading is enabled
94
108
set_env ("OMP_DYNAMIC" , "true" )
95
109
set_env ("KMP_AFFINITY" , "granularity=fine,compact,1,0" )
96
- processors = os . popen ( "grep \" processor \" /proc/cpuinfo|sort -u|wc -l" )
97
- processors = int ( processors . read () )
98
- trainers = kwargs . get ( 'trainer_count' , 1 )
99
- threads = processors / trainers
110
+ else :
111
+ set_env ( "OMP_DYNAMIC" , "false" )
112
+ set_env ( "KMP_AFFINITY" , "granularity=fine,compact,0,0" )
113
+ threads = num_processors / trainer_count
100
114
threads = '1' if threads < 1 else str (threads )
101
115
set_env ("OMP_NUM_THREADS" , threads )
102
116
set_env ("MKL_NUM_THREADS" , threads )
103
117
118
+
119
+ def init (** kwargs ):
120
+ import py_paddle .swig_paddle as api
121
+ args = []
122
+ args_dict = {}
123
+ # NOTE: append arguments if they are in ENV
124
+ for ek , ev in os .environ .iteritems ():
125
+ if ek .startswith ("PADDLE_INIT_" ):
126
+ args_dict [ek .replace ("PADDLE_INIT_" , "" ).lower ()] = str (ev )
127
+
128
+ args_dict .update (kwargs )
129
+ # NOTE: overwrite arguments from ENV if it is in kwargs
130
+ for key in args_dict .keys ():
131
+ args .append ('--%s=%s' % (key , str (args_dict [key ])))
132
+
133
+ set_omp_mkl_env_vars (kwargs .get ('trainer_count' , 1 ))
134
+
104
135
if 'use_gpu' in kwargs :
105
136
cp .g_command_config_args ['use_gpu' ] = kwargs ['use_gpu' ]
106
137
if 'use_mkldnn' in kwargs :
0 commit comments