问题
I've noticed that odeint uses very little memory when compared to my implementation of the RK4 algorithm or Mathematica. For the same step size, odeint uses about 3.11GB while my program uses 7GB and with Mathematica, I have to manually increase the pagefile size to 40GB or else it runs out of memory. (Edit: CPU usage is only 18%)
I am curious on how this is possible because when I save the results, the data file is almost the same size in all three cases.
However, when it comes to execution time, odeint seems an order of magnitude slower than either my program or Mathematica. Is this tradeoff normal? I do things the super noob way.
Edit:2 ** Step Size vs Execution Time **
- 0.0005=2:55.59 ~ 24.44hrs for 500 steps
- 0.001=1:29.14 ~ 12.5hrs for 500 steps
- 0.005= 0:17.19~ 2.5hrs for 500 steps.
- 0.01= 8.34 ~ 1hr10min for 500 steps
For example:
void Classical(vector<vector<double> >& u1,vector<vector<double> >& u2,vector<vector<double> >& phi1,vector<double>& delta,vector<vector<double> >& theta,vector<vector<double> >& phi2, vector<double>& Gamma,vector<double>& z,double h,double u10,double u20,double theta_initial){
for(int i=0;i<delta.size();++i){
double v1=u10;
double v2=u20;
double ph1=0.0;
double ph2=0.0;
double angle=delta[i]; //OK
u1.push_back ( vector<double>() );
u2.push_back ( vector<double>() );
phi1.push_back ( vector<double>() );
phi2.push_back ( vector<double>() );
theta.push_back ( vector<double>() );
for(int j=0;j<z.size();++j){
double k1=0.0; double k2=0.0;double k3=0.0;double k4=0.0;
double L1=0.0; double L2=0.0;double L3=0.0;double L4=0.0;
double m1=0.0; double m2=0.0;double m3=0.0;double m4=0.0;
double n1=0.0; double n2=0.0;double n3=0.0;double n4=0.0;
k1=h*(v2*v2-1.0)*cos((angle));
L1=h*( (2.0/(1.0-(v2*v2))) - (1.0/(v2*v2)) )*Gamma[i];
m1=h*(1.0/(1.0-(v2*v2)))*Gamma[i];
n1=h*(1.0/((v2*v2)))*Gamma[i];
k2=h*((v2+k1/2)*(v2+k1/2)-1)*cos(((angle+L1/2)));
L2=h*( (2.0/(1-((v2+k1/2)*(v2+k1/2)))) - (1/((v2+k1/2)*(v2+k1/2))) )*Gamma[i];
m2=h*(1/(1-((v2+k1/2)*(v2+k1/2))))*Gamma[i];
n2=h*(1/(((v2+k1/2)*(v2+k1/2))))*Gamma[i];
k3=h*((v2+k2/2)*(v2+k2/2)-1)*cos(((angle+L2/2)));
L3=h*( (2.0/(1-((v2+k2/2)*(v2+k2/2)))) - (1/((v2+k2/2)*(v2+k2/2))) )*Gamma[i];
m3=h*(1/(1-((v2+k2/2)*(v2+k2/2))))*Gamma[i];
n3=h*(1/(((v2+k2/2)*(v2+k2/2))))*Gamma[i];
k4=h*((v2+k3)*(v2+k3)-1)*cos(((angle+L3)));
L4=h*( (2.0/(1-((v2+k3)*(v2+k3)))) - (1/((v2+k3)*(v2+k3))) )*Gamma[i];
m4=h*(1/(1-((v2+k3)*(v2+k3))))*Gamma[i];
n4=h*(1/(((v2+k3)*(v2+k3))))*Gamma[i];
v2=v2+(k1/6)+(k2/3)+(k3/3)+(k4/6);
angle=angle + (L1/6)+(L2/3)+(L3/3)+(L4/6);
ph1=ph1+(m1/6)+(m2/3)+(m3/3)+(m4/6);
ph2=ph2+(n1/6)+(n2/3)+(n3/3)+(n4/6);
v1=sqrt(1.0-(v2*v2));
u1[i].push_back(v1);
u2[i].push_back(v2);
theta[i].push_back(angle);
phi1[i].push_back(ph1);
phi2[i].push_back(ph2);
}
}
}
回答1:
I think you should compile your programs in release modus to enable compiler optimization. odeint uses lots of template code which is quite slow when compiled in debug modus. The performance will be increaced by orders of magnitude in release modus.
来源:https://stackoverflow.com/questions/12202870/odeint-memory-usage-and-execution-time