Simple Parallelization with Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter eleteroboltz
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around parallelizing a Mathematica program that runs multiple independent cases by changing parameters. Participants explore issues related to the implementation of the ParallelDo function and the sharing of functions and variables across parallel kernels.

Discussion Character

  • Technical explanation, Debate/contested, Exploratory

Main Points Raised

  • One participant seeks assistance with running a Mathematica program in parallel using ParallelDo, noting that the values are not being passed correctly to the function f.
  • Another participant references the Mathematica documentation, highlighting that functions not known to parallel kernels may not work and suggests using DistributeDefinitions.
  • A later reply indicates that despite using DistributeDefinitions, the issue persists, prompting further exploration of the problem.
  • One participant claims to have found a solution by using SetSharedFunction, suggesting that this resolves the issue with the code.
  • Another participant acknowledges the solution but suggests considering alternative code structures that may not require shared functions, and raises concerns about potential performance impacts when using shared variables in parallel computations.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and impact of using shared functions in the code. While one participant presents a solution, others question the implications for performance and explore alternative approaches.

Contextual Notes

There are unresolved issues regarding the performance of parallel computations with shared functions and variables, as well as the specific conditions under which the proposed solutions work.

eleteroboltz
Messages
7
Reaction score
0
I have a program with Mathematica and I would like to run it in parallel.
The essential of the code is actually run a lot of cases, changing some parameters.
In other words, it means that each case is totally independent of the other.
But I'm not quite sure about how to do it.

To exemplify this situation, let's see the sample code bellow.

Code:
fcalc[i_, j_] := NIntegrate[i Cos[j x], {x, 0, 1}]

ParallelDo[
 f[i, j] = fcalc[i, j]
 , {i, 1, 10}
 , {j, 1, 10}]

The problem is that the values are not passing to f.

Any suggestions?

Thank you
 
Physics news on Phys.org
In
http://reference.wolfram.com/mathematica/ref/ParallelDo.html
under "Possible Issues" it says
"A function used that is not known on the parallel kernels has no effect:"
and then it describes using DistributeDefinitions[].

Perhaps reading every tiny detail of the documentation of ParallelDo may turn up other things you need to know.
 
Bill Simpson said:
In
http://reference.wolfram.com/mathematica/ref/ParallelDo.html
under "Possible Issues" it says
"A function used that is not known on the parallel kernels has no effect:"
and then it describes using DistributeDefinitions[].

Perhaps reading every tiny detail of the documentation of ParallelDo may turn up other things you need to know.


Thank you for the answer, Bill.
I tried to use DistributeDefinitions[] but it's still not working for me.

Code:
fcalc[i_, j_] := NIntegrate[i Cos[j x], {x, 0, 1}]

DistributeDefinitions[f]

ParallelDo[
 f[i, j] = fcalc[i, j]
 , {i, 1, 10}
 , {j, 1, 10}]
 
I found the solution for this problem.
The correct code is shown bellow:

Code:
fcalc[i_, j_] := NIntegrate[i Cos[j x], {x, 0, 1}]

SetSharedFunction[f]

ParallelDo[
 f[i, j] = fcalc[i, j]
 , {i, 1, 10}
 , {j, 1, 10}]
 
I'm glad that reading the documentation allowed you to get the code working.

I understand that the way you have written the code may require shared functions, but perhaps you can find a way to rewrite the code so that is not needed, just for a test.

If performance is important then you might see if you can get a stopwatch and find a way to measure the performance with and without the SetSharedFuction[].

It may or may not apply with shared functions, but someone reported using shared variables with parallel resulted in an order of magnitude slower performance.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K