Skip to content

Threads and memory

import BEAM
BEAM.set_threads(4) # at most 4 threads, for the whole process
BEAM.threads() # the limit in force (0: the system's choice)
BEAM.effective_threads() # what a parallel step would start right now

By default BEAM picks the width itself, from how busy the machine is and from where its own work stops scaling — on a quiet workstation that is up to eight threads, and on a shared node with little free it is far fewer, which is the difference between a job that runs and one that thrashes. OMP_NUM_THREADS, if you set it, decides instead: BEAM treats it as your instruction and stands aside.

set_threads is a hard limit, which is what you want inside a job scheduler or next to another parallel step. It applies to every model in the process; set_threads(0) restores the default. Set it before starting work, not while another thread is computing.

BEAM.parallel_plan() # what the default resolved to, and from what

For a benchmark or a figure you want to reproduce exactly, pin the machine estimate with the BEAM_AVAILABLE_CORES environment variable; the plan then depends only on your own settings rather than on what else happened to be running. parallel_plan() reports the width and the inputs it came from, so a surprising thread count can be explained rather than guessed at.

Results do not depend on the number of threads. Every sum is taken in a fixed order, so one thread and sixteen give the same answer, bit for bit.

BEAM.set_memory_budget(8 << 30) # 8 GiB
BEAM.memory_budget() # bytes; the default is 4 GiB, 0 restores it

Before a large computation, BEAM checks what it needs against the budget and refuses with BudgetExceeded if it would not fit; the message gives the amount needed. Within the budget, the budget only changes how much runs at once, never the answer. Lengths computed in parallel share the budget, so more threads do not multiply memory use.

After its first analysis a model keeps a cache of up to a quarter of the budget (model.live_cache_bytes); model.drop_caches() releases it without changing any result.

Scoring and all analyses only read the model and are safe to run concurrently on one model. Methods that change the model in place (add, subtract, set_alpha, compact, drop_caches) are not: do not change a model while another thread is using it. Build separate models in parallel and combine them from one thread.

Start worker processes with spawn, for example multiprocessing.get_context("spawn").Pool(...), and call BEAM.set_threads(T) in each worker so that workers × T fits the machine.

Spawn rather than fork, because an OpenMP thread pool does not survive fork: only the thread that called it exists in the child, so the child waits at its first parallel computation instead of raising. BEAM warns in the child when it is forked from a process that had worker threads running, which is the signal to switch start methods — but spawn avoids it altogether.

The check is whether any native thread was running at the fork, not whether BEAM itself had started one, so another library’s threads can trigger it even when BEAM has not run. That is deliberate — forking a multithreaded process is the hazard whoever started the threads — and the warning is a RuntimeWarning, so it can be filtered by category. A fork from a single-threaded process is not warned about.

Models are not pickled: pass each worker the model’s file path and load it there with BeamModel.from_file.

import multiprocessing as mp
import BEAM
from BEAM import BeamModel
def score(args):
path, sequences = args
BEAM.set_threads(2)
return BeamModel.from_file(path).log_pseq_batch(sequences)
if __name__ == "__main__": # spawned workers re-import this file
with mp.get_context("spawn").Pool(4) as pool:
results = pool.map(score, [("donor_A.beam", batch) for batch in batches])