参考サイトは
http://satoshi.blogs.com/life/2009/11/python%E5%85%A5%E9%96%80%E3%83%87%E3%82%B3%E3%83%AC%E3%83%BC%E3%82%BF%E3%81%A8%E3%81%AF.html
Tweet
def f(x):
return 4.0/(1.0+x*x)
def main():
import numpy
import mpi
from mpi import mpi_init, mpi_finalize
from mpi import mpi_comm_size, mpi_comm_rank, MPI_COMM_WORLD
from mpi import mpi_reduce, mpi_bcast, mpi_barrier,mpi_recv, mpi_send
from mpi import MPI_SUM, MPI_FLOAT,MPI_INT,MPI_TAG_UB
import sys
sys.argv = mpi_init(len(sys.argv),sys.argv)
number_of_proc = mpi_comm_size(MPI_COMM_WORLD)
myid = mpi_comm_rank(MPI_COMM_WORLD)
main_node = 0
n = 10000
#mpi_bcast(n, 1, MPI_INT, 0, MPI_COMM_WORLD)
h = 1.0 / n
sum = 0.0
for i in range(myid,n,number_of_proc):
x = h * (i -0.5)
sum += f(x)
mypi = h * sum
#mpi_barrier(MPI_COMM_WORLD)
cpi = mpi_reduce(mypi, 1, MPI_FLOAT,MPI_SUM, 0, MPI_COMM_WORLD)
if myid == main_node:
print cpi
mpi_finalize()
if __name__=="__main__":
main()
import numpy from numpy import * import mpi from mpi import * import sys sys.argv = mpi.mpi_init(len(sys.argv),sys.argv) print "after ",len(sys.argv),sys.argv myid=mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD) numprocs=mpi.mpi_comm_size(mpi.MPI_COMM_WORLD) print "Hello from ",myid print "Numprocs is ", numprocs print "python is not about snakes" mpi.mpi_finalize()
server1@~/test_mympi> mpirun --hostfile hosts -np 10 python example1.py
[puzzle15:26782] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle11:27440] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle15:26783] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle12:23530] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle11:27439] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle11:27441] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle11:27442] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle12:23529] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle12:23531] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
[puzzle12:23532] mca: base: component_find: unable to open osc pt2pt: file not found (ignored)
after 1 ('example1.py',)
Hello from 6
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 8
after 1 ('example1.py',)
Hello from 2
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 0
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 3
Numprocs is 10
python is not about snakes
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 9
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 4
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 5
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 7
Numprocs is 10
python is not about snakes
after 1 ('example1.py',)
Hello from 1
Numprocs is 10
python is not about snakes
setenv LD_LIBRARY_PATH $HOME/openmpi/libとパスを通しておく。
pythonとしてうまく行けばオッケー。
import mpi
make distclean
autoreconf --force --install
./configure --enable-shared --prefix=/home/openmpi/
make
make install
../../libtool: line 463: CDPATH: command not found
../../libtool: line 1266: func_opt_split: command not found
libtool: Version mismatch error. This is libtool 2.1a, but the
libtool: definition of this LT_INIT comes from an older release.
libtool: You should recreate aclocal.m4 with macros from libtool 2.1a
libtool: and run autoconf again.
tar jxf openmpi-1.2.8.tar.bz2なんと通った。
cd openmpi-1.2.8
./configure --enable-shared --prefix=/home/openmpi/
make all install
#!/usr/bin/env python
from mpi4py import MPI
import numpy
import sys
comm = MPI.COMM_SELF.Spawn(sys.executable,
args=['cpi.py'],
maxprocs=5)
N = numpy.array(100, 'i')
comm.Bcast([N, MPI.INT], root=MPI.ROOT)
PI = numpy.array(0.0, 'd')
comm.Reduce(None, [PI, MPI.DOUBLE],
op=MPI.SUM, root=MPI.ROOT)
print(PI)
comm.Disconnect()
#!/usr/bin/env python
from mpi4py import MPI
import numpy
comm = MPI.Comm.Get_parent()
size = comm.Get_size()
rank = comm.Get_rank()
N = numpy.array(0, dtype='i')
comm.Bcast([N, MPI.INT], root=0)
h = 1.0 / N; s = 0.0
for i in range(rank, N, size):
x = h * (i + 0.5)
s += 4.0 / (1.0 + x**2)
PI = numpy.array(s * h, dtype='d')
comm.Reduce([PI, MPI.DOUBLE], None,
op=MPI.SUM, root=0)
comm.Disconnect()
#!/usr/bin/env python """ Parallel Hello World """ from mpi4py import MPI import sys size = MPI.COMM_WORLD.Get_size() rank = MPI.COMM_WORLD.Get_rank() name = MPI.Get_processor_name() print "Hello, World! I am process %d of %d on %s." % (rank, size, name)