root / Pi / MP / PiMP.py.orig @ 39
Historique | Voir | Annoter | Télécharger (1,36 ko)
| 1 |
#!/usr/bin/env python |
|---|---|
| 2 |
|
| 3 |
# If numpy is loaded without being used, it does not work ! |
| 4 |
from math import exp |
| 5 |
from random import random |
| 6 |
# Multithread library call |
| 7 |
#import multiprocessing |
| 8 |
from multiprocessing import Pool |
| 9 |
import numpy |
| 10 |
from numpy.random import rand |
| 11 |
|
| 12 |
|
| 13 |
def MainLoop(iterations): |
| 14 |
|
| 15 |
total=numpy.int(0) |
| 16 |
for i in range(0,iterations): |
| 17 |
# Random access coordonate |
| 18 |
#x,y=random(),random() |
| 19 |
x,y=rand(),rand() |
| 20 |
|
| 21 |
if ((x*x+y*y) < 1.0): |
| 22 |
total+=1 |
| 23 |
|
| 24 |
return(total) |
| 25 |
|
| 26 |
if __name__=='__main__': |
| 27 |
|
| 28 |
# Machine |
| 29 |
CORES=4 |
| 30 |
|
| 31 |
# A=numpy.zeros(10) |
| 32 |
|
| 33 |
# Au dessus de 4 10^7, le MPI ne veut plus se lancer... |
| 34 |
Iterations=100000000 |
| 35 |
|
| 36 |
total=0 |
| 37 |
|
| 38 |
# Define iterations to send to each node |
| 39 |
if Iterations%CORES==0: |
| 40 |
iterations=Iterations/CORES |
| 41 |
else: |
| 42 |
iterations=Iterations/CORES+1 |
| 43 |
print "%i iterations will be send to each core" % iterations |
| 44 |
|
| 45 |
IT=[] |
| 46 |
for i in range(0,CORES): |
| 47 |
IT.append(iterations) |
| 48 |
|
| 49 |
print IT |
| 50 |
|
| 51 |
# Define the number of processes to be launched at a time |
| 52 |
# POOL: resources to be used ( |
| 53 |
# pool=Pool(processes=CORES) |
| 54 |
pool=Pool(processes=CORES) |
| 55 |
print "Start on %i processors..." % CORES |
| 56 |
|
| 57 |
# MAP: distribution of usecases T to be applied to MetropolisStrip |
| 58 |
Results=pool.map(MainLoop,IT) |
| 59 |
|
| 60 |
# print sum(Results.get()) |
| 61 |
print Results |