-
Notifications
You must be signed in to change notification settings - Fork 26
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
Speed up simulations #232
base: main
Are you sure you want to change the base?
Speed up simulations #232
Conversation
Performance testingUsing the following script to test before/after, testing different crystals (hexagonal, cubic and triclinic) and both small and large unit cells (to decrease/increase the number of reflections - which should be where the greatest performance gain is realized): from diffsims.generators.simulation_generator import SimulationGenerator
from orix.crystal_map import Phase
from orix.quaternion import Rotation
from diffpy.structure import Lattice, Atom, Structure
import numpy as np
import timeit
gold = [Atom("Au", [0, 0, 0])]
hexagonal = Phase("test", 161, structure=Structure(gold,
Lattice(4, 4, 5, 90, 90, 120)
))
large_hexagonal = Phase("test", 161, structure=Structure(gold,
Lattice(20, 20, 25, 90, 90, 120)
))
cubic = Phase("test", 221, structure=Structure(gold,
Lattice(4, 4, 4, 90, 90, 90)
))
large_cubic = Phase("test", 221, structure=Structure(gold,
Lattice(20, 20, 20, 90, 90, 90)
))
triclinic = Phase("test", 1, structure=Structure(gold,
Lattice(4, 5, 6, 80, 90, 130)
))
large_triclinic = Phase("test", 1, structure=Structure(gold,
Lattice(20, 25, 30, 80, 90, 130)
))
gen = SimulationGenerator()
from numpy import random
random.seed(0)
rot = Rotation.random(1000)
kwargs = {
"rotation": rot,
"with_direct_beam": False,
"reciprocal_radius": 5,
}
res = timeit.repeat(
"gen.calculate_diffraction2d(hexagonal, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'hexagonal' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s")
res = timeit.repeat(
"gen.calculate_diffraction2d(cubic, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'cubic' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s")
res = timeit.repeat(
"gen.calculate_diffraction2d(triclinic, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'triclinic' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s")
kwargs["reciprocal_radius"] = 2 # we still get a huge number of reflections
res = timeit.repeat(
"gen.calculate_diffraction2d(large_hexagonal, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'large_hexagonal' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s")
res = timeit.repeat(
"gen.calculate_diffraction2d(large_cubic, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'large_cubic' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s")
res = timeit.repeat(
"gen.calculate_diffraction2d(large_triclinic, **kwargs)",
globals=globals(),
number=2,
)
print(f"{'large_triclinic' :<18}: {np.mean(res) :.2f} ± {np.std(res) :.2f} s") Running on current main branch of diffsims and orix:
And with the full changes in this branch + Orix:
So roughly 2-3x speedup. Not sure what I did to get the 5-6x speedup I saw before, probably some mistake. When adding precession, the speedup is negligible. It might even be slower. Below is the script run with 1 degree precession, run on a different computer than above, so with/without precession times are not comparable.
This branch:
|
Description of the change
Speed up diffraction simulation.
With the new RotatedPhase class and pyxem/orix#533, I get around 6x speedup with a simple cubic crystal.
I want to look into SimulationGenerator.get_intersecting_reflections too, maybe we can rotate Ewald's sphere instead of the lattice to reduce computation.
Additionally, iterating over the rotations should be possible to parallelize.
@CSSFrancis maybe you have looked at these things already?
Progress of the PR
For reviewers
__init__.py
.unreleased section in
CHANGELOG.rst
.credits
indiffsims/release_info.py
andin
.zenodo.json
.