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()
結果は [ 3.14179277]
お試しあれ。
Tweet
0 件のコメント :
コメントを投稿