Skip to content

Performance of get/set field data #2201

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

Open
3 tasks done
FedericoNegri opened this issue Apr 1, 2025 · 1 comment · May be fixed by #2249
Open
3 tasks done

Performance of get/set field data #2201

FedericoNegri opened this issue Apr 1, 2025 · 1 comment · May be fixed by #2249
Labels
bug Something isn't working

Comments

@FedericoNegri
Copy link
Contributor

FedericoNegri commented Apr 1, 2025

Before submitting the issue

  • I have checked for Compatibility issues
  • I have searched among the existing issues
  • I am using a Python virtual environment

Description of the bug

Some recent changes (f7be394?) seem to have slowed down manipulating field data (by orders of magnitude!).

Steps To Reproduce

This is a minimal script to reproduce the issue. Using uv I could easily run it with different "versions" of PyDPF-Core and compare timings.

Using the latest released version (0.13.6) it takes ~20ms to run. With current main it instead takes >5s.

Note: I'm aware the operations tested here could be done in a different way, by setting the field data and scoping outside of the loop. But still, I feel like this could indicate some bigger problem.

# /// script
# requires-python = "==3.12.9"
# dependencies = [
#     "numpy==2.2.4",
#     "ansys-dpf-core==0.13.6", #fast
#     #"ansys-dpf-core @ git+https://[email protected]/ansys/pydpf-core.git@e2a1cf0caa01d3376e956cbea026590a40ccbf48", #fast
#     #"ansys-dpf-core @ git+https://[email protected]/ansys/pydpf-core.git@f7be394515266766b6208ef4f341a5fdfec558db", #slow
# ]
# ///
"""
Script to measure performance of creating a DPF field.

How to:

1. Install uv if not already installed:
    pip install uv
   See https://docs.astral.sh/uv/getting-started/installation/#standalone-installer

2. Save this code as, say, `create_field.py` and run it:
    uv run create_field.py

3. Change the ansys-dpf-core version in the script header and re-run the script:
    uv run create_field.py
"""

import time
import numpy as np
from ansys.dpf.core import fields_factory

from ansys.dpf import core as dpf

server = dpf.start_local_server(
    context=dpf.AvailableServerContexts.premium,
    config=dpf.AvailableServerConfigs.InProcessServer,
    as_global=True,
)

num_entities = int(1e+6)
field = fields_factory.create_scalar_field(
    num_entities=num_entities, location=dpf.locations.elemental, server=server
)
field.name = "my_field"
field.data = np.zeros(num_entities, dtype=np.int32)
field.scoping.ids = np.zeros(num_entities, dtype=np.int32)

all_indices = np.arange(num_entities)
chunks = np.array_split(all_indices, 200)

start_time = time.perf_counter()
for index, chunk in enumerate(chunks):

    field.data[chunk] = int(index)
    field.scoping.ids[chunk] = chunk

print(
    f"Filling DPF field done in {(time.perf_counter() - start_time):.3f} s"
)

Which Operating System causes the issue?

Windows

Which DPF/Ansys version are you using?

DPF Server 2025.2.pre0

Which Python version causes the issue?

3.12

Installed packages

See script above

@FedericoNegri FedericoNegri added the bug Something isn't working label Apr 1, 2025
@FedericoNegri
Copy link
Contributor Author

Adding an extra step to that script

for i in range(10):
    op = dpf.operators.math.scale(
        field=field,
        weights=1.5,
    )
    op.run()

    start_time = time.perf_counter()

    _ = op.outputs.field().data

    print(
        f"Getting the data in {(time.perf_counter() - start_time)*1000:.3f} ms"
    )

shows an average time to get the field data of 80ms, compared to ~3ms before!!

@cbellot000 @PProfizi @rafacanton can you please take a look?

@cbellot000 cbellot000 linked a pull request Apr 30, 2025 that will close this issue
@cbellot000 cbellot000 linked a pull request Apr 30, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant