OpenSees Cloud
OpenSees AMI
How to Profile an OpenSeesPy Analysis
Original Post - 22 Dec 2024 - Michael H. Scott
Visit Structural Analysis Is Simple on Substack.
Python has a couple of profiling libraries–pyinstrument
and
cProfile
–for finding out where all the time goes when you run a script.
But, as far as I can tell, these libraries only tell you that the
ops.analyze()
command is called, not what happens therein. What you
really want is to drill down into the state determinations and equation
solving required for your IDA of 3D reinforced concrete moment frame
models.
To profile the source code of OpenSees, not just the Python commands
that wrap the source code, you can use the perf
command and profiler. I
don’t want to get into all the details of using perf
, but you can find
more information from the following sites, upon which most of this post
is based:
- Profiling in Python
- Python 3.12 Support For the Linux perf Profiler
- How to Use the perf Command and Profiler
Once you are ready to profile an OpenSeesPy analysis, you’ll need the following:
- Linux operating system
-
sudo
access - Python 3.12 (or later)
-
perf
profiler installed
If you don’t meet all of these requirements, you can still get by, even if you don’t have Linux. My preferred approach is to use a virtual machine on AWS. And although I haven’t tried it, you should be able to use WSL on a Windows machine.
Virtual Machine
Hop on Amazon EC2
and launch a virtual machine with the Ubuntu 24.04
image, which comes with Python 3.12 pre-installed. I recommend a
t3.xlarge
instance
because you’ll need a decent amount of RAM.
After connecting to your instance, update the distribution.
$ sudo apt update
Next, install python3-pip
for package installation and python3-venv
for
managing virtual environments.
$ sudo apt install python3-pip python3-venv
The perf
command should be pre-installed on the Ubuntu 24.04 image. You
can check by running the following command
$ perf --version
If the perf
command is not found, install the linux-tools
libraries.
$ sudo apt install linux-tools-common linux-tools-generic
With the libraries installed, whether on your personal device or on a virtual machine, you can now start to profile.
Install OpenSeesPy in a Virtual Environment
To manage dependencies, e.g., if you want to profile an older version of
either OpenSeesPy or another package, create a virtual environment. The
following command creates a virtual environment in the my-venv
directory.
$ python3 -m venv my-venv
Activate the virtual environment by running the activate.sh
script in
the my-venv/bin/
directory.
$ source my-venv/bin/activate
Then install OpenSeesPy in the virtual environment.
(my-venv)$ my-venv/bin/python3 -m pip install openseespy
In the virtual environment, install any other Python libraries, e.g.,
numpy
, called by the script you plan to profile.
Example 1
The first example is a single 3D forceBeamColumn
element with 10
integration points and 10,000 Steel01
fibers per section. Although there
are 100,000 fibers in the model–the most you can have with one
element–there are only six equations of nodal equilibrium. Accordingly,
the run-time should be dominated by state determination, not equation
solving.
To generate a profile of the analysis, run the perf record
sub-command
with maximum sample frequency in your virtual environment.
(my-venv)$ sudo perf record --freq max -g my-venv/bin/python3 -X perf example1.py
After the analysis is complete, generate the report from the raw
perf.data
file.
(my-venv)$ sudo perf report -g -i perf.data
The report will be shown in the terminal.
The report shows the majority of time is spent in fiber section state determination and material stress-strain response. In the report explorer, you can drill down into each method to obtain more information.
Example 2
The second example is also a 3D cantilever, but with 100 dispBeamColumn
elements with two integration points per element and 36 Steel01
fibers
per section. In addition, the analysis uses a slow equation solver
(FullGeneral
) so that linear algebra–not state determination–will
dominate the run-time.
The report from this analysis is shown below.
In this case, the majority of time is spent in LAPACK subroutines,
particularly dgemm
,
which performs matrix-matrix operations in the
libraries invoked by the FullGeneral
equation solver.
Deactivate the Virtual Environment
When you have finished profiling, deactivate your virtual environment.
(my-venv)$ deactivate
Also, if you used an instance on EC2, don’t forget to stop or terminate your virtual machine.
Commentary
These two examples are extreme in their characteristics, but give you a starting point to assess the profile of any OpenSeesPy analysis. The profiles should lead to actionable steps you can take to reduce the run-time of your analyses. For these examples, the steps were obvious from the start–use fewer fibers or use a sparse equation solver. But in other cases, the actions may not be so obvious until after you profile.
Also note that if you are modifying source code, the profiles will tell you which methods could use a second look for improved implementation. Shaving off fractions of a penny in the Concrete23 state determination could lead to big reductions in your IDA run times.