0
$\begingroup$

I'm new to Mathematica.

My knowledge of parallel computing is very limited. I tried to figure out how to proceed using the documentation and have found something from online help pages. My program looks as shown below:

Clear[tabfidC, J, B]
tabfidC = 
  ParallelTable[
    {J, B, Quiet[fidnUZST[J, B, 0.5, 0.1]]}, 
    {J, -1, 1, 0.1}, {B, 0, 2, 0.1}];

Clear[JJ,BB]
Clear[listA8]
listA8 = {{0, 0, 0}}
SetSharedVariable[listA8]
Parallelize[
  For[JJ = 1, JJ <= Dimensions[tabfidC][[1]], JJ++,
    For[BB = 1, BB <= Dimensions[tabfidC][[2]], BB++, 
      listA8 = 
        AppendTo[
          listA8, 
          {N[(JJ - 11)*1/10], N[(BB-1)*1/10], 
           1/(4*π)*
             NIntegrate[tabfidC[[JJ ,BB, 3]]*Sin[θ], {ϕ, 0, 2*π},{θ, 0, π}]}]]]]
// Quiet;

 Show[
   ListPlot3D[{Drop[listA8, 1]},
     PlotRange -> {-.05, 1.1},
     AxesLabel ->
       {Style[J, Medium, Bold, Blue],
        Style[B, Medium, Bold, Blue],
        Style[OverBar[F], Medium, Bold, Blue]},
     LabelStyle -> Directive[Blue, Bold],
     ImageSize -> Scaled[.3]]]

The function fidnUZST has been defined before. I form a table from it named tabfidC. Then an integral over $\theta$ and $\phi$ is performed over the the components of the table and they constitute a list named listA8. Finally they get plotted.

What I would like to know is how to parallelize the construction of listA8. I used SetSharedVariable[listA8], but this does not make a difference in calculation time!

I would be grateful if you had suggestions.

$\endgroup$

closed as off-topic by m_goldberg, Henrik Schumacher, gwr, LCarvalho, MarcoB Jan 1 '18 at 18:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – m_goldberg, Henrik Schumacher, gwr, LCarvalho, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Before diving into parallelization and the like, try getting into the basic Mathematica way of doing things first. For is not something that is normally used; instead we loop with Do or make lists with Table. AppendTo is a very slow way of building a list; again Table is preferred, or one can use Sow/Reap if one cannot know the number of elements in the table beforehand. $\endgroup$ – Marius Ladegård Meyer Dec 26 '17 at 12:32
1
$\begingroup$

Your code is somewhat obfuscated given the fact that you do not share the definition of fidnUZST with us. I am almost sure that you can obtain listA8 (with only few and small further modifications) from

listA8 = Flatten[ParallelTable[f[J, B], {J, -1, 1, 0.1}, {B, 0, 2, 0.1}],1];

if you define the function f correctly. For example, the function f could look similar to

f[J_, B_] := {J, B/(4 π) NIntegrate[fidnUZST[J, B, 0.5, 0.1] Sin[θ], {ϕ, 0, 2*π}, {θ, 0, π}]};

Modifying a shared variable is a rather nontrivial task for a multicore system and needs a certain amount of overhead. Best to avoid that.

$\endgroup$
  • $\begingroup$ Thank you for your helpful answer. $\endgroup$ – Mahmood Shamirzaie Dec 31 '17 at 6:00
  • $\begingroup$ I would like to share the function fidnUZST were mentioned above: $\endgroup$ – Mahmood Shamirzaie Dec 31 '17 at 6:14
  • $\begingroup$ But i do not know how to attach my file $\endgroup$ – Mahmood Shamirzaie Dec 31 '17 at 6:15
  • $\begingroup$ @MahmoodShamirzaie Some people use pastebin. But maybe it suffices to give a short and simple example that fits into the format of this site? $\endgroup$ – Henrik Schumacher Dec 31 '17 at 8:29
0
$\begingroup$

I am sorry to type in this environment; I am new in this forum. Some few terms in fidnUZST is sent:

