Skip to content

Instantly share code, notes, and snippets.

@cindytsai
Created June 19, 2022 10:22
Show Gist options
  • Save cindytsai/ffc3ede3ea0269d5444864a0a98c37c0 to your computer and use it in GitHub Desktop.
Save cindytsai/ffc3ede3ea0269d5444864a0a98c37c0 to your computer and use it in GitHub Desktop.
How to use cProfile and visualize.

Using Profile Tools

cProfile

Implement in Inline Analysis

Use cProfile.runctx in a function specific for profiling.

import yt
import cProfile
from mpi4py import MPI

# Must include this line, if you are running in parallel.
yt.enable_parallelism()

myrank = MPI.COMM_WORLD.Get_rank()
step = 0

def yt_inline_ProjectionPlot( fields ):
    # Load the data, just like using yt.load()
    ds = yt.frontends.libyt.libytDataset()

    # Do yt operation
    prjz = yt.ProjectionPlot(ds, 'z', fields)

    # Include this line, otherwise yt will save one copy in each rank.
    if yt.is_root():
        prjz.save()

def test_function():
    global step
    field = "Dens"
    cProfile.runctx("yt_inline_ProjectionPlot( field )", globals(), locals(), filename="YT_cProfile_Step" + str(step) + "_" + str(myrank) + ".out")
    step += 1

Generate Useful Figure from Profiling Output

  • Install gpro2dot, dot:

    pip38 install gprof2dot
    sudo apt install graphviz
  • Generate graph from previous section profiling output.

    gprof2dot -f pstats YT_cProfile_Step0_0.out  | dot -Tpng -o YT_RecordTimeGraph.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment