Skip to content

feat: provide a way for the user to limit number of threads #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 21, 2025
Merged
1 change: 1 addition & 0 deletions doc/changelog.d/508.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
provide a way for the user to limit number of threads
14 changes: 13 additions & 1 deletion src/ansys/speos/core/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,27 @@ def set_source_paths(self, source_paths: List[str]) -> BaseSimulation:
# self._simulation_instance.geometries.geo_paths[:] = geo_paths
# return self

def compute_CPU(self) -> List[job_pb2.Result]:
def compute_CPU(self, threads_number: Optional[int] = None) -> List[job_pb2.Result]:
"""Compute the simulation on CPU.

Parameters
----------
threads_number : int, optional
The number of threads used.
By default, ``None``, means the number of processor available.

Returns
-------
List[ansys.api.speos.job.v2.job_pb2.Result]
List of simulation results.
"""
self._job.job_type = ProtoJob.Type.CPU

if threads_number is not None:
self._simulation_template.metadata["SimulationSetting::OPTThreadNumber"] = (
"int::" + str(threads_number)
)

self.result_list = self._run_job()
return self.result_list

Expand Down
30 changes: 30 additions & 0 deletions tests/core/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ def test_from_file(speos: Speos):
assert ssr_data.irradiance_sensor_template.dimensions.x_sampling == 500


def test_from_file_threads_limited(speos: Speos):
"""Test create a project from file."""
# Create a project from a file
p = Project(
speos=speos,
path=str(
Path(test_path) / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5"
),
)

# Check that scene is filled
assert len(p.scene_link.get().materials) == 4
assert len(p.scene_link.get().sensors) == 1
assert len(p.scene_link.get().sources) == 2
assert len(p.scene_link.get().simulations) == 1

feat_sims = p.find(name=p.scene_link.get().simulations[0].name)
assert len(feat_sims) == 1
assert type(feat_sims[0]) is SimulationDirect

# Choose number of threads
threads_nb = 8

# Compute on CPU with the amount of threads selected
feat_sims[0].compute_CPU(threads_number=threads_nb)
assert feat_sims[0].simulation_template_link.get().metadata[
"SimulationSetting::OPTThreadNumber"
] == "int::" + str(threads_nb)


def test_find_geom(speos: Speos):
"""Test find geometry feature in a project."""
# Create a project from a file
Expand Down