2 E^(I \[Phi])
  Cot[\[Theta]] Csc[\[Theta]] (4 ((
      E^(-5 (J + 
          2 Sqrt[B^2 + 16 \[Gamma]^2])) (B^2 (1 + E^(
            20 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         16 (1 + E^(20 Sqrt[B^2 + 16 \[Gamma]^2])) \[Gamma]^2 - 
         B (-1 + E^(20 Sqrt[B^2 + 16 \[Gamma]^2])) Sqrt[
          B^2 + 16 \[Gamma]^2]))/(
      4 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])) (B^2 + 
         16 \[Gamma]^2)) + (
      8 (E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])/(
         16 + (B - Sqrt[B^2 + 16 \[Gamma]^2])^2/\[Gamma]^2) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2]))/(
         16 + (B + Sqrt[B^2 + 16 \[Gamma]^2])^2/\[Gamma]^2)))/(
      E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(5 J + 10 Sqrt[b^2 + J^2]) + 
       E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
       E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])))^2 Cos[\[Theta]/
     2]^2 + (-(((E^(5 (J - 2 Sqrt[b^2 + J^2])) - E^(
          5 (J + 2 Sqrt[b^2 + J^2]))) J)/(
       2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
          5 J + 10 Sqrt[b^2 + J^2]) + 
          E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
          E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])) Sqrt[
        b^2 + J^2])) + ((
       E^(5 (J + 2 Sqrt[b^2 + J^2])) (b - Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 - b Sqrt[b^2 + J^2]) + (
       E^(5 (J - 2 Sqrt[b^2 + J^2])) (b + Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 + b Sqrt[b^2 + J^2]))/(
      4 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))) + (
      E^(5 (J + 2 Sqrt[b^2 + J^2]))/(
       1 + (b - Sqrt[b^2 + J^2])^2/J^2) + E^(
       5 (J - 2 Sqrt[b^2 + J^2]))/(1 + (b + Sqrt[b^2 + J^2])^2/J^2))/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))))^2 Sin[\[Theta]/
     2]^2 + 2 (-(((E^(5 (J - 2 Sqrt[b^2 + J^2])) - E^(
          5 (J + 2 Sqrt[b^2 + J^2]))) J)/(
       2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
          5 J + 10 Sqrt[b^2 + J^2]) + 
          E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
          E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])) Sqrt[
        b^2 + J^2])) + ((
       E^(5 (J + 2 Sqrt[b^2 + J^2])) (b - Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 - b Sqrt[b^2 + J^2]) + (
       E^(5 (J - 2 Sqrt[b^2 + J^2])) (b + Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 + b Sqrt[b^2 + J^2]))/(
      4 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))) + (
      E^(5 (J + 2 Sqrt[b^2 + J^2]))/(
       1 + (b - Sqrt[b^2 + J^2])^2/J^2) + E^(
       5 (J - 2 Sqrt[b^2 + J^2]))/(1 + (b + Sqrt[b^2 + J^2])^2/J^2))/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])))) (((E^(
         5 (J - 2 Sqrt[b^2 + J^2])) - E^(
         5 (J + 2 Sqrt[b^2 + J^2]))) J)/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])) Sqrt[
       b^2 + J^2]) + ((
       E^(5 (J + 2 Sqrt[b^2 + J^2])) (b - Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 - b Sqrt[b^2 + J^2]) + (
       E^(5 (J - 2 Sqrt[b^2 + J^2])) (b + Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 + b Sqrt[b^2 + J^2]))/(
      4 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))) + (
      E^(5 (J + 2 Sqrt[b^2 + J^2]))/(
       1 + (b - Sqrt[b^2 + J^2])^2/J^2) + E^(
       5 (J - 2 Sqrt[b^2 + J^2]))/(1 + (b + Sqrt[b^2 + J^2])^2/J^2))/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])))) Sin[\[Theta]/
     2]^2 + (((E^(5 (J - 2 Sqrt[b^2 + J^2])) - E^(
         5 (J + 2 Sqrt[b^2 + J^2]))) J)/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2])) Sqrt[
       b^2 + J^2]) + ((
       E^(5 (J + 2 Sqrt[b^2 + J^2])) (b - Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 - b Sqrt[b^2 + J^2]) + (
       E^(5 (J - 2 Sqrt[b^2 + J^2])) (b + Sqrt[b^2 + J^2])^2)/(
       b^2 + J^2 + b Sqrt[b^2 + J^2]))/(
      4 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))) + (
      E^(5 (J + 2 Sqrt[b^2 + J^2]))/(
       1 + (b - Sqrt[b^2 + J^2])^2/J^2) + E^(
       5 (J - 2 Sqrt[b^2 + J^2]))/(1 + (b + Sqrt[b^2 + J^2])^2/J^2))/(
      2 (E^(5 (J - 2 Sqrt[b^2 + J^2])) + E^(
         5 J + 10 Sqrt[b^2 + J^2]) + 
         E^(-5 (J + 2 Sqrt[B^2 + 16 \[Gamma]^2])) + 
         E^(-5 J + 10 Sqrt[B^2 + 16 \[Gamma]^2]))))^2 Sin[\[Theta]/
     2]^2)
$\endgroup$
  • $\begingroup$ @HenrikSchumacher, when I use the code you had sent, some errors appears which indicate that integration fail! $\endgroup$ – Mahmood Shamirzaie Jan 2 '18 at 5:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.