I have recently faced a problem of running something on multiple threads/cores. My setup: (Cpython with GIL) python 3.6.3 (anaconda) OS: windows 10 CPU: i7 8700 (6 cores/12 threads) GPU: 1080, 1070 tensorflow==1.8.0 tensorflow-gpu==1.8.0 keras==2.1.5
And there is no bottleneck for sure. RAM usage 6/24 GB Disk usage: 0%
The problem is that threading module seems to use only half of my cores/threads according to task manager, which shows CPU load of only 50% instead of 100.
Here is my code
class Environment(Thread):
stop_signal = False
def __init__(self, testing=False, eps_start=EPS_START, eps_end=EPS_STOP, eps_steps=EPS_STEPS):
Thread.__init__(self)
self.testing = testing
self.env = Market(1000, train_data, testing=testing)
self.agent = Agent(eps_start, eps_end, eps_steps)
def runEpisode(self):
s = self.env.reset()
R = 0
done = False
while not done:
time.sleep(THREAD_DELAY) # yield
a = self.agent.act(s)
s_, r, done, info = self.env.step(a)
if done: # terminal state
s_ = None
self.agent.train(s, a, r, s_)
s = s_
R += r
print("Total reward:", R)
def run(self):
while not self.stop_signal:
self.runEpisode()
if self.testing: break
def stop(self):
self.stop_signal = True
class Optimizer(Thread):
stop_signal = False
def __init__(self):
Thread.__init__(self)
def run(self):
while not self.stop_signal:
brain.optimize()
def stop(self):
self.stop_signal = True
if __name__ == '__main__':
#-- main
env_test = Environment(testing=True, eps_start=0., eps_end=0.)
NUM_STATE = env_test.env.observation_space.shape[0]
NUM_ACTIONS = env_test.env.action_space.n
NONE_STATE = np.zeros(NUM_STATE)
brain = Brain() # brain is global in A3C
envs = [Environment() for i in range(THREADS)]
opts = [Optimizer() for i in range(OPTIMIZERS)]
start_time = time.time()
for o in opts:
o.start()
for e in envs:
e.start()
time.sleep(RUN_TIME)
for e in envs:
e.stop()
for e in envs:
e.join()
for o in opts:
o.stop()
for o in opts:
o.join()
print("Training finished in ", time.time() - start_time)
brain.model.save('dense.h5')