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

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[ ...

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...

list manipulation - Selecting multiple columns from a matrix?

Sample data: data = { {{2013, 1, 1}, 24.13, 167.67, 231.82}, {{2013, 1, 2}, 32.15, 170.92, 225.99}, {{2013, 1, 3}, 35.43, 172.68, 221.67}, {{2013, 1, 4}, 36.73, 173.05, 218.32}, {{2013, 1, 5}, 58.19, 165.96, 197.05}, {{2013, 1, 6}, 69.99, 163.50, 187.52}, {{2013, 1, 7}, 71.37, 154.21, 175.58}, {{2013, 1, 8}, 72.51, 149.66, 163.25}}; I want a DateListPlot with three graphs, so for a matrix formed by columns 1 and 2, one for columns 1 and 3, and 1 for columns 1 and 4. At the moment I'm using this code: data2 = Transpose[{data[[All, 1]], data[[All, 2]]}]; data3 = Transpose[{data[[All, 1]], data[[All, 3]]}]; data4 = Transpose[{data[[All, 1]], data[[All, 4]]}]; DateListPlot[{data2, data3, data4}, Joined -> True, Filling -> {3 -> {1}}] but I have a hunch that this can be done more efficiently. I don't like the Transpose s in particular. Any ideas? edit (for extra credit) What if I need to multiply the second column by 2, which in my solution is simp...