Skip to content

Commit

Permalink
add multiprocessing and progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobEliasWagner committed Jan 16, 2024
1 parent 034791d commit 423a34e
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/data/helmholtz/helmholtz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import multiprocessing as mp
import pathlib

from src.utility import ProgressMonitor

from .domain_properties import read_config
from .solver import HelmholtzSolver

Expand All @@ -24,9 +27,32 @@ def __init__(self, problem_description_file: pathlib.Path, out_dir: pathlib.Path
self.descriptions = read_config(problem_description_file)
self.out_dir = out_dir

def run(self):
solver = HelmholtzSolver(self.out_dir, ("Lagrange", 3))
def run(self, n_threads: int = 1):
"""Generates the dataset for the current description in parallel.
:param n_threads:
:return:
"""
self.out_dir.mkdir(parents=True, exist_ok=True)
for description in self.descriptions:
description.save_to_json(self.out_dir)
solver(description)

# setup multi-processing
pool = mp.Pool(processes=n_threads)
manager = mp.Manager()
queue = manager.Queue()

args = [(i, description, queue) for i, description in enumerate(self.descriptions)]
result = pool.map_async(self.run_single_description, args)

# monitoring loop
ProgressMonitor.monitor_pool(
result, queue, len(self.descriptions), prefix="Helmholtz: ", suffix=f"Running on {n_threads} threads."
)

def run_single_description(self, args):
i, description, queue = args
description.save_to_json(self.out_dir)
solver = HelmholtzSolver(self.out_dir, ("Lagrange", 3))
solver(description)

# update queue
queue.put(i)

0 comments on commit 423a34e

Please sign in to comment.