root / Pi / MP / PiMP.py.orig2 @ 157
Historique | Voir | Annoter | Télécharger (1,2 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 |
from multiprocessing import Pool |
8 |
|
9 |
def MainLoop(iterations): |
10 |
|
11 |
total=0 |
12 |
for i in xrange(iterations): |
13 |
# Random access coordonate |
14 |
x,y=random(),random() |
15 |
|
16 |
if ((x*x+y*y) < 1.0): |
17 |
total+=1 |
18 |
|
19 |
return(total) |
20 |
|
21 |
if __name__=='__main__': |
22 |
|
23 |
# Machine |
24 |
CORES=4 |
25 |
|
26 |
# Au dessus de 4 10^7, le MPI ne veut plus se lancer... |
27 |
Iterations=1000000000 |
28 |
|
29 |
total=0 |
30 |
|
31 |
# Define iterations to send to each node |
32 |
if Iterations%CORES==0: |
33 |
iterations=Iterations/CORES |
34 |
else: |
35 |
iterations=Iterations/CORES+1 |
36 |
print "%i iterations will be send to each core" % iterations |
37 |
|
38 |
IT=[] |
39 |
for i in range(0,CORES): |
40 |
IT.append(iterations) |
41 |
|
42 |
print IT |
43 |
|
44 |
# Define the number of processes to be launched at a time |
45 |
# POOL: resources to be used ( |
46 |
#pool=Pool(processes=CORES) |
47 |
pool=Pool(CORES) |
48 |
print "Start on %i processors..." % CORES |
49 |
|
50 |
# MAP: distribution of usecases T to be applied to MetropolisStrip |
51 |
Results=[ pool.apply_async(MainLoop,(i,)) for i in IT] |
52 |
|
53 |
results=[r.get() for r in Results] |
54 |
print results,sum(results) |