Skip to main content

performance tuning - How to solve an eigensystem faster?


I have a module that I need to call 1-10 million times in my program. Currently, it is taking several hours to run so I am hoping that I can cut down some runtime with your help.


r = RandomReal[NormalDistribution[0., 1./2.], 6];

es = Eigensystem[H0[ω0, r[[1]], r[[2]], r[[3]], r[[4]], r[[5]], r[[6]] ];


ε = es[[1]];
v = es[[2]];
vS = Conjugate[v];

(*elements of v and vS are called later; v[[1]], vS[[1]] etc...*)

H0 is a compiled function which sped things up a little. It looks like this:


enter image description here.


In copy-paste form,



 H0={{0, (ωz1 - ωz2)/
2, (-ωx1 + ωx2)/(2 Sqrt[2]) - (I ωy1)/(
2 Sqrt[2]) + (I ωy2)/(
2 Sqrt[2]), (ωx1 - ωx2)/(2 Sqrt[2]) - (
I ωy1)/(2 Sqrt[2]) + (I ωy2)/(
2 Sqrt[2])}, {(ωz1 - ωz2)/2,
0, (ωx1 + ωx2)/(2 Sqrt[2]) + (I ωy1)/(
2 Sqrt[2]) + (I ωy2)/(
2 Sqrt[2]), (ωx1 + ωx2)/(2 Sqrt[2]) - (
I ωy1)/(2 Sqrt[2]) - (I ωy2)/(

2 Sqrt[2])}, {(-ωx1 + ωx2)/(2 Sqrt[2]) + (
I ωy1)/(2 Sqrt[2]) - (I ωy2)/(
2 Sqrt[2]), (ωx1 + ωx2)/(2 Sqrt[2]) - (
I ωy1)/(2 Sqrt[2]) - (I ωy2)/(
2 Sqrt[2]), ω0 + (ωz1 + ωz2)/2,
0}, {(ωx1 - ωx2)/(2 Sqrt[2]) + (I ωy1)/(
2 Sqrt[2]) - (I ωy2)/(
2 Sqrt[2]), (ωx1 + ωx2)/(2 Sqrt[2]) + (
I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[2]),
0, -ω0 + 1/2 (-ωz1 - ωz2)}}


Is there anything else that can be optimized here?



Answer



This is too long for a comment and honestly, to give a real answer, there is more information required in your question. Isn't it possible, that you give a working example, so that we see what takes long and how you implemented it?


If you are calling Eigensystem for many different input values which are know, there is still some place for speed-up. Since your expressions are very lengthy, please find the initialization in an extra section.


First we measure how long it takes to calculate the Eigenvectors of H0 for 1 Million random values


data = RandomReal[{-1, 1}, {1000000, 7}];
First@AbsoluteTiming[Eigenvectors[H0[#]] & /@ data]

This took 44.4 sec here. The next thing you can try is to distribute H0 over parallel kernels and use ParallelMap



DistributeDefinitions[H0];
First@AbsoluteTiming[ParallelMap[Eigenvectors[H0[#]] &, data]]

This took 25.3 sec with 4 subkernels. Let's test the compiled code. First when we apply it non-parallel


First@AbsoluteTiming[evectors @@@ data]  

This took 2.4 sec which is almost 20 times faster then the initial version. Let's see what we can get if we call it parallel


First@AbsoluteTiming[evectors @@ Transpose[data]]

This took only 0.24 sec. If this scales well, that it means I can run 10 million samples in about 2.5 seconds. An indeed, a test with $10^7$ runs required 2.75 sec.



Now you might ask, whaat??, why is evectors @@@ data a serial call while evectors @@ Transpose[data] is parallel? It's because of the Listable attribute in the Compile-call and since we turn on Parallelization. Sure is


evectors @@@ data == evectors @@ Transpose[data]

(* Out[21]= True *)

Initialization


Compiled parallel "C" versions of Eigenvalues and Eigenvectors


{evalues, evectors} = 
Compile[{{ω0, _Complex, 0}, {ωx1, _Complex,
0}, {ωx2, _Complex, 0}, {ωy1, _Complex,

0}, {ωy2, _Complex, 0}, {ωz1, _Complex,
0}, {ωz2, _Complex, 0}}, #, Parallelization -> True,
CompilationTarget -> "C", RuntimeAttributes -> {Listable}] & /@
Eigensystem[{{0, (ωz1 - ωz2)/
2, (-ωx1 + ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2]), (ωx1 - ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2])}, {(ωz1 - ωz2)/2,
0, (ωx1 + ωx2)/(2 Sqrt[

2]) + (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2]), (ωx1 + ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2])}, {(-ωx1 + ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2]), (ωx1 + ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2]), ω0 + (ωz1 + ωz2)/2,
0}, {(ωx1 - ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[

2]), (ωx1 + ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2]), 0, -ω0 + 1/2 (-ωz1 - ωz2)}}];

Furthermore, I try to copy your approach by defining H0


H0[{ω0_, ωx1_, ωx2_, ωy1_, ωy2_, 
ωz1_, ωz2_}] =
N[{{0, (ωz1 - ωz2)/
2, (-ωx1 + ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[

2]), (ωx1 - ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2])}, {(ωz1 - ωz2)/2,
0, (ωx1 + ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2]), (ωx1 + ωx2)/(2 Sqrt[
2]) - (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2])}, {(-ωx1 + ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2]), (ωx1 + ωx2)/(2 Sqrt[

2]) - (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2]), ω0 + (ωz1 + ωz2)/2,
0}, {(ωx1 - ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) - (I ωy2)/(2 Sqrt[
2]), (ωx1 + ωx2)/(2 Sqrt[
2]) + (I ωy1)/(2 Sqrt[2]) + (I ωy2)/(2 Sqrt[
2]), 0, -ω0 + 1/2 (-ωz1 - ωz2)}}];

Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...