New Reply

Simple Parallelization with Mathematica

 
Share Thread Thread Tools
Jun14-12, 01:53 PM   #1
 

Simple Parallelization with Mathematica


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
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Ants and carnivorous plants conspire for mutualistic feeding
>> Forecast for Titan: Wild weather could be ahead
>> Researchers stitch defects into the world's thinnest semiconductor
Jun14-12, 03:14 PM   #2
 
In
http://reference.wolfram.com/mathema...arallelDo.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.
Jun14-12, 03:35 PM   #3
 
Quote by Bill Simpson View Post
In
http://reference.wolfram.com/mathema...arallelDo.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}]
Jun14-12, 04:08 PM   #4
 

Simple Parallelization with Mathematica


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}]
Jun14-12, 11:21 PM   #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.
New Reply
Thread Tools


Similar Threads for: Simple Parallelization with Mathematica
Thread Forum Replies
Mathematica: a simple array Math & Science Software 2
Simple table in Mathematica Engineering, Comp Sci, & Technology Homework 1
Basic parallelization technique for computing ensemble's average Programming & Comp Sci 0
Simple Histogram using Mathematica - need help please General Math 2
Mathematica - (Very) Simple Question Math & Science Software 2