MultiThreadAgentParallelRunner¶
- class pams.runners.MultiThreadAgentParallelRunner(settings, prng=None, logger=None, simulator_class=<class 'pams.simulator.Simulator'>)[source]¶
Multi Thread Agent Parallel runner class. This is experimental.
In this runner, only
pams.agents.Agent.submit_orders()of normal (non high-frequency) agents is parallelized in each step usingconcurrent.futures.ThreadPoolExecutor. Order handling, executions, high-frequency agents, events, and logging are processed sequentially on the main thread in the same way aspams.runners.SequentialRunner.The simulation results are identical to those of
pams.runners.SequentialRunnerwith the same settings and the same seed, because agents are asked to submit orders in the same order and the same number of agents are asked aspams.runners.SequentialRunnerdoes. Agents are asked in batches: at first,maxNormalOrdersagents are asked in parallel, and if some of them submit no orders, the same number of the next agents are asked in parallel, and so on, until the number of agents submitting orders reachesmaxNormalOrders. This means that the number of agents that can be parallelized is limited bymaxNormalOrdersof the session.The number of workers can be set by
simulation.numParallelin the config. The default is the number of CPUs minus one (at least one).Subclasses can customize the workers by overriding the following internal methods:
_create_executor: creates the executor._get_worker_initializerand_get_worker_initargs: a function called once on each worker before it runs any task, and its arguments._split_agents_into_chunks: splits the agents asked in parallel into the tasks of the executor.
Note
pams.agents.Agent.submit_orders()is called on worker threads concurrently. The built-in agents only read markets and use their own pseudo random number generators, which is thread-safe in this runner because the main thread waits for all the workers before handling orders. User-defined agents must not modify shared objects such as markets, the simulator, the logger, or other agents inpams.agents.Agent.submit_orders().pams.agents.Agent.submit_orders()is called on the agents themselves, not on copies, so their changes to their own attributes are kept, andpams.agents.Agent.synced_attributesis not used.Note
Because of the GIL of python, this runner does not speed up CPU-bound agents such as the built-in agents. This runner is beneficial when
pams.agents.Agent.submit_orders()waits for I/O (e.g., network access to external models or services).