Simple Parallelization with Mathematica

  • Mathematica
  • Thread starter eleteroboltz
  • Start date
  • Tags
    Mathematica
In summary, the conversation involved a user who needed help running a Mathematica program in parallel. The program involved running multiple cases with different parameters, but the user was unsure how to do it. They shared a sample code and asked for suggestions, to which the expert responded with a solution involving using DistributeDefinitions[] and SetSharedFunction[]. The expert also suggested reading the documentation for ParallelDo to find any other necessary details. The conversation ended with the expert recommending a performance test to see if SetSharedFunction[] is necessary for the code.
  • #1
eleteroboltz
7
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
  • #2
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.
 
  • #3
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}]
 
  • #4
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}]
 
  • #5
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.
 

Related to Simple Parallelization with Mathematica

1. What is parallelization and why is it important?

Parallelization is the process of breaking down a large task into smaller subtasks that can be executed simultaneously on multiple processors. This allows for faster and more efficient computation, as the workload is divided among multiple processors. It is important because it can significantly reduce the time it takes to complete complex calculations and improve the overall performance of a system.

2. How does Mathematica support parallelization?

Mathematica has built-in functions and tools that allow for easy parallelization of tasks. These include Parallelize, ParallelTable, and ParallelMap, which distribute the workload among different processors. It also has support for multi-core processors and clusters, allowing for parallel computations on a larger scale.

3. Can any task be parallelized with Mathematica?

No, not all tasks can be parallelized. Parallelization is most effective for tasks that can be broken down into smaller, independent subtasks. Tasks that are sequential or require a lot of communication between subtasks may not benefit from parallelization. It is important to carefully consider the nature of the task before attempting to parallelize it.

4. How can I check if my computation is running in parallel?

You can use the $ProcessorCount and $KernelCount variables to check the number of processors and kernels being used in your computation. If these numbers are higher than 1, then your computation is running in parallel. You can also use the Parallelize function to explicitly specify the number of processors you want to use for a particular task.

5. Are there any limitations or drawbacks to parallelization?

There are a few limitations to parallelization, including the overhead involved in distributing and synchronizing the subtasks, which can sometimes outweigh the benefits. Additionally, not all tasks can be easily parallelized, as mentioned before. It is also important to note that parallelization does not always guarantee faster computation, as it depends on the nature of the task and the available resources. Careful consideration and testing is necessary to determine if parallelization is the best approach for a particular task.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
419
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
692
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
596
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
34
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top