Python 如何将任务分配给特定的CPU核心?
我在Python 如何将任务分配给特定的CPU核心?,python,multithreading,tensorflow,concurrency,Python,Multithreading,Tensorflow,Concurrency,我在tensorflow中阅读了distributed devices的资料,其中指出培训可以分配给特定的CPU核心 我们能否将任务/线程分配给CPU核心以实现并发或并行处理 with tf.device("/job:ps/task:0"): weights_1 = tf.Variable(...) biases_1 = tf.Variable(...) with tf.device("/job:ps/task:1"): weights_2 = tf.Variable(...)
tensorflow
中阅读了distributed devices
的资料,其中指出培训可以分配给特定的CPU核心
我们能否将任务/线程分配给CPU核心以实现并发或并行处理
with tf.device("/job:ps/task:0"):
weights_1 = tf.Variable(...)
biases_1 = tf.Variable(...)
with tf.device("/job:ps/task:1"):
weights_2 = tf.Variable(...)
biases_2 = tf.Variable(...)
您可以获取python进程的当前pid,并使用第三方实用程序将其分配给CPU核心
我对tensorflow不太了解,但我认为GIL将在这里发挥作用。您必须使用多处理,并将每个进程分配给不同的内核。您可以将进程的特定线程绑定到任意内核(假设您使用的是linux)。这不仅适用于python,而且适用于任何进程。我制作了一个python脚本来演示如何做到这一点 您可以通过
ps
命令获取线程ID:
[user@dev~]$ps-Lo pid,%cpu,lwp-p{pid}
我的输出:
PID %CPU LWP
28216 98.0 28216
28216 0.0 28217
28216 0.0 28218
这里28216是进程的PID,而您可以看到其他线程在一个简单的python脚本中运行
现在,您可以通过taskset
taskset -cp 0-5 28218
它将显示以下输出:
pid 28218's current affinity list: 0-11
pid 28218's new affinity list: 0-5
然后,您可以观察到一些线程绑定到不同的CPU集:
[user@host ~]$ taskset -cp 28218
pid 28218's current affinity list: 0-5
[user@host ~]$ taskset -cp 28217
pid 28217's current affinity list: 0-11
您真的需要将其分配给特定的核心吗?或者任何人都能胜任?谢谢你,拉菲。我来看看。