2

The program runs out quickly(30 seconds) if I don't do parallel.But when I replace Table with ParallelTable,the Program will keep running,no output results.

My code https://privatebin.net/?d1b1eeff435720eb#XWLsW2gY2EfTFnX+eQCXtCVBPN4budq3wQtVWaNwI4g=

guangya
  • 39
  • 3

2 Answers2

4

Replace your t' with anything legal, e.g. tp will solve the problem. Assigning t' is actually doing Derivative[1][t]=1, which is not advisable.

The reason of this strange behavior is that SubValues of derivative is not automatically distributed to kernels. Therefore you get 1'==0.6 for the main kernel, and 1'==0& for the sub kernels, and the value of this constant becomes a function which fails the later calculation.

After making such replacement, and deleting the duplicated ParallelTable in your F definition, you can get the expected result:

ParallelTable[F[0, 0, k], {k, 1, 10}]; // AbsoluteTiming
{4.8858, Null}
Table[F[0, 0, k], {k, 1, 10}]; // AbsoluteTiming
{8.10208, Null}
vapor
  • 7,911
  • 2
  • 22
  • 55
3

Two things will provide for immense speed-up with parallel functions like ParallelTable:

  1. Launch your kernels ahead of the initial parallel call with:

    LaunchKernels["Number of Kernels, max available if left blank"]
    
  2. Ensure each kernel has prior knowledge of the functions with:

    DistributeDefinitions["context`"]
    

You should also see some increase in speed, due to a decrease in CPU need, if you were to provide assumptions for all of your defined variable functions. What I mean by this is something like:

    f[x_?NumericQ,n_?IntegerQ]

Wherein x is always a numerical input and n is an integer.

I hope this helps you understand how to run parallel code better, it's a constant learning process, as we will continue to make more and more efficient ways that leave the previous best methods in the dust, and we will have to keep up :D

So, after discussing with @happy fish, I was able to test the code, and got this output, after replacing the second ParallelTable with Table:

    {-0.463029, -0.463029, -0.463029, -0.463029, -0.463029, -0.463029, -0.463029, -0.463029, -0.463029, -0.463029}

There is something amiss with this, and I apologize that I cannot immediately parse what the issue is. I will take some time to check later and see if I can provide additional input, after satisfying my own duties prior ;) As for the rest:

The issue with using two ParallelTable calls is why you would not receive output for your addition of Parallel to the second Table, again, barring my lack of understanding. Additionally, you would assuredly benefit from a functional method of implementing this code. There are numerous inline reassignments which can likely be shortened, and I anticipate that is another issue imparting itself upon your long-running/non-functional parallel implementation.

CA Trevillian
  • 3,342
  • 2
  • 8
  • 26
  • 2
    Thanks for your answer, but I don't think it addresses the problem OP encountered. LaunchKernels and DistributeDefinitions are done automatically, there is no need of explicitly writing down. There won't be an "immense speed-up with parallel functions" in either case. Testing the parameter can avoid unnecessary symbolic computations, but won't help here since everything is numerical. – vapor Apr 19 '19 at 06:34
  • @happyfish I'm not sure that is entirely accurate, unfortunately. Though it would be nice! My understanding is as follows: When you perform the first call on a parallel function, you will spend more time than subsequent calls, this being due to the need to launch all kernels. Additionally there is some time taken to distribute definitions, if this is indeed done automatically. I am curious if there is a part of the documentation you can point to for this? I am unable to have ParallelTable actually use all kernels unless you have done as I stated, otherwise they take about a second longer. – CA Trevillian Apr 19 '19 at 06:41
  • @happyfish my comment stems from my having had an order of magnitude speed-up earlier today, by distributing definitions of my self-packaged function and launching the kernels ahead of their parallel execution. It seems, though, this may be another time when "vectorized"(I still struggle to confidently state/grasp this concept/method) functions would be helpful if implemented? Your expertise puts mine at a junior, however, so I will look to your continued interpretation of this problem solution. – CA Trevillian Apr 19 '19 at 06:49
  • 1
    I agree with your general ideas on parallel evaluations. I am just saying that these theories don't localize for this particular problem. If you experiment on the problem you will find immediately that the bottleneck is not on where you focus: it's just distributing 10 difficult tasks to 6(by default) kernels, the overhead of subsequent calls and copying definitions is negligible. For the automatically distribute definition part, please refer to the first example in Options->DistributedContexts and https://mathematica.stackexchange.com/questions/39178/paralleltable-and-distributedefinitions – vapor Apr 19 '19 at 06:49
  • I appreciate your advise.happy fish's answer has solved my problem@CATrevillian – guangya Apr 19 '19 at 10:51
  • @guangya I am happy with the solution provided by happy fish as well :) enjoy your parallelization, I am curious, what is the goal of your program, to show what exactly? – CA Trevillian Apr 19 '19 at 10:53
  • 1
    Condensed matter physics.Bott index,is a kind of Chern number@CATrevillian – guangya Apr 19 '19 at 10:57
  • 1
    we use it to judge whether a substance is trivial topology or not@CATrevillian. Nobel Prize in 2016 – guangya Apr 19 '19 at 11:02
  • @guangya ah very nice :) I study magnetism and spin dynamics! Thank you for your clarification :) I may clean your code up and experiment with it, if this is okay? I will share my results and perhaps some interactive demonstration with you. – CA Trevillian Apr 19 '19 at 11:06
  • Of course.How to get in touch with you?Se isn't a good communication platform@CATrevillian – guangya Apr 19 '19 at 11:13
  • @guangya wonderful! I have added my contact information to my profile. – CA Trevillian Apr 19 '19 at 11:31
  • @CATrevillian I didn't find your contact information :) – guangya Apr 19 '19 at 11:45
  • @guangya trevilliandotoaklanddotedu :D – CA Trevillian Apr 19 '19 at 11:47
  • @CATrevillian E-mail address? – guangya Apr 19 '19 at 11:48
  • @guangya Uncompress["1:eJxTTMoPChZjYGAoKUoty8zJyUzMc8hPzM5JzEvRS00pBQCXeApu"] – CA Trevillian Apr 19 '19 at 12:05
  • @CATrevillian My E-mail address:1259050127@qq.com – guangya Apr 19 '19 at 12:15
  • @CATrevillian you can contact with me from this address.I didn't live in the USA.So the information your send made me confused – guangya Apr 19 '19 at 12:19