def get_image_size( imgdata, myid ): from mpi import mpi_gather, mpi_bcast, MPI_COMM_WORLD, MPI_INT nimg = len(imgdata) nimgs = mpi_gather( nimg, 1, MPI_INT, 1, MPI_INT, 0, MPI_COMM_WORLD ) if myid==0: src = -1 for i in xrange( len(nimgs) ): if int(nimgs[i]) > 0 : src = i break if src==-1: return 0 else: src = -1 size_src = mpi_bcast( src, 1, MPI_INT, 0, MPI_COMM_WORLD ) if myid==int(size_src[0]): assert nimg > 0 size = imgdata[0].get_xsize() else: size = -1 nx = mpi_bcast( size, 1, MPI_INT, size_src[0], MPI_COMM_WORLD ) return int(nx[0])
def get_image_size(imgdata, myid): from mpi import mpi_gather, mpi_bcast, MPI_COMM_WORLD, MPI_INT nimg = len(imgdata) nimgs = mpi_gather(nimg, 1, MPI_INT, 1, MPI_INT, 0, MPI_COMM_WORLD) if myid == 0: src = -1 for i in xrange(len(nimgs)): if int(nimgs[i]) > 0: src = i break if src == -1: return 0 else: src = -1 size_src = mpi_bcast(src, 1, MPI_INT, 0, MPI_COMM_WORLD) if myid == int(size_src[0]): assert nimg > 0 size = imgdata[0].get_xsize() else: size = -1 nx = mpi_bcast(size, 1, MPI_INT, size_src[0], MPI_COMM_WORLD) return int(nx[0])
count = 4 # in python we do not need to preallocate the array myray # we do need to assign a dummy value to the send_ray send_ray = zeros(0, "i") if myid == mpi_root: size = count * numnodes send_ray = zeros(size, "i") for i in range(0, size): send_ray[i] = i #send different data to each processor myray = mpi.mpi_scatter(send_ray, count, mpi.MPI_INT, count, mpi.MPI_INT, mpi_root, mpi.MPI_COMM_WORLD) #each processor does a local sum total = 0 for i in range(0, count): total = total + myray[i] print "myid=", myid, "total=", total #gather back to the root and sum/print back_ray = mpi.mpi_gather(total, 1, mpi.MPI_INT, 1, mpi.MPI_INT, mpi_root, mpi.MPI_COMM_WORLD) if myid == mpi_root: total = 0 for i in range(0, numnodes): total = total + back_ray[i] print "results from all processors=", total mpi.mpi_finalize()
mpi_root = 0 myid = mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD) numnodes = mpi.mpi_comm_size(mpi.MPI_COMM_WORLD) print "hello from ", myid, " of ", numnodes mysize = myid + 1 myray = zeros(mysize, "i") for i in range(0, mysize): myray[i] = myid + 1 if myid == mpi_root: displacements = zeros(numnodes, "i") else: displacements = zeros(0, "i") #myray[0] contains the number of values we will send in the gatherv counts = mpi.mpi_gather(myray[0], 1, mpi.MPI_INT, 1, mpi.MPI_INT, mpi_root, mpi.MPI_COMM_WORLD) if myid == mpi_root: displacements[0] = 0 for i in range(1, numnodes): displacements[i] = counts[i - 1] + displacements[i - 1] size = 0 for i in range(0, numnodes): size = size + counts[i] allray = mpi.mpi_gatherv(myray, mysize, mpi.MPI_INT, counts, displacements, mpi.MPI_INT, mpi_root, mpi.MPI_COMM_WORLD) if myid == mpi_root: print "allrray= ", allray
import random #print "before",len(sys.argv),sys.argv sys.argv = mpi.mpi_init(len(sys.argv),sys.argv) #print "after ",len(sys.argv),sys.argv mpi_root=0 myid=mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD) numnodes=mpi.mpi_comm_size(mpi.MPI_COMM_WORLD) print "hello from ",myid," of ",numnodes mysize=myid+1 #mysize contains the number of values we will send in the scatterv counts=mpi.mpi_gather(mysize,1,mpi.MPI_INT,1,mpi.MPI_INT, mpi_root,mpi.MPI_COMM_WORLD) if myid != mpi_root: displacements=zeros(0,"i") sray=zeros(0,"i") if myid == mpi_root: print "counts=",counts displacements=zeros(numnodes,"i") displacements[0]=0 for i in range(1, numnodes): displacements[i]=counts[i-1]+displacements[i-1] size=0 for i in range(0, numnodes): size=size+counts[i] sray=zeros(size,"i") for i in range(0, size):
count = 4 # in python we do not need to preallocate the array myray # we do need to assign a dummy value to the send_ray send_ray = zeros(0, "d") if myid == mpi_root: size = count * numnodes send_ray = zeros(size, "d") for i in range(0, size): send_ray[i] = i #send different data to each processor myray = mpi.mpi_scatter(send_ray, count, mpi.MPI_DOUBLE, count, mpi.MPI_DOUBLE, mpi_root, mpi.MPI_COMM_WORLD) #each processor does a local sum total = 0.0 for i in range(0, count): total = total + myray[i] print "myid=", myid, "total=", total #gather back to the root and sum/print back_ray = mpi.mpi_gather(total, 1, mpi.MPI_DOUBLE, 1, mpi.MPI_DOUBLE, mpi_root, mpi.MPI_COMM_WORLD) if myid == mpi_root: total = 0.0 for i in range(0, numnodes): total = total + back_ray[i] print "results from all processors=", total mpi.mpi_finalize()