5
$\begingroup$

I'm trying to do some optimizations to my code to make it run faster, but it seems that there are inconsistencies in the performance of the code. Namely, the codes have different performance in different execution order.

Here is the original version of the code I try to optimize:

TDSESolve1[EE_, energyLs_, diplMtx_, C0_, {tmin_, tmax_, dt_}] :=
 Module[{lth = Length[energyLs], Ht, tls, Ct, Cls, LHS, RHS},
  Ht[t_] := DiagonalMatrix[energyLs] + EE[t]*diplMtx;
  tls = Table[t, {t, tmin, tmax, dt}];
  Ct = C0;
  Cls = Table[
    LHS = IdentityMatrix[lth] + I dt/2 Ht[t + dt/2];
    RHS = (IdentityMatrix[lth] - I dt/2 Ht[t + dt/2]).Ct;
    Ct = LinearSolve[LHS, RHS],
    {t, Most@tls}];
  Prepend[Cls, C0]
  ]

In this version, I expanded the Ht explicitly:

TDSESolve2[EE_, energyLs_List, diplMtx_, C0_, {tmin_, tmax_, dt_}] := 
 Module[{lth = Length[energyLs], Ht, tls, Ct, Cls, LHS, RHS},
  tls = Table[t, {t, tmin, tmax, dt}];
  Ct = C0;
  Cls = Table[
    LHS = 
     DiagonalMatrix[1. + I dt/2 energyLs] + (I dt/2 EE[t + dt/2])*
       diplMtx;
    RHS = Ct - I dt/2 (energyLs*Ct) - (I dt/2 EE[t + dt/2]) diplMtx.Ct;
    Ct = LinearSolve[LHS, RHS],
    {t, Most@tls}];
  Prepend[Cls, C0]
  ]

Here are the parameters for the tests:

EE[t_] := Sin[t]
dim = 400;
energyLs = RandomReal[{0, 10}, dim];
tmp = RandomReal[{0, 10}, {dim, dim}];
diplMtx = 0.5*(# + Transpose[#]) &@(tmp - DiagonalMatrix[Diagonal[tmp]]);
C0 = PadRight[{1.}, dim];
tmin = 0.; tmax = 100.; dt = 0.1;

When I test these two versions, I get the results that the performance depends on the order of the execution. For example, the TDSESolve2 is about 2X slower after the execution of TDSESolve1:

MemoryInUse[]
TDSESolve2[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
TDSESolve1[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
TDSESolve2[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
(* 30233928 *)
(* {6.52668, Null} *)
(* 44830584 *)
(* {18.6517, Null} *)
(* 44832600 *)
(* {13.5754, Null} *)
(* 44830752 *)

While if we run TDSESolve2 repeatedly on a fresh kernel, I get a more consistent performance (with a slow decline of performance):

MemoryInUse[]
TDSESolve2[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
TDSESolve2[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
TDSESolve2[EE, energyLs, diplMtx, C0, {tmin, tmax, dt}]; // AbsoluteTiming
MemoryInUse[]
(* 47397856 *)
(* {6.45175, Null} *)
(* 47398160 *)
(* {7.37778, Null} *)
(* 47398032 *)
(* {8.85156, Null} *)
(* 47397760 *)

So why the performance of TDSESolve2 declined largely after TDSESolve1?

I'm using version 10.3 on OS X 10.11.5.

$\endgroup$
  • 1
    $\begingroup$ Unable to reproduce on 10.4, 10.4.1, OS X 10.11.5; reproduced on 10.3, 10.3.1, OS X 10.11.5 $\endgroup$ – happy fish May 21 '16 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.