#!/usr/bin/env python

# If numpy is loaded without being used, it does not work !
from math import exp
from random import random
# Multithread library call
from multiprocessing import Pool

def MainLoop(iterations):

    total=0
    for i in xrange(iterations):
        # Random access coordonate
        x,y=random(),random()

        if ((x*x+y*y) < 1.0):
                total+=1

    return(total)

if __name__=='__main__':

   # Machine
   CORES=4

   # Au dessus de 4 10^7, le MPI ne veut plus se lancer...
   Iterations=1000000000

   total=0

   # Define iterations to send to each node
   if Iterations%CORES==0:
	   iterations=Iterations/CORES
   else:
	   iterations=Iterations/CORES+1
   print "%i iterations will be send to each core" % iterations

   IT=[]
   for i in range(0,CORES):
	   IT.append(iterations)

   print IT

   # Define the number of processes to be launched at a time
   # POOL: resources to be used ( 
   #pool=Pool(processes=CORES)
   pool=Pool(CORES)
   print "Start on %i processors..." % CORES

   # MAP: distribution of usecases T to be applied to MetropolisStrip 
   Results=[ pool.apply_async(MainLoop,(i,)) for i in IT]

   results=[r.get() for r in Results]
   print results,sum(results)
