PCSVP使用C++重写对性能提升探讨

测试说明

  1. 目前仿真核心基本还是python版的,除了连续系统在求解时会用到C++的求解器
  2. 本次测试的目的是为之后使用c++全面重写仿真核心进行提前测试,探究更换语言对性能的提升幅度有多少,为之后的工作开展提供依据

C++版本求解器说明

  1. 实现了一个简单的仿真引擎,来执行仿真时需要执行的步骤,它可以对模型执行相应的仿真流程
    1. 对应python版本的simulate方法,cpp版实现了对应的流程initialize()、run()、terminate()
    2. initialize()方法中只实现了排序和系统的初始化。
      1. 相比python版本的少了创建运行时model、连接from和goto系统产生的边、模型的展开的步骤。由于目前不支持子系统、from等系统,所以不会有太大影响。
    3. run()方法就是一个循环,当时间没有结束时就循环调用大步长方法step()。
      1. 在一个step中,会调用顶层model的output、update、derivate方法。其中derivate方法相当于python版本中ode3的solve方法
      2. 三个方法都对应了python版本中的相应实现
    4. terminate方法就是对所有的系统调用terminate
  2. 实现简单的几个系统,可以用来创建模型即可
    1. 目前实现了step、state_space和scope

测试方法

  1. 创建相同的模型,分别在python版和C++版中进行仿真时间的测试
  2. 选用模型为两个,一个是step->state_space->scope,第二个是step->state_space*5->scope
  3. 求解器设置为ode3,仿真时间为50和10分别测试,步长固定为1e-3
    1. python
       from dssim_core import Engine
       import time
       
       engine = Engine()
       # 创建顶层模型
       model = engine.create_model()
       engine.set_primary_model(model)
       engine.set_parameter("step_size", 1e-3)
       engine.set_parameter("start_time", 0)
       engine.set_parameter("stop_time", 10)
       engine.set_parameter("solver", "ode3")
       # 创建系统
       step = model.create_system("sources.step")
       ss = model.create_system("continuous.state_space")
       ss2 = model.create_system("continuous.state_space")
       ss3 = model.create_system("continuous.state_space")
       ss4 = model.create_system("continuous.state_space")
       ss5 = model.create_system("continuous.state_space")
       scope = model.create_system("sinks.scope")
       # 连接系统
       model.link_system(step, "output", ss, "u")
       model.link_system(ss, "y", ss2, "u")
       model.link_system(ss2, "y", ss3, "u")
       model.link_system(ss3, "y", ss4, "u")
       model.link_system(ss4, "y", ss5, "u")
       model.link_system(ss5, "y", scope, "IP0")
       # model.link_system(ss, "y", scope, "IP0")
       # 仿真
       start = time.time()
       engine.simulate()
       end = time.time()
       print("python version run time is {}s".format(end-start))
      
    2. c++
      #include <iostream>
      #include <time.h>
      #include "engine.hh"
      
      int main(int args, char** argv) {
          time_t start, end;
          Engine engine;
          Model* model = engine.createModel();
          engine.setPrimaryModel(model);
      
          BaseSystem* step = model->createSystem("step");
          step->setId("step");
          BaseSystem* ss = model->createSystem("state space");
          BaseSystem* ss2 = model->createSystem("state space");
          BaseSystem* ss3 = model->createSystem("state space");
          BaseSystem* ss4 = model->createSystem("state space");
          BaseSystem* ss5 = model->createSystem("state space");
          ss->setId("ss");
          ss2->setId("ss2");
          ss3->setId("ss3");
          ss4->setId("ss4");
          ss5->setId("ss5");
      
          BaseSystem* scope = model->createSystem("scope");
          scope->setId("scope");
          // model->linkSystem(step, "output", scope, "input");
      
          model->linkSystem(step, "output", ss, "input");
          // model->linkSystem(ss, "output", scope, "input");
          model->linkSystem(ss, "output", ss2, "input");
          model->linkSystem(ss2, "output", ss3, "input");
          model->linkSystem(ss3, "output", ss4, "input");
          model->linkSystem(ss4, "output", ss5, "input");
          model->linkSystem(ss5, "output", scope, "input");
      
          start = clock();
          engine.simulate();
          end = clock();
          std::cout << "C++ version run time is " << (end - start) / 1000000.0 << "s" << std::endl;
          return 0;
      }
      

测试结果

根据四组测试结果,发现cpp版比python快10倍之上。


本文章使用limfx的vscode插件快速发布