TransWikia.com

How can I parallaize expresion that contains ParallelSum?

Mathematica Asked by HD2006 on December 15, 2020

I have this code where the last part contains ParallelSum

tc = {{0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, -1, 0, 0}};
m1[x_, y_] := {{0, I Sin[x] + Sin[y], 
    13/4 - Cos[x] - Cos[y], -1}, {-I Sin[x] + Sin[y], 0, -1, 
    13/4 - Cos[x] - Cos[y]}, {13/4 - Cos[x] - Cos[y], -1, 
    0, -I Sin[x] - Sin[y]}, {-1, 13/4 - Cos[x] - Cos[y], 
    I Sin[x] - Sin[y], 0}};
m2[x_, y_, Wn_] := 
  SparseArray[{Band[{1, 1}, {4 Wn, 4 Wn}] -> {m1[x, y]}, 
    Band[{1, 5}, {4 Wn, 4 Wn}] -> {tc}, 
    Band[{5, 1}, {4 Wn, 4 Wn}] -> {ConjugateTranspose[tc]}}];
Wn = 30;
M[x_, y_] := m2[x, y, Wn];
O1[x_, y_] = D[M[x, y], x];
fn[x_, y_, r_] := 
  Block[{ks = M[x, y], 
    id = IdentityMatrix[
      4 Wn]}, {(Inverse[(r - I*0.01)*id - 
       ks]), (Inverse[(r + I*0.01)*id - ks])}];
Pij[i_, j_] := SparseArray[{Band[{i, i}, {j, j}] -> 1}, {4 Wn, 4 Wn}]
F1[x_, y_, r_, i_, j_] := 
 Block[{f = fn[x, y, r]}, 
  Re[Tr[(O1[x, y].(f[[2]] - f[[1]]).O1[x, y].(f[[2]] - f[[1]])).Pij[i,
       j]]]]
F2[rf_, nc_, i_, j_] := 
 ParallelSum[
  Sqrt[i j] (1/nc)^2 F1[x, y, rf], {x, [Pi]/nc, [Pi], (2 [Pi])/
   nc}, {y, [Pi]/nc, [Pi], (2 [Pi])/nc}]

Now, I want to Evaluate F2[rf, nc, i, j] in Parallel so I tried different structures such as

ParallelTable[{j/4,r,F2[r,29.,j-3,j]},{j,4,8,4},{r,0,1.6,0.5}]//AbsoluteTiming
{10.804,{{{1,0.,0.0000262483},{1,0.5,0.238498},{1,1.,0.299957},{1,1.5,1.72422}},{{2,0.,0.0000830044},{2,0.5,0.754197},{2,1.,0.948546},{2,1.5,5.45245}}}}

but this gives an error message and it is more time expensive than

Table[{j/4,r,F2[r,29.,j-3,j]},{j,4,8,4},{r,0,1.6,0.5}]//AbsoluteTiming
    {3.91544,{{{1,0.,0.0000262483},{1,0.5,0.238498},{1,1.,0.299957},{1,1.5,1.72422}},{{2,0.,0.0000830044},{2,0.5,0.754197},{2,1.,0.948546},{2,1.5,5.45245}}}}  

Last try I used Sum in the definition of F2[rf, nc, i, j] instead of ParallelSum and then used Parallelize

Parallelize[Table[{j/4,r,F2[r,29.,j-3,j]},{j,4,8,4},{r,0,1.6,0.5}]]//AbsoluteTiming
{10.674,{{{1,0.,0.0000262483},{1,0.5,0.238498},{1,1.,0.299957},{1,1.5,1.72422}},{{2,0.,0.0000830044},{2,0.5,0.754197},{2,1.,0.948546},{2,1.5,5.45245}}}}   

but still can not efficiently Parallelize the computations. So, how this can be done and is it possible to use ParallelSum with ParallelTable efficiently?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP