I m confused about the semantics of using mpi inside a python function
 
The inline [non-function] code fragment might look like this (ignoring random number initialisation)
------------------------------------------------------------------
# start simulation on 10 cpu's, where each cpu only does 1/10 of simulations
MCprice = 0.0
StPayoff  = 0.0
Sum = 0.0

for i in range(1,nSims/mpi.size):
    St = Spot
    for j in range(1,nSteps):
        MCprice = ltqnorm(rmpi.random())
        St = St * math.exp(Drift + vSqrdt * MCprice)
    StPayoff = CalcCallPayoff(St,Strike)
    Sum = Sum + StPayoff
 
GSum = mpi.allreduce(Sum,mpi.SUM)
if mpi.rank == 0:
    price = GSum/nSims
    pv = math.exp(-(rf*q/yr2))
    price = pv * price
------------------------------------------------------------------
The next obvious thing is to put it into a function e.g.
 
def CalculateMcPrice(nSims,nSteps, Spot, Strike, Drift , vSqrdt )
    # inside the function, same code exists, except at the end you have the return statement
    ...
    return price
 
but in mpi where is the mpi.allreduce statement placed?
 
Would it have to be something like:
 
GSum = mpi.allreduce(CalculateMcPrice(nSims,nSteps, Spot, Strike, Drift , vSqrdt ),mpi.SUM)
GPrice = GSum/nSims
 
or simply?
 
Local_price = CalculateMcPrice(nSims,nSteps, Spot, Strike, Drift , vSqrdt )
GSum = mpi.allreduce(Local_price, mpi.SUM)
GPrice = GSum/nSims
 
tks
 
Julian Cook