GithubHelp home page GithubHelp logo

PyTorch Profiling Tools about reactorch HOT 3 CLOSED

deng-mit avatar deng-mit commented on June 15, 2024
PyTorch Profiling Tools

from reactorch.

Comments (3)

jiweiqi avatar jiweiqi commented on June 15, 2024

HI, @taosong1999 You may provide an example line of code to show how to profile the solution_test.py and how to interpret the profile results here.

from reactorch.

jiweiqi avatar jiweiqi commented on June 15, 2024

Line by Line Profiling in iPython

You can also use built-in profiler in Spyder.

from reactorch.

WeilunQiu avatar WeilunQiu commented on June 15, 2024

Hi, all. Here's a performance test on the reaction rate constants func (forward, equlibrium and reverse) and wdot func, in which I run these functions for several times (see x-axis). Note that the path of the mechanism file might need to be changed according to where you put the script.

Results:
(The unit of y-axis should be second, of course. )
Performance_test

Codes:

from time import perf_counter

import cantera as ct
import matplotlib.pyplot as plt
import numpy as np
import torch

import reactorch as rt

cpu = torch.device('cpu')

cuda = torch.device('cuda:0')

device = cpu

mech_yaml = '../../data/gri30.yaml'
composition = "CH4:0.5, O2:1.0, N2:3.76"

sol = rt.Solution(mech_yaml=mech_yaml, device=device)

gas = sol.gas
gas.TPX = 950, 20 * ct.one_atm, composition

r = ct.IdealGasReactor(gas)
sim = ct.ReactorNet([r])

time = 0.0
t_end = 10
idt = 0
states = ct.SolutionArray(gas, extra=['t'])
T0 = gas.T

print('%10s %10s %10s %14s' % ('t [s]', 'T [K]', 'P [atm]', 'u [J/kg]'))

count = 0
while sim.time < t_end:

    sim.step()

    states.append(r.thermo.state, t=time)

    if r.thermo.T > T0 + 600 and idt < 1e-10:
        idt = sim.time

    if idt > 1e-10 and sim.time > 4 * idt:
        break

print('%10.3e %10.3f %10.3f %14.6e' % (sim.time,
                                       r.T,
                                       r.thermo.P / ct.one_atm,
                                       r.thermo.u))

print('idt = {:.2e} [s] number of points {}'.format(idt, states.t.shape[0]))

TP = torch.stack((torch.Tensor(states.T), torch.Tensor(states.P)), dim=-1)
Y = torch.Tensor(states.Y)
TPY = torch.cat([TP, Y], dim=-1).to(device)

t0_start = perf_counter()

sol.set_states(TPY)

t1_stop = perf_counter()
print('sol set_states time spent {:.1e} [s]'.format(t1_stop - t0_start))

# tesing reaction rate func
def check_reaction_rate(num):
       for i in range(0,num):
              sol.forward_rate_constants_func()
              sol.equilibrium_constants_func()
              sol.reverse_rate_constants_func()
       
# tesing wdot calc func
def check_wdot(num):
       for i in range(0,num):
              sol.wdot_func()

test_n_iter = np.array([100,150,200,250,300,350,400,450,500])
time_cost1 = np.zeros_like(test_n_iter)
time_cost2 = np.zeros_like(test_n_iter)

for j in range(0,9):
       print('#Test {:d}'.format(j))
             
       t1_stop = perf_counter()
       
       check_reaction_rate(test_n_iter[j])
       t2_stop = perf_counter()
       t_cost1 = t2_stop - t1_stop
       print('sol check_reaction_rate time spent {:.1e} [s]'.format(t_cost1))      
       
       check_wdot(test_n_iter[j])
       t3_stop = perf_counter()
       t_cost2 = t3_stop - t2_stop
       print('sol check_wdot time spent {:.1e} [s]'.format(t_cost2))
       
       time_cost1[j] = t_cost1 
       time_cost2[j] = t_cost2
       
plt.plot(test_n_iter,time_cost1)
plt.plot(test_n_iter,time_cost2)
plt.xlabel('Number of iterations')
plt.ylabel('Time cost')
plt.legend(['Reaction rate cost','Wdot cost'])
plt.savefig('Performance_test.png',dpi=600)

from reactorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.